Objective with if-condition

Problems with syntax of GAMS
Post Reply
PeterBe
User
User
Posts: 40
Joined: 1 year ago

Objective with if-condition

Post by PeterBe » 6 days ago

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.

cladelpino
User
User
Posts: 96
Joined: 1 year ago

Re: Objective with if-condition

Post by cladelpino » 6 days ago

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
User
Posts: 40
Joined: 1 year ago

Re: Objective with if-condition

Post by PeterBe » 4 days ago

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

cladelpino
User
User
Posts: 96
Joined: 1 year ago

Re: Objective with if-condition

Post by cladelpino » 4 days ago

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
User
Posts: 40
Joined: 1 year ago

Re: Objective with if-condition

Post by PeterBe » 1 day ago

Thanks a lot cladelpino for your answer and your help,

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

cladelpino
User
User
Posts: 96
Joined: 1 year ago

Re: Objective with if-condition

Post by cladelpino » 1 day ago

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: 22
Joined: 1 year ago

Re: Objective with if-condition

Post by Fred » 17 hours ago

Peter,

In your initial post you wrote that you want to have the following:
PeterBe wrote:
6 days 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:
6 days 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

Post Reply