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

EL KYAL #59

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
10 changes: 10 additions & 0 deletions ElKyal/FlappyAgent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import numpy as np
from keras.models import Sequential, load_model

model = load_model("model.dqf")

def FlappyPolicy(state, screen):
q = model.predict(np.array(list(state.values())).reshape(1,len(state)))


return(np.argmax(q)*119)
Binary file added ElKyal/model.dqf
Binary file not shown.
84 changes: 84 additions & 0 deletions ElKyal/q_learn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@

from ple.games.flappybird import FlappyBird
from ple import PLE
from keras import optimizers
from keras.models import Sequential
from keras.layers.core import Dense, Activation
import numpy as np
import random


model = Sequential()


model.add(Dense(500, init='lecun_uniform', input_shape=(8,)))

model.add(Activation('relu'))
model.add(Dense(2, init='lecun_uniform'))
model.add(Activation('linear'))
model.compile(loss='mse', optimizer=optimizers.Adam(1e-4))


epochs = 28000
gamma = 0.99 # discount factor
epsilon = 0.7 # epsilon-greddy
batchSize = 256 # mini batch size

jeu = FlappyBird()
p= PLE(jeu, fps=30, frame_skip=1, num_steps=1,force_fps=True, display_screen=False)
p.init()

i=0

for i in range(epochs):
p.reset_game()
state = jeu.getGameState()
state = np.array(list(state.values()))
while(not jeu.game_over()):



qval = model.predict(state.reshape(1,len(state)), batch_size=batchSize) #Learn Q (Q-learning) / model initialise avant (neural-network)
if (random.random() < epsilon): # exploration exploitation strategy
action = np.random.randint(0,2)
# print(action)
else: #choose best action from Q(s,a) values
qval_av_action = [-9999]*2

for ac in range(0,2):
qval_av_action[ac] = qval[0][ac]
action = (np.argmax(qval_av_action))
#Take action, observe new state S'
#Observe reward
reward = p.act(119*action)
if reward == 1:
reaward = 1
elif reward == -5:
reward = -1000
new_state = jeu.getGameState()
new_state = np.array(list(new_state.values()))
# choose new reward values


#Get max_Q(S',a)
newQ = model.predict(new_state.reshape(1,len(state)), batch_size=batchSize)
maxQ = np.max(newQ)
y = np.zeros((1,2))
y[:] = qval[:]
if reward != -5: #non-terminal state
update = (reward + gamma * maxQ)
else:
update = reward
y[0][action] = update
print("Game #: %s" % (i,))
model.fit(state.reshape(1, len(state)), y, batch_size=batchSize, nb_epoch=3, verbose=0)
state = new_state


# update exploitation / exploration strategy
if epsilon > 0.2:
epsilon -= (1.0/epochs)



model.save("model.dqf")
2 changes: 1 addition & 1 deletion RandomBird/run.py → ElKyal/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
from FlappyAgent import FlappyPolicy

game = FlappyBird(graphics="fixed") # use "fancy" for full background, random bird color and random pipe color, use "fixed" (default) for black background and constant bird and pipe colors.
game = FlappyBird() # use "fancy" for full background, random bird color and random pipe color, use "fixed" (default) for black background and constant bird and pipe colors.
p = PLE(game, fps=30, frame_skip=1, num_steps=1, force_fps=False, display_screen=True)
# Note: if you want to see you agent act in real time, set force_fps to False. But don't use this setting for learning, just for display purposes.

Expand Down
9 changes: 0 additions & 9 deletions RandomBird/FlappyAgent.py

This file was deleted.