Skip to content

allow_duplicate_genes=False has no effect in 2.17.0 #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
sebastianardelean opened this issue Aug 19, 2022 · 2 comments
Open

allow_duplicate_genes=False has no effect in 2.17.0 #128

sebastianardelean opened this issue Aug 19, 2022 · 2 comments
Labels
help wanted Extra attention is needed

Comments

@sebastianardelean
Copy link

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
@ahmedfgad
Copy link
Owner

Hi @sebastianardelean,

Please refer to this issue: #39

Does this solve your problem?

@ghost
Copy link

ghost commented Sep 4, 2022

Yes, it solves. Thank you!

@ahmedfgad ahmedfgad added bug Something isn't working help wanted Extra attention is needed and removed bug Something isn't working labels Feb 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants