if else syntax

Problems with syntax of GAMS
Post Reply
Posts: 1
Joined: 11 months ago

if else syntax

Post by cpap81 » 11 months ago


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 production-demand>0 then charge the storage) otherwise not.

I have used on that side a binary variable to indicate this on-off situation but I am getting an error message.
Can you please help me with that?

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))));;

Posts: 85
Joined: 10 months ago

Re: if else syntax

Post by yanzhiping » 10 months ago

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?

Posts: 45
Joined: 11 months ago
Location: Rosario - Argentina

Re: if else syntax

Post by Manassaldi » 10 months ago

"wind energy production" and "wind energy demand" are variables?

User avatar
Posts: 62
Joined: 1 year ago

Re: if else syntax

Post by bussieck » 10 months ago

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 non-linear 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/Model-Building-M ... 1118443330).

Charging(t,s).. EGr(t,'Wind') =g= sum(u, v(t,u)) - (1-B(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.


Post Reply