-
Notifications
You must be signed in to change notification settings - Fork 2
/
darwinselection.py
136 lines (108 loc) · 4.38 KB
/
darwinselection.py
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from simulation import Simulation
from intelligentsoldier import Soldier
from multiprocessing import Pool
from getch import KBHit
from random import randrange
import os
import pickle
import time
import copy
def launchSimulation(soldiers):
sim = Simulation()
return sim.simulateOneGame(copy.deepcopy(soldiers))
class DarwinSelection:
def __init__(self, soldiers_file=None):
self.keyboard = KBHit()
self.soldiers_number = 20
self.soldiers = []
self.generation = 0
self.pow_proba = 2
self.sim = Simulation()
if soldiers_file == None:
for i in range(self.soldiers_number):
self.soldiers.append(Soldier(randrange(750, 1250),
randrange(250, 750),
""))
else:
with open(soldiers_file, 'rb') as f:
self.soldiers = pickle.load(f)
self.soldiers_number = len(self.soldiers)
self.save_id = 0
if not os.path.exists("saves"):
os.makedirs("saves")
while os.path.exists("saves/save_" + str(self.save_id)):
self.save_id += 1
os.makedirs("saves/save_" + str(self.save_id))
def save(self, generation):
path = 'saves/save_' + str(self.save_id) + "/" + str(generation)
with open(path, 'wb') as f:
pickle.dump(self.soldiers, f)
print("Successfull save at : {} !".format(path))
def run(self):
generation = 0
while True:
time_beggining = time.time()
if generation % 10 == 0:
self.save(generation)
print("Generation number : {}".format(generation))
print("Press (S) to save the current state ")
print("Press (Q) to quit and save ")
if(self.keyboard.kbhit()):
char = self.keyboard.getch()
if(char == "s"):
self.save(generation)
elif(char == "q"):
self.save(generation)
quit()
for sol in self.soldiers:
sol.kills = 0
fights = []
for sol in self.soldiers:
for sol2 in self.soldiers:
if sol is not sol2:
fights.append([sol, sol2])
pool = Pool(processes=12)
results = pool.map(launchSimulation, copy.deepcopy(fights))
pool.close()
average_steps = 0
for i, result in enumerate(results):
fights[i][0].kills += result[0]
fights[i][1].kills += result[1]
average_steps += result[2]
average_steps /= len(fights)
average_steps = round(average_steps, 0)
generation_time = round(time.time() - time_beggining, 2)
print(chr(27) + "[2J")
print("--------- Generation overview : ({}) seconds ---------- "
.format(generation_time))
print("Average steps to kill : ({}/1250)".format(average_steps))
self.reproduce()
generation += 1
def reproduce(self):
self.soldiers = sorted(self.soldiers,
key=lambda sol: sol.kills,
reverse=True)
total_probability = 0
for sol in self.soldiers:
total_probability += sol.kills ** self.pow_proba
for i, sol in enumerate(self.soldiers):
print("{} did {} kills ! {}/100".format(i, sol.kills, round(((sol.kills ** self.pow_proba)/ total_probability) * 100), 2))
ponderated_soldiers = []
for sol in self.soldiers:
if(sol.kills > 0):
for i in range((sol.kills) ** self.pow_proba):
ponderated_soldiers.append(sol)
else:
ponderated_soldiers.append(Soldier(0,0,"none"))
self.soldiers = []
for i in range(self.soldiers_number):
index = randrange(0, len(ponderated_soldiers))
self.soldiers.append(copy.deepcopy(
ponderated_soldiers[index - 1])
)
self.soldiers = sorted(self.soldiers,
key=lambda sol: sol.kills,
reverse=True)
for i, sol in enumerate(self.soldiers):
if(i != 0):
sol.mutate(0.10)