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

update to MOI 0.10 #65

Merged
merged 1 commit into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
name = "ParametricOptInterface"
uuid = "0ce4ce61-57bf-432b-a095-efac525d185e"
authors = ["Tomás Gutierrez <[email protected]>"]
version = "0.1.1"
version = "0.2"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"

[compat]
MathOptInterface = "0.9.20"
MathOptInterface = "0.10"
julia = "1"

[extras]
Expand Down
2 changes: 1 addition & 1 deletion benchmark/benchmark.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ MOI.Benchmarks.@add_benchmark function add_constraint_svf(new_model)
for i in 1:10_000
MOI.add_constraint(
model,
MOI.SingleVariable(x[i]),
x[i],
MOI.GreaterThan(1.0),
)
end
Expand Down
2 changes: 1 addition & 1 deletion docs/src/example.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ We declare the variable `x` as in a typical `MOI` model, and we add a non-negati
julia> x = MOI.add_variables(optimizer, length(c))

julia> for x_i in x
MOI.add_constraint(optimizer, MOI.SingleVariable(x_i), MOI.GreaterThan(0.0))
MOI.add_constraint(optimizer, x_i, MOI.GreaterThan(0.0))
end
```

Expand Down
46 changes: 23 additions & 23 deletions src/ParametricOptInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ end
function MOI.supports_constraint(
model::Optimizer,
F::Union{
Type{MOI.SingleVariable},
Type{MOI.VariableIndex},
Type{MOI.ScalarAffineFunction{T}},
Type{MOI.VectorOfVariables},
Type{MOI.VectorAffineFunction{T}},
Expand Down Expand Up @@ -205,16 +205,16 @@ function MOI.supports(
model::Optimizer,
attr::Union{
MOI.ObjectiveSense,
MOI.ObjectiveFunction{MOI.SingleVariable},
MOI.ObjectiveFunction{MOI.VariableIndex},
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{T}},
MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{T}},
},
) where {T}
return MOI.supports(model.optimizer, attr)
end

function MOI.Utilities.supports_default_copy_to(model::Optimizer, bool::Bool)
return MOI.Utilities.supports_default_copy_to(model.optimizer, bool)
function MOI.supports_incremental_interface(model::Optimizer)
return MOI.supports_incremental_interface(model.optimizer)
end
function MOI.supports(model::Optimizer, ::MOI.Name)
return MOI.supports(model.optimizer, MOI.Name())
Expand Down Expand Up @@ -421,8 +421,8 @@ function MOI.get(model::Optimizer, ::MOI.TimeLimitSec)
return MOI.get(model.optimizer, MOI.TimeLimitSec())
end

function MOI.get(model::Optimizer, ::MOI.SolveTime)
return MOI.get(model.optimizer, MOI.SolveTime())
function MOI.get(model::Optimizer, ::MOI.SolveTimeSec)
return MOI.get(model.optimizer, MOI.SolveTimeSec())
end

function MOI.supports(model::Optimizer, ::MOI.Silent)
Expand Down Expand Up @@ -458,7 +458,7 @@ function MOI.get(
attr::MOI.ConstraintSet,
ci::MOI.ConstraintIndex{F,S},
) where {
F<:Union{MOI.SingleVariable,MOI.VectorOfVariables,MOI.VectorAffineFunction},
F<:Union{MOI.VariableIndex,MOI.VectorOfVariables,MOI.VectorAffineFunction},
S<:MOI.AbstractSet,
}
return MOI.get(model.optimizer, attr, ci)
Expand All @@ -481,7 +481,7 @@ function MOI.get(
attr::MOI.ObjectiveFunction{F},
) where {
F<:Union{
MOI.SingleVariable,
MOI.VariableIndex,
MOI.ScalarAffineFunction{T},
MOI.ScalarQuadraticFunction{T},
},
Expand Down Expand Up @@ -511,8 +511,8 @@ end
# Here, you can look over keys(quadratic_added_cache) and add the F-S types of all the keys in constraints.
# To implement NumberOfConstraints, you call NumberOfConstraints to the inner optimizer.
# Then you remove the number of constraints of that that in values(quadratic_added_cache)
function MOI.get(model::Optimizer, ::MOI.ListOfConstraints)
inner_ctrs = MOI.get(model.optimizer, MOI.ListOfConstraints())
function MOI.get(model::Optimizer, ::MOI.ListOfConstraintTypesPresent)
inner_ctrs = MOI.get(model.optimizer, MOI.ListOfConstraintTypesPresent())
if !has_quadratic_constraint_caches(model)
return inner_ctrs
end
Expand All @@ -538,7 +538,7 @@ end
function MOI.get(
model::Optimizer,
attr::MOI.ListOfConstraintIndices{F,S},
) where {S,F<:Union{MOI.VectorOfVariables,MOI.SingleVariable}}
) where {S,F<:Union{MOI.VectorOfVariables,MOI.VariableIndex}}
return MOI.get(model.optimizer, attr)
end

Expand Down Expand Up @@ -609,7 +609,7 @@ function MOI.add_constrained_variable(model::Optimizer, set::Parameter)
next_parameter_index!(model)
p = MOI.VariableIndex(model.last_parameter_index_added)
model.parameters[p] = set.val
cp = MOI.ConstraintIndex{MOI.SingleVariable,Parameter}(
cp = MOI.ConstraintIndex{MOI.VariableIndex,Parameter}(
model.last_parameter_index_added,
)
update_number_of_parameters!(model)
Expand All @@ -618,12 +618,12 @@ end

function MOI.add_constraint(
model::Optimizer,
f::MOI.SingleVariable,
f::MOI.VariableIndex,
set::MOI.AbstractScalarSet,
)
if is_parameter_in_model(model, f.variable)
if is_parameter_in_model(model, f)
error("Cannot constrain a parameter")
elseif !is_variable_in_model(model, f.variable)
elseif !is_variable_in_model(model, f)
error("Variable not in the model")
end
return MOI.add_constraint(model.optimizer, f, set)
Expand Down Expand Up @@ -674,7 +674,7 @@ end
function MOI.set(
model::Optimizer,
::MOI.ConstraintSet,
cp::MOI.ConstraintIndex{MOI.SingleVariable,Parameter},
cp::MOI.ConstraintIndex{MOI.VariableIndex,Parameter},
set::Parameter,
)
p = MOI.VariableIndex(cp.value)
Expand Down Expand Up @@ -760,7 +760,7 @@ end
function MOI.set(
model::Optimizer,
attr::MOI.ObjectiveFunction,
v::MOI.SingleVariable,
v::MOI.VariableIndex,
)
if haskey(model.parameters, v)
error("Cannot use a parameter as objective function alone")
Expand All @@ -770,7 +770,7 @@ function MOI.set(
return MOI.set(
model.optimizer,
attr,
MOI.SingleVariable(model.variables[v.variable]),
MOI.VariableIndex(model.variables[v.variable]),
)
end

Expand Down Expand Up @@ -806,14 +806,14 @@ function MOI.get(
return MOI.get(model.optimizer, attr, c)
end

function MOI.set(model::Optimizer, attr::MOI.RawParameter, val::Any)
function MOI.set(model::Optimizer, attr::MOI.RawOptimizerAttribute, val::Any)
MOI.set(model.optimizer, attr, val)
return
end

# TODO(odow): remove this.
function MOI.set(model::Optimizer, attr::String, val::Any)
return MOI.set(model.optimizer, MOI.RawParameter(attr), val)
return MOI.set(model.optimizer, MOI.RawOptimizerAttribute(attr), val)
end

function MOI.get(model::Optimizer, ::MOI.SolverName)
Expand Down Expand Up @@ -964,7 +964,7 @@ function MOI.delete(
model::Optimizer,
c::MOI.ConstraintIndex{F,S},
) where {
F<:Union{MOI.SingleVariable,MOI.VectorOfVariables,MOI.VectorAffineFunction},
F<:Union{MOI.VariableIndex,MOI.VectorOfVariables,MOI.VectorAffineFunction},
S<:MOI.AbstractSet,
}
MOI.delete(model.optimizer, c)
Expand All @@ -975,7 +975,7 @@ function MOI.is_valid(
model::Optimizer,
c::MOI.ConstraintIndex{F,S},
) where {
F<:Union{MOI.SingleVariable,MOI.VectorOfVariables,MOI.VectorAffineFunction},
F<:Union{MOI.VariableIndex,MOI.VectorOfVariables,MOI.VectorAffineFunction},
S<:MOI.AbstractSet,
}
return MOI.is_valid(model.optimizer, c)
Expand Down Expand Up @@ -1065,7 +1065,7 @@ function MOI.set(
end

if !isempty(quad_terms)
f_quad = MOI.ScalarQuadraticFunction(aff_terms, quad_terms, const_term)
f_quad = MOI.ScalarQuadraticFunction(quad_terms, aff_terms, const_term)

MOI.set(model.optimizer, attr, f_quad)
else
Expand Down
21 changes: 9 additions & 12 deletions src/duals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ end
function update_duals_with_affine_constraint_cache!(
param_dual_cum_sum::Vector{Float64},
optimizer::OT,
affine_constraint_cache_inner::MOI.Utilities.DoubleDicts.WithType{F,S},
) where {OT,F,S}
affine_constraint_cache_inner::MOI.Utilities.DoubleDicts.DoubleDictInner{F, S, V}
) where {OT, F, S, V}
for (ci, param_array) in affine_constraint_cache_inner
calculate_parameters_in_ci!(
param_dual_cum_sum,
Expand Down Expand Up @@ -76,11 +76,8 @@ end
function update_duals_with_quadratic_constraint_cache!(
param_dual_cum_sum::Vector{Float64},
model::Optimizer,
quadratic_constraint_cache_pc_inner::MOI.Utilities.DoubleDicts.WithType{
F,
S,
},
) where {F,S}
quadratic_constraint_cache_pc_inner::MOI.Utilities.DoubleDicts.DoubleDictInner{F, S, V}
) where {F, S, V}
for (poi_ci, param_array) in quadratic_constraint_cache_pc_inner
moi_ci = model.quadratic_added_cache[poi_ci]
calculate_parameters_in_ci!(
Expand All @@ -102,7 +99,7 @@ function calculate_parameters_in_ci!(
cons_dual = MOI.get(optimizer, MOI.ConstraintDual(), ci)

for param in param_array
param_dual_cum_sum[param.variable_index.value-PARAMETER_INDEX_THRESHOLD] +=
param_dual_cum_sum[param.variable.value-PARAMETER_INDEX_THRESHOLD] +=
cons_dual * param.coefficient
end
return
Expand All @@ -113,7 +110,7 @@ function update_duals_in_affine_objective!(
affine_objective_cache::Vector{MOI.ScalarAffineTerm{T}},
) where {T}
for param in affine_objective_cache
param_dual_cum_sum[param.variable_index.value-PARAMETER_INDEX_THRESHOLD] +=
param_dual_cum_sum[param.variable.value-PARAMETER_INDEX_THRESHOLD] +=
param.coefficient
end
return
Expand All @@ -124,7 +121,7 @@ function update_duals_in_quadratic_objective!(
quadratic_objective_cache_pc::Vector{MOI.ScalarAffineTerm{T}},
) where {T}
for param in quadratic_objective_cache_pc
param_dual_cum_sum[param.variable_index.value-PARAMETER_INDEX_THRESHOLD] +=
param_dual_cum_sum[param.variable.value-PARAMETER_INDEX_THRESHOLD] +=
param.coefficient
end
return
Expand All @@ -147,7 +144,7 @@ struct ParameterDual <: MOI.AbstractVariableAttribute end
function MOI.get(model::Optimizer, ::ParameterDual, vi_val::Int64)
if !is_additive(
model,
MOI.ConstraintIndex{MOI.SingleVariable,Parameter}(vi_val),
MOI.ConstraintIndex{MOI.VariableIndex,Parameter}(vi_val),
)
error("Cannot calculate the dual of a multiplicative parameter")
end
Expand All @@ -157,7 +154,7 @@ end
function MOI.get(
model::Optimizer,
::MOI.ConstraintDual,
cp::MOI.ConstraintIndex{MOI.SingleVariable,Parameter},
cp::MOI.ConstraintIndex{MOI.VariableIndex,Parameter},
)
if !is_additive(model, cp)
error("Cannot calculate the dual of a multiplicative parameter")
Expand Down
Loading