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

WIP: MOI #139

Closed
wants to merge 38 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
881f701
Begin MOI rewrite
odow Jul 21, 2017
5a5395c
constraints
odow Jul 21, 2017
c098adf
objectives
odow Jul 21, 2017
744d084
more MOI
odow Jul 24, 2017
29b7ee5
Much changes
odow Jul 28, 2017
e8ada4e
Passing Linear1
odow Jul 31, 2017
48b90de
Improve status
odow Aug 1, 2017
68721dc
Tidy up and comment
odow Aug 1, 2017
64e0b57
Add integer
odow Aug 1, 2017
bc280b1
Spurious segfaults with sos
odow Aug 2, 2017
d964a82
Fails on SOS1 test
odow Aug 2, 2017
eadbf46
sos constraint sets and functions
odow Aug 2, 2017
441470b
Add solve attributes
odow Aug 2, 2017
c551729
More attributes
odow Aug 2, 2017
9632b1d
Most of quadratic basics
odow Aug 2, 2017
072d102
add new qp test
odow Aug 2, 2017
c729976
addlinearconstraints!
odow Aug 2, 2017
0971612
fix solve attributes
odow Aug 2, 2017
e5b940a
Turn off screen printing and bundle tests
odow Aug 2, 2017
2763835
Rejig constants, params, and status
odow Aug 3, 2017
fdeef48
Add defines for CPLEX12.7
odow Aug 3, 2017
bada1f9
Set variable warm starts
odow Aug 3, 2017
3e14070
tidy up more
odow Aug 3, 2017
7c15b15
Handle quad->linear objectives
odow Aug 6, 2017
3a23910
Fix bug in chgcoltyp
odow Aug 7, 2017
a500672
add vector bounds
odow Aug 7, 2017
b21eb6e
Add interval constraints
odow Aug 7, 2017
92a9594
modifying interval constraints
odow Aug 7, 2017
19bef32
rejig tolerances for tests
odow Aug 9, 2017
862c316
Reenable old code
odow Aug 9, 2017
cb01a5c
Enable certificates
odow Aug 9, 2017
239c921
can return infeasible primal solutions
odow Aug 9, 2017
630e182
Fix wherefrom in callbacks
odow Aug 14, 2017
c293732
Add transformconstraint
odow Aug 14, 2017
01b9f4e
tidy inclusion of 12.7.1 files
odow Aug 14, 2017
75738b0
Add canmodify and candelete
odow Aug 16, 2017
3425a7d
Tidy handling of infeasibility
odow Aug 16, 2017
1e7416f
Add cantransformconstraint
odow Aug 20, 2017
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
31 changes: 19 additions & 12 deletions src/CPLEX.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,35 @@ module CPLEX
include("cpx_env.jl")
v = version()
if startswith(v,"12.6")
include("full_defines_126.jl")
include("cpx_params_126.jl")
include(joinpath("cpx_defines", "full_defines_126.jl"))
include(joinpath("cpx_params", "cpx_params_126.jl"))
elseif startswith(v,"12.7.1")
include("full_defines_1271.jl")
include("cpx_params_1271.jl")
include(joinpath("cpx_defines", "full_defines_1271.jl"))
include(joinpath("cpx_params", "cpx_params_1271.jl"))
elseif startswith(v,"12.7")
include("full_defines_127.jl")
include("cpx_params_127.jl")
include(joinpath("cpx_defines", "full_defines_127.jl"))
include(joinpath("cpx_params", "cpx_params_127.jl"))
else
error("Unsupported CPLEX version $v. Only 12.6 and 12.7 are currently supported.")
end

include("cpx_model.jl")
include("cpx_params.jl")
include("cpx_vars.jl")
include("cpx_constrs.jl")
include("cpx_quad.jl")
# include("cpx_vars.jl")
# include("cpx_constrs.jl")
# include("cpx_quad.jl")
# include("cpx_solve.jl")
# include("cpx_callbacks.jl")
# include("cpx_highlevel.jl")


include("cpx_variables.jl")
include("cpx_constraints.jl")
include("cpx_objective.jl")
include("cpx_solve.jl")
include("cpx_callbacks.jl")
include("cpx_highlevel.jl")

# include("CplexSolverInterface.jl")
include("MOI/MathOptInterface.jl")
include("MathOptInterface.jl")
# These are undocumented JuMP extensions for CPLEX which
# will need to be hosted in a separate package for Julia 0.6 and later.
if isdir(Pkg.dir("JuMP")) && VERSION < v"0.6-"
Expand Down
19 changes: 0 additions & 19 deletions src/MOI/constraints_cpx.jl

This file was deleted.

48 changes: 0 additions & 48 deletions src/MOI/macros_cpx.jl

This file was deleted.

5 changes: 0 additions & 5 deletions src/MOI/solve.jl

This file was deleted.

13 changes: 0 additions & 13 deletions src/MOI/solve_cpx.jl

This file was deleted.

21 changes: 15 additions & 6 deletions src/MOI/MathOptInterface.jl → src/MathOptInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function CplexSolver(;mipstart_effortlevel::Cint = CPX_MIPSTART_AUTO, options...
CplexSolver(mipstart_effortlevel, options)
end

MOI.getattribute(s::CplexSolver, ::MOI.SupportsDuals) = false

const Linear = MOI.ScalarAffineFunction{Float64}
const LE = MOI.LessThan{Float64}
Expand Down Expand Up @@ -71,11 +72,16 @@ ConstraintMapping() = ConstraintMapping(

mutable struct CplexSolverInstance <: MOI.AbstractSolverInstance
inner::Model

last_variable_reference::UInt64
variable_mapping::Dict{MOI.VariableReference, Int}
variable_references::Vector{MOI.VariableReference}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need variable_mapping and variable_references, cant we just use keys(variable_mapping)?

primal_solution::Vector{Float64}

last_constraint_reference::UInt64
constraint_mapping::ConstraintMapping

objective_constant::Float64
end

function MOI.SolverInstance(s::CplexSolver)
Expand All @@ -88,13 +94,16 @@ function MOI.SolverInstance(s::CplexSolver)
Model(env),
0,
Dict{MOI.VariableReference, Int}(),
MOI.VariableReference[],
Float64[],
0,
ConstraintMapping()
ConstraintMapping(),
0.0
)
end
include(joinpath("cpx_status", "status_codes.jl"))

include("macros_cpx.jl")
include("variables.jl")
include("constraints.jl")
include("objective.jl")
include("solve.jl")
include("moi_variables.jl")
include("moi_constraints.jl")
include("moi_objective.jl")
include("moi_solve.jl")
74 changes: 24 additions & 50 deletions src/cpx_common.jl
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
# makes calling C functions a bit easier
"""
@cpx_ccall(func, args...)
"""
macro cpx_ccall(func, args...)
f = "CPX$(func)"
args = map(esc,args)
if is_unix()
return quote
ccall(($f,libcplex), $(args...))
end
end
if is_windows()
return quote stat = ccall(($f,libcplex), $(args...)) end
elseif is_windows()
if VERSION < v"0.6.0-dev.1512" # probably julia PR #15850
return quote
ccall(($f,libcplex), stdcall, $(args...))
end
return quote stat = ccall(($f,libcplex), stdcall, $(args...)) end
else
return quote
ccall(($f,libcplex), $(esc(:stdcall)), $(args...))
end
return quote stat = ccall(($f,libcplex), $(esc(:stdcall)), $(args...)) end
end
else
error("Unknown platform.")
end
end

"""
@cpx_ccall_error(env, func, args...)

Throws CplexError if return code is not 0
"""
macro cpx_ccall_error(env, func, args...)
args = map(esc,args)
quote
stat = $(Expr(:macrocall, Symbol("@cpx_ccall"), esc(func), args...))
if stat != 0
throw(CplexError($(esc(env)), stat))
end
end
end

macro cpx_ccall_intercept(model, func, args...)
f = "CPX$(func)"
args = map(esc,args)
quote
ccall(:jl_exit_on_sigint, Void, (Cint,), convert(Cint,0))
Expand All @@ -43,41 +55,3 @@ macro cpx_ccall_intercept(model, func, args...)
ret
end
end

const GChars = Union{Cchar, Char}
const IVec = Vector{Cint}
const FVec = Vector{Cdouble}
const CVec = Vector{Cchar}
const CoeffMat = Union{Matrix{Cdouble}, SparseMatrixCSC{Cdouble}}
@compat Bounds{T<:Real} = Union{T, Vector{T}}

const GCharOrVec = Union{Cchar, Char, Vector{Cchar}, Vector{Char}}

# empty vector & matrix (for the purpose of supplying default arguments)
const emptyfvec = Vector{Float64}(0)
const emptyfmat = Matrix{Float64}(0, 0)

cchar(c::Cchar) = c
cchar(c::Char) = convert(Cchar, c)

ivec(v::IVec) = v
fvec(v::FVec) = v
cvec(v::CVec) = v

ivec(v::Vector) = convert(IVec, v)
fvec(v::Vector) = convert(FVec, v)
cvec(v::Vector) = convert(CVec, v)

# cvecx(v, n) and fvecx(v, n)
# converts v into a vector of Cchar or Float64 of length n,
# where v can be either a scalar or a vector of length n.

_chklen(v, n::Integer) = (length(v) == n || error("Inconsistent argument dimensions."))

cvecx(c::GChars, n::Integer) = fill(cchar(c), n)
cvecx(c::Vector{Cchar}, n::Integer) = (_chklen(c, n); c)
cvecx(c::Vector{Char}, n::Integer) = (_chklen(c, n); convert(Vector{Cchar}, c))

fvecx(v::Real, n::Integer) = fill(Float64(v), n)
fvecx(v::Vector{Float64}, n::Integer) = (_chklen(v, n); v)
fvecx{T<:Real}(v::Vector{T}, n::Integer) = (_chklen(v, n); convert(Vector{Float64}, v))
55 changes: 55 additions & 0 deletions src/cpx_constraints.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
function cpx_add_constraint!(model::Model, cols::Vector{Int}, coefficients::Vector{Float64}, sense::Cchar, rhs::Float64)
@assert length(cols) == length(coefficients)
nnz = Cint(length(cols))
@cpx_ccall(addrows, Cint, (
Ptr{Void}, # environment
Ptr{Void}, # problem
Cint, # num new cols
Cint, # num new rows
Cint, # num non-zeros
Ptr{Cdouble}, # rhs
Ptr{Cchar}, # sense
Ptr{Cint}, # matrix start
Ptr{Cint}, # matrix index
Ptr{Cdouble}, # matrix values
Ptr{Ptr{Cchar}}, # col names
Ptr{Ptr{Cchar}} # row names
),
model.env.ptr, model.lp, 0, Cint(1), nnz, [rhs], [sense], [Cint(0)], Cint.(cols-1), coefficients, C_NULL, C_NULL)
end

function cpx_number_constraints(model::Model)
ncons = @cpx_ccall(getnumrows, Cint, (
Ptr{Void},
Ptr{Void}
),
model.env.ptr, model.lp)
return ncons
end

function get_rhs(model::Model)
ncons = cpx_number_constraints(model)
rhs = Vector{Cdouble}(ncons)
@cpx_ccall_error(model.env, getrhs, Cint, (
Ptr{Void},
Ptr{Void},
Ptr{Cdouble},
Cint,
Cint
),
model.env.ptr, model.lp, rhs, 0, ncons-1)
return rhs
end

function cpx_get_rhs(model::Model, row::Int)
rhs = Vector{Cdouble}(1)
@cpx_ccall_error(model.env, getrhs, Cint, (
Ptr{Void},
Ptr{Void},
Ptr{Cdouble},
Cint,
Cint
),
model.env.ptr, model.lp, rhs, Cint(row-1), Cint(row-1))
return rhs[1]
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 9 additions & 7 deletions src/cpx_env.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
function cpx_finalizer(env)
if env.num_models == 0
close_CPLEX(env)
else
env.finalize_called = true
end
end

type Env
ptr::Ptr{Void}
num_models::Int
Expand All @@ -10,13 +18,7 @@ type Env
error("CPLEX: Error creating environment")
end
env = new(tmp, 0, false)
finalizer(env, env -> begin
if env.num_models == 0
close_CPLEX(env)
else
env.finalize_called = true
end
end)
finalizer(env, cpx_finalizer)
env
end
end
Expand Down
Loading