Genetic algorithm problem with integer variables
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
Walter Roberson
on 11 Jul 2015
What is "index" in this context?
Stefanos Mavropoulos
on 13 Jul 2015
Edited: Stefanos Mavropoulos
on 13 Jul 2015
Walter Roberson
on 13 Jul 2015
You could recode your index function in terms of sub2ind()
Stefanos Mavropoulos
on 13 Jul 2015
Matt J
on 13 Jul 2015
You haven't shown us your nonlinear constraints.
Stefanos Mavropoulos
on 14 Jul 2015
Ghada Saleh
on 16 Jul 2015
What are 'G' and 'D' in your code?
Answers (0)
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!