VAR operands relational or boolean

Problems with modeling
sandy12310627
User
User
Posts: 23
Joined: 4 years ago

VAR operands relational or boolean

Post by sandy12310627 »

Hi,

I am appreciated that you can help me.
I had a problem with "if, then" or "either, or"
There are the variables DD(t), DU(t),DIP(t),DIN(t),CU(t) and the parameter DC.
i would like to give them the constraint that if DD(t)>=0,then DD(t)=DU(t)-(DIP(t)- DIN(t))-CU(t)
if DD(t)<0, then DD(t)=0

there is my equation.
E_DD(t)..DD(t+1)=l=DC-CU(t);
E_DD2(t)..DD(t)=e=(DU(t)-(DIP(t)- DIN(t))-CU(t))$((DU(t)-(DIP(t)- DIN(t))-CU(t))>0)+0$((DU(t)-(DIP(t)- DIN(t))-CU(t))<0);
however, it shows that
**** 57 Endogenous relational operations require model type "dnlp"
**** 256 Error(s) in analyzing solve statement. More detail appears
**** Below the solve statement above
**** The following LP errors were detected in model LP:
**** 57 equation E_DD2.. VAR operands relational or boolean
**** 57 equation E_DD2.. VAR operands relational or boolean

I am wondering if there is any way I could using LP but not DNLP.
Also, how to fix the operands relational or boolean problem.

I am thinking if I could change like this
E_DD2(t)$(DD(t)ge 0)..DD(t)=e=g(t);
E_DD3(t)..g(t)=e=DU(t)-(DIP(t)- DIN(t))-CU(t);
E_DD4(t)$(DD(t)<0)..DD(t)=e=0;
but it still have the same problem.

Also, I am thinking that using binary(sameAs or diag)
but only subsets and dynamic set can do it.

I search a lot on GAMS guide, GAMs error, and modeling library but I still cannot understand how to fix it.
Thank you for helping me!
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: VAR operands relational or boolean

Post by bussieck »

You can't model this as an LP. You can model this with some binary variables and make a MIP out of it. You need a binary variable that is 0 if DU(t)-(DIP(t)- DIN(t))-CU(t) <= 0 and 1 otherwise. Search the forum for logical constraint and HP Williams (that is an author of a book that describes such reformulations).

-Michael
sandy12310627
User
User
Posts: 23
Joined: 4 years ago

Re: VAR operands relational or boolean

Post by sandy12310627 »

Hi! Thank you so much!!!

However, I think I still have a problem.
this is my equation. DC and CU(t) are parameter. and others are variable
E_DD(t)..DD(t+1)=l=DC-CU(t);
E_DD4(t)..DDD(t)=e=DU(t)-(DIP(t)- DIN(t))-CU(t);
E_DD2(t)..DD(t)=g= DDD.lo(t)*bi;
E_DD3(t).. DD(t)=l= DDD.up(t)*bi;

I do not need to limit the upper and lower value so I give them a huge value.
DDD.lo(t)=-100000;
DDD.up(T)=100000;

however, the solution shows below
--- E_DD4 =E=

E_DD4(1).. DDD(1) - DU(1) + DIP(1) - DIN(1) =E= 0 ; (LHS = 0)

E_DD4(2).. DDD(2) - DU(2) + DIP(2) - DIN(2) =E= -30.2060965584 ; (LHS = 0, INFES = 30.2060965584 ****)

E_DD4(3).. DDD(3) - DU(3) + DIP(3) - DIN(3) =E= -32.573015113942 ; (LHS = 0, INFES = 32.573015113942 ****)

It is infeasible. I meet this situation before, that's why I want to give a condition for DD(t)
Is my equation wrong if I would like to constraint if DD(t)>=0, DD(t)=e=DU(t)-(DIP(t)- DIN(t))-CU(t); and if DD(t)<0, then DD(t)=0
I am appreciated for your help!
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: VAR operands relational or boolean

Post by bussieck »

I did not check your logic, but a few comments:

Don't use arbitrary large numbers for bigM. They mess up the numerics of your model. Find out from your understanding of model and data what's the maximum/minimum value a variable can take. If that's not possible, bigM is not possible and you need to go to different tricks (e.g. indicator or SOS formulations).

The INFES you show here are not from your solution, they comes from the equation listing (https://www.gams.com/latest/docs/UG_GAM ... ionListing) and show the infeasibilities of your input point.

-Michael
sandy12310627
User
User
Posts: 23
Joined: 4 years ago

Re: VAR operands relational or boolean

Post by sandy12310627 »

Hi!
Thank you for correcting my code.
I revised my code but it still infeasible. I am wondering if there is a problem that results in an infeasible solution.

First, this is my revised code. I am wondering if it is correct.

set s /slack, yExpr/;
SOS1 variable indic(s);
positive variable slack;

E_DD2(t)..DU(t)-(DIP(t)- DIN(t))-CU(t)-DD(t)=e=slack;
iON..indic("slack")=e=slack;
iOFF..indic("yExpr")=e=bi;

those are the constraint for " if DD(t)>0, DD(t)=DU(t)-(DIP(t)- DIN(t))-CU(t);if DD(t)<=0, DD(t)=0"

Moreover, I found that if the equation included parameter. The equation will be infeasible.
But I could not understand if there is a logic problem or code problem.

For example,
E_DL(t)..DL(t+1)=e=(1-alpha)*(DL(t)+DT(t))+alpha*X(t+1);
alpha and X(t) are parameters. Here, t is period. my question is related to time (period 1 ,period 2 ...)

this is infeasible.

---- E_DL =E= level equation of forecasting for DC

E_DL(1).. - 0.6*DT(1) - 0.6*DL(1) + DL(2) =E= 217.153434092947 ; (LHS = 40, INFES = 177.153434092947 ****)

E_DL(2).. - 0.6*DT(2) - 0.6*DL(2) + DL(3) =E= 225.077201238299 ; (LHS = 40, INFES = 185.077201238299 ****)

E_DL(3).. - 0.6*DT(3) - 0.6*DL(3) + DL(4) =E= 233.071439959776 ; (LHS = 40, INFES = 193.071439959776 ****)
I think the problem that is no feasible is 40=/=217 in the first equation. However, I would like to use (1-alpha)*(DL(t)+DT(t))+alpha*X(t+1) to calculate DL(t+1)
simple calculation but not optimal. Am I misunderstand anything?

Thank you so much again!
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: VAR operands relational or boolean

Post by bussieck »

I don't think that you logic is correct also you if logic looks flawed, but I guess you mean

Code: Select all

if(DU(t)-(DIP(t)- DIN(t))-CU(t))>=0, 
   DD(t)=DU(t)-(DIP(t)- DIN(t))-CU(t);
else 
   DD(t)=0;
)
or easier DD(T) = max(0,DU(t)-(DIP(t)- DIN(t))-CU(t)). I would do it like this:

Code: Select all

set s /slack1, slack2/;
SOS1 variable sl(s);

cond1(t).. DD(t) =g= 0; // better use positive variable DD
cond2(t).. DD(t) =g= DU(t)-(DIP(t)- DIN(t))-CU(t);
cond3(t).. DD(t) =l= 0 + sl('slack1');
cond4(t).. DD(t) =l= DU(t)-(DIP(t)- DIN(t))-CU(t) + sl('slack2');
Again, you show the equation listing which shows you the infeasibility of your starting point. At some stage things would be easier if you send the smallest working example.

-Michael
sandy12310627
User
User
Posts: 23
Joined: 4 years ago

Re: VAR operands relational or boolean

Post by sandy12310627 »

I see! thank you so much!!
This is my problem.
I think the attachment is not small so I ask you as a single problem.
But actually I confused about this problem for about 2 months... I really need help with finding why it is not feasible.
Thank you so much again for your willingness to answer my question.
new3.gms
(6.3 KiB) Downloaded 201 times
sandy12310627
User
User
Posts: 23
Joined: 4 years ago

Re: VAR operands relational or boolean

Post by sandy12310627 »

By the way, I follow your instruction and change the code to

E_DD(t)..DD(t+1)=l=DC-CU(t);
E_DD2(t)..DD(t) =g= DU(t)-(DIP(t)- DIN(t))-CU(t);
E_DD3(t)..DD(t) =l= 0 + sl('slack1');
E_DD4(t)..DD(t) =l= DU(t)-(DIP(t)- DIN(t))-CU(t) + sl('slack2');

However, E_DD2 and E_DD4 still have the same problem as before
I am wondering why it still has the problem though I change the formula with the condition?

---- E_DD2 =G=

E_DD2(1).. DD(1) - DU(1) + DIP(1) - DIN(1) =G= 0 ; (LHS = 0)

E_DD2(2).. DD(2) - DU(2) + DIP(2) - DIN(2) =G= -30.2060965584 ; (LHS = 0)

E_DD2(3).. DD(3) - DU(3) + DIP(3) - DIN(3) =G= -32.573015113942 ; (LHS = 0)

---- E_DD4 =L=

E_DD4(1).. - sl(slack2) + DD(1) - DU(1) + DIP(1) - DIN(1) =L= 0 ; (LHS = 0)

E_DD4(2).. - sl(slack2) + DD(2) - DU(2) + DIP(2) - DIN(2) =L= -30.2060965584 ; (LHS = 0, INFES = 30.2060965584 ****)

E_DD4(3).. - sl(slack2) + DD(3) - DU(3) + DIP(3) - DIN(3) =L= -32.573015113942 ; (LHS = 0, INFES = 32.573015113942 ****)
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: VAR operands relational or boolean

Post by bussieck »

Dealing with infeasibilities is not easy but requires insight into the details of your model and the data, so without diving into your model I/we can only provide you help to help yourself: The McCarl newletter vol. 40 gives good recommendations on how to deal with infeasible models (see section 3 of https://www.gams.com/fileadmin/communit ... news40.pdf). You model is already infeasible as an lp (change mip to rmip in your solve statement). Dealing with infeasible LPs is much easier than MIP. You can use iis also known as conflict refiner or the solution from simplex first phase. I tried iis on your rmip. I get a very small set of constraints:

Code: Select all

A conflict is a set equations and variables (ie a submodel) which is
infeasible but becomes feasible if any one equation or variable bound
is dropped.

A problem may contain several independent conflicts but only one will be
found per run.

Number of equations in the conflict:  1.
fixed: E_DU(1) = -830.668

Number of variables in the conflict:  3.
lower: DU(2) > 0
upper: DF(1) < 100
upper: DF(2) < 100
Look at equation E_DU: "E_DU(t)..DU(t+1)=e=DF(t+1)+z2*(DF(t)-X(t));" For t=1, it says "DU(2) =e= DF(2)+1.65*(DF(1)-503.435)". Since DF(1)=100 and the same for DF(2) the left hand side is negative. Since you require DU to be a positive variable, there is no way to make it feasible.

IIS/Conflict refiner works fine if the number of conflicts is small. After I changed DF(1) and DF(2) to an upper bound of 1000 the remaining conflict had 90 members! Probably continuing with adding selectively some slacks (as described by Bruce) is now the way to go. I always like constructing a "feasible" solution by hand, set the var.l to this "solution" and look at the equation listing and see where my "solution" violates constraints.

Good luck.

-Michael
sandy12310627
User
User
Posts: 23
Joined: 4 years ago

Re: VAR operands relational or boolean

Post by sandy12310627 »

Hi Michael,

I very much appreciated your help a lot.
I think I could understand the problem of my code; however, I do not how to fix it.
I try to change the positive variable to only "variable" or change DF(1) and DF(2) to 1000 to avoid the negative solution.
It still wrong. only change the wrong infeasible value as below.
image.png
This is a big problem of this code and I try to fix it by changing the variable or remove the initial value and fix value but problems still exist.
Could you suggest me to fix this kind of problem once?
Moreover, could I change to LP with the slack problem? The solution did not allow it but I am wondering if there is any way I could use LP.
Thank you so much!

Sincerely,
Sandy
Post Reply