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 1 commit
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
5 changes: 0 additions & 5 deletions src/LinQuadOptInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,6 @@ macro LinQuadOptimizerBase(inner_model_type=Any)
constraint_names::Dict{LinQuadOptInterface.CI, String}
constraint_names_rev::Dict{String, Set{LinQuadOptInterface.CI}}

objective_constant::Float64

termination_status::MOI.TerminationStatusCode
primal_status::MOI.ResultStatusCode
dual_status::MOI.ResultStatusCode
Expand Down Expand Up @@ -301,7 +299,6 @@ function MOI.is_empty(m::LinQuadOptimizer)
ret = ret && isempty(m.qconstraint_dual_solution)
ret = ret && isempty(m.constraint_names)
ret = ret && isempty(m.constraint_names_rev)
ret = ret && m.objective_constant == 0.0
ret = ret && m.termination_status == MOI.OPTIMIZE_NOT_CALLED
ret = ret && m.primal_status == MOI.NO_SOLUTION
ret = ret && m.dual_status == MOI.NO_SOLUTION
Expand Down Expand Up @@ -342,8 +339,6 @@ function MOI.empty!(m::M, env = nothing) where M<:LinQuadOptimizer
m.constraint_names = Dict{CI, String}()
m.constraint_names_rev = Dict{String, Set{CI}}()

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
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ end

@testset "Linear tests" begin
config = MOIT.TestConfig(solve=false)
MOIT.contlineartest(solver, config)
MOIT.contlineartest(MOIU.UniversalFallback(solver), config)
config = MOIT.TestConfig()
include("contlinear.jl")
set_linear1test_solutions!(solver)
Expand Down