Hi there,
-- I am having trouble with a linearized IF-Else constraint within my code. I have recently started doing conditional linearization operations so kindly bear with me. The code has the following logic-- IF R0<=R<=R1 Then F==F1 ELSE IF R1<=R<=R2 Then F==F2 Where R and F are variables and R1, R2 are parameters Originally, R is the maximum value from a set of values, obtained by another linearized set of constraints. Now, after this operation is done, when I check back the R, it changes to that of either R1 or R2, depending on the range of values R falls within. Is this something that is supposed to happen? Also, as I do this operation, I use binary variables as indicators, which I later use for another set of IF-Else operations. If needed I can supply with my actual code that I wrote. Thanks in advance. --Ishita You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. To post to this group, send email to [hidden email]. Visit this group at https://groups.google.com/group/ampl. For more options, visit https://groups.google.com/d/optout. |
Hi Ishita,
It would be helpful to see the linearization that you use for these if-then-else constraints. Also it would help to have an example of what you mean when you say, "after this operation is done, when I check back the R, it changes to that of either R1 or R2, depending on the range of values R falls within," to make it clear what you mean by this. Also it is important to clarify whether R is required to be between R0 and R2, or whether R can be outside this range. Bob Fourer [hidden email] ======= From: [hidden email] [mailto:[hidden email]] On Behalf Of Ishita Sent: Wednesday, November 22, 2017 3:16 AM To: AMPL Modeling Language Subject: [AMPL 15193] If-Else conditions changing original value being compared I am having trouble with a linearized IF-Else constraint within my code. I have recently started doing conditional linearization operations so kindly bear with me. The code has the following logic-- IF R0<=R<=R1 Then F==F1 ELSE IF R1<=R<=R2 Then F==F2 Where R and F are variables and R1, R2 are parameters Originally, R is the maximum value from a set of values, obtained by another linearized set of constraints. Now, after this operation is done, when I check back the R, it changes to that of either R1 or R2, depending on the range of values R falls within. Is this something that is supposed to happen? Also, as I do this operation, I use binary variables as indicators, which I later use for another set of IF-Else operations. If needed I can supply with my actual code that I wrote. -- You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. To post to this group, send email to [hidden email]. Visit this group at https://groups.google.com/group/ampl. For more options, visit https://groups.google.com/d/optout. |
Hi Bob,
-- Thanks and sorry for late reply, I fixed the problem on my own, much appreciate your reply! I however have another If-Else issue to handle, and if I am stuck after sometimes, I will post here. Have a nice day! --Ishita On Friday, November 24, 2017 at 7:10:43 PM UTC+11, Robert Fourer wrote: Hi Ishita, You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. To post to this group, send email to [hidden email]. Visit this group at https://groups.google.com/group/ampl. For more options, visit https://groups.google.com/d/optout. |
In reply to this post by Robert Fourer-2
Hi Bob,
-- Thanks for the reply, sorry for getting back late. I thought I had the problem fixed but something went wrong and I am back to square one. So let me type out the problem below. Say, b is my maximum value among a set of variable values, U is the upper bound of my value range and L is the lower bound, q, r, s are my binary variables, M is big value. So, I need to model the following-- If L<= b<= U, then q, r are both 1, which in turn goes into an AND operation where s = q AND r so I divide the comparison constraint condition into two parts--L<= b and b<= U. So if L<= b then q = 1 otherwise 0. Similarly, if b<= U, then r = 1 otherwise 0. For, L<= b, L<= b + M * (1-q) L>= (b+1) - M * q For b<= U, b <= U + M * (1-r) b >= (U+1) - M * r For AND operation, 0 <= q + r - 2s <=1 So after this is performed, my b doesn't retain its actual value. Instead, b becomes U or L, depending on the constraint it satisfies. Is this something to happen normally? and b should never be outside the range of U and L. Let me know if you need more information, thanks. --Ishita On Friday, November 24, 2017 at 7:10:43 PM UTC+11, Robert Fourer wrote: Hi Ishita, You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. To post to this group, send email to [hidden email]. Visit this group at https://groups.google.com/group/ampl. For more options, visit https://groups.google.com/d/optout. |
If b is an integer-valued variable and you are trying to linearize the constraint
L <= b <= U ==> s = 1 else s = 0 then your constraints look right. But I do not know what you mean when you say, "my b doesn't retain its actual value." If b is a variable, then when you invoke "solve" the value of b will be set by the solver to the value that it has in some optimal solution, and this may well be different from any value it had before solving. (Of course if the value of b is fixed then you can make it a param, at which point the value of s is also fixed and can be defined with "param s = if L <= b <= U then 1 else 0".) Also I do not understand why you state that "b should never be outside the range of U and L." When you say "if L <= b <= U" you are implying that b might not be between L and U. Bob Fourer [hidden email] ======= From: [hidden email] [mailto:[hidden email]] On Behalf Of Ishita Sent: Friday, December 1, 2017 8:31 AM To: AMPL Modeling Language Subject: Re: [AMPL 15263] If-Else conditions changing original value being compared Thanks for the reply, sorry for getting back late. I thought I had the problem fixed but something went wrong and I am back to square one. So let me type out the problem below. Say, b is my maximum value among a set of variable values, U is the upper bound of my value range and L is the lower bound, q, r, s are my binary variables, M is big value. So, I need to model the following-- If L<= b<= U, then q, r are both 1, which in turn goes into an AND operation where s = q AND r so I divide the comparison constraint condition into two parts--L<= b and b<= U. So if L<= b then q = 1 otherwise 0. Similarly, if b<= U, then r = 1 otherwise 0. For, L<= b, L<= b + M * (1-q) L>= (b+1) - M * q For b<= U, b <= U + M * (1-r) b >= (U+1) - M * r For AND operation, 0 <= q + r - 2s <=1 So after this is performed, my b doesn't retain its actual value. Instead, b becomes U or L, depending on the constraint it satisfies. Is this something to happen normally? and b should never be outside the range of U and L. -- You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. To post to this group, send email to [hidden email]. Visit this group at https://groups.google.com/group/ampl. For more options, visit https://groups.google.com/d/optout. |
Hi Bob,
-- Thank you so much, I got the answer to my primary question, that value of b will change to something set by solver as it is a variable. As for the other part, sorry what I meant to write is that b must be less or equal than U, as set by an earlier constraint. It was a typo that b must be between L and U, it can very well be lower than L but never more than U. Maybe I elaborate a little bit. Here, b is a set of m variables calculated within the code itself--it is the maximum value within a set of (m*n) variable values for each m i.e. b is maximum of (X[m] * d[m,n]). I use the following code to calculate this maximum value b-- b[m] >= X[m] * d[m,n] b[m] <= (X[m] * d[m,n]) + (M *f[m,n]) where X,f are binary, d is a parameter constant and M is big value So does calculation of b look ok or am I missing something? Would much appreciate if you can let me know, thanks! --Ishita On Monday, December 4, 2017 at 4:26:32 AM UTC+11, Robert Fourer wrote: If b is an integer-valued variable and you are trying to linearize the constraint You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. To post to this group, send email to [hidden email]. Visit this group at https://groups.google.com/group/ampl. For more options, visit https://groups.google.com/d/optout. |
Your constraints to determine b[m] look OK, provided you also have a constraint that forces at least one of the f[m,n] variables to be zero.
Bob Fourer [hidden email] ======= From: [hidden email] [mailto:[hidden email]] On Behalf Of Ishita Sent: Tuesday, December 5, 2017 3:29 AM To: AMPL Modeling Language Subject: Re: [AMPL 15301] If-Else conditions changing original value being compared Thank you so much, I got the answer to my primary question, that value of b will change to something set by solver as it is a variable. As for the other part, sorry what I meant to write is that b must be less or equal than U, as set by an earlier constraint. It was a typo that b must be between L and U, it can very well be lower than L but never more than U. Maybe I elaborate a little bit. Here, b is a set of m variables calculated within the code itself--it is the maximum value within a set of (m*n) variable values for each m i.e. b is maximum of (X[m] * d[m,n]). I use the following code to calculate this maximum value b-- b[m] >= X[m] * d[m,n] b[m] <= (X[m] * d[m,n]) + (M *f[m,n]) where X,f are binary, d is a parameter constant and M is big value So does calculation of b look ok or am I missing something? Would much appreciate if you can let me know, thanks! -- You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. To post to this group, send email to [hidden email]. Visit this group at https://groups.google.com/group/ampl. For more options, visit https://groups.google.com/d/optout. |
Hi Bob,
-- Sorry for getting back late, was putting the code off in favour of writing. Now that I am back to coding again, just a quick recap-- This is my code to determine the maximum value of the set X[m] * d[m,n] b[m] >= X[m] * d[m,n] b[m] <= (X[m] * d[m,n]) + (M *f[m,n]) You asked me to put a constraint to force atleast one of the values of f[m,n] to become zero. Should it look like this-- sum {n in N} f[m,n] >= 0, for all m in M Also seems I am getting error 9, and I believe I am using CPLEX 12.7+ solver, so couldn't really figure what's going on. If I stop the code after a few minutes, I get some results, but if I let it run then at certain point, it reaches code 9 and terminates. Please advise some suggestions, thanks. On Thursday, December 7, 2017 at 2:07:54 AM UTC+11, Robert Fourer wrote: Your constraints to determine b[m] look OK, provided you also have a constraint that forces at least one of the f[m,n] variables to be zero. You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. To post to this group, send email to [hidden email]. Visit this group at https://groups.google.com/group/ampl. For more options, visit https://groups.google.com/d/optout. |
To force at least one of the values of f[m,n] to be zero for each m in M, you need to write
subject to AtLeastOneZero {m in M}: sum {n in N} (1-f[m,n]) >= 1; If you continue to get errors in CPLEX, before solving set option cplex_options 'mipdisplay=2'; (or if you are already setting a cplex_options string, add mipdisplay=2 to it). Then after the CPLEX run has ended, copy all of the resulting output into an email message back to [hidden email]. Bob Fourer mailto:[hidden email] ======= From: [hidden email] [mailto:[hidden email]] On Behalf Of Ishita Sent: Friday, January 19, 2018 10:04 AM To: AMPL Modeling Language <[hidden email]> Subject: Re: [AMPL 15551] If-Else conditions changing original value being compared Sorry for getting back late, was putting the code off in favour of writing. Now that I am back to coding again, just a quick recap-- This is my code to determine the maximum value of the set X[m] * d[m,n] b[m] >= X[m] * d[m,n] b[m] <= (X[m] * d[m,n]) + (M *f[m,n]) You asked me to put a constraint to force atleast one of the values of f[m,n] to become zero. Should it look like this-- sum {n in N} f[m,n] >= 0, for all m in M Also seems I am getting error 9, and I believe I am using CPLEX 12.7+ solver, so couldn't really figure what's going on. If I stop the code after a few minutes, I get some results, but if I let it run then at certain point, it reaches code 9 and terminates. -- You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. To post to this group, send email to [hidden email]. Visit this group at https://groups.google.com/group/ampl. For more options, visit https://groups.google.com/d/optout. |
Free forum by Nabble | Edit this page |