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
採用された回答
その他の回答 (1 件)
Aaina
2018 年 11 月 19 日
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
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
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
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
2018 年 11 月 19 日
wowwwww.... u solved my problem, thak you soooo much
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
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
2018 年 11 月 20 日
well, im trying to change the selection method(random selection) to tournament method in nsga II coding,
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 Exchange で Performance and Memory についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!