Exemple #1
0
def evolutionary_strategies(args):

    minmax = (args.min, args.max)
    N = args.N
    gens = args.gens

    solution = common.initialize(N, minmax)
    best_fitness = common.fitness(solution[0])

    p = 1.5

    for gen in range(1, gens):
        if gen % 10 == 0:
            print("Generation :#%d" % gen)

        mutated = mutation.es_mutation(solution, minmax, p)
        #print(solution[0])
        #print(mutated[0])
        #print(list(map(operator.sub, mutated[0], solution[0])))

        fitness = common.fitness(mutated[0])

        if fitness <= best_fitness:
            best_fitness = fitness
            solution = mutated
            p = 1.5
            common.write_data(gen, fitness, 'es.dat')
        #elif fitness == best_fitness:
        #    p = 1
        else:
            p = 1.5**(-1 / 4)

        if fitness == 0:
            break

    print("#########################")
    print("# Strategy              : Evolutionary Strategies")
    print("# Generations           : " + str(gens))
    print("# Best Solution Fitness : %.3f" % best_fitness)
    print("# Log File              : ./es.dat")
    print(
        "# Graph                 : Evolutionary_Strategies_Ackleys_Function.png"
    )
    print("#########################")

    common.plot('Evolutionary Strategies: Ackleys Function', 'es.dat')
Exemple #2
0
def evolutionary_strategies(args):

    minmax = (args.min, args.max)
    N = args.N
    gens = args.gens

    solution = common.initialize(N,minmax)
    best_fitness = common.fitness(solution[0])

    p = 1.5
    
    for gen in range(1, gens):
        if gen % 10 == 0:
            print("Generation :#%d" % gen)

        mutated = mutation.es_mutation(solution, minmax, p)
        #print(solution[0])
        #print(mutated[0])
        #print(list(map(operator.sub, mutated[0], solution[0])))

        fitness = common.fitness(mutated[0])

        if fitness <= best_fitness:
            best_fitness = fitness
            solution = mutated
            p = 1.5
            common.write_data(gen, fitness, 'es.dat')
        #elif fitness == best_fitness:
        #    p = 1
        else:
            p = 1.5 ** (-1/4)
        
        if fitness == 0:
            break

    print("#########################")
    print("# Strategy              : Evolutionary Strategies")
    print("# Generations           : " + str(gens))
    print("# Best Solution Fitness : %.3f" % best_fitness)
    print("# Log File              : ./es.dat")
    print("# Graph                 : Evolutionary_Strategies_Ackleys_Function.png")
    print("#########################")
    
    common.plot('Evolutionary Strategies: Ackleys Function', 'es.dat')
    parser.add_argument("--max", help="Maximum value", type=int)
    parser.add_argument('-g', "--generations", help="Number of Generations",
                        dest='gens', type=int)

    args = parser.parse_args()
    
    minmax = (args.min, args.max)
    N = args.N
    gens = args.gens

    solution = common.initialize(N,minmax)
    best_fitness = common.fitness(solution[0])

    p = 1.5
    
    for gen in range(1, gens):
        mutated = mutation.es_mutation(solution, minmax, p)
        fitness = common.fitness(mutated[0])

        if fitness <= best_fitness:
            best_fitness = fitness
            solution = mutated
            p = 1.5
            common.write_data(gen, fitness, 'data.dat')
        else:
            p = 1.5 ** (-1/4)
        
        if fitness == 0:
            break

Exemple #4
0
def evolutionary_strategies(args):

    minmax = common.get_minmax(args.fitness)
    N = args.N
    gens = args.gens
    population_exchange = args.exchange
    exchange_individuals = args.iexchange
    islands = args.islands

    population_list = []
    fittest_list = []
    best_fitness_list = []
    
    for i in range(0,islands):
        population = common.initialize(N,minmax)
        fittest, best_fitness = common.fittest(population[0],
                                               args.fitness)
        population_list.append(population)
        fittest_list.append(fittest)
        best_fitness_list.append(best_fitness)

    p_list = [0] * islands
    successful_cases_list = [0] * islands
    
    for gen in range(1, gens):
        if gen % (gens / 10) == 0:
            print("Generation :#%d" % gen)

        if gen % population_exchange == 0:
            print(" -> Exchange in Generation: %d " % gen)
            population_list = common.exchange(population_list,
                                              exchange_individuals)
        
        mutated = [[]] * islands
        local_fittest = [0] * islands
        fitness = [0] * islands
        for i in range(0, islands):
            mutated[i] = mutation.es_mutation(population_list[i],
                                              minmax, p_list[i])

            local_fittest[i], fitness[i] = common.fittest(mutated[i][0],
                                                          args.fitness)
            
            if fitness[i] >= best_fitness_list[i]:
                fittest_list[i] = local_fittest[i]
                best_fitness_list[i] = fitness[i]
                successful_cases_list[i] += 1
                population = mutated
                
            common.write_data(gen, fitness[i], 'es%d.dat' % i)

            p_list[i] = successful_cases_list[i] / gen

    print("#########################")
    print("# Strategy              : Evolutionary Strategies")
    print("# Generations           : " + str(gens))

    fittest = 0
    best_fitness = 0
    best_island = 0
    for i in range(0, islands):
        if best_fitness_list[i] > best_fitness:
            fittest = fittest_list[i]
            best_fitness = best_fitness_list[i]
            best_island = i

    print("# Best Solution Value   : %.3f" % fittest)
    print("# Best Solution Fitness : %g" % best_fitness)
    print("# Obtained from  Island : %d" % best_island)
    print("# Log File              : ./es%d.dat" % best_island)
    print("# Graph                 : Evolutionary_Strategies_%s.png" %
          args.fitness.upper())
    print("#########################")
    
    common.plot('Evolutionary Strategies %s' % args.fitness.upper(),
                'es%d.dat' % best_island)

    files = []
    for island in range(0, islands):
        common.plot('Evolutionary Strategies %s_islands' % args.fitness.upper(),
                    'es%d.dat' % island, False)