def start_pso(): logger.info("*" * 80) logger.info("*" * 80) logger.info("Particle Swarm Optimization") logger.info("*" * 80) logger.info("Number of Generations: %s", NUMBER_OF_GENERATIONS) logger.info("Population Size: %s", POPULATION_SIZE) logger.info("Function: %s", SELECTED_FUNCTION) logger.info("Epsilon: %s", EPSILON) logger.info("*" * 80) logger.info("*" * 80) # Generate population[] of Particle() population = generate_population() # Get the best position in population best_of_all = get_best_of_all(population, population[0].best) logger.info("Best: %s Fitness: %s", best_of_all, evaluate(best_of_all)) for y in range(NUMBER_OF_GENERATIONS): if abs(evaluate(best_of_all)) < EPSILON: # Stop condition logger.info("*" * 80) logger.info("Best solution found at %s generation.", y) logger.info("Best of all: %s Fitness: %s", str(best_of_all), str(evaluate(best_of_all))) logger.info("*" * 80) break for particle in population: # Move each particle using its velocity particle.move_position(best_of_all) # Update best position ever in population's history best_of_all = get_best_of_all(population, best_of_all) # logger,info("%s", particle) if y % PRINTING_INTERVAL == 0: logger.debug("-" * 80) logger.debug("Generation: %s", y) logger.debug("Best of all: %s Fitness: %s", str(best_of_all), str(evaluate(best_of_all))) logger.debug("-" * 80) logger.info("*" * 80) logger.info("No better solution found. ") logger.info("Best solution at %s generation", NUMBER_OF_GENERATIONS) logger.info("Best of all: %s Fitness: %s", str(best_of_all), str(evaluate(best_of_all))) logger.info("*" * 80) return population
def show_population(population, fitness): logger.info("Population ") for index in range(0, POPULATION_SIZE): logger.info("%f => %f", (population[index], fitness[index]))
def start_evaluation(): logger.info("*" * 80) logger.info("*" * 80) logger.info("Starting Strassen Finder") logger.info("*" * 80) logger.info("Number of Generations: %s", NUMBER_OF_GENERATIONS) logger.info("Chromosome Length: %s", CHROMOSOME_LENGTH) logger.info("Population Size: %s", POPULATION_SIZE) logger.info("*" * 80) logger.info("*" * 80) # generating population population = generate_population(-1, 1) fitness = strassen_fitness(population) # ancestor = fitness[0] for i in range(0, NUMBER_OF_GENERATIONS): if fitness[0] < EPSILON: logger.info("*" * 80) logger.info("Best solution found at %s generation.", i) logger.info(population[0]) logger.info(fitness[0]) logger.info("*" * 80) break # There's no change, let's create an unfortunate event! if (ancestor == fitness[0]) and (i % (PRINTING_INTERVAL / 2) == 0): logger.debug("Wake Up!!!!") new_sons = one_point_crosses(population[0], population[1]) population[2] = new_sons[0] population[-1] = random_mutation(population[-1], -1, 1) else: ancestor = fitness[0] new_population = generate_new_population(population, fitness, mutator=strassen_mutation) population = sorted(new_population, key=evaluate_product, reverse=False)[0:POPULATION_SIZE] fitness = strassen_fitness(population) if i % PRINTING_INTERVAL == 0: # logger.debug("-" * 80) logger.debug("Generation %s ", i) logger.debug("BEST :") logger.debug("Single %s ", population[0]) logger.debug("Fitness %s ", fitness[0]) logger.debug("." * 80) logger.debug("WORST :") logger.debug("Single %s ", population[-1]) logger.debug("Fitness %s ", fitness[-1]) logger.debug("-" * 80) logger.info("*" * 80) logger.info("No better solution found. ") logger.info("Best solution at %s generation", NUMBER_OF_GENERATIONS) logger.info(population[0]) logger.info(fitness[0]) logger.info("*" * 80) return population