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

Remove objective_constant #94

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/LinQuadOptInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,6 @@ macro LinQuadOptimizerBase(inner_model_type=Any)
constraint_to_name::Dict{LinQuadOptInterface.CI, String}
name_to_constraint::Union{Nothing, Dict{String, LinQuadOptInterface.CI}}

objective_constant::Float64

termination_status::MOI.TerminationStatusCode
primal_status::MOI.ResultStatusCode
dual_status::MOI.ResultStatusCode
Expand Down Expand Up @@ -361,8 +359,6 @@ function MOI.empty!(m::M, env = nothing) where M<:LinQuadOptimizer
m.constraint_to_name = Dict{CI, String}()
m.name_to_constraint = nothing

m.objective_constant = 0.0

m.termination_status = MathOptInterface.OPTIMIZE_NOT_CALLED
m.primal_status = MathOptInterface.NO_SOLUTION
m.dual_status = MathOptInterface.NO_SOLUTION
Expand Down
16 changes: 13 additions & 3 deletions src/mockoptimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mutable struct MockLinQuadModel # <: LinQuadOptInterface.LinQuadOptimizer
b::Vector{Float64}
c::Vector{Float64}
Qobj::Matrix{Float64}

objective_constant::Float64
range::Vector{Float64} # constraint UB for range

lb::Vector{Float64}
Expand All @@ -42,7 +42,7 @@ mutable struct MockLinQuadModel # <: LinQuadOptInterface.LinQuadOptimizer
quadratic_primal_solution::Vector{Float64}
quadratic_dual_solution::Vector{Float64}

function MockLinQuadModel(env::Nothing,str::String="defaultname")
function MockLinQuadModel(env::Nothing, str::String = "defaultname")
m = new()

m.sense = :minimize
Expand All @@ -52,6 +52,7 @@ mutable struct MockLinQuadModel # <: LinQuadOptInterface.LinQuadOptimizer
m.b = zeros(0)
m.c = zeros(0)
m.Qobj = zeros(0,0)
m.objective_constant = 0.0

m.range = zeros(0)

Expand Down Expand Up @@ -637,6 +638,15 @@ function LQOI.set_linear_objective!(instance::MockLinQuadOptimizer, colvec, coef
return
end

function LQOI.set_constant_objective!(model::MockLinQuadOptimizer, value)
model.inner.objective_constant = value
return
end

function LQOI.get_constant_objective(model::MockLinQuadOptimizer)
return model.inner.objective_constant
end

function LQOI.change_objective_sense!(instance::MockLinQuadOptimizer, symbol)
if symbol == :min
instance.inner.sense = :minimize
Expand Down Expand Up @@ -823,7 +833,7 @@ function get_objval(inner::MockLinQuadModel)
Q = Matrix(Symmetric(inner.Qobj, :U))
obj += (0.5) * x' * Q * x
end
return obj
return obj + inner.objective_constant
end

LQOI.get_objective_value(instance::MockLinQuadOptimizer) = get_objval(instance.inner)
Expand Down
12 changes: 1 addition & 11 deletions src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,7 @@ end

function MOI.get(model::LinQuadOptimizer, attr::MOI.ObjectiveValue)
if attr.resultindex == 1
# Note: we add m.objective_constant here to account for any constant
# term which was not passed to the solver itself (and which therefore
# would not be accounted for in `get_objective_value(m)`. We do *not*
# call `get_constant_objective(m)` because that would also pull any
# constants which were passed to the solver, resulting those constants
# being counted twice. This confusion will be alleviated when all LQOI
# solvers implement `get_constant_objective()` and
# `set_constant_objective!()` by actually passing the relevant constants
# to the solvers, at which point we can just get rid of
# m.objective_constant entirely.
return get_objective_value(model) + model.objective_constant
return get_objective_value(model)
else
error("Unable to access multiple objective values")
end
Expand Down
7 changes: 2 additions & 5 deletions src/solver_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,7 @@ value.
Solver interfaces that overload this behavior (e.g. to pass that constant
objective to the solver itself) must also overload `get_constant_objective(m)`.
"""
function set_constant_objective!(m::LinQuadOptimizer, value)
m.objective_constant = value
return
end
function set_constant_objective! end

"""
set_quadratic_objective!(m, I::Vector{Int}, J::Vector{Int}, V::Vector{Float64})::Nothing
Expand Down Expand Up @@ -390,7 +387,7 @@ function change_objective_sense! end

Return the constant (i.e. offset) component of the objective.
"""
get_constant_objective(m::LinQuadOptimizer) = m.objective_constant
function get_constant_objective end

"""
get_linear_objective!(m, x::Vector{Float64})
Expand Down