Genetic algorithm problem with integer variables
이전 댓글 표시
I have created an optimization algorithm using ga.
The problem has 32 real variables (plant production) and 32 integer variables (binary: open/closed plant).
N=4;
T=8;
nvars=N*T*2;
IntCon=[N*T+1:N*T*2];
[x,fval,exitflag,output,population,score] = ga(@objective,nvars,A,b,[],[],lb,ub,@constraint,IntCon,options);
These are the linear constraints where I constraint the integer variables to be between lb(J)=0 and ub(J)=1 (because they are binary):
for t=1:T
for i=1:N
I=index(i,t,1);
lb(I)=0;
ub(I)=G(i,1);
J=index(i,t,2);
lb(J)=0;
ub(J)=1;
A(t,index(i,t,1))=-1;
A(t,index(i,t,2))=0;
end
b(t)=-D(t);
end
This is the fitness function:
function f = objective(x)
global G
global N
global T
f=0;
for i=1:N
for t=1:T
I=index(i,t,1);
J=index(i,t,2);
%if the plant is open then it has costs
if x(J)==1
f = f+G(i,3)*(x(I))^2+G(i,4)*x(I)+G(i,5);
end
end
end
When I run the program it immediately stops and says:
Subscripted assignment dimension mismatch.
...
Error in ga (line 351)
...
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
These are the ga lines 350:365:
if ~isempty(intcon)
[x,fval,exitFlag,output,population,scores] = gaminlp(FitnessFcn,nvars, ...
Aineq,bineq,Aeq,beq,lb,ub,NonconFcn,intcon,options,output,Iterate);
else
switch (output.problemtype)
case 'unconstrained'
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
options,output,Iterate);
case {'boundconstraints', 'linearconstraints'}
[x,fval,exitFlag,output,population,scores] = galincon(FitnessFcn,nvars, ...
Aineq,bineq,Aeq,beq,lb,ub,options,output,Iterate);
case 'nonlinearconstr'
[x,fval,exitFlag,output,population,scores] = gacon(FitnessFcn,nvars, ...
Aineq,bineq,Aeq,beq,lb,ub,NonconFcn,options,output,Iterate,type);
end
end
The algorithm runs if all integer variables are set to 1:
lb(J)=1;
ub(J)=1;
However, in that case, the integer variables appear in the results as real: 1.000
Does the algorithm understand that these variables are integers?
댓글 수: 7
Walter Roberson
2015년 7월 11일
What is "index" in this context?
Stefanos Mavropoulos
2015년 7월 13일
편집: Stefanos Mavropoulos
2015년 7월 13일
Walter Roberson
2015년 7월 13일
You could recode your index function in terms of sub2ind()
Stefanos Mavropoulos
2015년 7월 13일
Matt J
2015년 7월 13일
You haven't shown us your nonlinear constraints.
Stefanos Mavropoulos
2015년 7월 14일
Ghada Saleh
2015년 7월 16일
What are 'G' and 'D' in your code?
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Genetic Algorithm에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!