This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| import pygame | |
| screen_size = 512 | |
| background = None | |
| class Game: | |
| def __init__(self, board_size): | |
| self.board_size = board_size | |
| self.clear_board() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| plt.plot(rList) | |
| plt.plot(jList) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: |
NewerOlder