Skip to content

Commit ecf99d3

Browse files
committed
Fix objective_function with vector objectives
1 parent db9c4c8 commit ecf99d3

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/objective.jl

+21-10
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,10 @@ function set_objective_sense(model::Model, sense::MOI.OptimizationSense)
8282
end
8383

8484
"""
85-
set_objective_function(
86-
model::Model,
87-
func::Union{AbstractJuMPScalar, MathOptInterface.AbstractScalarFunction})
85+
set_objective_function(model::Model, func::MOI.AbstractFunction)
86+
set_objective_function(model::Model, func::AbstractJuMPScalar)
87+
set_objective_function(model::Model, func::Real)
88+
set_objective_function(model::Model, func::Vector{<:AbstractJuMPScalar})
8889
8990
Sets the objective function of the model to the given function. See
9091
[`set_objective_sense`](@ref) to set the objective sense. These are low-level
@@ -178,10 +179,13 @@ function objective_function_type(model::Model)
178179
end
179180

180181
"""
181-
objective_function(model::Model,
182-
T::Type{<:AbstractJuMPScalar}=objective_function_type(model))
182+
objective_function(
183+
model::Model,
184+
T::Type = objective_function_type(model),
185+
)
183186
184187
Return an object of type `T` representing the objective function.
188+
185189
Error if the objective is not convertible to type `T`.
186190
187191
## Examples
@@ -223,14 +227,21 @@ ERROR: InexactError: convert(MathOptInterface.VariableIndex, MathOptInterface.Sc
223227
"""
224228
function objective_function(
225229
model::Model,
226-
FunType::Type{<:AbstractJuMPScalar} = objective_function_type(model),
227-
)
228-
MOIFunType = moi_function_type(FunType)
229-
func =
230-
MOI.get(backend(model), MOI.ObjectiveFunction{MOIFunType}())::MOIFunType
230+
::Type{F},
231+
) where {F<:MOI.AbstractFunction}
232+
func = MOI.get(backend(model), MOI.ObjectiveFunction{F}())::F
231233
return jump_function(model, func)
232234
end
233235

236+
function objective_function(model::Model, ::Type{T}) where {T}
237+
return objective_function(model, moi_function_type(T))
238+
end
239+
240+
function objective_function(model::Model)
241+
F = MOI.get(backend(model), MOI.ObjectiveFunctionType())
242+
return objective_function(model, F)
243+
end
244+
234245
"""
235246
set_objective_coefficient(model::Model, variable::VariableRef, coefficient::Real)
236247

0 commit comments

Comments
 (0)