def run(problem=None, parents=PARENTS, offspring=OFFSPRING, use_tournament_selection=False): random.seed() if problem is None: problem = Cube() problem.scramble() population = [(deepcopy(problem), []) for i in range(parents)] selection_fn = tournament_selection if use_tournament_selection else truncation_selection # print("Original problem fitness: " + str(problem.fitness)) generations = 0 generations_without_improvement = 0 best_guy = population[0] while not solved(best_guy) and generations < MAX_GENERATIONS: old_population = deepcopy(population) old_best_buy = deepcopy(best_guy) # reproduction while len(population) < offspring: population.extend(deepcopy(old_population)) # mutation if generations_without_improvement >= MAX_ALLOWED_GENERATIONS_WITHOUT_IMPROVEMENT: print("Chaining mutations !") mutate(population, chain_mutations=True) generations_without_improvement = 0 else: mutate(population) population.extend(old_population) # selection population, best_guy = selection_fn(population, parents) print( "Generation: %d\tPopulation: %s\tFitness: %d\tBest_guy's_rotations_count: %s" % (generations, len(population), fitness(best_guy), len( best_guy[1]))) # measure improvement if fitness(best_guy) >= fitness(old_best_buy): generations_without_improvement += 1 else: generations_without_improvement = 0 generations += 1 if generations == MAX_GENERATIONS: return None else: # print("Solved in %d generations" % generations) return generations, len(best_guy[1]), calc_compression(best_guy[1])
def run(problem=None, parents=PARENTS, offspring=OFFSPRING, use_tournament_selection=False): random.seed() if problem is None: problem = Cube() problem.scramble() population = [(deepcopy(problem), []) for i in range(parents)] selection_fn = tournament_selection if use_tournament_selection else truncation_selection # print("Original problem fitness: " + str(problem.fitness)) generations = 0 generations_without_improvement = 0 best_guy = population[0] while not solved(best_guy) and generations < MAX_GENERATIONS: old_population = deepcopy(population) old_best_buy = deepcopy(best_guy) # reproduction while len(population) < offspring: population.extend(deepcopy(old_population)) # mutation if generations_without_improvement >= MAX_ALLOWED_GENERATIONS_WITHOUT_IMPROVEMENT: print("Chaining mutations !") mutate(population, chain_mutations=True) generations_without_improvement = 0 else: mutate(population) population.extend(old_population) # selection population, best_guy = selection_fn(population, parents) print("Generation: %d\tPopulation: %s\tFitness: %d\tBest_guy's_rotations_count: %s" % (generations, len(population), fitness(best_guy), len(best_guy[1]))) # measure improvement if fitness(best_guy) >= fitness(old_best_buy): generations_without_improvement += 1 else: generations_without_improvement = 0 generations += 1 if generations == MAX_GENERATIONS: return None else: # print("Solved in %d generations" % generations) return generations, len(best_guy[1]), calc_compression(best_guy[1])
def main(): problem = Cube() problem.scramble() results = init_results_dict() for is_tournament in USING_TOURNAMENT_SELECTION: for parents in PARENTS: for offspring in OFFSPRING: times = [] generations = [] rotations_count = [] compression = [] for i in range(REPETITIONS): start = time.process_time() result = None successful = False while not successful: result = run(problem, parents, offspring, use_tournament_selection=is_tournament) if result is not None: successful = True end = time.process_time() times.append(end - start) generations.append(result[0]) rotations_count.append(result[1]) compression.append(result[2]) results[is_tournament][parents][offspring] = ( avg(times), avg(generations), avg(rotations_count), avg(compression)) print( str(parents) + "," + str(offspring) + ": " + str(avg(times))) print('#' * 80 + '\n' * 2) print( "is_tournament parents offspring time[s] generations rotations_count compression\n" ) for is_tournament, v1 in results.items(): for parents, v2 in v1.items(): for offspring, v3 in v2.items(): print("%s\t%d\t%d\t%f\t%f\t%f\t%f" % (str(is_tournament), parents, offspring, v3[0], v3[1], v3[2], v3[3]))
def main(): problem = Cube() problem.scramble() results = init_results_dict() for is_tournament in USING_TOURNAMENT_SELECTION: for parents in PARENTS: for offspring in OFFSPRING: times = [] generations = [] rotations_count = [] compression = [] for i in range(REPETITIONS): start = time.process_time() result = None successful = False while not successful: result = run(problem, parents, offspring, use_tournament_selection=is_tournament) if result is not None: successful = True end = time.process_time() times.append(end - start) generations.append(result[0]) rotations_count.append(result[1]) compression.append(result[2]) results[is_tournament][parents][offspring] = ( avg(times), avg(generations), avg(rotations_count), avg(compression), ) print(str(parents) + "," + str(offspring) + ": " + str(avg(times))) print("#" * 80 + "\n" * 2) print("is_tournament parents offspring time[s] generations rotations_count compression\n") for is_tournament, v1 in results.items(): for parents, v2 in v1.items(): for offspring, v3 in v2.items(): print( "%s\t%d\t%d\t%f\t%f\t%f\t%f" % (str(is_tournament), parents, offspring, v3[0], v3[1], v3[2], v3[3]) )
def cube_factory(): cube = Cube() cube.scramble() return cube
def cube_factory(): cube = Cube() cube.scramble() return cube