gtopt::PamplParser class

Parser for pseudo-AMPL constraint files (.pampl)

Reads a .pampl text file (or string) and produces a list of UserConstraint and UserParam objects. Call parse_file() to parse from a filesystem path, or parse() to parse from an in-memory string.

Parameter declarations

# Scalar parameter
param pct_elec = 35;

# Monthly-indexed parameter (12 values, jan..dec)
param irr_seasonal[month] = [0, 0, 0, 100, 100, 100, 100, 100, 100, 100,
                              0, 0];

Parameters can then be referenced by name in constraint expressions:

constraint elec_limit:
  generator('G1').generation <= pct_elec;

Public static functions

static auto parse(std::string_view source, Uid start_uid = Uid {1}) -> PamplParseResult
Parse a .pampl string into constraints and parameters.
static auto parse_file(std::string_view filepath, Uid start_uid = Uid {1}) -> PamplParseResult
Parse a .pampl file into constraints and parameters.

Function documentation

static PamplParseResult gtopt::PamplParser::parse(std::string_view source, Uid start_uid = Uid {1})

Parse a .pampl string into constraints and parameters.

Parameters
source The PAMPL source text.
start_uid First UID to assign (default 1).
Returns Parsed constraints and parameters.
Exceptions
std::invalid_argument on syntax errors.

static PamplParseResult gtopt::PamplParser::parse_file(std::string_view filepath, Uid start_uid = Uid {1})

Parse a .pampl file into constraints and parameters.

Parameters
filepath Path to the .pampl file.
start_uid First UID to assign (default 1); increment by 1 for each constraint so UIDs are unique.
Returns Parsed constraints and parameters.
Exceptions
std::runtime_error if the file cannot be opened.
std::invalid_argument on syntax errors.