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

Add result fallbacks #447

Merged
merged 10 commits into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions docs/src/apireference.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ List of attributes useful for optimizers
RawSolver
ResultCount
ObjectiveFunction
ObjectiveFunctionType
ObjectiveValue
ObjectiveBound
RelativeGap
Expand Down
2 changes: 1 addition & 1 deletion src/Test/contconic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ function _psd0test(model::MOI.ModelLike, vecofvars::Bool, psdcone, config::TestC
@test MOI.canget(model, MOI.ConstraintDual(), typeof(c))
@test MOI.get(model, MOI.ConstraintDual(), c) ≈ 2 atol=atol rtol=rtol

cXv = square ? [1, -1, -1, 1] : [1, -1, 1]
cXv = square ? [1, -2, 0, 1] : [1, -1, 1]
@test MOI.canget(model, MOI.ConstraintDual(), typeof(cX))
@test MOI.get(model, MOI.ConstraintDual(), cX) ≈ cXv atol=atol rtol=rtol
end
Expand Down
2 changes: 2 additions & 0 deletions src/Utilities/Utilities.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Utilities

using Compat # For firstindex, lastindex and Nothing
using Compat.LinearAlgebra # For dot

using MathOptInterface
const MOI = MathOptInterface
Expand All @@ -20,6 +21,7 @@ const CI{F,S} = MOI.ConstraintIndex{F,S}
include("functions.jl")
include("sets.jl")
include("copy.jl")
include("results.jl")

include("model.jl")
include("parser.jl")
Expand Down
84 changes: 48 additions & 36 deletions src/Utilities/mockoptimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ mutable struct MockOptimizer{MT<:MOI.ModelLike} <: MOI.AbstractOptimizer
hasdual::Bool
terminationstatus::MOI.TerminationStatusCode
resultcount::Int
evalobjective::Bool # Computes ObjectiveValue by evaluation ObjectiveFunction with VariablePrimal
# Computes `ObjectiveValue` by evaluating the `ObjectiveFunction` with
# `VariablePrimal`. See `get_fallback`.
eval_objective_value::Bool
objectivevalue::Float64
objectivebound::Float64 # set this using MOI.set!(model, MOI.ObjectiveBound(), value)
primalstatus::MOI.ResultStatusCode
dualstatus::MOI.ResultStatusCode
varprimal::Dict{MOI.VariableIndex,Float64}
# Computes `ConstraintDual` of constraints with `SingleVariable` or
# `VectorOfVariables` functions by evaluating the `ConstraintDual` of
# constraints having the variable in the function. See `get_fallback`.
eval_variable_constraint_dual::Bool
condual::Dict{MOI.ConstraintIndex,Any}
end

Expand All @@ -42,27 +48,31 @@ xor_index(vi::VI) = VI(xor(vi.value, internal_xor_mask))
xor_index(ci::CI{F,S}) where {F,S} = CI{F,S}(xor(ci.value, internal_xor_mask))
xor_variables(f) = mapvariables(xor_index, f)

MockOptimizer(inner_model::MOI.ModelLike; needsallocateload=false, evalobjective=true) =
MockOptimizer(inner_model,
0,
Dict{MOI.VariableIndex,Int}(),
Dict{MOI.ConstraintIndex,Int}(),
needsallocateload,
true,
true,
(::MockOptimizer) -> begin end,
false,
false,
false,
MOI.Success,
0,
evalobjective,
NaN,
NaN,
MOI.UnknownResultStatus,
MOI.UnknownResultStatus,
Dict{MOI.VariableIndex,Float64}(),
Dict{MOI.ConstraintIndex,Any}())
function MockOptimizer(inner_model::MOI.ModelLike; needsallocateload=false,
eval_objective_value=true,
eval_variable_constraint_dual=true)
return MockOptimizer(inner_model,
0,
Dict{MOI.VariableIndex,Int}(),
Dict{MOI.ConstraintIndex,Int}(),
needsallocateload,
true,
true,
(::MockOptimizer) -> begin end,
false,
false,
false,
MOI.Success,
0,
eval_objective_value,
NaN,
NaN,
MOI.UnknownResultStatus,
MOI.UnknownResultStatus,
Dict{MOI.VariableIndex,Float64}(),
eval_variable_constraint_dual,
Dict{MOI.ConstraintIndex,Any}())
end

function MOI.addvariable!(mock::MockOptimizer)
if mock.canaddvar
Expand Down Expand Up @@ -154,17 +164,11 @@ MOI.get(b::MockOptimizer, IdxT::Type{<:MOI.Index}, name::String) = xor_index(MOI

MOI.get(mock::MockOptimizer, ::MOI.ResultCount) = mock.resultcount
MOI.get(mock::MockOptimizer, ::MOI.TerminationStatus) = mock.terminationstatus
# Gets the ObjectiveFunction attribute set to mock, i.e. the type of the scalar function is unknown
_getobjfunattr() = error("Objective Function not set")
_getobjfunattr(objfun::MOI.ObjectiveFunction, args...) = objfun
_getobjfunattr(::MOI.AbstractModelAttribute, args...) = _getobjfunattr(args...)
_getobjfunattr(mock::MockOptimizer) = _getobjfunattr(MOI.get(mock, MOI.ListOfModelAttributesSet())...)
function MOI.get(mock::MockOptimizer, ::MOI.ObjectiveValue)
if mock.evalobjective
f = MOI.get(mock, _getobjfunattr(mock))
evalvariables(vi -> MOI.get(mock, MOI.VariablePrimal(), vi), f)
function MOI.get(mock::MockOptimizer, attr::MOI.ObjectiveValue)
if mock.eval_objective_value
return get_fallback(mock, attr)
else
mock.objectivevalue
return mock.objectivevalue
end
end
MOI.get(mock::MockOptimizer, ::MOI.PrimalStatus) = mock.primalstatus
Expand All @@ -174,12 +178,20 @@ MOI.get(mock::MockOptimizer, ::MockModelAttribute) = mock.attribute
MOI.get(mock::MockOptimizer, attr::MOI.AbstractVariableAttribute, idx::MOI.VariableIndex) = MOI.get(mock.inner_model, attr, xor_index(idx))
MOI.get(mock::MockOptimizer, ::MockVariableAttribute, idx::MOI.VariableIndex) = mock.varattribute[xor_index(idx)]
MOI.get(mock::MockOptimizer, ::MOI.VariablePrimal, idx::MOI.VariableIndex) = mock.varprimal[xor_index(idx)]
function MOI.get(mock::MockOptimizer, ::MOI.ConstraintPrimal, idx::MOI.ConstraintIndex)
f = MOI.get(mock, MOI.ConstraintFunction(), idx)
evalvariables(vi -> MOI.get(mock, MOI.VariablePrimal(), vi), f)
function MOI.get(mock::MockOptimizer, attr::MOI.ConstraintPrimal,
idx::MOI.ConstraintIndex)
return get_fallback(mock, attr, idx)
end
MOI.get(mock::MockOptimizer, attr::MOI.AbstractConstraintAttribute, idx::MOI.ConstraintIndex) = MOI.get(mock.inner_model, attr, xor_index(idx))
MOI.get(mock::MockOptimizer, ::MOI.ConstraintDual, idx::MOI.ConstraintIndex) = mock.condual[xor_index(idx)]
function MOI.get(mock::MockOptimizer, attr::MOI.ConstraintDual,
idx::MOI.ConstraintIndex{F}) where F
if mock.eval_variable_constraint_dual &&
(F == MOI.SingleVariable || F == MOI.VectorOfVariables)
return get_fallback(mock, attr, idx)
else
return mock.condual[xor_index(idx)]
end
end
MOI.get(mock::MockOptimizer, ::MockConstraintAttribute, idx::MOI.ConstraintIndex) = mock.conattribute[xor_index(idx)]

MOI.supports(mock::MockOptimizer, ::MOI.ObjectiveBound) = true
Expand Down
4 changes: 4 additions & 0 deletions src/Utilities/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ function MOI.set!(model::AbstractModel, ::MOI.ObjectiveSense, sense::MOI.Optimiz
model.senseset = true
model.sense = sense
end
MOI.canget(model::AbstractModel, ::MOI.ObjectiveFunctionType) = true
function MOI.get(model::AbstractModel, ::MOI.ObjectiveFunctionType)
return MOI.typeof(model.objective)
end
MOI.canget(model::AbstractModel, ::MOI.ObjectiveFunction{T}) where T = model.objectiveset && typeof(model.objective) == T
function MOI.get(model::AbstractModel, ::MOI.ObjectiveFunction{T})::T where T
if typeof(model.objective) != T
Expand Down
Loading