partially mapped crossover function for tsp
조회 수: 4 (최근 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!