Skip to content
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
12 changes: 9 additions & 3 deletions src/Utilities/results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ function get_fallback(
if status == MOI.NO_SOLUTION
throw(MOI.GetAttributeNotAllowed(attr, "No dual solution is available"))
end
F = MOI.get(model, MOI.ObjectiveFunctionType())
if !(F <: Union{MOI.VariableIndex,MOI.ScalarAffineFunction})
err = MOI.GetAttributeNotAllowed(
attr,
"Cannot get the dual objective with an objective function of type `$F`",
)
throw(err)
end
value = zero(T) # sum will not work if there are zero constraints
for (F, S) in MOI.get(model, MOI.ListOfConstraintTypesPresent())
value += _dual_objective_value(model, F, S, T, attr.result_index)::T
Expand All @@ -83,9 +91,7 @@ function get_fallback(
end
if !is_ray(status)
# The objective constant should not be present in rays
F = MOI.get(model, MOI.ObjectiveFunctionType())
f = MOI.get(model, MOI.ObjectiveFunction{F}())
value += MOI.constant(f, T)
value += MOI.constant(MOI.get(model, MOI.ObjectiveFunction{F}()), T)
end
return value::T
end
Expand Down
3 changes: 2 additions & 1 deletion test/Bridges/Objective/test_QuadratizeBridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ function test_solve_result_index()
(MOI.VariableIndex, MOI.GreaterThan{Float64}) => [1.0],
),
)
MOI.Test.test_solve_result_index(model, MOI.Test.Config())
config = MOI.Test.Config(; exclude = Any[MOI.DualObjectiveValue])
MOI.Test.test_solve_result_index(model, config)
return
end

Expand Down
24 changes: 24 additions & 0 deletions test/Utilities/test_results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,30 @@ function test_get_fallback_DualObjectiveValue()
return
end

function test_get_fallback_DualObjectiveValue_Quadratic()
T = Float64
model = MOI.Utilities.MockOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{T}()),
T,
)
x = MOI.add_variable(model)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
f = 1.0 * x * x
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.set(model, MOI.ResultCount(), 1)
MOI.set(model, MOI.PrimalStatus(), MOI.FEASIBLE_POINT)
MOI.set(model, MOI.DualStatus(), MOI.FEASIBLE_POINT)
MOI.set(model, MOI.VariablePrimal(), x, 0.0)
@test_throws(
MOI.GetAttributeNotAllowed(
MOI.DualObjectiveValue(),
"Cannot get the dual objective with an objective function of type `$(typeof(f))`",
),
MOI.Utilities.get_fallback(model, MOI.DualObjectiveValue(), T),
)
return
end

end # module TestResults

TestResults.runtests()
Loading