-
-
Notifications
You must be signed in to change notification settings - Fork 390
/
objective.jl
80 lines (67 loc) · 2.8 KB
/
objective.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#############################################################################
# JuMP
# An algebraic modeling language for Julia
# See http://github.com/JuliaOpt/JuMP.jl
#############################################################################
# This file contains objective-related functions
"""
objective_bound(model::Model)
Return the best known bound on the optimal objective value after a call to
`optimize!model)`.
"""
objective_bound(model::Model) = MOI.get(model, MOI.ObjectiveBound())
"""
objective_value(model::Model)
Return the objective value after a call to `optimize!model)`.
"""
objective_value(model::Model) = MOI.get(model, MOI.ObjectiveValue())
"""
objective_sense(model::Model)::MathOptInterface.OptimizationSense
Return the objective sense.
"""
function objective_sense(model::Model)
return MOI.get(model, MOI.ObjectiveSense())
end
"""
set_objective_sense(model::Model, sense::MathOptInterface.OptimizationSense)
Sets the objective sense of the model to the given sense. See
[`set_objective_function`](@ref) to set the objective function. These are
low-level functions; the recommended way to set the objective is with the
[`@objective`](@ref) macro.
"""
function set_objective_sense(model::Model, sense::MOI.OptimizationSense)
MOI.set(model, MOI.ObjectiveSense(), sense)
end
"""
set_objective_function(model::Model,
func::Union{AbstractJuMPScalar,
MathOptInterface.AbstractScalarFunction})
Sets the objective function of the model to the given function. See
[`set_objective_sense`](@ref) to set the objective sense. These are low-level
functions; the recommended way to set the objective is with the
[`@objective`](@ref) macro.
"""
function set_objective_function end
function set_objective_function(model::Model, func::MOI.AbstractScalarFunction)
attr = MOI.ObjectiveFunction{typeof(func)}()
if !MOI.supports(model.moi_backend, attr)
error("The solver does not support an objective function of type ",
typeof(func), ".")
end
MOI.set(model, attr, func)
# Keeping the explicit `return` is helpful for type inference because we
# don't know what `MOI.set` will return.
return
end
function set_objective_function(model::Model, func::AbstractJuMPScalar)
set_objective_function(model, moi_function(func))
end
function set_objective(model::Model, sense::MOI.OptimizationSense,
func::AbstractJuMPScalar)
set_objective_sense(model, sense)
set_objective_function(model, func)
end