partially mapped crossover function for tsp

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..

Answers (2)

2 Comments

for tsp path generation 2 point cross over will give the repetation of cities.......
Okay, then write your own custom crossover function that follows whatever cross-over principles you want.

Sign in to comment.

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 Comments

More comments would help.
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?

Sign in to comment.

Asked:

on 4 Mar 2019

Commented:

on 30 Sep 2019

Community Treasure Hunt

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

Start Hunting!