Skip to content
Snippets Groups Projects
Commit 7b15e75c authored by hui's avatar hui
Browse files

plot the max and ave. scores together after the simulation.

increase the population size to 100
parent 1ef40c00
Branches main
No related tags found
No related merge requests found
......@@ -4,8 +4,9 @@ import src.genetic_algo as ga
import src.simu_bike as sb
import src.scorePlot as sp
gen=ga.Genetic_algorithm(population_size=15,
start_point=(0,5),
gen=ga.Genetic_algorithm(population_size=100,
start_point=(2,5),
side_length=3,
fit_func=sb.creat_bike)
sp.plot(gen.run())
\ No newline at end of file
x,y=gen.run()
sp.plot(x,y)
\ No newline at end of file
......@@ -140,7 +140,10 @@ class Population():
total = 0
for i in self.population:
total += i.fitness_score
return total / self.__size
return total / self.__size
def best_fitness_score(self):
return max([x.fitness_score for x in self.population])
def save_population_to_file(self,filename):
with open(filename,"w") as f:
json.dump([ind.coordinates for ind in self.population],f)
......@@ -325,6 +328,7 @@ random.randint(start_point[1], start_point[1]+side_length)),
last_ten_average = [0]*10
file_counter=0
score_list=[]
high_score_list=[]
while abs(average_fitness_score- np.mean(last_ten_average[-10:])) > 0.1:
last_ten_average.append(self.p.average_fitness_score())
self.p.save_population_to_file("data/population_"+str(file_counter)+".json")
......@@ -342,9 +346,10 @@ random.randint(start_point[1], start_point[1]+side_length)),
# UNTIL population has converged
# STOP
score_list.append(average_fitness_score)
high_score_list.append(self.p.best_fitness_score())
if len(score_list)>100:
break
return score_list
return score_list, high_score_list
#return the fittest child among the last siblings
# remaining_sibling = sorted(p.population, key=lambda x: x.fitness_score)
# remaining_sibling.reverse()
......
import matplotlib.pyplot as plt
def plot(scores):
def plot(scores,high_score):
plt.plot(range( len(scores)),scores,'--b')
plt.plot(range( len(high_score)),high_score,'-r')
plt.xlabel('Generation')
plt.ylabel('Ave. score')
plt.show()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment