Skip to content

Deleting constraint in order to modify it not possible due to name registered in model #1956

@junglegobs

Description

@junglegobs

I run an MILP several times with different parameter values. Instead of recreating the model every time, I would like to be able to just change the relevant constraints. From the documentation, I figured that I'd be able to do this by deleting the constraint and then reformulating it.

Here's a toy example:

using JuMP

model = Model(with_optimizer(Gurobi.Optimizer))
@variable(model, x[i=1:2,j=1:2])
@constraint(model, con[i=1:2,j=1:2], x[i,j] >= 1)
for i=1:2
    for j=1:2
        delete(model, con[i,j])
    end
end
@constraint(model, con[i=1:2,j=1:2], x[i,j] >= 2)

This produces the error:

ERROR: LoadError: An object of name con is already attached to this model. If this is intended, consider using the anonymous construction syntax, e.g., x = @variable(model, [1:N], ...) where the name of the object does not appear inside the macro.

One workaround I can see is to delete the name con in model, but I don't know how / cannot do this. Ideally I would like a simpler way of modifying constraints, for example as:

model = Model(with_optimizer(Gurobi.Optimizer))
@variable(model, x[i=1:2,j=1:2])
@constraint(model, con[i=1:2,j=1:2], x[i,j] >= 1)
@constraint(model, con[i=1:2,j=1:2], x[i,j] >= 2)

However it would appear that this is quite difficult judging from this issue: #1241

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions