UserConstraint struct
#include <gtopt/user_constraint.hpp>
Stores a user-defined linear constraint expression.
The expression field contains the full constraint in AMPL-inspired syntax. It is parsed at LP construction time into a ConstraintExpr AST (see constraint_parser.hpp) and then applied to the LinearProblem.
constraint_type and dual-value scaling
The optional constraint_type field controls how the dual (shadow price) of the constraint row is scaled when written to the output files. When absent, "power" is assumed. See ConstraintScaleType for the full table.
| Value | Dual output scaling |
|---|---|
"power" (default) | scale_obj / (prob × discount × Δt) |
"energy" | scale_obj / (prob × discount × Δt) |
"raw" / "unitless" | scale_obj / discount |
Soft constraints with visible slacks
When penalty is set to a positive value, the constraint becomes soft: the LP assembly automatically creates slack column(s) and folds them into the row so the constraint can be violated at the supplied per-unit penalty cost. All auto-created slacks are registered in the AMPL variable registry (so other constraints and reports can reference them) and emitted to output as UserConstraint/slack(_pos|_neg).{csv,parquet}, making any unserved water/energy demand traceable per constraint without primal-dual gap reconstruction.
LESS_EQUAL(expr <= rhs): one slack columnslack, row gets-1.0 * slack, cost+penalty * slack(weighted, below).GREATER_EQUAL(expr >= rhs): one slack columnslack, row gets+1.0 * slack, cost+penalty * slack.EQUAL(expr = rhs): two slack columnsslack_pos,slack_neg, row gets+1·slack_pos − 1·slack_neg, both with cost+penalty.RANGE: not yet supported withpenalty; an explicit pair of one-sided constraints should be used instead.
Penalty unit conversion (penalty_class)
penalty is $ per row unit per hour of violation and the slack cost is weighted like every other cost: penalty × probability × discount × duration[h] on a per-block row, penalty × probability × discount on a row spanning several blocks (daily_sum, or stage / phase / global scope: the row is already a span total). With penalty_class "hydro_flow" the penalty is interpreted as $/m³ (volume) and converted to $/(m³/s)·h via × 3600 (× 3600 × span hours on a span row), mirroring how hydro_fail_cost is applied to FlowRight fail columns. This lets an irrigation balance authored with penalty = hydro_fail_cost degrade gracefully without hard infeasibility while still being priced in the same unit system as the surrounding hydro network. See PenaltyClass for the full table.
Public static variables
- static LPClassName class_name
Public variables
- OptBool active
- Activation status (default: active).
- OptActive active_stages
- OptName constraint_type
- OptBool daily_sum
- OptName description
- Optional free-text description of the constraint.
- OptConstraintDirective directive
- Name expression
- Constraint expression in AMPL-inspired syntax.
- Name name
- Human-readable constraint name.
- OptReal penalty
- OptName penalty_class
- OptTBRealFieldSched rhs
- OptName scope
- OptName slack_name
- Uid uid
- Unique identifier.
Variable documentation
static LPClassName gtopt:: UserConstraint:: class_name
Canonical class-name constant used in LP row labels and config fields like VariableScale::class_name. Single source of truth — UserConstraintLP exposes no separate ClassName member; callers reach the constant via UserConstraint::class_name directly (or UserConstraintLP::Element::class_name in generic contexts).
OptActive gtopt:: UserConstraint:: active_stages
Per-stage activation schedule ([1, 0, 1, ...] or a file field): a stage with 0 emits no row for this constraint. Composes with the scalar active flag (both must allow the stage). Lets a merged, time-varying constraint exist only in some stages without a huge never-binding RHS (which degrades LP numerics).
OptName gtopt:: UserConstraint:: constraint_type
Scaling hint: "power" (default), "energy", "raw", or "unitless"
OptBool gtopt:: UserConstraint:: daily_sum
Per-day sum: emit ONE LP row per 24 h day instead of the default one-row-per-block expansion. The per-block terms are accumulated into a running row and flushed (lp.add_row) at each day-ending block — the block whose cumulative duration crosses a 24 h boundary, or the stage's last in-domain block. When set together with constraint_type == "energy" each block contributes its Δt-weighted value (coeff · Δt_b · col_b), so the LHS is a daily ENERGY sum Σ_day gen·Δt [MWh]; otherwise the daily sum is unweighted (a per-day COUNT, e.g. Σ_day startup ≤ N). Models PLEXOS RHS Day constraints (RALCO_max_e1/e2, CANUTILLARreserve, Diesel_OffTakeDay, *_Crew). Default unset ⇒ one row per block.
OptConstraintDirective gtopt:: UserConstraint:: directive
Typed constraint-family directive. Replaces the legacy name-regex classification (_RegRange_, Gas_MaxOpDay, MinProvision) and hardcoded soft-penalty ladder that lived inside the plexos2gtopt converter — see ConstraintDirective for the payload schema and the rationale.
Optional and defaulted to std::nullopt so every JSON written before the migration round-trips unchanged: when no directive is present UserConstraintLP behaves exactly as before, falling back to penalty and daily_sum for soft-tier and aggregation policy. Directive scaffolding lands in Step 1 of the AMPL/PAMPL modernization plan (2026-05-30); migration of the three plexos2gtopt detection sites (RegRange / Gas_MaxOpDay / MinProvision) follows in independent steps.
OptName gtopt:: UserConstraint:: penalty_class
Penalty unit hint: "raw" (default) or "hydro_flow". See PenaltyClass.
OptTBRealFieldSched gtopt:: UserConstraint:: rhs
Per-(stage, block) override of the constraint's RHS.
When set, the scalar RHS implied by the inline <op> NUMBER at the end of expression is replaced by rhs.optval(stage,
block) for every LP row instantiated from this constraint. Blocks where the schedule returns std::nullopt fall back to the expression's parsed scalar so legacy JSON keeps working.
Accepts the full OptTBRealFieldSched payload shape:
- scalar (
double): broadcast to every (stage, block). - T-schedule (one value per stage): broadcast across blocks.
- TB matrix (
[[stage_blocks]]): per-(stage, block). - string: name of a parquet field providing the per-block profile (resolved via
input_directory); also doubles as the AMPL parameter name exported foruser_constraint("X").rhsreferences in other constraints' expressions.
PLEXOS PATTERN / Constraint_RHS profiles map cleanly onto this field via plexos2gtopt's writer.
OptName gtopt:: UserConstraint:: scope
Time-granularity at which this constraint's LP rows are instantiated ("block" default, "stage", "phase", "global"). Parsed as ConstraintScope. ORTHOGONAL to the for(...) clause: scope reduces granularity (one row per stage / per cell), the for(...) clause restricts which instances are emitted.
block(default): one LP row per (scenario, stage, block) — the historical behaviour; per-block element references resolve directly.stage: one LP row per (scenario, stage). Per-block references inside the expression must be wrapped in asum{b in stage}time-aggregator so the coarse row reaches the per-block columns. Built in the per-(scenario, stage) operational pass.phase/global: one LP row per (scene, phase) cell, built in the planning pass (add_to_phase_lp/add_to_global_lp) — the FCF α terminal cut / annual-cap shape.
Default unset ⇒ block (every legacy JSON round-trips unchanged).
OptName gtopt:: UserConstraint:: slack_name
User-controlled name for the auto-created slack column on soft constraints (overrides the LP-internal default "slack").
Source. Populated by the PAMPL parser when a top-level var <ident> [, <ident>]*; declaration matches the constraint's name by naming convention (var slack_<NAME> binds to constraint NAME). JSON consumers can set this directly without the PAMPL bridge. Empty ⇒ LP uses the static UserConstraintLP::SlackName default.
Scope. Affects the LP-internal column label (SparseCol::variable_name) only — the parquet output schema stays aggregated as UserConstraint/slack_sol.parquet (keyed by constraint uid) so downstream tooling never has to discover the per-UC slack name. The benefit is purely audit / debug:
- CPLEX log lines reference
slack_HYDRO_FLOOR_Xinstead of a genericslack; .pamplfiles become self-documenting via the matchingvar slack_HYDRO_FLOOR_X;declaration.
Equality constraints (which produce two slack columns) suffix _pos / _neg on top of the user-supplied name.