Skip to content
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
17 changes: 14 additions & 3 deletions src/algorithms/LOA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,15 @@ function _store_results(
model.termination_status = MOI.TIME_LIMIT
elseif master_status === nothing
model.termination_status = MOI.ITERATION_LIMIT
elseif master_status == MOI.DUAL_INFEASIBLE
elseif master_status == MOI.DUAL_INFEASIBLE ||
master_status == MOI.INFEASIBLE_OR_UNBOUNDED
# solvers often report an unbounded master as
# INFEASIBLE_OR_UNBOUNDED
model.termination_status = MOI.OTHER_LIMIT
model.raw_status = "No feasible incumbent: the master is " *
"unbounded (no OA cut bounds the objective yet); the " *
"model itself may be unbounded."
"unbounded or infeasible ($master_status); with no OA " *
"cut bounding the objective yet, the model itself may " *
"be unbounded."
else
model.termination_status = MOI.OTHER_LIMIT
model.raw_status = "No feasible incumbent: the master " *
Expand All @@ -465,6 +469,13 @@ function _store_results(
model.primal_status = MOI.FEASIBLE_POINT
model.incumbent = incumbent_result.point
model.objective_value = best_objective
# the master bounds only unvisited combinations, so the incumbent
# bounds the visited ones
if master_bound !== nothing
master_bound = sense == MOI.MAX_SENSE ?
max(master_bound, best_objective) :
min(master_bound, best_objective)
end
model.objective_bound = master_bound === nothing ? nothing :
Float64(master_bound)
combinations_exhausted = master_status == MOI.INFEASIBLE &&
Expand Down
26 changes: 26 additions & 0 deletions src/algorithms/LOA/master.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ function _build_master(model::Optimizer, problem::_Problem)
end
alpha_oa = MOI.add_variable(mip)
sense = problem.sense
if _is_linear(problem.objective)
_add_linear_objective_row(mip, variable_map, problem.objective,
alpha_oa, sense)
end
oa_objective = MOI.ScalarAffineFunction(
[MOI.ScalarAffineTerm(1.0, alpha_oa)], 0.0)
MOI.set(mip, MOI.ObjectiveSense(), sense)
Expand All @@ -78,6 +82,28 @@ function _build_master(model::Optimizer, problem::_Problem)
oa_objective)
end

# A linear objective bounds `alpha_oa` exactly from the start: MIN
# `objective <= alpha_oa`, MAX symmetric. Without it `alpha_oa` stays
# free until an NLP returns a point, so a first infeasible combination
# leaves the master unbounded.
function _add_linear_objective_row(
mip::MOI.ModelLike,
variable_map::AbstractDict,
objective::MOI.AbstractScalarFunction,
alpha_oa::MOI.VariableIndex,
sense::MOI.OptimizationSense
)
objective_func = _map_to(variable_map, _to_affine(objective))
alpha = _to_affine(alpha_oa)
if sense == MOI.MAX_SENSE
body = MOI.Utilities.operate(-, Float64, alpha, objective_func)
else
body = MOI.Utilities.operate(-, Float64, objective_func, alpha)
end
MOI.Utilities.normalize_and_add_constraint(mip, body, MOI.LessThan(0.0))
return
end

function _build_set_cover(model::Optimizer, problem::_Problem)
mip = _instantiate(model.mip_solver)
binaries = Set(problem.binaries)
Expand Down
31 changes: 21 additions & 10 deletions src/algorithms/LOA/nlp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -297,17 +297,27 @@ end
################################################################################
# NLPF (FEASIBILITY SUBPROBLEM)
################################################################################
_nlpf_slacked(func, u, ::MOI.LessThan{Float64}) =
MOI.Utilities.operate(-, Float64, func, u)
_nlpf_slacked(func, u, ::MOI.GreaterThan{Float64}) =
MOI.Utilities.operate(+, Float64, func, u)
_nlpf_slacked(func, u, ::MOI.AbstractScalarSet) = nothing
# Rows of `func in set` relaxed by the nonnegative slack `u`; an
# equality or interval relaxes on both sides
_nlpf_slacked_rows(func, u, set::MOI.LessThan{Float64}) =
[(MOI.Utilities.operate(-, Float64, func, u), set)]
_nlpf_slacked_rows(func, u, set::MOI.GreaterThan{Float64}) =
[(MOI.Utilities.operate(+, Float64, func, u), set)]
function _nlpf_slacked_rows(func, u, set::MOI.EqualTo{Float64})
return _nlpf_slacked_rows(func, u, MOI.Interval(set.value, set.value))
end
function _nlpf_slacked_rows(func, u, set::MOI.Interval{Float64})
return [(MOI.Utilities.operate(-, Float64, func, u),
MOI.LessThan(set.upper)),
(MOI.Utilities.operate(+, Float64, func, u),
MOI.GreaterThan(set.lower))]
end

# The slacked feasibility NLP: one nonnegative `u` relaxes every scalar
# inequality row (bounds and equalities stay exact) and is minimized.
# Its solution is a linearization site for an infeasible combination.
# row (bounds stay exact) and is minimized. Its solution is a
# linearization site for an infeasible combination.
# One slacked copy at a combination: bounds and binary fixes exact,
# every inequality row relaxed by the copy's nonnegative `u`.
# every row relaxed by the copy's nonnegative `u`.
function _add_slacked_copy(
nlp::MOI.ModelLike,
model::Optimizer,
Expand Down Expand Up @@ -340,8 +350,9 @@ function _add_slacked_copy(
end
for (func, set) in rows
mapped = _map_to(variable_map, func)
slacked = _nlpf_slacked(mapped, u, set)
MOI.add_constraint(nlp, something(slacked, mapped), set)
for (slacked, slacked_set) in _nlpf_slacked_rows(mapped, u, set)
MOI.add_constraint(nlp, slacked, slacked_set)
end
end
return variable_map, u
end
Expand Down
95 changes: 91 additions & 4 deletions test/loa.jl
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,26 @@ function test_max_sense_linear()
@test value(z[2]) ≈ 1.0 atol = 1e-5
end

# The master bounds only unvisited combinations, so once the optimum is
# visited its value can fall below the incumbent; the reported bound
# is the better of the two and the gap is never negative.
function test_bound_not_worse_than_incumbent()
model = Model(_loa_optimizer())
set_silent(model)
@variable(model, 0 <= x[1:2] <= 9)
@variable(model, b[1:2], Bin)
for i in 1:2
@constraint(model, [1, b[i], 1 - b[i], x[i], x[i]] in
DA.DisjunctionSet([[MOI.LessThan(2.0)], [MOI.GreaterThan(8.0)]]))
end
@objective(model, Max, sum(x))
optimize!(model)
@test termination_status(model) == MOI.LOCALLY_SOLVED
@test objective_value(model) ≈ 18.0 atol = 1e-4
@test objective_bound(model) >= objective_value(model) - 1e-6
@test relative_gap(model) >= 0.0
end

# Two disjunctions: port of DP.jl test_loa_solve_two_disjunctions.
function test_two_disjunctions()
model = Model(_loa_optimizer())
Expand Down Expand Up @@ -728,20 +748,61 @@ function test_iteration_limit_with_incumbent()
@test objective_bound(model) == -Inf
end

# An unbounded master (no OA cuts yet bound alpha_oa) surfaces its
# status instead of looping.
# An unbounded master (no OA cuts yet bound alpha_oa under a nonlinear
# objective) surfaces its status instead of looping. Solvers report it
# as INFEASIBLE_OR_UNBOUNDED as often as DUAL_INFEASIBLE.
function test_master_abnormal_status()
model = Model(_loa_optimizer(DA.SetCoverIterationLimit() => 0))
set_silent(model)
@variable(model, 0 <= x <= 10)
@variable(model, z[1:2], Bin)
@constraint(model, [1, z[1], z[2], x, x] in DA.DisjunctionSet([
[MOI.LessThan(3.0)], [MOI.LessThan(7.0)]]))
@objective(model, Min, x)
@objective(model, Min, x^2)
optimize!(model)
@test termination_status(model) == MOI.OTHER_LIMIT
@test result_count(model) == 0
@test occursin("master solve finished", raw_status(model))
@test occursin("unbounded or infeasible", raw_status(model))
end

# A linear objective bounds alpha_oa exactly, so the master is bounded
# before any NLP point: the same instance with `Min x` solves even with
# set covering off.
function test_linear_objective_bounds_master()
model = Model(_loa_optimizer(DA.SetCoverIterationLimit() => 0))
set_silent(model)
@variable(model, 0 <= x <= 10)
@variable(model, z[1:2], Bin)
@constraint(model, [1, z[1], z[2], x, x] in DA.DisjunctionSet([
[MOI.LessThan(3.0)], [MOI.LessThan(7.0)]]))
@objective(model, Min, x)
optimize!(model)
@test termination_status(model) == MOI.LOCALLY_SOLVED
@test objective_value(model) ≈ 0.0 atol = 1e-5
@test objective_bound(model) ≈ 0.0 atol = 1e-5
end

# Complementarity pairs [v == 0] or [y == c]: the all-second-disjunct
# combination asks y == 0 and y == 6 at once. The NLPF relaxes the
# equalities on both sides, so that combination still yields a point
# and an objective cut; with exact equalities the master stays
# unbounded under the quadratic objective.
function test_equality_disjunct_infeasible_combination()
model = Model(_loa_optimizer())
set_silent(model)
@variable(model, 0 <= y <= 6)
@variable(model, -1 <= v[1:2] <= 0)
@variable(model, b[1:2], Bin)
equalities = [[MOI.EqualTo(0.0)], [MOI.EqualTo(0.0)]]
@constraint(model, [1, b[1], 1 - b[1], v[1], y] in
DA.DisjunctionSet(equalities))
@constraint(model, [1, b[2], 1 - b[2], v[2], y - 6] in
DA.DisjunctionSet(equalities))
@objective(model, Min, (y - 2)^2 + v[1] + v[2])
optimize!(model)
@test termination_status(model) == MOI.LOCALLY_SOLVED
@test objective_value(model) ≈ 0.0 atol = 1e-4
@test value(y) ≈ 2.0 atol = 1e-3
end

# An unbounded NLP restriction proves the model unbounded: the solve
Expand Down Expand Up @@ -925,6 +986,28 @@ function test_master_abnormal_status_with_incumbent()
@test occursin("limit hit", raw_status(model))
end

# The master finishes abnormally before any incumbent: with set
# covering off, the first MIP solve is the master and reports a node
# limit, which surfaces as the master's own status.
function test_master_abnormal_status_no_incumbent()
mip = () -> MockSolver(HiGHS.Optimizer, fail_from = 1)
factory = optimizer_with_attributes(
() -> DA.Optimizer(Ipopt.Optimizer, mip),
DA.SetCoverIterationLimit() => 0)
model = Model(factory)
set_silent(model)
@variable(model, 0 <= x <= 10)
@variable(model, z[1:2], Bin)
@constraint(model, [1, z[1], z[2], x, x] in DA.DisjunctionSet([
[MOI.GreaterThan(2.0)], [MOI.GreaterThan(5.0)]]))
@objective(model, Min, x)
optimize!(model)
@test termination_status(model) == MOI.OTHER_LIMIT
@test result_count(model) == 0
@test occursin("master solve finished with status NODE_LIMIT",
raw_status(model))
end

################################################################################
# UNIT TESTS
################################################################################
Expand Down Expand Up @@ -1216,6 +1299,7 @@ end
test_combination_sources()
test_quadratic_objective()
test_max_sense_linear()
test_bound_not_worse_than_incumbent()
test_two_disjunctions()
test_nonlinear_global()
test_nonlinear_exp_global()
Expand All @@ -1236,6 +1320,8 @@ end
test_iteration_limit_no_incumbent()
test_iteration_limit_with_incumbent()
test_master_abnormal_status()
test_linear_objective_bounds_master()
test_equality_disjunct_infeasible_combination()
test_unbounded_model()
test_nlp_failure_unresolved()
test_nlp_failure_no_incumbent()
Expand All @@ -1245,6 +1331,7 @@ end
test_strict_constant_solvers()
test_time_limit_with_incumbent()
test_master_abnormal_status_with_incumbent()
test_master_abnormal_status_no_incumbent()
test_nonconvex_never_optimal()
test_feasibility_sense()
test_linear_interval_disjunct()
Expand Down
Loading