Pyomo / pyomo

An object-oriented algebraic modeling language in Python for structured optimization problems.
https://www.pyomo.org
Other
1.98k stars 507 forks source link

How to add var during search? #1503

Closed marcovc closed 3 years ago

marcovc commented 4 years ago

Hi!

I'm trying to add variables during search using a Gurobi persistent model. Here's a simplified example:

...
m.lazy_constraints = ConstraintList()
m.lazy_variables = VarList(domain=Binary)
solver = SolverFactory('gurobi_persistent')
solver.set_instance(m)

from gurobipy import GRB
def callback(cb_m, cb_opt, cb_where):
    if cb_where == GRB.Callback.MIPSOL:
        b = m.lazy_variables.add()
        cb_opt.add_var(b)
        ctr = m.lazy_constraints.add(b == 1)
        cb_opt.cbLazy(ctr)

solver.set_callback(intsol_callback)
results = solver.solve(tee=True)

This is failing with gurobipy.GurobiError: Variable not in model referring to my lazy variable. Do I need to call some function to make it aware of the just added variable?

Thanks Marco

michaelbynum commented 4 years ago

@marcovc I suggest checking Gurobi's documentation to see how column generation is supported in Gurobi.

marcovc commented 4 years ago

Thanks. I can achieve what I am after in gurobi with code very similar to the above. The only difference (other than syntax) is that I have to call model.update() between adding lazy vars and constraints. But I couldn't find the counterpart to that method in pyomo. Is there one?

marcovc commented 4 years ago

There is this method https://github.com/Pyomo/pyomo/blob/41e4b3afb23ffaa77b64a4459bdd3e3f77a481d6/pyomo/solvers/plugins/solvers/gurobi_direct.py#L836 which seems to do what I need. But since it is private, I suppose I shouldn't be calling it explicitely. Right?

marcovc commented 4 years ago

Calling it explicitely cause the program to segfault on the cbLazy() function call.

marcovc commented 4 years ago

It seems adding variables from a callback is not possible with gurobi. I believe I did it in the past, it must have been with cplex...

Thanks!

mrmundt commented 3 years ago

Hello, @marcovc. The GitHub Issues are intended for bug reports and enhancement/feature requests. For help or use questions, we refer you to review our documentation for ways to get help. If this is still an issue for you, please post your question on one of the preferred forums and we can engage with you there.