Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic tidying of code #222

Merged
merged 2 commits into from
Mar 7, 2024
Merged
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
10 changes: 8 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ version = "0.16.2"
[deps]
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[compat]
Downloads = "<0.0.1, 1.6"
odow marked this conversation as resolved.
Show resolved Hide resolved
Libdl = "<0.0.1, 1.6"
LinearAlgebra = "<0.0.1, 1.6"
MathOptInterface = "1"
Random = "<0.0.1, 1.6"
SparseArrays = "<0.0.1, 1.6"
Test = "<0.0.1, 1.6"
julia = "1.6"

[extras]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "Random"]
test = ["LinearAlgebra", "Random", "Test"]
67 changes: 29 additions & 38 deletions src/MOI/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.

@static if v"1.2" > VERSION >= v"1.1"
# see: https://github.com/jump-dev/Xpress.jl/pull/44#issuecomment-585882858
error("Versions 1.1.x of julia are not supported. The current verions is $(VERSION)")
end

import MathOptInterface
using SparseArrays

const MOI = MathOptInterface
const CleverDicts = MOI.Utilities.CleverDicts

@enum(
Expand Down Expand Up @@ -161,7 +152,7 @@
var_status::Vector{Cint}
end

mutable struct SensitivityCache
mutable struct SensitivityCache
input::Vector{Float64}
output::Vector{Float64}
is_updated::Bool
Expand Down Expand Up @@ -240,7 +231,7 @@

# TODO: add functionality to the lower-level API to support querying single
# elements of the solution.

cached_solution::Union{Nothing, CachedSolution}
basis_status::Union{Nothing, BasisStatus}
conflict::Union{Nothing, IISData}
Expand Down Expand Up @@ -391,7 +382,7 @@
model.termination_status != MOI.OPTIMIZE_NOT_CALLED && return false
model.primal_status != MOI.NO_SOLUTION && return false
model.dual_status != MOI.NO_SOLUTION && return false

model.callback_cached_solution !== nothing && return false
# model.cb_cut_data !== nothing && return false
model.callback_state != CB_NONE && return false
Expand Down Expand Up @@ -816,7 +807,7 @@
0,#length(_dmatval)::Int,
Ref(0.0),#_dobj::Vector{Float64},
C_NULL,#Cint.(_mrwind::Vector{Int}),
C_NULL,#Cint.(_mrstart::Vector{Int}),
C_NULL,#Cint.(_mrstart::Vector{Int}),
C_NULL,#_dmatval::Vector{Float64},
Ref(-Inf),#_dbdl::Vector{Float64},
Ref(Inf),#_dbdu::Vector{Float64}
Expand All @@ -831,7 +822,7 @@
0,#length(_dmatval)::Int,
zeros(N),# _dobj::Vector{Float64},
C_NULL,#Cint.(_mrwind::Vector{Int}),
C_NULL,#Cint.(_mrstart::Vector{Int}),
C_NULL,#Cint.(_mrstart::Vector{Int}),
C_NULL,# _dmatval::Vector{Float64},
fill(-Inf, N),# _dbdl::Vector{Float64},
fill(Inf, N),# _dbdu::Vector{Float64}
Expand Down Expand Up @@ -1005,7 +996,7 @@
rows = @_invoke Lib.XPRSgetintattrib(model.inner, Lib.XPRS_ROWS, _)::Int
cols = @_invoke Lib.XPRSgetintattrib(model.inner, Lib.XPRS_COLS, _)::Int
if model.forward_sensitivity_cache === nothing
model.forward_sensitivity_cache =
model.forward_sensitivity_cache =
SensitivityCache(
zeros(rows),
zeros(cols),
Expand All @@ -1023,10 +1014,10 @@
if is_mip(model) && model.moi_warnings
@warn "The problem is a MIP, it might fail to get correct sensitivities."
end
if MOI.get(model, MOI.TerminationStatus()) != MOI.OPTIMAL
if MOI.get(model, MOI.TerminationStatus()) != MOI.OPTIMAL
error("Model not optimized. Cannot get sensitivities.")
end
if model.forward_sensitivity_cache === nothing
if model.forward_sensitivity_cache === nothing
error("Forward sensitivity cache not initiliazed correctly.")
end
if model.forward_sensitivity_cache.is_updated != true
Expand All @@ -1042,7 +1033,7 @@
rows = @_invoke Lib.XPRSgetintattrib(model.inner, Lib.XPRS_ROWS, _)::Int
cols = @_invoke Lib.XPRSgetintattrib(model.inner, Lib.XPRS_COLS, _)::Int
if model.backward_sensitivity_cache === nothing
model.backward_sensitivity_cache =
model.backward_sensitivity_cache =
SensitivityCache(
zeros(cols),
zeros(rows),
Expand All @@ -1060,10 +1051,10 @@
if is_mip(model) && model.moi_warnings
@warn "The problem is a MIP, it might fail to get correct sensitivities."
end
if MOI.get(model, MOI.TerminationStatus()) != MOI.OPTIMAL
if MOI.get(model, MOI.TerminationStatus()) != MOI.OPTIMAL
error("Model not optimized. Cannot get sensitivities.")
end
if model.backward_sensitivity_cache === nothing
if model.backward_sensitivity_cache === nothing
error("Backward sensitivity cache not initiliazed correctly.")
end
if model.backward_sensitivity_cache.is_updated != true
Expand Down Expand Up @@ -1623,7 +1614,7 @@
function _set_variable_semi_lower_bound(model, info, value)
info.semi_lower_bound = value
_set_variable_lower_bound(model, info, 0.0)
@checked Lib.XPRSchgglblimit(model.inner, Cint(1), Ref(Cint(info.column-1)),
@checked Lib.XPRSchgglblimit(model.inner, Cint(1), Ref(Cint(info.column-1)),
Ref(Cdouble(value)))
end

Expand Down Expand Up @@ -2045,8 +2036,8 @@
Ref{UInt8}(sense),#_srowtype,
Ref(rhs),#_drhs,
C_NULL,#_drng,
Ref{Cint}(0),#Cint.(_mrstart::Vector{Int}),
indices.-= 1,#Cint.(_mclind::Vector{Int}),
Ref{Cint}(0),#Cint.(_mrstart::Vector{Int}),
indices.-= 1,#Cint.(_mclind::Vector{Int}),
coefficients#_dmatval
)
return MOI.ConstraintIndex{typeof(f), typeof(s)}(model.last_constraint_index)
Expand Down Expand Up @@ -2090,16 +2081,16 @@
model.affine_constraint_info[model.last_constraint_index] =
ConstraintInfo(length(model.affine_constraint_info) + 1, si, AFFINE)
end
pop!(row_starts)
pop!(row_starts)
@checked Lib.XPRSaddrows(
model.inner,
length(rhss),#length(_drhs),
Cint(length(columns)),#Cint(length(_mclind)),
senses,#_srowtype,
rhss,#_drhs,
C_NULL,#_drng,
row_starts.-= 1,#Cint.(_mrstart::Vector{Int}),
columns.-= 1,#Cint.(_mclind::Vector{Int}),
row_starts.-= 1,#Cint.(_mrstart::Vector{Int}),
columns.-= 1,#Cint.(_mclind::Vector{Int}),
coefficients#_dmatval
)
return indices
Expand Down Expand Up @@ -2147,7 +2138,7 @@
@checked Lib.XPRSchgrhs(
model.inner,
Cint(1),
Ref(Cint(_info(model, c).row - 1)),
Ref(Cint(_info(model, c).row - 1)),
Ref(MOI.constant(s)))
return
end
Expand All @@ -2157,7 +2148,7 @@
nzcnt_max = Xpress.n_non_zero_elements(model.inner)

_nzcnt = Ref(Cint(0))
@checked Lib.XPRSgetrows(model.inner, C_NULL, C_NULL, C_NULL, 0, _nzcnt,
@checked Lib.XPRSgetrows(model.inner, C_NULL, C_NULL, C_NULL, 0, _nzcnt,
Cint(row-1), Cint(row-1))
nzcnt = _nzcnt[]

Expand Down Expand Up @@ -2392,8 +2383,8 @@
Ref{UInt8}(sense),#_srowtype,
Ref(rhs-cte),#_drhs,
C_NULL,#_drng,
Ref{Cint}(0),#Cint.(_mrstart::Vector{Int}),
indices.-= 1,#Cint.(_mrwind::Vector{Int}),
Ref{Cint}(0),#Cint.(_mrstart::Vector{Int}),
indices.-= 1,#Cint.(_mrwind::Vector{Int}),
coefficients#_dmatval
)
@checked Lib.XPRSsetindicators(model.inner,
Expand Down Expand Up @@ -2494,7 +2485,7 @@
@checked Lib.XPRSgetqrowqmatrixtriplets(model.inner, _info(model, c).row-1, nqelem, mqcol1, mqcol2, dqe)
mqcol1 .+= 1
mqcol2 .+= 1

quadratic_terms = MOI.ScalarQuadraticTerm{Float64}[]
for (i, j, coef) in zip(mqcol1, mqcol2, dqe)
push!(
Expand Down Expand Up @@ -2617,7 +2608,7 @@
V[j] = setvals[j]
end
end
return sparse(I, J, V, nsets[], n)
return SparseArrays.sparse(I, J, V, nsets[], n)
end

function MOI.get(
Expand Down Expand Up @@ -2799,7 +2790,7 @@
has_Ray = Int64[0]
@checked Lib.XPRSgetprimalray(model.inner, model.cached_solution.variable_primal, has_Ray)
model.cached_solution.has_primal_certificate = _has_primal_ray(model)
elseif status == MOI.FEASIBLE_POINT
elseif status == MOI.FEASIBLE_POINT
model.cached_solution.has_feasible_point = true
end
status = MOI.get(model, MOI.DualStatus())
Expand Down Expand Up @@ -3021,7 +3012,7 @@
end
end
return MOI.NO_SOLUTION

end

function MOI.get(model::Optimizer, attr::MOI.DualStatus)
Expand Down Expand Up @@ -3554,7 +3545,7 @@
for term in replacement.terms
col = _info(model, term.variable).column
@checked Lib.XPRSchgcoef(
model.inner, Cint(row-1), Cint(col-1),
model.inner, Cint(row-1), Cint(col-1),
MOI.coefficient(term)
)
end
Expand Down Expand Up @@ -3587,14 +3578,14 @@
# First, zero out the old constraint function terms.
for term in previous.terms
col = _info(model, term.variable).column
@checked Lib.XPRSchgcoef(model.inner, Cint(row - 1), Cint(col - 1),
@checked Lib.XPRSchgcoef(model.inner, Cint(row - 1), Cint(col - 1),

Check warning on line 3581 in src/MOI/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI/MOI_wrapper.jl#L3581

Added line #L3581 was not covered by tests
0.0)
end

# Next, set the new constraint function terms.
for term in previous.terms
col = _info(model, term.variable).column
@checked Lib.XPRSchgcoef(model.inner, Cint(row - 1), Cint(col - 1),
@checked Lib.XPRSchgcoef(model.inner, Cint(row - 1), Cint(col - 1),

Check warning on line 3588 in src/MOI/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI/MOI_wrapper.jl#L3588

Added line #L3588 was not covered by tests
MOI.coefficient(term))
end
return
Expand Down Expand Up @@ -4432,4 +4423,4 @@
MOI.check_result_index_bounds(model, attr)
column = _info(model, vi).column
return model.cached_solution.variable_dual[column]
end
end
107 changes: 50 additions & 57 deletions src/Xpress.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,64 @@
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.

__precompile__()

module Xpress

using Libdl

# Load in `deps.jl`, complaining if it does not exist
const depsjl_path = joinpath(@__DIR__, "..", "deps", "deps.jl")

if isfile(depsjl_path)
include(depsjl_path)
elseif !haskey(ENV, "XPRESS_JL_NO_DEPS_ERROR")
error("XPRESS cannot be loaded. Please run Pkg.build(\"Xpress\").")
else
const xpressdlpath = ""
end

const libxprs = joinpath(xpressdlpath, string(Sys.iswindows() ? "" : "lib", "xprs", ".", Libdl.dlext))

### imports

import Base.show, Base.copy
import Libdl
import MathOptInterface as MOI
import SparseArrays

# ### exports
# Load in `deps.jl`, complaining if it does not exist
const depsjl_path = joinpath(@__DIR__, "..", "deps", "deps.jl")

### include source files
module Lib
import ..Xpress
const libxprs = Xpress.libxprs
include("ctypes.jl")
include("common.jl")
include("lib.jl")
end
if isfile(depsjl_path)
include(depsjl_path)
elseif !haskey(ENV, "XPRESS_JL_NO_DEPS_ERROR")
error("XPRESS cannot be loaded. Please run Pkg.build(\"Xpress\").")
else
const xpressdlpath = ""
end

include("utils.jl")
include("helper.jl")
include("attributes_controls.jl")
include("api.jl")
include("xprs_callbacks.jl")
include("license.jl")
const libxprs = joinpath(xpressdlpath, string(Sys.iswindows() ? "" : "lib", "xprs", ".", Libdl.dlext))

const XPRS_ATTRIBUTES = Dict{String, Any}(
replace(string(name), "XPRS_"=>"") => getfield(Lib, name)
for name in names(Lib; all = true)
if startswith(string(name), "XPRS_") && all(isuppercase(c) || isdigit(c) for c in string(name) if c != '_')
)
module Lib
import ..Xpress
const libxprs = Xpress.libxprs
include("ctypes.jl")
include("common.jl")
include("lib.jl")
end

function initialize()
Libdl.dlopen(libxprs)
userlic()
init() # Call XPRSinit for initialization
# free is not strictly necessary since destroyprob is called
# inthe finalizer.
# the user can call free if needed.
# leaving it uncommented results in many print errors becaus
# it is called prior to finalizers.
# atexit(free) # Call free when process terminates
end
include("utils.jl")
include("helper.jl")
include("attributes_controls.jl")
include("api.jl")
include("xprs_callbacks.jl")
include("license.jl")

const XPRS_ATTRIBUTES = Dict{String, Any}(
replace(string(name), "XPRS_"=>"") => getfield(Lib, name)
for name in names(Lib; all = true)
if startswith(string(name), "XPRS_") && all(isuppercase(c) || isdigit(c) for c in string(name) if c != '_')
)

function initialize()
Libdl.dlopen(libxprs)
userlic()
init() # Call XPRSinit for initialization
# free is not strictly necessary since destroyprob is called
# inthe finalizer.
# the user can call free if needed.
# leaving it uncommented results in many print errors becaus
# it is called prior to finalizers.
# atexit(free) # Call free when process terminates
end

include("MOI/MOI_wrapper.jl")
include("MOI/MOI_wrapper.jl")

function __init__()
if !haskey(ENV, "XPRESS_JL_NO_AUTO_INIT") && get(ENV, "JULIA_REGISTRYCI_AUTOMERGE", "false") != "true"
Xpress.initialize()
end
function __init__()
if !haskey(ENV, "XPRESS_JL_NO_AUTO_INIT") && get(ENV, "JULIA_REGISTRYCI_AUTOMERGE", "false") != "true"
Xpress.initialize()
end

end

end # Xpress
Loading
Loading