Skip to content

Commit

Permalink
Completely drop the DEFAULT_SOLVER logic (#250)
Browse files Browse the repository at this point in the history
- Dropped get_default_solver and set_default_solver
- Reorganized tests to not depend on get_default_solver (the giant at-testset for loop can be changed).
- Only API change is that if you didn't define a problem with a solver/model then `solve!(p)` throws an argument
  (e.g., `solve!(p, solver)` is preferred now. NOTE: The existing `solve!(p, solver)` just assigns the problem model anyways
  with `problem.model = MathProgBase.ConicModel(s)`
  • Loading branch information
rofinn authored and ararslan committed Dec 12, 2018
1 parent 9490233 commit a8e64d4
Show file tree
Hide file tree
Showing 17 changed files with 797 additions and 955 deletions.
10 changes: 6 additions & 4 deletions src/problems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ mutable struct Problem
constraints::Array{Constraint}
status::Symbol
optval::Float64OrNothing
model::MathProgBase.AbstractConicModel
model::Union{MathProgBase.AbstractConicModel, Nothing}
solution::Solution

function Problem(head::Symbol, objective::AbstractExpr,
model::MathProgBase.AbstractConicModel, constraints::Array=Constraint[])
model::Union{MathProgBase.AbstractConicModel, Nothing}=nothing,
constraints::Array=Constraint[])
if sign(objective)== Convex.ComplexSign()
error("Objective can not be a complex expression")
else
Expand All @@ -40,8 +41,9 @@ end

# constructor if model is not specified
function Problem(head::Symbol, objective::AbstractExpr, constraints::Array=Constraint[],
solver::MathProgBase.AbstractMathProgSolver=DEFAULT_SOLVER)
Problem(head, objective, MathProgBase.ConicModel(solver), constraints)
solver::Union{MathProgBase.AbstractMathProgSolver, Nothing}=nothing)
model = solver !== nothing ? MathProgBase.ConicModel(solver) : solver
Problem(head, objective, model, constraints)
end

# If the problem constructed is of the form Ax=b where A is m x n
Expand Down
8 changes: 8 additions & 0 deletions src/solution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ function solve!(problem::Problem;
check_vexity=true,
verbose=true)

if problem.model === nothing
throw(ArgumentError(
"The provided problem hasn't been initialized with a conic model.
You can resolve this by passing in `AbstractMathProgSolver` such as:
solve!(problem, ECOSSolver())"
))
end

if check_vexity
vex = vexity(problem)
end
Expand Down
61 changes: 4 additions & 57 deletions src/solver_info.jl
Original file line number Diff line number Diff line change
@@ -1,46 +1,7 @@
using Pkg
import MathProgBase
export can_solve_mip, can_solve_socp, can_solve_sdp, can_solve_exp
export set_default_solver, get_default_solver, isinstalled

function set_default_solver(solver::MathProgBase.AbstractMathProgSolver)
Base.depwarn(
"Default solvers are deprecated and will not be provided in a future release.
You can still manually load and use solvers such as ECOS by running:
using ECOS
p = minimize(...);
solve!(p, ECOSSolver())",
:set_default_solver
)

global DEFAULT_SOLVER
DEFAULT_SOLVER = solver
end

function get_default_solver()
Base.depwarn(
"Default solvers are deprecated and will not be provided in a future release.
You can still manually load and use installed solvers such as ECOS by running:
using ECOS
p = minimize(...);
solve!(p, ECOSSolver())",
:get_default_solver
)

if DEFAULT_SOLVER == nothing
error("The default solver is set to `nothing`
You must have at least one solver installed to use Convex.
You can install a solver such as SCS by running:
Pkg.add(\"SCS\").
You will have to restart Julia after that.")
end

return DEFAULT_SOLVER
end

# TODO: I have not listed solvers such as CPLEX etc because I have not tested Convex with them
solvers = [("ECOS", "ECOSSolver"), ("SCS", "SCSSolver"), ("Gurobi", "GurobiSolver"), ("Mosek", "MosekSolver"),
("GLPKMathProgInterface", "GLPKSolverMIP")]
export isinstalled

function isinstalled(pkg)
for path in Base.DEPOT_PATH
Expand All @@ -52,24 +13,10 @@ function isinstalled(pkg)
return false
end

for (dir, solver) in solvers
if isinstalled(dir) && DEFAULT_SOLVER == nothing
eval(Meta.parse("using $dir"))
eval(Meta.parse("set_default_solver($solver())"))
end
end

# TODO: I have not listed solvers such as CPLEX etc because I have not tested Convex with them
solvers = [("ECOS", "ECOSSolver"), ("SCS", "SCSSolver"), ("Gurobi", "GurobiSolver"), ("Mosek", "MosekSolver"),
("GLPKMathProgInterface", "GLPKSolverMIP")]

if get_default_solver() == nothing
packages = join([dir for (dir, solver) in solvers], " | ")
@warn "***********************************************************************************************
You don't have any of
"*packages*" installed.
You must have at least one of these solvers. You can install a solver such as SCS by running:
Pkg.add(\"SCS\")
You will have to restart Julia after that.
***********************************************************************************************"
end

function can_solve_mip(solver)
name = typeof(solver).name.name
Expand Down
24 changes: 19 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
using Convex
using Convex: DotMultiplyAtom
using Test
using ECOS
using SCS
using GLPKMathProgInterface
using Random

import LinearAlgebra.eigen
import LinearAlgebra.I
import LinearAlgebra.opnorm
import Random.shuffle
import Statistics.mean

TOL = 1e-3
eye(n) = Matrix(1.0I, n, n)

# Seed random number stream to improve test reliability
Random.seed!(2)

Expand All @@ -24,9 +34,13 @@ if isinstalled("Mosek")
push!(solvers, MosekSolver(LOG=0))
end

for solver in solvers
println("Running tests with $(solver):")
set_default_solver(solver)
println(get_default_solver())
include("runtests_single_solver.jl")
@testset "Convex" begin
include("test_utilities.jl")
include("test_affine.jl")
include("test_lp.jl")
include("test_socp.jl")
include("test_sdp.jl")
include("test_exp.jl")
include("test_sdp_and_exp.jl")
include("test_mip.jl")
end
61 changes: 0 additions & 61 deletions test/runtests_single_solver.jl

This file was deleted.

Loading

0 comments on commit a8e64d4

Please sign in to comment.