From 19245e952e9a65439fd5191be0b3eb495f5f69e2 Mon Sep 17 00:00:00 2001 From: Arthur Fortes Date: Sun, 20 Sep 2020 12:25:17 -0300 Subject: [PATCH] new features | keras compability --- flat_game/carmunk.py | 42 ++++++++++++++++++++++++++++++++++++------ learning.py | 8 ++++---- nn.py | 6 +++--- playing.py | 2 +- 4 files changed, 44 insertions(+), 14 deletions(-) diff --git a/flat_game/carmunk.py b/flat_game/carmunk.py index 7e1f725..691849b 100644 --- a/flat_game/carmunk.py +++ b/flat_game/carmunk.py @@ -59,18 +59,45 @@ def __init__(self): s.group = 1 s.collision_type = 1 s.color = THECOLORS['red'] + self.space.add(static) # Create some obstacles, semi-randomly. # We'll create three and they'll move around to prevent over-fitting. self.obstacles = [] - self.obstacles.append(self.create_obstacle(200, 350, 100)) - self.obstacles.append(self.create_obstacle(700, 200, 125)) + self.obstacles.append(self.create_obstacle(200, 350, 35)) + self.obstacles.append(self.create_obstacle(700, 200, 35)) + self.obstacles.append(self.create_obstacle(600, 600, 35)) self.obstacles.append(self.create_obstacle(600, 600, 35)) # Create a cat. self.create_cat() + self.add_wall() + + + def add_wall(self): + """ + Create the static bodies. + :return: None + """ + static_body = self.space.static_body + static_lines = [ + pymunk.Segment(static_body, (111.0, 280.0), (407.0, 246.0), 0.0), + pymunk.Segment(static_body, (407.0, 246.0), (407.0, 343.0), 0.0), + # pymunk.Segment(self.space.static_body, Vec2d(1000,85), Vec2d(550,85), 1), + # pymunk.Segment(self.space.static_body, Vec2d(550,85), Vec2d(550,400), 1), + # pymunk.Segment(self.space.static_body, Vec2d(55,50), Vec2d(55,550), 1), + # pymunk.Segment(self.space.static_body, Vec2d(55,550), Vec2d(400,550), 1) + ] + + for line in static_lines: + line.elasticity = 0.95 + line.friction = 0.9 + + self.space.add(static_lines) + + def create_obstacle(self, x, y, r): c_body = pymunk.Body(pymunk.inf, pymunk.inf) c_shape = pymunk.Circle(c_body, r) @@ -81,10 +108,11 @@ def create_obstacle(self, x, y, r): return c_body def create_cat(self): + inertia = pymunk.moment_for_circle(1, 0, 14, (0, 0)) self.cat_body = pymunk.Body(1, inertia) self.cat_body.position = 50, height - 100 - self.cat_shape = pymunk.Circle(self.cat_body, 30) + self.cat_shape = pymunk.Circle(self.cat_body, 25) self.cat_shape.color = THECOLORS["orange"] self.cat_shape.elasticity = 1.0 self.cat_shape.angle = 0.5 @@ -95,7 +123,7 @@ def create_car(self, x, y, r): inertia = pymunk.moment_for_circle(1, 0, 14, (0, 0)) self.car_body = pymunk.Body(1, inertia) self.car_body.position = x, y - self.car_shape = pymunk.Circle(self.car_body, 25) + self.car_shape = pymunk.Circle(self.car_body, 35) self.car_shape.color = THECOLORS["green"] self.car_shape.elasticity = 1.0 self.car_body.angle = r @@ -149,7 +177,7 @@ def frame_step(self, action): def move_obstacles(self): # Randomly move obstacles around. - for obstacle in self.obstacles: + for num, obstacle in enumerate(self.obstacles): speed = random.randint(1, 5) direction = Vec2d(1, 0).rotated(self.car_body.angle + random.randint(-2, 2)) obstacle.velocity = speed * direction @@ -176,7 +204,7 @@ def recover_from_crash(self, driving_direction): self.crashed = False for i in range(10): self.car_body.angle += .2 # Turn a little. - screen.fill(THECOLORS["grey7"]) # Red is scary! + screen.fill(THECOLORS["red"]) # Red is scary! draw(screen, self.space) self.space.step(1./10) if draw_screen: @@ -273,4 +301,6 @@ def get_track_or_not(self, reading): if __name__ == "__main__": game_state = GameState() while True: + # game_state.frame_step((0)) game_state.frame_step((random.randint(0, 2))) + diff --git a/learning.py b/learning.py index 33777e6..54f502e 100644 --- a/learning.py +++ b/learning.py @@ -15,9 +15,9 @@ def train_net(model, params): filename = params_to_filename(params) - observe = 1000 # Number of frames to observe before training. + observe = 5000 # Number of frames to observe before training. epsilon = 1 - train_frames = 100000 # Number of frames to play. + train_frames = 500000 # Number of frames to play. batchSize = params['batchSize'] buffer = params['buffer'] @@ -51,7 +51,7 @@ def train_net(model, params): else: # Get Q values for each action. qval = model.predict(state, batch_size=1) - action = (np.argmax(qval)) # best + action = (np.argmax(qval)) # best # Take action, observe new state and get our treat. reward, new_state = game_state.frame_step(action) @@ -76,7 +76,7 @@ def train_net(model, params): history = LossHistory() model.fit( X_train, y_train, batch_size=batchSize, - nb_epoch=1, verbose=0, callbacks=[history] + epochs=1, verbose=0, callbacks=[history] ) loss_log.append(history.losses) diff --git a/nn.py b/nn.py index 77a157e..939868f 100644 --- a/nn.py +++ b/nn.py @@ -23,18 +23,18 @@ def neural_net(num_sensors, params, load=''): # First layer. model.add(Dense( - params[0], init='lecun_uniform', input_shape=(num_sensors,) + params[0], kernel_initializer='random_uniform', input_shape=(num_sensors,) )) model.add(Activation('relu')) model.add(Dropout(0.2)) # Second layer. - model.add(Dense(params[1], init='lecun_uniform')) + model.add(Dense(params[1], kernel_initializer='random_uniform')) model.add(Activation('relu')) model.add(Dropout(0.2)) # Output layer. - model.add(Dense(3, init='lecun_uniform')) + model.add(Dense(3, kernel_initializer='random_uniform')) model.add(Activation('linear')) rms = RMSprop() diff --git a/playing.py b/playing.py index 5c573f4..d087a7c 100644 --- a/playing.py +++ b/playing.py @@ -33,6 +33,6 @@ def play(model): if __name__ == "__main__": - saved_model = 'saved-models/128-128-64-50000-50000.h5' + saved_model = 'saved-models/128-128-64-50000-100000.h5' model = neural_net(NUM_SENSORS, [128, 128], saved_model) play(model)