partially mapped crossover function for tsp

6 ビュー (過去 30 日間)
msr16
msr16 2019 年 3 月 4 日
コメント済み: Walter Roberson 2019 年 9 月 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..

回答 (2 件)

Walter Roberson
Walter Roberson 2019 年 3 月 4 日
  2 件のコメント
msr16
msr16 2019 年 3 月 4 日
for tsp path generation 2 point cross over will give the repetation of cities.......
Walter Roberson
Walter Roberson 2019 年 3 月 4 日
Okay, then write your own custom crossover function that follows whatever cross-over principles you want.

サインインしてコメントする。


Arunkumar Gopu
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
Walter Roberson 2019 年 9 月 30 日
More comments would help.
Walter Roberson
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 ExchangeTraveling Salesman (TSP) についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by