Skip to content
This repository was archived by the owner on Jun 14, 2020. It is now read-only.

Commit dce0136

Browse files
authored
Merge pull request #68 from JuliaOpt/bl/objectivefunctiontype
Implements ObjectiveFunctionType
2 parents db4ddde + 23e49bf commit dce0136

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/objective.jl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,21 @@ function MOI.supports(model::LinQuadOptimizer, ::MOI.ObjectiveFunction{F}) where
9999
return F in supported_objectives(model)
100100
end
101101

102+
function MOI.get(model::LinQuadOptimizer, ::MOI.ObjectiveFunctionType)
103+
if model.obj_type == SingleVariableObjective
104+
return MOI.SingleVariable
105+
elseif model.obj_type == AffineObjective
106+
return MOI.ScalarAffineFunction{Float64}
107+
else
108+
@assert model.obj_type == QuadraticObjective
109+
return MOI.ScalarQuadraticFunction{Float64}
110+
end
111+
end
112+
102113
function MOI.get(model::LinQuadOptimizer, ::MOI.ObjectiveFunction{MOI.SingleVariable})
103114
if model.obj_type != SingleVariableObjective
104115
if VERSION >= v"0.7-"
105-
throw(InexactError(:convert, SingleVariableObjective, model.obj_type))
116+
throw(InexactError(:convert, SingleVariableObjective, model.obj_type))
106117
else
107118
throw(InexactError())
108119
end
@@ -140,7 +151,7 @@ function MOI.get(model::LinQuadOptimizer, ::MOI.ObjectiveFunction{Quad})
140151
if model.obj_type == QuadraticObjective
141152
Q = get_quadratic_terms_objective(model)
142153
rows = rowvals(Q)
143-
coefficients = nonzeros(Q)
154+
coefficients = nonzeros(Q)
144155
sizehint!(quadratic_terms, length(coefficients))
145156
for (column, variable) in enumerate(model.variable_references)
146157
for j in nzrange(Q, column)

test/runtests.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,16 @@ end
364364
check_row(model, c, f4, s)
365365
end
366366
end
367+
368+
@testset "Issue #67" begin
369+
model = LQOI.MockLinQuadOptimizer()
370+
function objective_type_test(f::MOI.AbstractScalarFunction)
371+
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
372+
@test MOI.get(model, MOI.ObjectiveFunctionType()) == typeof(f)
373+
end
374+
x = MOI.add_variable(model)
375+
f = MOI.SingleVariable(x)
376+
objective_type_test(f)
377+
objective_type_test(convert(MOI.ScalarAffineFunction{Float64}, f))
378+
objective_type_test(convert(MOI.ScalarQuadraticFunction{Float64}, f))
379+
end

0 commit comments

Comments
 (0)