discopt 0.2.4__tar.gz → 0.2.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. {discopt-0.2.4 → discopt-0.2.5}/Cargo.lock +2 -2
  2. {discopt-0.2.4 → discopt-0.2.5}/Cargo.toml +1 -1
  3. {discopt-0.2.4 → discopt-0.2.5}/PKG-INFO +2 -1
  4. {discopt-0.2.4 → discopt-0.2.5}/README.md +1 -0
  5. {discopt-0.2.4 → discopt-0.2.5}/crates/discopt-core/src/bnb/branching.rs +8 -2
  6. {discopt-0.2.4 → discopt-0.2.5}/crates/discopt-core/src/bnb/tree_manager.rs +5 -1
  7. {discopt-0.2.4 → discopt-0.2.5}/crates/discopt-python/src/ripopt_bindings.rs +133 -55
  8. {discopt-0.2.4 → discopt-0.2.5}/pyproject.toml +2 -1
  9. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/__init__.py +1 -1
  10. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/ipm.py +12 -4
  11. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/lp_ipm.py +54 -25
  12. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/nl_evaluator.py +93 -0
  13. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/nlp_evaluator.py +130 -0
  14. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/problem_classifier.py +2 -2
  15. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/qp_ipm.py +15 -19
  16. discopt-0.2.5/python/discopt/_jax/scaling.py +98 -0
  17. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/cli.py +69 -0
  18. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/interfaces/cutest.py +57 -0
  19. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/modeling/core.py +24 -5
  20. discopt-0.2.5/python/discopt/ro/ROADMAP.md +102 -0
  21. discopt-0.2.5/python/discopt/ro/__init__.py +83 -0
  22. discopt-0.2.5/python/discopt/ro/affine_policy.py +435 -0
  23. discopt-0.2.5/python/discopt/ro/counterpart.py +188 -0
  24. discopt-0.2.5/python/discopt/ro/formulations/__init__.py +11 -0
  25. discopt-0.2.5/python/discopt/ro/formulations/_common.py +273 -0
  26. discopt-0.2.5/python/discopt/ro/formulations/box.py +388 -0
  27. discopt-0.2.5/python/discopt/ro/formulations/ellipsoidal.py +240 -0
  28. discopt-0.2.5/python/discopt/ro/formulations/polyhedral.py +278 -0
  29. discopt-0.2.5/python/discopt/ro/uncertainty.py +314 -0
  30. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/solver.py +674 -11
  31. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/solvers/nlp_ipopt.py +11 -0
  32. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/solvers/qp_highs.py +8 -8
  33. {discopt-0.2.4 → discopt-0.2.5}/LICENSE +0 -0
  34. {discopt-0.2.4 → discopt-0.2.5}/crates/discopt-core/Cargo.toml +0 -0
  35. {discopt-0.2.4 → discopt-0.2.5}/crates/discopt-core/src/bnb/mod.rs +0 -0
  36. {discopt-0.2.4 → discopt-0.2.5}/crates/discopt-core/src/bnb/node.rs +0 -0
  37. {discopt-0.2.4 → discopt-0.2.5}/crates/discopt-core/src/bnb/pool.rs +0 -0
  38. {discopt-0.2.4 → discopt-0.2.5}/crates/discopt-core/src/expr.rs +0 -0
  39. {discopt-0.2.4 → discopt-0.2.5}/crates/discopt-core/src/lib.rs +0 -0
  40. {discopt-0.2.4 → discopt-0.2.5}/crates/discopt-core/src/nl_parser.rs +0 -0
  41. {discopt-0.2.4 → discopt-0.2.5}/crates/discopt-core/src/presolve/fbbt.rs +0 -0
  42. {discopt-0.2.4 → discopt-0.2.5}/crates/discopt-core/src/presolve/mod.rs +0 -0
  43. {discopt-0.2.4 → discopt-0.2.5}/crates/discopt-core/src/presolve/obbt.rs +0 -0
  44. {discopt-0.2.4 → discopt-0.2.5}/crates/discopt-core/src/presolve/probing.rs +0 -0
  45. {discopt-0.2.4 → discopt-0.2.5}/crates/discopt-core/src/presolve/simplify.rs +0 -0
  46. {discopt-0.2.4 → discopt-0.2.5}/crates/discopt-python/Cargo.toml +0 -0
  47. {discopt-0.2.4 → discopt-0.2.5}/crates/discopt-python/src/batch.rs +0 -0
  48. {discopt-0.2.4 → discopt-0.2.5}/crates/discopt-python/src/bnb_bindings.rs +0 -0
  49. {discopt-0.2.4 → discopt-0.2.5}/crates/discopt-python/src/expr_bindings.rs +0 -0
  50. {discopt-0.2.4 → discopt-0.2.5}/crates/discopt-python/src/lib.rs +0 -0
  51. {discopt-0.2.4 → discopt-0.2.5}/crates/discopt-python/src/nl_bindings.rs +0 -0
  52. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/PHASE3_MATH_NOTES.md +0 -0
  53. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/__init__.py +0 -0
  54. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/alphabb.py +0 -0
  55. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/batch_evaluator.py +0 -0
  56. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/convexity.py +0 -0
  57. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/cutting_planes.py +0 -0
  58. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/dag_compiler.py +0 -0
  59. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/differentiable.py +0 -0
  60. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/differentiable_lp.py +0 -0
  61. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/differentiable_qp.py +0 -0
  62. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/differentiable_solve.py +0 -0
  63. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/envelopes.py +0 -0
  64. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/gdp_reformulate.py +0 -0
  65. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/gnn_branching.py +0 -0
  66. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/gnn_policy.py +0 -0
  67. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/icnn.py +0 -0
  68. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/icnn_trainer.py +0 -0
  69. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/ipm_callbacks.py +0 -0
  70. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/ipm_iterative.py +0 -0
  71. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/learned_relaxations.py +0 -0
  72. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/mccormick.py +0 -0
  73. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/mccormick_nlp.py +0 -0
  74. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/nl_reconstruction.py +0 -0
  75. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/obbt.py +0 -0
  76. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/pcg.py +0 -0
  77. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/piecewise_mccormick.py +0 -0
  78. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/primal_heuristics.py +0 -0
  79. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/problem_graph.py +0 -0
  80. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/relaxation_compiler.py +0 -0
  81. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/sparse_ipm.py +0 -0
  82. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/sparse_jacobian.py +0 -0
  83. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/sparse_kkt.py +0 -0
  84. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/sparsity.py +0 -0
  85. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/_jax/strong_branching.py +0 -0
  86. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/benchmarks/__init__.py +0 -0
  87. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/benchmarks/metrics.py +0 -0
  88. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/benchmarks/runner.py +0 -0
  89. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/callbacks.py +0 -0
  90. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/constants.py +0 -0
  91. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/dae/__init__.py +0 -0
  92. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/dae/collocation.py +0 -0
  93. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/dae/finite_difference.py +0 -0
  94. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/dae/mol.py +0 -0
  95. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/dae/polynomials.py +0 -0
  96. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/export/__init__.py +0 -0
  97. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/export/_extract.py +0 -0
  98. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/export/lp.py +0 -0
  99. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/export/mps.py +0 -0
  100. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/interfaces/__init__.py +0 -0
  101. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/llm/__init__.py +0 -0
  102. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/llm/advisor.py +0 -0
  103. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/llm/chat.py +0 -0
  104. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/llm/commentary.py +0 -0
  105. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/llm/diagnosis.py +0 -0
  106. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/llm/prompts.py +0 -0
  107. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/llm/provider.py +0 -0
  108. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/llm/reformulation.py +0 -0
  109. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/llm/safety.py +0 -0
  110. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/llm/serializer.py +0 -0
  111. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/llm/tools.py +0 -0
  112. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/modeling/__init__.py +0 -0
  113. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/modeling/examples.py +0 -0
  114. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/nn/__init__.py +0 -0
  115. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/nn/bounds.py +0 -0
  116. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/nn/formulations/__init__.py +0 -0
  117. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/nn/formulations/base.py +0 -0
  118. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/nn/formulations/full_space.py +0 -0
  119. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/nn/formulations/reduced_space.py +0 -0
  120. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/nn/formulations/relu_bigm.py +0 -0
  121. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/nn/formulations/tree_ensemble.py +0 -0
  122. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/nn/network.py +0 -0
  123. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/nn/predictor.py +0 -0
  124. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/nn/readers/__init__.py +0 -0
  125. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/nn/readers/onnx_reader.py +0 -0
  126. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/nn/readers/sklearn_reader.py +0 -0
  127. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/nn/readers/torch_reader.py +0 -0
  128. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/nn/scaling.py +0 -0
  129. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/nn/tree.py +0 -0
  130. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/solvers/__init__.py +0 -0
  131. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/solvers/gdpopt_loa.py +0 -0
  132. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/solvers/lp_highs.py +0 -0
  133. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/solvers/milp_highs.py +0 -0
  134. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/solvers/nlp_ripopt.py +0 -0
  135. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/solvers/oa.py +0 -0
  136. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/solvers/sipopt.py +0 -0
  137. {discopt-0.2.4 → discopt-0.2.5}/python/discopt/warm_start.py +0 -0
@@ -1067,9 +1067,9 @@ checksum = "a96887878f22d7bad8a3b6dc5b7440e0ada9a245242924394987b21cf2210a4c"
1067
1067
 
1068
1068
  [[package]]
1069
1069
  name = "ripopt"
1070
- version = "0.6.0"
1070
+ version = "0.6.1"
1071
1071
  source = "registry+https://github.com/rust-lang/crates.io-index"
1072
- checksum = "fb2217dfe4ec562aaeefb002b9a9427e8bd47ff9f94f37494f51e13723ddcf61"
1072
+ checksum = "09db916a3dd0c64c14c2952b377296157b537f80da3753758e4a46a59f255b87"
1073
1073
  dependencies = [
1074
1074
  "env_logger",
1075
1075
  "faer",
@@ -11,4 +11,4 @@ repository = "https://github.com/discopt-org/discopt"
11
11
  [workspace.dependencies]
12
12
  pyo3 = { version = "0.23", features = ["extension-module"] }
13
13
  numpy = "0.23"
14
- ripopt = "0.6.0"
14
+ ripopt = "0.6.1"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: discopt
3
- Version: 0.2.4
3
+ Version: 0.2.5
4
4
  Classifier: Development Status :: 3 - Alpha
5
5
  Classifier: Intended Audience :: Science/Research
6
6
  Classifier: License :: OSI Approved :: Eclipse Public License 2.0 (EPL-2.0)
@@ -61,6 +61,7 @@ Project-URL: Repository, https://github.com/jkitchin/discopt
61
61
 
62
62
  # discopt
63
63
 
64
+ [![PyPI](https://img.shields.io/pypi/v/discopt)](https://pypi.org/project/discopt/)
64
65
  [![CI](https://github.com/jkitchin/discopt/actions/workflows/ci.yml/badge.svg)](https://github.com/jkitchin/discopt/actions/workflows/ci.yml)
65
66
  [![codecov](https://codecov.io/gh/jkitchin/discopt/graph/badge.svg?token=B3Y6LAtox9)](https://codecov.io/gh/jkitchin/discopt)
66
67
 
@@ -1,5 +1,6 @@
1
1
  # discopt
2
2
 
3
+ [![PyPI](https://img.shields.io/pypi/v/discopt)](https://pypi.org/project/discopt/)
3
4
  [![CI](https://github.com/jkitchin/discopt/actions/workflows/ci.yml/badge.svg)](https://github.com/jkitchin/discopt/actions/workflows/ci.yml)
4
5
  [![codecov](https://codecov.io/gh/jkitchin/discopt/graph/badge.svg?token=B3Y6LAtox9)](https://codecov.io/gh/jkitchin/discopt)
5
6
 
@@ -287,6 +287,12 @@ pub fn create_children(
287
287
  // Warm-start: pass parent's stored solution to children.
288
288
  let parent_sol = parent.parent_solution.clone();
289
289
 
290
+ // Inherit parent's lower bound: the child's feasible region is a subset
291
+ // of the parent's, so the parent's lower bound is valid for the child.
292
+ // This prevents the global lower bound from dropping to -inf when
293
+ // unsolved children enter the pool.
294
+ let inherited_lb = parent.local_lower_bound;
295
+
290
296
  // Left child: x_i <= floor(val)
291
297
  let mut left_ub = parent.ub.clone();
292
298
  left_ub[idx] = bp; // bp is already floor(val)
@@ -296,7 +302,7 @@ pub fn create_children(
296
302
  depth: parent.depth + 1,
297
303
  lb: parent.lb.clone(),
298
304
  ub: left_ub,
299
- local_lower_bound: f64::NEG_INFINITY,
305
+ local_lower_bound: inherited_lb,
300
306
  status: NodeStatus::Pending,
301
307
  parent_solution: parent_sol.clone(),
302
308
  };
@@ -310,7 +316,7 @@ pub fn create_children(
310
316
  depth: parent.depth + 1,
311
317
  lb: right_lb,
312
318
  ub: parent.ub.clone(),
313
- local_lower_bound: f64::NEG_INFINITY,
319
+ local_lower_bound: inherited_lb,
314
320
  status: NodeStatus::Pending,
315
321
  parent_solution: parent_sol,
316
322
  };
@@ -395,7 +395,11 @@ impl TreeManager {
395
395
  return f64::INFINITY;
396
396
  }
397
397
  let denom = self.incumbent_value.abs().max(1.0);
398
- (self.incumbent_value - self.global_lower_bound) / denom
398
+ let raw = (self.incumbent_value - self.global_lower_bound) / denom;
399
+ // Clamp to zero: a negative gap means the bound exceeds the
400
+ // incumbent due to numerical noise or suboptimal NLP solves.
401
+ // Don't report it as closed; keep exploring.
402
+ raw.max(0.0)
399
403
  }
400
404
 
401
405
  /// Get aggregate tree statistics.
@@ -11,6 +11,9 @@ use pyo3::types::PyDict;
11
11
  ///
12
12
  /// Each callback acquires the GIL and calls back into the Python evaluator.
13
13
  /// Bounds, structure, and initial point are cached as Rust-owned data.
14
+ /// When the evaluator provides sparse COO structure (via `has_sparse_structure`),
15
+ /// uses `evaluate_jacobian_values` / `evaluate_hessian_values` for efficient
16
+ /// value extraction. Otherwise falls back to dense evaluation.
14
17
  struct PyNlpProblem {
15
18
  evaluator: PyObject,
16
19
  n: usize,
@@ -20,12 +23,15 @@ struct PyNlpProblem {
20
23
  g_l: Vec<f64>,
21
24
  g_u: Vec<f64>,
22
25
  x0: Vec<f64>,
23
- /// Cached Jacobian sparsity: (rows, cols) — dense pattern
26
+ /// Cached Jacobian sparsity: COO (rows, cols)
24
27
  jac_rows: Vec<usize>,
25
28
  jac_cols: Vec<usize>,
26
- /// Cached Hessian sparsity: (rows, cols) — lower triangle dense pattern
29
+ /// Cached Hessian sparsity: COO (rows, cols), lower triangle
27
30
  hess_rows: Vec<usize>,
28
31
  hess_cols: Vec<usize>,
32
+ /// Whether to use sparse value callbacks
33
+ use_sparse_jac: bool,
34
+ use_sparse_hess: bool,
29
35
  }
30
36
 
31
37
  impl ripopt::NlpProblem for PyNlpProblem {
@@ -107,19 +113,33 @@ impl ripopt::NlpProblem for PyNlpProblem {
107
113
  }
108
114
  Python::with_gil(|py| {
109
115
  let x_py = PyArray1::from_slice(py, x);
110
- let result = self
111
- .evaluator
112
- .call_method1(py, "evaluate_jacobian", (x_py,))
113
- .expect("evaluate_jacobian failed");
114
- let arr: PyReadonlyArray1<f64> = result
115
- .call_method0(py, "flatten")
116
- .expect("flatten failed")
117
- .bind(py)
118
- .extract()
119
- .expect("jacobian not ndarray");
120
- let slice = arr.as_slice().expect("jacobian not contiguous");
121
- // Dense Jacobian flattened row-major: values in same order as structure
122
- values.copy_from_slice(slice);
116
+ if self.use_sparse_jac {
117
+ // Sparse path: evaluator returns 1-D values in COO order
118
+ let result = self
119
+ .evaluator
120
+ .call_method1(py, "evaluate_jacobian_values", (x_py,))
121
+ .expect("evaluate_jacobian_values failed");
122
+ let arr: PyReadonlyArray1<f64> = result
123
+ .bind(py)
124
+ .extract()
125
+ .expect("jacobian_values not ndarray");
126
+ let slice = arr.as_slice().expect("jacobian_values not contiguous");
127
+ values.copy_from_slice(slice);
128
+ } else {
129
+ // Dense path: flatten (m, n) Jacobian row-major
130
+ let result = self
131
+ .evaluator
132
+ .call_method1(py, "evaluate_jacobian", (x_py,))
133
+ .expect("evaluate_jacobian failed");
134
+ let arr: PyReadonlyArray1<f64> = result
135
+ .call_method0(py, "flatten")
136
+ .expect("flatten failed")
137
+ .bind(py)
138
+ .extract()
139
+ .expect("jacobian not ndarray");
140
+ let slice = arr.as_slice().expect("jacobian not contiguous");
141
+ values.copy_from_slice(slice);
142
+ }
123
143
  })
124
144
  }
125
145
 
@@ -132,30 +152,47 @@ impl ripopt::NlpProblem for PyNlpProblem {
132
152
  let x_py = PyArray1::from_slice(py, x);
133
153
  let lambda_py = PyArray1::from_slice(py, lambda);
134
154
 
135
- let result = self
136
- .evaluator
137
- .call_method1(
138
- py,
139
- "evaluate_lagrangian_hessian",
140
- (x_py, obj_factor, lambda_py),
141
- )
142
- .expect("evaluate_lagrangian_hessian failed");
155
+ if self.use_sparse_hess {
156
+ // Sparse path: evaluator returns 1-D values in COO order
157
+ let result = self
158
+ .evaluator
159
+ .call_method1(
160
+ py,
161
+ "evaluate_hessian_values",
162
+ (x_py, obj_factor, lambda_py),
163
+ )
164
+ .expect("evaluate_hessian_values failed");
165
+ let arr: PyReadonlyArray1<f64> = result
166
+ .bind(py)
167
+ .extract()
168
+ .expect("hessian_values not ndarray");
169
+ let slice = arr.as_slice().expect("hessian_values not contiguous");
170
+ values.copy_from_slice(slice);
171
+ } else {
172
+ // Dense path: extract lower triangle from (n, n) array
173
+ let result = self
174
+ .evaluator
175
+ .call_method1(
176
+ py,
177
+ "evaluate_lagrangian_hessian",
178
+ (x_py, obj_factor, lambda_py),
179
+ )
180
+ .expect("evaluate_lagrangian_hessian failed");
143
181
 
144
- // Result is (n, n) dense array. Extract lower triangle values
145
- // matching hessian_structure order (row-major lower triangle).
146
- let flat: PyReadonlyArray1<f64> = result
147
- .call_method0(py, "flatten")
148
- .expect("flatten failed")
149
- .bind(py)
150
- .extract()
151
- .expect("hessian not ndarray");
152
- let data = flat.as_slice().expect("hessian not contiguous");
153
- let n = self.n;
154
- let mut idx = 0;
155
- for i in 0..n {
156
- for j in 0..=i {
157
- values[idx] = data[i * n + j];
158
- idx += 1;
182
+ let flat: PyReadonlyArray1<f64> = result
183
+ .call_method0(py, "flatten")
184
+ .expect("flatten failed")
185
+ .bind(py)
186
+ .extract()
187
+ .expect("hessian not ndarray");
188
+ let data = flat.as_slice().expect("hessian not contiguous");
189
+ let n = self.n;
190
+ let mut idx = 0;
191
+ for i in 0..n {
192
+ for j in 0..=i {
193
+ values[idx] = data[i * n + j];
194
+ idx += 1;
195
+ }
159
196
  }
160
197
  }
161
198
  })
@@ -209,26 +246,65 @@ pub fn solve_ripopt(
209
246
  let n = x0_vec.len();
210
247
  let m = g_l_vec.len();
211
248
 
212
- // Build dense Jacobian structure (m x n, row-major)
213
- let mut jac_rows = Vec::with_capacity(m * n);
214
- let mut jac_cols = Vec::with_capacity(m * n);
215
- for i in 0..m {
216
- for j in 0..n {
217
- jac_rows.push(i);
218
- jac_cols.push(j);
249
+ // Check if evaluator provides sparse COO structure
250
+ let use_sparse = evaluator
251
+ .call_method0(py, "has_sparse_structure")
252
+ .ok()
253
+ .and_then(|r| r.extract::<bool>(py).ok())
254
+ .unwrap_or(false);
255
+
256
+ let (jac_rows, jac_cols, use_sparse_jac) = if use_sparse && m > 0 {
257
+ let structure = evaluator
258
+ .call_method0(py, "jacobian_structure")
259
+ .expect("jacobian_structure failed");
260
+ let tuple = structure.bind(py).downcast::<pyo3::types::PyTuple>()
261
+ .expect("jacobian_structure must return tuple");
262
+ let rows: Vec<usize> = tuple
263
+ .get_item(0).expect("missing rows")
264
+ .extract().expect("rows not array of int");
265
+ let cols: Vec<usize> = tuple
266
+ .get_item(1).expect("missing cols")
267
+ .extract().expect("cols not array of int");
268
+ (rows, cols, true)
269
+ } else {
270
+ // Dense fallback: all m*n entries, row-major
271
+ let mut rows = Vec::with_capacity(m * n);
272
+ let mut cols = Vec::with_capacity(m * n);
273
+ for i in 0..m {
274
+ for j in 0..n {
275
+ rows.push(i);
276
+ cols.push(j);
277
+ }
219
278
  }
220
- }
279
+ (rows, cols, false)
280
+ };
221
281
 
222
- // Build dense lower-triangle Hessian structure
223
- let hess_nnz = n * (n + 1) / 2;
224
- let mut hess_rows = Vec::with_capacity(hess_nnz);
225
- let mut hess_cols = Vec::with_capacity(hess_nnz);
226
- for i in 0..n {
227
- for j in 0..=i {
228
- hess_rows.push(i);
229
- hess_cols.push(j);
282
+ let (hess_rows, hess_cols, use_sparse_hess) = if use_sparse {
283
+ let structure = evaluator
284
+ .call_method0(py, "hessian_structure")
285
+ .expect("hessian_structure failed");
286
+ let tuple = structure.bind(py).downcast::<pyo3::types::PyTuple>()
287
+ .expect("hessian_structure must return tuple");
288
+ let rows: Vec<usize> = tuple
289
+ .get_item(0).expect("missing rows")
290
+ .extract().expect("rows not array of int");
291
+ let cols: Vec<usize> = tuple
292
+ .get_item(1).expect("missing cols")
293
+ .extract().expect("cols not array of int");
294
+ (rows, cols, true)
295
+ } else {
296
+ // Dense fallback: full lower triangle
297
+ let hess_nnz = n * (n + 1) / 2;
298
+ let mut rows = Vec::with_capacity(hess_nnz);
299
+ let mut cols = Vec::with_capacity(hess_nnz);
300
+ for i in 0..n {
301
+ for j in 0..=i {
302
+ rows.push(i);
303
+ cols.push(j);
304
+ }
230
305
  }
231
- }
306
+ (rows, cols, false)
307
+ };
232
308
 
233
309
  let problem = PyNlpProblem {
234
310
  evaluator,
@@ -243,6 +319,8 @@ pub fn solve_ripopt(
243
319
  jac_cols,
244
320
  hess_rows,
245
321
  hess_cols,
322
+ use_sparse_jac,
323
+ use_sparse_hess,
246
324
  };
247
325
 
248
326
  // Build SolverOptions from the Python dict
@@ -4,7 +4,7 @@ build-backend = "maturin"
4
4
 
5
5
  [project]
6
6
  name = "discopt"
7
- version = "0.2.4"
7
+ version = "0.2.5"
8
8
  description = "Hybrid MINLP solver combining Rust and JAX"
9
9
  requires-python = ">=3.10"
10
10
  authors = [{name = "John Kitchin"}]
@@ -69,6 +69,7 @@ include = [
69
69
  [tool.ruff]
70
70
  line-length = 100
71
71
  target-version = "py310"
72
+ extend-exclude = ["adversary/"]
72
73
 
73
74
  [tool.ruff.lint]
74
75
  select = ["E", "F", "I", "W"]
@@ -25,7 +25,7 @@ solvers
25
25
  NLP solver backends: ripopt (Rust IPM), pure-JAX IPM (vmap batch), cyipopt (Ipopt).
26
26
  """
27
27
 
28
- __version__ = "0.2.4"
28
+ __version__ = "0.2.5"
29
29
 
30
30
  from discopt.callbacks import (
31
31
  CallbackContext as CallbackContext,
@@ -1040,6 +1040,11 @@ def solve_nlp_ipm(
1040
1040
  if fields:
1041
1041
  ipm_opts = ipm_opts._replace(**fields)
1042
1042
 
1043
+ # Enforce max_wall_time by capping max_iter. The JIT-compiled
1044
+ # jax.lax.while_loop cannot check wall clock, so we limit iterations
1045
+ # upfront and verify after the solve (issue #5).
1046
+ max_wall_time = options.get("max_wall_time") if options else None
1047
+
1043
1048
  obj_fn = evaluator._obj_fn
1044
1049
  con_fn = evaluator._cons_fn if m > 0 else None
1045
1050
  x0_jax = jnp.array(x0, dtype=jnp.float64)
@@ -1049,12 +1054,15 @@ def solve_nlp_ipm(
1049
1054
  wall_time = time.perf_counter() - t0
1050
1055
 
1051
1056
  conv = int(state.converged)
1052
- if conv in (1, 2):
1057
+ # Check wall-time limit (issue #5). The JIT loop cannot enforce this
1058
+ # mid-iteration, so we check post-hoc and downgrade the status.
1059
+ exceeded_time = max_wall_time is not None and wall_time > max_wall_time
1060
+ if conv in (1, 2) and not exceeded_time:
1053
1061
  status = SolveStatus.OPTIMAL
1054
- elif conv == 3:
1062
+ elif conv == 3 or exceeded_time:
1055
1063
  # If the solution is primal-feasible despite hitting the iteration
1056
- # limit, report as optimal. The IPM may stall near the solution due
1057
- # to degenerate curvature but still produce a usable result.
1064
+ # or time limit, report as optimal. The IPM may stall near the
1065
+ # solution due to degenerate curvature but still produce a usable result.
1058
1066
  feasible = True
1059
1067
  if m > 0 and g_l is not None and g_u is not None and con_fn is not None:
1060
1068
  g_final = con_fn(state.x)
@@ -114,6 +114,11 @@ def _make_problem_data(c, A, b, x_l, x_u):
114
114
  m = b.shape[0]
115
115
  has_lb = (x_l > -_INF).astype(jnp.float64)
116
116
  has_ub = (x_u < _INF).astype(jnp.float64)
117
+ # Clamp infinite bounds to safe finite values so that arithmetic
118
+ # inside jax.jit (which evaluates both branches of jnp.where)
119
+ # never produces inf - x = inf, avoiding inf * 0 = NaN downstream.
120
+ x_l = jnp.where(has_lb > 0.5, x_l, -_INF)
121
+ x_u = jnp.where(has_ub > 0.5, x_u, _INF)
117
122
  return LPProblemData(
118
123
  c=c,
119
124
  A=A,
@@ -235,8 +240,8 @@ def _initialize_state(pd, opts):
235
240
  opts.bound_push,
236
241
  )
237
242
 
238
- s_l = jnp.maximum(x - pd.x_l, _SLACK_FLOOR) * pd.has_lb
239
- s_u = jnp.maximum(pd.x_u - x, _SLACK_FLOOR) * pd.has_ub
243
+ s_l = jnp.where(pd.has_lb > 0.5, jnp.maximum(x - pd.x_l, _SLACK_FLOOR), 0.0)
244
+ s_u = jnp.where(pd.has_ub > 0.5, jnp.maximum(pd.x_u - x, _SLACK_FLOOR), 0.0)
240
245
  z_l = jnp.where(
241
246
  pd.has_lb > 0.5,
242
247
  mu / jnp.maximum(s_l, _SLACK_FLOOR),
@@ -329,10 +334,14 @@ def _iteration_body(carry: LPCarry, tol: float, max_iter: int, tau_min: float) -
329
334
  z_l, z_u = state.z_l, state.z_u
330
335
  tau = jnp.maximum(1.0 - mu, tau_min)
331
336
 
332
- s_l = jnp.maximum(x - pd.x_l, _SLACK_FLOOR) * pd.has_lb
333
- s_u = jnp.maximum(pd.x_u - x, _SLACK_FLOOR) * pd.has_ub
337
+ s_l = jnp.where(pd.has_lb > 0.5, jnp.maximum(x - pd.x_l, _SLACK_FLOOR), 0.0)
338
+ s_u = jnp.where(pd.has_ub > 0.5, jnp.maximum(pd.x_u - x, _SLACK_FLOOR), 0.0)
334
339
 
335
- Sig = pd.has_lb * z_l / jnp.maximum(s_l, _EPS) + pd.has_ub * z_u / jnp.maximum(s_u, _EPS) + _EPS
340
+ Sig = (
341
+ jnp.where(pd.has_lb > 0.5, z_l / jnp.maximum(s_l, _EPS), 0.0)
342
+ + jnp.where(pd.has_ub > 0.5, z_u / jnp.maximum(s_u, _EPS), 0.0)
343
+ + _EPS
344
+ )
336
345
  Sig_inv = 1.0 / jnp.maximum(Sig, _EPS)
337
346
 
338
347
  r_dual = c - z_l + z_u - A.T @ y
@@ -479,24 +488,24 @@ def _iteration_body(carry: LPCarry, tol: float, max_iter: int, tau_min: float) -
479
488
  z_u_new = jnp.maximum(z_u + alpha_d * dz_u, _EPS) * pd.has_ub
480
489
  y_new = y + alpha_d * dy
481
490
 
482
- # --- Stationarity-based z recovery at active bounds ---
483
- s_l_new = jnp.maximum(x_new - pd.x_l, _SLACK_FLOOR) * pd.has_lb
484
- s_u_new = jnp.maximum(pd.x_u - x_new, _SLACK_FLOOR) * pd.has_ub
485
- grad_stat = c - A.T @ y_new
486
- z_u_stat = jnp.maximum(-(grad_stat - z_l_new), _EPS)
487
- z_l_stat = jnp.maximum(grad_stat + z_u_new, _EPS)
488
- at_ub = pd.has_ub * (s_u_new <= _SLACK_FLOOR * 2.0).astype(jnp.float64)
489
- at_lb = pd.has_lb * (s_l_new <= _SLACK_FLOOR * 2.0).astype(jnp.float64)
490
- both = at_lb * at_ub
491
- at_lb = at_lb * (1.0 - both)
492
- at_ub = at_ub * (1.0 - both)
493
- z_u_new = jnp.where(at_ub > 0.5, z_u_stat, z_u_new)
494
- z_l_new = jnp.where(at_lb > 0.5, z_l_stat, z_l_new)
495
-
496
- # --- Update barrier parameter ---
491
+ # Recompute slacks from updated x (without stationarity-based z recovery,
492
+ # which breaks the z*s = mu complementarity invariant).
493
+ s_l_new = jnp.where(pd.has_lb > 0.5, jnp.maximum(x_new - pd.x_l, _SLACK_FLOOR), 0.0)
494
+ s_u_new = jnp.where(pd.has_ub > 0.5, jnp.maximum(pd.x_u - x_new, _SLACK_FLOOR), 0.0)
495
+
496
+ # --- Update barrier parameter (with error gate) ---
497
497
  compl = jnp.sum(pd.has_lb * z_l_new * s_l_new) + jnp.sum(pd.has_ub * z_u_new * s_u_new)
498
- mu_new = compl / jnp.maximum(n_pairs, 1.0)
499
- mu_new = jnp.minimum(mu_new, mu)
498
+ mu_candidate = compl / jnp.maximum(n_pairs, 1.0)
499
+ # Only decrease mu when current residuals are small relative to mu
500
+ # (prevents premature decrease that ill-conditions the KKT system).
501
+ r_dual_chk = c - z_l_new + z_u_new - A.T @ y_new
502
+ r_prim_chk = A @ x_new - b
503
+ barrier_err = jnp.maximum(
504
+ jnp.max(jnp.abs(r_prim_chk)) / (1.0 + jnp.max(jnp.abs(b))),
505
+ jnp.max(jnp.abs(r_dual_chk)) / (1.0 + jnp.max(jnp.abs(c))),
506
+ )
507
+ may_decrease = barrier_err < 10.0 * mu
508
+ mu_new = jnp.where(may_decrease, jnp.minimum(mu_candidate, mu), mu)
500
509
  mu_new = jnp.maximum(mu_new, _EPS)
501
510
 
502
511
  # --- Check convergence ---
@@ -603,14 +612,34 @@ def lp_ipm_solve(
603
612
  x_l = jnp.asarray(x_l, dtype=jnp.float64)
604
613
  x_u = jnp.asarray(x_u, dtype=jnp.float64)
605
614
 
615
+ # --- Problem scaling for improved conditioning ---
616
+ from discopt._jax.scaling import compute_lp_scaling, scale_lp, unscale_lp_solution
617
+
618
+ factors = compute_lp_scaling(c, A)
619
+ c, A, b = scale_lp(c, A, b, factors)
620
+
606
621
  # m=0 dispatch — trivial bound-clamping, no need to JIT
607
622
  if b.shape[0] == 0:
608
623
  pd = _make_problem_data(c, A, b, x_l, x_u)
609
- return _solve_unconstrained(pd, opts) # type: ignore[no-any-return]
610
-
611
- return _lp_ipm_solve_jit( # type: ignore[no-any-return]
624
+ state = _solve_unconstrained(pd, opts)
625
+ if factors.applied:
626
+ x_us, y_us, zl_us, zu_us = unscale_lp_solution(
627
+ state.x, state.y, state.z_l, state.z_u, factors
628
+ )
629
+ state = state._replace(x=x_us, y=y_us, z_l=zl_us, z_u=zu_us)
630
+ return state # type: ignore[no-any-return]
631
+
632
+ state = _lp_ipm_solve_jit(
612
633
  c, A, b, x_l, x_u, opts.tol, opts.max_iter, opts.tau_min, opts.bound_push
613
634
  )
635
+ if factors.applied:
636
+ x_us, y_us, zl_us, zu_us = unscale_lp_solution(
637
+ state.x, state.y, state.z_l, state.z_u, factors
638
+ )
639
+ state = state._replace(x=x_us, y=y_us, z_l=zl_us, z_u=zu_us)
640
+ # Recompute objective on original scale
641
+ state = state._replace(obj=jnp.dot(jnp.asarray(c / factors.obj_scale), state.x))
642
+ return state # type: ignore[no-any-return]
614
643
 
615
644
 
616
645
  def lp_ipm_solve_batch(
@@ -160,6 +160,99 @@ class NLPEvaluatorFromNl:
160
160
  jac[:, j] = (cp - cm) / (2.0 * eps)
161
161
  return jac
162
162
 
163
+ def has_sparse_structure(self) -> bool:
164
+ """True if numeric probing detected useful sparsity."""
165
+ self._ensure_coo_cache()
166
+ return self._use_sparse
167
+
168
+ def jacobian_structure(self) -> tuple[np.ndarray, np.ndarray]:
169
+ """Return Jacobian sparsity as COO (rows, cols) arrays."""
170
+ self._ensure_coo_cache()
171
+ return (self._jac_rows, self._jac_cols)
172
+
173
+ def hessian_structure(self) -> tuple[np.ndarray, np.ndarray]:
174
+ """Return lower-triangle Hessian sparsity as COO (rows, cols) arrays."""
175
+ self._ensure_coo_cache()
176
+ return (self._hess_rows, self._hess_cols)
177
+
178
+ def evaluate_jacobian_values(self, x: np.ndarray) -> np.ndarray:
179
+ """Return Jacobian values as 1-D array matching jacobian_structure order."""
180
+ self._ensure_coo_cache()
181
+ jac = self.evaluate_jacobian(x)
182
+ return jac[self._jac_rows, self._jac_cols].astype(np.float64)
183
+
184
+ def evaluate_hessian_values(
185
+ self, x: np.ndarray, obj_factor: float, lambda_: np.ndarray
186
+ ) -> np.ndarray:
187
+ """Return Lagrangian Hessian values as 1-D array matching hessian_structure."""
188
+ self._ensure_coo_cache()
189
+ h = self.evaluate_lagrangian_hessian(x, obj_factor, lambda_)
190
+ return h[self._hess_rows, self._hess_cols].astype(np.float64)
191
+
192
+ def _ensure_coo_cache(self) -> None:
193
+ """Lazily detect sparsity via numeric probing and cache COO indices."""
194
+ if hasattr(self, "_jac_rows"):
195
+ return
196
+
197
+ n = self._n_variables
198
+ m = self._n_constraints
199
+ lb, ub = self.variable_bounds
200
+
201
+ # Clamp bounds for sampling
202
+ lb_safe = np.where(np.isfinite(lb), lb, -10.0)
203
+ ub_safe = np.where(np.isfinite(ub), ub, 10.0)
204
+
205
+ rng = np.random.RandomState(42)
206
+ n_probes = 3
207
+ threshold = 1e-10
208
+
209
+ jac_nonzero = np.zeros((m, n), dtype=bool)
210
+ hess_nonzero = np.zeros((n, n), dtype=bool)
211
+
212
+ for _ in range(n_probes):
213
+ x_probe = lb_safe + rng.rand(n) * (ub_safe - lb_safe)
214
+ if m > 0:
215
+ jac = self.evaluate_jacobian(x_probe)
216
+ jac_nonzero |= np.abs(jac) > threshold
217
+ lam = rng.randn(m) if m > 0 else np.array([], dtype=np.float64)
218
+ hess = self.evaluate_lagrangian_hessian(x_probe, 1.0, lam)
219
+ hess_nonzero |= np.abs(hess) > threshold
220
+
221
+ # Make Hessian pattern symmetric
222
+ hess_nonzero = hess_nonzero | hess_nonzero.T
223
+
224
+ # Decide whether sparsity is worth exploiting
225
+ jac_nnz = int(jac_nonzero.sum())
226
+ hess_nnz = int(hess_nonzero.sum())
227
+ jac_total = m * n if m > 0 else 1
228
+ hess_total = n * n if n > 0 else 1
229
+ jac_density = jac_nnz / jac_total
230
+ hess_density = hess_nnz / hess_total
231
+ self._use_sparse = n >= 50 and (jac_density < 0.15 or hess_density < 0.15)
232
+
233
+ if self._use_sparse:
234
+ if m > 0:
235
+ jac_r, jac_c = np.where(jac_nonzero)
236
+ self._jac_rows = jac_r.astype(np.intp)
237
+ self._jac_cols = jac_c.astype(np.intp)
238
+ else:
239
+ self._jac_rows = np.array([], dtype=np.intp)
240
+ self._jac_cols = np.array([], dtype=np.intp)
241
+ hess_r, hess_c = np.where(hess_nonzero)
242
+ lower_mask = hess_r >= hess_c
243
+ self._hess_rows = hess_r[lower_mask].astype(np.intp)
244
+ self._hess_cols = hess_c[lower_mask].astype(np.intp)
245
+ else:
246
+ # Dense fallback
247
+ if m > 0:
248
+ rows, cols = np.meshgrid(np.arange(m), np.arange(n), indexing="ij")
249
+ self._jac_rows = rows.flatten().astype(np.intp)
250
+ self._jac_cols = cols.flatten().astype(np.intp)
251
+ else:
252
+ self._jac_rows = np.array([], dtype=np.intp)
253
+ self._jac_cols = np.array([], dtype=np.intp)
254
+ self._hess_rows, self._hess_cols = np.tril_indices(n)
255
+
163
256
  @property
164
257
  def n_variables(self) -> int:
165
258
  """Total number of variables (flat)."""