gtopt::LinearProblem class

Main class for building and manipulating linear planning problems.

This class provides functionality to construct a linear problem by adding variables (columns) and constraints (rows), setting coefficients, and converting to solver-ready formats.

Public types

using cols_t = StableVector<SparseCol>
using index_t = FlatLinearProblem::index_t
using rows_t = StableVector<SparseRow>
using SparseMatrix = std::vector<SparseVector>
using SparseVector = flat_map<ColIndex, double>

Public static variables

static double DblMax

Constructors, destructors, conversion operators

LinearProblem(std::string name = {}) explicit constexpr noexcept

Public functions

template<typename SparseCol SparseCol = gtopt::SparseCol>
auto add_col(SparseCol&& col) -> ColIndex
void add_fast_start_cols(std::span<const ColIndex> columns)
void add_obj_constant(double c) noexcept
template<typename SparseRow SparseRow = gtopt::SparseRow>
auto add_row(SparseRow&& row) -> RowIndex
void add_sos2(std::initializer_list<ColIndex> columns)
void add_sos2(std::span<const ColIndex> columns)
template<typename Self>
auto col_at(this Self&& self, ColIndex index) -> auto&&
auto flatten(const LpMatrixOptions& opts = {}) -> FlatLinearProblem
auto get_coeff(RowIndex row, ColIndex col) const -> double
auto get_col_lowb(ColIndex index) const -> auto
auto get_col_scale(ColIndex index) const -> auto
auto get_col_uppb(ColIndex index) const -> auto
auto get_cols() const -> const cols_t& noexcept
Read-only access to the raw column vector (for fingerprinting).
auto get_fast_start_cols() const -> std::span<const index_t> noexcept
Fast-start columns registered so far (test / diagnostic accessor).
auto get_numcols() const -> index_t
auto get_numrows() const -> index_t
auto get_obj_constant() const -> double noexcept
auto get_rows() const -> const rows_t& noexcept
Read-only access to the raw row vector (for fingerprinting).
auto infinity() const -> double noexcept
Current infinity value (DblMax if not explicitly set).
auto label_maker() const -> const LabelMaker& noexcept
Get the LabelMaker (default-constructed = names off if not set).
void reserve(size_t est_cols, size_t est_rows)
template<typename Self>
auto row_at(this Self&& self, RowIndex index) -> auto&&
void set_coeff(RowIndex row, ColIndex col, double coeff)
void set_col_lowb(ColIndex index, double lowb)
void set_col_uppb(ColIndex index, double uppb)
void set_infinity(double inf) noexcept
void set_label_maker(LabelMaker lm) noexcept
void set_variable_scale_map(VariableScaleMap map)
Set the VariableScaleMap used for automatic scale resolution in add_col.
auto variable_scale_map() const -> const VariableScaleMap& noexcept
Get the VariableScaleMap (empty if not set).

Function documentation

gtopt::LinearProblem::LinearProblem(std::string name = {}) explicit constexpr noexcept

Parameters
name Problem name

Constructs a new linear problem

template<typename SparseCol SparseCol = gtopt::SparseCol>
ColIndex gtopt::LinearProblem::add_col(SparseCol&& col)

Parameters
col Column (variable) definition
Returns Index of the added column

Adds a new variable to the problem

void gtopt::LinearProblem::add_fast_start_cols(std::span<const ColIndex> columns)

Register integer columns of a fast-start unit (its commitment u/v/w) — see FlatLinearProblem::fast_start_cols.

void gtopt::LinearProblem::add_obj_constant(double c) noexcept

Accumulate a physical-scale constant into the objective.

Use this when a model rewrite folds a variable away by substitution. Example — demand-failure substitution: the algebraic identity fail = lmax − load lets us drop both the fail column and the lcol + fcol = lmax balance row, at the cost of:

  1. shifting lcol's objective coefficient by −fail_cost (the linear-in-load part of the substituted cost), and
  2. carrying +fail_cost × lmax as an LP-external constant (the constant part of the substitution).

c is interpreted on the physical cost scale (same units as the original cost fields of SparseCol). It is NOT divided by scale_objective — the solver never sees this term. Final reporting via LinearInterface::get_obj_value() combines: physical_obj = solver_raw_obj × scale_objective + obj_constant so the obj-constant flows through SDDP cuts, forward-pass cost accumulation, and standalone obj-value reporting at full fidelity regardless of how aggressive scale_objective is.

Calls accumulate (additive); pass a negative c to subtract.

template<typename SparseRow SparseRow = gtopt::SparseRow>
RowIndex gtopt::LinearProblem::add_row(SparseRow&& row)

Parameters
row Row (constraint) definition
Returns Index of the added row

Adds a new constraint to the problem

void gtopt::LinearProblem::add_sos2(std::initializer_list<ColIndex> columns)

Convenience overload accepting an initializer_list of column indices. Forwards to the span-based add_sos2. Reserved for callers that build a small SOS2 inline (e.g. lp.add_sos2({c1, c2, c3}) in a unit test) and have no pre-allocated vector to hand a span over. Production callers in line_losses.cpp (issue #504) use the span form because they construct the column list iteratively per (line, block).

void gtopt::LinearProblem::add_sos2(std::span<const ColIndex> columns)

Parameters
columns Column indices in geometric breakpoint order.

Declare a Type-2 Special-Ordered-Set (SOS2) over the given column indices. Stored on the LinearProblem until flatten() copies it into FlatLinearProblem::sos2_sets; LinearInterface:: load_flat then forwards each set to SolverBackend::add_sos2. See SolverBackend::add_sos2 for the supported-backend matrix. Indices MUST already be valid column indices (call AFTER the relevant add_col calls).

Vacuous sets (size < 2) are dropped silently — SOS2 over a single column has no effect, and an empty span is most likely an LP-build edge case that the caller would prefer not to see surface as a structured error in SolverBackend::add_sos2.

template<typename Self>
auto&& gtopt::LinearProblem::col_at(this Self&& self, ColIndex index)

Parameters
self Deduced object reference
index Column index
Returns Reference to the column

Gets a reference to a column by index

FlatLinearProblem gtopt::LinearProblem::flatten(const LpMatrixOptions& opts = {})

Parameters
opts LP build options
Returns Flat representation of the problem

Builds the flat (column-major) LP representation

double gtopt::LinearProblem::get_coeff(RowIndex row, ColIndex col) const

Parameters
row Row index
col Column index
Returns Coefficient value

Gets a coefficient from the constraint matrix

auto gtopt::LinearProblem::get_col_lowb(ColIndex index) const

Parameters
index Column index
Returns Lower bound value

Gets the lower bound of a column

auto gtopt::LinearProblem::get_col_scale(ColIndex index) const

Parameters
index Column index
Returns Scale factor (1.0 = no scaling)

Gets the physical-to-LP scale factor of a column. physical_value = LP_value × scale.

auto gtopt::LinearProblem::get_col_uppb(ColIndex index) const

Parameters
index Column index
Returns Upper bound value

Gets the upper bound of a column

index_t gtopt::LinearProblem::get_numcols() const

Returns Number of columns (variables) in the problem

index_t gtopt::LinearProblem::get_numrows() const

Returns Number of rows (constraints) in the problem

double gtopt::LinearProblem::get_obj_constant() const noexcept

Current accumulated objective constant (physical units). Zero in every existing call site that does not opt into the substitution rewrite. Named with the get_ prefix to match the other LP-side accessors (get_obj_value family).

void gtopt::LinearProblem::reserve(size_t est_cols, size_t est_rows)

Parameters
est_cols Estimated number of columns (variables)
est_rows Estimated number of rows (constraints)

Pre-reserves capacity for columns, rows, and coefficients. Call before the build loop to avoid repeated reallocations.

template<typename Self>
auto&& gtopt::LinearProblem::row_at(this Self&& self, RowIndex index)

Parameters
self Deduced object reference
index Row index
Returns Reference to the row

Gets a reference to a row by index

void gtopt::LinearProblem::set_coeff(RowIndex row, ColIndex col, double coeff)

Parameters
row Row index
col Column index
coeff Coefficient value

Sets a coefficient in the constraint matrix

void gtopt::LinearProblem::set_col_lowb(ColIndex index, double lowb)

Parameters
index Column index
lowb New lower bound value

Sets the lower bound of a column

void gtopt::LinearProblem::set_col_uppb(ColIndex index, double uppb)

Parameters
index Column index
uppb New upper bound value

Sets the upper bound of a column

void gtopt::LinearProblem::set_infinity(double inf) noexcept

Parameters
inf Target infinity value (e.g. LinearInterface::infinity())

Sets the infinity value used for bound normalization.

When set to a value smaller than DblMax (e.g. from the solver backend's infinity()), add_col()/add_row() will clamp DblMax bounds to ±infinity. This ensures that flattened LP vectors never contain raw DblMax values, avoiding noisy solver warnings (e.g. HiGHS "bounds >= 1e20 treated as +Infinity").

void gtopt::LinearProblem::set_label_maker(LabelMaker lm) noexcept

Set the LabelMaker used to generate column/row labels during flatten(). The LabelMaker is copied into FlatLinearProblem so LinearInterface can continue generating labels for dynamically added columns/rows.