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 个评论

What is "index" in this context?
Thanks for the answer.
i:refers to the plant i
t:refers to time period t
j: j=1 refers to production variables, while j=2 refers to on/off state for the plant i the period t
Hence, I use index to number the variables in one-dimension since the variable matrix x needs to be one-dimensional.
So the on/off state of the plant 2 the period 3 would be f(2,3,2)=42
function f = index(i,t,j)
global N
global T
f=i+(t-1)*N+(j-1)*N*T;
Update: I transferred all the linear constraints to the non-linear constraint and now the script runs but matlab refuses to recognize the integer variables as integer! I made another question about that:
You could recode your index function in terms of sub2ind()
Is the only way to define binary variables in matlab, if you define them as integer and then constraint them [0,1]?
It always gives me as a solution values close to 0 for all binary variables. (0.0142, -0.02312, ...).
You haven't shown us your nonlinear constraints.
function [c,ceq] = constraint(x)
%%Global variables
global N
global T
global G
global D
%%Nonlinear Constraints (Max,min production, ramp rates)
m=1;%nonlinear constraint m
for t=1:T
C=0;
for i=1:N
I=index(i,t,1);
J=index(i,t,2);
J1=index(i,t+1,2);
%Max,min power generation
%x(i,t)<=MAX*u(i,t)
c(m)=x(I)-x(J)*G(i,1);
%MIN*u(i,t)<=x(i,t)
c(m+1)=-x(I)+x(J)*G(i,2);
%The integer values are binary to represent on/off for plants
c(m+2)=x(J)-1;
c(m+3)=-x(J);
m=m+4;
%Ramp rates
if t<T & x(J)==1 & x(J1)==1
I1=index(i,t+1,1);
%x(i,t)*RD?x(i,t+1)
c(m)=x(I)*G(i,10)-x(I1);
%x(i,t+1)?x(i,t)*RU
c(m+1)=x(I1)-x(I)*G(i,9);
m=m+2;
end
%If a power plant is closed, it shall remain closed for some hours
if t<T & x(J)==1 & x(J1)==0
tt=t+2;
while tt<T & tt<t+G(i,8)+1
c(m)=x(index(i,tt,2));
m=m+1;
tt=tt+1;
end
end
%Total supply in period t calculation
C=C+x(I);
end
%Total supply in period t has to match demand
c(m)=D(t)-C;
m=m+1;
end
%%Ceq nonlinear equalities
ceq=[];
Is it possible that the ga can't solve it because of the number of integer variables. Each one of them can have 2 states (on/off:1/0). Hence, there are 2^32 combinations=4bn. That's why I have included a loop "If a power plant is closed, it shall remain closed for some hours" to keep a power plant closed for a few hours (G(i,8)) and drastically reduce the number of combinations.
What are 'G' and 'D' in your code?

请先登录,再进行评论。

回答(0 个)

类别

Community Treasure Hunt

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

Start Hunting!

Translated by