Objective with if-condition

Problems with syntax of GAMS
Fred
Posts: 70
Joined: 1 year ago

Re: Objective with if-condition

Post by Fred » 4 months ago

Hi,

It seems that the example from post #2 misses to properly link variables SurplusPower and isPositive. The example doesn't work if the objective is minimized instead of maximized.

For example equations like

Code: Select all

equation bm3a, bm3b;
bm3a(t).. isPositive(t)   =g=  SurplusPower(t) / bigM;
bm3b(t).. 1-isPositive(t) =g= -SurplusPower(t) / bigM;
should fix this.

@Peter: The section on Solution Listing in the documentation should help to answer your questions regarding the equation levels (https://www.gams.com/latest/docs/UG_GAM ... ionListing).

Best,
Fred

PeterBe
User
User
Posts: 62
Joined: 1 year ago

Re: Objective with if-condition

Post by PeterBe » 4 months ago

Thanks Fred for your answer,

I inserted the two equations. Now a reasonable solution is found But only for one household. If I increase the number of households I always get the error message:

"Row 'eq_electricalPowerTotal(144)' infeasible, all entries at implied bounds"

The problematic equation looks like this (as posted before):

Code: Select all

eq_electricalPowerTotal(t)..electricalPowerTotal(t) =e= sum(household, (x(t, household)* electricalPower(household) + y(t,household) * electricalPower(household) + demand_electrical(t, household)));
I do not understand this as I did not set any bound for the eq_electricalPowerTotal. Besides I am questioning, why this problem does not occur when only using 1 household?


By the way: I read the section on Solution Listing in the documentation but still I don't understand how the values of equations in the solution are calculated. There it is only wirtten:
"Equation level .l Level of the equation in the current solution, equal to the level of all terms involving variables."

This does not help me to understand it.

Fred
Posts: 70
Joined: 1 year ago

Re: Objective with if-condition

Post by Fred » 4 months ago

Peter,

It's probably easier to help if you could post your entire code such that it can be executed.
As mentioned before, there are various formulations to retrieve only the positive part of a variable. One that appears a bit mor intuitive to me is

Code: Select all

positive variable negativeSurplusPower;
equation e1(t),e2(t),e3(t);
e1(t).. surplusPower(t) =e= positiveSurplusPower(t) - negativeSurplusPower(t);
e2(t).. positiveSurplusPower(t) =l= bigM*isPositive(t);
e3(t).. negativeSurplusPower(t) =l= bigM*(1-isPositive(t));

cladelpino
User
User
Posts: 108
Joined: 1 year ago

Re: Objective with if-condition

Post by cladelpino » 4 months ago

It seems that the example from post #2 misses to properly link variables SurplusPower and isPositive. The example doesn't work if the objective is minimized instead of maximized.
Yes, my bad, sorry :oops: . This last formulation that Fred presents is much better.

Peter:
I do not understand this as I did not set any bound for the eq_electricalPowerTotal.


The message from the solver is telling you that the equation is infeasible and all of the values of variables appearing in it cannot be changed to make it feasible (because they have reached either a explicit or implicit bound), it is not making reference only to the equation "bound".

On equation levels: Picture your equation in a way where every variable is on the left hand side of the operator (=g=,=l=, or =e=) , and on the right hand side you have (if there is any) the "constant" (determined only by parameters and constants) term. The equation level is the current value of the left hand side, considering the current values of the variables.

By the message that you posted I think that you are using CPLEX. The drawback is that you won't get back the infeasible solution to ease your debug, but CPLEX has some conflict resolution capabilities. https://www.gams.com/latest/docs/S_CPLEX.html#CPLEXiis

Best and sorry again for the erroneous example.

PeterBe
User
User
Posts: 62
Joined: 1 year ago

Re: Objective with if-condition

Post by PeterBe » 4 months ago

@cladelpino
Thanks cladelpino for your answer and help,
On equation levels: Picture your equation in a way where every variable is on the left hand side of the operator (=g=,=l=, or =e=) , and on the right hand side you have (if there is any) the "constant" (determined only by parameters and constants) term. The equation level is the current value of the left hand side, considering the current values of the variables.
Unfortunately this is not possible as I am defining variables in the equations. So my equations are not only constraints but sometimes only define formulars for variables. For example:

Code: Select all

eq_electricalPowerTotal(t)..electricalPowerTotal(t) =e= sum(household, (x(t, household)* electricalPower(household) + y(t,household) * electricalPower(household) + demand_electrical(t, household)));

Regarding the infeasible solutions, you wrote:
By the message that you posted I think that you are using CPLEX. The drawback is that you won't get back the infeasible solution to ease your debug, but CPLEX has some conflict resolution capabilities. https://www.gams.com/latest/docs/S_CPLEX.html#CPLEXiis
I included the iis in the option file but I cannot understand anything from the additional output which looks like this
Refine conflict on 60477 members...

Iteration Max Members Min Members
1 45358 0
2 44414 0
3 43942 0
4 43883 0
5 43876 0
6 43875 0
7 43704 0
8 43619 0
9 43598 0
10 40873 0
11 40192 0
12 39852 0
13 39682 0
14 39597 0
15 39576 0
16 39571 0
17 39568 0
18 39567 0
19 34621 0
20 32149 0
21 32072 0
22 32034 0
23 32025 0
24 32020 0
25 32018 0
26 16011 0
27 12010 0
28 10009 0
29 9009 0
30 8509 0
31 8384 0
32 8353 0
33 8338 0
34 5 0
35 5 1
36 5 2
37 5 3
38 5 4
39 5 5
@Fred
Thanks for your new suggestion,

I used this approach and for one household everything is okay. But the same problem which occurs when using the other approaches with more than one houshold also occurs with this new appraoch. I get the message that the problem is integer infeasible.

Fred
Posts: 70
Joined: 1 year ago

Re: Objective with if-condition

Post by Fred » 4 months ago

Peter,

We have discussed several topics in this thread and should try to keep things separate.

1. There was the original question how to consider only the positive part of a variable. Some bigM formulations have been discussed. If you understand the reformulation(s), I think this question has been answered.

2. There was the question on how to interpret equation levels in the solution listing.
Claudio's explanation is perfectly fine:
cladelpino wrote:
4 months ago
On equation levels: Picture your equation in a way where every variable is on the left hand side of the operator (=g=,=l=, or =e=) , and on the right hand side you have (if there is any) the "constant" (determined only by parameters and constants) term. The equation level is the current value of the left hand side, considering the current values of the variables.
I do not understand your objection. If an equation defines a variable (e.g. as sum of other variables) and there are no other constants involved, all variables are moved to the left hand side and the level of the equation is zero (in a feasible solution).

3. Your model is integer infeasible. It seems that you suspect the cause of infeasibility in an erroneous bigM reformulation. But this is not necessarily the cause. There can be many reasons why your model is infeasible. Unfortunately, you still have not shared your code to allow other users to reproduce this. That makes it difficult to provide target-oriented help for your particular model.

Your model is (integer) infeasible, so there is no feasible point to all the constraints and integrality conditions you have specified. That means, there is either a mistake implementing some constraint, or the combination of model and data really allows for no (integer) feasible solution. This state of the optimization model is in general not a bug or problem. It is one of the possible theoretic outcomes of an optimization run.
The real work it to figure out where the infeasibility comes from. There is no single way of doing this. It very much depends on your knowledge and understanding of the original problem and it's implementation as a model. If you are convinced, that there must be a feasible solution to your problem, maybe you can provide such a solution by hand. If that is possible, a good way to go about analyzing infeasibilities is to provide such a "feasible" solution to the problem by manually setting the variable level values (x.l(i,j) = ...) and then generating the model with a full equation listing (option limrow=1e9;) This will flag the constraints that are infeasible with your "feasible" solution.
If you don't want to work with your own feasible solution, you can also ask Cplex you give you the smallest relaxation of your model to make it feasible (look for option FeasOpt: https://www.gams.com/latest/docs/S_CPLE ... LEXfeasopt ). This is just an alternative to the iis option mentioned by Claudio. What you posted is the log of the conflict refiner. It shows that Cplex found an irreducible infeasible set of 5 constraints/variable bounds. In the lst file these equations/variables should be reported between solve summary and solution listing.

I hope this helps!

Fred

gigibotte
User
User
Posts: 4
Joined: 6 months ago

Re: Objective with if-condition

Post by gigibotte » 4 months ago

Hi Guys,

I run into this thread and i found it useful but it unfortunately it doesn't completely solve my problem.
I'm running an optimization problem where I have to minimize my objective function.

my objective function is, in a simplified version, the following one:

Code: Select all

z =e = sum(t, sum(i,production_costs(i)*power_production(i,t))) 
Now, I have to add an element to this that should look like this:

Code: Select all

z =e =[...] - sum(j, TWC(j)*HS(j)) 
My problem is the following. TWC(j) (where j is a set of 5 different units) should be defined as follow (not in GAMS language):

if HS(j)>=1000
then TWC(j) =3.21

while if HS(j) <1000
then TWC(j) = 0

the way I tried to implement this is this one:

Code: Select all

Parameters
TWC(j)           Tradable White Certificates             /wall 0, roof 0, floor 0, window 0/

Equation
TWCup;

TWCup(j)$(HS.l(j)>=1000)..                  TWC(j) =e= 3.21;
HS(j) is a variable. however This seems not working. Do you know how can I solve my problem?

THanks!

cladelpino
User
User
Posts: 108
Joined: 1 year ago

Re: Objective with if-condition

Post by cladelpino » 4 months ago

Hi Peter:

What Fred told you is very correct. Just to sum up, would you please follow his recommendation and:

1. Provide a feasible solution for the problematic situation (more than one household) and use the equation listing to tell which constraints are infeasible.

The steps to do this have been described to you recently by Fred (post viewtopic.php?f=11&t=10371&start=10#p23875 and post viewtopic.php?f=2&t=10384&start=10#p23931) and me (post viewtopic.php?f=11&t=10371&sid=f94d7887 ... 60f#p23867). Should you not understand them, ask again.

2. Show us the output of the IIS.

At least I would expect you to do 1 and 2 on your own before I run your actual model.

Side note:
Unfortunately this is not possible as I am defining variables in the equations. So my equations are not only constraints but sometimes only define formulars for variables. For example:
This is a very common misconception. The sooner you forget about it, the better your path in optimization will be. There is no difference between one type of equality constraint or the other, neither is the problem solved sequentially.

The best model to reason about optimization problems is: Constraints are scalar functions involving variables. Their value is then restricted by an operator and a constant term (not involving ANY variable).

In the example that you posted, you are expressing that, in feasible solutions, only combinations of values that make the difference between

Code: Select all

electricalPowerTotal(t)
and

Code: Select all

sum(household, (x(t, household)* electricalPower(household) + y(t,household) * electricalPower(household) + demand_electrical(t, household))) 
equal to 0 are allowed.

For this equation, the equation level is exactly the value of this difference, so if you see an constraint value of 1, this means that the constraint is currently infeasible, and in the current solution, electricPowerTotal(t) is greater by one unit than the summation.

Best and good luck
Claudio

Fred
Posts: 70
Joined: 1 year ago

Re: Objective with if-condition

Post by Fred » 3 months ago

Gigibotte,

You can use pretty much the same reformulation as Peter. The only difference is, that you are interested whether a variable is <= or >= 1000 instead of zero. Please consider the following modified example.

Code: Select all

set j /j1*j10/;
scalar bigM /1000/;
parameter TWC(j) / #j 3.21 /;

variable HS(j),z;
positive variable HSplus(j), HSminus(j);
binary variable b(j);
equation obj,e1(j),e2(j),e3(j);
         
obj..   z          =e= -sum(j, TWC(j)*(1000+HSplus(j)));
e1(j).. HS(j)      =e= 1000 + HSplus(j) - HSminus(j);
e2(j).. HSplus(j)  =l= bigM * b(j);
e3(j).. HSminus(j) =l= bigM * (1-b(j));

HS.fx(j)=uniformint(995,1005);

model test /all/
solve test use mip min z;

parameter rep(j,*);
rep(j,'HS')      = HS.l(j);
rep(j,'b')       = b.l(j);
rep(j,'HSplus')  = HSplus.l(j);
rep(j,'HSminus') = HSminus.l(j);
display rep;
Your approach with HS.l in the dollar condition of an equation does not work for several reasons.
1. If you use the .l attribute of a variable in a model the refers to the level of that variable before the model is solved. Just dropping the .l is also no solution. Tying the generation of a constraint to a variable whose level is determined while the corresponding model is solved, does not fit the general concept of a Mathematical Program. Constraints have to be known before the model is solved
2. You cannot set parameter values inside a model

PeterBe
User
User
Posts: 62
Joined: 1 year ago

Re: Objective with if-condition

Post by PeterBe » 3 months ago

Thanks cladelpino for your answer,

you wrote
This is a very common misconception. The sooner you forget about it, the better your path in optimization will be. There is no difference between one type of equality constraint or the other, neither is the problem solved sequentially.

The best model to reason about optimization problems is: Constraints are scalar functions involving variables. Their value is then restricted by an operator and a constant term (not involving ANY variable).
So you suggest that I should not define variables in equations but rather define them immediately? So instead of having for example

Code: Select all

positive variable pvGenerationTotal(t);
...
Equation eq_pvGenerationTotal(t);
...
eq_pvGenerationTotal(t).. pvGenerationTotal(t) =e= sum(household, (pv_Nominal(t, household) * pv_Peak(household)));
I should write

Code: Select all

positive variable pvGenerationTotal(t) = sum(household, (pv_Nominal(t, household) * pv_Peak(household)));
Have I undestood it correctly?
In the example that you posted, you are expressing that, in feasible solutions, only combinations of values that make the difference between

electricalPowerTotal(t)
and

sum(household, (x(t, household)* electricalPower(household) + y(t,household) * electricalPower(household) + demand_electrical(t, household)))

equal to 0 are allowed.

For this equation, the equation level is exactly the value of this difference, so if you see an constraint value of 1, this means that the constraint is currently infeasible, and in the current solution, electricPowerTotal(t) is greater by one unit than the summation.
I am sorry but I have to admit that I don't understand what you are trying to tell me. What do you mean with seeing an constraint value of 1? If the problem is infeasible (according to the solver) I do not see any constraint value because the results are not created. When there is a solution the result file is created by GAMS and there you most often find values greater than one for the =e= equations.

Post Reply