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 #213

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ version = "0.16.2"
[deps]
LazyArtifacts = "4af54fe1-eca0-43a8-85a7-787d91b784e3"
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]
LazyArtifacts = "<0.0.1, 1.6"
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"
Expand Down
13 changes: 0 additions & 13 deletions deps/build.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,6 @@ if isfile(depsfile)
rm(depsfile)
end


function my_download(url::AbstractString, filename::AbstractString)
if Sys.iswindows() && VERSION < v"1.1"
# from https://github.com/JuliaLang/julia/blob/788b2c77c10c2160f4794a4d4b6b81a95a90940c/base/download.jl#L26
# otherwise files are not properly downloaded - check sizes
curl_exe = joinpath(get(ENV, "SYSTEMROOT", "C:\\Windows"), "System32\\curl.exe")
process = run(`$curl_exe -s -S -g -L -f -o $filename $url`)#, wait=false)
return filename
else
return download(url, filename)
end
end

function write_depsfile(path)
f = open(depsfile,"w")
if Sys.iswindows()
Expand Down
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
4 changes: 2 additions & 2 deletions src/Xpress.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Xpress

import LazyArtifacts
import Libdl
import MathOptInterface as MOI
import SparseArrays

# Load in `deps.jl`, complaining if it does not exist
const depsjl_path = joinpath(@__DIR__, "..", "deps", "deps.jl")
Expand Down Expand Up @@ -43,8 +45,6 @@ else
error("XPRESS cannot be loaded. Please run Pkg.build(\"Xpress\").")
end

import Base.show, Base.copy

module Lib
import ..Xpress
const libxprs = Xpress.libxprs
Expand Down
15 changes: 7 additions & 8 deletions test/Derivative/DerivativeExample.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
# 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.

using MathOptInterface
using Xpress
using Test

const MOI = MathOptInterface
import MathOptInterface as MOI

#Example of simple Thermal Generation Dispatch and the derivatives of the generation by the demand (dg/dd)
mutable struct DispachModel
Expand All @@ -27,7 +26,7 @@ function GenerateModel_VariableIndex()
g_sup = [10.0, 20.0, 30.0] # Upper limit of generation for each generator
c_g = [1.0, 3.0, 5.0] # Cost of generation for each generator
c_Df = 10.0 # Cost of deficit

# Creation of the Optimizer
optimizer = Xpress.Optimizer(PRESOLVE=0, logfile = "outputXpress_SV.log")
# Variables
Expand All @@ -43,7 +42,7 @@ function GenerateModel_VariableIndex()
end
c_limit_inf[I+1] = MOI.add_constraint(optimizer, Df, MOI.GreaterThan(0.0))

c_demand = MOI.add_constraint(optimizer,
c_demand = MOI.add_constraint(optimizer,
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(ones(I+1),[g;Df]), 0.0),
MOI.EqualTo(d)
) # Constraint of the Demand
Expand All @@ -68,7 +67,7 @@ function GenerateModel_ScalarAffineFunction()
g_sup = [10.0, 20.0, 30.0] # Upper limit of generation for each generator
c_g = [1.0, 3.0, 5.0] # Cost of generation for each generator
c_Df = 10.0 # Cost of deficit

# Creation of the Optimizer
optimizer = Xpress.Optimizer(PRESOLVE=0, logfile = "outputXpress_SAF.log")
# Variables
Expand All @@ -95,7 +94,7 @@ function GenerateModel_ScalarAffineFunction()
c_limit_inf[I+1] = MOI.add_constraint(optimizer, MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(vectAux,[g;Df]),0.0), MOI.GreaterThan(0.0))
vectAux[I+1] = 0.0

c_demand = MOI.add_constraint(optimizer,
c_demand = MOI.add_constraint(optimizer,
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(ones(I+1),[g;Df]), 0.0),
MOI.EqualTo(d)
) # Constraint of the Demand
Expand All @@ -121,10 +120,10 @@ function Forward(model::DispachModel, ϵ::Float64 = 1.0)

#Get the primal solution of the model
vect = MOI.get.(optimizer, MOI.VariablePrimal(), vectRef)

#Pass the perturbation to the Xpress Framework and set the context to Forward
MOI.set(optimizer, Xpress.ForwardSensitivityInputConstraint(), model.c_demand, ϵ)

#Get the derivative of the model
dvect = MOI.get.(optimizer, Xpress.ForwardSensitivityOutputVariable(), vectRef)

Expand Down
7 changes: 3 additions & 4 deletions test/MathOptInterface/MOI_callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.

using Xpress
using MathOptInterface
using Random
using Test

const MOI = MathOptInterface
import MathOptInterface as MOI
import Random

function callback_simple_model()
model = Xpress.Optimizer(
Expand Down Expand Up @@ -323,7 +322,7 @@ end
cb_calls = Int32[]
MOI.set(model, Xpress.CallbackFunction(), (cb_data) -> begin
push!(cb_calls)

if Xpress.get_control_or_attribute(cb_data.model, Xpress.Lib.XPRS_CALLBACKCOUNT_OPTNODE) > 1
return
end
Expand Down
Loading
Loading