Genetic algorithm problem with integer variables
2 views (last 30 days)
Show older comments
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 Comments
Answers (0)
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!