Skip to content

Commit

Permalink
Fix MOI.is_valid support for MOI.VariableIndex (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Nov 15, 2023
1 parent 70988e4 commit 44872c4
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -315,28 +315,28 @@ function MOI.add_variable(model::Optimizer)
return MOI.VariableIndex(length(model.variable_info))
end

function _check_inbounds(model::Optimizer, x::MOI.VariableIndex)
num_variables = length(model.variable_info)
if !(1 <= x.value <= num_variables)
error("Invalid variable index $x. ($num_variables variables in the model.)")
end
return
function MOI.is_valid(model::Optimizer, x::MOI.VariableIndex)
return 1 <= x.value <= length(model.variable_info)
end

function _throw_if_not_valid(model::Optimizer, x::MOI.VariableIndex)
return MOI.throw_if_not_valid(model, x)
end

function _check_inbounds(model::Optimizer, aff::MOI.ScalarAffineFunction)
function _throw_if_not_valid(model::Optimizer, aff::MOI.ScalarAffineFunction)
for term in aff.terms
_check_inbounds(model, term.variable)
_throw_if_not_valid(model, term.variable)
end
return
end

function _check_inbounds(model::Optimizer, quad::MOI.ScalarQuadraticFunction)
function _throw_if_not_valid(model::Optimizer, quad::MOI.ScalarQuadraticFunction)
for term in quad.affine_terms
_check_inbounds(model, term.variable)
_throw_if_not_valid(model, term.variable)
end
for term in quad.quadratic_terms
_check_inbounds(model, term.variable_1)
_check_inbounds(model, term.variable_2)
_throw_if_not_valid(model, term.variable_1)
_throw_if_not_valid(model, term.variable_2)
end
return
end
Expand All @@ -363,7 +363,7 @@ function MOI.set(
x::MOI.VariableIndex,
value::Union{Real,Nothing},
)
_check_inbounds(model, x)
MOI.throw_if_not_valid(model, x)
start = something(value, 0.0)
KN_set_var_primal_init_value(model.inner, x.value - 1, Cdouble(start))
return
Expand Down Expand Up @@ -471,7 +471,7 @@ function MOI.add_constraint(
),
)
end
_check_inbounds(model, x)
MOI.throw_if_not_valid(model, x)
if isnan(lt.upper)
error("Invalid upper bound value $(lt.upper).")
end
Expand Down Expand Up @@ -513,7 +513,7 @@ function MOI.add_constraint(
),
)
end
_check_inbounds(model, x)
MOI.throw_if_not_valid(model, x)
if isnan(gt.lower)
error("Invalid lower bound value $(gt.lower).")
end
Expand Down Expand Up @@ -556,7 +556,7 @@ function MOI.add_constraint(
),
)
end
_check_inbounds(model, x)
MOI.throw_if_not_valid(model, x)
if isnan(set.lower) || isnan(set.upper)
error("Invalid lower bound value $(set.lower).")
end
Expand Down Expand Up @@ -601,7 +601,7 @@ function MOI.add_constraint(
),
)
end
_check_inbounds(model, x)
MOI.throw_if_not_valid(model, x)
if isnan(eq.value)
error("Invalid fixed value $(eq.value).")
end
Expand Down Expand Up @@ -658,7 +658,7 @@ end

function MOI.add_constraint(model::Optimizer, x::MOI.VariableIndex, ::MOI.ZeroOne)
indv = x.value - 1
_check_inbounds(model, x)
MOI.throw_if_not_valid(model, x)
model.number_zeroone_constraints += 1
lb, ub = nothing, nothing
if model.variable_info[x.value].has_lower_bound
Expand Down Expand Up @@ -691,7 +691,7 @@ function MOI.add_constraint(model::Optimizer, x::MOI.VariableIndex, set::MOI.Int
),
)
end
_check_inbounds(model, x)
MOI.throw_if_not_valid(model, x)
model.number_integer_constraints += 1
KN_set_var_type(model.inner, x.value - 1, KN_VARTYPE_INTEGER)
return MOI.ConstraintIndex{MOI.VariableIndex,MOI.Integer}(x.value)
Expand All @@ -718,7 +718,7 @@ function MOI.add_constraint(
),
)
end
_check_inbounds(model, func)
_throw_if_not_valid(model, func)
# Add a single constraint in KNITRO.
num_cons = KN_add_con(model.inner)
# Add bound to constraint.
Expand Down Expand Up @@ -801,7 +801,7 @@ function MOI.add_constraint(
),
)
end
_check_inbounds(model, func)
_throw_if_not_valid(model, func)
# We add a constraint in KNITRO.
num_cons = KN_add_con(model.inner)
# Add upper bound.
Expand Down Expand Up @@ -1143,7 +1143,7 @@ function MOI.set(
@warn("Objective is already specified in NLPBlockData.")
return
end
_check_inbounds(model, func)
_throw_if_not_valid(model, func)
model.objective = func
return
end
Expand Down Expand Up @@ -1529,7 +1529,7 @@ function MOI.get(model::Optimizer, v::MOI.VariablePrimal, x::MOI.VariableIndex)
elseif v.result_index > 1
throw(MOI.ResultIndexBoundsError{MOI.VariablePrimal}(v, 1))
end
_check_inbounds(model, x)
MOI.throw_if_not_valid(model, x)
return get_solution(model.inner, x.value)
end

Expand Down Expand Up @@ -1584,7 +1584,7 @@ function MOI.get(
throw(MOI.ResultIndexBoundsError{MOI.ConstraintPrimal}(cp, 1))
end
x = MOI.VariableIndex(ci.value)
_check_inbounds(model, x)
MOI.throw_if_not_valid(model, x)
return get_solution(model.inner, x.value)
end

Expand Down Expand Up @@ -1657,7 +1657,7 @@ function _reduced_cost(
throw(MOI.ResultIndexBoundsError{MOI.ConstraintDual}(attr, 1))
end
x = MOI.VariableIndex(ci.value)
_check_inbounds(model, x)
MOI.throw_if_not_valid(model, x)
offset = _number_constraints(model)
return _sense_dual(model) * get_dual(model.inner, x.value + offset)
end
Expand Down

0 comments on commit 44872c4

Please sign in to comment.