Skip to content
This repository has been archived by the owner on Jun 14, 2020. It is now read-only.

make setting and getting the objective constant a part of the LQOI interface #44

Merged
merged 1 commit into from
Jul 31, 2018

Conversation

rdeits
Copy link
Contributor

@rdeits rdeits commented Jul 31, 2018

First step for #43

@codecov-io
Copy link

codecov-io commented Jul 31, 2018

Codecov Report

Merging #44 into master will increase coverage by 0.05%.
The diff coverage is 88.88%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #44      +/-   ##
==========================================
+ Coverage   75.29%   75.35%   +0.05%     
==========================================
  Files          13       13              
  Lines        1263     1266       +3     
==========================================
+ Hits          951      954       +3     
  Misses        312      312
Impacted Files Coverage Δ
src/solve.jl 0% <ø> (ø) ⬆️
src/solver_interface.jl 53.33% <100%> (+11.66%) ⬆️
src/objective.jl 88.09% <83.33%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d6957f3...4c8289a. Read the comment docs.

@rdeits
Copy link
Contributor Author

rdeits commented Jul 31, 2018

Gurobi.jl tests also pass for me with LQOI set to this branch.

@odow odow mentioned this pull request Jul 31, 2018
4 tasks
@rdeits
Copy link
Contributor Author

rdeits commented Jul 31, 2018

I tried updating Gurobi.jl to overload the functions introduced here by setting ObjCon, but i think I may have found a bug in Gurobi itself. While setting ObjCon before solving the model works once, further modifications to it have no effect on the objective value returned by later solves. You should be able to reproduce this without needing this particular branch:

model = GurobiOptimizer();
MOI.empty!(model)
MOIU.loadfromstring!(model,"""
    variables: x
    maxobjective: 1.0x
    c1: 1.0x <= 1.0
""")
x = MOI.get(model, MOI.VariableIndex, "x")

Gurobi.set_dblattr!(model.inner, "ObjCon", 1.0)  # this works
Gurobi.update_model!(model.inner)
MOI.optimize!(model)
# This assertion passes
@assert MOI.get(model, MOI.ObjectiveValue()) == 2.0  # (1.0 * 1.0 + 1.0)

Gurobi.set_dblattr!(model.inner, "ObjCon", 2.0)  # this has no effect
Gurobi.update_model!(model.inner)
MOI.optimize!(model)
# This assertion fails:
@assert MOI.get(model, MOI.ObjectiveValue()) == 3.0  # (1.0 * 1.0 + 2.0)

@odow
Copy link
Member

odow commented Jul 31, 2018

Do you have python/gurobipy installed? Otherwise I can have a look when I get back to my desk (edit: to see if it is a gurobi bug)

@rdeits
Copy link
Contributor Author

rdeits commented Jul 31, 2018

Yeah, I'm trying it in python now

@rdeits
Copy link
Contributor Author

rdeits commented Jul 31, 2018

Hm, I'm not sure how to reproduce this in Python, since the API is totally different. I tried doing:

from __future__ import print_function
import gurobipy as grb

m = grb.Model("lp")
x = m.addVar(name="x")
obj = 1.0 * x + 1.0
m.setObjective(obj, sense=grb.GRB.MAXIMIZE)
m.addConstr(1.0 * x <= 1.0)
m.optimize()
print("objval:", m.getObjective().getValue())

m.setObjective(1.0 * x + 2.0)
m.optimize()
print("objval:", m.getObjective().getValue())

which works just fine, but that involves replacing the entire objective expression, which isn't what we're doing in Julia.

@rdeits
Copy link
Contributor Author

rdeits commented Jul 31, 2018

In Julia, I get the correct result if I also set the "Obj" attribute (to the same value it had before):

model = GurobiOptimizer();
MOI.empty!(model)
MOIU.loadfromstring!(model,"""
    variables: x
    maxobjective: 1.0x
    c1: 1.0x <= 1.0
""")
x = MOI.get(model, MOI.VariableIndex, "x")

Gurobi.set_dblattr!(model.inner, "ObjCon", 1.0)  # this works
Gurobi.update_model!(model.inner)
MOI.optimize!(model)
# This assertion passes
@assert MOI.get(model, MOI.ObjectiveValue()) == 2.0  # (1.0 * 1.0 + 1.0)

Gurobi.set_dblattr!(model.inner, "ObjCon", 2.0) 
Gurobi.set_dblattrarray!(model.inner, "Obj", 1, 1, ones(1))
Gurobi.update_model!(model.inner)
MOI.optimize!(model)
@assert MOI.get(model, MOI.ObjectiveValue()) == 3.0  # (1.0 * 1.0 + 2.0)

@rdeits
Copy link
Contributor Author

rdeits commented Jul 31, 2018

Ok, I can reproduce the issue in Python:

from __future__ import print_function
import gurobipy as grb

m = grb.Model("lp")
x = m.addVar(name="x")
obj = 1.0 * x + 1.0
m.setObjective(obj, sense=grb.GRB.MAXIMIZE)
m.addConstr(1.0 * x <= 1.0)
m.optimize()
print("ObjVal:", m.getAttr("ObjVal"))

m.setAttr("ObjCon", 2.0)
m.optimize()
print("ObjVal:", m.getAttr("ObjVal"))

@odow
Copy link
Member

odow commented Jul 31, 2018

Have you sent them an issue (https://groups.google.com/forum/#!newtopic/gurobi)? Otherwise I'll do it now.

@rdeits
Copy link
Contributor Author

rdeits commented Jul 31, 2018

I just did (but it looks like my message is still being held for moderation)

@odow
Copy link
Member

odow commented Jul 31, 2018

Since @joaquimg is happy, would you like me to merge this now or wait for Gurobi?

@rdeits
Copy link
Contributor Author

rdeits commented Jul 31, 2018

I'd say go ahead and merge. The issue with Gurobi is annoying but not related to this PR, and I think this is a good change nonetheless.

@odow odow merged commit 2676245 into JuliaOpt:master Jul 31, 2018
@rdeits rdeits deleted the rd/set-constant-objective branch July 31, 2018 22:17
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants