1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
/* pounce.h — C API for the POUNCE nonlinear interior-point solver.
*
* Drop-in replacement for Ipopt 3.14's `IpStdCInterface.h`. Every
* function name, argument list, and return code matches upstream so a
* caller linking against libipopt can swap to libpounce_cinterface
* without source changes.
*
* Quick-start (C):
*
* #include "pounce.h"
*
* IpoptProblem nlp = CreateIpoptProblem(
* n, x_L, x_U, m, g_L, g_U,
* nele_jac, nele_hess, 0,
* eval_f, eval_g, eval_grad_f, eval_jac_g, eval_h);
* AddIpoptNumOption(nlp, "tol", 1e-8);
* AddIpoptIntOption(nlp, "max_iter", 500);
* enum ApplicationReturnStatus status = IpoptSolve(
* nlp, x, NULL, &obj, NULL, NULL, NULL, user_data);
* FreeIpoptProblem(nlp);
*
* Build & link example (macOS):
*
* cargo build --release -p pounce-cinterface
* cc app.c -I crates/pounce-cinterface/include \
* -L target/release -lpounce_cinterface \
* -Wl,-rpath,target/release -o app
*/
/* -----------------------------------------------------------------
* Version
* ----------------------------------------------------------------- */
extern "C" TRUE
/** Opaque handle to a pounce problem. */
;
typedef struct IpoptProblemInfo* IpoptProblem;
/** Pointer for arbitrary caller state passed to every callback. */
typedef void* UserDataPtr;
/* -----------------------------------------------------------------
* Callback signatures (identical to Ipopt C API).
* All callbacks return true on success, false on error.
* `new_x` / `new_lambda` indicate whether x / lambda changed since
* the last call.
*
* Jacobian / Hessian callbacks are dispatched in two modes:
* values == NULL → fill iRow/jCol with the sparsity pattern
* values != NULL → fill values in the same element order
* ----------------------------------------------------------------- */
typedef bool ;
typedef bool ;
typedef bool ;
typedef bool ;
typedef bool ;
typedef bool ;
/* -----------------------------------------------------------------
* Lifecycle
* ----------------------------------------------------------------- */
/** Allocate a new problem handle.
*
* n number of primal variables
* x_L/x_U variable lower/upper bounds (length n; ±1e19 for ±∞)
* m number of constraints
* g_L/g_U constraint lower/upper bounds (length m)
* nele_jac number of nonzeros in the Jacobian
* nele_hess number of nonzeros in the lower-triangular Hessian
* index_style 0 = C (0-based indices), 1 = Fortran (1-based)
* eval_* callback function pointers
*
* Returns NULL on invalid arguments (negative dims, missing required
* callbacks, NULL bound pointers when the corresponding dim > 0). */
IpoptProblem ;
/** Free a problem handle. After this call the pointer is invalid. */
void ;
/* -----------------------------------------------------------------
* Options
* Each Add*Option function returns true on success, false if the
* keyword is unknown or the value violates registered bounds.
* ----------------------------------------------------------------- */
bool ;
bool ;
bool ;
/** Open a file to receive solver output at `print_level`. Equivalent
* to setting the `output_file` and `file_print_level` options and
* attaching a journalist FileJournal. */
bool ;
/** Install user-provided NLP scaling. Pass NULL for `x_scaling` or
* `g_scaling` to leave that axis unscaled. Set option
* `nlp_scaling_method = user-scaling` for the scaling to take
* effect.
*
* `x_scaling` is applied as a change of variables, so the solution
* and bound multipliers IpoptSolve writes back are in your own units.
* A factor that is not finite and positive is refused by IpoptSolve,
* which returns Invalid_Option and prints why. */
bool ;
/* -----------------------------------------------------------------
* Intermediate callback
* ----------------------------------------------------------------- */
/** Install (or remove, with cb == NULL) a per-iteration callback.
* Returning false from the callback signals
* ApplicationReturnStatus::User_Requested_Stop.
*
* Fires from the outer loop with `alg_mod = 0` (RegularMode) and from
* the feasibility-restoration inner solver with `alg_mod = 1`
* (RestorationPhaseMode). Two things about the restoration fires:
*
* - The scalars beside `alg_mod` (`obj_value`, `inf_pr`, `inf_du`,
* `mu`, `d_norm`, `regularization_size`, `alpha_*`, `ls_trials`)
* describe the min-||c||_1 feasibility subproblem, not your NLP.
* `alg_mod` is what tells them apart; do not plot them on one axis
* without checking it.
* - The GetIpoptCurrent* inspectors report no data for the duration.
* The restoration iterate is not a point of your problem, and does
* not even have its dimensions.
*
* Returning false from a restoration fire ends the solve at the last
* iterate accepted for your NLP, not at the subproblem's iterate — so
* a caller aborting on a deadline gets back a point it can use. */
bool ;
/* -----------------------------------------------------------------
* Solve
*
* problem handle from CreateIpoptProblem()
* x [in/out] initial point (length n) → primal solution
* g [out] constraint values g(x*), or NULL to skip
* obj_val [out] objective f(x*), or NULL to skip
* mult_g [out] constraint multipliers λ (length m), or NULL
* mult_x_L [out] lower-bound multipliers z_L (length n), or NULL
* mult_x_U [out] upper-bound multipliers z_U (length n), or NULL
* user_data forwarded unmodified to every callback
*
* Returns an ApplicationReturnStatus (see IpoptReturnCodes.h).
* ----------------------------------------------------------------- */
enum ApplicationReturnStatus ;
/* -----------------------------------------------------------------
* Inspection (valid only during the intermediate callback)
*
* These mirror Ipopt 3.14's GetIpoptCurrent* functions. Pass NULL for
* any output buffer to skip retrieving it.
*
* Both are live: pounce's algorithm core installs the context these
* read from around every intermediate-callback invocation. Outside
* one — before the first iteration, after IpoptSolve returns, or from
* another thread — they return false and write nothing, which is the
* upstream contract.
* ----------------------------------------------------------------- */
bool ;
bool ;
/* -----------------------------------------------------------------
* Library info
* ----------------------------------------------------------------- */
/** Get the pounce version as `major.minor.release`. Any pointer may
* be NULL to skip that component. */
void ;
/* -----------------------------------------------------------------
* Pounce extensions — post-solve statistics
*
* Convenience accessors not present in upstream Ipopt's C API. All are
* valid only after IpoptSolve() has returned; they yield zero before
* the first solve.
* ----------------------------------------------------------------- */
/** Number of IPM iterations in the most recent solve. */
ipindex ;
/** Wall-clock solve time in seconds from the most recent solve. */
ipnumber ;
/** Final primal infeasibility from the most recent solve. */
ipnumber ;
/** Final dual infeasibility from the most recent solve. */
ipnumber ;
/** Final complementarity error from the most recent solve. */
ipnumber ;
/**
* Restoration-phase activity in the most recent solve. Any pointer may
* be NULL to skip that component.
*
* `calls` is how many times restoration was entered, `inner_iters` the
* total iterations its inner solver ran, `outer_iters` the outer
* iterations driving a restoration trial step, and `wall_secs` the
* cumulative seconds spent there — enough to answer "did this solve
* struggle, and how much of it was restoration?".
*
* This is solve-level. Individual iterations can be labelled too — the
* intermediate callback fires from the restoration inner solver with
* `alg_mod = 1` (`RestorationPhaseMode`); see `SetIntermediateCallback`
* for what those fires do and do not carry. These counters remain the
* only source for the inner iteration count and the wall time, and the
* only way to ask the question without installing a callback.
*/
void ;
/**
* Finite-difference Hessian census for the most recent solve. Any
* pointer may be NULL to skip that component.
*
* Only `hessian_approximation=finite-difference` populates these. Every
* other mode leaves `pattern_used` at -1 and the counts at 0, so -1 is
* how you tell "the mode did not run" from "it ran and the pattern was
* empty".
*
* `pattern_used` is 0 for the TNLP's declared Hessian structure and 1
* for the pattern derived from the Jacobian. It reports what the solve
* ENDED UP with, not what was requested: `fd_hessian_pattern=declared`
* falls back to the Jacobian derivation whenever the TNLP declares no
* Hessian structure, and that fallback is the difference between 17 and
* 341 probe groups on benchmarks/large_scale laptime -- which is the
* question this call exists to answer.
*
* `nnz` is the coloured pattern's lower-triangle nonzero count, `n` the
* columns the colouring ran over (so `groups / n` is the fraction of a
* dense scheme's probes this pattern costs), `groups`
* the probe groups per Hessian (each one extra gradient and Jacobian
* evaluation per rebuild), and `rho_max` the pattern's widest row.
* `coloring_fell_back` is 1 when a requested star colouring failed
* validation and Curtis-Powell-Reid was substituted.
*
* `objective_clique_widened` is 1 when the objective clique fell back to
* a conservative structural set because the model stated no objective
* linearity. It is the field that explains a surprising `groups`: the
* clique is then the nonlinear-variable set, or every variable, and the
* probe count reflects that rather than the objective's true support. A
* model implementing get_objective_variables_linearity pays none of it.
*/
void ;
/* -----------------------------------------------------------------
* Pounce extensions — linear-solver post-mortem
*
* What the KKT linear solver did during the most recent solve. The
* `linear_solver` option selects the backend (`feral`, pounce's own
* sparse LDL^T, is the default); `solver_name` reports the one that
* actually ran, which is the reliable way to confirm it.
*
* The struct is versioned with the library, not independently: a
* caller must compile against the pounce.h that ships with the
* libpounce_cinterface it loads. Fields pounce collects unconditionally
* are always set; the optional ones carry the sentinels named below
* when the backend did not report them.
* ----------------------------------------------------------------- */
typedef struct PounceLinearSolverStats;
/**
* Fill `stats` with the linear-solver post-mortem of the most recent
* solve. Returns false — leaving `stats` untouched — when the problem
* has not been solved yet or the backend reported no summary.
*
* Timings (symbolic analysis, numeric factorization, back-solve) are
* deliberately absent: pounce does not instrument them today, and this
* struct reports only what it already collects.
*/
Bool ;
/* -----------------------------------------------------------------
* Pounce extensions — option introspection
* ----------------------------------------------------------------- */
/** Value type of a registered option, as reported by
* GetPounceOptionType. */
;
/**
* Which AddIpopt*Option a keyword expects, so a caller forwarding
* options from a differently-typed source (a scripting language's
* dictionary, say) can pick the right setter instead of guessing from
* the value it happens to hold. Returns POUNCE_OPTION_UNKNOWN for a
* keyword this build does not register — which also answers "is this
* option available here?".
*
* `ipopt_problem` may be NULL: an option's type is a property of the
* build, not of a problem, and a caller deciding how to forward options
* need not have created one yet.
*/
int ;
/* -----------------------------------------------------------------
* Pounce extensions — active-set SQP working-set warm start
*
* Phase 5c (§7.2 of docs/research/active-set-sqp-warm-start.md).
* These functions are only meaningful when the `algorithm` option
* has been set to "active-set-sqp" via AddIpoptStrOption.
*
* Status enum values are stable across versions:
* 0 = Inactive, 1 = AtLower (active at lower bound),
* 2 = AtUpper, 3 = Fixed (variables) or Equality (constraints).
* ----------------------------------------------------------------- */
typedef int IpoptBoundStatus;
typedef int IpoptConsStatus;
/**
* Retrieve the QP working set produced by the most recent SQP solve.
* Pass NULL for either output buffer to skip that side; otherwise
* `bound_status_out` must hold at least `n` ints and
* `cons_status_out` at least `m` ints.
*
* Statuses are indexed by YOUR row and variable numbering. (The SQP
* works on a reordered constraint vector internally — equalities
* first — and both entry points used to expose that ordering, so a
* problem whose first row was an inequality and whose second was an
* equality reported the two statuses swapped.) A fixed variable
* (x_L == x_U) is absent from the internal problem and is reported as
* POUNCE_WS_FIXED_OR_EQ.
*
* Returns 1 (TRUE) on success, 0 (FALSE) when no working set is
* available — e.g. no SQP solve has been run, the IPM path was
* used, or the SQP solve converged at iter 0 (no QP solved).
*/
Bool ;
/**
* Supply a warm-start working set consumed by the next IpoptSolve.
* `bound_status_in` must hold `n` valid status codes (or NULL to
* cold-start bounds); `cons_status_in` must hold `m` valid status
* codes (or NULL to cold-start constraints). Returns 1 on success,
* 0 on a NULL problem handle, an out-of-range status code, or
* both inputs NULL.
*
* Status codes are validated against the problem, not merely against
* the enum's range. POUNCE_WS_FIXED_OR_EQ asserts x_L == x_U for a
* variable, or g_L == g_U for a row; POUNCE_WS_AT_LOWER / _AT_UPPER
* assert that the bound being sat on is finite. Those are claims about
* the model rather than guesses about the active set, so a false one
* returns 0 (FALSE) instead of being accepted and acted on. (Accepting
* them silently over-constrained the solve and returned a wrong optimum
* on a convex program, with this function having returned TRUE.)
*
* Buffers are indexed by YOUR row and variable numbering, matching what
* IpoptGetWorkingSet hands back, so the documented round-trip through
* the two is order-preserving.
*
* Only the working set is supplied here. The starting iterate stays
* the caller's: the next IpoptSolve warm-starts from the `x` buffer
* it is passed, exactly as it would without this call. Under
* `warm_start_init_point=yes` that solve also seeds the SQP's
* multipliers from its `mult_g` / `mult_x_L` / `mult_x_U` buffers
* (upstream Ipopt's in/out contract for those arguments); with the
* option off — the default — they stay strictly outputs and the
* multipliers start at zero.
*
* The staged working set is consumed by one solve. A following
* IpoptSolve without a fresh call here cold-starts its working set.
*/
Bool ;
/** Drop any pending warm-start working set without solving. */
Bool ;
/**
* Declare which variables enter the problem NONLINEARLY.
*
* The C-API face of Ipopt's TNLP::get_number_of_nonlinear_variables /
* get_list_of_nonlinear_variables pair (which upstream exposes only to
* C++ callers), for frontends that already know their model's
* structure — CasADi's `pass_nonlinear_variables`, an algebraic
* modeling language, a hand-written driver.
*
* The effect is confined to the LIMITED-MEMORY Hessian
* (`hessian_approximation=limited-memory`): curvature is approximated
* over the declared subset only, and the Hessian is exactly zero for
* every other variable — the win on a model whose variables mostly
* enter linearly. Exact-Hessian solves ignore the declaration, and so
* does any solve that never calls this: the default is "all variables
* are nonlinear", identical to previous behavior. The subset takes
* precedence over the `num_linear_variables` option, matching Ipopt.
*
* `pos_nonlin_vars` holds `num_nonlin_vars` variable indices in the
* problem's own index style (the `index_style` given to
* CreateIpoptProblem). The subset may be arbitrary and noncontiguous;
* order does not matter. Declaring all `n` variables is the same as
* not calling this at all.
*
* Returns 1 on success, 0 — leaving any previous declaration in place
* — on a NULL handle, a negative or oversized count, a NULL array with
* a positive count, or an out-of-range index.
*/
Bool ;
/** Drop a declared nonlinear-variable subset (back to "all nonlinear"). */
Bool ;
/**
* Convenience one-shot solve combining IpoptSetWarmStartWorkingSet,
* IpoptSolve, and IpoptGetWorkingSet. Any of the in/out buffers
* may be NULL to skip that side. Returns the
* ApplicationReturnStatus integer (same contract as IpoptSolve).
*
* NOTE: an invalid warm-start input (out-of-range status code,
* dimension mismatch) is silently discarded — the solve proceeds
* with cold-start instead. Callers who need to detect this fall-
* back must invoke IpoptSetWarmStartWorkingSet directly first
* and check its Bool return value before calling IpoptSolve.
*/
enum ApplicationReturnStatus ;
/* -----------------------------------------------------------------
* Pounce extensions — JSON solve-report writing
*
* The CLI's `--json-output` path writes a `pounce.solve-report/v1`
* file. These two functions expose the same payload to embedders
* (GAMS solver link, custom drivers) so that downstream tools — the
* studio MCP server's `diagnose`, `find_stalls`, `convergence_trace`,
* etc. — work against any cinterface-driven solve.
* ----------------------------------------------------------------- */
/**
* Enable per-iteration trajectory capture on the next IpoptSolve.
* Must be called BEFORE IpoptSolve for the per-iter trace to land in
* the report; off by default to avoid the (small) per-iter cost.
*
* Returns 1 (TRUE) on success, 0 (FALSE) when ipopt_problem is NULL.
*/
Bool ;
/**
* Write the most recent IpoptSolve result to `path` as a
* `pounce.solve-report/v1` JSON file. `detail` is `"summary"` or
* `"full"`; pass NULL for the default ("summary"). At
* `"full"`, the per-iteration trajectory is included when
* IpoptEnableIterHistory was called before the solve.
*
* The `kind` of the input descriptor is recorded as `"tnlp-direct"`
* because the C API receives callbacks rather than an .nl file or
* builtin name.
*
* Returns 1 (TRUE) on a successful write, 0 (FALSE) for: NULL handle,
* no prior solve, an invalid `detail`, a bad path, or an I/O error.
*/
Bool ;
/* ===========================================================
* Factor-once / solve-many session API
*
* The Solver session keeps the converged KKT factor alive
* between calls so several follow-up operations (parametric
* sensitivity sweep, reduced Hessian over different pin
* sets, raw KKT back-solve) reuse the same factorization.
*
* IpoptProblem prob = CreateIpoptProblem(...);
* // ... AddIpoptStrOption / SetIntermediateCallback as usual
* IpoptSolver sol = IpoptCreateSolver(&prob); // consumes prob
* IpoptSolverSolve(sol, x, g, &obj, ...); // run the IPM
* IpoptSolverParametricStep(sol, n_pins, pins, deltas, dx);
* IpoptSolverReducedHessian(sol, n_pins, pins, 1.0, hr);
* IpoptSolverKktSolve(sol, rhs, lhs);
* IpoptFreeSolver(sol);
*
* The classic `IpoptSolve` API is unchanged and unaffected.
* =========================================================== */
/** Opaque solver-session handle. */
typedef struct IpoptSolverInfo* IpoptSolver;
/**
* Construct a session from a prepared IpoptProblem. Consumes
* `*prob_handle` (the inner pointer is nulled out so the caller
* cannot accidentally double-free). Returns NULL on a NULL/empty
* input handle.
*/
IpoptSolver ;
/** Release a session handle. After this call the pointer is invalid. */
void ;
/**
* Run the IPM. Same output buffer contract as IpoptSolve: `x` is
* in/out (initial guess in, solution out); `g`, `obj_val`,
* `mult_g`, `mult_x_L`, `mult_x_U` are out-only and may be NULL.
* `user_data` is threaded into the C callbacks unchanged.
*/
enum ApplicationReturnStatus ;
/**
* Dimension of the augmented KKT system held by the session.
* Returns -1 on a NULL handle or before a successful Solve.
*/
Index ;
/**
* Apply the converged KKT factor to `rhs` (length = KKT dim).
* Writes the result into `lhs`. Returns 1 (TRUE) on success,
* 0 (FALSE) on NULL inputs or absent factor.
*
* The solve is in natural (unscaled) units: any NLP scaling the IPM
* applied internally (`nlp_scaling_method`) is undone, so RHS and
* result are in the user's own units (pounce#128). The same applies
* to IpoptSolverParametricStep and IpoptSolverReducedHessian. Use
* IpoptSolverKktSolveScaled for the raw solver-space back-solve
* (the pre-#128 behavior).
*/
Bool ;
/**
* IpoptSolverKktSolve without the natural-units correction: the
* back-solve runs in the solver's internal scaled space. Identical
* to IpoptSolverKktSolve when no NLP scaling is active.
*/
Bool ;
/**
* Parametric step: given perturbations `deltas` on the constraints
* named by `pin_indices` (length `n_pins`), write the predicted
* primal step `dx` into `dx_out` (length n).
*/
Bool ;
/**
* Reduced Hessian `H_R = obj_scal * B K^-1 B^T` over the pinned
* equality-constraint rows in `pin_indices` (0-based indices into
* g(x)). Writes a dense `n_pins x n_pins` matrix in column-major
* order to `hr_out`.
*
* `H_R` is in natural (unscaled) units: any NLP scaling the IPM
* applied (`nlp_scaling_method`) is undone before the value is
* reported, so `-inv(H_R)` is directly the parameter covariance of
* an estimation problem (pounce#128). `obj_scal` is a plain extra
* multiplier (pass 1.0).
*/
Bool ;
} /* extern "C" */
/* POUNCE_H */