How to do mutation condition in a matrix?

7 views (last 30 days)
Akash Pal
Akash Pal on 26 Nov 2022
Answered: Siraj on 4 Sep 2023
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
Jan
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
Akash Pal on 28 Nov 2022
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.

Sign in to comment.

Answers (1)

Siraj
Siraj on 4 Sep 2023
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.

Categories

Find more on Language Support in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!