MATLAB Answers

1

Can you create an array of Constraints in an Optimisation MILP problem?

Asked by Humberto Lopez on 13 Jun 2019
Latest activity Edited by Matt J
on 14 Jun 2019
Hello, I am modelling power plant capacity additions but because there would be alot of plants, I am trying to code the constraints with an array, I have tryed to input the following constraint code where Eih, Eil and Cinew are continuous variables.
for i = 1:252
NewPlantCapacityConstraint=Eih(i)+Eil(i);
c(i,1)=NewPlantCapacityConstraint;
MymodelRP.Constraints.NewEnergyAllocation=NewPlantCapacityConstraint(i)<=Cinew(i,1);
end
I get this error when runing the code "OptimizationVariables appearing in the same problem, constraint, or expression must have distinct "Name" properties.
Make a new variable with a different "Name" property, or retrieve the original variable using the Variables property.". Is this because I can only code one constraint at the time? Is there a way to code more than one constraint at the time? I would really appreciate some help on the matter
Thank you very much

  0 Comments

Sign in to comment.

2 Answers

Answer by Matt J
on 13 Jun 2019
Edited by Matt J
on 13 Jun 2019

You can just write
I=1:252;
MymodelRP.Constraints.NewEnergyAllocation = ( Eih(I,1)+Eil(I,1)<=Cinew(I,1) );

  2 Comments

Yes, I have tried that from the beginning but it doesn't work, I get this message:
"Warning: Existing constraints for this problem will be overwritten. To specify multiple constraints, assign constraints using labels. For example, PROB.Constraint.myCon = CONSTR."
I do not want MATLAB to overwrite the constraint but to store each constraint or to take in accont an array of constraints if that is possible somehow. It should take in account 252 constraints, one for each plant, when solving the objective function.
Thank you, hope you can help
I would have to see more of your code to know why that's happening. There is nothing improper about defining vector-valued constraints. What version of Matlab is this?

Sign in to comment.


Answer by Matt J
on 13 Jun 2019
Edited by Matt J
on 13 Jun 2019

The error message you are getting occurs when you do things like this,
x=optimvar('Xname',252,1);
y=optimvar('Xname',252,1);
prob=optimproblem;
prob.Constraints.xLessThany=(x<=y);
instead of this,
x=optimvar('Xname',252,1);
y=optimvar('Yname',252,1);
prob=optimproblem;
prob.Constraints.xLessThany=(x<=y);
It has nothing to do with whether the constraint called "xLessThany" is array-valued.

  3 Comments

Yes, thank you, I have corrected the problem but now it is not giving me reasonable answers. I have this constraint for all xi's being integer variables with lower bound 0 and upper bound 1. But the model is taking all the upper bounds (e.g. xiasist=[1;0] and xiasisCCS=[1;0]) to solve the problem, when it should only choose one xi=1:
for j=1:2
Mymodel.Constraints.xi=xiasist(j)+xiasisCCS(j)+xswitch(j)+xswitchCCS(j)<=1;
end
Do you think I could send you my code and possibly give me an insight to my main issues?
Thank you!
I think you should post your code here. However, the for-loop above makes no sense. The left hand side does not vary with j and is simply getting overwritten with every iteration of the loop. Also, as we've now established, it should be possible to avoid loops altogether and just give the constraint in vectorized form,
Mymodel.Constraints.xi = ( xiasist + xiasisCCS +xswitch +xswitchCCS <= 1 );
if that is indeed the intended constraint.
Display the generated problem with showproblem to see if it matches what you intended.

Sign in to comment.