constant term in LHS of constraint then updating RHS

637 views
Skip to first unread message

Fabien Tricoire

unread,
Jul 1, 2015, 9:16:32 AM7/1/15
to gur...@googlegroups.com
Hi,

I first create a constraint of the form q - y <= a, where q and a are constants and y is a continuous variable. Then I change the value of a by updating the RHS of this constraint. This simply does not work in my experience. My understanding is that Gurobi subtracts the constant term from both LHS and RHS. Because of that, subsequent updates of RHS assuming the original constraint definition are invalid.

See attached file for a small example running on 6.0.0. In my opinion constraint z2Bound should be "10 - y <= -1" or "-y <= -11", but my program produces an lp file that defines it as "-y <= -1". The documentation states that constant terms are allowed in LHS of constraints, so one would expect that it just works. Is there anything I am missing?

kind regards,
Fabien Tricoire

constant_term_LHS.py

Renan Garcia

unread,
Jul 1, 2015, 9:21:25 AM7/1/15
to gur...@googlegroups.com
In your Python code, you add the constraint 10 - y <= 0, and then change its RHS to -1. When you add the initial constraint, Gurobi transforms it to standard a'x <= b form, or '-y <= -10'. By subsequently changing the RHS to -1, you transform that constraint to '-y <= -1'.

--

---
You received this message because you are subscribed to the Google Groups "Gurobi Optimization" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gurobi+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
<constant_term_LHS.py>

Fabien Tricoire

unread,
Jul 1, 2015, 9:52:30 AM7/1/15
to gur...@googlegroups.com
Thanks for the prompt reply!

I understand what is going on, but I don't think it should be this way. It is possible to define a constraint, then it is possible to modify it, except the version that is modified is not the same as the one that was defined. That is not very intuitive.

Fabien Tricoire

(apologies for the duplicate topic, I did not realise this one was awaiting moderation).

Renan Garcia

unread,
Jul 5, 2015, 12:14:03 PM7/5/15
to gur...@googlegroups.com
Thank you for the feedback. We'll look into how to improve our documentation to make this nuance clearer. That is, the RHS constraint attribute always refers to the constant after transforming to standard form (i.e., all variables appearing on the left hand side and the constant appearing on the right hand side), not the expression on the right hand side of the original constraint.

 

Note the Python API allows for adding a new constraint in non-standard form as a convenience to the user. But I would argue that converting the constraint to standard form after it's added removes ambiguity and makes it straightforward to query/modify it incrementally. You are guaranteed there will be a single coefficient for each variable and a single constant. Therefore, you know exactly where to access/modify any existing values.

vaibhav kumar

unread,
Jul 16, 2018, 12:50:23 AM7/16/18
to Gurobi Optimization
Hello,

I am facing a similar issue. I am trying to assign a value of the expression to a variable in the constraint. Instead of calculating the expression and assigning the values, the constraint is equating my LHS and RHS. I didn't find any documentation related to how to assign expression values. Gurobi has GRB.equal, but I dnt think it has any assignment type operator.  Below is the snippet.

list_of_locations=[list of values] (ex. [1,2,3-----])

for t in l:
    for j in list_of_locations:
        fjt[(j,t)] = model.addVar(vtype="B", name="fjt[%s,%s]"%(j,t))

for t in l:
    for j in list_of_locations:
        Xjt[(j,t)] = model.addVar(vtype="C", name="Xjt[%s,%s]"%(j,t))

for t in range(0,len(l)-1):       # t is different time period
      for j in list_of_locations:         
         model.addConstr(Xjt[(j,t)],"=",(fjt[(j,t)]-fjt[(j,t+1)]))        # here lies the problem I want to calculate the difference of the (fjt[(j,t)]-fjt[(j,t+1)]) and assign it to Xjt[(j,t)] to know                                                                                                  the locations when facility locations have changed during various time periods. so I expect if a facility moves out of j or a new facility is opened difference value would be {1,-1}, instead I a getting all values of Xjt to be zero because instead of substracting the values and assigning the values, constraint is just equating the LHS and RHS thus imposing the value of RHS to be zero as initialzed values of Xjt is zero.


What is the correct way to do this? 

As I tried this way as well:
fjt = m.addVars(l, T, vtype='B', name='fjt')
Xjt = m.addVars(l, T, vtype='C', name='Xjt')
diff = m.addConstrs(((Xjt[j, T[t]] == fjt[j,T[t]] - fjt[j,T[t+1]]) for j in l for t in range(len(T)-1)), name='diff')

Thanks in advance,

Vaibhav Kumar

Dr Michael F

unread,
Jul 16, 2018, 5:39:28 AM7/16/18
to Gurobi Optimization
I think the problem is that, by default, variables cannot be negative.  You should change Xjt to have a lower bound of -1.  lb=-1 in m.addVar. 
Reply all
Reply to author
Forward
0 new messages