-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
108 lines (100 loc) · 4.05 KB
/
main.py
File metadata and controls
108 lines (100 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from time import sleep
from random import randint
from godot import Godot, array_to_key
from sys import argv
import socket
import neat
HOST = "127.0.0.1"
PORT = 4040
NN = 10
INIT_STEPS = 10
MAX_SCORE = 2460
STEPS_PER_SEC = 20.0
def eval_genomes(genomes, config):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
error = None
try:
s.listen()
for genome_id, genome in genomes:
genome.fitness = 0.0
net = neat.nn.FeedForwardNetwork.create(genome, config)
print("Wating connection")
cnx, addr = s.accept()
godot = Godot(cnx)
# godot.send_genome(genome)
# input("Press enter")
try:
# Store last step state to check whether Pacman made any progress or not
steps_without_progress = 0
steps_not_moving = 0
dist_to_walls = {
"left": 0,
"front": 0,
"right": 0,
"back": 0,
}
last_score = 0
while steps_without_progress < STEPS_PER_SEC*10 and steps_not_moving < STEPS_PER_SEC/2.0:
if godot.update() == False: # Can continue updating?
break
# Is Pacman making any progress?
if godot._pacman.score != last_score:
last_score = godot._pacman.score
steps_without_progress = 0
else:
steps_without_progress += 1
if steps_without_progress % 2 == 1:
n = 0
for key in dist_to_walls.keys():
if (dist_to_walls[key] - godot._pacman.walls[key]) < 0.01:
n += 1
if n == 3 or n == 4:
steps_not_moving += 1
else:
steps_not_moving = 0
dist_to_walls = godot._pacman.walls.copy()
value = net.activate(godot.to_array())
# print("Activation yielded ", value)
key = array_to_key(value)
print("Key: ", key)
godot.move(key)
print(key, ": ", value)
print("Steps without progress: ",
steps_without_progress)
print("Steps without moving: ",
steps_not_moving)
sleep(1.0 / STEPS_PER_SEC)
print("Score: ", godot._pacman.score)
genome.fitness = godot._pacman.score * godot._pacman.score
print("Fitness: ", genome.fitness)
godot.quit()
except BrokenPipeError as bp:
print("Broken pipe")
except:
godot.quit()
raise
except Exception as e:
print("Error: ", e)
s.close()
config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation, "neat-config")
pop = None
if argv.__len__() == 2:
import os
if os.path.exists(argv[1]):
print("Using file")
pop = neat.Checkpointer.restore_checkpoint(argv[1])
else:
raise Exception("File not found")
else:
print("New pop")
pop = neat.Population(config)
pop.add_reporter(neat.StdOutReporter(False))
pop.add_reporter(neat.Checkpointer(1, 30, "checkpoints/neat-"))
pop.add_reporter(neat.StatisticsReporter())
winner = pop.run(eval_genomes)
with open("winner", "w") as file:
file.write(str(winner))
print('\nBest genome:\n{!s}'.format(winner))