gtopt::FlatHelper class

Converts multi-dimensional optimization data into flat vectors for LP formulation.

This helper class transforms scenario/stage/block indexed data into:

  • Flat vectors for LP matrix construction
  • Valid/invalid markers for sparse problems
  • Scaled values using provided factors

Key features:

  • Handles active element filtering (only processes active scenarios/stages/blocks)
  • Applies scaling factors during flattening
  • Supports multiple index holder types (GSTB, STB, ST, T)
  • Provides constexpr accessors for active elements
  • Thread-safe operations (all methods are const)

Derived classes

class SystemContext

Constructors, destructors, conversion operators

FlatHelper(const SimulationLP& simulation) explicit

Public functions

auto active_block_count() const -> size_t noexcept
auto active_blocks() const -> const auto& noexcept
auto active_scenario_count() const -> size_t noexcept
auto active_scenarios() const -> const auto& noexcept
auto active_stage_blocks() const -> const auto& noexcept
auto active_stage_count() const -> size_t noexcept
auto active_stages() const -> const auto& noexcept
template<typename Projection, typename Value = Index, typename Factor = block_factor_matrix_t>
auto flat(const GSTBIndexHolder<Value>& hstb, Projection proj, const Factor& factor = Factor()) const -> auto
Flattens GSTB-indexed data into dense vectors with optional scaling.
template<typename Projection, typename Value = Index, typename Factor = block_factor_matrix_t>
auto flat(const STBIndexHolder<Value>& hstb, Projection proj, const Factor& factor, const STIndexHolder<double>& st_scale) const -> auto
template<typename Projection, typename Value = Index, typename Factor = block_factor_matrix_t>
auto flat(const STBIndexHolder<Value>& hstb, Projection proj, const Factor& factor = Factor()) const -> auto
template<typename Projection, typename Value = Index, typename Factor = scenario_stage_factor_matrix_t>
auto flat(const STIndexHolder<Value>& hst, Projection proj, const Factor& factor = Factor()) const -> auto
template<typename Projection = std::identity, typename Value = Index, typename Factor = stage_factor_matrix_t>
auto flat(const TIndexHolder<Value>& ht, Projection proj = {}, const Factor& factor = {}) const -> auto
template<typename Projection, typename Value = Index, typename Factor = block_factor_matrix_t>
auto flat_sparse(const GSTBIndexHolder<Value>& hstb, Projection proj, const Factor& factor = Factor()) const -> SparseEntries
Sparse long-form counterpart of flat() for GSTB holders.
template<typename Projection, typename Value = Index, typename Factor = block_factor_matrix_t>
auto flat_sparse(const STBIndexHolder<Value>& hstb, Projection proj, const Factor& factor, const STIndexHolder<double>& st_scale) const -> SparseEntries
template<typename Projection, typename Value = Index, typename Factor = block_factor_matrix_t>
auto flat_sparse(const STBIndexHolder<Value>& hstb, Projection proj, const Factor& factor = Factor()) const -> SparseEntries
template<typename Projection, typename Value = Index, typename Factor = scenario_stage_factor_matrix_t>
auto flat_sparse(const STIndexHolder<Value>& hst, Projection proj, const Factor& factor = Factor()) const -> SparseEntries
template<typename Projection = std::identity, typename Value = Index, typename Factor = stage_factor_matrix_t>
auto flat_sparse(const TIndexHolder<Value>& ht, Projection proj = {}, const Factor& factor = {}) const -> SparseEntries
auto is_first_scenario(const ScenarioUid& scenario_uid) const -> bool noexcept
auto is_first_stage(const StageLP& stage) const -> bool noexcept
auto is_first_stage(const StageUid& stage_uid) const -> bool noexcept
auto is_last_stage(const StageUid& stage_uid) const -> bool noexcept
auto prev_stage(const StageLP& stage) const -> auto noexcept
auto simulation() const -> const SimulationLP& noexcept
auto st_uids() const -> STUids
auto stb_uids() const -> STBUids
auto t_uids() const -> TUids

Function documentation

template<typename Projection, typename Value = Index, typename Factor = block_factor_matrix_t>
auto gtopt::FlatHelper::flat(const GSTBIndexHolder<Value>& hstb, Projection proj, const Factor& factor = Factor()) const

Flattens GSTB-indexed data into dense vectors with optional scaling.

Parameters
hstb The GSTB-indexed data holder
proj Projection function to apply to each value
factor Optional scaling factors (applied after projection)
Returns Pair of (values, valid) vectors

Processes a 3D (Scenario/Stage/Block) indexed container into:

  • Values vector with projected/scaled values
  • Valid vector marking which indices had data

Implemented as flat_sparse() + a dense scatter, preserving the historical dense contract exactly (see scatter_flat). The write-out path no longer uses this — OutputContext consumes flat_sparse() directly — but the dense form remains for reference/tests and any consumer that wants the full grid.

template<typename Projection, typename Value = Index, typename Factor = block_factor_matrix_t>
auto gtopt::FlatHelper::flat(const STBIndexHolder<Value>& hstb, Projection proj, const Factor& factor, const STIndexHolder<double>& st_scale) const

flat() overload that applies an additional per-(scenario,stage) scale factor on top of the block-level factor. Used by StorageLP::add_to_output to back-scale volume-balance duals when the daily-cycle option was active for a stage (scale = 24/stage_dur).

The st_scale lookup is performed once per (scenario,stage) pair, outside the block loop, so there is no per-block overhead. When a (suid,tuid) key is absent from st_scale the scale defaults to 1.0 (i.e. no scaling is applied for that stage), making this a safe drop-in for the plain flat() call when st_scale is empty.

template<typename Projection, typename Value = Index, typename Factor = block_factor_matrix_t>
SparseEntries gtopt::FlatHelper::flat_sparse(const GSTBIndexHolder<Value>& hstb, Projection proj, const Factor& factor = Factor()) const

Sparse long-form counterpart of flat() for GSTB holders.

Parameters
hstb The GSTB-indexed data holder
proj Projection function to apply to each value
factor Optional scaling factors (applied after projection)
Returns (grid-slot, value) entries, slot-ascending

Collects ONLY the holder's own entries as (grid-slot, value) pairs sorted ascending by slot. The slot index is exactly the position the dense flat() vector fills for the same entry (si * stride + block_base + bi), so callers can label each sample with the slot's (scenario, stage, block) uids without materialising the dense grid. This is the write-out hot path: per element-field stream the work is O(H log H) in the holder size H instead of O(active grid).

Stray holder keys (inactive scenario/stage uid, unknown uid, block uid filed under the wrong stage, P1 zero-bound-elided blocks) are skipped exactly as the dense fill skips them. Explicit zero values are KEPT (the long-form writer drops them at emission time, after any post-collection value snapping).

template<typename Projection, typename Value = Index, typename Factor = block_factor_matrix_t>
SparseEntries gtopt::FlatHelper::flat_sparse(const STBIndexHolder<Value>& hstb, Projection proj, const Factor& factor, const STIndexHolder<double>& st_scale) const

flat_sparse() overload that applies an additional per-(scenario,stage) scale factor on top of the block-level factor — sparse counterpart of the st_scale flat() overload below (StorageLP daily-cycle dual back-scaling).

template<typename Projection, typename Value = Index, typename Factor = block_factor_matrix_t>
SparseEntries gtopt::FlatHelper::flat_sparse(const STBIndexHolder<Value>& hstb, Projection proj, const Factor& factor = Factor()) const

Sparse long-form counterpart of flat() for STB holders. See the GSTB overload for the contract. Blocks not present in a stage's active block list are simply skipped: the P1 zero-bound skip in generator_lp.cpp:131 / waterway_lp.cpp:72 / demand_lp.cpp:206 may have elided individual block entries while leaving the outer (scenario, stage) key populated; elided slots emit no entry, same as the outer-key-missing case.

template<typename Projection, typename Value = Index, typename Factor = scenario_stage_factor_matrix_t>
SparseEntries gtopt::FlatHelper::flat_sparse(const STIndexHolder<Value>& hst, Projection proj, const Factor& factor = Factor()) const

Sparse long-form counterpart of flat() for ST holders. Slot index is si * active_stage_count + ti.

template<typename Projection = std::identity, typename Value = Index, typename Factor = stage_factor_matrix_t>
SparseEntries gtopt::FlatHelper::flat_sparse(const TIndexHolder<Value>& ht, Projection proj = {}, const Factor& factor = {}) const

Sparse long-form counterpart of flat() for T holders. Slot index is the active-stage position.