Allow multiple instances to run
This commit is contained in:
		
							parent
							
								
									dc111a64f8
								
							
						
					
					
						commit
						858fb48a4b
					
				
							
								
								
									
										32
									
								
								src/lifecycle.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/lifecycle.py
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,32 @@ | |||
| from population import Population | ||||
| import matplotlib.pyplot as plt | ||||
| 
 | ||||
| 
 | ||||
| class Lifecycle(object): | ||||
|     def __init__(self, id, params): | ||||
|         self.id = id | ||||
|         self.params = params | ||||
|         self.population = Population(self.params["population_size"]) | ||||
|         self.best_fit = list() | ||||
|         self.average_fit = list() | ||||
| 
 | ||||
|     def start(self): | ||||
|         for epoch in range(0, self.params["iter"]): | ||||
|             elite = round(self.params["elite"] * self.params["population_size"]) | ||||
|             crossover = round((self.params["crossover"] | ||||
|                                * self.params["population_size"]) / 2) | ||||
|             self.population.advance_generation(elite, crossover) | ||||
|             self.best_fit.append(self.population.best_fitness()) | ||||
|             self.average_fit.append(self.population.avg_fitness()) | ||||
| 
 | ||||
|     def best_member(self): | ||||
|         return self.population.best_member() | ||||
| 
 | ||||
|     def generate_graph(self, show=False, location=None): | ||||
|         plt.plot(self.best_fit) | ||||
|         plt.xlabel("Epoch") | ||||
|         plt.ylabel("Best fitness") | ||||
|         if show: | ||||
|             plt.show() | ||||
|         if location is not None: | ||||
|             plt.savefig(location) | ||||
							
								
								
									
										36
									
								
								src/main.py
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								src/main.py
									
									
									
									
									
								
							|  | @ -1,38 +1,16 @@ | |||
| import os | ||||
| import argparse | ||||
| from population import Population | ||||
| from time import sleep | ||||
| import matplotlib.pyplot as plt | ||||
| from lifecycle import Lifecycle | ||||
| 
 | ||||
| 
 | ||||
| def cls(): | ||||
|     os.system('cls' if os.name == 'nt' else 'clear') | ||||
| 
 | ||||
| 
 | ||||
| best_fit = list() | ||||
| ga = Lifecycle(1, {'population_size': 10, | ||||
|                    'elite': 0.1, | ||||
|                    'crossover': 0.6, | ||||
|                    'iter': 1000}) | ||||
| 
 | ||||
| args = argparse.ArgumentParser() | ||||
| args.add_argument("pop", help="Population size", type=int) | ||||
| args.add_argument("iter", help="Iterations", type=int) | ||||
| args.add_argument("-w", "--wait", help="Time in seconds to wait between iterations", type=int, default=0) | ||||
| args = args.parse_args() | ||||
| ga.start() | ||||
| 
 | ||||
| pop = Population(args.pop) | ||||
| 
 | ||||
| for i in range(0, args.iter): | ||||
|     cls() | ||||
|     print("Epoch: %d " % i) | ||||
|     elite = round(args.pop * .1) | ||||
|     crossover = round((args.pop * .8) / 2) | ||||
|     pop.advance_generation(elite, crossover) | ||||
|     best_fit.append(pop.best_fitness().fitness()) | ||||
|     #input("...") | ||||
|     sleep(args.wait) | ||||
| 
 | ||||
| print("Best: ") | ||||
| print(pop.best_fitness()) | ||||
| 
 | ||||
| plt.plot(best_fit) | ||||
| plt.xlabel("Epoch") | ||||
| plt.ylabel("Best fitness") | ||||
| plt.show() | ||||
| ga.generate_graph(show=True) | ||||
|  |  | |||
|  | @ -39,6 +39,9 @@ class Population(object): | |||
|         return float(self.total_fitness() / len(self.members)) | ||||
| 
 | ||||
|     def best_fitness(self): | ||||
|         return self.best_member().fitness() | ||||
| 
 | ||||
|     def best_member(self): | ||||
|         best_member = self.members[0] | ||||
| 
 | ||||
|         for member in self.members: | ||||
|  | @ -73,8 +76,8 @@ class Population(object): | |||
|         new_generation = list() | ||||
| 
 | ||||
|         # elitism | ||||
|         #for member in self.elite(n_elite): | ||||
|             #new_generation.append(member) | ||||
|         for member in self.elite(n_elite): | ||||
|             new_generation.append(member) | ||||
| 
 | ||||
|         # parent | ||||
|         for ind in range(0, n_crossover): | ||||
|  | @ -89,7 +92,7 @@ class Population(object): | |||
| 
 | ||||
|         self.members = new_generation | ||||
| 
 | ||||
|         self.mutate(2) | ||||
|         self.mutate(20) | ||||
| 
 | ||||
|     def remove_member(self, member): | ||||
|         self.members.remove(member) | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user