From 08899dde67a575d72a7a11ecd9245f8aa27351e3 Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 1 May 2018 17:01:54 +1200 Subject: [PATCH 01/10] Explore renaming interface --- src/constraints/scalaraffine.jl | 18 +++---- src/constraints/singlevariable.jl | 20 +++++--- src/constraints/vectoraffine.jl | 10 ++-- src/constraints/vectorofvariables.jl | 6 +-- src/solver_interface.jl | 76 +++++++++++++++------------- 5 files changed, 71 insertions(+), 59 deletions(-) diff --git a/src/constraints/scalaraffine.jl b/src/constraints/scalaraffine.jl index ce6dd81..6eaf1f6 100644 --- a/src/constraints/scalaraffine.jl +++ b/src/constraints/scalaraffine.jl @@ -15,7 +15,7 @@ function MOI.addconstraint!(m::LinQuadOptimizer, func::Linear, set::T) where T < m.last_constraint_reference += 1 ref = LCI{T}(m.last_constraint_reference) dict = constrdict(m, ref) - dict[ref] = lqs_getnumrows(m) + dict[ref] = get_number_linear_constraints(m) push!(m.constraint_primal_solution, NaN) push!(m.constraint_dual_solution, NaN) push!(m.constraint_constant, func.constant) @@ -28,14 +28,14 @@ end function addlinearconstraint!(m::LinQuadOptimizer, func::Linear, set::IV) addlinearconstraint!(m, func, lqs_char(m,set), set.lower) - lqs_chgrngval!(m, [lqs_getnumrows(m)], [set.upper - set.lower]) + lqs_chgrngval!(m, [get_number_linear_constraints(m)], [set.upper - set.lower]) end function addlinearconstraint!(m::LinQuadOptimizer, func::Linear, sense::Cchar, rhs) if abs(func.constant) > eps(Float64) warn("Constant in scalar function moved into set.") end - lqs_addrows!(m, [1], getcol.(m, func.variables), func.coefficients, [sense], [rhs - func.constant]) + add_linear_constraints!(m, [1], getcol.(m, func.variables), func.coefficients, [sense], [rhs - func.constant]) end #= @@ -47,7 +47,7 @@ function MOI.addconstraints!(m::LinQuadOptimizer, func::Vector{Linear}, set::Vec cfunc = MOIU.canonical.(func) @assert length(cfunc) == length(set) - numrows = lqs_getnumrows(m) + numrows = get_number_linear_constraints(m) addlinearconstraints!(m, cfunc, set) crefs = Vector{LCI{S}}(length(cfunc)) for i in 1:length(cfunc) @@ -68,9 +68,9 @@ function addlinearconstraints!(m::LinQuadOptimizer, func::Vector{Linear}, set::V end function addlinearconstraints!(m::LinQuadOptimizer, func::Vector{Linear}, set::Vector{IV}) - numrows = lqs_getnumrows(m) + numrows = get_number_linear_constraints(m) addlinearconstraints!(m, func, lqs_char.(m,set), [s.lower for s in set]) - numrows2 = lqs_getnumrows(m) + numrows2 = get_number_linear_constraints(m) lqs_chgrngval!(m, collect(numrows+1:numrows2), [s.upper - s.lower for s in set]) end @@ -96,7 +96,7 @@ function addlinearconstraints!(m::LinQuadOptimizer, func::Vector{Linear}, sense: cnt += 1 end end - lqs_addrows!(m, rowbegins, column_indices, nnz_vals, sense, rhs) + add_linear_constraints!(m, rowbegins, column_indices, nnz_vals, sense, rhs) end #= @@ -105,7 +105,7 @@ end MOI.canget(m::LinQuadOptimizer, ::MOI.ConstraintSet, ::Type{LCI{S}}) where S <: Union{LE, GE, EQ} = true function MOI.get(m::LinQuadOptimizer, ::MOI.ConstraintSet, c::LCI{S}) where S <: Union{LE, GE, EQ} - rhs = lqs_getrhs(m, m[c]) + rhs = get_rhs(m, m[c]) S(rhs+m.constraint_constant[m[c]]) end @@ -116,7 +116,7 @@ end MOI.canget(m::LinQuadOptimizer, ::MOI.ConstraintFunction, ::Type{<:LCI{<: LinSets}}) = true function MOI.get(m::LinQuadOptimizer, ::MOI.ConstraintFunction, c::LCI{<: LinSets}) # TODO more efficiently - colidx, coefs = lqs_getrows(m, m[c]) + colidx, coefs = get_linear_constraint(m, m[c]) Linear(m.variable_references[colidx+1], coefs, -m.constraint_constant[m[c]]) end diff --git a/src/constraints/singlevariable.jl b/src/constraints/singlevariable.jl index 8d50b99..d1fc35e 100644 --- a/src/constraints/singlevariable.jl +++ b/src/constraints/singlevariable.jl @@ -61,17 +61,23 @@ end # constraint set MOI.canget(m::LinQuadOptimizer, ::MOI.ConstraintSet, ::Type{SVCI{S}}) where S <: LinSets = true function MOI.get(m::LinQuadOptimizer, ::MOI.ConstraintSet, c::SVCI{LE}) - MOI.LessThan{Float64}(lqs_getub(m, getcol(m, m[c]))) + MOI.LessThan{Float64}( + get_variable_upperbound(m, getcol(m, m[c])) + ) end function MOI.get(m::LinQuadOptimizer, ::MOI.ConstraintSet, c::SVCI{GE}) - MOI.GreaterThan{Float64}(lqs_getlb(m, getcol(m, m[c]))) + MOI.GreaterThan{Float64}( + get_variable_lowerbound(m, getcol(m, m[c])) + ) end function MOI.get(m::LinQuadOptimizer, ::MOI.ConstraintSet, c::SVCI{EQ}) - MOI.EqualTo{Float64}(lqs_getlb(m, getcol(m, m[c]))) + MOI.EqualTo{Float64}( + get_variable_lowerbound(m, getcol(m, m[c])) + ) end function MOI.get(m::LinQuadOptimizer, ::MOI.ConstraintSet, c::SVCI{IV}) - lb = lqs_getlb(m, getcol(m, m[c])) - ub = lqs_getub(m, getcol(m, m[c])) + lb = get_variable_lowerbound(m, getcol(m, m[c])) + ub = get_variable_upperbound(m, getcol(m, m[c])) return MOI.Interval{Float64}(lb, ub) end @@ -100,8 +106,8 @@ function MOI.addconstraint!(m::LinQuadOptimizer, v::SinVar, set::MOI.ZeroOne) m.last_constraint_reference += 1 ref = SVCI{MOI.ZeroOne}(m.last_constraint_reference) dict = constrdict(m, ref) - ub = lqs_getub(m, getcol(m, v)) - lb = lqs_getlb(m, getcol(m, v)) + ub = get_variable_upperbound(m, getcol(m, v)) + lb = get_variable_lowerbound(m, getcol(m, v)) dict[ref] = (v.variable, lb, ub) lqs_chgctype!(m, [getcol(m, v)], [lqs_char(m, set)]) setvariablebound!(m, getcol(m, v), 1.0, lqs_char(m, Val{:Upperbound}())) diff --git a/src/constraints/vectoraffine.jl b/src/constraints/vectoraffine.jl index ecaabe0..6dfe551 100644 --- a/src/constraints/vectoraffine.jl +++ b/src/constraints/vectoraffine.jl @@ -8,9 +8,9 @@ constrdict(m::LinQuadOptimizer, ::VLCI{MOI.Zeros}) = cmap(m).zeros function MOI.addconstraint!(m::LinQuadOptimizer, func::VecLin, set::S) where S <: VecLinSets @assert MOI.dimension(set) == length(func.constant) - nrows = lqs_getnumrows(m) + nrows = get_number_linear_constraints(m) addlinearconstraint!(m, func, lqs_char(m,set)) - nrows2 = lqs_getnumrows(m) + nrows2 = get_number_linear_constraints(m) m.last_constraint_reference += 1 ref = VLCI{S}(m.last_constraint_reference) @@ -44,7 +44,7 @@ function addlinearconstraint!(m::LinQuadOptimizer, func::VecLin, sense::Cchar) rowbegins[cnt] = i end end - lqs_addrows!(m, rowbegins, cols, vals, fill(sense, length(rows)), -func.constant) + add_linear_constraints!(m, rowbegins, cols, vals, fill(sense, length(rows)), -func.constant) end MOI.canmodifyconstraint(m::LinQuadOptimizer, ::VLCI{<: VecLinSets}, ::Type{MOI.VectorConstantChange{Float64}}) = true @@ -76,11 +76,11 @@ function MOI.get(m::LinQuadOptimizer, ::MOI.ConstraintFunction, c::VLCI{<: VecLi n = length(ctrs) out = MOI.VectorAffineFunction(Int[],VarInd[],Float64[],Float64[]) for i in 1:n - rhs = lqs_getrhs(m, ctrs[i]) + rhs = get_rhs(m, ctrs[i]) push!(out.constant, -rhs) # TODO more efficiently - colidx, coefs = lqs_getrows(m, ctrs[i]) + colidx, coefs = get_linear_constraint(m, ctrs[i]) append!(out.variables, m.variable_references[colidx+1]) append!(out.coefficients, coefs) append!(out.outputindex, i*ones(Int,length(coefs))) diff --git a/src/constraints/vectorofvariables.jl b/src/constraints/vectorofvariables.jl index 6f1b757..7ad54d3 100644 --- a/src/constraints/vectorofvariables.jl +++ b/src/constraints/vectorofvariables.jl @@ -19,9 +19,9 @@ function MOI.addconstraint!(m::LinQuadOptimizer, func::VecVar, set::S) where S < @assert length(func.variables) == MOI.dimension(set) m.last_constraint_reference += 1 ref = VVCI{S}(m.last_constraint_reference) - rows = lqs_getnumrows(m) + rows = get_number_linear_constraints(m) n = MOI.dimension(set) - lqs_addrows!(m, collect(1:n), getcol.(m, func.variables), ones(n), fill(lqs_char(m, set),n), zeros(n)) + add_linear_constraints!(m, collect(1:n), getcol.(m, func.variables), ones(n), fill(lqs_char(m, set),n), zeros(n)) dict = constrdict(m, ref) dict[ref] = collect(rows+1:rows+n) append!(m.constraint_primal_solution, fill(NaN,n)) @@ -50,7 +50,7 @@ function MOI.get(m::LinQuadOptimizer, ::MOI.ConstraintFunction, c::VVCI{<: VecLi out = VarInd[] sizehint!(out, length(refs)) for ref in refs - colidx, coefs = lqs_getrows(m, ref) + colidx, coefs = get_linear_constraint(m, ref) if length(colidx) != 1 error("Unexpected constraint") end diff --git a/src/solver_interface.jl b/src/solver_interface.jl index 8e0f98c..2b7caed 100644 --- a/src/solver_interface.jl +++ b/src/solver_interface.jl @@ -78,23 +78,23 @@ Initializes a model given a model type `M` and an `env` that might be a `nothing for some solvers. """ function LinQuadModel end - -""" - lqs_setparam!(m, name, val)::Void - -Set the parameter `name` to `val` for the model `m`. -""" -function lqs_setparam!(m::LinQuadOptimizer, name, val) end - -""" - lqs_setlogfile!(m, path::String)::Void - -Set the log file to `path` for the model `m`. -""" -function lqs_setlogfile!(m::LinQuadOptimizer, path) end - -# TODO(@joaquimg): what is this? -function lqs_getprobtype(m::LinQuadOptimizer) end +# +# """ +# lqs_setparam!(m, name, val)::Void +# +# Set the parameter `name` to `val` for the model `m`. +# """ +# function lqs_setparam!(m::LinQuadOptimizer, name, val) end +# +# """ +# lqs_setlogfile!(m, path::String)::Void +# +# Set the log file to `path` for the model `m`. +# """ +# function lqs_setlogfile!(m::LinQuadOptimizer, path) end +# +# # TODO(@joaquimg): what is this? +# function lqs_getprobtype(m::LinQuadOptimizer) end """ lqs_supported_constraints(m)::Vector{ @@ -121,57 +121,63 @@ lqs_supported_objectives(m::LinQuadOptimizer) = [] function lqs_chgbds!(m::LinQuadOptimizer, colvec, valvec, sensevec) end """ - lqs_getlb(m, col::Int)::Float64 + get_variable_lowerbound(m, col::Int)::Float64 Get the lower bound of the variable in 1-indexed column `col` of the model `m`. """ -function lqs_getlb(m::LinQuadOptimizer, col) end +function get_variable_lowerbound end +@deprecate lqs_getlb get_variable_lowerbound """ - lqs_getub(m, col::Int)::Float64 + get_variable_upperbound(m, col::Int)::Float64 Get the upper bound of the variable in 1-indexed column `col` of the model `m`. """ -function lqs_getub(m::LinQuadOptimizer, col) end +function get_variable_upperbound end +@deprecate lqs_getub get_variable_upperbound """ - lqs_getnumrows(m)::Int + get_number_linear_constraints(m)::Int Get the number of linear constraints in the model `m`. """ -function lqs_getnumrows(m::LinQuadOptimizer) end +function get_number_linear_constraints(m::LinQuadOptimizer) end +@deprecate lqs_getnumrows get_number_linear_constraints """ - lqs_addrows(m, rows::Vector{Int}, cols::Vector{Int}, coefs::Vector{Float64}, - sense::Vector{Symbol}, rhs::Vector{Float64})::Void + add_linear_constraints!(m, rows::Vector{Int}, cols::Vector{Int}, + coefs::Vector{Float64}, + sense::Vector{Cchar}, rhs::Vector{Float64})::Void Adds linear constraints of the form `Ax (sense) rhs` to the model `m`. The A matrix is given in triplet form `A[rows[i], cols[i]] = coef[i]` for all `i`, and `length(rows) == length(cols) == length(coefs)`. -The `sense` is one of `:RANGE`, `:LOWER`, `:UPPER`, `:EQUALITY`. +The `sense` is given by `lqs_char(m, set)`. -Ranged constraints (sense=`:RANGE`) require a call to `lqs_chgrngval!`. +Ranged constraints (`set=MOI.Interval`) require a call to `lqs_chgrngval!`. """ -function lqs_addrows!(m::LinQuadOptimizer, rows, cols, coefs, sense, rhs) end +function add_linear_constraints!(m::LinQuadOptimizer, rows, cols, coefs, sense, rhs) end +@deprecate lqs_addrows! add_linear_constraints! """ - lqs_getrhs(m, row::Int)::Float64 + get_rhs(m, row::Int)::Float64 Get the right-hand side of the linear constraint in the 1-indexed row `row` in the model `m`. """ -function lqs_getrhs(m::LinQuadOptimizer, row) end +function get_rhs(m::LinQuadOptimizer, row) end +@deprecate lqs_getrhs get_rhs -# TODO(@joaquim): I think this is really lqs_getrow? (singluar) """ - lqs_getrows(m, row::Int)::Tuple{Vector{Int}, Vector{Float64}} + get_linear_constraint(m, row::Int)::Tuple{Vector{Int}, Vector{Float64}} Get the linear component of the constraint in the 1-indexed row `row` in the model `m`. Returns a tuple of `(cols, vals)`. """ -function lqs_getrows(m::LinQuadOptimizer, row) end +function get_linear_constraint(m::LinQuadOptimizer, row) end +@deprecate lqs_getrows get_linear_constraint """ lqs_getcoef(m, row::Int, col::Int)::Float64 @@ -264,7 +270,7 @@ function lqs_getnumqconstrs(m::LinQuadOptimizer) end sense::Symbol, I::Vector{Int}, J::Vector{Int}, V::Vector{Float64})::Void Add a quadratic constraint `a'x + 0.5 x' Q x`. -See `lqs_addrows!` for information of linear component. +See `add_linear_constraints!` for information of linear component. Arguments `(I,J,V)` given in triplet form for the Q matrix in `0.5 x' Q x`. """ function lqs_addqconstr!(m::LinQuadOptimizer, cols, coefs, rhs, sense, I, J, V) end @@ -276,7 +282,7 @@ function lqs_addqconstr!(m::LinQuadOptimizer, cols, coefs, rhs, sense, I, J, V) A range constraint `l <= a'x <= u` is added as the linear constraint `a'x :RANGED l`, then this function is called to set `u - l`, the range value. -See `lqs_addrows!` for more. +See `add_linear_constraints!` for more. """ function lqs_chgrngval!(m::LinQuadOptimizer, rows, vals) end# later From 86e0f32cad9a95f898eec03dd66404380003256e Mon Sep 17 00:00:00 2001 From: odow Date: Mon, 7 May 2018 19:42:12 +1200 Subject: [PATCH 02/10] More Renamings --- src/constraints/scalaraffine.jl | 8 +- src/constraints/vectoraffine.jl | 2 +- src/objective.jl | 18 ++--- src/solve.jl | 24 +++--- src/solver_interface.jl | 136 ++++++++++++++++++-------------- src/variables.jl | 8 +- 6 files changed, 108 insertions(+), 88 deletions(-) diff --git a/src/constraints/scalaraffine.jl b/src/constraints/scalaraffine.jl index 6eaf1f6..1e25395 100644 --- a/src/constraints/scalaraffine.jl +++ b/src/constraints/scalaraffine.jl @@ -127,7 +127,7 @@ end MOI.canmodifyconstraint(m::LinQuadOptimizer, c::LCI{<: LinSets}, ::Type{MOI.ScalarCoefficientChange{Float64}}) = true function MOI.modifyconstraint!(m::LinQuadOptimizer, c::LCI{<: LinSets}, chg::MOI.ScalarCoefficientChange{Float64}) col = m.variable_mapping[chg.variable] - lqs_chgcoef!(m, m[c], col, chg.new_coefficient) + change_coefficient!(m, m[c], col, chg.new_coefficient) end #= @@ -137,7 +137,7 @@ end MOI.canmodifyconstraint(m::LinQuadOptimizer, c::LCI{S}, ::Type{S}) where S <: Union{LE, GE, EQ} = true function MOI.modifyconstraint!(m::LinQuadOptimizer, c::LCI{S}, newset::S) where S <: Union{LE, GE, EQ} # the column 0 (or -1 in 0-index) is the rhs. - lqs_chgcoef!(m, m[c], 0, _getrhs(newset)) + change_coefficient!(m, m[c], 0, _getrhs(newset)) end MOI.canmodifyconstraint(m::LinQuadOptimizer, c::LCI{IV}, ::Type{IV}) = true @@ -146,7 +146,7 @@ function MOI.modifyconstraint!(m::LinQuadOptimizer, c::LCI{IV}, set::IV) # a range constraint has the RHS value of the lower limit of the range, and # a rngval equal to upper-lower. row = m[c] - lqs_chgcoef!(m, row, 0, set.lower) + change_coefficient!(m, row, 0, set.lower) lqs_chgrngval!(m, [row], [set.upper - set.lower]) end @@ -159,7 +159,7 @@ function MOI.delete!(m::LinQuadOptimizer, c::LCI{<: LinSets}) deleteconstraintname!(m, c) dict = constrdict(m, c) row = dict[c] - lqs_delrows!(m, row, row) + delete_linear_constraints!(m, row, row) deleteat!(m.constraint_primal_solution, row) deleteat!(m.constraint_dual_solution, row) deleteat!(m.constraint_constant, row) diff --git a/src/constraints/vectoraffine.jl b/src/constraints/vectoraffine.jl index 6dfe551..9e6c93c 100644 --- a/src/constraints/vectoraffine.jl +++ b/src/constraints/vectoraffine.jl @@ -51,7 +51,7 @@ MOI.canmodifyconstraint(m::LinQuadOptimizer, ::VLCI{<: VecLinSets}, ::Type{MOI.V function MOI.modifyconstraint!(m::LinQuadOptimizer, ref::VLCI{<: VecLinSets}, chg::MOI.VectorConstantChange{Float64}) @assert length(chg.new_constant) == length(m[ref]) for (r, v) in zip(m[ref], chg.new_constant) - lqs_chgcoef!(m, r, 0, -v) + change_coefficient!(m, r, 0, -v) m.constraint_constant[r] = v end end diff --git a/src/objective.jl b/src/objective.jl index b6f5368..e2a612c 100644 --- a/src/objective.jl +++ b/src/objective.jl @@ -8,14 +8,14 @@ MOI.get(m::LinQuadOptimizer,::MOI.ObjectiveSense) = m.obj_sense MOI.canset(::LinQuadOptimizer, ::MOI.ObjectiveSense) = true function MOI.set!(m::LinQuadOptimizer, ::MOI.ObjectiveSense, sense::MOI.OptimizationSense) if sense == MOI.MinSense - lqs_chgobjsen!(m, :min) + change_objectivesense!(m, :min) m.obj_sense = MOI.MinSense elseif sense == MOI.MaxSense - lqs_chgobjsen!(m, :max) + change_objectivesense!(m, :max) m.obj_sense = MOI.MaxSense elseif sense == MOI.FeasibilitySense # we set the objective sense to :min, and the objective to 0.0 - lqs_chgobjsen!(m, :min) + change_objectivesense!(m, :min) unsafe_set!(m, MOI.ObjectiveFunction{Linear}(), MOI.ScalarAffineFunction(VarInd[],Float64[],0.0)) m.obj_is_quad = false m.obj_sense = MOI.FeasibilitySense @@ -51,16 +51,16 @@ function unsafe_set!(m::LinQuadOptimizer, ::MOI.ObjectiveFunction{F}, objf::Line # previous objective was quadratic... m.obj_is_quad = false # zero quadratic part - lqs_copyquad!(m, Int[], Int[], Float64[]) + set_quadratic_objective!(m, Int[], Int[], Float64[]) end - lqs_chgobj!(m, getcol.(m, objf.variables), objf.coefficients) + set_linear_objective!(m, getcol.(m, objf.variables), objf.coefficients) m.objective_constant = objf.constant nothing end function MOI.set!(m::LinQuadOptimizer, ::MOI.ObjectiveFunction, objf::Quad) m.obj_is_quad = true - lqs_chgobj!(m, + set_linear_objective!(m, getcol.(m, objf.affine_variables), objf.affine_coefficients ) @@ -69,7 +69,7 @@ function MOI.set!(m::LinQuadOptimizer, ::MOI.ObjectiveFunction, objf::Quad) getcol.(m, objf.quadratic_colvariables), objf.quadratic_coefficients ) - lqs_copyquad!(m, + set_quadratic_objective!(m, ri, ci, vi @@ -84,7 +84,7 @@ end MOI.canget(m::LinQuadOptimizer, ::MOI.ObjectiveFunction{Linear}) = !m.obj_is_quad function MOI.get(m::LinQuadOptimizer, ::MOI.ObjectiveFunction{Linear}) - variable_coefficients = lqs_getobj(m) + variable_coefficients = get_linearobjective(m) Linear(m.variable_references, variable_coefficients, m.objective_constant) end @@ -96,5 +96,5 @@ MOI.canmodifyobjective(m::LinQuadOptimizer, ::Type{MOI.ScalarCoefficientChange{F function MOI.modifyobjective!(m::LinQuadOptimizer, chg::MOI.ScalarCoefficientChange{Float64}) col = m.variable_mapping[chg.variable] # 0 row is the objective - lqs_chgcoef!(m, 0, col, chg.new_coefficient) + change_coefficient!(m, 0, col, chg.new_coefficient) end diff --git a/src/solve.jl b/src/solve.jl index 27bf511..2806500 100644 --- a/src/solve.jl +++ b/src/solve.jl @@ -28,34 +28,34 @@ function MOI.optimize!(m::LinQuadOptimizer) m.solvetime = time() - t # termination_status - m.termination_status = lqs_terminationstatus(m) - m.primal_status = lqs_primalstatus(m) - m.dual_status = lqs_dualstatus(m) + m.termination_status = get_terminationstatus(m) + m.primal_status = get_primalstatus(m) + m.dual_status = get_dualstatus(m) if m.primal_status in [MOI.FeasiblePoint, MOI.InfeasiblePoint] # primal solution exists - lqs_getx!(m, m.variable_primal_solution) - lqs_getax!(m, m.constraint_primal_solution) + get_variable_primal_solution!(m, m.variable_primal_solution) + get_linear_primal_solution!(m, m.constraint_primal_solution) if hasquadratic(m) - lqs_getqcax!(m, m.qconstraint_primal_solution) + get_quadratic_primal_solution!(m, m.qconstraint_primal_solution) end m.primal_result_count = 1 # CPLEX can return infeasible points elseif m.primal_status == MOI.InfeasibilityCertificate - lqs_getray!(m, m.variable_primal_solution) + get_unboundedray!(m, m.variable_primal_solution) m.primal_result_count = 1 end if m.dual_status in [MOI.FeasiblePoint, MOI.InfeasiblePoint] # dual solution exists - lqs_getdj!(m, m.variable_dual_solution) - lqs_getpi!(m, m.constraint_dual_solution) + get_reducedcosts!(m, m.variable_dual_solution) + get_linear_dual_solution!(m, m.constraint_dual_solution) if hasquadratic(m) - lqs_getqcpi!(m, m.qconstraint_dual_solution) + get_quadratic_dual_solution!(m, m.qconstraint_dual_solution) end m.dual_result_count = 1 # dual solution may not be feasible elseif m.dual_status == MOI.InfeasibilityCertificate - lqs_dualfarkas!(m, m.constraint_dual_solution) + get_farkasdual!(m, m.constraint_dual_solution) m.dual_result_count = 1 end @@ -118,7 +118,7 @@ end function MOI.get(m::LinQuadOptimizer, attr::MOI.ObjectiveValue) if attr.resultindex == 1 - lqs_getobjval(m) + m.objective_constant + get_objectivevalue(m) + m.objective_constant else error("Unable to access multiple objective values") end diff --git a/src/solver_interface.jl b/src/solver_interface.jl index 2b7caed..a602af8 100644 --- a/src/solver_interface.jl +++ b/src/solver_interface.jl @@ -141,7 +141,7 @@ function get_variable_upperbound end Get the number of linear constraints in the model `m`. """ -function get_number_linear_constraints(m::LinQuadOptimizer) end +function get_number_linear_constraints end @deprecate lqs_getnumrows get_number_linear_constraints """ @@ -158,7 +158,7 @@ The `sense` is given by `lqs_char(m, set)`. Ranged constraints (`set=MOI.Interval`) require a call to `lqs_chgrngval!`. """ -function add_linear_constraints!(m::LinQuadOptimizer, rows, cols, coefs, sense, rhs) end +function add_linear_constraints! end @deprecate lqs_addrows! add_linear_constraints! """ @@ -167,7 +167,7 @@ function add_linear_constraints!(m::LinQuadOptimizer, rows, cols, coefs, sense, Get the right-hand side of the linear constraint in the 1-indexed row `row` in the model `m`. """ -function get_rhs(m::LinQuadOptimizer, row) end +function get_rhs end @deprecate lqs_getrhs get_rhs """ @@ -176,34 +176,37 @@ function get_rhs(m::LinQuadOptimizer, row) end Get the linear component of the constraint in the 1-indexed row `row` in the model `m`. Returns a tuple of `(cols, vals)`. """ -function get_linear_constraint(m::LinQuadOptimizer, row) end +function get_linear_constraint end @deprecate lqs_getrows get_linear_constraint -""" - lqs_getcoef(m, row::Int, col::Int)::Float64 - -Get the linear coefficient of the variable in column `col`, constraint `row`. -""" -function lqs_getcoef(m::LinQuadOptimizer, row, col) end +# """ +# get_coefficient(m, row::Int, col::Int) +# +# Get the linear coefficient of the variable in column `col`, constraint `row`. +# """ +# function get_coefficient end +# @deprecate lqs_getcoef get_coefficient """ - lqs_chgcoef(m, row::Int, col::Int, coef::Float64)::Void + change_coefficient!(m, row, col, coef) Set the linear coefficient of the variable in column `col`, constraint `row` to `coef`. """ -function lqs_chgcoef!(m::LinQuadOptimizer, row, col, coef) end +function change_coefficient! end +@deprecate lqs_chgcoef! change_coefficient! """ - lqs_delrows!(m, start_row::Int, end_row::Int)::Void + delete_linear_constraints!(m, start_row::Int, end_row::Int)::Void Delete the linear constraints `start_row`, `start_row+1`, ..., `end_row` from the model `m`. """ -function lqs_delrows!(m::LinQuadOptimizer, start_row, end_row) end +function delete_linear_constraints! end +@deprecate lqs_delrows! delete_linear_constraints! """ - lqs_chgctype(m, cols::Vector{Int}, types::Vector{Symbol})::Void + lqs_chgctype(m, cols::Vector{Int}, types):Void Change the variable types. Variable type must be `:CONTINUOUS`, `:INTEGER`, or `:BINARY`. @@ -286,46 +289,48 @@ See `add_linear_constraints!` for more. """ function lqs_chgrngval!(m::LinQuadOptimizer, rows, vals) end# later -#Objective """ - lqs_chgobj!(m, I::Vector{Int}, J::Vector{Int}, V::Vector{Float64})::Void + set_quadratic_objective!(m, I::Vector{Int}, J::Vector{Int}, V::Vector{Float64})::Void Set the quadratic component of the objective. Arguments given in triplet form for the Q matrix in `0.5 x' Q x`. """ -function lqs_copyquad!(m::LinQuadOptimizer, I, J, V) end +function set_quadratic_objective! end +@deprecate lqs_copyquad! set_quadratic_objective! """ - lqs_chgobj!(m, cols::Vector{Int}, coefs::Vector{Float64})::Void + set_linear_objective!(m, cols::Vector{Int}, coefs::Vector{Float64})::Void Set the linear component of the objective. """ -function lqs_chgobj!(m::LinQuadOptimizer, cols, coefs) end +function set_linear_objective! end +@deprecate lqs_chgobj! set_linear_objective! """ - lqs_chgobjsen(m, sense::Symbol)::Void + change_objectivesense!(m, sense::Symbol)::Void Change the optimization sense of the model `m` to `sense`. `sense` must be `:min` or `:max`. """ -function lqs_chgobjsen!(m::LinQuadOptimizer, sense) end +function change_objectivesense! end +@deprecate lqs_chgobjsen! change_objectivesense! #TODO(@joaquimg): why is this not in-place? """ - lqs_getobj(m)::Vector{Float64} + get_linearobjective(m)::Vector{Float64} Change the linear coefficients of the objective. """ -function lqs_getobj(m::LinQuadOptimizer) end +function get_linearobjective end +@deprecate lqs_getobj get_linearobjective """ - lqs_getobjsen(m)::MOI.OptimizationSense + get_objectivesense(m)::MOI.OptimizationSense Get the optimization sense of the model `m`. """ -function lqs_getobjsen(m::LinQuadOptimizer) end - -#Solve +function get_objectivesense end +@deprecate lqs_getobjsen get_objectivesense """ lqs_mipopt!(m)::Void @@ -355,61 +360,68 @@ function lqs_getstat(m::LinQuadOptimizer) end function lqs_solninfo(m::LinQuadOptimizer) end # complex """ - lqs_getx!(m, x::Vector{Float64}) + get_variable_primal_solution!(m, x::Vector{Float64}) Get the primal solution for the variables in the model `m`, and store in `x`. `x`must have one element for each variable. """ -function lqs_getx!(m::LinQuadOptimizer, x) end +function get_variable_primal_solution! end +@deprecate lqs_getx! get_variable_primal_solution! """ - lqs_getax!(m, x::Vector{Float64}) + get_linear_primal_solution!(m, x::Vector{Float64}) Given a set of linear constraints `l <= a'x <= b` in the model `m`, get the constraint primal `a'x` for each constraint, and store in `x`. `x` must have one element for each linear constraint. """ -function lqs_getax!(m::LinQuadOptimizer, x) end +function get_linear_primal_solution! end +@deprecate lqs_getax! get_linear_primal_solution! """ - lqs_getqcax!(m, x::Vector{Float64}) + get_quadratic_primal_solution!(m, x::Vector{Float64}) Given a set of quadratic constraints `l <= a'x + x'Qx <= b` in the model `m`, get the constraint primal `a'x + x'Qx` for each constraint, and store in `x`. `x` must have one element for each quadratic constraint. """ -function lqs_getqcax!(m::LinQuadOptimizer, x) end +function get_quadratic_primal_solution! end +@deprecate lqs_getqcax! get_quadratic_primal_solution! """ - lqs_getdj!(m, x::Vector{Float64}) + get_reducedcosts!(m, x::Vector{Float64}) Get the dual solution (reduced-costs) for the variables in the model `m`, and store in `x`. `x`must have one element for each variable. """ -function lqs_getdj!(m::LinQuadOptimizer, x) end +function get_reducedcosts! end +@deprecate lqs_getdj! get_reducedcosts! """ - lqs_getpi!(m, x::Vector{Float64}) + get_linear_dual_solution!(m, x::Vector{Float64}) Get the dual solution for the linear constraints in the model `m`, and store in `x`. `x`must have one element for each linear constraint. """ -function lqs_getpi!(m::LinQuadOptimizer, x) end +function get_linear_dual_solution! end +@deprecate lqs_getpi! get_linear_dual_solution! """ - lqs_getqcpi!(m, x::Vector{Float64}) + get_quadratic_dual_solution!(m, x::Vector{Float64}) Get the dual solution for the quadratic constraints in the model `m`, and store in `x`. `x`must have one element for each quadratic constraint. """ -function lqs_getqcpi!(m::LinQuadOptimizer, x) end +function get_quadratic_dual_solution! end +@deprecate lqs_getqcpi! get_quadratic_dual_solution! """ - lqs_getobjval!(m) + get_objectivevalue!(m) Get the objective value of the solved model `m`. """ -function lqs_getobjval(m::LinQuadOptimizer) end +function get_objectivevalue(m::LinQuadOptimizer) end +@deprecate lqs_getobjval get_objectivevalue # TODO(@joaquimg): what is this? function lqs_getbestobjval(m::LinQuadOptimizer) end @@ -446,64 +458,72 @@ optimization of the model `m`. function lqs_getnodecnt(m::LinQuadOptimizer) end """ - lqs_dualfarkas!(m, x::Vector{Float64}) + get_farkasdual!(m, x::Vector{Float64}) Get the farkas dual (certificate of primal infeasiblility) for the linear constraints in the model `m`, and store in `x`. `x`must have one element for each linear constraint. """ -function lqs_dualfarkas!(m::LinQuadOptimizer, x) end +function get_farkasdual! end +@deprecate lqs_dualfarkas! get_farkasdual! """ - lqs_getray!(m, x::Vector{Float64}) + get_unboundedray!(m, x::Vector{Float64}) Get the unbounded ray (certificate of dual infeasiblility) for the linear constraints in the model `m`, and store in `x`. `x`must have one element for each variable. """ -function lqs_getray!(m::LinQuadOptimizer, x) end +function get_unboundedray! end +@deprecate lqs_getray! get_unboundedray! """ - lqs_terminationstatus(m) + get_terminationstatus(m) Get the termination status of the model `m`. """ -function lqs_terminationstatus(m::LinQuadOptimizer) end +function get_terminationstatus end +@deprecate lqs_terminationstatus get_terminationstatus """ - lqs_primalstatus(m) + get_primalstatus(m) Get the primal status of the model `m`. """ -function lqs_primalstatus(m::LinQuadOptimizer) end +function get_primalstatus end +@deprecate lqs_primalstatus get_primalstatus """ - lqs_dualstatus(m) + get_dualstatus(m) Get the dual status of the model `m`. """ -function lqs_dualstatus(m::LinQuadOptimizer) end +function get_dualstatus end +@deprecate lqs_dualstatus get_dualstatus # Variables """ - lqs_getnumcols(m)::Int + get_number_variables(m)::Int Get the number of variables in the model `m`. """ -function lqs_getnumcols(m::LinQuadOptimizer) end +function get_number_variables end +@deprecate lqs_getnumcols get_number_variables """ - lqs_newcols!(m, n::Int)::Void + add_variables!(m, n::Int)::Void Add `n` new variables to the model `m`. """ -function lqs_newcols!(m::LinQuadOptimizer, n) end +function add_variables! end +@deprecate lqs_newcols! add_variables! """ - lqs_delcols!(m, start_col::Int, end_col::Int)::Void + delete_variables!(m, start_col::Int, end_col::Int)::Void Delete the columns `start_col`, `start_col+1`, ..., `end_col` from the model `m`. """ -function lqs_delcols!(m::LinQuadOptimizer, start_col, end_col) end +function delete_variables! end +@deprecate lqs_delcols! delete_variables! """ lqs_addmipstarts!(m, cols::Vector{Int}, x::Vector{Float64})::Void diff --git a/src/variables.jl b/src/variables.jl index 16f5c67..965d163 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -62,7 +62,7 @@ end MOI.canget(m::LinQuadOptimizer, ::MOI.NumberOfVariables) = true function MOI.get(m::LinQuadOptimizer, ::MOI.NumberOfVariables) - return lqs_getnumcols(m) + return get_number_variables(m) end #= @@ -79,7 +79,7 @@ end =# function MOI.addvariable!(m::LinQuadOptimizer) - lqs_newcols!(m, 1) + add_variables!(m, 1) # assumes we add columns linearly m.last_variable_reference += 1 ref = VarInd(m.last_variable_reference) @@ -96,7 +96,7 @@ end function MOI.addvariables!(m::LinQuadOptimizer, n::Int) previous_vars = MOI.get(m, MOI.NumberOfVariables()) - lqs_newcols!(m, n) + add_variables!(m, n) variable_references = VarInd[] sizehint!(variable_references, n) for i in 1:n @@ -135,7 +135,7 @@ end MOI.candelete(m::LinQuadOptimizer, ref::VarInd) = MOI.isvalid(m, ref) function MOI.delete!(m::LinQuadOptimizer, ref::VarInd) col = m.variable_mapping[ref] - lqs_delcols!(m, col, col) + delete_variables!(m, col, col) # delete from problem deleteat!(m.variable_references, col) From 9a27b9d0f823a4a8901b6d5ba1d69ec60b258a1a Mon Sep 17 00:00:00 2001 From: odow Date: Mon, 7 May 2018 20:10:37 +1200 Subject: [PATCH 03/10] More Renamings --- src/LinQuadOptInterface.jl | 4 +- src/constraints.jl | 8 +- src/constraints/scalarquadratic.jl | 4 +- src/constraints/vectorofvariables.jl | 8 +- src/objective.jl | 11 +-- src/solve.jl | 10 +-- src/solver_interface.jl | 106 ++++++++++++--------------- src/variables.jl | 4 +- 8 files changed, 71 insertions(+), 84 deletions(-) diff --git a/src/LinQuadOptInterface.jl b/src/LinQuadOptInterface.jl index 5c69ee6..eafedda 100644 --- a/src/LinQuadOptInterface.jl +++ b/src/LinQuadOptInterface.jl @@ -267,10 +267,10 @@ end MOI.canset(m::LinQuadOptimizer, ::MOI.Name) = true function MOI.supportsconstraint(m::LinQuadOptimizer, ft::Type{F}, st::Type{S}) where F <: MOI.AbstractFunction where S <: MOI.AbstractSet - (ft,st) in lqs_supported_constraints(m) + (ft,st) in supported_constraints(m) end function MOI.supports(m::LinQuadOptimizer, ::MOI.ObjectiveFunction{F}) where F <: MOI.AbstractFunction - F in lqs_supported_objectives(m) + F in supported_objectives(m) end # a useful helper function diff --git a/src/constraints.jl b/src/constraints.jl index 78bae16..c816516 100644 --- a/src/constraints.jl +++ b/src/constraints.jl @@ -52,7 +52,7 @@ end =# function MOI.canaddconstraint(m::LinQuadOptimizer, f::Type{F}, s::Type{S}) where {F<:MOI.AbstractFunction, S<:MOI.AbstractSet} - return (f,s) in lqs_supported_constraints(m) + return (f,s) in supported_constraints(m) end #= @@ -60,7 +60,7 @@ end =# function MOI.canget(m::LinQuadOptimizer, ::MOI.NumberOfConstraints{F, S}) where F where S - return (F,S) in lqs_supported_constraints(m) + return (F,S) in supported_constraints(m) end function MOI.get(m::LinQuadOptimizer, ::MOI.NumberOfConstraints{F, S}) where F where S length(constrdict(m, CI{F,S}(UInt(0)))) @@ -71,7 +71,7 @@ end =# function MOI.canget(m::LinQuadOptimizer, ::MOI.ListOfConstraintIndices{F, S}) where F where S - return (F,S) in lqs_supported_constraints(m) + return (F,S) in supported_constraints(m) end function MOI.get(m::LinQuadOptimizer, ::MOI.ListOfConstraintIndices{F, S}) where F where S sort(collect(keys(constrdict(m, CI{F,S}(UInt(0))))), by=x->x.value) @@ -84,7 +84,7 @@ end MOI.canget(m::LinQuadOptimizer, ::MOI.ListOfConstraints) = true function MOI.get(m::LinQuadOptimizer, ::MOI.ListOfConstraints) ret = [] - for (F,S) in lqs_supported_constraints(m) + for (F,S) in supported_constraints(m) if MOI.get(m, MOI.NumberOfConstraints{F,S}()) > 0 push!(ret, (F,S)) end diff --git a/src/constraints/scalarquadratic.jl b/src/constraints/scalarquadratic.jl index 853cc07..18e5419 100644 --- a/src/constraints/scalarquadratic.jl +++ b/src/constraints/scalarquadratic.jl @@ -19,7 +19,7 @@ function MOI.addconstraint!(m::LinQuadOptimizer, func::Quad, set::S) where S <: dict = constrdict(m, ref) push!(m.qconstraint_primal_solution, NaN) push!(m.qconstraint_dual_solution, NaN) - # dict[ref] = lqs_getnumqconstrs(m) + # dict[ref] = get_number_quadratic_constraints(m) dict[ref] = length(m.qconstraint_primal_solution) return ref end @@ -37,7 +37,7 @@ function addquadraticconstraint!(m::LinQuadOptimizer, f::Quad, sense::Cchar, rhs getcol.(m, f.quadratic_colvariables), f.quadratic_coefficients ) - lqs_addqconstr!(m, + add_quadratic_constraint!(m, getcol.(m, f.affine_variables), f.affine_coefficients, rhs - f.constant, diff --git a/src/constraints/vectorofvariables.jl b/src/constraints/vectorofvariables.jl index 7ad54d3..47db669 100644 --- a/src/constraints/vectorofvariables.jl +++ b/src/constraints/vectorofvariables.jl @@ -65,7 +65,7 @@ end function MOI.addconstraint!(m::LinQuadOptimizer, v::VecVar, sos::S) where S <: Union{MOI.SOS1, MOI.SOS2} lqs_make_problem_type_integer(m) - lqs_addsos!(m, getcol.(m, v.variables), sos.weights, lqs_char(m, sos)) + add_sos_constraint!(m, getcol.(m, v.variables), sos.weights, lqs_char(m, sos)) m.last_constraint_reference += 1 ref = VVCI{S}(m.last_constraint_reference) dict = constrdict(m, ref) @@ -78,7 +78,7 @@ function MOI.delete!(m::LinQuadOptimizer, c::VVCI{<:Union{SOS1, SOS2}}) deleteconstraintname!(m, c) dict = constrdict(m, c) idx = dict[c] - lqs_delsos!(m, idx, idx) + delete_sos!(m, idx, idx) deleteref!(cmap(m).sos1, idx, c) deleteref!(cmap(m).sos2, idx, c) if !hasinteger(m) @@ -88,7 +88,7 @@ end MOI.canget(m::LinQuadOptimizer, ::MOI.ConstraintSet, ::Type{VVCI{S}}) where S <: Union{MOI.SOS1, MOI.SOS2} = true function MOI.get(m::LinQuadOptimizer, ::MOI.ConstraintSet, c::VVCI{S}) where S <: Union{MOI.SOS1, MOI.SOS2} - indices, weights, types = lqs_getsos(m, m[c]) + indices, weights, types = get_sos_constraint(m, m[c]) set = S(weights) @assert types == lqs_char(m, set) return set @@ -96,6 +96,6 @@ end MOI.canget(m::LinQuadOptimizer, ::MOI.ConstraintFunction, ::Type{VVCI{S}}) where S <: Union{MOI.SOS1, MOI.SOS2} = true function MOI.get(m::LinQuadOptimizer, ::MOI.ConstraintFunction, c::VVCI{<:Union{SOS1, SOS2}}) - indices, weights, types = lqs_getsos(m, m[c]) + indices, weights, types = get_sos_constraint(m, m[c]) return VecVar(m.variable_references[indices]) end diff --git a/src/objective.jl b/src/objective.jl index e2a612c..c6793e8 100644 --- a/src/objective.jl +++ b/src/objective.jl @@ -8,14 +8,14 @@ MOI.get(m::LinQuadOptimizer,::MOI.ObjectiveSense) = m.obj_sense MOI.canset(::LinQuadOptimizer, ::MOI.ObjectiveSense) = true function MOI.set!(m::LinQuadOptimizer, ::MOI.ObjectiveSense, sense::MOI.OptimizationSense) if sense == MOI.MinSense - change_objectivesense!(m, :min) + change_objective_sense!(m, :min) m.obj_sense = MOI.MinSense elseif sense == MOI.MaxSense - change_objectivesense!(m, :max) + change_objective_sense!(m, :max) m.obj_sense = MOI.MaxSense elseif sense == MOI.FeasibilitySense # we set the objective sense to :min, and the objective to 0.0 - change_objectivesense!(m, :min) + change_objective_sense!(m, :min) unsafe_set!(m, MOI.ObjectiveFunction{Linear}(), MOI.ScalarAffineFunction(VarInd[],Float64[],0.0)) m.obj_is_quad = false m.obj_sense = MOI.FeasibilitySense @@ -33,7 +33,7 @@ function MOI.canset(m::LinQuadOptimizer, ::MOI.ObjectiveFunction{F}) where F<:MO # it doesn't make sense to set an objective for a feasibility problem return false end - return F in lqs_supported_objectives(m) + return F in supported_objectives(m) end function MOI.set!(m::LinQuadOptimizer, ::MOI.ObjectiveFunction{F}, objf::Linear) where F @@ -84,7 +84,8 @@ end MOI.canget(m::LinQuadOptimizer, ::MOI.ObjectiveFunction{Linear}) = !m.obj_is_quad function MOI.get(m::LinQuadOptimizer, ::MOI.ObjectiveFunction{Linear}) - variable_coefficients = get_linearobjective(m) + variable_coefficients = zeros(length(m.variable_references)) + get_linear_objective!(m, variable_coefficients) Linear(m.variable_references, variable_coefficients, m.objective_constant) end diff --git a/src/solve.jl b/src/solve.jl index 2806500..e5444ca 100644 --- a/src/solve.jl +++ b/src/solve.jl @@ -118,7 +118,7 @@ end function MOI.get(m::LinQuadOptimizer, attr::MOI.ObjectiveValue) if attr.resultindex == 1 - get_objectivevalue(m) + m.objective_constant + get_objective_value(m) + m.objective_constant else error("Unable to access multiple objective values") end @@ -261,7 +261,7 @@ MOI.get(m::LinQuadOptimizer, ::MOI.ObjectiveBound) = lqs_getbestobjval(m) MOI.canget(m::LinQuadOptimizer, ::MOI.ObjectiveBound) = true # struct RelativeGap <: MOI.AbstractOptimizerAttribute end -MOI.get(m::LinQuadOptimizer, ::MOI.RelativeGap) = lqs_getmiprelgap(m) +MOI.get(m::LinQuadOptimizer, ::MOI.RelativeGap) = get_relative_mip_gap(m) MOI.canget(m::LinQuadOptimizer, ::MOI.RelativeGap) = true # struct SolveTime <: MOI.AbstractOptimizerAttribute end @@ -269,15 +269,15 @@ MOI.get(m::LinQuadOptimizer, ::MOI.SolveTime) = m.solvetime MOI.canget(m::LinQuadOptimizer, ::MOI.SolveTime) = true # struct SimplexIterations <: MOI.AbstractOptimizerAttribute end -MOI.get(m::LinQuadOptimizer, ::MOI.SimplexIterations) = lqs_getitcnt(m) +MOI.get(m::LinQuadOptimizer, ::MOI.SimplexIterations) = get_iteration_count(m) MOI.canget(m::LinQuadOptimizer, ::MOI.SimplexIterations) = true # struct BarrierIterations <: MOI.AbstractOptimizerAttribute end -MOI.get(m::LinQuadOptimizer, ::MOI.BarrierIterations) = lqs_getbaritcnt(m) +MOI.get(m::LinQuadOptimizer, ::MOI.BarrierIterations) = get_barrier_iterations(m) MOI.canget(m::LinQuadOptimizer, ::MOI.BarrierIterations) = true # struct NodeCount <: MOI.AbstractOptimizerAttribute end -MOI.get(m::LinQuadOptimizer, ::MOI.NodeCount) = lqs_getnodecnt(m) +MOI.get(m::LinQuadOptimizer, ::MOI.NodeCount) = get_node_count(m) MOI.canget(m::LinQuadOptimizer, ::MOI.NodeCount) = true # struct RawSolver <: MOI.AbstractOptimizerAttribute end diff --git a/src/solver_interface.jl b/src/solver_interface.jl index a602af8..cde2891 100644 --- a/src/solver_interface.jl +++ b/src/solver_interface.jl @@ -78,26 +78,9 @@ Initializes a model given a model type `M` and an `env` that might be a `nothing for some solvers. """ function LinQuadModel end -# -# """ -# lqs_setparam!(m, name, val)::Void -# -# Set the parameter `name` to `val` for the model `m`. -# """ -# function lqs_setparam!(m::LinQuadOptimizer, name, val) end -# -# """ -# lqs_setlogfile!(m, path::String)::Void -# -# Set the log file to `path` for the model `m`. -# """ -# function lqs_setlogfile!(m::LinQuadOptimizer, path) end -# -# # TODO(@joaquimg): what is this? -# function lqs_getprobtype(m::LinQuadOptimizer) end """ - lqs_supported_constraints(m)::Vector{ + supported_constraints(m)::Vector{ Tuple{MOI.AbstractFunction, MOI.AbstractSet} } @@ -105,16 +88,18 @@ Get a list of supported constraint types in the model `m`. For example, `[(LQOI.Linear, LQOI.EQ)]` """ -lqs_supported_constraints(m::LinQuadOptimizer) = [] +function supported_constraints end +@deprecate lqs_supported_constraints supported_constraints """ - lqs_supported_objectives(m)::Vector{MOI.AbstractScalarFunction} + supported_objectives(m)::Vector{MOI.AbstractScalarFunction} Get a list of supported objective types in the model `m`. For example, `[LQOI.Linear, LQOI.Quad]` """ -lqs_supported_objectives(m::LinQuadOptimizer) = [] +function supported_objectives end +@deprecate lqs_supported_objectives supported_objectives # Constraints @@ -239,44 +224,47 @@ If an explicit call is needed to change the problem type continuous (e.g., CPLEX function lqs_make_problem_type_continuous(m::LinQuadOptimizer) end """ - lqs_addsos!(m, cols::Vector{Int}, vals::Vector{Float64}, typ::Symbol)::Void + add_sos_constraint!(m, cols::Vector{Int}, vals::Vector{Float64}, typ::Symbol)::Void Add the SOS constraint to the model `m`. `typ` is either `:SOS1` or `:SOS2`. """ -function lqs_addsos!(m::LinQuadOptimizer, cols, vals, typ) end +function add_sos_constraint! end +@deprecate lqs_addsos! add_sos_constraint! """ - lqs_delrows!(m, start_idx::Int, end_idx::Int)::Void + delete_sos!(m, start_idx::Int, end_idx::Int)::Void Delete the SOS constraints `start_idx`, `start_idx+1`, ..., `end_idx` from the model `m`. """ -function lqs_delsos!(m::LinQuadOptimizer, start_idx, end_idx) end +function delete_sos! end +@deprecate lqs_delsos! delete_sos! """ - lqs_getsos(m, idx::Int)::Tuple{Vector{Int}, Vector{Float64}, Symbol} + get_sos_constraint(m, idx::Int)::Tuple{Vector{Int}, Vector{Float64}, Symbol} Get the SOS constraint `idx` from the model `m`. Returns the triplet `(cols, vals, typ)`. """ -function lqs_getsos(m::LinQuadOptimizer, idx) end +function get_sos_constraint end """ - lqs_getnumqconstrs(m)::Int + get_number_quadratic_constraints(m)::Int Get the number of quadratic constraints in the model `m`. """ -function lqs_getnumqconstrs(m::LinQuadOptimizer) end +function get_number_quadratic_constraints end +@deprecate lqs_getnumqcosntrs get_number_quadratic_constraints """ - lqs_addqconstr!(m, cols::Vector{Int}, coefs::Vector{Float64}, rhs::Float64, - sense::Symbol, I::Vector{Int}, J::Vector{Int}, V::Vector{Float64})::Void + add_quadratic_constraint!(m, cols::Vector{Int}, coefs::Vector{Float64}, rhs::Float64, + sense, I::Vector{Int}, J::Vector{Int}, V::Vector{Float64})::Void Add a quadratic constraint `a'x + 0.5 x' Q x`. See `add_linear_constraints!` for information of linear component. Arguments `(I,J,V)` given in triplet form for the Q matrix in `0.5 x' Q x`. """ -function lqs_addqconstr!(m::LinQuadOptimizer, cols, coefs, rhs, sense, I, J, V) end +function add_quadratic_constraint! end """ @@ -307,22 +295,22 @@ function set_linear_objective! end @deprecate lqs_chgobj! set_linear_objective! """ - change_objectivesense!(m, sense::Symbol)::Void + change_objective_sense!(m, sense::Symbol)::Void Change the optimization sense of the model `m` to `sense`. `sense` must be `:min` or `:max`. """ -function change_objectivesense! end -@deprecate lqs_chgobjsen! change_objectivesense! +function change_objective_sense! end +@deprecate lqs_chgobjsen! change_objective_sense! -#TODO(@joaquimg): why is this not in-place? """ - get_linearobjective(m)::Vector{Float64} + get_linear_objective!(m, x::Vector{Float64}) -Change the linear coefficients of the objective. +Change the linear coefficients of the objective and store +in `x`. """ -function get_linearobjective end -@deprecate lqs_getobj get_linearobjective +function get_linear_objective! end +@deprecate lqs_getobj get_linear_objective! """ get_objectivesense(m)::MOI.OptimizationSense @@ -353,12 +341,6 @@ Solve a linear program `m`. """ function lqs_lpopt!(m::LinQuadOptimizer) end -# TODO(@joaquim): what is this? -function lqs_getstat(m::LinQuadOptimizer) end - -# TODO(@joaquim): what is this? -function lqs_solninfo(m::LinQuadOptimizer) end # complex - """ get_variable_primal_solution!(m, x::Vector{Float64}) @@ -416,46 +398,50 @@ function get_quadratic_dual_solution! end @deprecate lqs_getqcpi! get_quadratic_dual_solution! """ - get_objectivevalue!(m) + get_objective_value(m) Get the objective value of the solved model `m`. """ -function get_objectivevalue(m::LinQuadOptimizer) end -@deprecate lqs_getobjval get_objectivevalue +function get_objective_value end +@deprecate lqs_getobjval get_objective_value # TODO(@joaquimg): what is this? function lqs_getbestobjval(m::LinQuadOptimizer) end """ - lqs_getmiprelgap!(m) + get_relative_mip_gap(m) Get the relative MIP gap of the solved model `m`. """ -function lqs_getmiprelgap(m::LinQuadOptimizer) end +function get_relative_mip_gap end +@deprecate lqs_getmiprelgap get_relative_mip_gap """ - lqs_getitcnt!(m) + get_iteration_count(m) Get the number of simplex iterations performed during the most recent optimization of the model `m`. """ -function lqs_getitcnt(m::LinQuadOptimizer) end +function get_iteration_count end +@deprecate lqs_getitcnt get_iteration_count """ - lqs_getbaritcnt!(m) + get_barrier_iterations(m) Get the number of barrier iterations performed during the most recent optimization of the model `m`. """ -function lqs_getbaritcnt(m::LinQuadOptimizer) end +function get_barrier_iterations end +@deprecate lqs_getbaritcnt get_barrier_iterations """ - lqs_getnodecnt!(m) + get_node_count(m) Get the number of branch-and-cut nodes expolored during the most recent optimization of the model `m`. """ -function lqs_getnodecnt(m::LinQuadOptimizer) end +function get_node_count end +@deprecate lqs_getnodecnt get_node_count """ get_farkasdual!(m, x::Vector{Float64}) @@ -500,7 +486,6 @@ Get the dual status of the model `m`. function get_dualstatus end @deprecate lqs_dualstatus get_dualstatus -# Variables """ get_number_variables(m)::Int @@ -526,8 +511,9 @@ function delete_variables! end @deprecate lqs_delcols! delete_variables! """ - lqs_addmipstarts!(m, cols::Vector{Int}, x::Vector{Float64})::Void + add_mip_starts!(m, cols::Vector{Int}, x::Vector{Float64})::Void Add the MIP start `x` for the variables in the columns `cols` of the model `m`. """ -function lqs_addmipstarts!(m::LinQuadOptimizer, cols, x) end +function add_mip_starts! end +@deprecate lqs_addmipstarts! add_mip_starts! diff --git a/src/variables.jl b/src/variables.jl index 965d163..f504c79 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -175,10 +175,10 @@ end MOI.canset(m::LinQuadOptimizer, ::MOI.VariablePrimalStart, ::VarInd) = true function MOI.set!(m::LinQuadOptimizer, ::MOI.VariablePrimalStart, ref::VarInd, val::Float64) - lqs_addmipstarts!(m, [getcol(m, ref)], [val]) + add_mip_starts!(m, [getcol(m, ref)], [val]) end MOI.canset(m::LinQuadOptimizer, ::MOI.VariablePrimalStart, ::Vector{VarInd}) = true function MOI.set!(m::LinQuadOptimizer, ::MOI.VariablePrimalStart, refs::Vector{VarInd}, vals::Vector{Float64}) - lqs_addmipstarts!(m, getcol.(m, refs), vals) + add_mip_starts!(m, getcol.(m, refs), vals) end From fb17540fa60dfd770f010efa47d945ad783c4a5d Mon Sep 17 00:00:00 2001 From: odow Date: Mon, 7 May 2018 21:20:06 +1200 Subject: [PATCH 04/10] More Renamings --- src/constraints/scalaraffine.jl | 8 ++-- src/constraints/singlevariable.jl | 16 +++---- src/constraints/vectorofvariables.jl | 4 +- src/solve.jl | 8 ++-- src/solver_interface.jl | 72 ++++++++++++++++------------ 5 files changed, 59 insertions(+), 49 deletions(-) diff --git a/src/constraints/scalaraffine.jl b/src/constraints/scalaraffine.jl index 1e25395..e5c14e8 100644 --- a/src/constraints/scalaraffine.jl +++ b/src/constraints/scalaraffine.jl @@ -28,7 +28,7 @@ end function addlinearconstraint!(m::LinQuadOptimizer, func::Linear, set::IV) addlinearconstraint!(m, func, lqs_char(m,set), set.lower) - lqs_chgrngval!(m, [get_number_linear_constraints(m)], [set.upper - set.lower]) + change_range_value!(m, [get_number_linear_constraints(m)], [set.upper - set.lower]) end function addlinearconstraint!(m::LinQuadOptimizer, func::Linear, sense::Cchar, rhs) @@ -71,7 +71,7 @@ function addlinearconstraints!(m::LinQuadOptimizer, func::Vector{Linear}, set::V numrows = get_number_linear_constraints(m) addlinearconstraints!(m, func, lqs_char.(m,set), [s.lower for s in set]) numrows2 = get_number_linear_constraints(m) - lqs_chgrngval!(m, collect(numrows+1:numrows2), [s.upper - s.lower for s in set]) + change_range_value!(m, collect(numrows+1:numrows2), [s.upper - s.lower for s in set]) end function addlinearconstraints!(m::LinQuadOptimizer, func::Vector{Linear}, sense::Vector{Cchar}, rhs::Vector{Float64}) @@ -147,7 +147,7 @@ function MOI.modifyconstraint!(m::LinQuadOptimizer, c::LCI{IV}, set::IV) # a rngval equal to upper-lower. row = m[c] change_coefficient!(m, row, 0, set.lower) - lqs_chgrngval!(m, [row], [set.upper - set.lower]) + change_range_value!(m, [row], [set.upper - set.lower]) end #= @@ -189,7 +189,7 @@ end function MOI.transformconstraint!(m::LinQuadOptimizer, ref::LCI{S1}, newset::S2) where S1 where S2 <: Union{LE, GE, EQ} dict = constrdict(m, ref) row = dict[ref] - lqs_chgsense!(m, [row], [lqs_char(m,newset)]) + change_linear_constraint_sense!(m, [row], [lqs_char(m,newset)]) m.last_constraint_reference += 1 ref2 = LCI{S2}(m.last_constraint_reference) dict2 = constrdict(m, ref2) diff --git a/src/constraints/singlevariable.jl b/src/constraints/singlevariable.jl index d1fc35e..4b89a7b 100644 --- a/src/constraints/singlevariable.jl +++ b/src/constraints/singlevariable.jl @@ -109,10 +109,10 @@ function MOI.addconstraint!(m::LinQuadOptimizer, v::SinVar, set::MOI.ZeroOne) ub = get_variable_upperbound(m, getcol(m, v)) lb = get_variable_lowerbound(m, getcol(m, v)) dict[ref] = (v.variable, lb, ub) - lqs_chgctype!(m, [getcol(m, v)], [lqs_char(m, set)]) + change_variable_types!(m, [getcol(m, v)], [lqs_char(m, set)]) setvariablebound!(m, getcol(m, v), 1.0, lqs_char(m, Val{:Upperbound}())) setvariablebound!(m, getcol(m, v), 0.0, lqs_char(m, Val{:Lowerbound}())) - lqs_make_problem_type_integer(m) + make_problem_type_integer(m) ref end @@ -121,12 +121,12 @@ function MOI.delete!(m::LinQuadOptimizer, c::SVCI{MOI.ZeroOne}) deleteconstraintname!(m, c) dict = constrdict(m, c) (v, lb, ub) = dict[c] - lqs_chgctype!(m, [getcol(m, v)], [lqs_char(m, Val{:Continuous}())]) + change_variable_types!(m, [getcol(m, v)], [lqs_char(m, Val{:Continuous}())]) setvariablebound!(m, getcol(m, v), ub, lqs_char(m, Val{:Upperbound}())) setvariablebound!(m, getcol(m, v), lb, lqs_char(m, Val{:Lowerbound}())) delete!(dict, c) if !hasinteger(m) - lqs_make_problem_type_continuous(m) + make_problem_type_continuous(m) end end @@ -141,12 +141,12 @@ MOI.get(m::LinQuadOptimizer, ::MOI.ConstraintFunction, c::SVCI{MOI.ZeroOne}) = m =# function MOI.addconstraint!(m::LinQuadOptimizer, v::SinVar, set::MOI.Integer) - lqs_chgctype!(m, [getcol(m, v)], [lqs_char(m, set)]) + change_variable_types!(m, [getcol(m, v)], [lqs_char(m, set)]) m.last_constraint_reference += 1 ref = SVCI{MOI.Integer}(m.last_constraint_reference) dict = constrdict(m, ref) dict[ref] = v.variable - lqs_make_problem_type_integer(m) + make_problem_type_integer(m) ref end @@ -154,10 +154,10 @@ function MOI.delete!(m::LinQuadOptimizer, c::SVCI{MOI.Integer}) deleteconstraintname!(m, c) dict = constrdict(m, c) v = dict[c] - lqs_chgctype!(m, [getcol(m, v)], [lqs_char(m, Val{:Continuous}())]) + change_variable_types!(m, [getcol(m, v)], [lqs_char(m, Val{:Continuous}())]) delete!(dict, c) if !hasinteger(m) - lqs_make_problem_type_continuous(m) + make_problem_type_continuous(m) end end MOI.candelete(m::LinQuadOptimizer, c::SVCI{MOI.Integer}) = true diff --git a/src/constraints/vectorofvariables.jl b/src/constraints/vectorofvariables.jl index 47db669..0289bcc 100644 --- a/src/constraints/vectorofvariables.jl +++ b/src/constraints/vectorofvariables.jl @@ -64,7 +64,7 @@ end =# function MOI.addconstraint!(m::LinQuadOptimizer, v::VecVar, sos::S) where S <: Union{MOI.SOS1, MOI.SOS2} - lqs_make_problem_type_integer(m) + make_problem_type_integer(m) add_sos_constraint!(m, getcol.(m, v.variables), sos.weights, lqs_char(m, sos)) m.last_constraint_reference += 1 ref = VVCI{S}(m.last_constraint_reference) @@ -82,7 +82,7 @@ function MOI.delete!(m::LinQuadOptimizer, c::VVCI{<:Union{SOS1, SOS2}}) deleteref!(cmap(m).sos1, idx, c) deleteref!(cmap(m).sos2, idx, c) if !hasinteger(m) - lqs_make_problem_type_continuous(m) + make_problem_type_continuous(m) end end diff --git a/src/solve.jl b/src/solve.jl index e5444ca..4d5b664 100644 --- a/src/solve.jl +++ b/src/solve.jl @@ -19,11 +19,11 @@ function MOI.optimize!(m::LinQuadOptimizer) t = time() if hasinteger(m) - lqs_mipopt!(m) + solve_mip_problem!(m) elseif hasquadratic(m) - lqs_qpopt!(m) + solve_quadratic_problem!(m) else - lqs_lpopt!(m) + solve_linear_problem!(m) end m.solvetime = time() - t @@ -257,7 +257,7 @@ MOI.canget(m::LinQuadOptimizer, ::MOI.ConstraintDual, ::Type{VLCI{S}}) where S < =# # struct ObjectiveBound <: MOI.AbstractOptimizerAttribute end -MOI.get(m::LinQuadOptimizer, ::MOI.ObjectiveBound) = lqs_getbestobjval(m) +MOI.get(m::LinQuadOptimizer, ::MOI.ObjectiveBound) = get_objective_bound(m) MOI.canget(m::LinQuadOptimizer, ::MOI.ObjectiveBound) = true # struct RelativeGap <: MOI.AbstractOptimizerAttribute end diff --git a/src/solver_interface.jl b/src/solver_interface.jl index cde2891..2678502 100644 --- a/src/solver_interface.jl +++ b/src/solver_interface.jl @@ -141,7 +141,7 @@ The A matrix is given in triplet form `A[rows[i], cols[i]] = coef[i]` for all The `sense` is given by `lqs_char(m, set)`. -Ranged constraints (`set=MOI.Interval`) require a call to `lqs_chgrngval!`. +Ranged constraints (`set=MOI.Interval`) require a call to `change_range_value!`. """ function add_linear_constraints! end @deprecate lqs_addrows! add_linear_constraints! @@ -164,14 +164,6 @@ the model `m`. Returns a tuple of `(cols, vals)`. function get_linear_constraint end @deprecate lqs_getrows get_linear_constraint -# """ -# get_coefficient(m, row::Int, col::Int) -# -# Get the linear coefficient of the variable in column `col`, constraint `row`. -# """ -# function get_coefficient end -# @deprecate lqs_getcoef get_coefficient - """ change_coefficient!(m, row, col, coef) @@ -193,35 +185,44 @@ function delete_linear_constraints! end """ lqs_chgctype(m, cols::Vector{Int}, types):Void -Change the variable types. Variable type must be `:CONTINUOUS`, `:INTEGER`, or -`:BINARY`. +Change the variable types. Type is the output of one of: + - `lqs_char(m, ::ZeroOne)`, for binary variables; + - `lqs_char(m, ::Integer)`, for integer variables; and + - `lqs_char(m, Val{:Continuous})`, for continuous variables. """ -function lqs_chgctype!(m::LinQuadOptimizer, cols, types) end +function change_variable_types! end +@deprecate lqs_chgctype! change_variable_types! """ - lqs_chgsense(m, rows::Vector{Int}, sense::Vector{Symbol})::Void + change_linear_constraint_sense!(m, rows::Vector{Int}, sense::Vector{Symbol})::Void Change the sense of the linear constraints in `rows` to `sense`. -The `sense` is one of `:RANGE`, `:LOWER`, `:UPPER`, `:EQUALITY`. +`sense` is the output of `lqs_char(m, set)`, where `set` +is the corresponding set for the row `rows[i]`. -Ranged constraints (sense=`:RANGE`) require a call to `lqs_chgrngval!`. +`Interval` constraints require a call to `change_range_value!`. """ -function lqs_chgsense!(m::LinQuadOptimizer, rows, sense) end +function change_linear_constraint_sense! end +@deprecate lqs_chgsense! change_linear_constraint_sense! """ - lqs_make_problem_type_integer(m)::Void + make_problem_type_integer(m)::Void If an explicit call is needed to change the problem type integer (e.g., CPLEX). """ -function lqs_make_problem_type_integer(m::LinQuadOptimizer) end +function make_problem_type_integer(m::LinQuadOptimizer) + nothing # default +end """ - lqs_make_problem_type_continuous(m)::Void + make_problem_type_continuous(m)::Void If an explicit call is needed to change the problem type continuous (e.g., CPLEX). """ -function lqs_make_problem_type_continuous(m::LinQuadOptimizer) end +function make_problem_type_continuous(m::LinQuadOptimizer) + nothing # default +end """ add_sos_constraint!(m, cols::Vector{Int}, vals::Vector{Float64}, typ::Symbol)::Void @@ -268,14 +269,15 @@ function add_quadratic_constraint! end """ - lqs_chgrngval!(m, rows::Vector{Int}, vals::Vector{Float64})::Void + change_range_value!(m, rows::Vector{Int}, vals::Vector{Float64})::Void A range constraint `l <= a'x <= u` is added as the linear constraint -`a'x :RANGED l`, then this function is called to set `u - l`, the range value. +`a'x [sym] l`, where `sym= lqs_char(m, Interval(l,u))`, then this function is called to set `u - l`, the range value. See `add_linear_constraints!` for more. """ -function lqs_chgrngval!(m::LinQuadOptimizer, rows, vals) end# later +function change_range_value! end +@deprecate lqs_chgrngval! change_range_value! """ set_quadratic_objective!(m, I::Vector{Int}, J::Vector{Int}, V::Vector{Float64})::Void @@ -321,25 +323,28 @@ function get_objectivesense end @deprecate lqs_getobjsen get_objectivesense """ - lqs_mipopt!(m)::Void + solve_mip_problem!(m)::Void Solve a mixed-integer model `m`. """ -function lqs_mipopt!(m::LinQuadOptimizer) end +function solve_mip_problem! end +@deprecate lqs_mipopt! solve_mip_problem! """ - lqs_qpopt!(m)::Void + solve_quadratic_problem!(m)::Void Solve a model `m` with quadratic components. """ -function lqs_qpopt!(m::LinQuadOptimizer) end +function solve_quadratic_problem! end +@deprecate lqs_qpopt! solve_quadratic_problem! """ - lqs_lpopt!(m)::Void + solve_linear_problem!(m)::Void Solve a linear program `m`. """ -function lqs_lpopt!(m::LinQuadOptimizer) end +function solve_linear_problem! end +@deprecate lqs_lpopt! solve_linear_problem! """ get_variable_primal_solution!(m, x::Vector{Float64}) @@ -405,8 +410,13 @@ Get the objective value of the solved model `m`. function get_objective_value end @deprecate lqs_getobjval get_objective_value -# TODO(@joaquimg): what is this? -function lqs_getbestobjval(m::LinQuadOptimizer) end +""" + get_objective_bound(m) + +Get the objective bound of the model `m`. +""" +function get_objective_bound end +@deprecate lqs_getbestobjval get_objective_bound """ get_relative_mip_gap(m) From 77b95f327594be3157871295223efa413c87050b Mon Sep 17 00:00:00 2001 From: odow Date: Mon, 7 May 2018 21:49:01 +1200 Subject: [PATCH 05/10] More Renamings --- src/LinQuadOptInterface.jl | 2 +- src/constraints/singlevariable.jl | 2 +- src/solver_interface.jl | 19 +++++++++++++++---- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/LinQuadOptInterface.jl b/src/LinQuadOptInterface.jl index eafedda..407d7c8 100644 --- a/src/LinQuadOptInterface.jl +++ b/src/LinQuadOptInterface.jl @@ -216,7 +216,7 @@ function MOI.isempty(m::LinQuadOptimizer) end function MOI.empty!(m::M, env = nothing) where M<:LinQuadOptimizer m.name = "" - m.inner = LinQuadModel(M,env) + m.inner = LinearQuadraticModel(M,env) m.obj_is_quad = false # we assume the default is minimization diff --git a/src/constraints/singlevariable.jl b/src/constraints/singlevariable.jl index 4b89a7b..a4c4efd 100644 --- a/src/constraints/singlevariable.jl +++ b/src/constraints/singlevariable.jl @@ -20,7 +20,7 @@ constrdict(m::LinQuadOptimizer, ::SVCI{MOI.ZeroOne}) = cmap(m).binary constrdict(m::LinQuadOptimizer, ::SVCI{MOI.Integer}) = cmap(m).integer function setvariablebound!(m::LinQuadOptimizer, col::Int, bound::Float64, sense::Cchar) - lqs_chgbds!(m, [col], [bound], [sense]) + change_variable_bounds!(m, [col], [bound], [sense]) end function setvariablebound!(m::LinQuadOptimizer, v::SinVar, set::LE) diff --git a/src/solver_interface.jl b/src/solver_interface.jl index 2678502..c6fc262 100644 --- a/src/solver_interface.jl +++ b/src/solver_interface.jl @@ -72,12 +72,13 @@ lqs_char(m::LinQuadOptimizer, ::Val{:Upperbound}) = Cchar('U') lqs_char(m::LinQuadOptimizer, ::Val{:Lowerbound}) = Cchar('L') """ - LinQuadModel(M::Type{<:LinQuadOptimizer}, env) + LinearQuadraticModel(M::Type{<:LinQuadOptimizer}, env) Initializes a model given a model type `M` and an `env` that might be a `nothing` for some solvers. """ -function LinQuadModel end +function LinearQuadraticModel end +@deprecate LinQuadModel LinearQuadraticModel """ supported_constraints(m)::Vector{ @@ -103,7 +104,15 @@ function supported_objectives end # Constraints -function lqs_chgbds!(m::LinQuadOptimizer, colvec, valvec, sensevec) end +""" + change_variable_bounds!(m, cols::Vector{Int}, values::Vector{Float64}, senses::Vector) + +Change the bounds of the variable. The sense of the upperbound +is given by `lqs_char(m, Val{:Upperbound}())`. The sense +of the lowerbound is given by `lqs_char(m, Val{:Lowerbound}())` +""" +function change_variable_bounds! end +@deprecate lqs_chgbds! change_variable_bounds! """ get_variable_lowerbound(m, col::Int)::Float64 @@ -188,7 +197,7 @@ function delete_linear_constraints! end Change the variable types. Type is the output of one of: - `lqs_char(m, ::ZeroOne)`, for binary variables; - `lqs_char(m, ::Integer)`, for integer variables; and - - `lqs_char(m, Val{:Continuous})`, for continuous variables. + - `lqs_char(m, Val{:Continuous}())`, for continuous variables. """ function change_variable_types! end @deprecate lqs_chgctype! change_variable_types! @@ -214,6 +223,7 @@ If an explicit call is needed to change the problem type integer (e.g., CPLEX). function make_problem_type_integer(m::LinQuadOptimizer) nothing # default end +@deprecate lqs_make_problem_type_integer make_problem_type_integer """ make_problem_type_continuous(m)::Void @@ -223,6 +233,7 @@ If an explicit call is needed to change the problem type continuous (e.g., CPLEX function make_problem_type_continuous(m::LinQuadOptimizer) nothing # default end +@deprecate lqs_make_problem_type_continuous make_problem_type_continuous """ add_sos_constraint!(m, cols::Vector{Int}, vals::Vector{Float64}, typ::Symbol)::Void From d08d7681f7296561e84ace6a85ab9af122be6536 Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 8 May 2018 10:22:59 +1200 Subject: [PATCH 06/10] More Renamings --- src/solve.jl | 12 +++++----- src/solver_interface.jl | 50 +++++++++++++++++++++-------------------- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/src/solve.jl b/src/solve.jl index 4d5b664..db03e36 100644 --- a/src/solve.jl +++ b/src/solve.jl @@ -28,9 +28,9 @@ function MOI.optimize!(m::LinQuadOptimizer) m.solvetime = time() - t # termination_status - m.termination_status = get_terminationstatus(m) - m.primal_status = get_primalstatus(m) - m.dual_status = get_dualstatus(m) + m.termination_status = get_termination_status(m) + m.primal_status = get_primal_status(m) + m.dual_status = get_dual_status(m) if m.primal_status in [MOI.FeasiblePoint, MOI.InfeasiblePoint] # primal solution exists @@ -42,12 +42,12 @@ function MOI.optimize!(m::LinQuadOptimizer) m.primal_result_count = 1 # CPLEX can return infeasible points elseif m.primal_status == MOI.InfeasibilityCertificate - get_unboundedray!(m, m.variable_primal_solution) + get_unbounded_ray!(m, m.variable_primal_solution) m.primal_result_count = 1 end if m.dual_status in [MOI.FeasiblePoint, MOI.InfeasiblePoint] # dual solution exists - get_reducedcosts!(m, m.variable_dual_solution) + get_variable_dual_solution!(m, m.variable_dual_solution) get_linear_dual_solution!(m, m.constraint_dual_solution) if hasquadratic(m) get_quadratic_dual_solution!(m, m.qconstraint_dual_solution) @@ -55,7 +55,7 @@ function MOI.optimize!(m::LinQuadOptimizer) m.dual_result_count = 1 # dual solution may not be feasible elseif m.dual_status == MOI.InfeasibilityCertificate - get_farkasdual!(m, m.constraint_dual_solution) + get_farkas_dual!(m, m.constraint_dual_solution) m.dual_result_count = 1 end diff --git a/src/solver_interface.jl b/src/solver_interface.jl index c6fc262..eddb42e 100644 --- a/src/solver_interface.jl +++ b/src/solver_interface.jl @@ -283,7 +283,8 @@ function add_quadratic_constraint! end change_range_value!(m, rows::Vector{Int}, vals::Vector{Float64})::Void A range constraint `l <= a'x <= u` is added as the linear constraint -`a'x [sym] l`, where `sym= lqs_char(m, Interval(l,u))`, then this function is called to set `u - l`, the range value. +`a'x [sym] l`, where `sym= lqs_char(m, Interval(l,u))`, then this function is +called to set `u - l`, the range value. See `add_linear_constraints!` for more. """ @@ -387,13 +388,13 @@ function get_quadratic_primal_solution! end @deprecate lqs_getqcax! get_quadratic_primal_solution! """ - get_reducedcosts!(m, x::Vector{Float64}) + get_variable_dual_solution!(m, x::Vector{Float64}) Get the dual solution (reduced-costs) for the variables in the model `m`, and store in `x`. `x`must have one element for each variable. """ -function get_reducedcosts! end -@deprecate lqs_getdj! get_reducedcosts! +function get_variable_dual_solution! end +@deprecate lqs_getdj! get_variable_dual_solution! """ get_linear_dual_solution!(m, x::Vector{Float64}) @@ -465,47 +466,48 @@ function get_node_count end @deprecate lqs_getnodecnt get_node_count """ - get_farkasdual!(m, x::Vector{Float64}) + get_farkas_dual!(m, x::Vector{Float64}) -Get the farkas dual (certificate of primal infeasiblility) for the linear constraints -in the model `m`, and store in `x`. `x`must have one element for each linear -constraint. +Get the farkas dual (certificate of primal infeasiblility) for the linear +constraints in the model `m`, and store in `x`. `x`must have one element for +each linear constraint. """ -function get_farkasdual! end -@deprecate lqs_dualfarkas! get_farkasdual! +function get_farkas_dual! end +@deprecate lqs_dualfarkas! get_farkas_dual! """ - get_unboundedray!(m, x::Vector{Float64}) + get_unbounded_ray!(m, x::Vector{Float64}) -Get the unbounded ray (certificate of dual infeasiblility) for the linear constraints -in the model `m`, and store in `x`. `x`must have one element for each variable. +Get the unbounded ray (certificate of dual infeasiblility) for the linear +constraints in the model `m`, and store in `x`. `x`must have one element for +each variable. """ -function get_unboundedray! end -@deprecate lqs_getray! get_unboundedray! +function get_unbounded_ray! end +@deprecate lqs_getray! get_unbounded_ray! """ - get_terminationstatus(m) + get_termination_status(m) Get the termination status of the model `m`. """ -function get_terminationstatus end -@deprecate lqs_terminationstatus get_terminationstatus +function get_termination_status end +@deprecate lqs_terminationstatus get_termination_status """ - get_primalstatus(m) + get_primal_status(m) Get the primal status of the model `m`. """ -function get_primalstatus end -@deprecate lqs_primalstatus get_primalstatus +function get_primal_status end +@deprecate lqs_primalstatus get_primal_status """ - get_dualstatus(m) + get_dual_status(m) Get the dual status of the model `m`. """ -function get_dualstatus end -@deprecate lqs_dualstatus get_dualstatus +function get_dual_status end +@deprecate lqs_dualstatus get_dual_status """ get_number_variables(m)::Int From 7984a195d95ade5e35b9e6991d402f256addafda Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 8 May 2018 10:54:39 +1200 Subject: [PATCH 07/10] Add explict ranged constraint methods --- src/constraints/scalaraffine.jl | 39 +++++++++++++++++++--------- src/solver_interface.jl | 46 +++++++++++++++++++++------------ 2 files changed, 57 insertions(+), 28 deletions(-) diff --git a/src/constraints/scalaraffine.jl b/src/constraints/scalaraffine.jl index e5c14e8..30ae8e7 100644 --- a/src/constraints/scalaraffine.jl +++ b/src/constraints/scalaraffine.jl @@ -27,8 +27,7 @@ function addlinearconstraint!(m::LinQuadOptimizer, func::Linear, set::S) where S end function addlinearconstraint!(m::LinQuadOptimizer, func::Linear, set::IV) - addlinearconstraint!(m, func, lqs_char(m,set), set.lower) - change_range_value!(m, [get_number_linear_constraints(m)], [set.upper - set.lower]) + add_ranged_constraints!(m, [1], getcol.(m, func.variables), func.coefficients, [set.lower], [set.upper]) end function addlinearconstraint!(m::LinQuadOptimizer, func::Linear, sense::Cchar, rhs) @@ -68,10 +67,31 @@ function addlinearconstraints!(m::LinQuadOptimizer, func::Vector{Linear}, set::V end function addlinearconstraints!(m::LinQuadOptimizer, func::Vector{Linear}, set::Vector{IV}) - numrows = get_number_linear_constraints(m) - addlinearconstraints!(m, func, lqs_char.(m,set), [s.lower for s in set]) - numrows2 = get_number_linear_constraints(m) - change_range_value!(m, collect(numrows+1:numrows2), [s.upper - s.lower for s in set]) + # loop through once to get number of non-zeros and to move rhs across + lowerbounds = [s.lower for s in set] + upperbounds = [s.upper for s in set] + nnz = 0 + for (i, f) in enumerate(func) + if abs(f.constant) > eps(Float64) + warn("Constant in scalar function moved into set.") + lowerbounds[i] -= f.constant + upperbounds[i] -= f.constant + end + nnz += length(f.coefficients) + end + row_starts = Vector{Int}(length(func)) # index of start of each row + column_indices = Vector{Int}(nnz) # flattened columns for each function + coefficients = Vector{Float64}(nnz) # corresponding non-zeros + i = 1 + for (fi, f) in enumerate(func) + row_starts[fi] = cnt + for (var, coef) in zip(f.variables, f.coefficients) + column_indices[i] = getcol(m, var) + coefficients[i] = coef + i += 1 + end + end + add_ranged_constraints!(m, row_starts, column_indices, coefficients, lowerbounds, upperbounds) end function addlinearconstraints!(m::LinQuadOptimizer, func::Vector{Linear}, sense::Vector{Cchar}, rhs::Vector{Float64}) @@ -142,12 +162,7 @@ end MOI.canmodifyconstraint(m::LinQuadOptimizer, c::LCI{IV}, ::Type{IV}) = true function MOI.modifyconstraint!(m::LinQuadOptimizer, c::LCI{IV}, set::IV) - # the column 0 (or -1 in 0-index) is the rhs. - # a range constraint has the RHS value of the lower limit of the range, and - # a rngval equal to upper-lower. - row = m[c] - change_coefficient!(m, row, 0, set.lower) - change_range_value!(m, [row], [set.upper - set.lower]) + modify_ranged_constraints!(m, [m[c]], [set.lower], [set.upper]) end #= diff --git a/src/solver_interface.jl b/src/solver_interface.jl index eddb42e..ba681a9 100644 --- a/src/solver_interface.jl +++ b/src/solver_interface.jl @@ -35,7 +35,6 @@ Three are special cases: MOI.GreaterThan - 'G' MOI.LessThan - 'L' MOI.EqualTo - 'E' - MOI.Interval - 'R' MOI.Zeros - 'E' MOI.Nonpositives - 'L' @@ -54,7 +53,8 @@ Three are special cases: lqs_char(m::LinQuadOptimizer, ::MOI.GreaterThan{T}) where T = Cchar('G') lqs_char(m::LinQuadOptimizer, ::MOI.LessThan{T}) where T = Cchar('L') lqs_char(m::LinQuadOptimizer, ::MOI.EqualTo{T}) where T = Cchar('E') -lqs_char(m::LinQuadOptimizer, ::MOI.Interval{T}) where T = Cchar('R') +# Implemented separately +# lqs_char(m::LinQuadOptimizer, ::MOI.Interval{T}) where T = Cchar('R') lqs_char(m::LinQuadOptimizer, ::MOI.Zeros) = Cchar('E') lqs_char(m::LinQuadOptimizer, ::MOI.Nonpositives) = Cchar('L') @@ -150,11 +150,38 @@ The A matrix is given in triplet form `A[rows[i], cols[i]] = coef[i]` for all The `sense` is given by `lqs_char(m, set)`. -Ranged constraints (`set=MOI.Interval`) require a call to `change_range_value!`. +Ranged constraints (`set=MOI.Interval`) should be added via `add_ranged_constraint!` +instead. """ function add_linear_constraints! end @deprecate lqs_addrows! add_linear_constraints! +""" + add_ranged_constraint!(m, rows::Vector{Int}, cols::Vector{Int}, + coefs::Vector{Float64}, lowerbound::Vector{Float64}, upperbound::Vector{Float64}) + +Adds linear constraints of the form `lowerbound <= Ax <= upperbound` to the +model `m`. + +The A matrix is given in triplet form `A[rows[i], cols[i]] = coef[i]` for all +`i`, + +This is a special case compared to standard `add_linear_constraints!` since it +is often implemented via multiple API calls. +""" +function add_ranged_constraints! end + +""" + modify_ranged_constraint!(m, rows::Vector{Int}, lowerbound::Vector{Float64}, upperbound::Vector{Float64}) + +Modify the lower and upperbounds of a ranged constraint in the model `m`. + +This is a special case compared to standard the `change_coefficient!` since it +is often implemented via multiple API calls. +""" +function modify_ranged_constraints! end + + """ get_rhs(m, row::Int)::Float64 @@ -278,19 +305,6 @@ Arguments `(I,J,V)` given in triplet form for the Q matrix in `0.5 x' Q x`. """ function add_quadratic_constraint! end - -""" - change_range_value!(m, rows::Vector{Int}, vals::Vector{Float64})::Void - -A range constraint `l <= a'x <= u` is added as the linear constraint -`a'x [sym] l`, where `sym= lqs_char(m, Interval(l,u))`, then this function is -called to set `u - l`, the range value. - -See `add_linear_constraints!` for more. -""" -function change_range_value! end -@deprecate lqs_chgrngval! change_range_value! - """ set_quadratic_objective!(m, I::Vector{Int}, J::Vector{Int}, V::Vector{Float64})::Void From 247ece96865a9ab1c48cb1939906d37f7a598cea Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 8 May 2018 11:30:19 +1200 Subject: [PATCH 08/10] Add MOI to REQUIRE --- REQUIRE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/REQUIRE b/REQUIRE index f953fd5..c2decc7 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,2 +1,2 @@ julia 0.6 -MathOptInterface +MathOptInterface 0.2 0.3 From e3cc806d354eee1953b9c72d645937ca4fbfb46a Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 8 May 2018 12:09:56 +1200 Subject: [PATCH 09/10] More renamings --- src/LinQuadOptInterface.jl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/LinQuadOptInterface.jl b/src/LinQuadOptInterface.jl index 407d7c8..12fc00f 100644 --- a/src/LinQuadOptInterface.jl +++ b/src/LinQuadOptInterface.jl @@ -149,10 +149,10 @@ abstract type LinQuadOptimizer <: MOI.AbstractOptimizer end obj_sense::MOI.OptimizationSense last_variable_reference::UInt64 - variable_mapping::Dict{MathOptInterface.VariableIndex, Int} - variable_names::Dict{MathOptInterface.VariableIndex, String} - variable_names_rev::Dict{String, MathOptInterface.VariableIndex} - variable_references::Vector{MathOptInterface.VariableIndex} + variable_mapping::Dict{MOI.VariableIndex, Int} + variable_names::Dict{MOI.VariableIndex, String} + variable_names_rev::Dict{String, MOI.VariableIndex} + variable_references::Vector{MOI.VariableIndex} variable_primal_solution::Vector{Float64} variable_dual_solution::Vector{Float64} @@ -173,9 +173,9 @@ abstract type LinQuadOptimizer <: MOI.AbstractOptimizer end objective_constant::Float64 - termination_status::MathOptInterface.TerminationStatusCode - primal_status::MathOptInterface.ResultStatusCode - dual_status::MathOptInterface.ResultStatusCode + termination_status::MOI.TerminationStatusCode + primal_status::MOI.ResultStatusCode + dual_status::MOI.ResultStatusCode primal_result_count::Int dual_result_count::Int From cfc4e5d1798a180d67690af93980cbe58128397e Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 8 May 2018 14:00:34 +1200 Subject: [PATCH 10/10] Last renaming --- src/constraints/scalaraffine.jl | 6 +-- src/constraints/scalarquadratic.jl | 2 +- src/constraints/singlevariable.jl | 28 +++++++------- src/constraints/vectoraffine.jl | 2 +- src/constraints/vectorofvariables.jl | 6 +-- src/solver_interface.jl | 56 ++++++++++++++-------------- 6 files changed, 50 insertions(+), 50 deletions(-) diff --git a/src/constraints/scalaraffine.jl b/src/constraints/scalaraffine.jl index 30ae8e7..e858610 100644 --- a/src/constraints/scalaraffine.jl +++ b/src/constraints/scalaraffine.jl @@ -23,7 +23,7 @@ function MOI.addconstraint!(m::LinQuadOptimizer, func::Linear, set::T) where T < end function addlinearconstraint!(m::LinQuadOptimizer, func::Linear, set::S) where S <: Union{LE, GE, EQ} - addlinearconstraint!(m, func, lqs_char(m,set), _getrhs(set)) + addlinearconstraint!(m, func, backend_type(m,set), _getrhs(set)) end function addlinearconstraint!(m::LinQuadOptimizer, func::Linear, set::IV) @@ -63,7 +63,7 @@ function MOI.addconstraints!(m::LinQuadOptimizer, func::Vector{Linear}, set::Vec end function addlinearconstraints!(m::LinQuadOptimizer, func::Vector{Linear}, set::Vector{S}) where S <: LinSets - addlinearconstraints!(m, func, lqs_char.(m,set), [_getrhs(s) for s in set]) + addlinearconstraints!(m, func, backend_type.(m,set), [_getrhs(s) for s in set]) end function addlinearconstraints!(m::LinQuadOptimizer, func::Vector{Linear}, set::Vector{IV}) @@ -204,7 +204,7 @@ end function MOI.transformconstraint!(m::LinQuadOptimizer, ref::LCI{S1}, newset::S2) where S1 where S2 <: Union{LE, GE, EQ} dict = constrdict(m, ref) row = dict[ref] - change_linear_constraint_sense!(m, [row], [lqs_char(m,newset)]) + change_linear_constraint_sense!(m, [row], [backend_type(m,newset)]) m.last_constraint_reference += 1 ref2 = LCI{S2}(m.last_constraint_reference) dict2 = constrdict(m, ref2) diff --git a/src/constraints/scalarquadratic.jl b/src/constraints/scalarquadratic.jl index 18e5419..edd5600 100644 --- a/src/constraints/scalarquadratic.jl +++ b/src/constraints/scalarquadratic.jl @@ -25,7 +25,7 @@ function MOI.addconstraint!(m::LinQuadOptimizer, func::Quad, set::S) where S <: end function addquadraticconstraint!(m::LinQuadOptimizer, func::Quad, set::S) where S<: Union{LE, GE, EQ} - addquadraticconstraint!(m, func, lqs_char(m,set), _getrhs(set)) + addquadraticconstraint!(m, func, backend_type(m,set), _getrhs(set)) end function addquadraticconstraint!(m::LinQuadOptimizer, f::Quad, sense::Cchar, rhs::Float64) diff --git a/src/constraints/singlevariable.jl b/src/constraints/singlevariable.jl index a4c4efd..33f0b4f 100644 --- a/src/constraints/singlevariable.jl +++ b/src/constraints/singlevariable.jl @@ -24,18 +24,18 @@ function setvariablebound!(m::LinQuadOptimizer, col::Int, bound::Float64, sense: end function setvariablebound!(m::LinQuadOptimizer, v::SinVar, set::LE) - setvariablebound!(m, getcol(m, v), set.upper, lqs_char(m, Val{:Upperbound}())) + setvariablebound!(m, getcol(m, v), set.upper, backend_type(m, Val{:Upperbound}())) end function setvariablebound!(m::LinQuadOptimizer, v::SinVar, set::GE) - setvariablebound!(m, getcol(m, v), set.lower, lqs_char(m, Val{:Lowerbound}())) + setvariablebound!(m, getcol(m, v), set.lower, backend_type(m, Val{:Lowerbound}())) end function setvariablebound!(m::LinQuadOptimizer, v::SinVar, set::EQ) - setvariablebound!(m, getcol(m, v), set.value, lqs_char(m, Val{:Upperbound}())) - setvariablebound!(m, getcol(m, v), set.value, lqs_char(m, Val{:Lowerbound}())) + setvariablebound!(m, getcol(m, v), set.value, backend_type(m, Val{:Upperbound}())) + setvariablebound!(m, getcol(m, v), set.value, backend_type(m, Val{:Lowerbound}())) end function setvariablebound!(m::LinQuadOptimizer, v::SinVar, set::IV) - setvariablebound!(m, getcol(m, v), set.upper, lqs_char(m, Val{:Upperbound}())) - setvariablebound!(m, getcol(m, v), set.lower, lqs_char(m, Val{:Lowerbound}())) + setvariablebound!(m, getcol(m, v), set.upper, backend_type(m, Val{:Upperbound}())) + setvariablebound!(m, getcol(m, v), set.lower, backend_type(m, Val{:Lowerbound}())) end # add constraint @@ -109,9 +109,9 @@ function MOI.addconstraint!(m::LinQuadOptimizer, v::SinVar, set::MOI.ZeroOne) ub = get_variable_upperbound(m, getcol(m, v)) lb = get_variable_lowerbound(m, getcol(m, v)) dict[ref] = (v.variable, lb, ub) - change_variable_types!(m, [getcol(m, v)], [lqs_char(m, set)]) - setvariablebound!(m, getcol(m, v), 1.0, lqs_char(m, Val{:Upperbound}())) - setvariablebound!(m, getcol(m, v), 0.0, lqs_char(m, Val{:Lowerbound}())) + change_variable_types!(m, [getcol(m, v)], [backend_type(m, set)]) + setvariablebound!(m, getcol(m, v), 1.0, backend_type(m, Val{:Upperbound}())) + setvariablebound!(m, getcol(m, v), 0.0, backend_type(m, Val{:Lowerbound}())) make_problem_type_integer(m) ref end @@ -121,9 +121,9 @@ function MOI.delete!(m::LinQuadOptimizer, c::SVCI{MOI.ZeroOne}) deleteconstraintname!(m, c) dict = constrdict(m, c) (v, lb, ub) = dict[c] - change_variable_types!(m, [getcol(m, v)], [lqs_char(m, Val{:Continuous}())]) - setvariablebound!(m, getcol(m, v), ub, lqs_char(m, Val{:Upperbound}())) - setvariablebound!(m, getcol(m, v), lb, lqs_char(m, Val{:Lowerbound}())) + change_variable_types!(m, [getcol(m, v)], [backend_type(m, Val{:Continuous}())]) + setvariablebound!(m, getcol(m, v), ub, backend_type(m, Val{:Upperbound}())) + setvariablebound!(m, getcol(m, v), lb, backend_type(m, Val{:Lowerbound}())) delete!(dict, c) if !hasinteger(m) make_problem_type_continuous(m) @@ -141,7 +141,7 @@ MOI.get(m::LinQuadOptimizer, ::MOI.ConstraintFunction, c::SVCI{MOI.ZeroOne}) = m =# function MOI.addconstraint!(m::LinQuadOptimizer, v::SinVar, set::MOI.Integer) - change_variable_types!(m, [getcol(m, v)], [lqs_char(m, set)]) + change_variable_types!(m, [getcol(m, v)], [backend_type(m, set)]) m.last_constraint_reference += 1 ref = SVCI{MOI.Integer}(m.last_constraint_reference) dict = constrdict(m, ref) @@ -154,7 +154,7 @@ function MOI.delete!(m::LinQuadOptimizer, c::SVCI{MOI.Integer}) deleteconstraintname!(m, c) dict = constrdict(m, c) v = dict[c] - change_variable_types!(m, [getcol(m, v)], [lqs_char(m, Val{:Continuous}())]) + change_variable_types!(m, [getcol(m, v)], [backend_type(m, Val{:Continuous}())]) delete!(dict, c) if !hasinteger(m) make_problem_type_continuous(m) diff --git a/src/constraints/vectoraffine.jl b/src/constraints/vectoraffine.jl index 9e6c93c..4c0319b 100644 --- a/src/constraints/vectoraffine.jl +++ b/src/constraints/vectoraffine.jl @@ -9,7 +9,7 @@ function MOI.addconstraint!(m::LinQuadOptimizer, func::VecLin, set::S) where S < @assert MOI.dimension(set) == length(func.constant) nrows = get_number_linear_constraints(m) - addlinearconstraint!(m, func, lqs_char(m,set)) + addlinearconstraint!(m, func, backend_type(m,set)) nrows2 = get_number_linear_constraints(m) m.last_constraint_reference += 1 diff --git a/src/constraints/vectorofvariables.jl b/src/constraints/vectorofvariables.jl index 0289bcc..f677228 100644 --- a/src/constraints/vectorofvariables.jl +++ b/src/constraints/vectorofvariables.jl @@ -21,7 +21,7 @@ function MOI.addconstraint!(m::LinQuadOptimizer, func::VecVar, set::S) where S < ref = VVCI{S}(m.last_constraint_reference) rows = get_number_linear_constraints(m) n = MOI.dimension(set) - add_linear_constraints!(m, collect(1:n), getcol.(m, func.variables), ones(n), fill(lqs_char(m, set),n), zeros(n)) + add_linear_constraints!(m, collect(1:n), getcol.(m, func.variables), ones(n), fill(backend_type(m, set),n), zeros(n)) dict = constrdict(m, ref) dict[ref] = collect(rows+1:rows+n) append!(m.constraint_primal_solution, fill(NaN,n)) @@ -65,7 +65,7 @@ end function MOI.addconstraint!(m::LinQuadOptimizer, v::VecVar, sos::S) where S <: Union{MOI.SOS1, MOI.SOS2} make_problem_type_integer(m) - add_sos_constraint!(m, getcol.(m, v.variables), sos.weights, lqs_char(m, sos)) + add_sos_constraint!(m, getcol.(m, v.variables), sos.weights, backend_type(m, sos)) m.last_constraint_reference += 1 ref = VVCI{S}(m.last_constraint_reference) dict = constrdict(m, ref) @@ -90,7 +90,7 @@ MOI.canget(m::LinQuadOptimizer, ::MOI.ConstraintSet, ::Type{VVCI{S}}) where S <: function MOI.get(m::LinQuadOptimizer, ::MOI.ConstraintSet, c::VVCI{S}) where S <: Union{MOI.SOS1, MOI.SOS2} indices, weights, types = get_sos_constraint(m, m[c]) set = S(weights) - @assert types == lqs_char(m, set) + @assert types == backend_type(m, set) return set end diff --git a/src/solver_interface.jl b/src/solver_interface.jl index ba681a9..b8570f1 100644 --- a/src/solver_interface.jl +++ b/src/solver_interface.jl @@ -18,17 +18,17 @@ =# """ - lqs_char(m, ::MOI.AbstractSet)::CChar + backend_type(m, ::MOI.AbstractSet)::CChar -Return a Cchar specifiying the constraint set sense. +An overloadable type for dispatching the appropriate types to the backends. -Interval constraints are sometimes called range constraints and have the form -`l <= a'x <= u` +For example, GLPK.jl uses `'E'` for `a'x=b` constraints, where as Gurobi.jl uses +`==`. Three are special cases: - - `Val{:Continuous}`: Cchar for a continuous variable - - `Val{:Upperbound}`: Cchar for the upper bound of a variable - - `Val{:Lowerbound}`: Cchar for the lower bound of a variable + - `Val{:Continuous}`: for the type of a continuous variable + - `Val{:Upperbound}`: for the upper bound of a variable + - `Val{:Lowerbound}`: for the lower bound of a variable ### Defaults @@ -50,26 +50,26 @@ Three are special cases: Val{:Upperbound} - 'U' Val{:Lowerbound} - 'L' """ -lqs_char(m::LinQuadOptimizer, ::MOI.GreaterThan{T}) where T = Cchar('G') -lqs_char(m::LinQuadOptimizer, ::MOI.LessThan{T}) where T = Cchar('L') -lqs_char(m::LinQuadOptimizer, ::MOI.EqualTo{T}) where T = Cchar('E') +backend_type(m::LinQuadOptimizer, ::MOI.GreaterThan{T}) where T = Cchar('G') +backend_type(m::LinQuadOptimizer, ::MOI.LessThan{T}) where T = Cchar('L') +backend_type(m::LinQuadOptimizer, ::MOI.EqualTo{T}) where T = Cchar('E') # Implemented separately -# lqs_char(m::LinQuadOptimizer, ::MOI.Interval{T}) where T = Cchar('R') +# backend_type(m::LinQuadOptimizer, ::MOI.Interval{T}) where T = Cchar('R') -lqs_char(m::LinQuadOptimizer, ::MOI.Zeros) = Cchar('E') -lqs_char(m::LinQuadOptimizer, ::MOI.Nonpositives) = Cchar('L') -lqs_char(m::LinQuadOptimizer, ::MOI.Nonnegatives) = Cchar('G') +backend_type(m::LinQuadOptimizer, ::MOI.Zeros) = Cchar('E') +backend_type(m::LinQuadOptimizer, ::MOI.Nonpositives) = Cchar('L') +backend_type(m::LinQuadOptimizer, ::MOI.Nonnegatives) = Cchar('G') -lqs_char(m::LinQuadOptimizer, ::MOI.ZeroOne) = Cchar('B') -lqs_char(m::LinQuadOptimizer, ::MOI.Integer) = Cchar('I') +backend_type(m::LinQuadOptimizer, ::MOI.ZeroOne) = Cchar('B') +backend_type(m::LinQuadOptimizer, ::MOI.Integer) = Cchar('I') -lqs_char(m::LinQuadOptimizer, ::MOI.SOS1{T}) where T = :SOS1 # Cchar('1') -lqs_char(m::LinQuadOptimizer, ::MOI.SOS2{T}) where T = :SOS2 # Cchar('2') +backend_type(m::LinQuadOptimizer, ::MOI.SOS1{T}) where T = :SOS1 # Cchar('1') +backend_type(m::LinQuadOptimizer, ::MOI.SOS2{T}) where T = :SOS2 # Cchar('2') -lqs_char(m::LinQuadOptimizer, ::Val{:Continuous}) = Cchar('C') -lqs_char(m::LinQuadOptimizer, ::Val{:Upperbound}) = Cchar('U') -lqs_char(m::LinQuadOptimizer, ::Val{:Lowerbound}) = Cchar('L') +backend_type(m::LinQuadOptimizer, ::Val{:Continuous}) = Cchar('C') +backend_type(m::LinQuadOptimizer, ::Val{:Upperbound}) = Cchar('U') +backend_type(m::LinQuadOptimizer, ::Val{:Lowerbound}) = Cchar('L') """ LinearQuadraticModel(M::Type{<:LinQuadOptimizer}, env) @@ -108,8 +108,8 @@ function supported_objectives end change_variable_bounds!(m, cols::Vector{Int}, values::Vector{Float64}, senses::Vector) Change the bounds of the variable. The sense of the upperbound -is given by `lqs_char(m, Val{:Upperbound}())`. The sense -of the lowerbound is given by `lqs_char(m, Val{:Lowerbound}())` +is given by `backend_type(m, Val{:Upperbound}())`. The sense +of the lowerbound is given by `backend_type(m, Val{:Lowerbound}())` """ function change_variable_bounds! end @deprecate lqs_chgbds! change_variable_bounds! @@ -148,7 +148,7 @@ Adds linear constraints of the form `Ax (sense) rhs` to the model `m`. The A matrix is given in triplet form `A[rows[i], cols[i]] = coef[i]` for all `i`, and `length(rows) == length(cols) == length(coefs)`. -The `sense` is given by `lqs_char(m, set)`. +The `sense` is given by `backend_type(m, set)`. Ranged constraints (`set=MOI.Interval`) should be added via `add_ranged_constraint!` instead. @@ -222,9 +222,9 @@ function delete_linear_constraints! end lqs_chgctype(m, cols::Vector{Int}, types):Void Change the variable types. Type is the output of one of: - - `lqs_char(m, ::ZeroOne)`, for binary variables; - - `lqs_char(m, ::Integer)`, for integer variables; and - - `lqs_char(m, Val{:Continuous}())`, for continuous variables. + - `backend_type(m, ::ZeroOne)`, for binary variables; + - `backend_type(m, ::Integer)`, for integer variables; and + - `backend_type(m, Val{:Continuous}())`, for continuous variables. """ function change_variable_types! end @deprecate lqs_chgctype! change_variable_types! @@ -234,7 +234,7 @@ function change_variable_types! end Change the sense of the linear constraints in `rows` to `sense`. -`sense` is the output of `lqs_char(m, set)`, where `set` +`sense` is the output of `backend_type(m, set)`, where `set` is the corresponding set for the row `rows[i]`. `Interval` constraints require a call to `change_range_value!`.