Skip to content

allow_duplicate_genes=False has no effect in 2.17.0 #128

Open
@sebastianardelean

Description

@sebastianardelean

Hi, I tested the pygad library version 2.17.0 for solving the knapsack problem and I observed that even if I set allow_duplicate_genes=False, I still have duplicated genes in the solution.

As shown in the following solution, It can be seen that gene -5 is duplicated, -25 also.

 [28, -3, 12, -21, 18, 24, -22, 24, -30, 2, 28, -27, 21, -5, -5, 5, -1, 7, 12, 23, -9, -15, -21, -16, -16, -23, 26, -14, -25, -25]

I instantiate pygad as following

ga_instance = pygad.GA(num_generations=METAHEURISTIC_NO_OF_GENERATIONS,
                           num_parents_mating=METAHEURISTIC_NUM_PARENS_MATING,
                           gene_type=int,
                           fitness_func=fitness_func,
                           sol_per_pop=METAHEURISTIC_POPULATION_SIZE,
                           num_genes=METAHEURISTIC_GENE_LENGTH,
                           init_range_low=METAHEURISTIC_GENE_LOW,
                           init_range_high=METAHEURISTIC_GENE_HIGH,
                           parent_selection_type=METAHEURISTIC_SELECTION_TYPE,
                           keep_parents=METAHEURISTIC_KEEP_PARENTS,
                           crossover_type=METAHEURISTIC_CROSSOVER_TYPE,
                           crossover_probability=METAHEURISTIC_CROSSOVER_RATE,
                           mutation_type=METAHEURISTIC_MUTATION_TYPE,
                           mutation_probability = METAHEURISTIC_MUTATION_RATE,
                           #                       mutation_probability = METAHEURISTIC_MUTATION_PROBABILITY,
                           #mutation_percent_genes=METAHEURISTIC_MUTATION_PERCENT_GENES,
                           stop_criteria="saturate_75",
                           allow_duplicate_genes=False,
                           parallel_processing=['process', 10]
                           )

and the fitness function is defined as:

def fitness_func(solution, solution_idx):
    # print(solution)
    calculated_mass = 0
    calculated_value = 0
    output = 0

    unique_elements = check_duplicates(solution)
    if len(unique_elements) < len(solution):
        return 1/sys.maxsize

    for gene in solution:
        position = abs(gene) - 1
        if gene > 0:
            calculated_mass += items[position].m
            calculated_value += items[position].v
    output = fitness_function(calculated_mass, calculated_value)
    if calculated_mass > MAX_ALLOWED_MASS:
        return 1/sys.maxsize
    return output

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions