Skip to content

Instantly share code, notes, and snippets.

View ikbendewilliam's full-sized avatar
🤷‍♂️
coding I guess

William Verhaeghe ikbendewilliam

🤷‍♂️
coding I guess
View GitHub Profile
import 'dart:async';
import 'dart:math' as math;
import 'package:flutter/material.dart';
class SwipeCard extends StatefulWidget {
final Widget child;
final void Function(DragStartDetails details)? onSwipeStart;
board_size = 4
output_size = 4
learning_rate = 1e-3
maximum_discount = .99
random_action_threshold = 0.1
game_max_length = 1024
num_episodes = 8000
save_interval = 100
env = game.Game(board_size)
rlModel = model.RLModel(learning_rate=learning_rate)
...
def distance_to_apple(self, pos):
return ((pos[0] - self.apple[0]) ** 2 + (pos[1] - self.apple[1]) ** 2) ** (1 / 2)
def get_reward(self):
distance = self.distance_to_apple(self.snake[0])
last_distance = self.distance_to_apple(self.snake[1])
if last_distance > distance:
return 0
return -1
def train_single_step(self, state0, state1, a, reward, maximum_discount):
Q0 = self.predict(state0)
Q1 = np.argmax(self.predict(state1)[0])
Q0[0][a] = reward + maximum_discount * Q1
self.model.fit(np.array(state0).reshape(1, -1), Q0, epochs=1, verbose=0)
import tensorflow as tf
import pickle
import numpy as np
class RLModel:
def __init__(self, version = None, learning_rate = 1e-3):
if version is not None:
self.retrieveVariables(version)
else:
self.learning_rate = learning_rate
import pygame
from pygame.locals import QUIT, KEYDOWN, K_ESCAPE, K_UP, K_DOWN, K_LEFT, K_RIGHT, K_r
import game
import model
import numpy as np
version = 'newModel'
board_size = 4
screen_size = 512
block_size = screen_size / board_size
import random
import pygame
screen_size = 512
background = None
class Game:
def __init__(self, board_size):
self.board_size = board_size
self.clear_board()
@ikbendewilliam
ikbendewilliam / AIP0-training-graphs.py
Created January 31, 2021 21:44
Code for medium article about RL on Snake
plt.plot(rList)
plt.plot(jList)
@ikbendewilliam
ikbendewilliam / AIP0-training-step.py
Created January 31, 2021 21:43
Code for medium article about RL on Snake
j+=1
s = env.get_board()
a = np.argmax(rlModel.predict(s)[0])
if np.random.rand(1) < random_action_threshold:
a = env.random_action()
s1, reward, done = env.step(a)
rlModel.train_single_step(s, s1, a, reward, maximum_discount)
rAll += reward
if done:
break
@ikbendewilliam
ikbendewilliam / AIP0-training-loop.py
Created January 31, 2021 21:42
Code for medium article about RL on Snake
jList = []
rList = []
for i in range(num_episodes):
s = env.clear_board()
rAll = 0
d = False
j = 0
while j < game_max_length:
# Game step...
if i % save_interval == 0 and i > 0: