Skip to content

Commit

Permalink
code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
harshangrjn committed Sep 24, 2022
1 parent c86d72b commit 1fb1053
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 32 deletions.
17 changes: 9 additions & 8 deletions src/bounding_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ will choose one specific partition as the lower bound solution. The more partiti
better or finer bounding model relax the original MINLP while the more efforts required to solve
this MILP is required.
"""
function create_bounding_mip(m::Optimizer; use_disc = nothing)
function create_bounding_mip(m::Optimizer; use_disc = nothing)
if (use_disc === nothing)
if (m.logs[:n_iter] == 1) && (m.status[:local_solve] in union(STATUS_OPT, STATUS_LIMIT, STATUS_WARM_START))
if (m.logs[:n_iter] == 1) &&
(m.status[:local_solve] in union(STATUS_OPT, STATUS_LIMIT, STATUS_WARM_START))
# Setting up an initial partition
Alp.add_partition(m, use_solution = m.best_sol)
Alp.add_partition(m, use_solution = m.best_sol)
elseif m.logs[:n_iter] >= 2
# Add subsequent iteration partitions
Alp.add_partition(m)
end
discretization = m.discretization
else
else
discretization = use_disc
end

Expand Down Expand Up @@ -59,11 +60,11 @@ function amp_post_vars(m::Optimizer; kwargs...)
if haskey(options, :use_disc)
l_var = [
options[:use_disc][i][1] for
i in 1:(m.num_var_orig + m.num_var_linear_mip + m.num_var_nonlinear_mip)
i in 1:(m.num_var_orig+m.num_var_linear_mip+m.num_var_nonlinear_mip)
]
u_var = [
options[:use_disc][i][end] for
i in 1:(m.num_var_orig + m.num_var_linear_mip + m.num_var_nonlinear_mip)
i in 1:(m.num_var_orig+m.num_var_linear_mip+m.num_var_nonlinear_mip)
]
else
l_var = m.l_var_tight
Expand All @@ -72,10 +73,10 @@ function amp_post_vars(m::Optimizer; kwargs...)

JuMP.@variable(
m.model_mip,
x[i = 1:(m.num_var_orig + m.num_var_linear_mip + m.num_var_nonlinear_mip)]
x[i = 1:(m.num_var_orig+m.num_var_linear_mip+m.num_var_nonlinear_mip)]
)

for i in 1:(m.num_var_orig + m.num_var_linear_mip + m.num_var_nonlinear_mip)
for i in 1:(m.num_var_orig+m.num_var_linear_mip+m.num_var_nonlinear_mip)
# Interestingly, not enforcing category of lifted variables is able to improve performance
if i <= m.num_var_orig
if m.var_type_orig[i] == :Bin
Expand Down
20 changes: 14 additions & 6 deletions src/main_algorithm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,21 +208,28 @@ function presolve(m::Optimizer)
if Alp.get_option(m, :use_start_as_incumbent)
# Check for bound feasibility of the warm start value
if !(m.l_var_orig <= m.warm_start_value <= m.u_var_orig)
error("Provide a valid, feasible, warm starting point. Else, set use_start_as_incumbent to false")
error(
"Provide a valid, feasible, warm starting point. Else, set use_start_as_incumbent to false",
)
end

obj_warmval = if m.has_nl_objective
MOI.eval_objective(m.d_orig, m.warm_start_value)
else
MOI.Utilities.eval_variables(vi -> m.warm_start_value[vi.value], m.objective_function)
MOI.Utilities.eval_variables(
vi -> m.warm_start_value[vi.value],
m.objective_function,
)
end
Alp.update_incumbent(m, obj_warmval, m.warm_start_value)
m.status[:local_solve] = MOI.OPTIMIZE_NOT_CALLED
Alp.get_option(m, :log_level) > 0 && println(" Using warm starting point as a local incumbent solution with value $(round(m.best_obj, digits=4))")
Alp.get_option(m, :log_level) > 0 && println(
" Using warm starting point as a local incumbent solution with value $(round(m.best_obj, digits=4))",
)
else
Alp.local_solve(m, presolve = true)
end

# Solver status
if m.status[:local_solve] in union(STATUS_OPT, STATUS_LIMIT, STATUS_WARM_START)
if !Alp.get_option(m, :use_start_as_incumbent)
Expand Down Expand Up @@ -345,9 +352,10 @@ function load_nonlinear_model(m::Optimizer, model::MOI.ModelLike, l_var, u_var)
m.objective_function,
)
end

if m.d_orig !== nothing
block = MOI.NLPBlockData(m.nl_constraint_bounds_orig, m.d_orig, m.has_nl_objective)
block =
MOI.NLPBlockData(m.nl_constraint_bounds_orig, m.d_orig, m.has_nl_objective)
MOI.set(model, MOI.NLPBlock(), block)
end

Expand Down
3 changes: 2 additions & 1 deletion src/multilinear.jl
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,8 @@ function amp_post_inequalities_cont(
return
elseif Alp.get_option(m, :convhull_formulation) == "facet"
for j in 1:(partition_cnt-1) # Constraint cluster of α >= f(λ)
sliced_indices = Alp.collect_indices(λ[ml_indices][:indices], cnt, [1:j;], dim)
sliced_indices =
Alp.collect_indices(λ[ml_indices][:indices], cnt, [1:j;], dim)
JuMP.@constraint(
m.model_mip,
sum(α[var_ind][1:j]) >= sum(λ[ml_indices][:vars][sliced_indices])
Expand Down
9 changes: 2 additions & 7 deletions src/presolve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ function optimization_based_bound_tightening(
println(" Actual iterations (OBBT): ", (m.logs[:bt_iter]))

m.l_var_tight, m.u_var_tight = Alp.update_var_bounds(discretization)

if haskey(options, :use_tmc)
m.discretization = Alp.add_adaptive_partition(m, use_solution = m.best_sol)
end
Expand Down Expand Up @@ -283,12 +283,7 @@ end
This function takes in the initial discretization information and builds the OBBT model.
It is an algorithm specific function called by [`optimization_based_bound_tightening`](@ref).
"""
function create_obbt_model(
m::Optimizer,
discretization,
bound::Number;
kwargs...,
)
function create_obbt_model(m::Optimizer, discretization, bound::Number; kwargs...)

# options = Dict(kwargs)
start_build = time()
Expand Down
2 changes: 1 addition & 1 deletion src/utility.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ Check if the solution is always the same within the last disc_consecutive_forbid
"""
function check_solution_history(m::Optimizer, ind::Int)
Alp.get_option(m, :disc_consecutive_forbid) == 0 && return false
((m.logs[:n_iter]-1) < Alp.get_option(m, :disc_consecutive_forbid)) && return false
((m.logs[:n_iter] - 1) < Alp.get_option(m, :disc_consecutive_forbid)) && return false

sol_val = m.bound_sol_history[mod(
m.logs[:n_iter] - 2,
Expand Down
2 changes: 1 addition & 1 deletion src/variable_bounds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ Output::
l_var::Vector{Float64}, u_var::Vector{Float64}
"""
function update_var_bounds(discretization::Dict{Any, Any}; kwargs...)
function update_var_bounds(discretization::Dict{Any,Any}; kwargs...)
options = Dict(kwargs)

haskey(options, :len) ? len = options[:len] : len = length(keys(discretization))
Expand Down
23 changes: 15 additions & 8 deletions test/test_algorithm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -972,19 +972,27 @@ end
end

@testset "Warm start value used as the incumbent solution" begin

test_solver = JuMP.optimizer_with_attributes(
Alpine.Optimizer,
"nlp_solver" => IPOPT,
"mip_solver" => HIGHS,
"presolve_bt" => false,
"max_iter" => 1,
"use_start_as_incumbent" => false
"use_start_as_incumbent" => false,
)
sol = [579.30669, 1359.97066, 5109.97054, 182.0177, 295.60118, 217.9823, 286.41653, 395.60118]
sol = [
579.30669,
1359.97066,
5109.97054,
182.0177,
295.60118,
217.9823,
286.41653,
395.60118,
]

m = nlp3(solver = test_solver)
for i=1:length(m[:x])
for i in 1:length(m[:x])
JuMP.set_start_value(m[:x][i], sol[i])
end

Expand All @@ -1002,11 +1010,11 @@ end
"mip_solver" => HIGHS,
"presolve_bt" => true,
"max_iter" => 1,
"use_start_as_incumbent" => true
"use_start_as_incumbent" => true,
)

m = nlp3(solver = test_solver)
for i=1:length(m[:x])
for i in 1:length(m[:x])
JuMP.set_start_value(m[:x][i], sol[i])
end

Expand All @@ -1017,5 +1025,4 @@ end
@test isapprox(alp.best_bound, 4810.212866711817, atol = 1E-6)
@test alp.warm_start_value == sol
@test alp.options.use_start_as_incumbent == true

end
end

0 comments on commit 1fb1053

Please sign in to comment.