From 443315afe0c08a3bcdf0db1170cbf9c68384d9fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 29 Oct 2018 13:51:58 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Move=20objective-relat?= =?UTF-8?q?ed=20code=20into=20objective.jl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/JuMP.jl | 55 +------------------------------------- src/aff_expr.jl | 8 ------ src/objective.jl | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ src/quad_expr.jl | 8 ------ src/variables.jl | 9 ------- 5 files changed, 70 insertions(+), 79 deletions(-) create mode 100644 src/objective.jl diff --git a/src/JuMP.jl b/src/JuMP.jl index fe5cc045524..b42c88aba16 100644 --- a/src/JuMP.jl +++ b/src/JuMP.jl @@ -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()) @@ -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)) diff --git a/src/aff_expr.jl b/src/aff_expr.jl index bdc0a933dc1..19a091481f3 100644 --- a/src/aff_expr.jl +++ b/src/aff_expr.jl @@ -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}) diff --git a/src/objective.jl b/src/objective.jl new file mode 100644 index 00000000000..656ce3d06b1 --- /dev/null +++ b/src/objective.jl @@ -0,0 +1,69 @@ +""" + 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 diff --git a/src/quad_expr.jl b/src/quad_expr.jl index 25119ccc6ef..bb5ae4b034a 100644 --- a/src/quad_expr.jl +++ b/src/quad_expr.jl @@ -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}) diff --git a/src/variables.jl b/src/variables.jl index 979fa0ce7bb..9c1de983be0 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -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}) From 61bba9374bd490185f5ec19fa02923ffc99b1074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 29 Oct 2018 15:16:55 +0100 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=84=20Add=20LICENSE=20and=20short?= =?UTF-8?q?=20summary?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/objective.jl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/objective.jl b/src/objective.jl index 656ce3d06b1..c438162d219 100644 --- a/src/objective.jl +++ b/src/objective.jl @@ -1,3 +1,14 @@ +# 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)