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 .
How to do mutation condition in a matrix?
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Jan
il 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?
Risposte (1)
Siraj
il 4 Set 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);
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.
0 Commenti
Vedere anche
Categorie
Scopri di più su Language Support in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!