I wish to kindly ask for help on how to linearize the following anded condition

if(r=m and m >0)
then c=1
else c=0

thank you in advance

asked 19 Apr '16, 12:24

nardinebasta's gravatar image

accept rate: 0%

Presumably \(r\), \(m\) and \(c\) are all variables in an optimization model. Are \(r\) and \(m\) continuous, general integer or binary?

(19 Apr '16, 15:17) Paul Rubin ♦♦

yes they are all variables. c is binary {0,1} r and m are in [0,inf) theoretically but technically the upper bound won't exceed 100.

r stands for a set of attraction values, m is the maximum value, the statement is trying to replace the argmax by finding the element(s) that has the maximum value.

Thank you :)

(19 Apr '16, 15:31) nardinebasta

If m is in R+ then the second part of the condition is redundant, so we are left with if r==m ... However I don't think comparing floats for equality is a good idea; how about turning the condition into an inequality, i.e. if r<m then c=0 else c=1 ?


answered 19 Apr '16, 15:43

ocramz's gravatar image

accept rate: 0%

edited 19 Apr '16, 15:44

the idea is that in some cases all the elements in R are zeros thus the maximum value in this case is zero. I want to exclude this case from the solution. i.e. c =0 if m = 0 even if r=m

(19 Apr '16, 15:52) nardinebasta

Setting \(c=1\) if and only if \(r=m\) can only be done approximately, meaning setting \(c=1\) if \(r\) is within some nonzero tolerance of \(m\). I posted a solution for that a while back. Notational conversion: \(x, y, m\) in my post would be \(c, r - m, 0\) respectively for you.

You also specified the condition \(m>0\), which again can only be handled to within some tolerance. Let me relabel the binary variable in the previous part \(c_1\). Introduce a second binary variable \(c_2\) with the constraints \(\epsilon c_2 \le m \le 100c_2\). Now you want a constraint that says that \(c\) is the conjunction of \(c_1\) and \(c_2\), which is easy: \(c \le c_1, c \le c_2, c \ge c_1 + c_2 - 1\).


answered 19 Apr '16, 15:53

Paul%20Rubin's gravatar image

Paul Rubin ♦♦
accept rate: 19%

edited 19 Apr '16, 15:56

thank you so much for your help. I am sorry I find difficulty in understanding the notations.

so c is my c or is c1 is my c? what does c1 and c2 stands for? my r and m are not integers

May I kindly ask you to put it in my notations so I can understand the solution.

thank you again so much

(19 Apr '16, 16:17) nardinebasta

Your \(c\) is \(c_1\) AND \(c_2\). \(c_1\) indicates whether \(r = m\); \(c_2\) indicates whether \(m > 0\) (more precisely, whether \(m \ge \epsilon\)).

(19 Apr '16, 16:35) Paul Rubin ♦♦


a problem popped up with some data after applying the above constraints.

in some cases my maximum variable is altered to have the value of 100 while in fact it should not be 100.

m= max(y) y is a vector of doubles in [0,100]

i am calculating the max through the following constraint


"the actual constraints looks as follows maximumAttraction(v,t) >= SocialAttrPerLoc(v,e,t)"

what might be the reason?

(13 Jul '16, 00:24) nardinebasta

If neither the constraints nor the objective function apply pressure on \(m\) to be as small as possible, and particularly if the objective function rewards larger values of \(m\), the model will be free (and perhaps encouraged) to overstate \(m\). In such a case, you need to use binary variables (the number of binaries equaling the dimension of \(y\)) to enforce that \(m\) actually equals the maximum.

(13 Jul '16, 14:46) Paul Rubin ♦♦

it seems that my constraints are not reliable, as with some data i receive the message that the model is not reliable. also with some data the variable are given values so that r has the minimum values they could have! I will be thankful to now what is wrong with my constraints

i am using the following set of constrains to manage the maximum where m is the maximum and z3 is a binary where r in[0,100]:

m(v,t) <= r(v,e,t)+100*(1-z3(v,e,t))
m(v,t) >= r(v,e,t)
sum(e ,z3(v,e,t))=1

i am using the following constrains to manage if(r=m)c1=1 else c1 =0, where z1,z2 are binaries r in[0,100] and m is the maximum.


to manage that if(m=0)c2=0 (with this i mean if r = m c should be 1 however if m =r=0, c should be zero)


to manage the conjunction of c1 and c 2


to manage that i have only one chosen location c per vehicle v i added:

sum( e , c(v,e,t) ) <=1

my objective function is to maximize the following:

sum[(v,e,t) | e=Destination(v,t),c(v,e,t)]

the variable to optimize are in the calculation of r(v,e,t)values.

many thanks


answered 14 Jul '16, 23:19

nardinebasta's gravatar image

accept rate: 0%

edited 23 Jul '16, 15:23

Just to be clear, when you write a constraint such as \(9-c(v,e,t) \le c1(v,3,6)\), are you subtracting something from 9 on the left side, or is \(9-\) just an equation number (in which case I would strongly urge you not to number things in such a confusing way in the future)?

(23 Jul '16, 13:52) Paul Rubin ♦♦

This is the equation number. I will put this in mind when posting. My apologies

(23 Jul '16, 15:21) nardinebasta
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](http://url.com/ "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: 19 Apr '16, 12:24

Seen: 978 times

Last updated: 26 Jul '16, 16:00

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