Skip to content

Commit d214f0d

Browse files
committedSep 16, 2018
Initial commit
0 parents  commit d214f0d

25 files changed

+32291
-0
lines changed
 

‎.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

‎.idea/SnakeGA.iml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/codeStyles/codeStyleConfig.xml

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/misc.xml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/workspace.xml

+733
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎DNQ_internet.py

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
from keras.models import Sequential
2+
from keras.layers import Dense
3+
from keras.optimizers import Adam
4+
import numpy as np
5+
import random
6+
7+
# action = 1,2,3,4
8+
# state = right_free, left_free, , position_food
9+
# Deep Q-learning Agent
10+
11+
12+
class DQNAgent:
13+
def state(self):
14+
15+
def __init__(self, state_size, action_size):
16+
self.state_size = state_size
17+
self.action_size = action_size
18+
self.memory = []
19+
self.gamma = 0.95 # discount rate
20+
self.epsilon = 1.0 # exploration rate
21+
self.epsilon_min = 0.01
22+
self.epsilon_decay = 0.995
23+
self.learning_rate = 0.001
24+
self.model = self._build_model()
25+
26+
def _build_model(self):
27+
# Neural Net for Deep-Q learning Model
28+
model = Sequential()
29+
model.add(Dense(24, input_dim=self.state_size, activation='relu'))
30+
model.add(Dense(24, activation='relu'))
31+
model.add(Dense(self.action_size, activation='linear'))
32+
model.compile(loss='mse',
33+
optimizer=Adam(lr=self.learning_rate))
34+
return model
35+
36+
def remember(self, state, action, reward, next_state, done):
37+
self.memory.append((state, action, reward, next_state, done))
38+
39+
def act(self, state):
40+
if np.random.rand() <= self.epsilon:
41+
return random.randrange(self.action_size)
42+
act_values = self.model.predict(state)
43+
return np.argmax(act_values[0]) # returns action
44+
45+
def replay(self, batch_size):
46+
minibatch = random.sample(self.memory, batch_size)
47+
for state, action, reward, next_state, done in minibatch:
48+
target = reward
49+
if not done:
50+
target = reward + self.gamma * \
51+
np.amax(self.model.predict(next_state)[0])
52+
target_f = self.model.predict(state)
53+
target_f[0][action] = target
54+
self.model.fit(state, target_f, epochs=1, verbose=0)
55+
if self.epsilon > self.epsilon_min:
56+
self.epsilon *= self.epsilon_decay
57+
58+
agent = DQNAgent(env)
59+
# Iterate the game
60+
for e in range(episodes):
61+
# reset state in the beginning of each game
62+
state = env.reset()
63+
state = np.reshape(state, [1, 4])
64+
# time_t represents each frame of the game
65+
# Our goal is to keep the pole upright as long as possible until score of 500
66+
# the more time_t the more score
67+
for time_t in range(500):
68+
# turn this on if you want to render
69+
# env.render()
70+
# Decide action
71+
action = agent.act(state)
72+
# Advance the game to the next frame based on the action.
73+
# Reward is 1 for every frame the pole survived
74+
next_state, reward, done, _ = env.step(action)
75+
next_state = np.reshape(next_state, [1, 4])
76+
# Remember the previous state, action, reward, and done
77+
agent.remember(state, action, rewrd, next_state, done)
78+
# make next_state the new current state for the next frame.
79+
state = next_state
80+
# done becomes True when the game ends
81+
# ex) The agent drops the pole
82+
if done:
83+
# print the score and break out of the loop
84+
print("episode: {}/{}, score: {}"
85+
.format(e, episodes, time_t))
86+
break
87+
# train the agent with the experience of the episode
88+
agent.replay(32)

‎DQN.py

+203
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
#import snakeClass
2+
import pygame
3+
from keras.models import Sequential,Model
4+
from keras.layers.core import Flatten, Dense, Dropout
5+
from keras.optimizers import RMSprop
6+
import random
7+
import numpy as np
8+
import pandas as pd
9+
import pygame
10+
from keras.models import Sequential,Model
11+
from keras.layers.core import Flatten, Dense, Dropout
12+
from keras.optimizers import RMSprop
13+
import random
14+
import numpy as np
15+
import pandas as pd
16+
import keras
17+
import keras.backend as K
18+
import copy
19+
20+
pd.set_option('display.max_columns', 500)
21+
22+
class DQNAgent(object):
23+
24+
def __init__(self):
25+
self.reward = 0
26+
self.gamma = 0.9
27+
self.dataframe = pd.DataFrame()
28+
self.short_memory = np.array([])
29+
self.agent_target = 1
30+
self.agent_predict = 0
31+
self.model = self.network()
32+
#self.model = self.network("weights.hdf5")
33+
self.epsilon = 3
34+
self.actual = []
35+
36+
37+
38+
def get_state(self, game, player, food):
39+
state = [
40+
player.position[-1][0] - 20 in player.position or player.position[-1][0] - 20 < 0, # danger left
41+
player.position[-1][0] - 40 in player.position or player.position[-1][0] - 40 < 0, # danger 2 left
42+
player.position[-1][0] + 20 in player.position or player.position[-1][0] + 20 > game.display_width, # danger right
43+
player.position[-1][0] + 40 in player.position or player.position[-1][0] + 40 > game.display_width, # danger 2 right
44+
player.position[-1][1] - 20 in player.position or player.position[-1][1] - 20 < 0, # danger up
45+
player.position[-1][-1] - 40 in player.position or player.position[-1][-1] - 40 < 0, # danger 2 up
46+
player.position[-1][1] + 20 in player.position or player.position[-1][1] + 20 > game.display_height, # danger down
47+
player.position[-1][-1] + 40 in player.position or player.position[-1][-1] + 40 > game.display_height,# danger 2 down
48+
player.x_change == -20, # move left
49+
player.x_change == 20, # move right
50+
player.y_change == -20, # move up
51+
player.y_change == 20, # move down
52+
food.x_food < player.x, # food left
53+
food.x_food > player.x, # food right
54+
food.y_food < player.y, # food up
55+
food.y_food > player.y # food down
56+
]
57+
58+
for i in range(len(state)):
59+
if state[i]:
60+
state[i]=1
61+
else:
62+
state[i]=0
63+
return np.asarray(state)
64+
65+
66+
def set_reward(self, game, player, food):
67+
if game.crash:
68+
self.reward = -10
69+
return self.reward
70+
if player.eaten:
71+
self.reward = 10
72+
elif (player.x_change < 0 and food.x_food < player.x) or (player.x_change > 0 and food.x_food > player.x) or (player.y_change < 0 and food.y_food < player.y) or (player.y_change > 0 and food.y_food > player.y):
73+
self.reward = 2
74+
else:
75+
self.reward = -1
76+
return self.reward
77+
78+
def possible_moves(self, player):
79+
if player.x_change == -20:
80+
return [0,2,3]
81+
elif player.x_change == 20:
82+
return [1,2,3]
83+
elif player.y_change == -20:
84+
return [0,1,2]
85+
elif player.y_change == 20:
86+
return [0,1,3]
87+
88+
def replay(self, game, player, food, actual):
89+
player.position = copy.deepcopy(actual[0])
90+
player.x, player.y, player.x_change, player.y_change, food.x_food, food.y_food, game.crash, player.eaten, player.food = actual[1:]
91+
92+
'''
93+
def next_state(self, game, player, food, i):
94+
actual = [player.position, player.x, player.y, player.x_change, player.y_change, food.x_food, food.y_food, game.crash, player.eaten]
95+
original_state = self.get_state(game, player, food)
96+
player.do_move(i, player.x, player.y, game, food)
97+
player.display_player(player.x, player.y,player.food,game,player)
98+
array = [original_state, i, self.set_reward(game, player), self.get_state(game, player, food)]
99+
pygame.time.wait(500)
100+
self.replay(game, player, food, actual)
101+
player.display_player(player.x, player.y, player.food, game, player)
102+
return array
103+
'''
104+
105+
def next_state(self, game, player, food, i):
106+
actual = [player.position, player.x, player.y, player.x_change, player.y_change, food.x_food, food.y_food, game.crash, player.eaten, player.food]
107+
original_state = self.get_state(game, player, food)
108+
player.do_move(i, player.x, player.y, game, food)
109+
player.display_player(player.x, player.y,player.food,game,player)
110+
array = [original_state, i, self.set_reward(game, player), self.get_state(game, player, food)]
111+
pygame.time.wait(500)
112+
self.replay(game, player, food, actual)
113+
player.display_player(player.x, player.y, player.food, game, player)
114+
return array
115+
116+
def loss(self, target, state, action):
117+
return K.mean(K.square(target - self.predict_q(self.model, state, action)), axis=-1)
118+
119+
def network(self,weights=None):
120+
model = Sequential()
121+
model.add(Dense(output_dim=50, activation='relu', input_dim=17))
122+
model.add(Dense(output_dim=50, activation='relu'))
123+
model.add(Dense(output_dim=1, activation='softmax'))
124+
opt = RMSprop(lr=0.025)
125+
model.compile(loss='mse', optimizer=opt)
126+
127+
if weights:
128+
model.load_weights(weights)
129+
# [self.loss(self.agent_target, self.agent_predict)]
130+
return model
131+
132+
def act(self, state):
133+
if random.random(0, 1) < self.epsilon:
134+
return random.randint(0, 4)
135+
else:
136+
return np.argmax(self.brain.predictOne(state))
137+
138+
def observe(self, sequence): # in (s, a, r, s_) format
139+
self.memory.add(sequence)
140+
141+
def q_parameter(self):
142+
q = self.reward + self.gamma * np.argmax(self.fit_q())
143+
144+
def predict_q(self, model, state, action):
145+
predictor = np.array([np.hstack(np.array([state, action]))])
146+
q = model.predict(predictor)
147+
return q
148+
149+
def train_q(self, storage, state, action):
150+
train = np.array([storage[:17]])
151+
test = np.array([storage[17]])
152+
self.model.compile(loss='mse', optimizer=RMSprop(lr=0.025))
153+
self.model.fit(train, test, epochs=1)
154+
155+
def train2_q(self,training, test):
156+
training = training.values
157+
test = test.values
158+
self.model.fit(training, test, epochs=1)
159+
160+
161+
def initialize_dataframe(self):
162+
state = [0]*16
163+
for i in range(16):
164+
state[i]= random.choice([0, 1])
165+
move = random.randint(1,4)
166+
reward = random.choice([-1, -10, 10])
167+
future_state = [0]*16
168+
for i in range(16):
169+
future_state[i] = random.choice([True, False])
170+
Q = 1
171+
array = [state, move, reward, future_state, Q]
172+
self.dataframe = self.dataframe.append([array])
173+
174+
def store_memory(self, state, action, q):
175+
self.short_memory = np.hstack(np.array([state, action, q]))
176+
#print(self.short_memory)
177+
178+
179+
180+
181+
182+
'''
183+
def run():
184+
pygame.init()
185+
while True:
186+
game = Game(400, 400)
187+
player1 = game.player
188+
food1 = game.food
189+
display(player1, food1, game)
190+
initial_move(player1,game,food1)
191+
display(player1, food1, game)
192+
pygame.time.wait(game.speed)
193+
while not game.crash:
194+
loop(player1, food1, game)
195+
display(player1, food1, game)
196+
pygame.time.wait(game.speed)
197+
198+
run()
199+
'''
200+
201+
202+
203+

‎__pycache__/DQN.cpython-36.pyc

5.76 KB
Binary file not shown.

‎__pycache__/snake.cpython-36.pyc

3.1 KB
Binary file not shown.

‎__pycache__/snakeClass.cpython-36.pyc

6.44 KB
Binary file not shown.
3.21 KB
Binary file not shown.

‎car.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import pygame
2+
3+
pygame.init()
4+
display_width= 800
5+
display_height = 800
6+
7+
black = (0,0,0)
8+
white = (255,255,255)
9+
red = (255,0,0)
10+
11+
gameDisplay = pygame.display.set_mode((display_width,display_height))
12+
pygame.display.set_caption('SnakeGen')
13+
clock = pygame.time.Clock()
14+
15+
img = cv2.imread('racecar.png')
16+
carImg = pygame.image.load(img)
17+
18+
19+
def car(x,y):
20+
gameDisplay.blit(carImg,(x,y))
21+
22+
23+
24+
def text_objects(text, font):
25+
textSurface = font.render(text, True, black)
26+
return textSurface, textSurface.get_rect()
27+
28+
29+
def message_display(text):
30+
largeText = pygame.font.Font('freesansbold.ttf',115)
31+
TextSurf, TextRect = text_objects(text, largeText)
32+
TextRect.center = ((display_width/2),(display_height/2))
33+
gameDisplay.blit(TextSurf, TextRect)
34+
35+
def crash():
36+
message_display('You Crashed')
37+
38+
39+
def game_loop():
40+
x = display_width * 0.45
41+
y = display_height * 0.5
42+
43+
x_change = 0
44+
y_change = 0
45+
gameExit = False
46+
47+
while not gameExit:
48+
for event in pygame.event.get():
49+
if event.type == pygame.QUIT:
50+
crashed = True
51+
52+
if event.type == pygame.KEYDOWN:
53+
if event.key == pygame.K_LEFT:
54+
x_change = -25
55+
elif event.key == pygame.K_RIGHT:
56+
x_change = 25
57+
elif event.key == pygame.K_UP:
58+
y_change = - 25
59+
elif event.key == pygame.K_DOWN:
60+
y_change = 25
61+
62+
if event.type == pygame.KEYUP:
63+
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_UP or event.key == pygame.K_DOWN:
64+
x_change = 0
65+
y_change = 0
66+
print(event)
67+
x = x + x_change
68+
y = y + y_change
69+
gameDisplay.fill(white)
70+
car(x,y)
71+
72+
if x < 0 or x > display_width:
73+
gameExit = True
74+
75+
if y == 50:
76+
crash()
77+
78+
pygame.display.update()
79+
clock.tick(60)
80+
81+
game_loop()
82+
pygame.quit()
83+
quit()

‎food.png

274 Bytes
Loading

‎genetic_game.py

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import snakeGenetic
2+
import keras
3+
import pygame
4+
from keras.models import Sequential
5+
from keras.layers.core import Flatten, Dense, Dropout
6+
from keras.models import load_model
7+
import pandas as pd
8+
import numpy as np
9+
10+
modello = load_model('snake8.h5')
11+
12+
df = pd.DataFrame()
13+
t=0
14+
while t<50:
15+
food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change = snakeGenetic.initialize()
16+
horizontal=1
17+
vertical=0
18+
if x_food < x:
19+
right_food = 0
20+
left_food = 1
21+
else:
22+
right_food = 1
23+
left_food = 0
24+
if y_food < y:
25+
up_food = 1
26+
down_food = 0
27+
else:
28+
up_food = 0
29+
down_food = 1
30+
if [(x + 20), y] not in position and (x + 20) < snakeGenetic.display_width:
31+
right_free = 1
32+
else:
33+
right_free = 0
34+
if [(x - 20), y] not in position and (x - 20) > 0:
35+
left_free = 1
36+
else:
37+
left_free = 0
38+
if [x, (y - 20)] not in position and (y - 20) > 0:
39+
up_free = 1
40+
else:
41+
up_free = 0
42+
if [x, (y + 20)] not in position and (y + 20) < snakeGenetic.display_height:
43+
down_free = 1
44+
else:
45+
down_free = 0
46+
food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change = snakeGenetic.loop_move(
47+
food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change, 1)
48+
49+
array2 = [right_food, left_food, up_food, down_food, right_free, left_free, up_free, down_free, vertical, horizontal]
50+
move=1
51+
while gameExit == False:
52+
move = [move]
53+
array1 = np.concatenate([array2,move], axis=0)
54+
if x_food < x:
55+
right_food = 0
56+
left_food = 1
57+
else:
58+
right_food = 1
59+
left_food = 0
60+
if y_food < y:
61+
up_food = 1
62+
down_food = 0
63+
else:
64+
up_food = 0
65+
down_food = 1
66+
if [(x + 20), y] not in position and (x + 20) < snakeGenetic.display_width:
67+
right_free = 1
68+
else:
69+
right_free = 0
70+
if [(x - 20), y] not in position and (x - 20) > 0:
71+
left_free = 1
72+
else:
73+
left_free = 0
74+
if [x, (y - 20)] not in position and (y - 20) > 0:
75+
up_free = 1
76+
else:
77+
up_free = 0
78+
if [x, (y + 20)] not in position and (y + 20) < snakeGenetic.display_height:
79+
down_free = 1
80+
else:
81+
down_free = 0
82+
83+
84+
array2 = [right_food, left_food, up_food, down_food, right_free, left_free, up_free, down_free, vertical, horizontal]
85+
86+
array = np.concatenate([array1,array2], axis = 0)
87+
array = np.array([array])
88+
pred = modello.predict(array)
89+
pos = np.where(np.amax(pred[0])==pred[0])
90+
print(pred)
91+
move_attempt =1+ pos[0][0]
92+
print(move_attempt)
93+
food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change = snakeGenetic.loop_move(food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change, move_attempt)
94+
if x_change == 20:
95+
move = 1
96+
elif x_change == -20:
97+
move = 2
98+
elif y_change == -20:
99+
move = 3
100+
elif y_change == 20:
101+
move = 4
102+
103+
t=t+1

‎neuralnet.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import keras
2+
from keras.models import Sequential,Model
3+
from keras.layers.core import Flatten, Dense, Dropout
4+
import numpy as np
5+
import pandas as pd
6+
7+
pd.set_option('display.max_columns', 500)
8+
9+
traintest=pd.read_csv('snakemove6.csv')
10+
x=traintest.drop(['3.1'], axis=1)
11+
#x=x.iloc[1:,:]
12+
y=traintest['3.1']
13+
14+
def network(weights_path=None):
15+
model = Sequential()
16+
model.add(Dense(21,input_dim = 21,activation='relu'))
17+
model.add(Dense(40, activation='relu'))
18+
model.add(Dense(4, activation='softmax'))
19+
20+
if weights_path:
21+
model.load_weights(weights_path)
22+
23+
return model
24+
25+
def fitness(x_food, x, y_food, y, food):
26+
score = (1-(np.sqrt((x_food-x)**2 + (y_food - y)**2))/566 + food)
27+
return score
28+
29+
model = network()
30+
31+
32+
x=x.values
33+
y=y-1
34+
y_onehot = keras.utils.to_categorical(y, num_classes=4)
35+
#print(y_onehot[:])
36+
lr_init = 0.0001
37+
optimizer = keras.optimizers.Adam(lr= lr_init, beta_1=0.9, beta_2=0.999, epsilon=1e-8)
38+
model.compile(loss= 'sparse_categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
39+
model.fit(x,y, epochs=20 ,validation_split=0.2)
40+
model.save('snake8.h5')
41+
42+
#snake5.h5 = 408 variables
43+
#snake6.h5 = 8 variables (big architecture)
44+
#snake7.h5 = 8 variables (small architecture)
45+
#snake8.h5 = 17 variables (small architecture)

‎racecar.png

5.46 KB
Loading

‎snake.py

+217
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
import pygame
2+
import cv2
3+
from random import randint
4+
import pandas as pd
5+
6+
7+
pygame.init()
8+
display_width = 400
9+
display_height = 400
10+
11+
black = (0, 0, 0)
12+
white = (255, 255, 255)
13+
14+
position = []
15+
16+
gameDisplay = pygame.display.set_mode((display_width,display_height))
17+
pygame.display.set_caption('SnakeGen')
18+
clock = pygame.time.Clock()
19+
20+
img = cv2.imread('ball.png')
21+
img = cv2.resize(img, (20, 20))
22+
cv2.imwrite('ball_sized.png',img)
23+
carImg = pygame.image.load('ball_sized.png')
24+
img = cv2.imread('pizza.png')
25+
img = cv2.resize(img, (20, 20))
26+
cv2.imwrite('pizza_sized.png',img)
27+
pizzaImg = pygame.image.load('pizza_sized.png')
28+
29+
30+
def car(x,y):
31+
gameDisplay.blit(carImg,(x, y))
32+
33+
34+
def text_objects(text, font):
35+
textSurface = font.render(text, True, black)
36+
return textSurface, textSurface.get_rect()
37+
38+
39+
def message_display(text):
40+
largeText = pygame.font.Font('freesansbold.ttf',30)
41+
TextSurf, TextRect = text_objects(text, largeText)
42+
TextRect.center = ((display_width/2),(display_height/2))
43+
gameDisplay.blit(TextSurf, TextRect)
44+
pygame.display.update()
45+
pygame.time.wait(1000)
46+
47+
48+
def crash():
49+
message_display('OPS SEI MORTO!')
50+
51+
def food_coord():
52+
x_rand = randint(20, display_width - 20)
53+
x_rand = x_rand - x_rand % 20
54+
y_rand = randint(20, display_width - 20)
55+
y_rand = y_rand - y_rand % 20
56+
return x_rand, y_rand
57+
58+
def display_food(x,y):
59+
gameDisplay.blit(pizzaImg, (x, y))
60+
61+
def initialize():
62+
position=[]
63+
x = 0.45 * display_width
64+
y = 0.5 * display_height
65+
x = x - x % 20
66+
y = y - y % 20
67+
68+
69+
position.append([x,y])
70+
71+
food_coord()
72+
x_food = food_coord()[0]
73+
y_food = food_coord()[1]
74+
return 1, False, True, False, False, 0, position, x, y, x_food, y_food, -20 ,0 , 1
75+
76+
def loop_move(food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change,move):
77+
78+
for event in pygame.event.get():
79+
if event.type == pygame.KEYDOWN:
80+
if event.key == pygame.K_LEFT and horizontal == False:
81+
x_change = -20
82+
y_change = 0
83+
vertical = False
84+
horizontal = True
85+
move=1
86+
elif event.key == pygame.K_RIGHT and horizontal == False:
87+
x_change = 20
88+
y_change = 0
89+
vertical = False
90+
horizontal = True
91+
move=2
92+
elif event.key == pygame.K_UP and vertical == False:
93+
y_change = - 20
94+
x_change = 0
95+
vertical = True
96+
horizontal = False
97+
move=3
98+
elif event.key == pygame.K_DOWN and vertical == False:
99+
y_change = 20
100+
x_change = 0
101+
vertical = True
102+
horizontal = False
103+
move=4
104+
105+
x = x + x_change
106+
y = y + y_change
107+
if (position[-1][0] != x or position[-1][1] != y):
108+
if [x,y] in position:
109+
crash()
110+
gameExit = True
111+
112+
if food>1:
113+
for i in range(0,food-1):
114+
position[i][0]= position[i+1][0]
115+
position[i][1] = position[i+1][1]
116+
position[-1][0] = x
117+
position[-1][1] = y
118+
119+
if eaten == True:
120+
food_coord()
121+
x_food = food_coord()[0]
122+
y_food = food_coord()[1]
123+
eaten = False
124+
125+
if x == x_food and y == y_food:
126+
eaten= True
127+
food = food + 1
128+
position.append([x,y])
129+
130+
gameDisplay.fill(white)
131+
display_food(x_food, y_food)
132+
133+
for i in range(food):
134+
print(len(position))
135+
x_temp=position[len(position)-1-i][0]
136+
y_temp=position[len(position)-1-i][1]
137+
car(x_temp, y_temp)
138+
pygame.time.wait(100)
139+
pygame.display.update()
140+
141+
142+
if x < 0 or x > display_width-20 or y<0 or y > display_height-20:
143+
crash()
144+
gameExit = True
145+
146+
clock.tick(100)
147+
return food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change,move
148+
149+
150+
151+
dataset=pd.DataFrame()
152+
t=0
153+
while t <10:
154+
food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change,move = initialize()
155+
while gameExit==False:
156+
#print(position)
157+
print('1',food)
158+
print('2',gameExit)
159+
print('3',horizontal)
160+
print('4',vertical)
161+
print('5',eaten)
162+
print('6',cont)
163+
print('7',position)
164+
print('8',x)
165+
print('9',y)
166+
print('10',x_food)
167+
print('11',x_change)
168+
print('12',y_change)
169+
170+
food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change,move= loop_move(food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change,move)
171+
172+
if x_food < x:
173+
right_food = 0
174+
left_food = 1
175+
else:
176+
right_food = 1
177+
left_food = 0
178+
if y_food < y:
179+
up_food = 1
180+
down_food = 0
181+
else:
182+
up_food = 0
183+
down_food = 1
184+
if [(x+20),y] not in position and (x+20)<display_width:
185+
right_free = 1
186+
else:
187+
right_free = 0
188+
if [(x-20),y] not in position and (x-20)>0:
189+
left_free = 1
190+
else:
191+
left_free=0
192+
if [x,(y-20)] not in position and (y-20)>0:
193+
up_free = 1
194+
else:
195+
up_free = 0
196+
if [x,(y+20)] not in position and (y+20)<display_height:
197+
down_free = 1
198+
else:
199+
down_free=0
200+
array = [right_food,left_food,up_food,down_food,right_free,left_free,up_free,down_free,move]
201+
print(array)
202+
203+
dataset = dataset.append([array])
204+
t=t+1
205+
206+
colnames=['right_food','left_food','up_food','down_food','right_free','left_free','up_free','down_free','move']
207+
dataset.to_csv(path_or_buf='snakemove2.csv',sep = ',',index=False,header=colnames)
208+
209+
pygame.quit()
210+
quit()
211+
212+
'''
213+
#Execute game
214+
game_loop()
215+
pygame.quit()
216+
quit()
217+
'''

‎snake8.h5

39.1 KB
Binary file not shown.

‎snakeBody.png

114 Bytes
Loading

‎snakeClass.py

+335
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,335 @@
1+
import pygame
2+
from random import randint
3+
from DQN import DQNAgent
4+
import numpy as np
5+
import copy
6+
7+
8+
class Game:
9+
10+
def __init__(self, display_width, display_height):
11+
pygame.display.set_caption('SnakeGen')
12+
self.display_width = display_width
13+
self.display_height = display_height
14+
self.gameDisplay = pygame.display.set_mode((display_width, display_height))
15+
self.crash = False
16+
self.player = Player(self)
17+
self.food = Food(self, self.player)
18+
self.speed = 1
19+
20+
21+
class Player(object):
22+
23+
def __init__(self, game):
24+
x = 0.45 * game.display_width
25+
y = 0.5 * game.display_height
26+
self.x = x - x % 20
27+
self.y = y - y % 20
28+
self.position = []
29+
self.position.append([self.x, self.y])
30+
self.food = 1
31+
self.eaten = False
32+
self.image = pygame.image.load('snakeBody.png')
33+
self.x_change = 20
34+
self.y_change = 0
35+
36+
def update_position(self, x, y,agent):
37+
if self.position[-1][0] != x or self.position[-1][1] != y:
38+
if self.food > 1:
39+
for i in range(0, self.food - 1):
40+
self.position[i][0], self.position[i][1] = self.position[i + 1]
41+
self.position[-1][0] = x
42+
self.position[-1][1] = y
43+
44+
def do_move(self, move, x, y, game, food,agent):
45+
move_array = [self.x_change, self.y_change]
46+
47+
if self.eaten:
48+
self.position.append([self.x, self.y])
49+
self.eaten = False
50+
self.food = self.food + 1
51+
if move == 0 and self.x_change == 0: # left
52+
move_array = [-20, 0]
53+
elif move == 1 and self.x_change == 0: # right
54+
move_array = [20, 0]
55+
elif move == 2 and self.y_change == 0: # top
56+
move_array = [0, -20]
57+
elif move == 3 and self.y_change == 0: # bottom
58+
move_array = [0, 20]
59+
self.x_change, self.y_change = move_array
60+
self.x = x + self.x_change
61+
self.y = y + self.y_change
62+
63+
#print(self.x_change, self.y_change, self.x, self.y, self.position)
64+
if self.x < 0 or self.x == game.display_width or self.y < 0 or self.y == game.display_height or [self.x, self.y] in self.position:
65+
game.crash = True
66+
eat(self, food, game)
67+
68+
self.update_position(self.x, self.y,agent)
69+
70+
71+
72+
73+
def display_player(self, x, y, food, game, player):
74+
self.position[-1][0] = x
75+
self.position[-1][1] = y
76+
77+
for i in range(food):
78+
x_temp, y_temp = self.position[len(self.position) - 1 - i]
79+
game.gameDisplay.blit(player.image, (x_temp, y_temp))
80+
update_screen()
81+
82+
83+
class Food(object):
84+
85+
def __init__(self, game, player):
86+
#self.x_food, self.y_food = self.food_coord(game, player)
87+
self.x_food = 240
88+
self.y_food = 200
89+
self.image = pygame.image.load('food.png')
90+
91+
def food_coord(self, game, player):
92+
x_rand = randint(20, game.display_width - 20)
93+
self.x_food = x_rand - x_rand % 20
94+
y_rand = randint(20, game.display_height - 20)
95+
self.y_food = y_rand - y_rand % 20
96+
if [self.x_food, self.y_food] not in player.position:
97+
return self.x_food, self.y_food
98+
else:
99+
self.food_coord(game,player)
100+
101+
def display_food(self, x, y, game):
102+
game.gameDisplay.blit(self.image, (x, y))
103+
update_screen()
104+
105+
106+
def eat(player, food, game):
107+
if player.x == food.x_food and player.y == food.y_food:
108+
food.food_coord(game, player)
109+
player.eaten = True
110+
111+
112+
def display(player, food, game):
113+
game.gameDisplay.fill((255, 255, 255))
114+
player.display_player(player.position[-1][0], player.position[-1][1], player.food, game, player)
115+
food.display_food(food.x_food, food.y_food, game)
116+
117+
118+
def update_screen():
119+
pygame.display.update()
120+
121+
def initial_move(player, game, food,agent):
122+
player.do_move(1, player.x, player.y, game, food,agent)
123+
124+
# def loop(player, food, game,agent):
125+
# move = 0
126+
# if food.x_food < player.x and [(player.x - 20), player.y] not in player.position and (player.x - 20) > 0:
127+
# move = 0
128+
#
129+
# elif food.x_food > player.x and [(player.x + 20), player.y] not in player.position and (
130+
# player.x + 20) < game.display_width:
131+
# move=1
132+
#
133+
# elif food.y_food < player.y and [player.x, (player.y - 20)] not in player.position and (player.y - 20) > 0:
134+
# move =2
135+
#
136+
# elif food.y_food > player.y and [player.x, (player.y + 20)] not in player.position and (
137+
# player.y + 20) < game.display_height:
138+
# move = 3
139+
#
140+
# elif [(player.x - 20), player.y] not in player.position and player.x_change == 0 and (player.x - 20) > 0:
141+
# move = 0
142+
#
143+
# elif [(player.x + 20), player.y] not in player.position and player.x_change == 0 and (
144+
# player.x + 20) < game.display_width:
145+
# move = 1
146+
#
147+
# elif [player.x, (player.y - 20)] not in player.position and player.y_change == 0 and (player.y - 20) > 0:
148+
# move = 2
149+
#
150+
# elif [player.x, (player.y + 20)] not in player.position and player.y_change == 0 and (
151+
# player.y + 20) < game.display_height:
152+
# move = 3
153+
#
154+
# else:
155+
# move = randint(1, 4)
156+
#
157+
# player.do_move(move, player.x, player.y, game, food,agent)
158+
159+
160+
def run():
161+
pygame.init()
162+
agent = DQNAgent()
163+
counter_games = 0
164+
while counter_games < 10:
165+
#Initialize game
166+
game = Game(400, 400)
167+
player1 = game.player
168+
food1 = game.food
169+
#agent.reward = 0
170+
#Initialize storage to train first network
171+
state1 = agent.get_state(game,player1,food1)
172+
action = 1
173+
agent.store_memory(state1, action, 1)
174+
agent.dataframe = agent.dataframe.append([np.hstack(np.array([state1, action, 1]))])
175+
# agent.dataframe = agent.dataframe.append([np.hstack(np.array([state1, 2, 3]))])
176+
# agent.dataframe = agent.dataframe.append([np.hstack(np.array([state1, 3, 5]))])
177+
agent.train2_q(agent.dataframe[agent.dataframe.columns[:17]], agent.dataframe[agent.dataframe.columns[17]])
178+
#Performn first move
179+
display(player1, food1, game)
180+
initial_move(player1, game, food1,agent)
181+
display(player1, food1, game)
182+
while not game.crash:
183+
state2 = agent.get_state(game,player1,food1)
184+
agent.actual = copy.deepcopy([player1.position, player1.x, player1.y, player1.x_change, player1.y_change, food1.x_food,
185+
food1.y_food,game.crash, player1.eaten, player1.food])
186+
primary_q_array = []
187+
if randint(0,10)<agent.epsilon:
188+
final_move=randint(0,3)
189+
else:
190+
for i in agent.possible_moves(player1):
191+
player1.eaten = False
192+
player1.do_move(i, player1.x, player1.y, game, food1,agent)
193+
if game.crash==True:
194+
primary_q_array.append(agent.set_reward(game,player1, food1))
195+
else:
196+
primary_q = agent.predict_q(agent.model, state2, i)
197+
primary_q_array.append(primary_q)
198+
agent.replay(game,player1,food1,agent.actual)
199+
predicted_q =max(primary_q_array)
200+
max_index = primary_q_array.index(predicted_q)
201+
final_move = agent.possible_moves(player1)[max_index]
202+
player1.do_move(final_move, player1.x, player1.y, game, food1,agent)
203+
temp_reward = agent.reward + agent.set_reward(game, player1, food1)
204+
print('REWARD: ', temp_reward)
205+
temp_state = agent.get_state(game, player1, food1)
206+
agent.actual = copy.deepcopy([player1.position, player1.x, player1.y, player1.x_change, player1.y_change, food1.x_food,
207+
food1.y_food,game.crash, player1.eaten, player1.food])
208+
secondary_q_array = []
209+
210+
if not game.crash:
211+
for j in agent.possible_moves(player1):
212+
player1.eaten = False
213+
player1.do_move(j, player1.x, player1.y, game, food1, agent)
214+
215+
if game.crash == True:
216+
secondary_q_array.append(agent.reward)
217+
print('CRASH', secondary_q_array)
218+
219+
else:
220+
221+
secondary_q = agent.predict_q(agent.model, temp_state, j)
222+
secondary_q_array.append(secondary_q)
223+
agent.replay(game, player1, food1, agent.actual)
224+
max_target_q = max(secondary_q_array)
225+
target_q = [temp_reward + agent.gamma * max_target_q]
226+
print('1', target_q)
227+
228+
else:
229+
target_q = temp_reward
230+
print('2', target_q)
231+
#agent.agent_target = target_q
232+
#agent.agent_predict = predicted_q
233+
#agent.store_memory(state2, final_move, target_q[0])
234+
print('3', state2)
235+
print('3', final_move)
236+
print('3', target_q[0])
237+
print('3', [np.hstack(np.array([state2, final_move, target_q[0][0]]))])
238+
agent.dataframe = agent.dataframe.append([np.hstack(np.array([state2, final_move, target_q[0][0]]))])
239+
agent.train2_q(agent.dataframe[agent.dataframe.columns[:17]], agent.dataframe[agent.dataframe.columns[17]])
240+
display(player1, food1, game)
241+
pygame.time.wait(game.speed)
242+
print('GAME: ',counter_games)
243+
counter_games = counter_games + 1
244+
245+
agent.model.save_weights('weights.hdf5')
246+
247+
248+
249+
# pygame.init()
250+
# agent = DQNAgent()
251+
# counter_games = 0
252+
# while counter_games < 10:
253+
# #Initialize game
254+
# game = Game(400, 400)
255+
# player1 = game.player
256+
# food1 = game.food
257+
# agent.reward = 0
258+
# #Initialize storage to train first network
259+
# state1 = agent.get_state(game,player1,food1)
260+
# action = 1
261+
# agent.store_memory(state1, action, 1)
262+
# agent.dataframe = agent.dataframe.append([np.hstack(np.array([state1, action, 1]))])
263+
# # agent.dataframe = agent.dataframe.append([np.hstack(np.array([state1, 2, 3]))])
264+
# # agent.dataframe = agent.dataframe.append([np.hstack(np.array([state1, 3, 5]))])
265+
# agent.train2_q(agent.dataframe[agent.dataframe.columns[:17]], agent.dataframe[agent.dataframe.columns[17]])
266+
# #Performn first move
267+
# display(player1, food1, game)
268+
# initial_move(player1, game, food1,agent)
269+
# display(player1, food1, game)
270+
# while not game.crash:
271+
# state2 = agent.get_state(game,player1,food1)
272+
# agent.actual = copy.deepcopy([player1.position, player1.x, player1.y, player1.x_change, player1.y_change, food1.x_food,
273+
# food1.y_food,game.crash, player1.eaten, player1.food])
274+
# primary_q_array = []
275+
# if randint(0,10)<agent.epsilon:
276+
# final_move=randint(0,3)
277+
# else:
278+
# for i in agent.possible_moves(player1):
279+
# player1.eaten = False
280+
# player1.do_move(i, player1.x, player1.y, game, food1,agent)
281+
# if game.crash==True:
282+
# primary_q_array.append(agent.set_reward(game,player1, food1))
283+
# else:
284+
# primary_q = agent.predict_q(agent.model, state2, i)
285+
# primary_q_array.append(primary_q)
286+
# agent.replay(game,player1,food1,agent.actual)
287+
# predicted_q =max(primary_q_array)
288+
# max_index = primary_q_array.index(predicted_q)
289+
# final_move = agent.possible_moves(player1)[max_index]
290+
# player1.do_move(final_move, player1.x, player1.y, game, food1,agent)
291+
# temp_reward = agent.reward + agent.set_reward(game, player1, food1)
292+
# print('REWARD: ', temp_reward)
293+
# temp_state = agent.get_state(game, player1, food1)
294+
# agent.actual = copy.deepcopy([player1.position, player1.x, player1.y, player1.x_change, player1.y_change, food1.x_food,
295+
# food1.y_food,game.crash, player1.eaten, player1.food])
296+
# secondary_q_array = []
297+
#
298+
# if not game.crash:
299+
# for j in agent.possible_moves(player1):
300+
# player1.eaten = False
301+
# player1.do_move(j, player1.x, player1.y, game, food1, agent)
302+
#
303+
# if game.crash == True:
304+
# secondary_q_array.append(agent.reward)
305+
# print('CRASH', secondary_q_array)
306+
#
307+
# else:
308+
#
309+
# secondary_q = agent.predict_q(agent.model, temp_state, j)
310+
# secondary_q_array.append(secondary_q)
311+
# agent.replay(game, player1, food1, agent.actual)
312+
# max_target_q = max(secondary_q_array)
313+
# target_q = [temp_reward + agent.gamma * max_target_q]
314+
# print('1', target_q)
315+
#
316+
# else:
317+
# target_q = temp_reward
318+
# print('2', target_q)
319+
# #agent.agent_target = target_q
320+
# #agent.agent_predict = predicted_q
321+
# #agent.store_memory(state2, final_move, target_q[0])
322+
# print('3', state2)
323+
# print('3', final_move)
324+
# print('3', target_q[0])
325+
# print('3', [np.hstack(np.array([state2, final_move, target_q[0][0]]))])
326+
# agent.dataframe = agent.dataframe.append([np.hstack(np.array([state2, final_move, target_q[0][0]]))])
327+
# agent.train2_q(agent.dataframe[agent.dataframe.columns[:17]], agent.dataframe[agent.dataframe.columns[17]])
328+
# display(player1, food1, game)
329+
# pygame.time.wait(game.speed)
330+
# print('GAME: ',counter_games)
331+
# counter_games = counter_games + 1
332+
#
333+
# agent.model.save_weights('weights.hdf5')
334+
335+
run()

‎snakeGenetic.py

+243
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
import pygame
2+
import cv2
3+
from random import randint
4+
import csv
5+
import numpy as np
6+
import pandas as pd
7+
8+
9+
pygame.init()
10+
display_width= 400
11+
display_height = 400
12+
13+
black = (0,0,0)
14+
white = (255,255,255)
15+
16+
position = []
17+
18+
gameDisplay = pygame.display.set_mode((display_width,display_height))
19+
pygame.display.set_caption('SnakeGen')
20+
clock = pygame.time.Clock()
21+
22+
23+
carImg = pygame.image.load('ball_sized2.png')
24+
pizzaImg = pygame.image.load('pizza_sized.png')
25+
26+
27+
def car(x,y):
28+
gameDisplay.blit(carImg,(x,y))
29+
30+
31+
def text_objects(text, font):
32+
textSurface = font.render(text, True, black)
33+
return textSurface, textSurface.get_rect()
34+
35+
36+
def message_display(text):
37+
largeText = pygame.font.Font('freesansbold.ttf',30)
38+
TextSurf, TextRect = text_objects(text, largeText)
39+
TextRect.center = ((display_width/2),(display_height/2))
40+
gameDisplay.blit(TextSurf, TextRect)
41+
#pygame.display.update()
42+
#pygame.time.wait(100)
43+
44+
45+
def crash():
46+
message_display('OPS SEI MORTO!')
47+
48+
def food_coord():
49+
x_rand = randint(20, display_width - 20)
50+
x_rand = x_rand - x_rand % 20
51+
y_rand = randint(20, display_width - 20)
52+
y_rand = y_rand - y_rand % 20
53+
return x_rand, y_rand
54+
55+
def display_food(x,y):
56+
gameDisplay.blit(pizzaImg, (x, y))
57+
58+
59+
def initialize():
60+
position=[]
61+
x = 0.45 * display_width
62+
y = 0.5 * display_height
63+
x = x - x % 20
64+
y = y - y % 20
65+
66+
position.append([x,y])
67+
68+
food_coord()
69+
x_food = food_coord()[0]
70+
y_food = food_coord()[1]
71+
return 1, False, 0, 0, False, 0, position, x, y, x_food, y_food, 0 ,0
72+
'''
73+
food = 1
74+
75+
x_change = 0
76+
y_change = 0
77+
gameExit = False
78+
horizontal = False
79+
vertical = False
80+
eaten = True
81+
cont = 0
82+
'''
83+
84+
def loop_move(food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change,move):
85+
if move == 1 and horizontal == 0: #left
86+
x_change = -20
87+
y_change = 0
88+
vertical = 0
89+
horizontal = 1
90+
elif move == 2 and horizontal == 0: #right
91+
x_change = 20
92+
y_change = 0
93+
vertical = 0
94+
horizontal = 1
95+
elif move == 3 and vertical == 0: #top
96+
y_change = - 20
97+
x_change = 0
98+
vertical = 1
99+
horizontal = 0
100+
elif move == 4 and vertical == 0: #bottom
101+
y_change = 20
102+
x_change = 0
103+
vertical = 1
104+
horizontal = 0
105+
106+
x = x + x_change
107+
y = y + y_change
108+
if (position[-1][0] != x or position[-1][1] != y):
109+
if [x,y] in position:
110+
crash()
111+
pygame.time.wait(100)
112+
gameExit = True
113+
114+
if food>1:
115+
for i in range(0,food-1):
116+
position[i][0]= position[i+1][0]
117+
position[i][1] = position[i+1][1]
118+
position[-1][0] = x
119+
position[-1][1] = y
120+
121+
if eaten == True:
122+
food_coord()
123+
x_food = food_coord()[0]
124+
y_food = food_coord()[1]
125+
eaten = False
126+
127+
if x == x_food and y == y_food:
128+
eaten= True
129+
food = food + 1
130+
position.append([x,y])
131+
132+
gameDisplay.fill(white)
133+
display_food(x_food, y_food)
134+
135+
for i in range(food):
136+
#print(len(position))
137+
x_temp=position[len(position)-1-i][0]
138+
y_temp=position[len(position)-1-i][1]
139+
car(x_temp, y_temp)
140+
pygame.time.wait(100)
141+
pygame.display.update()
142+
143+
144+
if x < 0 or x > display_width-20 or y<0 or y > display_height-20:
145+
crash()
146+
gameExit = True
147+
148+
clock.tick(1000)
149+
cont = cont+1
150+
151+
return food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change
152+
153+
def fitness(x_food, x, y_food, y, food):
154+
score = (1-(np.sqrt((x_food-x)**2 + (y_food - y)**2))/566 + food)
155+
return score
156+
157+
158+
#Execute game
159+
#food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change = initialize()
160+
#l r t b
161+
'''
162+
dataset=pd.DataFrame()
163+
t=1
164+
while(t<100):
165+
gameExit=False
166+
167+
food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change = initialize()
168+
169+
while gameExit==False:
170+
#print(position)
171+
if x_food < x and [(x-20),y] not in position and (x-20)>0:
172+
food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change = loop_move(food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change,1)
173+
move=1
174+
elif x_food > x and [(x+20),y] not in position and (x+20)<display_width:
175+
food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change = loop_move(food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change, 2)
176+
move=2
177+
elif y_food < y and [x,(y-20)] not in position and (y-20)>0:
178+
food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change = loop_move(food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change, 3)
179+
move=3
180+
elif y_food > y and [x,(y+20)] not in position and (y+20) < display_height:
181+
food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change = loop_move(food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change, 4)
182+
move=4
183+
elif [(x-20),y] not in position and horizontal==False and (x-20)>0:
184+
food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change = loop_move(food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change, 1)
185+
move=1
186+
elif [(x+20),y] not in position and horizontal==False and (x+20)<display_width:
187+
food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change = loop_move(food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change, 2)
188+
move=2
189+
elif [x,(y-20)] not in position and vertical==False and (y-20)>0:
190+
food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change = loop_move(food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change, 3)
191+
move=3
192+
elif [x,(y+20)] not in position and vertical==False and (y+20) < display_height:
193+
food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change = loop_move(food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change, 4)
194+
move=4
195+
else:
196+
move = randint(1,4)
197+
food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change = loop_move(food, gameExit, horizontal, vertical, eaten, cont, position, x, y, x_food, y_food, x_change, y_change, move)
198+
199+
200+
if x_food < x:
201+
right_food = 0
202+
left_food = 1
203+
else:
204+
right_food = 1
205+
left_food = 0
206+
if y_food < y:
207+
up_food = 1
208+
down_food = 0
209+
else:
210+
up_food = 0
211+
down_food = 1
212+
if [(x+20),y] not in position and (x+20)<display_width:
213+
right_free = 1
214+
else:
215+
right_free = 0
216+
if [(x-20),y] not in position and (x-20)>0:
217+
left_free = 1
218+
else:
219+
left_free=0
220+
if [x,(y-20)] not in position and (y-20)>0:
221+
up_free = 1
222+
else:
223+
up_free = 0
224+
if [x,(y+20)] not in position and (y+20)<display_height:
225+
down_free = 1
226+
else:
227+
down_free=0
228+
229+
230+
231+
array = [right_food,left_food,up_food,down_food,right_free,left_free,up_free,down_free, vertical, horizontal, move]
232+
#for i in range(len(grid)):
233+
#array.append(grid[i])
234+
dataset = dataset.append([array])
235+
236+
print(t)
237+
t=t+1
238+
239+
dataset.to_csv(path_or_buf='snakemove6.csv',sep = ',',index=False, header=False)
240+
241+
pygame.quit()
242+
quit()
243+
'''

‎snakemove6.csv

+30,157
Large diffs are not rendered by default.

‎try.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import pygame
2+
from keras.models import Sequential,Model
3+
from keras.layers.core import Flatten, Dense, Dropout
4+
from keras.optimizers import RMSprop
5+
import random
6+
import numpy as np
7+
import pandas as pd
8+
from DQN import DQNAgent
9+
from snakeClass import *
10+
import copy
11+
12+
class Agent(object):
13+
def __init__(self):
14+
self.actual = []
15+
16+
class Play(object):
17+
18+
def __init__(self):
19+
self.x = 0.45 * 400
20+
self.y = 0.5 * 400
21+
self.position = []
22+
self.position.append([self.x, self.y])
23+
self.x_change = 20
24+
self.y_change = 0
25+
26+
def do_move(self, move, x, y):
27+
move_array = [self.x_change, self.y_change]
28+
29+
if move == 0 and self.x_change == 0: # left
30+
move_array = [-20, 0]
31+
elif move == 1 and self.x_change == 0: # right
32+
move_array = [20, 0]
33+
self.x_change, self.y_change = move_array
34+
self.x = x + self.x_change
35+
self.y = y + self.y_change
36+
37+
self.update_position(self.x, self.y)
38+
39+
def update_position(self, x, y):
40+
self.position[-1][0] = x
41+
self.position[-1][1] = y
42+
43+
44+
def run():
45+
agent = Agent()
46+
player1 = Play()
47+
agent.actual = copy.deepcopy([player1.position, player1.x])
48+
print(agent.actual[0])
49+
i = 1
50+
player1.do_move(i, player1.x, player1.y)
51+
print(agent.actual[0])
52+
53+
run()

‎weights.hdf5

28.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.