Skip to content

Commit

Permalink
Fix get for MOI.VariablePrimalStart and error when x is a Parameter (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Nov 24, 2024
1 parent 5e3aec2 commit d5ee69f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -630,14 +630,26 @@ function MOI.supports(
return true
end

function MOI.get(
model::Optimizer,
attr::MOI.VariablePrimalStart,
vi::MOI.VariableIndex,
)
if _is_parameter(vi)
throw(MOI.GetAttributeNotAllowed(attr, "Variable is a Parameter"))
end
MOI.throw_if_not_valid(model, vi)
return model.variable_primal_start[column(vi)]
end

function MOI.set(
model::Optimizer,
::MOI.VariablePrimalStart,
attr::MOI.VariablePrimalStart,
vi::MOI.VariableIndex,
value::Union{Real,Nothing},
)
if _is_parameter(vi)
return # Do nothing
throw(MOI.SetAttributeNotAllowed(attr, "Variable is a Parameter"))
end
MOI.throw_if_not_valid(model, vi)
model.variable_primal_start[column(vi)] = value
Expand Down
20 changes: 20 additions & 0 deletions test/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,26 @@ function test_nlp_model_set_set()
return
end

function test_VariablePrimalStart()
attr = MOI.VariablePrimalStart()
model = Ipopt.Optimizer()
x = MOI.add_variable(model)
@test MOI.supports(model, attr, typeof(x))
@test MOI.get(model, attr, x) === nothing
MOI.set(model, attr, x, 1.0)
@test MOI.get(model, attr, x) == 1.0
p, _ = MOI.add_constrained_variable(model, MOI.Parameter(1.0))
@test_throws(
MOI.GetAttributeNotAllowed{typeof(attr)},
MOI.get(model, attr, p),
)
@test_throws(
MOI.SetAttributeNotAllowed{typeof(attr)},
MOI.set(model, attr, p, 1.0),
)
return
end

function test_manually_evaluated_primal_status()
model = Ipopt.Optimizer()
MOI.set(model, MOI.Silent(), true)
Expand Down

0 comments on commit d5ee69f

Please sign in to comment.