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

Remove unnecessary calls to Base.eval #251

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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: 5 additions & 6 deletions src/bounding_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ end
function amp_post_lifted_objective(m::Optimizer)

#if isa(m.obj_expr_orig, Number)
if expr_isconst(m.obj_expr_orig)
if m.obj_expr_orig == :(+())
JuMP.@objective(m.model_mip, m.sense_orig, 0.0)
elseif expr_isconst(m.obj_expr_orig)
# This eval is okay because expr_isconst is true
JuMP.@objective(m.model_mip, m.sense_orig, eval(m.obj_expr_orig))
elseif m.obj_structure == :affine
JuMP.@objective(
Expand Down Expand Up @@ -235,7 +238,6 @@ function add_partition(m::Optimizer; kwargs...)
point_vec = m.best_bound_sol

if isa(get_option(m, :disc_add_partition_method), Function)
# m.discretization = eval(get_option(m, :disc_add_partition_method))(m, use_disc=discretization, use_solution=point_vec)
m.discretization = get_option(m, :disc_add_partition_method)(
m,
use_disc = discretization,
Expand Down Expand Up @@ -441,9 +443,6 @@ function update_partition_scaling_factor(m::Optimizer, presolve = false)
m.logs[:n_iter] > 2 && return get_option(m, :partition_scaling_factor) # Stop branching after the second iterations

ratio_pool = [8:2:20;] # Built-in try range
convertor = Dict(MOI.MAX_SENSE => :<, MOI.MIN_SENSE => :>)
# revconvertor = Dict(MOI.MAX_SENSE => :>, MOI.MIN_SENSE => :<)

incumb_ratio = ratio_pool[1]
is_min_sense(m) ? incumb_res = -Inf : incumb_res = Inf
res_collector = Float64[]
Expand All @@ -469,7 +468,7 @@ function update_partition_scaling_factor(m::Optimizer, presolve = false)
create_bounding_mip(m, use_disc = branch_disc)
res = disc_branch_solve(m)
push!(res_collector, res)
if eval(convertor[m.sense_orig])(res, incumb_res) # && abs(abs(collector[end]-res)/collector[end]) > 1e-1 # %1 of difference
if m.sense_orig == MOI.MAX_SENSE ? res < incumb_res : res > incumb_res
incumb_res = res
incumb_ratio = r
end
Expand Down
11 changes: 7 additions & 4 deletions src/log.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ function logging_summary(m::Optimizer)
end

function logging_head(m::Optimizer)
if is_min_sense(m)
if !is_max_sense(m)
printstyled("LOWER-BOUNDING ITERATIONS", color = :cyan, bold = true)
UB_iter = "Incumbent"
UB = "Best Incumbent"
LB = "Lower Bound"
elseif is_max_sense(m)
else
printstyled("UPPER-BOUNDING ITERATIONS", color = :cyan, bold = true)
UB_iter = "Incumbent"
UB = "Best Incumbent"
Expand Down Expand Up @@ -148,8 +148,11 @@ function logging_row_entry(m::Optimizer; kwargs...)
spc = max(0, b_len - length(objstr))
end
UB_block = string(" ", objstr, " "^spc)

if expr_isconst(m.obj_expr_orig)
if m.obj_expr_orig == :(+())
bdstr = 0.0
spc = b_len - 1
elseif expr_isconst(m.obj_expr_orig)
# This eval is okay because expr_isconst is true
bdstr = eval(m.obj_expr_orig)
spc = b_len - length(bdstr)
elseif isa(m.logs[:bound][end], Float64)
Expand Down
9 changes: 6 additions & 3 deletions src/main_algorithm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ function check_exit(m::Optimizer)
# constant objective with feasible local solve check
if expr_isconst(m.obj_expr_orig) &&
(m.status[:local_solve] == MOI.OPTIMAL || m.status == MOI.LOCALLY_SOLVED)
# m.best_bound = eval(m.obj_expr_orig)
m.best_bound = m.obj_expr_orig
m.best_rel_gap = 0.0
m.best_abs_gap = 0.0
Expand Down Expand Up @@ -544,7 +543,12 @@ function bounding_solve(m::Optimizer)
)+1] = copy(candidate_bound_sol) # Requires proper offseting
end
push!(m.logs[:bound], candidate_bound)
if eval(convertor[m.sense_orig])(candidate_bound, m.best_bound)
is_tighter = ifelse(
m.sense_orig == MOI.MAX_SENSE,
candidate_bound < m.best_bound,
candidate_bound > m.best_bound,
)
if is_tighter
m.best_bound = candidate_bound
m.best_bound_sol = copy(candidate_bound_sol)
m.status[:bounding_solve] = status
Expand Down Expand Up @@ -590,7 +594,6 @@ function pick_disc_vars(m::Optimizer)
disc_var_pick = get_option(m, :disc_var_pick)

if isa(disc_var_pick, Function)
# eval(get_option(m, :disc_var_pick))(m)
disc_var_pick(m)
length(m.disc_vars) == 0 &&
length(m.nonconvex_terms) > 0 &&
Expand Down
11 changes: 9 additions & 2 deletions src/multilinear.jl
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,15 @@ function amp_warmstart_α(m::Optimizer, α::Dict)
for i in 1:m.bound_sol_pool[:cnt]
m.bound_sol_pool[:stat][i] == :Warmstarter &&
(m.bound_sol_pool[:stat][i] = :Alive) # reset the status if not dead
if m.bound_sol_pool[:stat][i] != :Dead &&
eval(comp_opr[m.sense_orig])(m.bound_sol_pool[:obj][i], ws_obj)
if m.bound_sol_pool[:stat][i] == :Dead
continue
end
is_better = ifelse(
m.sense_orig == MOI.MAX_SENSE,
m.bound_sol_pool[:obj][i] > ws_obj,
m.bound_sol_pool[:obj][i] < ws_obj,
)
if is_better
ws_idx = i
ws_obj = m.bound_sol_pool[:obj][i]
end
Expand Down
2 changes: 1 addition & 1 deletion src/nlexpr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ function expr_constr_parsing(expr::Expr, m::Optimizer, idx::Int = 0)

# First process user-defined structures in-cases of over-ride
# for i in 1:length(get_option(m, :constr_patterns))
# is_structural = eval(get_option(m, :constr_patterns)[i])(expr, m, idx)
# is_structural = get_option(m, :constr_patterns)[i](expr, m, idx)
# return
# end

Expand Down
2 changes: 1 addition & 1 deletion src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function detect_nonconvex_terms(expr::Any, constr_id::Int, m::Optimizer; kwargs.

# First process user-defined structures in-cases of over-ride
# for i in 1:length(get_option(m, :term_patterns))
# skip, expr = eval(get_option(m, :term_patterns)[i])(expr, constr_id, m)
# skip, expr = get_option(m, :term_patterns)[i](expr, constr_id, m)
# skip && return expr
# end

Expand Down
1 change: 0 additions & 1 deletion src/presolve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ function bound_tightening_wrapper(m::Optimizer; use_bound = true, kwargs...)
elseif get_option(m, :presolve_bt_algo) == 2
optimization_based_bound_tightening(m, use_bound = use_bound, use_tmc = true)
elseif isa(get_option(m, :presolve_bt_algo), Function)
# eval(get_option(m, :presolve_bt_algo))(m)
get_option(m, :presolve_bt_algo)(m)
else
error("Unrecognized bound tightening algorithm")
Expand Down
4 changes: 1 addition & 3 deletions src/utility.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,12 @@ discretization_to_bounds(d::Dict, l::Int) = update_var_bounds(d, len = l)
Update the data structure with feasible solution and its associated objective (if better)
"""
function update_incumbent(m::Optimizer, objval::Float64, sol::Vector)
convertor = Dict(MOI.MAX_SENSE => :>, MOI.MIN_SENSE => :<)
push!(m.logs[:obj], objval)
if eval(convertor[m.sense_orig])(objval, m.best_obj) #&& !eval(convertor[m.sense_orig])(objval, m.best_bound)
if m.sense_orig == MOI.MAX_SENSE ? objval > m.best_obj : objval < m.best_obj
m.best_obj = objval
m.best_sol = sol
m.detected_incumbent = true
end

return
end

Expand Down
Loading