Disassociate parameter or variable from a model in JuMP

Suppose I have associated the NLvariable y with the model mod.

Then suppose I change my mind and want to disassociate the variable y from mod.

Is there something like unset?

Please provide a minimal working example. We don’t have the concept of an NLvariable.

Use delete to delete a variable. Variables · JuMP

Re delete for a variable, many thanks for quick reply. I am still confused as the number of variables is decreasing, but the Names registered in the model is not.

julia> delete(mod0,y_11)

**julia>** mod0

A JuMP Model

Maximization problem with:

Variables: 5

Objective function type: Nonlinear

Model mode: AUTOMATIC

CachingOptimizer state: EMPTY_OPTIMIZER

Solver name: Ipopt

Names registered in the model: p_1, u, y_11, y_12, y_21, y_22

**julia>** delete(mod0, u)

**julia>** mod0

A JuMP Model

Maximization problem with:

Variables: 4

Objective function type: Nonlinear

Model mode: AUTOMATIC

CachingOptimizer state: EMPTY_OPTIMIZER

Solver name: Ipopt

Names registered in the model: p_1, u, y_11, y_12, y_21, y_22 

There is nothing equivalent for NLparameter right?

Not a major one, this is for quick REPL work really.

Oh I see, this is probably because these variables appear in equations such as the objective etc… Got it.

Or maybe not:

My objective is

@NLobjective(mod0, Max, 1.8898*y_11^.3333*y_21^.6666)

So, u actually doesn’t appear anywhere yet.

@variable(mod0, u, start = 1)

Take a read of the first post in PSA: make it easier to help you. It has some advice on how to write a good post on construct a minimal working example.

There is nothing equivalent for NLparameter right?

No. Certain base functions on JuMP constraints not implemented for NLconstraints. · Issue #2355 · jump-dev/JuMP.jl · GitHub

because these variables appear in equations such as the objective

You can delete variables that appear in objectives and constraints.

I am still confused as the number of variables is decreasing, but the Names registered in the model is not.

Looks like you found a bug :smile:. I don’t know how we missed this one for so long… I’ll open an issue.

julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> @variable(model, x)
x

julia> model
A JuMP Model
Feasibility problem with:
Variable: 1
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.
Names registered in the model: x

julia> delete(model, x)

julia> model[:x]
noname

julia> model
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.
Names registered in the model: x
1 Like

Actually, this is expected behavior. Use unregister(model, :x) to remove the name.

model = Model()
@variable(model, x)
delete(model, x)
unregister(model, :x)

Opened an issue to document this better: Document that delete does not call unregister · Issue #2589 · jump-dev/JuMP.jl · GitHub

1 Like

Please also document “why” not just “that” delete does not unregister. Understanding is key here.