Solving a model and its modified copy


#1

As a follow-up to IainNZ’s comments on issue #399, I’ve slightly modified the code to experiment with variable fixings. While I only intend to fix variables of the object r, the fixings are applied to object p as well. I don’t quite understand the functioning of the fixings here. Could someone elaborate on what’s happening behind the scenes?

using JuMP
using CPLEX

type MyProblem
  model
  x
  y
end

function createproblem()
  m = Model(solver=CplexSolver())
  @variable(m, 0 <= x <= 2 )
  @variable(m, 0 <= y <= 30 )

  @objective(m, Max, 5x + 3*y )
  @constraint(m, 2x + 5y <= 3.0 )
  return MyProblem(m, x, y)
end

p = createproblem()
r = p
setlowerbound(r.x, 2)
setupperbound(r.x, 2)

solve(r.model)
solve(p.model)

BoundsError when re-solving a model
#2

setlowerbound(obj, value) operates in-place. When setting r = p, it only sets another pointer to the same memory location. If you want to keep a copy you should use copy, but is pretty common not to have that method defined for MyStructs. Try modifying the code to,

r = deepcopy(p)

#3

Thanks a lot @Nosferican, your solution works for me.