Skip to main content

Module nl_reader

Module nl_reader 

Source
Expand description

Minimal AMPL .nl ASCII-format reader.

Implements the g-header text dialect for problems whose constraint and objective expressions are restricted to a polynomial-friendly subset of opcodes. This is not a full .nl reader — it is the smallest piece that lets pounce --nl-file foo.nl solve a real AMPL-emitted unconstrained problem.

Supported:

  • Text header (g…).
  • Constraint and objective expression segments using opcodes o0 (add), o1 (sub), o2 (mul), o3 (div), o5 (pow), o16 (unary minus), o39 (sqrt), o42 (log10), o43 (log), o44 (exp), o15 (abs), o41 (sin), o46 (cos), o38 (tan), o49 (atan), o53 (acos), plus n<num> constants and v<idx> variables.
  • Linear-Jacobian (J) and linear-objective (G) segments.
  • Variable bounds (b) and constraint bounds (r).
  • Optional initial primal (x) segment. Initial dual (d) is read and discarded.
  • Multiple objectives (we use only the first; per AMPL convention).

Not supported (will return an error explaining what’s missing):

  • Network / piecewise-linear constructs.
  • Complementarity rows.
  • Binary-format .nl files (b… header).

References:

  • https://ampl.com/REFS/hooking2.pdf — “Hooking Your Solver to AMPL” (David M. Gay), the canonical .nl spec.
  • ref/Ipopt/test/mytoy.nl — annotated example used for the unit tests in this module.

Structs§

ImportedFunc
An AMPL imported (external) function declaration from a top-level F<id> <type> <nargs> <name> segment.
NlProblem
Parsed .nl problem in the form needed by NlTnlp.
NlSuffixes
Suffix data parsed out of S-segments. Sparse entries are scattered into dense vectors at problem load time so callers can index by variable / constraint number directly. Empty maps when the .nl file declared no suffixes.
NlTnlp

Enums§

BinOp
CmpOp
Relational operator carried by Expr::Compare. The variants map 1:1 onto AMPL opcodes o22 LT, o23 LE, o24 EQ, o28 GE, o29 GT, o30 NE.
Expr
FuncallArg
One positional argument to an AMPL imported function call. AMPL splits arguments into reals (carried by ra[]) and strings (carried by sa[]); FuncallArg mirrors that split. Real args are arbitrary expressions.
UnaryOp

Functions§

collect_vars
Walk e and insert every Var(i) index into out.
constraint_jacobian_sparsity
Structural sparsity of the constraint Jacobian as flat 0-based triplets (irow, jcol): one pair per variable that constraint k structurally depends on — the union of its linear support and the Var(i) indices appearing anywhere in its nonlinear tree (collect_vars). Sorted and deduplicated within each row.
eval_expr
Forward-mode value evaluation.
grad_expr
Reverse-mode gradient: accumulates seed * d(expr)/dx_i into grad.
load_nl_as_tnlp
Convenience: read an .nl file and build a TNLP-compatible Rc.
parse_nl_text
Parse .nl text content. Public so tests can use string literals.
read_nl_file
Parse an .nl file from disk.
render_all_constraint_equations
Render every constraint to text, index-aligned to g (original .nl row order). Used to build the debugger’s static equation book.
render_constraint_equation
Render constraint k as a full relation, e.g. mass_in - mass_out = 0 or 0 <= T_reactor <= 500. Bounds outside ±1e19 are treated as infinite (AMPL’s convention), matching [TNLPAdapter]’s classifier.