def run_sim(self): simulation = self.simulation cfg = self.controller.global_params['cfg'] MAX_RUNS = cfg['max_runs'] status: SimulationStatus = self.controller.global_params['status'] status.running = True dt_start = datetime.timestamp(datetime.now()) simulation.generate_initial_population() # symulujemy kolejne iteracje, zapisując wyniki każdej z nich for i in range(MAX_RUNS): if status.kill is True: #status.running = False return # populacja i fitness z aktualnego pokolenia population = simulation.population fitness_values = simulation.fitness_values # może już przetwarzać sobie następne, jeśli to nie było ostatnie if i < MAX_RUNS - 1: simulation.next_generation() std = np.std(fitness_values) # add the champion chromosome to a list of champions for plotting index_of_champion = listtools.max_index_in_list(fitness_values) status.champion_chromosomes.append(population[index_of_champion]) # add the max/average values to lists for plotting status.max_values.append( listtools.max_value_in_list(fitness_values)) status.avg_values.append(listtools.avgList(fitness_values)) status.stds.append(std) status.current_population = population status.drawEvent = True status.iteration = i self.after( 0, self.update_label(i, status.max_values[i], status.avg_values[i], std)) if status.kill is True: #status.running = False return dt_end = datetime.timestamp(datetime.now()) status.totalTime = dt_end - dt_start status.running = False
def exhaustive_maxvalue(): """ Main 1 [Start] Generate random population of n chromosomes (suitable solutions for the problem) 2 [Fitness] Evaluate the fitness f(x) of each chromosome x in the population 3 [New population] Create a new population by repeating following steps until the new population is complete 3a[Selection] Select two parent chromosomes from a population according to their fitness (the better fitness, the bigger chance to be selected) 3b[Crossover] With a crossover probability cross over the parents to form a new offspring (children). If no crossover was performed, offspring is an exact copy of parents. 3c[Mutation] With a mutation probability mutate new offspring at each locus (position in chromosome). 3d[Accepting] Place new offspring in a new population 4 [Replace] Use new generated population for a further run of algorithm 5 [Test] If the end condition is satisfied, stop, and return the best solution in current population 6 [Loop] Go to step 2 """ ################################################################################################################################################# # # NOTE: Fitness_values is indexed by chromosome number, so if we attempt to sort it, # we will pick the wrong index when generating a new population. Which would be bad. # ################################################################################################################################################# map = create_map() population = generate_initial_population() fitness_values = run_simulation(map, population) # write out our results to a csv for graphing csvWriter = csv.writer(open('geneticalgo.csv', 'wb'), delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) for i in range(MAX_RUNS): population = generate_new_population(fitness_values, population) fitness_values = run_simulation(map, population) max_value = listtools.max_value_in_list(fitness_values) avg_value = listtools.avgList(fitness_values) csvWriter.writerow([avg_value, max_value]) print "Run " + str(i) + ": max value " + str(max_value) + ", avg value " + str(avg_value) # currently our function evaluates to distance so max distance is true. if max_value > .95 and max_value <= 1: print "SOLUTION FOUND." break
def run(self) -> AlgorithmResult: MAX_RUNS = self.config['max_runs'] result = AlgorithmResult() result.gene_names = self.params.gene_names dt_start = datetime.timestamp(datetime.now()) self.generate_initial_population() # symulujemy kolejne iteracje, zapisując wyniki każdej z nich for i in range(MAX_RUNS): # może już przetwarzać sobie następne, jeśli to nie było ostatnie if i < MAX_RUNS - 1: self.next_generation() std = np.std(self.fitness_values) # add the champion chromosome to a list of champions for plotting index_of_champion = listtools.max_index_in_list( self.fitness_values) result.champion_chromosomes.append( self.population[index_of_champion]) # add the max/average values to lists for plotting result.max_values.append( listtools.max_value_in_list(self.fitness_values)) result.avg_values.append(listtools.avgList(self.fitness_values)) result.stds.append(std) result.current_population = self.population result.iteration = i dt_end = datetime.timestamp(datetime.now()) result.totalTime = dt_end - dt_start return result
################################################################################################################################################# # # NOTE: Fitness_values is indexed by chromosome number, so if we attempt to sort it, # we will pick the wrong index when generating a new population. Which would be bad. # ################################################################################################################################################# map = create_map() population = generate_initial_population() fitness_values = run_simulation(map, population) # write out our results to a csv for graphing csvWriter = csv.writer(open('geneticalgo.csv', 'wb'), delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) for i in range(MAX_RUNS): population = generate_new_population(fitness_values, population) fitness_values = run_simulation(map, population) min_value = listtools.min_value_in_list(fitness_values) avg_value = listtools.avgList(fitness_values) csvWriter.writerow([avg_value, min_value]) print "Run " + str(i) + ": avg value " + str( avg_value) + ", min value " + str(min_value) # currently our function evaluates to distance so max distance is true. if min_value == 0: print "SOLUTION FOUND." break
for i in range(config['max_runs']): simulation.generate_new_population() fitness_values = simulation.fitness_values population = simulation.population # add the champion chromosome to a list of champions for plotting index_of_champion = listtools.max_index_in_list(fitness_values) kp_values.append(population[index_of_champion].kp) kd_values.append(population[index_of_champion].kd) ki_values.append(population[index_of_champion].ki) # add the max/average values to lists for plotting max_values.append(listtools.max_value_in_list(fitness_values)) avg_values.append(listtools.avgList(fitness_values)) print("Run " + str(i) + ": max value " + str(max_values[i]) + ", avg value " + str(avg_values[i])) print(population[index_of_champion].kp, population[index_of_champion].kd, population[index_of_champion].ki) print('\n') plt.figure() plt.plot() plt.title("Fitness Values Over Time") plt.plot(range(config['max_runs']), max_values, label=r"Max Value") plt.plot(range(config['max_runs']), avg_values, label=r"Average Value") plt.legend(loc='lower right') plt.xlabel("Run")
5 [Test] If the end condition is satisfied, stop, and return the best solution in current population 6 [Loop] Go to step 2 """ ################################################################################################################################################# # # NOTE: Fitness_values is indexed by chromosome number, so if we attempt to sort it, # we will pick the wrong index when generating a new population. Which would be bad. # ################################################################################################################################################# map = create_map() population = generate_initial_population() fitness_values = run_simulation(map, population) # write out our results to a csv for graphing csvWriter = csv.writer(open('geneticalgo.csv', 'wb'), delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) for i in range(MAX_RUNS): population = generate_new_population(fitness_values, population) fitness_values = run_simulation(map, population) min_value = listtools.min_value_in_list(fitness_values) avg_value = listtools.avgList(fitness_values) csvWriter.writerow([avg_value, min_value]) print "Run " + str(i) + ": avg value " + str(avg_value) + ", min value " + str(min_value) # currently our function evaluates to distance so max distance is true. if min_value == 0: print "SOLUTION FOUND." break
def test_avg_of_list(self): lista = [1, 3, 4, 0] avg = listtools.avgList(lista) self.assertEqual(avg, 2.0)
for i in range(MAX_RUNS): # generate population and run the simulation population = generate_new_population(fitness_values, population) fitness_values = run_simulation(map, population) # add the champion chromosome to a list of champions for plotting index_of_champion = listtools.max_index_in_list(fitness_values) kp_values.append(population[index_of_champion].kp) kd_values.append(population[index_of_champion].kd) ki_values.append(population[index_of_champion].ki) # add the max/average values to lists for plotting max_values.append(listtools.max_value_in_list(fitness_values)) avg_values.append(listtools.avgList(fitness_values)) # every RUNS_PER_SCREENSHOT runs, do a plot of the champion chromosome if i % RUNS_PER_SCREENSHOT == 0: # run the simulation for the first selected parent run_simulation_for_champion(map, population, index_of_champion, i, listtools.max_value_in_list(fitness_values)) print "Run " + str(i) + ": max value " + str(max_values[i]) + ", avg value " + str(avg_values[i]) # plot fitness results of each run plt.figure() plt.plot() plt.title("Fitness Values Over Time")