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

[RFC] Translate empty column rule and reduction to apply! syntax #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions docs/src/manual/reductions.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ CurrentModule = MathOptPresolve
```@docs
RemoveEmptyRow
RemoveEmptyRows
RemoveEmptyColumn
RemoveEmptyColumns
FixSingleVariable
```
86 changes: 85 additions & 1 deletion src/lp/empty_column.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,71 @@
@doc raw"""
RemoveEmptyColumn <: AbstractRule

Eliminate a single column if it is empty, i.e. all coefficients are zero.

## Presolve

If column $j$ is empty, variable $x_{j}$ is fixed to $x_{j}^{*}$ as follows
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
If column $j$ is empty, variable $x_{j}$ is fixed to $x_{j}^{*}$ as follows
If column $j$ is empty, variable $x_{j}$ is fixed to $x_{j}^{*}$ as follows:


| $c_{j}$ | $l_{j}$ | $u_{j}$ | $x^{*}_{j}$ |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all assuming minimization, correct?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. I'll mention it in the docs.

|--------:|---------------:|---------------:|------------:|
| $<0$ | - | $∈ \mathbb{R}$ | $u_{j}$ |
| $<0$ | - | $+∞$ | $+∞^{†}$ |
| $0$ | $∈ \mathbb{R}$ | - | $l_{j}$ |
| $0$ | $-∞$ | $∈ \mathbb{R}$ | $u_{j}$ |
| $0$ | $-∞$ | $+∞$ | $0$ |
| $>0$ | $∈ \mathbb{R}$ | - | $l_{j}$ |
| $>0$ | $-∞$ | - | $-∞^{†}$ |
``^{†}`` problem is dual infeasible

## Dual infeasibility (unboundedness) checks

For a prescribed tolerance $ϵ ≥ 0$, the problem is dual infeasible if
column $j$ is empty and either
1. Variable $j$ has no lower bound, and its objective coefficient is positive,
i.e., $l^{c}_{j} = -∞$ and $c_{j} > ϵ$.
2. column $j$ is empty, has no upper bound, and its objective coefficient is negative,
i.e., $l^{c}_{j} = +∞$ and $c_{j} < -ϵ$.

The corresponding unbounded rays (dual infeasibility certificates) are
1. $x = -e_{j}$,
2. $x = e_{j}$,

where $e_{j}$ is the $j$-th vector of the canonical basis.

## Postsolve

TODO

## Misc

* This is a dual reduction, and it can handle integer variables.
* This reduction is non-destructive.
* This reduction does not create any fill-in.
"""
struct RemoveEmptyColumn <: AbstractRule
j::Int
end

@doc raw"""
EmptyColumn{T} <: AbstractReduction{T}

Column $j$ is empty (all coefficients are zero).
"""
struct EmptyColumn{T} <: AbstractReduction{T}
j::Int # variable index
x::T # Variable value
s::T # Reduced cost
end

function remove_empty_column!(ps::PresolveData{T}, j::Int) where {T}
function apply!(
ps::PresolveData{T},
r::RemoveEmptyColumn,
config::PresolveOptions{T},
) where {T}
j = r.j
ϵ = config.DualTolerance

ps.pb0.is_continuous || error("Empty column routine currently only supported for LPs.")

# Sanity check
Expand Down Expand Up @@ -105,3 +166,26 @@ function postsolve!(sol::Solution{T}, op::EmptyColumn{T}) where {T}
sol.s_upper[op.j] = neg_part(op.s)
return nothing
end

"""
RemoveEmptyColumns <: AbstractRule

Remove all empty columns in the problem.

See [`RemoveEmptyColumn`](@ref).
"""
struct RemoveEmptyColumns <: AbstractRule end

function apply!(
ps::PresolveData{T},
::RemoveEmptyColumns,
config::PresolveOptions{T}
) where {T}
for j in 1:ps.pb0.nvar
if ps.colflag[j] && (ps.nzcol[j] == 0)
apply!(ps, RemoveEmptyColumn(j), config)
ps.status == NOT_INFERRED || return nothing
end
end
return nothing
end
25 changes: 4 additions & 21 deletions src/presolve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ function presolve!(ps::PresolveData{T}) where {T}
# I. Remove all fixed variables, empty rows and columns
@_return_if_inferred apply!(ps, RemoveFixedVariables(), config)
@_return_if_inferred apply!(ps, RemoveEmptyRows(), config)
@_return_if_inferred remove_empty_columns!(ps)
@_return_if_inferred apply!(ps, RemoveEmptyColumns(), config)

# Identify row singletons
ps.row_singletons = [i for (i, nz) in enumerate(ps.nzrow) if ps.rowflag[i] && nz == 1]
Expand All @@ -435,7 +435,7 @@ function presolve!(ps::PresolveData{T}) where {T}
@debug "Presolve pass $npasses" ps.nrow ps.ncol
round_integer_bounds!(ps)
@_return_if_inferred bounds_consistency_checks!(ps)
@_return_if_inferred remove_empty_columns!(ps)
@_return_if_inferred apply!(ps, RemoveEmptyColumns(), config)


# Remove all fixed variables
Expand All @@ -458,7 +458,7 @@ function presolve!(ps::PresolveData{T}) where {T}
@_return_if_inferred remove_dominated_columns!(ps)
end

@_return_if_inferred remove_empty_columns!(ps)
@_return_if_inferred apply!(ps, RemoveEmptyColumns(), config) # Why this here??
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea why this was here in the first place


@debug("Presolved problem info",
ps.pb0.ncon, ps.nrow,
Expand Down Expand Up @@ -593,23 +593,6 @@ function bounds_consistency_checks!(ps::PresolveData{T}) where {T}
return nothing
end

"""
remove_empty_columns!(ps::PresolveData)

Remove all empty columns.

Called once at the beginning of the presolve procedure.
If an empty column is created later, it is removed on the spot.
"""
function remove_empty_columns!(ps::PresolveData{T}) where {T}
for j in 1:ps.pb0.nvar
remove_empty_column!(ps, j)
ps.status == NOT_INFERRED || break
end
return nothing
end


"""
round_integer_bounds!(ps::PresolveData)

Expand Down Expand Up @@ -681,7 +664,7 @@ function remove_dominated_columns!(ps::PresolveData{T}) where {T}
iszero(aij) && continue # empty column

# Strengthen dual bounds
#=
#=

=#
cj = ps.obj[j]
Expand Down
20 changes: 11 additions & 9 deletions test/lp/empty_column.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function emtpy_column_tests(T::Type)
function empty_column_tests(T::Type)

# We test all the following combinations:
#=
#=
min c * x
s.t. lb ≤ x ≤ ub

Expand All @@ -14,11 +14,12 @@ function emtpy_column_tests(T::Type)
(-∞, +∞) | -∞ | +∞ | 0
( l, u) | l | u | l
( l, +∞) | l | +∞ | l
------------------------------ =#
------------------------------
=#
function build_problem(l, u, c)
pb = MathOptPresolve.ProblemData{T}()
pb = MOP.ProblemData{T}()

MathOptPresolve.load_problem!(pb, "Test",
MOP.load_problem!(pb, "Test",
true, [c], zero(T),
spzeros(T, 0, 1), T[], T[], [l], [u],
)
Expand All @@ -32,10 +33,11 @@ function emtpy_column_tests(T::Type)
@testset "$((l, u, c))" begin
pb = build_problem(l, u, c)

ps = MathOptPresolve.PresolveData(pb)
ps = MOP.PresolveData(pb)
config = MOP.PresolveOptions{T}()

# Remove empty variable
MathOptPresolve.remove_empty_column!(ps, 1)
MOP.apply!(ps, MOP.RemoveEmptyColumn(1), config)

if c > 0 && !isfinite(l)
@test ps.status == MOP.DUAL_INFEASIBLE
Expand Down Expand Up @@ -64,7 +66,7 @@ function emtpy_column_tests(T::Type)
# Check that operation was recorded correctly
@test length(ps.ops) == 1
op = ps.ops[1]
@test isa(op, MathOptPresolve.EmptyColumn)
@test isa(op, MOP.EmptyColumn)
@test op.j == 1
end
end # testset
Expand All @@ -75,6 +77,6 @@ end

@testset "Empty column" begin
for T in COEFF_TYPES
@testset "$T" begin emtpy_column_tests(T) end
@testset "$T" begin empty_column_tests(T) end
end
end