## Objective with if-condition

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

### Re: Objective with if-condition

Hi Peter,
So you suggest that I should not define variables in equations but rather define them immediately? So instead of having for example
No, I'm not suggesting that ! Your code is totally correct. Your problem is just your conceptual understanding. This is a little hard to explain not in person... A retry: There are no "variable definitions" in optimization problems, just constraints involving variables.
If the problem is infeasible (according to the solver) I do not see any constraint value because the results are not created.
This is a peculiarity of CPLEX on GAMS. Some solvers return infeasible solutions.
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.
Like I told you before, it all depends on the "right hand side" of the constraints (the part that doesn't depend on variables).

ie

Code: Select all

``someEq..  x + y =e= 2``
Has a value of 2 in feasible solutions, while

Code: Select all

``someEq..  x  =e= y``

is equal to

Code: Select all

``someEq..  x  - y =e= 0``
Which has a value of 0 in feasible solutions.

Best
Claudio

PeterBe
User
Posts: 62
Joined: 1 year ago

### Re: Objective with if-condition

I think (and hope) that I have understood it now

PeterBe
User
Posts: 62
Joined: 1 year ago

### Re: Objective with if-condition

Hi guys,

it's me again having a slightly different question as the one posted before but it has also something to do with modelling if conditions:

I now have two variables x and y that with the following constraings:
x + y <= 1
x + y >= 0.1
if x > 0 --> y =0
if y > 0 --> x =0
0<=x<=1
0<=y<=1

How can I implement that in GAMS? Appreciate every comment.

Fred
Posts: 70
Joined: 1 year ago

### Re: Objective with if-condition

Peter,

You might want to look into SOS1 variables: https://www.gams.com/latest/docs/UG_Lan ... eVariables
As usual, there are many ways to formulate such logical conditions, e.g. via an additional binary variable.

Best,
Fred

PeterBe
User
Posts: 62
Joined: 1 year ago

### Re: Objective with if-condition

I changed the variables

Code: Select all

``````Binary variable  x(t, household) heatGenerationForBufferTank;
Binary variable y(t, household) heatGenerationForDHWTank;
``````
to

Code: Select all

``````SOS1 variable  x(t, household) heatGenerationForBufferTank;
SOS1 variable y(t, household) heatGenerationForDHWTank;
``````
and it seems to work . However this surprises me a little bit because in the documentation it is said that

Code: Select all

``````SOS1 Variable s1(i)
``````
"s1 represents one special ordered set of type 1 with i elements". I'd understand from this description that among the x(t, household) only one variable can have a nonzero value and not among the x and y variables for every (t, household). Is this because in my code x and y have the same index which groups them together as a set of SOS1 variables?

Fred
Posts: 70
Joined: 1 year ago

### Re: Objective with if-condition

Peter,

Your doubts are legitimate. As explained in the documentation, the members of the innermost (the right-most) index belong to the same SOS set.
For your variable x that means for every t your x variable can have at most one non-zero value. Assuming that your set household has 3 elements h1, h2, h3 and t has 10 elements t1*t10 that could for example look as follows

Code: Select all

``````x('t1','h1')  = 0
x('t1','h2')  = 0
x('t1','h3')  = 1

x('t2','h1')  = 0
x('t2','h2')  = 0
x('t2','h3')  = 0

[...]

x('t10','h1') = 0
x('t10','h2') = 1
x('t10','h3') = 0
``````
Your variables x and y are completely independent which is probably not what you want.

Best,
Fred

PeterBe
User
Posts: 62
Joined: 1 year ago

### Re: Objective with if-condition

now I understand my mistake but I do not know how to fix it. How can the variable x depend on y? I tried

Code: Select all

``````SOS1 variable  x(t, household,y) heatGenerationForBufferTank;
SOS1 variable y(t, household,x) heatGenerationForDHWTank;
``````
but it did not work because y and x are not sets but variables.

Fred
Posts: 70
Joined: 1 year ago

### Re: Objective with if-condition

Peter,

If you want to treat x and y as sos1 variables of the same sos1 set, you have to reformulate the model and make them one variable with an additional index, e.g. as follows

Code: Select all

``````set t         / t1*t10 /
household / h1*h3 /
var       / x 'heatGenerationForBufferTank', y 'heatGenerationForDHWTank' /;
sos1 variable xy(t,household,var);

xy.up(t,household,var) = 1;

equation lb(t, household) 'lower bound for x+y';

lb(t,household).. sum(var, xy(t,household,var)) =g= 0.1;
[...]
``````

As mentioned before, you could also reformulate the logical conditions using binary variables. I think the following formulation should also do the trick.

Code: Select all

``````positive variable  x(t,household) heatGenerationForBufferTank;
positive variable  y(t,household) heatGenerationForDHWTank;
binary variable    xAct(t,household);

x.up(t,household) = 1;
y.up(t,household) = 1;

equation e1,e2,e3;

e1(t,household).. x(t,household)    =l= xAct(t,household);
e2(t,household).. y(t,household)    =l= (1-xAct(t,household));
e3(t,household).. x(t,household) + y(t,household) =g= 0.1;
``````
I hope this helps!

Best,
Fred

PeterBe
User
Posts: 62
Joined: 1 year ago