## Objective with if-condition

Problems with syntax of GAMS
PeterBe
User Posts: 66
Joined: 1 year ago

### Objective with if-condition

Hi all,

I have a problem regarding modelling an objective function which should only count if it is positive. I am using a MIP model with binary variables. So I have the following equation (not in GAMS yet):
surplusPower(t) =PVgeneration(t) - electricalPowerTotal(t);

if (surplusPower(t) < 0) {
surplusPower(t) = 0;
}

The variable electricalPowerTotal(t) also depends on binary decision variables to schedule electrical loads, whereas PVgeneratrion (t) is predefined and fixed.

The goal is to minimze the sum (for all t) of surplusPower(t).

Does anyone have an idea how I could do that? I am struggeling with the if-condition. I'd appreciate your help.

User Posts: 108
Joined: 2 years ago

### Re: Objective with if-condition

This is a branch of optimization called "disjunctive programming". Much can be said about it, but one of the easiest way to implement this for your case is to use an auxiliary binary variable in what is called a "big-M" approach. I've put together a small example for you, in this case a general approach that can be improved depending on the objective function. But, perhaps it will be enough for you to get the idea.

Code: Select all

``````set t /t1*t10/;

variable surplusPower(t);
positive variable positiveSurplusPower(t);
binary variable isPositive(t);

scalar bigM /1000/;

* surplusPower(t) > 0  ==> positiveSurplusPower(t) = surplusPower(t)
* surplusPower(t) <= 0 ==> positiveSurplusPower(t) = 0

equation bm1a,bm1b;

bm1a(t).. positiveSurplusPower(t) =l= surplusPower(t) + bigM * (1-isPositive(t)) ;

bm1b(t).. positiveSurplusPower(t) =g= surplusPower(t) - bigM * (1-isPositive(t)) ;

equation bm2a,bm2b;

bm2a(t).. positiveSurplusPower(t) =l= bigM * isPositive(t) ;

bm2b(t).. positiveSurplusPower(t) =g= -bigM * isPositive(t) ;

variable z;
equation fobj;

fobj.. z=e=sum(t,positiveSurplusPower(t));

surplusPower.fx(t)=uniformint(-10,10);

model test /all/
solve test using MIP maximizing z;

``````
Best!
Claudio

PeterBe
User Posts: 66
Joined: 1 year ago

### Re: Objective with if-condition

Thanks cladelpino for suggesting this interesting approach,

I have a question regarding it:
I do not really understand how the values for the variable isPositive(t) are determined? I understand the purpose of this binary variable but do I have to specify the values before the optimization (this would not be what I want) or is it a decision variable (meaning that the solver decides about its value)?

User Posts: 108
Joined: 2 years ago

### Re: Objective with if-condition

They are, as you call them, "decision variables". Check out my ready to run example in the prior post, I've never set their values.

In that example, by using constraints bm1a,bm1b,bm2a,bm2b you can see that the feasible region of the problem is limited to having isPositive = 1 only when surplusPower > 0. The usual approach to study these kind of formulations is to use a "truth table", where you write each constraint for values of isPositive = 0 and isPositive = 1 and look at them. You will instantly notice how they work.

There are many other strategies to tackle this problem. Two readily available in GAMS are: EMP https://www.gams.com/latest/docs/UG_EMP ... mming.html "solver" in GAMS (where you can choose a" convex hull" reformulation over the bigM, which, if manageable, will be more efficient), and "indicator variables" in some MIP solvers https://www.gams.com/latest/docs/UG_Lan ... onstraints .

PeterBe
User Posts: 66
Joined: 1 year ago

### Re: Objective with if-condition

unfortunately I do not understand everything:
In that example, by using constraints bm1a,bm1b,bm2a,bm2b you can see that the feasible region of the problem is limited to having isPositive = 1 only when surplusPower > 0.
I see that if isPositive=1 --> positiveSurplusPower(t) = surplusPower(t)
and if isPositive=0 --> positiveSurplusPower(t) = 0

But I do not understand why isPositive = 1 when surplusPower > 0. Let's say surplusPower(t) = -10 and isPositive(t) = 1:
bm1a(t): positiveSurplusPower(t) =l= -10
bm1b(t): positiveSurplusPower(t) =g= -10
--> positiveSurplusPower(t) = -10
bm2a(t): positiveSurplusPower(t) =l= bigM
bm2b(t): positiveSurplusPower(t) =g= -bigM

So these equations are satifsfied if we for instance set bigM=1000, because -10 =l=1000 and -10 =g= -1000.

Furthermore I did not really get your idea of the truth table (maybe this is related to my question above):
The usual approach to study these kind of formulations is to use a "truth table", where you write each constraint for values of isPositive = 0 and isPositive = 1 and look at them. You will instantly notice how they work.
Would you mind saying some further words about that approach (I know what a truth table is, however I do not understand how to derive the constraints out of it).

User Posts: 108
Joined: 2 years ago

### Re: Objective with if-condition

But I do not understand why isPositive = 1 when surplusPower > 0. Let's say surplusPower(t) = -10 and isPositive(t) = 1:
bm1a(t): positiveSurplusPower(t) =l= -10
bm1b(t): positiveSurplusPower(t) =g= -10
--> positiveSurplusPower(t) = -10
bm2a(t): positiveSurplusPower(t) =l= bigM
bm2b(t): positiveSurplusPower(t) =g= -bigM
This combination is infeasible because I declared positiveSurplusPower as a "positive variable" (line 4), which implicitly sets a lower bound of 0 for its value.

Here is a truth table for you (but I think that you've got it right now that we've clarified the implicit bound)

Code: Select all

``````            isPositive = 0                                                  |  isPositive = 1
bm1a |  positiveSurplusPower(t) =l= surplusPower(t)  +bigM    |  positiveSurplusPower(t) =l= surplusPower(t)
bm1b |  positiveSurplusPower(t) =g= surplusPower(t) - bigM    |  positiveSurplusPower(t) =g= surplusPower(t)
bm2a |  positiveSurplusPower(t) =l= 0                                   |   positiveSurplusPower(t) =l= bigM
bm2b |  positiveSurplusPower(t) =g= 0                                  |   positiveSurplusPower(t) =g= -bigM
``````
(I couldn't find a way to get the proper spacing, sorry )

best
Claudio

Fred
Posts: 87
Joined: 2 years ago

### Re: Objective with if-condition

Peter,

In your initial post you wrote that you want to have the following:
PeterBe wrote:
10 months ago
surplusPower(t) =PVgeneration(t) - electricalPowerTotal(t);

if (surplusPower(t) < 0) {
surplusPower(t) = 0;
}
This basically means you want:

Code: Select all

``surplusPower(t) = max(0, PVgeneration(t) - electricalPowerTotal(t));``
The following fact is important because it makes modeling easier:
PeterBe wrote:
10 months ago
The goal is to minimze the sum (for all t) of surplusPower(t).
In your GAMS model you could write:

Code: Select all

``````positive variable surplusPower(t);
variable z objective variable;
[...]
equation eq(t)
obj objective function;
eq(t).. surplusPower(t) =g= PVgeneration(t) - electricalPowerTotal(t);
obj..   z =e= sum(t, surplusPower(t)) + [...];
[...]
model m /all/;
solve m use mip min z;
``````
The minimization pushes surplusPower(t) down. Since it is defined as positive variable, it has a lower bound of 0.
Apparently, this only works if you are minimizing sum(t, surplusPower(t)).

Best,
Fred

PeterBe
User Posts: 66
Joined: 1 year ago

### Re: Objective with if-condition

first I want to try it with the bigM approach suggested by cladelpino (afterwards I will also try the other approach). So I inserted the following code:

Code: Select all

``````...
eq_bm1a(t).. positiveSurplusPower(t) =l= surplusPower(t) + bigM * (1-isPositive(t)) ;

eq_bm1b(t).. positiveSurplusPower(t) =g= surplusPower(t) - bigM * (1-isPositive(t)) ;

eq_bm2a(t).. positiveSurplusPower(t) =l= bigM * isPositive(t) ;

eq_bm2b(t).. positiveSurplusPower(t) =g= -bigM * isPositive(t) ;

eq_pvGenerationTotal(t).. pvGenerationTotal(t) =e= sum(household, (pv_Nominal(t, household) * pv_Peak(household)));
eq_surplusPower(t).. surplusPower(t) =e= pvGenerationTotal(t) - electricalPowerTotal(t);
eq_electricalPowerTotal(t)..electricalPowerTotal(t) =e= sum(household, (x(t, household)* electricalPower(household) + y(t,household) * electricalPower(household) + demand_electrical(t, household)));

eq_fobj ..
z
=e=
sum(t,positiveSurplusPower(t));
...
``````
The model is immediately solved and the objective value is 0. When you have a closer look at the results, you'll see that the variable surplusPower(t) has the expected values (positive and negative values, depending on pvGenerationTotal(t) - electricalPowerTotal(t)) while the variable positiveSurplusPower(t) is always 0. So obviously there is a semantic mistake in the code, that I do not see. Does anyone have an idea?

User Posts: 108
Joined: 2 years ago

### Re: Objective with if-condition

( Disclaimer : Fred suggestion comes into the domain of what I meant with
(BigM is) a general approach that can be improved depending on the objective function
. I'm so used to not being able to make assumptions about the objective function that I remember bigMs better than I can remember the simple "linearizations". If you don't see an objective function change in the short term, using the "linearization" suggested by Fred is the way to go. Anyway, disjunctive models are a lot of fun, and bigM is a nice tool to learn and have around. )

Regarding your attempt at implementation: I don't see any obvious error in your equations.

A common matter with these kind of formulations: What are the values of isPositive ? If you see small non zero values (on the order of 1/bigM), you may need to tighten the integer tolerance of your solver (option epint in cplex, I normally set it at "0" with no issues I could attribute to that so far)

Best
Claudio

PeterBe
User Posts: 66
Joined: 1 year ago

### Re: Objective with if-condition

the values for isPositive are 0 for every timeslot in the results.

There is one further aspect that I generally don't understand: What is the difference (in the result file) between the values of a variable and a corresponding equation that defines this variable? Let's have a look at the following variables that are defined via equations:

Code: Select all

``````eq_pvGenerationTotal(t).. pvGenerationTotal(t) =e= sum(household, (pv_Nominal(t, household) * pv_Peak(household)));
eq_surplusPower(t).. surplusPower(t) =e= pvGenerationTotal(t) - electricalPowerTotal(t);
eq_electricalPowerTotal(t)..electricalPowerTotal(t) =e= sum(household, (x(t, household)* electricalPower(household) + y(t,household) * electricalPower(household) + demand_electrical(t, household)));
``````
Following points are incomprehensible for me in the results:
- the values for eq_pvGenerationTotal and pvGenerationTotal(t) are equal to the ones of pvGenerationTotal(t)
- the variable surplusPower(t) has different values (positive and negative) depending on pvGenerationTotal(t) - electricalPowerTotal(t), but the equation eq_surplusPower(t) is always 0
- the variable electricalPowerTotal(t) and the equation eq_electricalPowerTotal(t) are almost identical with the difference that in the variable some timeslots have an increased power values of 3000. Whenever a decision variable x(t, household) or y(t,household) has the value 1 (binary variables), 3000 is added which is the constant value for electricalPower(household) .

I don't understand why there are differences among those different variables and equations. Maybe this has something to do with the problem that my model is not solves in a reasonible way (but this does not necessarily have to be the case).