Dear All,

I am trying to code several if then or if else using gams code but I cannot get it right.

my formulas and gams code are below. is there any easy way to write these codes?

Z1, Z2, and Z3 are binary variables. I try to wrote on of the equations as follow

asked 18 May '15, 19:07

deniz's gravatar image

accept rate: 0%

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.

When solving a MINLP it is often a good idea to first make sure that the RMINLP solves ok.


answered 03 Jun '15, 22:52

erwin's gravatar image

accept rate: 10%

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).

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.


answered 19 May '15, 00:45

Ehsan's gravatar image

Ehsan ♦
accept rate: 16%

Thank you so much Ehsan. I have been really struggling to understand the concept and unfortunately, I cannot find way to solve the problem.

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.

Z1,Z2,Z3 are binary variables

const3a(T,P).. lambda(p,t)=e= Lambda(P,t-1)A+Lambda(P,t-1)B1Z1(t-1,p)+Lambda(P,t-1)B2Z2(t-1,p)+Lambda(P,t-1)KB*Z3(T-1,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)A); elseif ((z1(t-1,p)=1), lambda(p,t)= Lambda(P,t-1)B1); elseif ((z2(t-1,p)=1), lambda(p,t)= Lambda(P,t-1)B2); elseif ((z3(t-1,p))=1, lambda(p,t)= Lambda(P,t-1)B3); );

Const5(T,P) ..Z1(T,P)+ Z2(T,P)+Z3(T,P)=e=1;

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; );

(19 May '15, 10:02) deniz

@deniz: 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 lecture note.

(22 May '15, 05:36) Ehsan ♦

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

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.

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.

(23 May '15, 00:53) deniz

I think adding the following constraints would do the trick:

\(R_1 * Z_2 + R_2 * Z_3 \leq \lambda\)

\(\lambda \leq R_1 * Z_1 + R_2* Z_2 + R_3 * Z_3\)

\(Z_1 + Z_2 + Z_3 = 1\)

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.

(23 May '15, 03:46) Ehsan ♦

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.

(23 May '15, 13:58) deniz

@deniz: 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\)).

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.

Feel free to ask any other question you might have. Good luck.

(23 May '15, 14:16) Ehsan ♦
showing 5 of 6 show 1 more comments

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

Warning 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.

"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.

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 ;


answered 26 May '15, 01:11

deniz's gravatar image

accept rate: 0%

@deniz: 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.

(28 May '15, 16:03) Ehsan ♦

@deniz: You should check the section 6.4 of the CONOPT documentation for more information.

(28 May '15, 16:04) Ehsan ♦

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....

(28 May '15, 18:55) deniz
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported



Asked: 18 May '15, 19:07

Seen: 1,671 times

Last updated: 03 Jun '15, 22:52

OR-Exchange! Your site for questions, answers, and announcements about operations research.