Can you create an array of Constraints in an Optimisation MILP problem?
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
Matt J
el 13 de Jun. de 2019
Editada: Matt J
el 13 de Jun. de 2019
You can just write
I=1:252;
MymodelRP.Constraints.NewEnergyAllocation = ( Eih(I,1)+Eil(I,1)<=Cinew(I,1) );
2 comentarios
Matt J
el 13 de Jun. de 2019
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?
Matt J
el 13 de Jun. de 2019
Editada: Matt J
el 13 de Jun. de 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 comentarios
Matt J
el 14 de Jun. de 2019
Editada: Matt J
el 14 de Jun. de 2019
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.
Mary Fenelon
el 14 de Jun. de 2019
Display the generated problem with showproblem to see if it matches what you intended.
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!