|
| 1 | +""" |
| 2 | + CrossoverParameters |
| 3 | +
|
| 4 | +Post-solve crossover settings (V1: primal threshold snapping to bounds). |
| 5 | +
|
| 6 | +# Fields |
| 7 | +
|
| 8 | +$(TYPEDFIELDS) |
| 9 | +""" |
| 10 | +@kwdef struct CrossoverParameters{T <: Number} |
| 11 | + "whether to apply crossover after PDLP/PDHG terminates optimally" |
| 12 | + enabled::Bool = true |
| 13 | + "distance to a bound below which the primal is snapped to that bound" |
| 14 | + threshold::T = 1.0e-6 |
| 15 | + "tolerance for treating lower and upper bounds as equal (fixed variable)" |
| 16 | + fixed_tol::T = 1.0e-8 |
| 17 | + "revert to the pre-crossover primal if KKT errors regress beyond tolerances" |
| 18 | + rollback_on_kkt_regression::Bool = true |
| 19 | + "relative KKT increase tolerated above the pre-crossover value (0 = no increase)" |
| 20 | + kkt_rtol::T = 0.0 |
| 21 | + "tighten infinite bounds from equality rows before snapping" |
| 22 | + use_effective_bounds::Bool = true |
| 23 | +end |
| 24 | + |
| 25 | +function Base.show(io::IO, params::CrossoverParameters) |
| 26 | + (; enabled, threshold, fixed_tol, rollback_on_kkt_regression, kkt_rtol, use_effective_bounds) = params |
| 27 | + return print( |
| 28 | + io, |
| 29 | + "CrossoverParameters: enabled=$enabled, threshold=$threshold, fixed_tol=$fixed_tol, ", |
| 30 | + "rollback_on_kkt_regression=$rollback_on_kkt_regression, kkt_rtol=$kkt_rtol, ", |
| 31 | + "use_effective_bounds=$use_effective_bounds", |
| 32 | + ) |
| 33 | +end |
| 34 | + |
| 35 | +""" |
| 36 | + crossover_kkt_acceptable(err_before, err_after, termination_reltol, params) |
| 37 | +
|
| 38 | +Return `true` if the post-crossover KKT errors should be kept. |
| 39 | +
|
| 40 | +When `rollback_on_kkt_regression` is true, reject the crossover if either: |
| 41 | +- `relative(err_after) > termination_reltol` (would invalidate the optimality certificate), or |
| 42 | +- `relative(err_after) > relative(err_before) * (1 + kkt_rtol)`. |
| 43 | +""" |
| 44 | +function crossover_kkt_acceptable( |
| 45 | + err_before::KKTErrors, |
| 46 | + err_after::KKTErrors, |
| 47 | + termination_reltol, |
| 48 | + params::CrossoverParameters, |
| 49 | + ) |
| 50 | + params.rollback_on_kkt_regression || return true |
| 51 | + rel_before = relative(err_before) |
| 52 | + rel_after = relative(err_after) |
| 53 | + rel_after <= termination_reltol || return false |
| 54 | + rel_after <= rel_before * (1 + params.kkt_rtol) || return false |
| 55 | + return true |
| 56 | +end |
| 57 | + |
| 58 | +"""True if coordinate `j` is on a finite box bound (within `atol`).""" |
| 59 | +function _crossover_at_box_bound(xj, lj, uj; atol::Real = 1.0e-12) |
| 60 | + return (isfinite(lj) && abs(xj - lj) <= atol) || |
| 61 | + (isfinite(uj) && abs(xj - uj) <= atol) |
| 62 | +end |
| 63 | + |
| 64 | +""" |
| 65 | + crossover_effective_bounds(milp, x) |
| 66 | +
|
| 67 | +Box bounds tightened with implied limits from equality rows. |
| 68 | +
|
| 69 | +When an equality row has exactly one variable not yet on a finite box bound, that |
| 70 | +row's implied bound is used to fill in an infinite bound (e.g. `x₁ ≤ 1` from |
| 71 | +`x₁ + x₂ = 1` when `x₂` is already on its lower bound). |
| 72 | +""" |
| 73 | +function crossover_effective_bounds( |
| 74 | + milp::MILP{T}, |
| 75 | + x::AbstractVector{T}; |
| 76 | + bound_atol::Real = 1.0e-12, |
| 77 | + eq_atol::Real = 1.0e-12, |
| 78 | + ) where {T} |
| 79 | + lv_eff = copy(milp.lv) |
| 80 | + uv_eff = copy(milp.uv) |
| 81 | + m, n = size(milp.A) |
| 82 | + at_box = [ |
| 83 | + _crossover_at_box_bound(x[j], milp.lv[j], milp.uv[j]; atol = bound_atol) for j in 1:n |
| 84 | + ] |
| 85 | + for i in 1:m |
| 86 | + isapprox(milp.lc[i], milp.uc[i]; atol = eq_atol) || continue |
| 87 | + free = Int[] |
| 88 | + slack = milp.lc[i] |
| 89 | + @inbounds for j in 1:n |
| 90 | + aij = milp.A[i, j] |
| 91 | + aij == 0 && continue |
| 92 | + if at_box[j] |
| 93 | + slack -= aij * x[j] |
| 94 | + else |
| 95 | + push!(free, j) |
| 96 | + end |
| 97 | + end |
| 98 | + length(free) == 1 || continue |
| 99 | + j = only(free) |
| 100 | + aij = milp.A[i, j] |
| 101 | + if aij > 0 |
| 102 | + implied = slack / aij |
| 103 | + if !isfinite(uv_eff[j]) |
| 104 | + uv_eff[j] = implied |
| 105 | + else |
| 106 | + uv_eff[j] = min(uv_eff[j], implied) |
| 107 | + end |
| 108 | + elseif aij < 0 |
| 109 | + implied = slack / aij |
| 110 | + if !isfinite(lv_eff[j]) |
| 111 | + lv_eff[j] = implied |
| 112 | + else |
| 113 | + lv_eff[j] = max(lv_eff[j], implied) |
| 114 | + end |
| 115 | + end |
| 116 | + end |
| 117 | + return lv_eff, uv_eff |
| 118 | +end |
| 119 | + |
| 120 | +""" |
| 121 | + crossover_threshold!(x, lv, uv, params::CrossoverParameters) |
| 122 | +
|
| 123 | +Snap primal `x` to variable bounds using a fixed threshold (naive crossover). |
| 124 | +
|
| 125 | +For each coordinate: fixed variables are set to their bound; otherwise, if `x` is |
| 126 | +within `threshold` of a finite lower or upper bound, it is moved to that bound. |
| 127 | +""" |
| 128 | +function crossover_threshold!( |
| 129 | + x::AbstractVector{T}, |
| 130 | + lv::AbstractVector{T}, |
| 131 | + uv::AbstractVector{T}, |
| 132 | + params::CrossoverParameters{T}, |
| 133 | + ) where {T} |
| 134 | + (; threshold, fixed_tol) = params |
| 135 | + # Broadcast (GPU-safe); same order as the former scalar loop: fixed → lower → upper. |
| 136 | + fixed = abs.(lv .- uv) .<= fixed_tol |
| 137 | + @. x = ifelse(fixed, lv, x) |
| 138 | + near_l = isfinite.(lv) .& (x .- lv .<= threshold) |
| 139 | + @. x = ifelse(near_l, lv, x) |
| 140 | + near_u = isfinite.(uv) .& (uv .- x .<= threshold) |
| 141 | + @. x = ifelse(near_u, uv, x) |
| 142 | + return x |
| 143 | +end |
| 144 | + |
| 145 | +function crossover_threshold!( |
| 146 | + x::AbstractVector{T}, |
| 147 | + milp::MILP{T}, |
| 148 | + params::CrossoverParameters{T}, |
| 149 | + ) where {T} |
| 150 | + if params.use_effective_bounds |
| 151 | + lv_eff, uv_eff = crossover_effective_bounds(milp, x) |
| 152 | + else |
| 153 | + lv_eff, uv_eff = milp.lv, milp.uv |
| 154 | + end |
| 155 | + crossover_threshold!(x, lv_eff, uv_eff, params) |
| 156 | + return x |
| 157 | +end |
| 158 | + |
| 159 | +function crossover_threshold!( |
| 160 | + sol::PrimalDualSolution{T}, |
| 161 | + milp::MILP{T}, |
| 162 | + params::CrossoverParameters{T}, |
| 163 | + ) where {T} |
| 164 | + crossover_threshold!(sol.x, milp, params) |
| 165 | + return sol |
| 166 | +end |
| 167 | + |
| 168 | +"""Count coordinates changed by crossover (exact compare).""" |
| 169 | +function crossover_n_changed(x_after, x_before) |
| 170 | + return count(i -> x_after[i] != x_before[i], eachindex(x_before)) |
| 171 | +end |
| 172 | + |
| 173 | +""" |
| 174 | + fraction_at_bounds(x, milp; atol=1e-12) |
| 175 | +
|
| 176 | +Fraction of coordinates equal to a finite bound (diagnostic for crossover effect). |
| 177 | +""" |
| 178 | +function fraction_at_bounds( |
| 179 | + x::AbstractVector, |
| 180 | + milp::MILP; |
| 181 | + atol::Real = 1.0e-12, |
| 182 | + ) |
| 183 | + (; lv, uv) = milp |
| 184 | + n = length(x) |
| 185 | + n == 0 && return 0.0 |
| 186 | + at_l = isfinite.(lv) .& (abs.(x .- lv) .<= atol) |
| 187 | + at_u = isfinite.(uv) .& (abs.(x .- uv) .<= atol) |
| 188 | + return sum(at_l .| at_u) / n |
| 189 | +end |
0 commit comments