Answers to: several if then or if else using gamshttp://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams<p>Dear All,</p>
<p>I am trying to code several if then or if else using gams code but I cannot get it right.</p>
<p>my formulas and gams code are below. is there any easy way to write these codes?</p>
<p>Z1, Z2, and Z3 are binary variables. I try to wrote on of the equations as follow</p>
<p>enWed, 03 Jun 2015 22:52:11 -0400Answer by erwinhttp://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams/12444<p>When you run an MINLP model and you see: "An equation in the pre-triangular part of the model cannot be solved because the critical slack is at a bound", this probably means the relaxed MINLP (RMINLP) is already infeasible. CONOPT gives a detailed message in the listing file how it deduced the model was infeasible in the preprocessor. This should make it easy to diagnose the problem.</p>
<p>When solving a MINLP it is often a good idea to first make sure that the RMINLP solves ok. </p>erwinWed, 03 Jun 2015 22:52:11 -0400http://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams/12444Comment by deniz on deniz's answerhttp://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams#12386<p>Thank you very much Ehsan,
I am going to try to solve the problem. Thanks again for all your guidance and valuable inputs.
Hopefully, I will solve the problem... I am so close to give up....</p>denizThu, 28 May 2015 18:55:53 -0400http://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams#12386Comment by Ehsan on deniz's answerhttp://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams#12385<p><a href="/users/27559/deniz">@deniz</a>: You should check the section 6.4 of the CONOPT documentation for more information.</p>EhsanThu, 28 May 2015 16:04:34 -0400http://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams#12385Comment by Ehsan on deniz's answerhttp://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams#12384<p><a href="/users/27559/deniz">@deniz</a>: Based on the warning, it seems you are using CONOPT as your selected solver. Essentially, the warning message means that the input parameters of the model have different scales (e.g., some in a range of 0-10 while some in a range of 100000-10000000). This could happen if the parameters have different measuring units (e.g., milligrams and kilograms). If this is affecting your model performance and precision, you should consider re-scaling the input parameters.</p>EhsanThu, 28 May 2015 16:03:39 -0400http://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams#12384Answer by denizhttp://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams/12358<p>Dear Ehsan,
Thank you very much for your guidance. I define the additional constrains as follows. Since I have ranges for the R values I had to define several constraints for each R range. I also change the R values as you said. Model run without error but model statistic gives a message stating that </p>
<p><strong> Warning </strong> The variance of the derivatives in the initial point is large (= 5.4 ). A better initial point, a better scaling, or better bounds on the variables will probably help the optimization.</p>
<p>"An equation in the pre-triangular part of the model cannot be solved because the critical slack is at a bound".
I have run very simple MIP problem but this is my first MINLP and I really don't know what is this means also I don't know how to even solve the problem.
Thanks again for your time and guidance.</p>
<p>const5(T,P)..lambda(p,t+1))=l= z1(t-1,p) * R1 ;
const5a(T,P)..lambda(p,t+1))=g= z2(t-1,p) * R1 ;
const5b(T,P)..lambda(p,t+1))=l= z2(t-1,p) * R2;
const5c(T,P)..lambda(p,t+1))=g= z3(t-1,p) * R2;
const5d(T,P)..lambda(p,t+1))=l= z3(t-1,p) * R3 ;
const5e(T,P)..lambda(p,t+1))=g= (1 - z1(t-1,p)) * (1 - z2(t-1,p)) * (1-z3(t-1,p) ) * R3 ;</p>denizTue, 26 May 2015 01:11:28 -0400http://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams/12358Comment by Ehsan on Ehsan's answerhttp://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams#12337<p><a href="/users/27559/deniz">@deniz</a>: What I meant by the \(\epsilon\) values was that you should consider \(R_1\) and \(R_2\) to be \(0.50001\) and \(0.70001\), instead of \(0.5\) and \(0.7\). This would give you a more precise solution for boundary cases (e.g., \(\lambda = 0.5\) or \(\lambda = 0.7\)).</p>
<p>By the way, if my suggestion didn't work out for you, I think it would be better for you to post your newer question as a separate question on modeling, but not GAMS. This way, you would have a better chance of getting an answer.</p>
<p>Feel free to ask any other question you might have. Good luck.</p>EhsanSat, 23 May 2015 14:16:11 -0400http://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams#12337Comment by deniz on Ehsan's answerhttp://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams#12336<p>Dear Ehsan,
I am going to try these additional constraints. When you say epsilon values, I am not quite sure what is that means since I am not very good in operation research and coding, but I will certainly try to find out that, too
You cannot imagine how much your help appreciated!!
I have been trying every possible ways to solve this problem.
Thanks again and have a wonderful day.
Regards.</p>denizSat, 23 May 2015 13:58:19 -0400http://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams#12336Comment by Ehsan on Ehsan's answerhttp://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams#12331<p>I think adding the following constraints would do the trick:</p>
<p>\(R_1 * Z_2 + R_2 * Z_3 \leq \lambda\)</p>
<p>\(\lambda \leq R_1 * Z_1 + R_2* Z_2 + R_3 * Z_3\)</p>
<p>\(Z_1 + Z_2 + Z_3 = 1\)</p>
<p>Please note that you might need to add some epsilon values to \(R_1\) and \(R_2\) to correctly observe thresholds for \(\lambda\) values right on the interval boundaries.</p>EhsanSat, 23 May 2015 03:46:29 -0400http://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams#12331Comment by deniz on Ehsan's answerhttp://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams#12330<p>Thank you Ehsan.
I have several values of R. I am first calculating lambda values and comparing this lambda values with R values.
R1:0.5
R2:0.7
R3:0.9</p>
<p>IF Lambda value is less than R1=0.5 , then I want Z1 (binary variable ) to be equal to 1
IF Lambda value is bigger than R1=0.5 but less than R2 , then I want Z2 (binary variable ) to be equal to 1
IF Lambda value is bigger than R2=0.5 but less than R3 , then I want Z3 (binary variable ) to be equal to 1.</p>
<p>I am trying to use if else to write a above conditions.
I am sure there must be an easy way to do this but since I am new coding in GAMS, I cannot find the right way. I has been several weeks, I am tying but so far no luck.
Thanks again for the guidance!
Regards.</p>denizSat, 23 May 2015 00:53:33 -0400http://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams#12330Comment by Ehsan on Ehsan's answerhttp://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams#12328<p><a href="/users/27559/deniz"><a href="/users/27559/deniz">@deniz</a></a>: I cannot figure out what your model is trying to do. However, what I forgot to mention in my original post was that you could model logical conditions on variables and constraints using (usually additional) binary variables. To start this, you might consult an introductory book to OR or IP. You might also check this <a href="http://web.mit.edu/15.053/www/AMP-Chapter-09.pdf">lecture note</a>.</p>EhsanFri, 22 May 2015 05:36:23 -0400http://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams#12328Comment by deniz on Ehsan's answerhttp://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams#12283<p>Thank you so much Ehsan.
I have been really struggling to understand the concept and unfortunately, I cannot find way to solve the problem.</p>
<p>Here is my gams code. Could you please give an example (simple one) when you say I cannot write variable in if else statement. What type of key words should I use in if else statement. Simple example might help me a lot.
I appreciate your help very much. </p>
<p>Z1,Z2,Z3 are binary variables </p>
<p>const3a(T,P).. lambda(p,t)=e= Lambda(P,t-1)<em>A+Lambda(P,t-1)</em>B1<em>Z1(t-1,p)+Lambda(P,t-1)</em>B2<em>Z2(t-1,p)+Lambda(P,t-1)</em>KB*Z3(T-1,P);</p>
<p>const4(t+1,p)..
if((z1(t,p)=0)and (z2(t,p)=0) and (z3(t,p)=0),lambda(p,t)= Lambda(P,t-1)<em>A);
elseif ((z1(t-1,p)=1), lambda(p,t)= Lambda(P,t-1)</em>B1);
elseif ((z2(t-1,p)=1), lambda(p,t)= Lambda(P,t-1)<em>B2);
elseif ((z3(t-1,p))=1, lambda(p,t)= Lambda(P,t-1)</em>B3);
);</p>
<p>Const5(T,P) ..Z1(T,P)+ Z2(T,P)+Z3(T,P)=e=1;</p>
<p>Const6(T,P) .. If ((lambda(p,t)< R1),
z1(t,p)=0;
z2(t,p)=0;
elseif ((lambda(p,t)> R1 and (lambda(p,t)< R2),
z1(t,p)=0;
z3(t,p)=0;
elseif ((lambda(p,t)> R2 and (lambda(p,t)< R3),
z2(t,p)=0;
z3(t,p)=0;
elseif ((lambda(p,t)> R3),
z1(t,p)=0;
z2(t,p)=0;
z3(t,p)=0;
);</p>denizTue, 19 May 2015 10:02:04 -0400http://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams#12283Answer by Ehsanhttp://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams/12269<p>AFAIK, if, ifthen, and other similar keywords in GAMS are used for manipulating data and/or scripting (e.g., writing an algorithm such as benders decomposition). Hence, you cannot use them directly with variables in the statements. In other words, you should use these keywords only with statements involving parameters (i.e., before/after the solve statement) or attributes of variables (i.e., after the solve statement).</p>
<p>PS. Your post is different from the email alert sent by OR-X for an unknown reason. You might want to edit your post to make it clearer for everybody.</p>EhsanTue, 19 May 2015 00:45:37 -0400http://www.or-exchange.com/questions/12268/several-if-then-or-if-else-using-gams/12269