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