gtopt/constraint_expr.hpp file

AST types for parsed user constraint expressions.

This module defines the Abstract Syntax Tree (AST) types used to represent parsed user constraint expressions. The AST is produced by the ConstraintParser and consumed by the LP construction layer to add user-defined constraints to the LinearProblem.

Supported element types and attributes

Element typeAttributes
generatorgeneration, capainst (alias: capacity)
demandload, fail, capainst (alias: capacity)
lineflow, flowp, flown, lossp, lossn,
capainst (alias: capacity)
batteryenergy, charge, discharge, spill (alias: drain),
eini, efin, soft_emin,
capainst (alias: capacity)
converterflow (= +discharge − charge), charge, discharge
reservoirvolume (alias: energy), extraction,
spill (alias: drain),
eini, efin, soft_emin
bustheta (alias: angle)
waterwayflow
turbinegeneration
junctiondrain
flowflow (alias: discharge)
flow_rightflow, fail
volume_rightextraction (aliases: flow, fout), saving
seepageflow (alias: seepage)
reserve_provisionup (aliases: uprovision, up_provision),
dn (aliases: dprovision, dn_provision, down)
reserve_zoneup (aliases: urequirement, up_requirement),
dn (aliases: drequirement, dn_requirement,
down)
lng_terminalenergy (tank volume), delivery,
spill (alias: drain), eini, efin, soft_emin

Variable scaling

Some LP variables are internally scaled for numerical conditioning. User constraints are written in physical units; the resolver automatically applies the correct scale factor to each coefficient so that the LP constraint is dimensionally correct.

The resolver uses LinearProblem::get_col_scale() to read the scale factor that was stored at variable-creation time, so all current and future scaled variables are handled uniformly.

VariableScale (physical = LP × scale)
reservoir.volumeenergy_scale: auto-computed from capacity
(capacity/1000 rounded to next power of 10),
or set explicitly via energy_scale JSON field
reservoir.extractionflow_scale (default 1.0; overridable via
variable_scales option)
reservoir.spillflow_scale (same)
reservoir.einienergy_scale (same as reservoir.volume)
reservoir.efinenergy_scale (same as reservoir.volume)
reservoir.soft_eminenergy_scale (same as reservoir.volume)
battery.energyenergy_scale (default 1.0)
battery.chargeflow_scale (default 1.0)
battery.dischargeflow_scale (default 1.0)
battery.spillflow_scale (default 1.0)
battery.einienergy_scale (default 1.0)
battery.efinenergy_scale (default 1.0)
battery.soft_eminenergy_scale (default 1.0)
bus.theta1 / scale_theta (θ_LP = scale_theta · θ)
all others1.0 (no scaling)

Stage-level vs block-level attributes

Most attributes are block-level variables (one value per block per stage). Some are stage-level (one value per stage), returned for every block:

Stage-level attributes
generator.capainst, demand.capainst, line.capainst
battery.capainst, battery.eini, battery.efin, battery.soft_emin
reservoir.eini, reservoir.efin, reservoir.soft_emin

Stage-level attributes may appear in any per-block constraint expression; the same LP column is referenced across all blocks in the stage.

Grammar (pseudo-BNF)

constraint   := expr comp_op expr [',' for_clause]
             |  number comp_op expr comp_op number [',' for_clause]

expr         := term (('+' | '-') term)*

term         := [coeff '*'] element_ref
             |  [coeff '*'] state_ref
             |  [coeff '*'] sum_expr        // ELEMENT aggregation, see below
             |  [coeff '*'] time_agg_expr   // TIME aggregation, see below
             |  ['-'] number

coeff        := number
             |  coeff_profile          // F9 per-block coefficient

coeff_profile := '[' number (',' number)* [','] ']'
             // A bracketed list of per-block coefficients standing in
             // for the leading scalar multiplier of a single
             // variable-bearing term.  Block ordinal `b` (0-based,
             // within the stage's block list) selects entry
             // `profile[min(b, size-1)]`; a short profile broadcasts
             // its last value.  Example:
             //     [1, 2, 3] * generator("G1").generation <= 100
             // Mirrors the per-block `rhs [v0, v1, ...]` schedule on
             // the LHS.  May appear at most once per term and only as
             // the term's leading multiplier.

element_ref  := element_type '(' string ')' '.' attribute

state_ref    := 'state' '(' element_ref ')'

sum_expr     := 'sum' '(' element_type '(' string_list ')' '.'
                attribute ')'
             |  'sum' '(' element_type '(' 'all' [ ',' 'type' '=' string ]
')'
                '.' attribute ')'

─── TWO `sum` SPELLINGS (read carefully — they are DIFFERENT operators) ───

  ELEMENT aggregation:  sum( type(ids).attr )           — uses PARENTHESES
    Sums one attribute over a SET OF ELEMENTS of one type, all evaluated
    at the SAME ambient (scenario, stage, block).  AST: `SumElementRef`.
    Example:  sum(generator(all).generation) <= 300

  TIME aggregation:     sum{ idx in window } inner_expr  — uses BRACES
    Sums an inner linear expression over TIME (the blocks of the named
    window), letting a coarse-scope (`stage`/`phase`/`global`) row reach
    per-block variables.  AST: `TimeAggRef`.  Over RATE variables (flow
    [m³/s]) the energy form MUST carry the per-block duration weight —
    spell it `sum{b in stage} dur[b] * waterway('w').flow` (the `dur[b]`
    prefix sets `weight = duration`).  `daily_sum` is the special case
    `sum{b in day}` (count) / `sum{b in day} dur[b]*…` (energy).

time_agg_expr := 'sum' '{' IDENT 'in' time_window '}'
                 [ 'dur' '[' IDENT ']' '*' ] inner_expr

time_window  := 'stage' | 'day'

string_list  := string (',' string)*

element_type := 'generator' | 'demand' | 'line' | 'battery'
             |  'converter' | 'reservoir' | 'bus'
             |  'waterway'  | 'turbine'
             |  'junction'  | 'flow' | 'seepage'
             |  'flow_right' | 'volume_right'
             |  'reserve_provision' | 'reserve_zone'
             |  'lng_terminal'

attribute    := IDENT

comp_op      := '<=' | '>=' | '='

for_clause   := 'for' '(' index_spec (',' index_spec)* ')'

index_spec   := index_dim ('in' | '=') index_set

index_dim    := 'scenario' | 'stage' | 'block'

index_set    := 'all'
             |  '{' index_values '}'
             |  number '..' number
             |  number

index_values := index_value (',' index_value)*

index_value  := number
             |  number '..' number

comment      := '#' <any>* EOL
             |  '//' <any>* EOL

Namespaces

namespace gtopt
namespace gtopt::detail

Classes

struct gtopt::AbsExpr
abs(linear_expression) term (F5).
struct gtopt::ConstraintDomain
Domain over which a constraint is instantiated.
struct gtopt::ConstraintExpr
Complete parsed constraint expression.
struct gtopt::ConstraintTerm
A single term in a constraint expression.
struct gtopt::ElementRef
Reference to a gtopt element and one of its LP attributes.
struct gtopt::IfCondAtom
struct gtopt::IfExpr
Data-only conditional expression (F8).
struct gtopt::IndexRange
Specifies a set of index values for a time dimension.
struct gtopt::MinMaxExpr
max(arg1, arg2, ...) / min(arg1, arg2, ...) term (F7).
struct gtopt::SumElementRef
Aggregation over multiple elements of the same type.
struct gtopt::SumPredicate
A predicate that filters a sum(...) aggregation (F4).
struct gtopt::TimeAggRef
sum{idx in window} [dur[idx] *] inner_expr — TIME aggregation.