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 ifcondition. I'd appreciate your help.
Objective with ifcondition

 User
 Posts: 96
 Joined: 1 year ago
Re: Objective with ifcondition
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 "bigM" 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.
Best!
Claudio
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 * (1isPositive(t)) ;
bm1b(t).. positiveSurplusPower(t) =g= surplusPower(t)  bigM * (1isPositive(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;
Claudio
Re: Objective with ifcondition
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)?
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: 96
 Joined: 1 year ago
Re: Objective with ifcondition
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 .
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 .
Re: Objective with ifcondition
Thanks a lot cladelpino for your answer and your help,
unfortunately I do not understand everything:
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):
unfortunately I do not understand everything:
I see that if isPositive=1 > positiveSurplusPower(t) = surplusPower(t)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.
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):
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).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.

 User
 Posts: 96
 Joined: 1 year ago
Re: Objective with ifcondition
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.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
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
best
Claudio
Re: Objective with ifcondition
Peter,
In your initial post you wrote that you want to have the following:
The following fact is important because it makes modeling easier:
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
In your initial post you wrote that you want to have the following:
This basically means you want:
Code: Select all
surplusPower(t) = max(0, PVgeneration(t)  electricalPowerTotal(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;
Apparently, this only works if you are minimizing sum(t, surplusPower(t)).
Best,
Fred