How to do mutation condition in a matrix?
Show older comments

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 Comments
Akash Pal
on 26 Nov 2022
Akash Pal
on 26 Nov 2022
@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
on 27 Nov 2022
Akash Pal
on 27 Nov 2022
Jan
on 27 Nov 2022
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
on 28 Nov 2022
Answers (1)
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);
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);
Refer to the following link for a mutation code for a genetic algorithm.
Hope this helps.
Categories
Find more on Entering Commands in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
