About fixing variables

746 views
Skip to first unread message

LJN

unread,
Jul 25, 2015, 10:16:43 AM7/25/15
to Gurobi Optimization
Hello to Everyone.
I'm implementing a simple fixing heuristic following one of the examples provided (fixanddive.py) in the Gurobi Optimizer Example Tour, using gurobipy. I intend to set the lower bound of an originally defined as binary variable to 1. As in the example, I first relax all binary variables, and then use v.lb = 1, to set the bound. I noticed that this was causing no effect as I immediatelly print the bounds and the lower bound is still 0 (print v.lb). I'm most probably missing something here. Any help is greatly appreciated.

Here is part of the script for reference.


# def sortkey(v1):
# sol=v1.x
# return 1-sol

# while heuristic:
#
# M.update()
# M.optimize()
#
# fractional = []
# for v in M.getVars():
# sol = v.x
# if abs(sol-int(sol+0.5)) > 1e-5:
# fractional.append(v)
#
# fractional.sort(key=sortkey)

# c = 0
# for v in fractional:
# if v.x > fixbound:
# c+=1
# v.lb = 1
# elif c==0:
# v.lb = 1
# else:
# break
#
# M.update

Renan Garcia

unread,
Jul 25, 2015, 10:57:13 AM7/25/15
to gur...@googlegroups.com
You should always call Model.update() before querying for any model changes. For example,

   gurobi> m=Model()
   gurobi> v=m.addVar()
   gurobi> m.update()
   gurobi> v.lb=1
   gurobi> print v.lb
   0.0
   gurobi> m.update()
   gurobi> print v.lb
   1.0

Of course, you don't need to do this before calling Model.optimize(), as it will process any pending changes.

--

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

LJN

unread,
Jul 25, 2015, 12:59:01 PM7/25/15
to Gurobi Optimization
Thanks for the reply. I am updating after I set the bounds to 1 (please see the short script I sent) but no success. Now, if I call update() within the loop (after each v.lb=1), I get an error when trying to retreive the next variable value (v.x)

Thanks

Renan Garcia

unread,
Jul 25, 2015, 1:34:03 PM7/25/15
to gur...@googlegroups.com
I'm not suggesting you call Model.update() within the loop, as that's inefficient and will invalidate the current x. I just wanted to point out that if you were immediately printing v.lb within the loop, it would appear as if it was having no effect.

> On Jul 25, 2015, at 12:57 PM, LJN <luisjo...@gmail.com> wrote:
>
> Thanks for the reply. I am updating after I set the bounds to 1 (please see the short script I sent) but no success. Now, if I call update() within the loop (after each v.lb=1), I get an error when trying to retreive the next variable value (v.x)
>
> Thanks
>

LJN

unread,
Jul 25, 2015, 1:54:01 PM7/25/15
to Gurobi Optimization
Thanks again. Thats what I understood. My point is that after the update() done after the loop, when I print the lb, it still gives me 0.

Again, thanks for your help

Renan Garcia

unread,
Jul 25, 2015, 1:58:38 PM7/25/15
to gur...@googlegroups.com
I don't see a check for len(fractional) > 0 in your script. Is this in the part you excluded?

> On Jul 25, 2015, at 1:41 PM, LJN <luisjo...@gmail.com> wrote:
>
> Thanks again. Thats what I understood. My point is that after the update() done after the loop, when I print the lb, it still gives me 0.
>
> Again, thanks for your help
>

LJN

unread,
Jul 25, 2015, 2:06:35 PM7/25/15
to Gurobi Optimization
I included it in my code. I ommitted it from the example I pasted in my first message.
Reply all
Reply to author
Forward
0 new messages