Reputation: 11
So I have used a GA for TSP where the initial population is generated using Nearest Neighbor Method with different starting points. When my rate of mutation is >0, the initial best in the population remains the best when the rate of mutation is 0, the improvement happens but only in short bursts as shown in the following image:
I tried increasing the rate of mutation iteratively but that is not beneficial
here is my code for mutation:
def mutate(route,rate):
for i in range(len(route)):
#if (random.uniform(0,1) < rate):
if (random.uniform(0,1) < rate):
j=int(random.random()*len(route))
cityA=route[i]
cityB=route[j]
route[i]=cityB
route[j]=cityA
return route
here is my code for making children:
def make_babies(parent1, parent2):
child = []
child_1 = []
half1_1=[]
half1_2=[]
child_2 = []
half2_1=[]
half2_2=[]
splicepoint1 = int(random.random() * len(parent1))
splicepoint2 = int(random.random() * len(parent1))
startsplice = min(splicepoint1, splicepoint2)
endsplice = max(splicepoint1, splicepoint2)
for i in range(startsplice, endsplice):
half1_1.append(parent1[i])
#print(parent2)
half2_1.append(parent2[i])
half1_2 = [j for j in parent2 if j not in half1_1]
half2_2 = [j for j in parent1 if j not in half2_1]
child_1 = half1_1+half1_2
child_2 = half2_1+half2_2
return [child_1,child_2]
Even after this there are criss-crossing edges Please help me
EDIT1:- I have ensured eltism for best routes Here is my fitness function in case here lies an error:
sum=0
fitness_sum=0
fitness=[]
for i in range(len(sol)):
fitness_sum=fitness_sum+(1/sol[i][1])**2
for j in range(len(sol)):
a=((1/(sol[j][1])**2)/(fitness_sum))
fitness.append(a)
return fitness
Upvotes: 1
Views: 100
Reputation: 834
Your best is bound to improve depending on the size of the candidate population, but not necessarily on each iteration which explains the 'short bursts' (since the best may cross-over to a worse solution, while alternatives exhibit limited improvements).
Also, your mutation function is a cross-over function, as mutation typically introduces a new candidacy within a route's genotype from potentially unexplored candidacy within the phenotype.
Hence, you have 2 cross-over functions, and depending on your selection of candidates to the next iteration, are bound by a local optimum defined in your initial population.
Finally, criss-cross edges are irrelevant since graphs are not constrained to euclidian space, and edges are uni or bi-directional.
Upvotes: 1