Skip to content

Commit

Permalink
compat fixes for 0.7 (#127)
Browse files Browse the repository at this point in the history
* compat fixes for 0.7

* fix rmul
  • Loading branch information
mlubin committed Jul 22, 2018
1 parent 4fba47d commit 870f037
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/Ipopt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function __init__()
# Sets up the library paths so that we can run the ipopt binary from Julia.
# TODO: Restructure into a function that wraps the call to the binary and
# doesn't leave environment variables changed.
julia_libdir = joinpath(dirname(first(filter(x -> contains(x, "libjulia"), Sys.Libdl.dllist()))), "julia")
julia_libdir = joinpath(dirname(first(filter(x -> occursin("libjulia", x), Compat.Libdl.dllist()))), "julia")
@static if Compat.Sys.isapple()
ENV["DYLD_LIBRARY_PATH"] = string(get(ENV, "DYLD_LIBRARY_PATH", ""), ":", julia_libdir)
elseif Compat.Sys.islinux()
Expand Down Expand Up @@ -64,7 +64,7 @@ mutable struct IpoptProblem
:Min)
# Free the internal IpoptProblem structure when
# the Julia IpoptProblem instance goes out of scope
finalizer(prob, freeProblem)
@compat finalizer(freeProblem, prob)
# Return the object we just made
prob
end
Expand Down
4 changes: 2 additions & 2 deletions src/MOIWrapper.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MathOptInterface
import MathOptInterface
const MOI = MathOptInterface

mutable struct VariableInfo
Expand Down Expand Up @@ -627,7 +627,7 @@ function MOI.optimize!(m::IpoptOptimizer)
# Objective gradient callback
function eval_grad_f_cb(x, grad_f)
eval_objective_gradient(m, grad_f, x)
scale!(grad_f,objective_scale)
Compat.rmul!(grad_f,objective_scale)
end

# Constraint value callback
Expand Down
64 changes: 34 additions & 30 deletions src/MPBWrapper.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Standard LP interface
importall MathProgBase.SolverInterface
import MathProgBase
const MPB = MathProgBase

###############################################################################
# Solver objects
export IpoptSolver
struct IpoptSolver <: AbstractMathProgSolver
struct IpoptSolver <: MPB.AbstractMathProgSolver
options
end
IpoptSolver(;kwargs...) = IpoptSolver(kwargs)

mutable struct IpoptMathProgModel <: AbstractNonlinearModel
mutable struct IpoptMathProgModel <: MPB.AbstractNonlinearModel
inner::IpoptProblem
numvar::Int
numconstr::Int
Expand All @@ -25,38 +25,42 @@ mutable struct IpoptMathProgModel <: AbstractNonlinearModel
model
end
end
NonlinearModel(s::IpoptSolver) = IpoptMathProgModel(;s.options...)
LinearQuadraticModel(s::IpoptSolver) = NonlinearToLPQPBridge(NonlinearModel(s))
MPB.NonlinearModel(s::IpoptSolver) = IpoptMathProgModel(;s.options...)
MPB.LinearQuadraticModel(s::IpoptSolver) = MPB.NonlinearToLPQPBridge(MPB.NonlinearModel(s))

###############################################################################
# Begin interface implementation

# generic nonlinear interface
function loadproblem!(m::IpoptMathProgModel, numVar::Integer, numConstr::Integer, x_l, x_u, g_lb, g_ub, sense::Symbol, d::AbstractNLPEvaluator)
function MPB.loadproblem!(m::IpoptMathProgModel, numVar::Integer, numConstr::Integer,
x_l, x_u, g_lb, g_ub, sense::Symbol, d::MPB.AbstractNLPEvaluator)

m.numvar = numVar
features = features_available(d)
features = MPB.features_available(d)
has_hessian = (:Hess in features)
init_feat = [:Grad]
has_hessian && push!(init_feat, :Hess)
numConstr > 0 && push!(init_feat, :Jac)

initialize(d, init_feat)
Ihess, Jhess = has_hessian ? hesslag_structure(d) : (Int[], Int[])
Ijac, Jjac = numConstr > 0 ? jac_structure(d) : (Int[], Int[])
MPB.initialize(d, init_feat)
Ihess, Jhess = has_hessian ? MPB.hesslag_structure(d) : (Int[], Int[])
Ijac, Jjac = numConstr > 0 ? MPB.jac_structure(d) : (Int[], Int[])
@assert length(Ijac) == length(Jjac)
@assert length(Ihess) == length(Jhess)
@assert sense == :Min || sense == :Max

# Objective callback
scl = (sense == :Min) ? 1.0 : -1.0
eval_f_cb(x) = scl*eval_f(d,x)
eval_f_cb(x) = scl * MPB.eval_f(d,x)

# Objective gradient callback
eval_grad_f_cb(x, grad_f) = (eval_grad_f(d, grad_f, x); scale!(grad_f,scl))
function eval_grad_f_cb(x, grad_f)
MPB.eval_grad_f(d, grad_f, x)
Compat.rmul!(grad_f, scl)
end

# Constraint value callback
eval_g_cb(x, g) = eval_g(d, g, x)
eval_g_cb(x, g) = MPB.eval_g(d, g, x)

# Jacobian callback
function eval_jac_g_cb(x, mode, rows, cols, values)
Expand All @@ -66,7 +70,7 @@ function loadproblem!(m::IpoptMathProgModel, numVar::Integer, numConstr::Integer
cols[i] = Jjac[i]
end
else
eval_jac_g(d, values, x)
MPB.eval_jac_g(d, values, x)
end
end

Expand All @@ -81,7 +85,7 @@ function loadproblem!(m::IpoptMathProgModel, numVar::Integer, numConstr::Integer
end
else
obj_factor *= scl
eval_hesslag(d, values, x, obj_factor, lambda)
MPB.eval_hesslag(d, values, x, obj_factor, lambda)
end
end
else
Expand All @@ -100,16 +104,16 @@ function loadproblem!(m::IpoptMathProgModel, numVar::Integer, numConstr::Integer

end

getsense(m::IpoptMathProgModel) = m.inner.sense
numvar(m::IpoptMathProgModel) = m.numvar
numconstr(m::IpoptMathProgModel) = m.numconstr
MPB.getsense(m::IpoptMathProgModel) = m.inner.sense
MPB.numvar(m::IpoptMathProgModel) = m.numvar
MPB.numconstr(m::IpoptMathProgModel) = m.numconstr

numlinconstr(m::IpoptMathProgModel) = 0
MPB.numlinconstr(m::IpoptMathProgModel) = 0

numquadconstr(m::IpoptMathProgModel) = 0
MPB.numquadconstr(m::IpoptMathProgModel) = 0

function optimize!(m::IpoptMathProgModel)
copy!(m.inner.x, m.warmstart) # set warmstart
function MPB.optimize!(m::IpoptMathProgModel)
copyto!(m.inner.x, m.warmstart) # set warmstart
for (name,value) in m.options
sname = string(name)
if match(r"(^resto_)", sname) != nothing
Expand All @@ -120,7 +124,7 @@ function optimize!(m::IpoptMathProgModel)
solveProblem(m.inner)
end

function status(m::IpoptMathProgModel)
function MPB.status(m::IpoptMathProgModel)
# Map all the possible return codes, as enumerated in
# Ipopt.ApplicationReturnStatus, to the MPB statuses:
# :Optimal, :Infeasible, :Unbounded, :UserLimit, and :Error
Expand Down Expand Up @@ -159,18 +163,18 @@ function status(m::IpoptMathProgModel)
end

end
getobjval(m::IpoptMathProgModel) = m.inner.obj_val * (m.inner.sense == :Max ? -1 : +1)
getsolution(m::IpoptMathProgModel) = m.inner.x
MPB.getobjval(m::IpoptMathProgModel) = m.inner.obj_val * (m.inner.sense == :Max ? -1 : +1)
MPB.getsolution(m::IpoptMathProgModel) = m.inner.x

function getreducedcosts(m::IpoptMathProgModel)
function MPB.getreducedcosts(m::IpoptMathProgModel)
sense = m.inner.sense
redcost = m.inner.mult_x_U - m.inner.mult_x_L
return sense == :Max ? redcost : -redcost
end
function getconstrduals(m::IpoptMathProgModel)
function MPB.getconstrduals(m::IpoptMathProgModel)
v = m.inner.mult_g # return multipliers for all constraints
return m.inner.sense == :Max ? copy(v) : -v
end

getrawsolver(m::IpoptMathProgModel) = m.inner
setwarmstart!(m::IpoptMathProgModel, x) = (m.warmstart = x)
MPB.getrawsolver(m::IpoptMathProgModel) = m.inner
MPB.setwarmstart!(m::IpoptMathProgModel, x) = (m.warmstart = x)
6 changes: 4 additions & 2 deletions test/MPBWrapper.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Test MathProgBase stuff
#include(joinpath(Pkg.dir("MathProgBase"),"test","linprog.jl"))
#linprogtest(IpoptSolver())

using MathProgBase
using Compat.LinearAlgebra
using Compat.SparseArrays

solver = IpoptSolver()
sol = linprog([-1,0],[2 1],'<',1.5,solver)
@test sol.status == :Optimal
Expand Down
3 changes: 2 additions & 1 deletion test/hs071_test.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Ipopt, Base.Test
using Ipopt
using Compat.Test

# hs071
# min x1 * x4 * (x1 + x2 + x3) + x3
Expand Down
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Ipopt, Base.Test
using Ipopt
using Compat
using Compat.Test

@testset "C API" begin
# First of all, test that hs071 example works
Expand Down

0 comments on commit 870f037

Please sign in to comment.