Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new obstacles and upgrade code to support new version of Keras #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions flat_game/carmunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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)))

8 changes: 4 additions & 4 deletions learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion playing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)