Skip to content

Commit

Permalink
Merge pull request #1128 from JuliaOpt/bl/nosolver
Browse files Browse the repository at this point in the history
Remove solver concept and AbstractSolverInstanceAttribute
  • Loading branch information
blegat committed Nov 21, 2017
2 parents 9cfe927 + fdbabe0 commit db33c82
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 60 deletions.
25 changes: 2 additions & 23 deletions src/JuMP.jl
Expand Up @@ -35,7 +35,7 @@ export
PSDCone,
# Functions
# Model related
setobjectivesense, setsolver,
setobjectivesense,
writeLP, writeMPS,
#addSOS1, addSOS2,
solve,
Expand Down Expand Up @@ -76,10 +76,6 @@ const BINREF = MOICON{MOI.SingleVariable,MOI.ZeroOne}

@MOIU.instance JuMPInstance (ZeroOne, Integer) (EqualTo, GreaterThan, LessThan, Interval) (Zeros, Nonnegatives, Nonpositives, SecondOrderCone, RotatedSecondOrderCone, PositiveSemidefiniteConeTriangle) () (SingleVariable,) (ScalarAffineFunction,ScalarQuadraticFunction) (VectorOfVariables,) (VectorAffineFunction,)

# dummy solver
type UnsetSolver <: MathOptInterface.AbstractSolver
end

###############################################################################
# Model class
# Keeps track of all model and column info
Expand Down Expand Up @@ -141,7 +137,6 @@ mutable struct Model <: AbstractModel
# internal solver instance object
solverinstance
# Solver+option object from MPB or MOI
solver
solverinstanceattached::Bool
# callbacks
callbacks
Expand Down Expand Up @@ -172,11 +167,8 @@ mutable struct Model <: AbstractModel
# dictionary keyed on an extension-specific symbol
ext::Dict{Symbol,Any}
# Default constructor
function Model(;solver=UnsetSolver(), simplify_nonlinear_expressions::Bool=false)
function Model(; simplify_nonlinear_expressions::Bool=false)
# TODO need to support MPB also
if !isa(solver,MOI.AbstractSolver)
error("solver argument ($solver) must be an AbstractSolver")
end
m = new()
# TODO make pretty
m.instance = JuMPInstance{Float64}()
Expand All @@ -192,7 +184,6 @@ mutable struct Model <: AbstractModel
m.objbound = 0.0
m.objval = 0.0
m.solverinstance = nothing
m.solver = solver
m.solverinstanceattached = false
m.callbacks = Any[]
m.solvehook = nothing
Expand Down Expand Up @@ -404,18 +395,6 @@ dualstatus(m::Model) = MOI.get(m, MOI.DualStatus())
# setobjective(::Model, ::Symbol, x::AbstractArray) =
# error("in setobjective: array of size $(_size(x)) passed as objective; only scalar objectives are allowed")

"""
setsolver(m::Model, solver::MathOptInterface.AbstractSolver)
Change the solver which will be used for the next call to `solve()`, discarding the current internal model if present.
"""
function setsolver(m::Model, solver::MOI.AbstractSolver)
m.solver = solver
m.solverinstance = nothing
m.solverinstanceattached = false
nothing
end

# # Deep copy the model
# function Base.copy(source::Model)
#
Expand Down
16 changes: 7 additions & 9 deletions src/solverinterface.jl
Expand Up @@ -10,9 +10,9 @@ Attach the JuMP model `m` to a new solver instance. All instance data is transfe
and while the solver instance remains attached, all modifications to the JuMP model are immediately mirrored
in the solver instance. See also `isattached()` and `detach()`.
"""
function attach(m::Model)
function attach(m::Model, solverinstance::MOI.AbstractSolverInstance)
@assert !m.solverinstanceattached
m.solverinstance = MOI.SolverInstance(m.solver)
m.solverinstance = solverinstance
solvervariables = MOI.addvariables!(m.solverinstance, numvar(m)) # TODO numvar shouldn't return unsigned int

m.variabletosolvervariable = Dict{MOIVAR,MOIVAR}()
Expand Down Expand Up @@ -60,24 +60,24 @@ function detach(m::Model)
end

"""
canget(m::JuMP.Model, attr::MathOptInterface.AbstractSolverInstanceAttribute)::Bool
canget(m::JuMP.Model, attr::MathOptInterface.AbstractInstanceAttribute)::Bool
Return `true` if one may query the attribute `attr` from the solver instance attached to the JuMP model,
false if not.
Throws an error if no solver instance is currently attached.
"""
function MOI.canget(m::Model, attr::MOI.AbstractSolverInstanceAttribute)
function MOI.canget(m::Model, attr::MOI.AbstractInstanceAttribute)
@assert m.solverinstanceattached
return MOI.canget(m.solverinstance, attr)
end

"""
get(m::JuMP.Model, attr::MathOptInterface.AbstractSolverInstanceAttribute)
get(m::JuMP.Model, attr::MathOptInterface.AbstractInstanceAttribute)
Return the value of the attribute `attr` from the solver instance attached to the JuMP model.
Throws an error if no solver instance is currently attached.
"""
function MOI.get(m::Model, attr::MOI.AbstractSolverInstanceAttribute)
function MOI.get(m::Model, attr::MOI.AbstractInstanceAttribute)
@assert m.solverinstanceattached
return MOI.get(m.solverinstance, attr)
end
Expand All @@ -91,9 +91,7 @@ function solve(m::Model;
return m.solvehook(m)
end

if !m.solverinstanceattached
attach(m)
end
@assert m.solverinstanceattached
MOI.optimize!(m.solverinstance)

empty!(m.variableresult)
Expand Down
3 changes: 2 additions & 1 deletion test/lp.jl
Expand Up @@ -5,13 +5,14 @@
# st x + y <= 1 (x + y - 1 ∈ Nonpositives)
# x, y >= 0 (x, y ∈ Nonnegatives)

m = Model(solver=CSDPSolver(printlevel=0))
m = Model()
@variable(m, x >= 0.0)
@variable(m, y >= 0.0)
@objective(m, Min, -x)

@constraint(m, x + y <= 1)

JuMP.attach(m, CSDPInstance(printlevel=0))
JuMP.solve(m)

@test JuMP.isattached(m)
Expand Down

0 comments on commit db33c82

Please sign in to comment.