Ejemplo n.º 1
0
    ]

    for count, i in enumerate(n):
        if i[count] == 1:
            i[count] = 0
        elif i[count] == 0:
            i[count] = 1

    n = [Individual(i) for i in n]
    return n


# Monkey Patching
Individual.evaluate = evaluate
Individual.get_neighbours = get_neighbours

pop = Population(size=20,
                 optim="max",
                 sol_size=len(values),
                 valid_set=[0, 1],
                 replacement=True)

pop.evolve(
    gens=100,
    select=fps,
    crossover=single_point_co,
    mutate=binary_mutation,
    co_p=0.7,
    mu_p=0.2,
    elitism=False,
)
Ejemplo n.º 2
0
    for count, i in enumerate(n):
        i[count], i[count + 1] = i[count + 1], i[count]

    n = [Individual(i) for i in n]
    return n


# Monkey patching
Individual.evaluate = evaluate
Individual.get_neighbours = get_neighbours


pop = Population(
    size=100,
    sol_size=len(distance_matrix[0]),
    valid_set=[i for i in range(len(distance_matrix[0]))],
    replacement=False,
    optim="min",
)

pop.evolve(
    gens=100, 
    select= rank,
    crossover= pmx_co,
    mutate=swap_mutation,
    co_p=0.8,
    mu_p=0.5,
    elitism=True
)
Ejemplo n.º 3
0
    indexes around in pairs.

    Returns:
        list: a list of individuals
    """
    n = [
        deepcopy(self.representation)
        for i in range(len(self.representation) - 1)
    ]

    for count, i in enumerate(n):
        i[count], i[count + 1] = i[count + 1], i[count]

    n = [Individual(i) for i in n]
    return n


# Monkey patching
Individual.evaluate = evaluate
Individual.get_neighbours = get_neighbours

pop = Population(
    size=20,
    sol_size=len(distance_matrix[0]),
    valid_set=[i for i in range(len(distance_matrix[0]))],
    replacement=False,
    optim="min",
)

hill_climb(pop, log=1)
Ejemplo n.º 4
0
    """
    return self.representation.count(1)


def get_neighbours(self):
    n = [
        deepcopy(self.representation) for i in range(len(self.representation))
    ]

    for count, i in enumerate(n):
        if i[count] == 1:
            i[count] = 0
        elif i[count] == 0:
            i[count] = 1

    n = [Individual(i) for i in n]
    return n


Individual.evaluate = evaluate
Individual.get_neighbours = get_neighbours

pop = Population(size=20,
                 optim="max",
                 sol_size=4,
                 valid_set=[0, 1],
                 replacement=True)

hill_climb(pop)
sim_annealing(pop)
Ejemplo n.º 5
0
        stopped_early = []

        for epoch in range(config['epochs']):
            # collect data
            puzzle = collect_puzzle_data(config)

            # compute metadata on board shapes
            init_positions = find_init_positions(puzzle.puzzle_flat)

            # Monkey Patching
            Individual.create_representation = resolve_create_representation(
                config, puzzle)
            Individual.evaluate = resolve_evaluate_fct(puzzle)
            Individual.get_neighbours = None

            pop = Population(size=config['pop_size'], optim=config['optim'])

            pop.evolve(
                gens=config['gens'],
                select=globals()[config['selection']],
                crossover=resolve_crossover(
                    config,
                    init_positions),  # define operator in function above
                mutate=resolve_mutation(
                    config,
                    init_positions),  # define operator in function above
                co_p=config['co_p'],
                mu_p=config['mu_p'],
                elitism=config['elitism'],
                fitness_sharing=config['fitness_sharing'],
                diversity_measure=config['diversity_measure'],
Ejemplo n.º 6
0
        elif i[count] == 0:
            i[count] = 1

    n = [Individual(i) for i in n]
    return n


# Monkey Patching
Individual.evaluate = evaluate
Individual.get_neighbours = get_neighbours


def create_representation(self):
    sol_size = len(values)
    valid_set = [0, 1]
    return [choice(valid_set) for i in range(sol_size)]


Individual.create_representation = create_representation

if __name__ == '__main__':
    pop = Population(size=100, optim="max")

    pop.evolve(gens=100,
               select=fps,
               crossover=single_point_co,
               mutate=binary_mutation,
               co_p=0.7,
               mu_p=0.2,
               elitism=False)
    print('ok')