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 type | Attributes |
|---|---|
| generator | generation, capainst (alias: capacity) |
| demand | load, fail, capainst (alias: capacity) |
| line | flow, flowp, flown, lossp, lossn, |
| capainst (alias: capacity) | |
| battery | energy, charge, discharge, spill (alias: drain), |
| eini, efin, soft_emin, | |
| capainst (alias: capacity) | |
| converter | charge, discharge |
| reservoir | volume (alias: energy), extraction, |
| spill (alias: drain), | |
| eini, efin, soft_emin | |
| bus | theta (alias: angle) |
| waterway | flow |
| turbine | generation |
| junction | drain |
| flow | flow (alias: discharge) |
| flow_right | flow, fail |
| volume_right | extraction (aliases: flow, fout), saving |
| seepage | flow (alias: seepage) |
| reserve_provision | up (aliases: uprovision, up_provision), |
| dn (aliases: dprovision, dn_provision, down) | |
| reserve_zone | up (aliases: urequirement, up_requirement), |
| dn (aliases: drequirement, dn_requirement, | |
| down) |
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.
| Variable | Scale (physical = LP × scale) |
|---|---|
| reservoir.volume | energy_scale: auto-computed from capacity |
| (capacity/1000 rounded to next power of 10), | |
or set explicitly via energy_scale JSON field | |
| reservoir.extraction | flow_scale (default 1.0; overridable via |
variable_scales option) | |
| reservoir.spill | flow_scale (same) |
| reservoir.eini | energy_scale (same as reservoir.volume) |
| reservoir.efin | energy_scale (same as reservoir.volume) |
| reservoir.soft_emin | energy_scale (same as reservoir.volume) |
| battery.energy | energy_scale (default 1.0) |
| battery.charge | flow_scale (default 1.0) |
| battery.discharge | flow_scale (default 1.0) |
| battery.spill | flow_scale (default 1.0) |
| battery.eini | energy_scale (default 1.0) |
| battery.efin | energy_scale (default 1.0) |
| battery.soft_emin | energy_scale (default 1.0) |
| bus.theta | 1 / scale_theta (default 1/1000) |
| all others | 1.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 := [number '*'] element_ref
| [number '*'] sum_expr
| ['-'] number
element_ref := element_type '(' string ')' '.' attribute
sum_expr := 'sum' '(' element_type '(' string_list ')' '.'
attribute ')'
| 'sum' '(' element_type '(' 'all' [ ',' 'type' '=' string ]
')'
'.' attribute ')'
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'
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>* EOLNamespaces
- namespace gtopt
Classes
- 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::IndexRange
- Specifies a set of index values for a time dimension.
- struct gtopt::SumElementRef
- Aggregation over multiple elements of the same type.