Filter löschen
Filter löschen

Real coded genetic algorithm in matlab

4 Ansichten (letzte 30 Tage)
Sandi J
Sandi J am 7 Sep. 2018
Kommentiert: Walter Roberson am 20 Nov. 2018
For create a vector of population with best solution , must randomly select two individuals P1 and P2, it also assumes that P2 is not worse than P1.
How can i do that using this code:
CostFunction=@(x) sum(x.^2);; % Cost Function
nVar=10; % Number of Decision Variables
VarSize=[1 nVar]; % Decision Variables Matrix Size
VarMin=-10; % Lower Bound of Variables
VarMax= 10; % Upper Bound of Variables
%%Initialization
empty_individual.Position=[];
empty_individual.Cost=[];
pop=repmat(empty_individual,nPop,1);
for i=1:nPop
% Initialize Position
pop(i).Position=unifrnd(VarMin,VarMax,VarSize);
% Evaluation
pop(i).Cost=CostFunction(pop(i).Position);
end

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 7 Sep. 2018
I would suggest that instead of continually trying to force P2 to not be worse than P1, that you just go ahead and generate and then sort by fitness afterwards.
... Especially if you are trying to generate unique locations. After all, it could be the case that P1 happened to be the single global minimum, in which case the only way generating P2 to be no worse would require P2 to be the same point.
  3 Kommentare
Walter Roberson
Walter Roberson am 9 Sep. 2018
Suppose
f = @(x) sum(x.^2)
This has a unique global minimum at (0, 0).
Now imagine that you randomly generate (0, 0) as P1.
Now generate P2 such that f(P2) is no worse than f(P1).
"No worse" would require that f(P2) < f(P1) or else that f(P2) == f(P1).
Now, with real valued inputs, can the sum of squares of values ever be less than 0? No. So there is no possible P2 such that f(P2) < f([0,0]) which is 0.
So for "no worse" we must have f(P2) == f(P1)
Now, for f([0,0]) = 0, is there any P2 that is not the same as [0,0] that gives the same result, 0? No, not unless you take into account underflow to 0, such as if the pair of inputs was realmin because the square of realmin is 0 even though realmin is not 0.
Therefore when you require that f(P2) be no worse than f(P1), you could be accidentally requiring that P2 be exactly the same as P1 or at least that it computes the exact same after round-off.
Now suppose that f is defined as being the absolute value of the difference between its input and the sum of the unique factors of the number, including one but excluding the number itself. Also define the result for even values to be 1, and define the result at 1 to be 0. Now let P1 be 1. The value of the function is 0 because you defined it that way. The value at all even numbers is 1, which is a higher value. Now require that the function at P2 be no worse than at P1, and also said that P2 cannot be 1.
What do you have? Well what you now have is that P2 must be an odd "perfect number". A perfect number is a number that is equal to the sum of its unique factors excluding itself. For example 6 = 1+2+3 so 6 is a perfect number.
Now here's the thing: no one knows whether any odd perfect numbers exist. People have put in long effort, but they have not been able to either find an odd perfect number or else prove that none exists. But your algorithm that requires that P2 be no worse than P1 would force the routine to solve that question, to do what hundreds of years of number theorists have not been able to do.
I think you will agree that it is much much easier to just generate some entries independently of each other and then sort the results in descending order. The second sorted result will be no worse than the first sorted result, without any need for any elaborate search for a no worse result that might not exist.
Sandi J
Sandi J am 9 Sep. 2018
Good explanation, it's clear enough, thanks.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Aaina
Aaina am 19 Nov. 2018
Hello, may I ask why my code has error:
Error using Crossover
Too many input arguments.
Error in nsga2 (line 83)
[popc(k,1).Position,popc(k,2).Position]=Crossover(p1.Position,p2.Position);
the code for Crossover:
function [y1,y2]=Crossover(x1,x2)
alpha=rand(size(x1));
y1=alpha.*x1+(1-alpha).*x2;
y2=alpha.*x2+(1-alpha).*x1;
end
  8 Kommentare
Aaina
Aaina am 20 Nov. 2018
well, im trying to change the selection method(random selection) to tournament method in nsga II coding,
Walter Roberson
Walter Roberson am 20 Nov. 2018
At some point you will try to tournament two positions of different length at the same time and will try to crossover them . How do you want to define the result?

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by