필터 지우기
필터 지우기

How do I write a partially mapped crossover that can produce multiple pairs of chromosomes

조회 수: 5 (최근 30일)
From this link https://www.mathworks.com/matlabcentral/answers/448078-partially-mapped-crossover-function-for-tsp#answer_394090 , I'm able to produce a pair of chromosomes but I want to run the crossover function for multiple times to get multiple pairs of chromosomes.
I have 2 random parents data example,
P1: 1 | 3 7 | 6 4 10
P2: 2 | 3 4 | 7 6 10
The first and last genes (the number underlined) cannot be changed as they are the starting and endpoints. The other four genes are free to exchange at any cut point (need not be the same as the example).
I have tried to put the function into a for loop but it only produces a pair of chromosomes. Alternatively, I have tried to put the for loop inside the function but it results in the crossover function not functioning.
Here is my coding.
for z = 1:4
a = YY2(:,randi(y4))'; % Fist Vector Needed to be Cross
b = YY2(:,randi(y4))'; %Second Vector Needed to be Cross
child1 = zeros(1,size(a,2));
child2 = zeros(1,size(b,2));
[~,sizea]=size(a);
[~,sizeb]=size(b);
if sizea~=sizeb
print("a and b are not equal cant do crossover");
end
for i=1:100
r = randi([2,size(b,2)-1],1,2);
lo = min(r);
up = max(r);
if lo==up
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,a,sizea,child1);
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,b,sizeb,child2);
end
end
for q = 1:sizeb
if child2(q)==0
child2(q)=b(q);
end
end
S = child1';
T = child2';
odd = 2*z-1;
even = 2*z;
Y2(:,odd) = S;
Y2(:,even) = T;
end
Unrecognized function or variable 'YY2'.
Y2(bus+2,:) = cus_node;
YY4 = Y2;
function crossover1(n,i,a,sizea,child1)
for k=1:sizea
if child1(n) == a(k)
if child1(k) == 0
child1(k) = a(i);
break;
else
crossover1(k,i,a,sizea,child1)
end
end
end
end
function crossover2(n,i,b,sizeb,child2)
for k=1:sizeb
if child2(n) == b(k)
if child2(k) == 0
child2(k) = b(i);
break;
else
crossover2(k,i,b,sizeb,child2)
end
end
end
end
  댓글 수: 2
Debadipto
Debadipto 2023년 6월 5일
Could you please elaborate on the output you're intending to get? Wrapping the function inside a for loop should ideally give you multiple pairs of output. Maybe you can share your code snippet.
Wan
Wan 2023년 6월 7일
For example, I would like to produce 4 pairs of chromosomes :
1 3 4 6 7 10
2 3 7 4 6 10
2 7 3 6 4 10
1 4 7 6 3 10
2 6 7 3 4 10
1 7 4 3 6 10
2 4 6 3 7 10
1 6 4 7 3 10
I have fixed the starting point to be 1 and 2 and the endpoint to be 10. Only the four genes (3,4,6 and 7) will undergo crossover.

댓글을 달려면 로그인하십시오.

채택된 답변

Debadipto
Debadipto 2023년 6월 8일
Hi Wan,
You can use the following script to generate multiple pair of chromosomes:
a = [1 3 4 6 7 10];
b = [2 3 7 4 6 10];
n = 4;
chromosomes = [];
for i = 1:4
chromosomes(:, :, i) = crossover(a, b);
end
disp(chromosomes)
function child= crossover(a, b)
% crossover function logic ...
end
Moreover, you can use the “randi” function to generate random vectors for `a` and `b`.
Regards,
Debadipto Biswas

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Genetic Algorithm에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by