partially mapped crossover function for tsp

4 visualizzazioni (ultimi 30 giorni)
msr16
msr16 il 4 Mar 2019
Commentato: Walter Roberson il 30 Set 2019
Hi,
I have 2 random parents data example,
P1: 4 8 7 | 3 6 5 | 1 10 9 2
P2: 3 1 4 | 2 7 9 | 10 8 6 5
I need to perform partially mapped crossover function to find a new child.
C1: 4 8 6 | 2 7 9 | 1 10 5 3
C2: 2 1 4 | 3 6 5 | 10 8 7 9
help me in coding this
Thanks in Advance..

Risposte (2)

Walter Roberson
Walter Roberson il 4 Mar 2019
  2 Commenti
msr16
msr16 il 4 Mar 2019
for tsp path generation 2 point cross over will give the repetation of cities.......
Walter Roberson
Walter Roberson il 4 Mar 2019
Okay, then write your own custom crossover function that follows whatever cross-over principles you want.

Accedi per commentare.


Arunkumar Gopu
Arunkumar Gopu il 30 Set 2019
Modificato: Arunkumar Gopu il 30 Set 2019
Yes, i do understand that you shoud not have repeated values when working with TSP problem in you sequence of cities. You can do PMX for such kind of problem and the code goes here.
function child= crossover(a,b)
crossoverrate=0.3;
[~,sizea]=size(a);
[~,sizeb]=size(b);
child1=zeros(1,sizea);
child2=zeros(1,sizea);
if sizea~=sizeb
print("a and b are not equal cant do crossover");
end
for i=1:100
%this line will generate 2 random values
lo=randi(sizea);
rate=ceil(sizea*crossoverrate);
up=lo+rate;
if up>sizea
continue;
else
break;
end
end
for i=lo:up
child1(i)=b(i);
child2(i)=a(i);
end
% child1
for i=lo:up
d=0;
for j=lo:up
if(a(i)==child1(j))
d=d+1;
break;
end
end
if d==0
crossover1(i,i);
end
end
function crossover1(n,i)
for k=1:sizea
if child1(n)==a(k)
if child1(k)==0
child1(k)=a(i);
break;
else
crossover1(k,i)
end
end
end
end
for q=1:sizea
if child1(q)==0
child1(q)=a(q);
end
end
% child2
for i=lo:up
d=0;
for j=lo:up
if(b(i)==child2(j))
d=d+1;
break;
end
end
if d==0
crossover2(i,i);
end
end
function crossover2(n,i)
for k=1:sizeb
if child2(n)==b(k)
if child2(k)==0
child2(k)=b(i);
break;
else
crossover2(k,i)
end
end
end
end
for q=1:sizeb
if child2(q)==0
child2(q)=b(q);
end
end
%return the values here
child=[child1,
child2];
end
  2 Commenti
Walter Roberson
Walter Roberson il 30 Set 2019
More comments would help.
Walter Roberson
Walter Roberson il 30 Set 2019
Even more comments than that would help. For example what is the purpose of your two extra functions? What is their "interface contract" (inputs requirements, output types) ? What algorithm is being used?

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by