Skip to content

Commit

Permalink
Merge 61bba93 into b6f7de8
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Oct 29, 2018
2 parents b6f7de8 + 61bba93 commit 8c722ca
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 79 deletions.
55 changes: 1 addition & 54 deletions src/JuMP.jl
Expand Up @@ -317,60 +317,6 @@ function num_nl_constraints(model::Model)
return model.nlp_data !== nothing ? length(model.nlp_data.nlconstr) : 0
end

"""
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::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(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)
end

# TODO(IainNZ): Document these too.
object_dictionary(model::Model) = model.obj_dict
termination_status(model::Model) = MOI.get(model, MOI.TerminationStatus())
Expand Down Expand Up @@ -416,6 +362,7 @@ end

include("constraints.jl")
include("variables.jl")
include("objective.jl")

Base.zero(::Type{V}) where V<:AbstractVariableRef = zero(GenericAffExpr{Float64, V})
Base.zero(v::AbstractVariableRef) = zero(typeof(v))
Expand Down
8 changes: 0 additions & 8 deletions src/aff_expr.jl
Expand Up @@ -295,14 +295,6 @@ function MOI.VectorAffineFunction(affs::Vector{AffExpr})
end
moi_function(a::Vector{<:GenericAffExpr}) = MOI.VectorAffineFunction(a)

function set_objective(model::Model, sense::MOI.OptimizationSense, a::AffExpr)
set_objective_sense(model, sense)
set_objective_function(model, MOI.ScalarAffineFunction(a))
# Keeping the explicit `return` is helpful for type inference because we
# don't know what `MOI.set` will return.
return
end

"""
objective_function(m::Model, ::Type{AffExpr})
Expand Down
80 changes: 80 additions & 0 deletions src/objective.jl
@@ -0,0 +1,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
8 changes: 0 additions & 8 deletions src/quad_expr.jl
Expand Up @@ -221,14 +221,6 @@ function jump_function(model::AbstractModel, aff::MOI.ScalarQuadraticFunction)
return QuadExpr(model, aff)
end

function set_objective(model::Model, sense::MOI.OptimizationSense, a::QuadExpr)
set_objective_sense(model, sense)
set_objective_function(model, MOI.ScalarQuadraticFunction(a))
# Keeping the explicit `return` is helpful for type inference because we
# don't know what `MOI.set` will return.
return
end

"""
objective_function(m::Model, ::Type{QuadExpr})
Expand Down
9 changes: 0 additions & 9 deletions src/variables.jl
Expand Up @@ -244,15 +244,6 @@ function jump_function(model::AbstractModel, variable::MOI.SingleVariable)
return VariableRef(model, variable)
end

function set_objective(model::Model, sense::MOI.OptimizationSense,
x::VariableRef)
set_objective_sense(model, sense)
set_objective_function(model, MOI.SingleVariable(x))
# Keeping the explicit `return` is helpful for type inference because we
# don't know what `MOI.set` will return.
return
end

"""
objective_function(m::Model, ::Type{VariableRef})
Expand Down

0 comments on commit 8c722ca

Please sign in to comment.