class MainCarousel(Carousel): pongLoopTick = None # Pong animation loop game = None # Pong game overview = None # Overview panel def __init__(self): # Create carousel - doesn't work so well in .kv Carousel.__init__(self, direction='right', loop='true', scroll_distance=80, scroll_timeout=100) self.overview = Overview() self.add_widget(self.overview) self.add_widget(Label(text='Hello World2')) self.game = PongGame() self.game.serve_ball() self.add_widget(self.game) def on_index(self, *args): slideIndex = args[1] print("slide #{}".format(args[1])) if 2 == slideIndex: self.pongLoopTick = Clock.schedule_interval( self.game.update, 1.0 / 60.0) elif None != self.pongLoopTick: Clock.unschedule(self.pongLoopTick) self.pongLookTick = None super(MainCarousel, self).on_index(self, args)
def build(self): game = PongGame() game.serve_ball() print("now playing") #target = partial(game.update,model = model(get_weights_from_encoded(final_best_individual)))).start() self.event = Clock.schedule_interval( partial(game.update, model=model(get_weights_from_encoded(trained_individual))), SPEED) return game
def play_against_boring_ai(self): # if nobody survived the last generation, generate a new population if len(self.population) == 0: print('\nCreating new population') self.strict_breeding = False self.games = [] self.generation = 0 while len(self.population) < self.population_size: game = PongGame() nn_paddle = NNPaddle(PongGame.window_width - 50, PongGame.window_height / 2, game.ball, game) nn_paddle.generation = self.generation game.paddle1 = nn_paddle # ai_2 = NNPaddle(50, PongGame.window_height / 2, ball, game) self.population.append(nn_paddle) self.games.append(game) for game in self.games: game.paddle2.ball = game.ball game.speed = self.cur_speed game.start_game() print(game.paddle1) self.cur_speed = game.speed else: print('\n=== Generation', self.generation, '===') for game in self.games: game.paddle2.ball = game.ball game.paddle2.score = 0 game.paddle1.score = 0 game.paddle1.reset(PongGame.window_width - 50, PongGame.window_height / 2, game.ball) game.speed = self.cur_speed game.start_game() print(game.paddle1) self.cur_speed = game.speed
class MainCarousel(Carousel): pongLoopTick = None # Pong animation loop game = None # Pong game overview = None # Overview panel is_idle = False def __init__(self, **kwargs): # Create carousel - doesn't work so well in .kv Carousel.__init__(self, direction='right', loop='true', scroll_distance=80, scroll_timeout=100, **kwargs) self.config = MyConfig() self.backlight = BacklightFactory.Make(self.config) self.overview = Overview() self.add_widget(self.overview) self.add_widget(Label(text='Hello World2')) self.game = PongGame() self.game.serve_ball() self.add_widget(self.game) self.idle_clock = Clock.schedule_once( self.on_idle, float(self.config.switch_off_time)) Window.bind(on_motion=self.on_motion) # Setup screen saver def on_motion(self, *args): # Switch back light back on print("On Motion") self.idle_clock.cancel() if (self.is_idle): self.is_idle = False self.backlight.switch_on() self.idle_clock = Clock.schedule_once( self.on_idle, float(self.config.switch_off_time)) def on_idle(self, dt): # Switch off back light power print("On Idle") self.backlight.switch_off() self.is_idle = True def on_index(self, *args): slideIndex = args[1] print("slide #{}".format(args[1])) if 2 == slideIndex: self.pongLoopTick = Clock.schedule_interval( self.game.update, 1.0 / 60.0) elif None != self.pongLoopTick: Clock.unschedule(self.pongLoopTick) self.pongLookTick = None super(MainCarousel, self).on_index(self, args)
def __init__(self): # Create carousel - doesn't work so well in .kv Carousel.__init__(self, direction='right', loop='true', scroll_distance=80, scroll_timeout=100) self.overview = Overview() self.add_widget(self.overview) self.add_widget(Label(text='Hello World2')) self.game = PongGame() self.game.serve_ball() self.add_widget(self.game)
def init_breeder(self, parent=None): # if there is no parent, create a new randomly generated population if parent is None: self.create_new_population() population = [] for game in self.games: # set the game up to play using the new paddles and ball game.paddle1.ball = game.ball game.paddle2.ball = game.ball game.speed = self.cur_speed # play the game and record the game speed multiplier to use for future games game.start_game() print(game.paddle1) self.cur_speed = game.speed # append the neural net to the population population.append(game.paddle1) # sort the population best -> worst self.population = sorted(population, key=lambda x: x.fitness, reverse=True) # if there is a parent, create a generation based off the parent's genes elif parent is not None: population = parent if type(parent) is list: self.generation = population[0].generation else: self.generation = parent.generation print(population, len(population)) # go through the population and assign paddles to games, balls to paddles, etc for p in population: game = PongGame() p.game = game p.ball = game.ball game.paddle1 = p self.games.append(game) for i in range(self.population_size - 1): if type(parent) is list and len(parent) > 1: population.append( self.crossover(random.choice(parent), random.choice(parent))) else: population.append(self.crossover(parent)) self.population = population self.generation = self.population[0].generation
def crossover(self, parent1, parent2=None): # use the parent as a skeleton to create the offspring, like adam and eve or something game = PongGame() offspring = copy.deepcopy(parent1) offspring.game = game offspring.ball = game.ball game.paddle1 = offspring self.games.append(game) # if no other parent, breed with some random if parent2 is None: mate = NNPaddle(PongGame.window_width - 50, PongGame.window_height / 2, offspring.game.ball, offspring.game) offspring.parents = [parent1, mate] else: offspring.parents = [parent1, parent2] mate = parent2 offspring.fitness = 0 offspring.contacts_ball = 0 offspring.generation = self.generation # perform crossover for each layer and synapse for layer in range(len(offspring.net.synapses)): for synapse in range(len(offspring.net.synapses[layer])): # if the laws of nature say it must be so, mutate the current synapse if random.uniform(0, 1) < self.mutation_rate: offspring.net.synapses[layer][ synapse].weight = self.mutate( offspring.net.synapses[layer][synapse].weight) else: if random.uniform(0, 1) > (1 / 3): offspring.net.synapses[layer][ synapse].weight = parent1.net.synapses[layer][ synapse].weight else: offspring.net.synapses[layer][ synapse].weight = mate.net.synapses[layer][ synapse].weight # crossover the parent's colors as well offspring.colors = [ parent1.colors[0], mate.colors[1], parent1.colors[2], mate.colors[3] ] f_name = random.choice(parent1.name.split()) l_name = random.choice(mate.name.split()) offspring.name = f_name + ' ' + l_name return offspring
class keyUpTest(unittest.TestCase): def setUp(self): """Call before every test case.""" self.pong = PongGame() self.pong.init() def tearDown(self): """Call after every test case.""" def testKeyUp(self): test_paddle1_vel = self.pong.paddle1_vel-self.pong.DELTA_VELOCITY test_paddle2_vel = self.pong.paddle2_vel assert self.pong.keyup("up") == (test_paddle1_vel, test_paddle2_vel), 'key_up() #1 does not provide the right return value' test_paddle1_vel = self.pong.paddle1_vel test_paddle2_vel = self.pong.paddle2_vel-self.pong.DELTA_VELOCITY assert self.pong.keyup("e") == (test_paddle1_vel, test_paddle2_vel), 'key_up() #2 does not provide the right return value'
class MainCarousel(Carousel): pongLoopTick = None # Pong animation loop game = None # Pong game overview = None # Overview panel is_idle = False def __init__(self, **kwargs): # Create carousel - doesn't work so well in .kv Carousel.__init__(self,direction='right',loop='true',scroll_distance=80,scroll_timeout=100,**kwargs) self.config = MyConfig() self.backlight = BacklightFactory.Make(self.config) self.overview = Overview() self.add_widget(self.overview) self.add_widget(Label(text='Hello World2')) self.game = PongGame() self.game.serve_ball() self.add_widget(self.game) self.idle_clock = Clock.schedule_once(self.on_idle, float(self.config.switch_off_time)) Window.bind(on_motion=self.on_motion) # Setup screen saver def on_motion(self, *args): # Switch back light back on print("On Motion") self.idle_clock.cancel() if (self.is_idle): self.is_idle = False self.backlight.switch_on() self.idle_clock = Clock.schedule_once(self.on_idle, float(self.config.switch_off_time)) def on_idle(self, dt): # Switch off back light power print("On Idle") self.backlight.switch_off() self.is_idle = True def on_index(self, *args): slideIndex = args[1] print ("slide #{}".format(args[1])) if 2 == slideIndex: self.pongLoopTick = Clock.schedule_interval(self.game.update, 1.0/60.0) elif None != self.pongLoopTick: Clock.unschedule(self.pongLoopTick) self.pongLookTick = None super(MainCarousel, self).on_index(self, args)
def __init__(self): # Create carousel - doesn't work so well in .kv Carousel.__init__(self,direction='right',loop='true',scroll_distance=80,scroll_timeout=100) self.overview = Overview() self.add_widget(self.overview) self.add_widget(Label(text='Hello World2')) self.game = PongGame() self.game.serve_ball() self.add_widget(self.game)
def __init__(self, **kwargs): # Create carousel - doesn't work so well in .kv Carousel.__init__(self, direction='right', loop='true', scroll_distance=80, scroll_timeout=100, **kwargs) self.config = MyConfig() self.backlight = BacklightFactory.Make(self.config) self.overview = Overview() self.add_widget(self.overview) self.add_widget(Label(text='Hello World2')) self.game = PongGame() self.game.serve_ball() self.add_widget(self.game) self.idle_clock = Clock.schedule_once( self.on_idle, float(self.config.switch_off_time)) Window.bind(on_motion=self.on_motion)
def create_new_population(self): print('\nCreating new population of size', self.population_size) self.generation = 0 self.train_each_other = False temp_population_size = self.population_size population = [] # create new games and neural net paddles equal to the population size for ndx in range(temp_population_size): game = PongGame() ai_1 = NNPaddle(PongGame.window_width - 50, PongGame.window_height / 2, game.ball, game) ai_1.generation = self.generation # ai_2 = NNPaddle(50, PongGame.window_height / 2, ball, game) game.paddle1 = ai_1 population.append(ai_1) # population.append(ai_2) self.games.append(game) self.population = population
class pongTest(unittest.TestCase): def setUp(self): """Call before every test case.""" self.pong = PongGame() def tearDown(self): """Call after every test case.""" def testGuessTheNumber(self): assert self.pong.main() == dummy(self), 'main() does not provide the right return value'
def arena_battle(parent): game = PongGame(True) if type(parent) is list: if len(parent) == 4: # use these parents to start an arena battle game.paddle1.load_genomes(parent[0].name) game.paddle2.load_genomes(parent[1].name) game.paddle3.load_genomes(parent[2].name) game.paddle4.load_genomes(parent[3].name) else: print('Must supply a list of four names OR a single name') return elif type(parent) is NNPaddle: print(parent) game.paddle1.load_genomes(parent.name) game.paddle2.load_genomes(parent.name) game.paddle3.load_genomes(parent.name) game.paddle4.load_genomes(parent.name) game.start_game()
def main(_): # initialize parameters DATA_DIR = "" NUM_ACTIONS = 3 # number of valid actions (left, stay, right) GAMMA = 0.99 # decay rate of past observations INITIAL_EPSILON = 0.1 # starting value of epsilon FINAL_EPSILON = 0.0001 # final value of epsilon MEMORY_SIZE = 50000 # number of previous transitions to remember NUM_EPOCHS_OBSERVE = 1 NUM_EPOCHS_TRAIN = 1000 BATCH_SIZE = 1 NUM_EPOCHS = NUM_EPOCHS_OBSERVE + NUM_EPOCHS_TRAIN model_file = "pong_net" if os.path.exists(model_file): # load the model pass else: # build the model pass pong_game = PongGame() experience = collections.deque(maxlen=MEMORY_SIZE) num_games, num_wins = 0, 0 epsilon = INITIAL_EPSILON for e in range(NUM_EPOCHS): loss = 0.0 pong_game.reset_game() # get first state a_0 = 1 # (0 = left, 1 = stay, 2 = right) x_t, r_0, game_over = pong_game.step(a_0) s_t = preprocess_frames(x_t) while not game_over: pass
class drawBallTest(unittest.TestCase): def setUp(self): """Call before every test case.""" self.pong = PongGame() self.pong.init() def tearDown(self): """Call after every test case.""" def testDrawBall(self): self.aCanvas = Canvas() test_paddle1_pos = 200.0 test_paddle2_pos = 150.0 test_paddle1_vel = self.pong.DEFAULT_VELOCITY test_paddle2_vel = self.pong.DEFAULT_VELOCITY ball_radius = self.pong.BALL_RADIUS ball_pos = [300,200] color = "Black" assert self.pong.draw_ball(self.aCanvas, ball_pos, color) == (ball_pos, ball_radius, color, color), 'draw_ball() does not provide the right return value' color = "White" assert self.pong.draw_ball(self.aCanvas, ball_pos, color) == (ball_pos, ball_radius, color, color), 'draw_ball() does not provide the right return value'
def __init__(self, **kwargs): # Create carousel - doesn't work so well in .kv Carousel.__init__(self,direction='right',loop='true',scroll_distance=80,scroll_timeout=100,**kwargs) self.config = MyConfig() self.backlight = BacklightFactory.Make(self.config) self.overview = Overview() self.add_widget(self.overview) self.add_widget(Label(text='Hello World2')) self.game = PongGame() self.game.serve_ball() self.add_widget(self.game) self.idle_clock = Clock.schedule_once(self.on_idle, float(self.config.switch_off_time)) Window.bind(on_motion=self.on_motion)
class ballInitTest(unittest.TestCase): def setUp(self): """Call before every test case.""" self.pong = PongGame() def tearDown(self): """Call after every test case.""" def testBallInit(self): right = None ball_pos = [300,200] ball_vel = [5, 5] assert self.pong.ball_init(right) == (ball_pos, ball_vel), 'ball_init() does not provide the right return value'
class MainCarousel(Carousel): pongLoopTick = None # Pong animation loop game = None # Pong game overview = None # Overview panel def __init__(self): # Create carousel - doesn't work so well in .kv Carousel.__init__(self,direction='right',loop='true',scroll_distance=80,scroll_timeout=100) self.overview = Overview() self.add_widget(self.overview) self.add_widget(Label(text='Hello World2')) self.game = PongGame() self.game.serve_ball() self.add_widget(self.game) def on_index(self, *args): slideIndex = args[1] print ("slide #{}".format(args[1])) if 2 == slideIndex: self.pongLoopTick = Clock.schedule_interval(self.game.update, 1.0/60.0) elif None != self.pongLoopTick: Clock.unschedule(self.pongLoopTick) self.pongLookTick = None super(MainCarousel, self).on_index(self, args)
class initTest(unittest.TestCase): def setUp(self): """Call before every test case.""" self.pong = PongGame() def tearDown(self): """Call after every test case.""" def testInit(self): test_paddle1_pos = 200.0 test_paddle2_pos = 150.0 test_paddle1_vel = 5.0 test_paddle2_vel = 5.0 test_score1 = 0 test_score2 = 0 assert self.pong.init() == (test_paddle1_pos, test_paddle2_pos, test_paddle1_vel, test_paddle2_vel, test_score1, test_score2), 'init() does not provide the right return value'
#!/usr/bin/python from __future__ import print_function from pong import PongGame def train(num_iteration): pass if __name__ == '__main__': pong_game = PongGame()
import numpy as np from pong import PongGame if __name__ == '__main__': env = PongGame(competitive = False) obs = env.reset() while 1: env.render(mode='human') obs, reward, dones, win = env.step([np.random.uniform(-1,1),np.random.uniform(-1,1)])
def setUp(self): """Call before every test case.""" self.pong = PongGame() self.pong.init()
def build(self): game = PongGame() game.serve_ball() threading.Thread(target=partial(main_function, "", game)).start() return game