Skip to content

Commit

Permalink
format (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
matbesancon authored Oct 30, 2023
1 parent b028993 commit c42b0d3
Show file tree
Hide file tree
Showing 26 changed files with 400 additions and 300 deletions.
6 changes: 3 additions & 3 deletions examples/HiGHS_example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ end
lmo = FrankWolfe.MathOptLMO(o)

function f(x)
return 0.5 * sum((x.-diffw).^2)
return 0.5 * sum((x .- diffw) .^ 2)
end

function grad!(storage, x)
@. storage = x-diffw
@. storage = x - diffw
end

x, _, result = Boscia.solve(f, grad!, lmo, verbose = true)
x, _, result = Boscia.solve(f, grad!, lmo, verbose=true)
34 changes: 18 additions & 16 deletions examples/approx_planted_point.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ diffi = Random.rand(Bool, n) * 0.6 .+ 0.3

@testset "Using Cube LMO" begin
int_vars = collect(1:n)

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

x, _, result = Boscia.solve(f, grad!, blmo, verbose =true)
x, _, result = Boscia.solve(f, grad!, blmo, verbose=true)

@test x == round.(diffi)
@test isapprox(f(x), f(result[:raw_solution]), atol=1e-6, rtol=1e-3)
Expand All @@ -59,14 +59,15 @@ diffi = Random.rand(Bool, n) * 0.6 .+ 0.3
int_vars = collect(1:n)
lbs = zeros(n)
ubs = ones(n)

sblmo = Boscia.CubeSimpleBLMO(lbs, ubs, int_vars)

x, _, result = Boscia.solve(f, grad!, sblmo, lbs[int_vars], ubs[int_vars], int_vars, n, verbose =true)
x, _, result =
Boscia.solve(f, grad!, sblmo, lbs[int_vars], ubs[int_vars], int_vars, n, verbose=true)

@test x == round.(diffi)
@test isapprox(f(x), f(result[:raw_solution]), atol=1e-6, rtol=1e-3)
end
end
end


Expand All @@ -79,7 +80,7 @@ end
@. storage = x - diffi
end

int_vars = unique!(rand(collect(1:n), Int(floor(n/2))))
int_vars = unique!(rand(collect(1:n), Int(floor(n / 2))))

@testset "Using SCIP" begin
o = SCIP.Optimizer()
Expand All @@ -99,37 +100,38 @@ end

sol = diffi
sol[int_vars] = round.(sol[int_vars])
@test sum(isapprox.(x, sol, atol =1e-6, rtol=1e-2)) == n
@test sum(isapprox.(x, sol, atol=1e-6, rtol=1e-2)) == n
@test isapprox(f(x), f(result[:raw_solution]), atol=1e-6, rtol=1e-3)
end

@testset "Using Cube LMO" begin
@testset "Using Cube LMO" begin
bounds = Boscia.IntegerBounds()
for i in 1:n
for i in 1:n
push!(bounds, (i, 0.0), :greaterthan)
push!(bounds, (i, 1.0), :lessthan)
end
blmo = Boscia.CubeBLMO(n, int_vars, bounds)

x, _, result = Boscia.solve(f, grad!, blmo, verbose =true)
x, _, result = Boscia.solve(f, grad!, blmo, verbose=true)

sol = diffi
sol[int_vars] = round.(sol[int_vars])
@test sum(isapprox.(x, sol, atol =1e-6, rtol=1e-2)) == n
@test sum(isapprox.(x, sol, atol=1e-6, rtol=1e-2)) == n
@test isapprox(f(x), f(result[:raw_solution]), atol=1e-6, rtol=1e-3)
end

@testset "Using Cube Simple LMO" begin
lbs = zeros(n)
ubs = ones(n)

sblmo = Boscia.CubeSimpleBLMO(lbs, ubs, int_vars)

x, _, result = Boscia.solve(f, grad!, sblmo, lbs[int_vars], ubs[int_vars], int_vars, n, verbose =true)
x, _, result =
Boscia.solve(f, grad!, sblmo, lbs[int_vars], ubs[int_vars], int_vars, n, verbose=true)

sol = diffi
sol[int_vars] = round.(sol[int_vars])
@test sum(isapprox.(x, sol, atol =1e-6, rtol=1e-2)) == n
@test sum(isapprox.(x, sol, atol=1e-6, rtol=1e-2)) == n
@test isapprox(f(x), f(result[:raw_solution]), atol=1e-6, rtol=1e-3)
end
end
end
1 change: 0 additions & 1 deletion examples/mps-example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,3 @@ end
x, _, result = Boscia.solve(f, grad!, lmo, verbose=true)
@test f(x) <= f(result[:raw_solution])
end

18 changes: 13 additions & 5 deletions examples/mps-examples/mip-examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function build_example(example, num_v)
MOI.set(o, MOI.RawOptimizerAttribute("presolving/maxrounds"), 0)

#trick to push the optimum towards the interior
vs = [FrankWolfe.compute_extreme_point(lmo, randn(n)) for _ in 1:num_v]
vs = [FrankWolfe.compute_extreme_point(lmo, randn(n)) for _ in 1:num_v]
# done to avoid one vertex being systematically selected
unique!(vs)

Expand All @@ -62,10 +62,10 @@ function build_example(example, num_v)
end

function grad!(storage, x)
mul!(storage, length(vs)/max_norm * I, x)
mul!(storage, length(vs) / max_norm * I, x)
storage .+= b_mps
for v in vs
@. storage -= 1/max_norm * v
@. storage -= 1 / max_norm * v
end
end

Expand Down Expand Up @@ -94,7 +94,15 @@ test_instance = string("MPS ", example, " instance")
@testset "$test_instance" begin
println("Example $(example)")
lmo, f, grad! = build_example(example, num_v)
x, _, result = Boscia.solve(f, grad!, lmo, verbose=true, print_iter = 10, fw_epsilon = 1e-1, min_node_fw_epsilon = 1e-3, time_limit=600)
x, _, result = Boscia.solve(
f,
grad!,
lmo,
verbose=true,
print_iter=10,
fw_epsilon=1e-1,
min_node_fw_epsilon=1e-3,
time_limit=600,
)
@test f(x) <= f(result[:raw_solution])
end

2 changes: 1 addition & 1 deletion ext/BosciaHiGHSExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ const MOI = MathOptInterface
const MOIU = MOI.Utilities

import MathOptSetDistances as MOD

end # module
11 changes: 8 additions & 3 deletions ext/BosciaSCIPExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ 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)
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
Expand All @@ -35,7 +40,7 @@ end
Cleanup internal SCIP model
"""
function Boscia.free_model(o::SCIP.Optimizer)
SCIP.SCIPfreeTransform(o)
return SCIP.SCIPfreeTransform(o)
end

"""
Expand All @@ -44,5 +49,5 @@ Get solving tolerance.
function Boscia.get_tol(o::SCIP.Optimizer)
return MOI.get(o, MOI.RawOptimizerAttribute("numerics/feastol"))
end

end # module
Loading

0 comments on commit c42b0d3

Please sign in to comment.