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), plusn<num>constants andv<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
.nlfiles (b…header).
References:
- https://ampl.com/REFS/hooking2.pdf — “Hooking Your Solver to
AMPL” (David M. Gay), the canonical
.nlspec. ref/Ipopt/test/mytoy.nl— annotated example used for the unit tests in this module.
Structs§
- Imported
Func - An AMPL imported (external) function declaration from a top-level
F<id> <type> <nargs> <name>segment. - NlProblem
- Parsed
.nlproblem in the form needed byNlTnlp. - 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.nlfile declared no suffixes. - NlTnlp
Enums§
- BinOp
- CmpOp
- Relational operator carried by
Expr::Compare. The variants map 1:1 onto AMPL opcodeso22 LT,o23 LE,o24 EQ,o28 GE,o29 GT,o30 NE. - Expr
- Funcall
Arg - One positional argument to an AMPL imported function call. AMPL splits
arguments into reals (carried by
ra[]) and strings (carried bysa[]);FuncallArgmirrors that split. Real args are arbitrary expressions. - UnaryOp
Functions§
- collect_
vars - Walk
eand insert everyVar(i)index intoout. - constraint_
jacobian_ sparsity - Structural sparsity of the constraint Jacobian as flat 0-based
triplets
(irow, jcol): one pair per variable that constraintkstructurally depends on — the union of its linear support and theVar(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_iintograd. - load_
nl_ as_ tnlp - Convenience: read an
.nlfile and build a TNLP-compatible Rc. - parse_
nl_ text - Parse
.nltext content. Public so tests can use string literals. - read_
nl_ file - Parse an
.nlfile from disk. - render_
all_ constraint_ equations - Render every constraint to text, index-aligned to
g(original.nlrow order). Used to build the debugger’s static equation book. - render_
constraint_ equation - Render constraint
kas a full relation, e.g.mass_in - mass_out = 0or0 <= T_reactor <= 500. Bounds outside ±1e19 are treated as infinite (AMPL’s convention), matching [TNLPAdapter]’s classifier.