Skip to content

Instantly share code, notes, and snippets.

View antigones's full-sized avatar
Doing some magic

Juna Salviati antigones

Doing some magic
View GitHub Profile
@antigones
antigones / main.py
Created January 17, 2023 20:14
main file
from wolf_goat_cabbage_qlearning import WolfGoatCabbageQLearning
def main():
wcg_arena = WolfGoatCabbageQLearning(
start_state=[['🥦', '🐐', '⛵', '🐺'], [], []],
goal_state=[[], [], ['⛵', '🐐', '🥦', '🐺']],
gamma=0.8,
max_episodes=1000,
epsilon_greedy=True)
class RLKey:
def __init__(self, k1, k2):
self.k1 = k1
self.k2 = k2
def __hash__(self):
return hash((tuple(frozenset(x) for x in self.k1),
tuple(frozenset(x) for x in self.k2)))
def __eq__(self, other):
def train(self):
convergence_count = 0
q_s_a = defaultdict(int)
q_s_a_prec = copy.deepcopy(q_s_a)
episode = 1
scores = []
eps_list = []
rewards = {}
for episode in range(1,self.max_episodes):
def move(self, state, what, where_from, where_to):
state[where_from].remove(what)
state[where_to].add(what)
def get_next_state(self, starting_state, action):
next_state = copy.deepcopy(starting_state)
if action == 'MOVE_PLAYER_FROM_LEFT_TO_BOAT':
if '⛵' in next_state[0]:
@antigones
antigones / wolf_goat_cabbage_qlearning.py
Created January 17, 2023 18:31
Class initialization
class WolfGoatCabbageQLearning:
def __init__(self, start_state, goal_state, gamma=0.8, max_episodes=50000,
epsilon_greedy=True, min_epsilon=0.1, max_epsilon=1.0):
self.start_state = tuple(set(x) for x in start_state)
self.goal_state = tuple(set(x) for x in goal_state)
self.gamma = gamma
self.max_episodes = max_episodes
@antigones
antigones / wolf_goat_cabbage_qlearning.py
Last active January 17, 2023 22:01
Reward function
def get_reward(self, state):
# GOAT and WOLF cannot be left unsupervised together
# GOAT and CABBAGE cannot be left unsupervised together
if state[2] == self.goal_state[2]:
return 100
if {'🐐', '🐺'} <= state[0] and '⛵' not in state[0]:
return -100
if {'🐐', '🥦'} <= state[0] and '⛵' not in state[0]:
return -100
if {'🐐', '🐺'} <= state[2] and '⛵' not in state[2]:
"""
Have yourself a merry coding Christmas...
"""
from num2words import num2words
def sing():
things_my_true_love_gave_to_me = [
(1, 'partridge in a pear tree'),
(2, 'turtle doves'),
@antigones
antigones / index.html
Created March 10, 2021 08:09
The late night tinkering projects #15: "Rock, paper, scissors!" - Page callback
Leap.loop(controllerOptions, function(frame) {
// Body of callback function
var result = "";
var extendedFingers = 0;
if (frame.hands.length > 0) {
var hand = frame.hands[0];
for(var f = 0; f < hand.fingers.length; f++){
var finger = hand.fingers[f];
if (finger.extended) extendedFingers++;
}
@antigones
antigones / gist:4890b4622c421f66c095a2829065bcf3
Last active March 10, 2021 08:06
The late night tinkering projects #15: "Rock, paper, scissors!" - Page skeleton
<html>
<head>
<title>Leap Test</title>
<style type="text/css">
#out {
font-size: 60px;
}
</style>
@antigones
antigones / MainPage.xaml.cs
Created January 26, 2021 15:16
MagicBall - MainPage.xaml.cs with button action
using System;
using Xamarin.Forms;
namespace MagicBall
{
public partial class MainPage : ContentPage
{
readonly Prediction prediction = new Prediction();
public MainPage()
{