Skip to content

Commit

Permalink
Merge pull request #190 from joaquimg/gb/julia_formatter
Browse files Browse the repository at this point in the history
Add julia formatter
  • Loading branch information
joaquimg authored Oct 6, 2022
2 parents 4a5b5b7 + 6dbdfd1 commit 565c0ef
Show file tree
Hide file tree
Showing 36 changed files with 2,546 additions and 1,658 deletions.
9 changes: 9 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Configuration file for JuliaFormatter.jl
# For more information, see: https://domluna.github.io/JuliaFormatter.jl/stable/config/

always_for_in = true
always_use_return = true
margin = 80
remove_extra_newlines = true
separate_kwargs_with_semicolon = true
short_to_long_function_def = true
90 changes: 57 additions & 33 deletions benchmarks/forecast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ using Random
using JuMP, BilevelJuMP

function bench_forecast(prods, samples, optimizer, mode, seed = 1234)

rng = Random.MersenneTwister(seed)

SampleSize = samples
Expand All @@ -13,35 +12,36 @@ function bench_forecast(prods, samples, optimizer, mode, seed = 1234)
phi1 = 0.7 * ones(Products)
phi0 = 3 * ones(Products)

mu = 3.0/(1.0 - 0.7)
mu = 3.0 / (1.0 - 0.7)

demand = ones(SampleSize, Products)

for p in 1:Products
for t in Samples
demand[t, p] = max(0.0, phi0[p] + phi1[p] * demand[t-1, p] + 0.2 * randn(rng))
demand[t, p] =
max(0.0, phi0[p] + phi1[p] * demand[t-1, p] + 0.2 * randn(rng))
end
end

p_s = 5 .+ 0.1*collect(1:Products)
p_b = 4 .+ 0.1*collect(1:Products)
p_r = 3 .+ 0.1*collect(1:Products)
p_s = 5 .+ 0.1 * collect(1:Products)
p_b = 4 .+ 0.1 * collect(1:Products)
p_r = 3 .+ 0.1 * collect(1:Products)

u = 8*Products
u = 8 * Products

ctrs = []
vars = []

# MOI.empty!(optimizer)
model = BilevelModel(optimizer, mode = mode)
model = BilevelModel(optimizer; mode = mode)
try
JuMP.set_time_limit_sec(model, MAX_TIME)
catch e
@show e
@show "failed to set limit time"
end
# parameters
v = @variable(Upper(model), 0 <= φ0[1:Products] <= 10)
v = @variable(Upper(model), 0 <= φ0[1:Products] <= 10)
push!(vars, vec(v))
v = @variable(Upper(model), -1 <= φ1[1:Products] <= 1)
push!(vars, vec(v))
Expand All @@ -51,53 +51,77 @@ function bench_forecast(prods, samples, optimizer, mode, seed = 1234)
=#

# buying quantity is the only
v = @variable(Lower(model), 0 <= q_b[p=1:Products, t=Samples] <= u)
v = @variable(Lower(model), 0 <= q_b[p = 1:Products, t = Samples] <= u)
push!(vars, vec(v))

#
v = @variable(Lower(model), 0 <= q_s[p=1:Products, t=Samples] <= u)
v = @variable(Lower(model), 0 <= q_s[p = 1:Products, t = Samples] <= u)
push!(vars, vec(v))
v = @variable(Lower(model), 0 <= q_r[p=1:Products, t=Samples] <= u)
v = @variable(Lower(model), 0 <= q_r[p = 1:Products, t = Samples] <= u)
push!(vars, vec(v))


c = @constraint(Lower(model),[p=1:Products,t=Samples],
q_s[p,t] + q_r[p,t] <= q_b[p,t])
c = @constraint(
Lower(model),
[p = 1:Products, t = Samples],
q_s[p, t] + q_r[p, t] <= q_b[p, t]
)
push!(ctrs, vec(c))

c = @constraint(Lower(model),[p=1:Products,t=Samples],
q_s[p,t] <= φ0[p] + φ1[p] * demand[t-1, p])
c = @constraint(
Lower(model),
[p = 1:Products, t = Samples],
q_s[p, t] <= φ0[p] + φ1[p] * demand[t-1, p]
)
push!(ctrs, vec(c))

c = @constraint(Lower(model),[t=Samples],
sum(q_b[p,t] for p in 1:Products) <= u)
c = @constraint(
Lower(model),
[t = Samples],
sum(q_b[p, t] for p in 1:Products) <= u
)
push!(ctrs, vec(c))

@objective(Lower(model), Min,
sum(p_b[p] * q_b[p,t] - p_s[p] * q_s[p,t] - p_r[p] * q_r[p,t]
for t in Samples, p in 1:Products))
@objective(
Lower(model),
Min,
sum(
p_b[p] * q_b[p, t] - p_s[p] * q_s[p, t] - p_r[p] * q_r[p, t] for
t in Samples, p in 1:Products
)
)

#=
upper model
=#

#
v = @variable(Upper(model), 0 <= q2_s[p=1:Products, t=Samples] <= u)
v = @variable(Upper(model), 0 <= q2_s[p = 1:Products, t = Samples] <= u)
push!(vars, vec(v))
v = @variable(Upper(model), 0 <= q2_r[p=1:Products, t=Samples] <= u)
v = @variable(Upper(model), 0 <= q2_r[p = 1:Products, t = Samples] <= u)
push!(vars, vec(v))

c = @constraint(Upper(model),[p=1:Products,t=Samples],
q2_s[p,t] + q2_r[p,t] <= q_b[p,t])
c = @constraint(
Upper(model),
[p = 1:Products, t = Samples],
q2_s[p, t] + q2_r[p, t] <= q_b[p, t]
)
push!(ctrs, vec(c))

c = @constraint(Upper(model),[p=1:Products,t=Samples],
q2_s[p,t] <= demand[t, p])
c = @constraint(
Upper(model),
[p = 1:Products, t = Samples],
q2_s[p, t] <= demand[t, p]
)
push!(ctrs, vec(c))

@objective(Upper(model), Min,
sum(p_b[p] * q_b[p,t] - p_s[p] * q2_s[p,t] - p_r[p] * q2_r[p,t]
for t in Samples, p in 1:Products))
@objective(
Upper(model),
Min,
sum(
p_b[p] * q_b[p, t] - p_s[p] * q2_s[p, t] - p_r[p] * q2_r[p, t] for
t in Samples, p in 1:Products
)
)

#=
Optimize
Expand Down Expand Up @@ -133,10 +157,10 @@ function bench_forecast(prods, samples, optimizer, mode, seed = 1234)
end
gap = try
bound = objective_bound(Upper(model))
abs(obj_u - bound)/max(abs(bound), 1e-8)
abs(obj_u - bound) / max(abs(bound), 1e-8)
catch
NaN
end

return primal_st, term_st, solve_t, build_t, obj_l, obj_u, gap
end
end
7 changes: 3 additions & 4 deletions benchmarks/rand.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function bench_rand(rows, cols, density, optimizer, mode, seed = 1234)
cu = sprand(rng, cols, 0.5, f_c)

# MOI.empty!(optimizer)
model = BilevelModel(optimizer, mode = mode)
model = BilevelModel(optimizer; mode = mode)
try
JuMP.set_time_limit_sec(model, MAX_TIME)
catch e
Expand All @@ -47,7 +47,6 @@ function bench_rand(rows, cols, density, optimizer, mode, seed = 1234)
@objective(Upper(model), Min, cu' * x)
@objective(Lower(model), Min, cl' * y)


#=
Optimize
=#
Expand All @@ -68,11 +67,11 @@ function bench_rand(rows, cols, density, optimizer, mode, seed = 1234)
obj_u = try
objective_value(Upper(model))
catch
NaN
NaN
end
gap = try
bound = objective_bound(Upper(model))
abs(obj_u - bound)/max(abs(bound), 1e-8)
abs(obj_u - bound) / max(abs(bound), 1e-8)
catch
NaN
end
Expand Down
93 changes: 60 additions & 33 deletions benchmarks/runbench.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ using Juniper
using QuadraticToBinary

QB = QuadraticToBinary.Optimizer{Float64}
cache(opt) = MOIU.CachingOptimizer(
MOIU.UniversalFallback(MOIU.Model{Float64}()), opt)
function cache(opt)
return MOIU.CachingOptimizer(
MOIU.UniversalFallback(MOIU.Model{Float64}()),
opt,
)
end

function cpx()
s=CPLEX.Optimizer()
MOI.set(s, MOI.RawParameter("CPXPARAM_TimeLimit"),MAX_TIME*1)
s
s = CPLEX.Optimizer()
MOI.set(s, MOI.RawParameter("CPXPARAM_TimeLimit"), MAX_TIME * 1)
return s
end

# warm-up - precompilation
Expand All @@ -61,16 +65,39 @@ MAX_TIME = 600
# return KN_OPT
# end


SOLVERS = [
#=
SOS1
=#
(with_att(Gurobi.Optimizer, "TimeLimit" => MAX_TIME*1), BilevelJuMP.SOS1Mode(), "gurobi_sos1"),
(with_att(CPLEX.Optimizer, "CPXPARAM_TimeLimit" => MAX_TIME*1), BilevelJuMP.SOS1Mode(), "cplex_sos1"),
(with_att(Xpress.Optimizer, "MAXTIME" => -MAX_TIME*1, "logfile" => "output.log"), BilevelJuMP.SOS1Mode(), "xpress_sos1"),
(with_att(Cbc.Optimizer, "seconds" => MAX_TIME*1.0), BilevelJuMP.SOS1Mode(), "cbc_sos1"),
(with_att(SCIP.Optimizer, "limits/time" => MAX_TIME*1), BilevelJuMP.SOS1Mode(), "scip_sos1"),
(
with_att(Gurobi.Optimizer, "TimeLimit" => MAX_TIME * 1),
BilevelJuMP.SOS1Mode(),
"gurobi_sos1",
),
(
with_att(CPLEX.Optimizer, "CPXPARAM_TimeLimit" => MAX_TIME * 1),
BilevelJuMP.SOS1Mode(),
"cplex_sos1",
),
(
with_att(
Xpress.Optimizer,
"MAXTIME" => -MAX_TIME * 1,
"logfile" => "output.log",
),
BilevelJuMP.SOS1Mode(),
"xpress_sos1",
),
(
with_att(Cbc.Optimizer, "seconds" => MAX_TIME * 1.0),
BilevelJuMP.SOS1Mode(),
"cbc_sos1",
),
(
with_att(SCIP.Optimizer, "limits/time" => MAX_TIME * 1),
BilevelJuMP.SOS1Mode(),
"scip_sos1",
),
#=
indicator
=#
Expand Down Expand Up @@ -162,12 +189,7 @@ SOLVERS = [
# (() -> AmplNLWriter.Optimizer("couenne"), BilevelJuMP.StrongDualityEqualityMode(), "couenne_sd"),
]

PROBLEMS = [
:SVR,
:TOLL,
:FORECAST,
:RAND,
]
PROBLEMS = [:SVR, :TOLL, :FORECAST, :RAND]

SEEDS = [
1234,
Expand All @@ -184,9 +206,9 @@ SEEDS = [

SVR = [
# (features, sample_size)
( 1, 10),
( 1, 10),
( 2, 10),
(1, 10),
(1, 10),
(2, 10),
# ( 5, 10),
# ( 1, 100),
# ( 2, 100), # 600
Expand All @@ -208,10 +230,10 @@ SVR = [

RAND = [
# (rows, cols)
( 5, 5),
( 10, 5),
( 5, 10),
( 10, 10),
(5, 5),
(10, 5),
(5, 10),
(10, 10),
# ( 50, 10),
# ( 10, 50),
# ( 50, 50), # 600
Expand Down Expand Up @@ -245,9 +267,9 @@ TOLL = [

FORECAST = [
# (products, sample_size)
( 1, 10),
( 2, 10),
( 5, 10),
(1, 10),
(2, 10),
(5, 10),
# ( 1, 100),
# ( 2, 100), # hard for prod10
# ( 5, 100), # 600
Expand All @@ -272,19 +294,25 @@ function separator()
println("============================================================")
println("============================================================")
println()
println()
return println()
end

function new_file()
cd(dirname(@__FILE__))
FILE = open("bench$(replace("$(now())",":"=>"_")).log", "w")
println(FILE, "opt_mode, prob, inst, primal_status, termination_status, solve_time, build_time, lower_obj, upper_obj")
println(
FILE,
"opt_mode, prob, inst, primal_status, termination_status, solve_time, build_time, lower_obj, upper_obj",
)
flush(FILE)
return FILE
end
function newline(FILE, data, opt, prb, inst, seed)
println(FILE, "$opt, $prb, $inst, $seed, $(data[1]),$(data[2]),$(data[3]),$(data[4]),$(data[5]),$(data[6]),$(data[7])")
flush(FILE)
println(
FILE,
"$opt, $prb, $inst, $seed, $(data[1]),$(data[2]),$(data[3]),$(data[4]),$(data[5]),$(data[6]),$(data[7])",
)
return flush(FILE)
end
FILE = new_file()
for seed in SEEDS
Expand All @@ -305,7 +333,6 @@ for seed in SEEDS
separator()
ret = bench_rand(rows, cols, 0.5, optimizer, mode, seed)
newline(FILE, ret, name, :RAND, (rows, cols), seed)

end
end
if :TOLL in PROBLEMS
Expand All @@ -331,4 +358,4 @@ end

close(FILE)

exit(0)
exit(0)
Loading

0 comments on commit 565c0ef

Please sign in to comment.