gtopt/pampl_parser.hpp file

Parser for pseudo-AMPL (.pampl) user constraint files.

This module provides PamplParser::parse_file(), which reads a .pampl file and converts it to a vector of UserConstraint objects ready to be merged into a System::user_constraint_array.

PAMPL file format

A .pampl file contains a sequence of constraint definitions, each terminated by a semicolon. Blank lines and # / // comments are ignored everywhere.

Formal grammar (pseudo-BNF):

pampl_file      := (constraint_stmt | param_stmt | var_stmt)*

constraint_stmt := constraint_hdr? constraint_expr ';'

constraint_hdr  := ['inactive'] 'constraint' IDENT [STRING]
                   (penalty_clause | rhs_clause)* ':'

penalty_clause  := 'penalty' VALUE_EXPR

rhs_clause      := 'rhs' '[' VALUE_EXPR (',' VALUE_EXPR)* ']'
                   # per-block (scheduled) RHS → UserConstraint.rhs as the
                   # single-row TB matrix [[v0, ..., vK]]; the inline
                   # `<op> NUMBER` tail of constraint_expr is the per-block
                   # fallback.

var_stmt        := 'var' IDENT (',' IDENT)* ';'
                   # AMPL-style free-variable declarations.  Today these
                   # are bookkeeping only: every name is captured in
                   # PamplParseResult.declared_vars; by naming convention
                   # ``var slack_<NAME>;`` also populates
                   # ``UserConstraint::slack_name`` on the matching
                   # constraint, so a soft constraint's auto-created
                   # slack column gets a user-controlled LP-internal
                   # label (CPLEX logs, .lp dumps).  No new LP columns
                   # are introduced by the declaration itself.

constraint_expr := <same syntax as UserConstraint::expression>
                   e.g. generator('G1').generation + ... <= 300,
                        for(stage in {1,2})

STRING          := '"' ... '"'  |  '\'' ... '\''
IDENT           := [A-Za-z_][A-Za-z0-9_]*

Examples

# System peak capacity limit
constraint gen_pair_limit "Combined generation limit for G1 and G2":
  generator('G1').generation + generator('G2').generation <= 300,
  for(stage in {1,2,3}, block in 1..24);

# Inactive during tuning
inactive constraint flow_check:
  line('L1').flow <= 200;

# No header — uid is auto-assigned
sum(generator(all).generation) <= 1000;

# Per-block (scheduled) RHS: the inline `<= 0` is the fallback, the
# `rhs [...]` vector overrides it per block (one value per block).
constraint ralco_ramp_max "RALCO daily ramp" rhs [40, 40, 60, 60]:
  generator('RALCO').generation <= 0;

UID assignment

If the file is merged into a system that already has constraints with UIDs 1–N, pass start_uid = N + 1 so there are no collisions. When no header is present the name is auto-generated as "uc_<uid>".

Namespaces

namespace gtopt

Classes

class gtopt::PamplParser
Parser for pseudo-AMPL constraint files (.pampl).
struct gtopt::PamplParseResult
Result of parsing a PAMPL file or string.