How to do mutation condition in a matrix?

I mean in case of second matrix B ,in first row for the first part only one 1 is there and in the 2nd part it has two 1 so it’s ok ,but for the first part also I want two 1 after mutation .
Can I make any condition for it?
also for my mutation iAm using the mutation rate is Pm=0.1.
function [B,mutated] =mutation(A,Pm)
mutated = find(rand(size(A))<Pm);
B = A;
B(mutated) = 1-A(mutated);
end

댓글 수: 7

Akash Pal
Akash Pal 2022년 11월 26일
This is the real example what i did in matlab ,we can see that in case of the result popnew1 ,the first row if we divide in two parts then it will be separate in two part each part will take 4 column value ,where incase of first part we can see that only one 1 is present what actually chaged AFTER MUTATION , can we make any condition that until in both the part there will be two number of 1 until that time the mutation will continue .
function [popnew1,mutated] =mutation(A,Pm)
mutated = find(rand(size(A))<Pm); % getting the mutation point
popnew1 = A;
% I am writting this condition but it's not giving any answer
if popnew1(mutated)==0
popnew1(mutated) = 1-A(mutated);
else
if popnew1(mutated)==1
popnew1(mutated) = 2-A(mutated);
end
end
% until this part
end
Jan
Jan 2022년 11월 26일
편집: Jan 2022년 11월 27일
@Akash Pal: The if command needs a scalar condition. Your variable mutated is not a scalar and in consequence popnew1(mutated)==0 is not also.
Maybe you mean:
function [A, mutated] = mutation(A, Pm)
mutated = (rand(size(A)) < Pm); % Logical indexing, not need for FIND
m0 = A(mutated) == 0;
m1 = A(mutated) == 1;
A(m0) = 1 - A(m0);
A(m1) = 2 - A(m1); % [EDITED] Typo: "A(1)" -> "A(m1)"
end
Akash Pal
Akash Pal 2022년 11월 27일
Thanks for your reply ,i understand the problem ,but in my case what i wanted that is not coming from the code you have given ,Because this code when i am executing its showing one value that is 2 ,which i dont want .
Akash Pal
Akash Pal 2022년 11월 27일
Your coding result
Please post code and messages as text, not as screenshots.
It is useful, if you try to understand the meaning of a suggested solution:
A(m0) = 1 - A(m0);
A(m1) = 2 - A(1);
This was a typo, obviously. "A(1)" must be "A(m1)".
I did not understand the explanations in the question. I've posted only a simplified version of your code.
Please explain it clearly again: What do you call "mutation"? What does the "1st" and "2nd" parts mean?
Akash Pal
Akash Pal 2022년 11월 28일
Yes ,after changing the code it's work fine. The first and the second part is just the two part of a CHROMOSOME SET . i split the chromosome set into two part where equal numbwe of gene is present in every part . So my question was ,when i am doing the mutation some of my gene was changing to 0 from 1 that time in my chromosome set most of the values were 0 .So i intended to do the things that in my every part at least two number of gene should be 1 ,after mutation also ,so i wanted to develope the logic.

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

답변 (1개)

Siraj
Siraj 2023년 9월 4일
Hi! It is my understanding that you want to mutate the matrix in such a way that when the mutated matrix is divided (vertical division) into 2 equal halves, the first row of the first half and the first row of the second half should contain at least 2 ones.
We can initially do the mutation and then proceed to check if the mutated matrix adheres to the above criteria. If it does not meet the specified condition, we can take appropriate action by reversing some of the mutations until the condition is satisfied.
Refer to the following code for better understanding. This code can be modified for your requirements.
rng(0);
A = randi([0 1], 3,8);
Pm = 0.9;
disp(A);
1 1 0 1 1 0 1 0 1 1 1 0 0 0 1 1 0 0 1 1 1 1 1 1
B = A; % B will be the mutated matrix.
%element's indexes that will be mutated.
indexes = (rand((size(A))) < Pm);
m0 = A(indexes) == 0;
m1 = A(indexes) == 1;
B(m0) = 1; % mutation of 0 to 1;
B(m1) = 0; % mutation of 1 to 0;
%% Check if the first part of the first row of the mutated matrix has atleast 2 ones or not
mid = round(size(B,2)/2);
count_of_1 = nnz(B(1,1:mid));
if(count_of_1 == 0)
% in this case we need to reverse mutate 2 positions in the first row.
reverse_mut = 0;
for i = 1:mid
if(A(1,i) == 1 && B(1,i) == 0)
B(1,i) = 1; %reverse mutate.
reverse_mut = reverse_mut+1;
if(reverse_mut == 2) % as soon as we reverse mutate 2 positions we break this loop
break
end
end
end
else if( count_of_1 == 1)
for i = 1:mid
if(A(1,i) == 1 && B(1,i) == 0)
B(1,i) = 1; %reverse mutate.
break; % since only one reverse mutation is required.
end
end
end
end
%% Check if the second part of the first row of the mutated matrix has atleast 2 ones or not
count_of_1 = nnz(B(1,mid+1:end));
if(count_of_1 == 0)
% in this case we need to reverse mutate 2 positions in the first row.
reverse_mut = 0;
for i = mid+1:size(B,2)
if(A(1,i) == 1 && B(1,i) == 0)
B(1,i) = 1; %reverse mutate.
reverse_mut = reverse_mut+1;
if(reverse_mut == 2) % as soon as we reverse mutate 2 positions we break this loop
break
end
end
end
else if( count_of_1 == 1)
for i = mid+1:size(B,2)
if(A(1,i) == 1 && B(1,i) == 0)
B(1,i) = 1; %reverse mutate.
break; % since only one reverse mutation is required.
end
end
end
end
disp(B);
1 0 1 0 1 1 0 0 0 0 0 1 1 0 0 0 1 1 0 0 1 0 1 1
Refer to the following link for a mutation code for a genetic algorithm.
Hope this helps.

카테고리

도움말 센터File Exchange에서 Mathematics and Optimization에 대해 자세히 알아보기

질문:

2022년 11월 26일

답변:

2023년 9월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by