## Conditional statement with variable

Problems with syntax of GAMS
FrancescaC
User Posts: 3
Joined: 1 month ago

### Conditional statement with variable

Hello,

I am working on an energy system optimization with a storage device. I need the power stored or delivered by the battery to be = 0 when the state of charge is max (>= 100%) or min (<= 0%) respectively. The problem is both the power and the SOC are variables defined at each timestep. The parts of my code related to this are:

Code: Select all

``````Set     t       hours               /t1*t24/;

Scalar  PdisN   /241.5/;	*Discharge power of 1 cell
Scalar  PchN    /395/;		*Charge power of 1 cell
Scalar  DSOCdis	/-0.01/;	*SOC decrease while discharging at max power
Scalar  DSOCch	/0.01/;		*SOC increase while charging at max power
[...]

Variables           SOC(t);
Integer variables   N_cells;
Positive variables  pSTOR_Dis(t), pSTOR_Ch(t), [...];
Binary variable     U;
N_cells.lo = 1;
SOC.lo(t) = 0; SOC.up(t) = 1; SOC.l(t) = 0.5;

Equations   STORdis, STORch, SOCchange [...];
STORdis(t)..    pSTOR_Dis(t) =l= N_cells * PdisN * U(t);
STORch(t)..     pSTOR_Ch(t) =l= N_cells * (-PchN) * (U(t) - 1);
SOCchange(t)..  SOC(t) =e= SOC(t-1) + (pSTOR_Dis(t) / (PdisN * N_cells)) * DSOCdis - (pSTOR_Ch(t) / (PchN * N_cells)) * DSOCch;
[...]
``````
I tried inserting two other equations:

Code: Select all

``````STORfull(t) \$ (SOC(t) ge 0.95)..  pSTOR_Ch(t) =e= 0;
STORempty(t) \$ (SOC(t) le 0.05).. pSTOR_Dis(t) =e= 0;``````
but it didn't work. What can I do instead?

(I am using MINLP solver for now, but I still have to choose the best solver for the problem)

abhosekar
Moderator Posts: 209
Joined: 1 year ago

### Re: Conditional statement with variable

You can't use conditions on variables. Until you solve, variables don't have a value and using \$ condition on variable while defining your constraint does not make much sense. Please refer to the following FAQ.

viewtopic.php?f=15&t=12108

Hope this helps.

- Atharv

FrancescaC
User Posts: 3
Joined: 1 month ago

### Re: Conditional statement with variable

Thank you Atharv!

I understood why I can't use conditional statements on variables.
I also looked into the bigM method, which would work perfectly if my "control" variable was binary, for example:

Code: Select all

``````Set t 'time' /1:24/
Binary variable U(t) 'State of the battery ON-OFF'
Variable Pbatt
Parameters V, I
Equations
if U(t) = 1 then Pbatt(t) = V*I
if U(t) = 0 then Pbatt(t) = 0
``````

But in my case the control variable is a real positive number (the state of charge, varying from 0 to 1), therefore the bigM method cannot be applied.

I also tried to reformulate the parameters so that one of the Pbatt factors would be 0 in case of SOC = 0 (or 1 for charging), but there is no function of SOC that can replicate this behaviour.

Do you know any other way to model this?

Thank you!

abhosekar
Moderator Posts: 209
Joined: 1 year ago

### Re: Conditional statement with variable

You can use big-M to declare another variable that is 1 if say SOC is greater than 0.95 or less than 0.05 based on your need.

Let's say you want a binary variable to be 1 if SOC(t) is greater than 0.95. You can do the following:

binary variable b(t);
SOC(t) - 0.95 =l= M * b(t);

The idea is that if soc(t) -0.95 is greater than 0 then b(t) has to be 1.

You can then use b(t) to model your constraint.

- Atharv

FrancescaC
User Posts: 3
Joined: 1 month ago

### Re: Conditional statement with variable

Thank you very much, this worked!

Sorry if I insist with questions, but this is really helping me a lot Is it normal that the binary variable ( b(t) in your example ) is not really binary in the solutions? It often (i.e. in many of the timesteps) assumes values very close to 1 or 0, but not exactly 1 or 0. This causes problems in my model.

Of course, I introduce it as binary variable in the code. I tried increasing the magnitude of M but after a certain value I get errors while compiling. Is there anything else I can do, or is this how it should be?

Thank you again for your kind help!

abhosekar
Moderator Posts: 209
Joined: 1 year ago

Francesca,