Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copying a Model and adding new constraints generates an error when calling solve(m) #490

Closed
h3nnn4n opened this issue Jul 26, 2015 · 13 comments

Comments

@h3nnn4n
Copy link

h3nnn4n commented Jul 26, 2015

Here I have a small snippet of code that reproduces an error on a bigger piece of code.

The code is:

using JuMP

## Simple LP model
m = Model()

@defVar(m, 0 <= x <= 2 )
@defVar(m, 0 <= y <= 30 )

@setObjective(m, Max, 5x + 3*y )
@addConstraint(m, 1x + 5y <= 3.0 )

## The model is copied
n = copy(m)

## New constraint using the same variables is added to the copy
@addConstraint(n, 2x + 3y <= 3.0 )

solve(m)

## Error Here
solve(n)

and the error:

h3nnn4n@clusterfuck ~/julia_bnb (git)-[master] % julia jump_copy_error.jl
ERROR: Variable not owned by model present in constraints
 in prepConstrMatrix at /home/h3nnn4n/.julia/v0.3/JuMP/src/solvers.jl:163
 in solveLP at /home/h3nnn4n/.julia/v0.3/JuMP/src/solvers.jl:221
 in solve at /home/h3nnn4n/.julia/v0.3/JuMP/src/solvers.jl:39
 in include at ./boot.jl:245
 in include_from_node1 at loading.jl:128
 in process_options at ./client.jl:285
 in _start at ./client.jl:354
while loading /home/h3nnn4n/julia_bnb/bug.jl, in expression starting on line 21

The error happens on both Julia 0.3 and 0.4 using the following packages:

julia> Pkg.status()
6 required packages:
 - GLPK                          0.2.16
 - GLPKMathProgInterface         0.1.13
 - JuMP                          0.9.2
@IainNZ
Copy link
Collaborator

IainNZ commented Jul 26, 2015

The error message is correct, the variables belong to the initial model, not the copy.
I guess what you want is a way to get a handle on the equivalent variables for the copy?

@h3nnn4n
Copy link
Author

h3nnn4n commented Jul 26, 2015

Yes.

In the code I am working on I have

type instance
    m :: Model
    weight :: Array{Int}
    profit :: Array{Int}
    size :: Int
    capacity :: Int
    x
end

When I copy the whole thing the model m keeps the reference to the original instance.x. Here is the whole code in case you want to take a closer look.

@h3nnn4n
Copy link
Author

h3nnn4n commented Jul 26, 2015

I think that #399 is what I want. I just pulled the last version, lets see if it works.

@h3nnn4n
Copy link
Author

h3nnn4n commented Jul 26, 2015

Here n is a copy of m. n has an empty varDict though. Looks like varDict wasn't copied to the new model.

julia> n = copy(m)
Maximization problem with:
 * 1 linear constraint
 * 2 variables
Solver set to Default

julia> print(m.varDict)
[:x=>x,:y=>y]
julia> print(n.varDict)
Dict{Symbol,Any}()

julia> getVar(m, :y)
y

julia> getVar(n, :y)
ERROR: No variable with name y
 in error at error.jl:21
 in getVar at /home/h3nnn4n/.julia/v0.3/JuMP/src/JuMP.jl:805

@IainNZ
Copy link
Collaborator

IainNZ commented Jul 26, 2015

Thats true. We'll need to implement code to take an existing variable and mapping it to the new model. I've actually had to do something similar for a JuMP extension, funny enough.

@joehuchette
Copy link
Contributor

@h3nnn4n try pulling the latest master and see if it helps.

@h3nnn4n
Copy link
Author

h3nnn4n commented Jul 27, 2015

It worked. Thank you for the quick responses and all the support.

@mlubin mlubin closed this as completed Jul 27, 2015
@FransdR
Copy link

FransdR commented Sep 1, 2016

Is it still possible to copy a model like this? I get the same error message as @h3nnn4n if I try this (also on his snippet of code).

ERROR: Variable not owned by model present in a constraint
 in prepConstrMatrix at /Users/FransdeRuiter/.julia/v0.4/JuMP/src/solvers.jl:563
 in build at /Users/FransdeRuiter/.julia/v0.4/JuMP/src/solvers.jl:372
 in solve at /Users/FransdeRuiter/.julia/v0.4/JuMP/src/solvers.jl:134

(Julia 0.4.6, JuMP 0.14)

@mlubin
Copy link
Member

mlubin commented Sep 1, 2016

You can copy a model, but you can't mix variables from different models. Use x_new = copy(x,m_new) to get a reference to x in the new model.

@FransdR
Copy link

FransdR commented Sep 1, 2016

Thanks Miles, that works! It makes sense as well of course, because you can refer to the variables without mentioning the model (with getvalue(x) for instance).

@ahmadreza-marandi
Copy link

ahmadreza-marandi commented Sep 13, 2019

I am trying to do the same in JuMP 0.18.6 and I got exactly the same error:

`
P_k=ones(10,1);

extra_cost=20*ones(10,1);

LP=Model(solver=CplexSolver());

@variable(LP,p[1:10]<=20);

@constraint(LP, sum(p) <= 100);

@objective(LP,Min,sum(p));

m=copy(LP);

p_new=copy(p,LP)

@constraint(m, 2*sum(p_new) <= 100);

solve(m)`

and this is the error:
ERROR: VariableNotOwnedError: Variable not owned by model present in constraint ...

When I checked the models details, it seemed that I had the right variables in the copied model:

julia> LP.varData
IdDict{Any,Any} with 1 entry:
p[i] <= 20 for all i in {1,2,..,9,10} => JuMPContainerData(:p, ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10],), JuMP.IndexPair[IndexPair(nothing, :(1:10))], :(()))
julia> m.varData
IdDict{Any,Any} with 1 entry:
p[i] <= 20 for all i in {1,2,..,9,10} => JuMPContainerData(:p, ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10],), JuMP.IndexPair[IndexPair(nothing, :(1:10))], :(()))

But when I checked the constraints in the models, the old constraint in the copied model didn't look to be right:

julia> LP.linconstr
1-element Array{JuMP.GenericRangeConstraint{JuMP.GenericAffExpr{Float64,Variable}},1}:
p[1] + p[2] + p[3] + p[4] + p[5] + p[6] + p[7] + p[8] + p[9] + p[10] <= 100

julia> m.linconstr
2-element Array{JuMP.GenericRangeConstraint{JuMP.GenericAffExpr{Float64,Variable}},1}:
col_1 + col_2 + col_3 + col_4 + col_5 + col_6 + col_7 + col_8 + col_9 + col_10 <= 100
2 p[1] + 2 p[2] + 2 p[3] + 2 p[4] + 2 p[5] + 2 p[6] + 2 p[7] + 2 p[8] + 2 p[9] + 2 p[10] <= 100

So, can someone tell me what can be wrong here?
Thanks

@odow
Copy link
Member

odow commented Sep 13, 2019

You probably need to use p_new=copy(p, m) instead of p_new=copy(p,LP).

p.s. In future, please post questions like this on the Discourse forum: https://discourse.julialang.org/c/domain/opt.

@ahmadreza-marandi
Copy link

@odow Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

7 participants