ConstraintTerm struct
#include <gtopt/constraint_expr.hpp>
A single term in a constraint expression.
Either:
- coefficient × element reference (single variable term)
- coefficient × sum reference (aggregation term)
- coefficient × named parameter (resolved at LP time)
- coefficient × abs(linear) wrapper (F5)
- coefficient × min/max(args…) wrapper (F7)
- coefficient × if-then-else wrapper (F8)
- standalone coefficient (constant term, all nullopt)
The wrapper payloads (AbsExpr, MinMaxExpr, IfExpr) are stored by std::shared_ptr<const …> so that ConstraintTerm remains default-copyable even though those types recursively contain std::vector<ConstraintTerm>. Sharing is fine because the AST is immutable after parsing.
Public functions
- auto coeff_at(std::size_t block_ordinal) const -> double noexcept
Public variables
- std::shared_ptr<const AbsExpr> abs_expr
abs(...)wrapper (F5)- std::optional<std::vector<double>> coeff_profile
- double coefficient
- Scalar multiplier.
- std::size_t column
- bool duration_weighted
- std::optional<ElementRef> element
- Single element (nullopt = none).
- std::shared_ptr<const IfExpr> if_expr
if ... then ... else ...(F8)- std::shared_ptr<const MinMaxExpr> minmax_expr
min/maxwrapper (F7)- std::optional<std::string> param_name
- Named user parameter (nullopt = none).
- std::optional<SumElementRef> sum_ref
- Sum aggregation (nullopt = none).
- std::shared_ptr<const TimeAggRef> time_agg
sum{idx in window} ...TIME aggregation (piece-4)
Function documentation
double gtopt:: ConstraintTerm:: coeff_at(std::size_t block_ordinal) const noexcept
Resolve the effective coefficient for the given 0-based block ordinal within the stage. Returns the profile entry (clamped to the last when the ordinal runs past the profile) when a per-block profile is set, otherwise the scalar coefficient. Does NOT apply the duration_weighted Δt factor (that needs the block's duration).
Variable documentation
std::optional<std::vector<double>> gtopt:: ConstraintTerm:: coeff_profile
Optional per-block coefficient profile (F9).
When set, the term's effective coefficient at block ordinal b (0-based position within the stage's block list) is coeff_profile[min(b, size()-1)] — the last entry broadcasts to every trailing block, so a profile shorter than the stage's block count still resolves. The coefficient scalar is folded into the profile at parse time (see scale_terms), so a profile is always the authoritative per-block value when present.
Authored in the grammar as a bracketed list standing in for the leading scalar multiplier of a single variable-bearing term, e.g. [1, 2, 3] * generator("G1").generation. Mirrors the per-block UserConstraint.rhs schedule on the LHS. Unset ⇒ the scalar coefficient is used uniformly across all blocks (backward compatible).
std::size_t gtopt:: ConstraintTerm:: column
Stored 1-BASED column of the term's anchor token in the source expression string, populated by the parser when the term is constructed. Surfaced in resolver diagnostics so a stale reference reports a at column N source location instead of just the constraint name (task #55, the P1 safety win).
Convention: the anchor is the token that names the referenced entity — the element-type identifier for element / sum_ref terms (generator(...)), the identifier for bare param_name terms. 0 means UNSET (term constructed by code that doesn't track positions — e.g. internal lowering for abs / min / max slack columns). Stored 1-based so the sentinel doesn't collide with the legitimate byte-offset-0 (= column 1) case; format_error_with_caret and make_unresolved_element_error use the value verbatim.
bool gtopt:: ConstraintTerm:: duration_weighted
When true, this term's coefficient is additionally multiplied by the ambient block's duration (Δt, hours) at row-assembly time. Set by the parser when a term is multiplied by the singleton scalar block.duration (e.g. generator('g').generation * block.duration), turning a per-block rate (power, flow) into the per-block energy/volume it contributes — mirroring the native StorageLP energy balance, which scales every flow column by block.duration(). Orthogonal to coeff_profile; the Δt factor is applied on top of coeff_at(...) in user_constraint_lp (which has the block), not in coeff_at here. Both may be set at once — [k0,k1] * gen * block.duration is supported: the profile picks the per-block coefficient and Δt then multiplies it.