Real coded genetic algorithm in matlab

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

 採用された回答

Walter Roberson
Walter Roberson 2018 年 9 月 7 日

0 投票

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 件のコメント

Sandi J
Sandi J 2018 年 9 月 8 日
What do you mean by 'P2 to be the same point'?
Walter Roberson
Walter Roberson 2018 年 9 月 9 日
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 2018 年 9 月 9 日
Good explanation, it's clear enough, thanks.

サインインしてコメントする。

その他の回答 (1 件)

Aaina
Aaina 2018 年 11 月 19 日

0 投票

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 件のコメント

Aaina
Aaina 2018 年 11 月 19 日
編集済み: madhan ravi 2018 年 11 月 19 日
the line of the error:
% Use Tournament Selection method
i1=TournamentSelection(pop,TournamentSize);
i2=TournamentSelection(pop,TournamentSize);
% Select Parents
p1=pop(i1);
p2=pop(i2);
% Apply Crossover
[popc(k,1).Position,popc(k,2).Position]=Crossover(p1.Position,p2.Position);
Aaina
Aaina 2018 年 11 月 19 日
編集済み: madhan ravi 2018 年 11 月 19 日
Code for Tournament Selection:
function i=TournamentSelection(pop,m)
nPop=numel(pop);
S=randsample(nPop,m);
spop=pop(S);
scosts=[spop.Cost];
[~, j]=min(scosts);
i=S(j);
end
Walter Roberson
Walter Roberson 2018 年 11 月 19 日
i1 and i2 are vectors, so pop(i1) and pop(i2) are not scalars. pop is a non-scalar structure array, so p1 = pop(i1) and p2 = pop(i2) are non-scalar structure arrays. When you use p1.Position, you are getting structure expansion, so p1.Position and p2.Position each expand to many arguments.
I suggest
[p1, p2] = Crossover(vertcat(p1.Position), vertcat(p2.Position));
p1c = num2cell(p1,2); p2c = num2cell(p2,2);
[popc(k,1).Position] = p1c{:};
[popc(k,2).Position] = p2c{:};
You are switching back and forth between fields of separate structure array entries, or treating everything as a group. Fields of separate structure array entries are effectively separate variables as far as storing is concerned in MATLAB, so it is not possible to just output two variables the way you were doing.
Aaina
Aaina 2018 年 11 月 19 日
wowwwww.... u solved my problem, thak you soooo much
Aaina
Aaina 2018 年 11 月 19 日
編集済み: Aaina 2018 年 11 月 19 日
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in final_run_withdg (line 70)
[loc1_data, loc2_data] = cross_over_process(vertcat(loc1_data.Position),
vertcat(loc2_data.Position),minval1,maxval1,minval2,maxval2);
what is that mean?
minval1=1;
maxval1=14;
minval2=0.05;
maxval2=0.2;
Walter Roberson
Walter Roberson 2018 年 11 月 19 日
You have position data that is not the same number of elements for each entry . It is possible to deal with it but it will require rewriting the crossover function . But before doing that I have to ask how it can be meaningful to do genetic function work when the position vectors are not the same size as each other ? The randomized selection you use makes it almost certain that at some point you will be asking to cross over a p1 with aa corresponding p2 that is aa different length .
Aaina
Aaina 2018 年 11 月 20 日
well, im trying to change the selection method(random selection) to tournament method in nsga II coding,
Walter Roberson
Walter Roberson 2018 年 11 月 20 日
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?

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by