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

active the write data function to log the data

parent 169e3ff9
No related branches found
No related tags found
No related merge requests found
.*__* .*__*
.*/__* .*/__*
.*/__pycache__ .*/__pycache__
bike/__pycache__/gen.cpython-310.pyc bike/__pycache__/*
bike/__pycache__/simu_bike.cpython-310.pyc debug.log
debug.log data/*
\ No newline at end of file \ No newline at end of file
...@@ -3,13 +3,7 @@ ...@@ -3,13 +3,7 @@
import src.genetic_algo as ga import src.genetic_algo as ga
import bike.simu_bike as sb import bike.simu_bike as sb
# points_position = np.random.rand(4, 2)*8+3 gen=ga.Genetic_algorithm(population_size=5,
# a_bike=sb.creat_bike(points_position=points_position)
# print(a_bike)
gen=ga.Genetic_algorithm(population_size=20,
start_point=(0,5), start_point=(0,5),
side_length=5, side_length=5,
fit_func=sb.creat_bike) fit_func=sb.creat_bike)
......
...@@ -122,10 +122,7 @@ class Population(): ...@@ -122,10 +122,7 @@ class Population():
def save_population_to_file(self,filename): def save_population_to_file(self,filename):
with open(filename,"w") as f: with open(filename,"w") as f:
json.dump([ind.coordinates for ind in self.population],f) json.dump([ind.coordinates for ind in self.population],f)
def load_fitness_score_from_file(self,filename): f.close()
with open(filename,"r") as f:
fitness_scores =json.load(f)
return fitness_scores
class Genetic_algorithm(): class Genetic_algorithm():
''' '''
...@@ -271,13 +268,14 @@ random.randint(start_point[1], start_point[1]+side_length)), ...@@ -271,13 +268,14 @@ random.randint(start_point[1], start_point[1]+side_length)),
# START # START
# Generate the initial population and Compute fitness # Generate the initial population and Compute fitness
self.p = Population(self.__population_size, self.__start_point, self.__side_length, self.fit_func) self.p = Population(self.__population_size, self.__start_point, self.__side_length, self.fit_func)
#self.p.save_population_to_file()
#self.p.load_fitness_score_from_file("data/population.json")
average_fitness_score = self.p.average_fitness_score() average_fitness_score = self.p.average_fitness_score()
best_fitness_score = self.p.best_fitness_score() best_fitness_score = self.p.best_fitness_score()
# REPEAT # REPEAT
file_counter=0
while abs(best_fitness_score - average_fitness_score) > 0.5: while abs(best_fitness_score - average_fitness_score) > 0.5:
self.p.save_population_to_file("data/population_"+str(file_counter)+".json")
file_counter+=1
# Selection # Selection
top_individuals = self.selection(self.p, percentage=0.2) top_individuals = self.selection(self.p, percentage=0.2)
# Crossover # Crossover
......
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