Genetic algorithm problem with integer variables

2 次查看(过去 30 天)
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 个评论
Stefanos Mavropoulos
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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by