LpCache class
#include <gtopt/lp_cache.hpp>
Post-solve cache for LinearInterface.
Owns the 9 scalar / vector fields that hold the most recent successful solve's outputs (objective value, kappa, row/col counts, optimality flag, primal/dual vectors). Used by downstream readers — OutputContext, Benders cut assembly, SDDP state propagation — to access the solution after the solver backend has been released under LowMemoryMode::compress, without paying for a re-solve.
Empty when the backend was never released, never solved to optimum, or LowMemoryMode::off (in which case the live backend is the canonical source of truth and the LI getters read directly from SolverBackend::col_solution() etc.).
Public functions
- auto backend_solution_fresh() const -> bool noexcept
- void clear_all_solution_vectors() noexcept
- void clear_col_bounds_cache() noexcept
- auto col_cost() const -> std::span<const double> noexcept
- auto col_cost_buffer(std::size_t n) -> std::span<double>
- auto col_low() const -> std::span<const double> noexcept
- auto col_low_buffer(std::size_t n) -> std::span<double>
- auto col_sol() const -> std::span<const double> noexcept
- auto col_sol_buffer(std::size_t n) -> std::span<double>
- auto col_upp() const -> std::span<const double> noexcept
- auto col_upp_buffer(std::size_t n) -> std::span<double>
- void drop_solution_caches() noexcept
- void invalidate_optimal_on_mutation() noexcept
- auto is_optimal() const -> bool noexcept
- auto kappa() const -> const std::optional<double>& noexcept
- void mark_solution_fresh(bool v) noexcept
- auto numcols() const -> Index noexcept
- auto numrows() const -> Index noexcept
- auto obj_value() const -> double noexcept
- auto row_dual() const -> std::span<const double> noexcept
- auto row_dual_buffer(std::size_t n) -> std::span<double>
- void set_is_optimal(bool v) noexcept
- void set_kappa(std::optional<double> v) noexcept
- void set_numcols(Index v) noexcept
- void set_numrows(Index v) noexcept
- void set_obj_value(double v) noexcept
- auto size_bytes() const -> std::size_t noexcept
- auto validate_consistency() const -> bool noexcept
Function documentation
void gtopt:: LpCache:: clear_all_solution_vectors() noexcept
Drop every cached solution + column-bound vector (col_sol, col_cost, row_dual, col_low, col_upp) and clear the optimality flag. Used by reconstruct_backend() and release_backend() paths that fully invalidate the cached solve. The col-bound vectors are evicted through clear_col_bounds_cache() so the eviction policy lives in exactly one place.
void gtopt:: LpCache:: clear_col_bounds_cache() noexcept
Drop only the column-bound cache vectors (col_low + col_upp).
Called from reconstruct_backend() to prevent stale forward-pass solver bounds (which may reflect internal presolve tightening) from being served by get_col_low_raw() / get_col_upp_raw() during the subsequent backward-pass apply_alpha_floor infinity checks. Under LowMemoryMode::compress, populate_solution_cache_post_solve() snapshots solver-internal column bounds after every solve — including any presolve-tightened finite values — into these vectors. Without this clear, the stale values survive the apply_post_load_replay cycle (the ReplayGuard suppresses invalidate_cached_optimal_on_ mutation() during replay), causing is_pos_inf / is_neg_inf to mis-classify a state-variable column as bounded when it should be structurally unbounded, which makes the alpha floor formula compute a deeply negative floor and drives UB negative under 2+ scenarios. The primal/dual solution vectors (col_sol, col_cost, row_dual) are intentionally left intact: they are gated on is_optimal() (cleared by mark_solution_fresh(false) in reconstruct_backend()) so no consumer reads them after reconstruction without a fresh solve.
std::span<const double> gtopt:: LpCache:: col_low() const noexcept
Cached column-bound vectors snapshotted post-solve via SolverBackend::fill_col_lower / fill_col_upper. When non-empty these are the source of truth for get_col_low_raw / get_col_upp_raw and the get_col_sol() clamp path under low_memory = compress — preventing each read from forcing the CPLEX/MindOpt/Gurobi backend to allocate a full numcols-sized scratch on its own (m_collb_ / m_colub_). Empty under LowMemoryMode::off (live backend serves bounds directly).
std::span<double> gtopt:: LpCache:: col_sol_buffer(std::size_t n)
Resize the col_sol vector to n elements and return a writable span over it. The single write entry-point used by SolverBackend::fill_col_sol(span) in compress mode.
void gtopt:: LpCache:: drop_solution_caches() noexcept
C3 — drop col_sol + col_cost (the largest-footprint vectors), keep row_dual + scalars + optimality flag. Used after the iteration's state CSV has been written and the backward pass has consumed any reduced-cost data — the row duals + objective value are still needed downstream for output writing.
void gtopt:: LpCache:: invalidate_optimal_on_mutation() noexcept
C2 — flip is_optimal to false. Idempotent. Does NOT clear the col_sol / col_cost / row_dual vectors (some readers in compress mode still want the last-known values via the empty-fallback path even after a structural mutation).
void gtopt:: LpCache:: mark_solution_fresh(bool v) noexcept
C5 — track whether the live backend's col_solution() / is_proven_optimal() reflect a fresh solve. Cleared on reconstruct_backend(); set on successful resolve() / initial_solve().
void gtopt:: LpCache:: set_is_optimal(bool v) noexcept
C4 — flip the optimality flag. The only path that may set it to true. Callers that pass false here are equivalent to invalidate_optimal_on_mutation() (kept distinct for call-site readability).
std::size_t gtopt:: LpCache:: size_bytes() const noexcept
C6 — total bytes held by the cached vectors. Used by LinearInterface::cache_size_bytes() for memory-usage reporting / TUI dashboards. Includes the three solution vectors plus the two column-bound vectors snapshotted post-solve.
bool gtopt:: LpCache:: validate_consistency() const noexcept
C1 debug self-check. Returns true iff the cache is internally consistent (is_optimal ⇒ vector sizes match scalar counts). Returns true when is_optimal is false (no constraint on vector sizes in that case).