pythongeneticpygad

in PyGAD how Can I get non duplicate genes eventhough I give parameter allow_duplicate_genes=False


Hi I am trying to solve a Tsp problem by using PyGAD I made a result any how but result make duplicated number

I put in initial_population:

[[ 1 12 26 19 22 20  6 15 17 23 21  7 28  5 13 14 16  2 24  4  3 10  9  8  18 25 27 11]
 [ 2 17 23 27 22 12 20 21 24 25 13  5 10  4  9 26  7  1 11  3 15 18 16 14 8 19 28  6]
 [ 3 23 12  7  2 11 15 13 19 26 21 14  9  5 24 20 25  1  8 16 22 28 27 10 4  6 18 17]
 [ 4 19  2 25 21 13  98 18 28  7 27 20 11 23 22 14  1 10 16 12  5 26 24 17  3 15  6]
 [ 5  9 19  7 22 10 11 13  1 25  6 17  8 12  2 24 28 20 26  4 15 14 18 23 21 27  3 16]]

but result was [ 6 8 13 1 19 10 6 23 18 22 5 3 *21* 11 6 16 28 1 4 10 6 25 7 22 5 3 *21* 11]

You can see in here that some values are duplycated

this is part of my code

import pygad
import numpy as np
import copy


def fitness_func(solution, solution_idx):
        distance_treshold=np.load('distance.npy')
        function_inputs=distance_simple(distance_treshold)
        a1=treshold(function_inputs)
        f=0
        for i in range(len(solution)):
         if i == 0:
            f+= distance_treshold[solution[0]][solution[i+1]]   
         else:
          try:
            f+= distance_treshold[solution[i]][solution[i+1]]
   
          except:

             f+=distance_treshold[solution[i]][solution[0]]
                    
               
        
        fitness_score=pow((a1)/f,2)#fitness
        return fitness_score

def treshold(solution):
    distance_treshold=np.load('distance.npy')
    f=0
    for i in range(len(solution)):
             if i == 0:
                f+= distance_treshold[solution[0]][solution[i+1]]    
             else:
                try:
                    f+= distance_treshold[solution[i]][solution[i+1]]
   
                except:

                    f+=distance_treshold[solution[i]][solution[0]] 
    return f


function_inputs=distance_simple(distance_treshold)
a1=treshold(function_inputs)
print(a1)
np.load('distance.npy')

#print(initial_pop)
initial_population=np.load('inital_generation.npy')
print(initial_population)
num_parents_mating= 2
num_generations= 30
parent_selection_type='sus'
mutation_type="swap"
keep_parents=0
mutation_num_genes=1
mutation_percent_genes=3
crossover_type="single_point"
allow_duplicate_genes=False
gene_type=int
mutation_probability=0.03

print("GA start")
ga_instance = pygad.GA(num_generations=num_generations,mutation_probability=mutation_probability,
parent_selection_type=parent_selection_type,initial_population=initial_population,
num_parents_mating=num_parents_mating, fitness_func=fitness_func,gene_type=gene_type, 
mutation_percent_genes=mutation_num_genes,mutation_num_genes=mutation_percent_genes,
mutation_type=mutation_type,allow_duplicate_genes=False)


ga_instance.run()
ga_instance.plot_fitness()
best_solution,best_solution_fitness,best_match_idx=ga_instance.best_solution()
print(best_solution)
fitness_func(best_solution,0)
print(best_solution_fitness)

I also saw How to solve TSP problem using pyGAD package? so I Tried allow_duplicate_genes=False but it doesn't works. Also I tried input initial_population type as numpy, but it still not works

thank you for your helps It helps me a lot


Solution

  • Thanks for using PyGAD :)

    For the allow_duplicate_genes parameter to work and prevent duplicate genes, the number of distinct gene values must be higher than or equal to the number of genes. Let me explain further.

    Assume that the gene space is set to [0.4, 7, 9, 2.3] (with only 4 values) and there are 5 genes. In this case, it is impossible to prevent duplicates because at least 2 genes will share the same value. To solve this issue, you have to add other values to the gene space so that the number of values in the space is >= the number of genes (5 in this case).

    TO solve your issue, you may use the gene_space parameter and give it enough values to prevent duplicates. This is already used in the question you mentioned.