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

MathOptInterface, SCIP and HiGHS as weak dependencies #147

Merged
merged 19 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 17 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
11 changes: 10 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SCIP = "82193955-e24f-5292-bf16-6f2c5261a85f"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[weakdeps]
HiGHS = "87dc4568-4c63-4d18-b0c0-bb2238e4078b"
SCIP = "82193955-e24f-5292-bf16-6f2c5261a85f"

[extensions]
BosciaHiGHSExt = "HiGHS"
BosciaSCIPExt = "SCIP"

[compat]
Bonobo = "0.1.3"
DataStructures = "0.18"
Expand All @@ -31,6 +39,7 @@ julia = "1.6"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
HiGHS = "87dc4568-4c63-4d18-b0c0-bb2238e4078b"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
SCIP = "82193955-e24f-5292-bf16-6f2c5261a85f"

[targets]
test = ["Test", "HiGHS", "Distributions"]
test = ["Test", "HiGHS", "Distributions", "SCIP"]
4 changes: 2 additions & 2 deletions examples/approx_planted_point.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ diffi = Random.rand(Bool, n) * 0.6 .+ 0.3

bounds = Boscia.IntegerBounds()
for i in 1:n
push!(bounds, (i, MOI.GreaterThan(0.0)))
push!(bounds, (i, MOI.LessThan(1.0)))
push!(bounds, (i, 0.0), :greaterthan)
push!(bounds, (i, 1.0), :lessthan)
end
blmo = Boscia.CubeBLMO(n, int_vars, bin_vars, bounds)

Expand Down
8 changes: 4 additions & 4 deletions examples/lasso.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ push!(groups, ((k_int-1)*group_size+1):p)
lmo = FrankWolfe.MathOptLMO(o)
global_bounds = Boscia.IntegerBounds()
for i in 1:p
push!(global_bounds, (i + p, MOI.GreaterThan(0.0)))
push!(global_bounds, (i + p, MOI.LessThan(1.0)))
push!(global_bounds, (i, MOI.GreaterThan(-M_g)))
push!(global_bounds, (i, MOI.LessThan(M_g)))
push!(global_bounds, (i + p, 0.0), :greaterthan)
push!(global_bounds, (i + p, 1.0), :lessthan)
push!(global_bounds, (i, -M_g), :greaterthan)
push!(global_bounds, (i, M_g), :lessthan)
end

function f(x)
Expand Down
11 changes: 11 additions & 0 deletions ext/BosciaHiGHSExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module BosciaHiGHSExt

using Boscia
using HiGHS
using MathOptInterface
const MOI = MathOptInterface
const MOIU = MOI.Utilities

import MathOptSetDistances as MOD

end # module
48 changes: 48 additions & 0 deletions ext/BosciaSCIPExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module BosciaSCIPExt

using Boscia
using MathOptInterface
using SCIP
const MOI = MathOptInterface
const MOIU = MOI.Utilities

import MathOptSetDistances as MOD

"""
Finds the best solution in the SCIP solution storage, based on the objective function `f`.
Returns the solution vector and the corresponding best value.
"""
function Boscia.find_best_solution(f::Function, o::SCIP.Optimizer, vars::Vector{MOI.VariableIndex}, domain_oracle)
sols_vec =
unsafe_wrap(Vector{Ptr{Cvoid}}, SCIP.LibSCIP.SCIPgetSols(o), SCIP.LibSCIP.SCIPgetNSols(o))
best_val = Inf
best_v = nothing
for sol in sols_vec
v = SCIP.sol_values(o, vars, sol)
if domain_oracle(v)
val = f(v)
if val < best_val
best_val = val
best_v = v
end
end
end
#@assert isfinite(best_val) -> not necessarily the case if the domain oracle is not the default.
return (best_v, best_val)
end

"""
Cleanup internal SCIP model
"""
function Boscia.free_model(o::SCIP.Optimizer)
SCIP.SCIPfreeTransform(o)
end

"""
Get solving tolerance.
"""
function Boscia.get_tol(o::SCIP.Optimizer)
return MOI.get(o, MOI.RawOptimizerAttribute("numerics/feastol"))
end

end # module
12 changes: 8 additions & 4 deletions src/Boscia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ using FrankWolfe
import FrankWolfe: compute_extreme_point
export compute_extreme_point
using Random
using SCIP
import MathOptInterface
import Bonobo
using Printf
using Dates
using MathOptInterface
const MOI = MathOptInterface
const MOIU = MOI.Utilities

import MathOptSetDistances as MOD

matbesancon marked this conversation as resolved.
Show resolved Hide resolved
include("integer_bounds.jl")
include("blmo_interface.jl")
include("time_tracking_lmo.jl")
Expand All @@ -30,4 +28,10 @@ include("interface.jl")
include("MOI_bounded_oracle.jl")
include("cube_blmo.jl")

# For extensions
if !isdefined(Base, :get_extension)
include("../ext/BosciaSCIPExt.jl")
include("../ext/BosciaHiGHSExt.jl")
end
matbesancon marked this conversation as resolved.
Show resolved Hide resolved

end # module
Loading
Loading