Hello@
I am quite new at the team and I am facing an issue with an if else equation , which describes the operation of energy storage.
I want to represent that if there is an excess in wind energy production (if wind energy productiondemand>0 then charge the storage) otherwise not.
I have used on that side a binary variable to indicate this onoff situation but I am getting an error message.
Can you please help me with that?
Equation
Charging (t,s)
Binary variable B(t,s);
Charging (t,s) .. B(t,s)=e= 1$((EGr(t,"wind")gt(sum(u,(v(t,u))));;
if else syntax

 User
 Posts: 85
 Joined: 11 months ago
Re: if else syntax
what is your error message? is it something like 'error 57 endogenous relational operations require model type dnlp'?
If yes, then the problem is you cannot use variable in if ($).
Is there any variable in the following?
EGr(t,"wind")gt(sum(u,(v(t,u))))
If yes, then the problem is you cannot use variable in if ($).
Is there any variable in the following?
EGr(t,"wind")gt(sum(u,(v(t,u))))

 User
 Posts: 45
 Joined: 1 year ago
 Location: Rosario  Argentina
Re: if else syntax
Hi,
"wind energy production" and "wind energy demand" are variables?
"wind energy production" and "wind energy demand" are variables?
Re: if else syntax
As the other answers already suggest you use an endogenous variable in a $() expression. This is not possible, because the $() is evaluated at model generation time and not passed on to the solver. You need to write this differently. What you try to express is
EGr(t,"wind") > sum(u,v(t,u)) <=> B(t,s) = 1
If you want to have a linear the implementation of such a logical expression this is more work, if the model is already nonlinear you can easily write this as:
Charging (t,s) .. B(t,s)=e= ifThen(EGr(t,"wind") >= sum(u,v(t,u)), 1, 0);
Some solvers like Lindo can linearize this internally.
The following reformulations are part of the optimization folklore and can it's not always easy to give a proper reference. A general reference could be the book by HP Williams (https://www.amazon.com/ModelBuildingM ... 1118443330).
Charging(t,s).. EGr(t,'Wind') =g= sum(u, v(t,u))  (1B(t,s))*bigM;
So if B(t,s) is 1 the second term becomes 0 and the constraint enforces "EGr(t,"wind") >= sum(u,v(t,u))", so this implements one part of the equivalence: EGr(t,"wind") > sum(u,v(t,u)) <= B(t,s) = 1. Very often the other part of the equivalence is not important or implicitly fulfilled. Leaves us with the calculation of bigM. Some lazy modellers just make this a big number (1e9) and hope for the best. Unfortunately, this gets the solver frequently in trouble. One should aims for the smallest possible bigM. We need to ensure that with any setting of EGr(t,'Wind') and sum(u,v(t,u)) this constraint is feasible (with either B(t,s)=0 or 1). So if we set bigM as the maximum of sum(u,v(t,u)) over all possible settings of v minus the minimum of EGr(t,'Wind') over all possible setting of EGr you are on the safe side.
Michael
EGr(t,"wind") > sum(u,v(t,u)) <=> B(t,s) = 1
If you want to have a linear the implementation of such a logical expression this is more work, if the model is already nonlinear you can easily write this as:
Charging (t,s) .. B(t,s)=e= ifThen(EGr(t,"wind") >= sum(u,v(t,u)), 1, 0);
Some solvers like Lindo can linearize this internally.
The following reformulations are part of the optimization folklore and can it's not always easy to give a proper reference. A general reference could be the book by HP Williams (https://www.amazon.com/ModelBuildingM ... 1118443330).
Charging(t,s).. EGr(t,'Wind') =g= sum(u, v(t,u))  (1B(t,s))*bigM;
So if B(t,s) is 1 the second term becomes 0 and the constraint enforces "EGr(t,"wind") >= sum(u,v(t,u))", so this implements one part of the equivalence: EGr(t,"wind") > sum(u,v(t,u)) <= B(t,s) = 1. Very often the other part of the equivalence is not important or implicitly fulfilled. Leaves us with the calculation of bigM. Some lazy modellers just make this a big number (1e9) and hope for the best. Unfortunately, this gets the solver frequently in trouble. One should aims for the smallest possible bigM. We need to ensure that with any setting of EGr(t,'Wind') and sum(u,v(t,u)) this constraint is feasible (with either B(t,s)=0 or 1). So if we set bigM as the maximum of sum(u,v(t,u)) over all possible settings of v minus the minimum of EGr(t,'Wind') over all possible setting of EGr you are on the safe side.
Michael