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)
convertercharge, 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)

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 (default 1/1000)
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         := [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>* EOL

Namespaces

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.