-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
220 lines (177 loc) · 9.68 KB
/
main.py
File metadata and controls
220 lines (177 loc) · 9.68 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import random
import pprint
import threading
import time
import datetime
import os
from multiprocessing import Process
from gene import Gene
import matplotlib.pyplot as plt
pp = pprint.PrettyPrinter(indent=4)
def create_input_board(filename):
file = open(filename, 'r')
width, height = [int(dims) for dims in file.readline().split()]
input_board = file.readline().strip().split(',')
return (input_board, width, height)
def create_population(population_size, input_board, width, height):
population = []
for x in range(population_size):
gene = Gene(input_board, width, height)
population.append(gene)
population.sort()
#pp.pprint(population)
return population
def two_parent_tournament_select(population, tournament_size, mutation_precentage):
tournament = sorted(random.sample(population, tournament_size))
new_gene = Gene.Create_Gene_From_Parent(Gene, tournament[0], tournament[1])
new_gene.Mutate(mutation_precentage)
return new_gene
def one_parent_tournament_select(population, tournament_size, mutation_precentage):
tournament = sorted(random.sample(population, tournament_size))
new_gene = Gene.Create_Gene_From_Parent(Gene,tournament[0])
new_gene.Mutate(mutation_precentage)
return new_gene
def threading_append_population(new_population,population, tournament_size, mutation_precentage,additional_mutation_constant):
new_population.append(two_parent_tournament_select(population, tournament_size, mutation_precentage + additional_mutation_constant))
def evolve_until_convergence(population, max_epochs, start_mutation_precentage, mutation_generations_increase, mutation_generations_increase_percentage, generation_wipe_after, tournament_size, elitism, startTime, logFileName, graphsDirectory):
population_size = len(population)
generation = 0
previous_best = 100000
generations_at_current_best = 1
mutation_precentage = start_mutation_precentage
max_fitness = []
avg_fitness = []
while (population[0].Get_Fitness() >= 1 and generation <= max_epochs):
new_population = []
#print("-----------")
#pp.pprint(new_population)
#pp.pprint(population)
#Keep the Top Genes
for top_gene in range(elitism):
new_population.append(population[top_gene])
while len(new_population) != population_size:
new_population.append(two_parent_tournament_select(population, tournament_size, mutation_precentage))
#new_population.append(one_parent_tournament_select(population, tournament_size, mutation_precentage))
new_population.sort()
population = new_population
#Increase mutation constant
if(round(previous_best) == round(population[0].Get_Fitness())):
generations_at_current_best += 1
if(generations_at_current_best % mutation_generations_increase == 0 and mutation_precentage != 1):
if (mutation_precentage < 1):
mutation_precentage += mutation_generations_increase_percentage
else:
mutation_precentage = 1
else:
previous_best = population[0].Get_Fitness()
generations_at_current_best = 1
mutation_precentage = start_mutation_precentage
if(generations_at_current_best >= generation_wipe_after):
print("---------Wiping Population-----------")
population = create_population(population_size, population[0].correct_board, population[0].width, population[0].height)
generation += 1
max_fitness.append(population[0].Get_Fitness())
avg_fitness.append(sum([x.Get_Fitness() for x in population])/population_size)
if(generation % 10 == 0):
print("Generation " + str(generation))
print("Current Time: " + "%.2f" % (time.clock() - startTime))
print("Fitness: " + "%.2f" %(max_fitness[-1]))
print("Avg Fitness: " + "%.2f" %(avg_fitness[-1]))
print("Current Mutation: " + "%.2f" %(mutation_precentage))
print("Generations At current Best: " + str(generations_at_current_best))
print("Best Grid\n" + population[0].pretty_print() + '\n')
print()
#pp.pprint(population)
if(generation % 50 == 0):
outfile = open(logFileName, 'a')
outfile.write("--------------------------------------" + '\n')
outfile.write("Generation: " + str(generation) + '\n')
outfile.write("Current Time: " + "%.2f" % (time.clock() - startTime) + '\n')
outfile.write("Fitness: " + "%.2f" %(max_fitness[-1]) + '\n')
outfile.write("Avg Fitness: " + "%.2f" %(avg_fitness[-1]) + '\n')
outfile.write("Current Mutation: " + "%.2f" % (mutation_precentage) + '\n')
outfile.write("Generations At current Best: " + "%.2f" % (generations_at_current_best) + '\n\n')
outfile.write(population[0].pretty_print() + '\n')
#outfile.write(pp.pformat(population) + '\n\n')
outfile.close()
if(generation % 50 == 0):
plotResults(graphsDirectory + '/Graph'+str(generation) +'.png', max_fitness, avg_fitness)
#print(max_fitness)
#pp.pprint(population)
return (population,generation,max_fitness, avg_fitness)
def plotResults(fileName, max_fitness, avg_fitness):
plt.plot(range(len(max_fitness)), max_fitness, label='Max Fitness', color = 'blue', )
plt.plot(range(len(avg_fitness)), avg_fitness, label='Avg Fitness', color = 'red', )
plt.title("Fitness vs Generations")
plt.xlabel('Epoch')
plt.ylabel('Fitness')
plt.legend(loc='upper right')
plt.savefig(fileName)
plt.close()
def main():
#Constants
population_size = 10000
max_epochs = 1000000000
mutation_precentage = .2
mutation_generations_increase = 1
mutation_generations_increase_percentage = .1
generation_wipe_after = 5000000
tournament_size = 5
elitism = 0
startTime = time.clock()
mainDirectory = "Runs"
subDirectory = mainDirectory + '/' + datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H-%M-%S')
graphsDirectory = subDirectory + '/' + 'graphs'
logFileName = subDirectory + '/' + "log.txt"
outputFileName = subDirectory + '/' + "out.txt"
plotFileName = subDirectory + '/' + "graph.png"
runInfoFileName = subDirectory + '/' + "runInfo.txt"
if not os.path.exists(mainDirectory):
os.makedirs(mainDirectory)
if not os.path.exists(subDirectory):
os.makedirs(subDirectory)
if not os.path.exists(graphsDirectory):
os.makedirs(graphsDirectory)
outfile = open(runInfoFileName, 'a')
outfile.write("------------Settings-------------\n")
outfile.write("population_size = "+str(population_size) + '\n')
outfile.write("mutation_precentage = "+str(mutation_precentage) + '\n')
outfile.write("mutation_generations_increase = "+ "%.2f" %(mutation_generations_increase) + '\n')
outfile.write("mutation_generations_increase_percentage = "+ "%.2f" %(mutation_generations_increase_percentage) + '\n')
outfile.write("tournament_size = "+str(tournament_size) + '\n')
outfile.write("elitism = " + str(elitism) + '\n\n')
outfile.close()
#Read in Board
input_board, width, height = create_input_board('in.txt')
#Create Population
population = create_population(population_size, input_board, width, height)
population, generation, max_fitness, avg_fitness = evolve_until_convergence(population,
max_epochs,
mutation_precentage,
mutation_generations_increase,
mutation_generations_increase_percentage,
generation_wipe_after,
tournament_size,
elitism,
startTime,
logFileName,
graphsDirectory)
outfile = open(outputFileName, 'a')
outfile.write("------------Settings-------------\n")
outfile.write("population_size = "+str(population_size) + '\n')
outfile.write("mutation_precentage = "+str(mutation_precentage) + '\n')
outfile.write("mutation_generations_increase = "+ "%.2f" %(mutation_generations_increase) + '\n')
outfile.write("mutation_generations_increase_percentage = "+ "%.2f" %(mutation_generations_increase_percentage) + '\n')
outfile.write("tournament_size = "+str(tournament_size) + '\n')
outfile.write("elitism = " + str(elitism) + '\n\n')
outfile.write("------------Results-------------\n")
outfile.write("Generations: " + str(generation) + '\n')
outfile.write("Final Time: " + "%.2f" % (time.clock() - startTime) + '\n')
outfile.write(str(input_board) + '\n')
outfile.write(str(population[0].board)+ '\n')
outfile.write(str(max_fitness)+ '\n')
outfile.write(str(avg_fitness)+ '\n\n')
outfile.write(population[0].pretty_print() + '\n')
plotResults(plotFileName, max_fitness, avg_fitness)
if __name__ == '__main__':
main()