partially mapped crossover function for tsp
10 次查看(过去 30 天)
显示 更早的评论
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..
0 个评论
回答(2 个)
Walter Roberson
2019-3-4
Use a CrossOverFcn of 'crossovertwopoint' https://www.mathworks.com/help/gads/genetic-algorithm-options.html#f7820
2 个评论
Walter Roberson
2019-3-4
Okay, then write your own custom crossover function that follows whatever cross-over principles you want.
Arunkumar Gopu
2019-9-30
编辑:Arunkumar Gopu
2019-9-30
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 个评论
Walter Roberson
2019-9-30
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?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Traveling Salesman (TSP) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!