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

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
Expr
UnaryOp

Functions§

collect_vars
Walk e and insert every Var(i) index into out.
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.