simplex-chat-0004: introduceToRemaining notElem O(N×M) member dedup; fix: Set.notMember O(log N)

This commit is contained in:
russell@unturf.com 2026-03-29 22:03:20 -04:00
parent eb534f0944
commit d9ca5b236f
12 changed files with 1006 additions and 0 deletions

View file

@ -0,0 +1,107 @@
# UNDF: (pending)
# scipy-0001: SHGO minimizers() — xl_maps list scan ignores xl_maps_set O(V×L)
## CWE-407 — Algorithmic Complexity: O(V×L) list scan despite O(1) set already present
| Field | Value |
|-------|-------|
| ID | scipy-0001 |
| Severity | MEDIUM |
| Ecosystem | scipy |
| Package | `scipy.optimize` |
| File | `scipy/optimize/_shgo.py` |
| Lines | 11551174 |
| Complexity | O(V×L) — vertices × local minima, inside per-iteration minimizers() call |
| Hot path | `SHGO.minimizers()` — called every iteration of the SHGO optimizer |
## Background
SHGO (Simplicial Homology Global Optimization) is scipy's global optimizer for
non-convex problems. Each iteration calls `minimizers()` to find all current
local minima of the simplicial complex. It iterates all vertices in `HC.V.cache`
(V vertices) and for each checks whether it has already been mapped as a local
minimum via the `LMC` (Local Minima Cache).
## Defect
```python
# scipy/optimize/_shgo.py lines 11551174
def minimizers(self):
self.minimizer_pool = []
for x in self.HC.V.cache: # V vertices
in_LMC = False
if len(self.LMC.xl_maps) > 0:
for xlmi in self.LMC.xl_maps: # DEFECT: O(L) list scan
if np.all(np.array(x) == np.array(xlmi)):
in_LMC = True
if in_LMC:
continue
if self.HC.V[x].minimiser():
if self.HC.V[x] not in self.minimizer_pool: # DEFECT: O(M) list scan
self.minimizer_pool.append(self.HC.V[x])
```
`LMapCache` already maintains `xl_maps_set` — a set of tuples for O(1) lookup:
```python
# scipy/optimize/_shgo.py lines 15601595
class LMapCache:
def __init__(self):
self.xl_maps = []
self.xl_maps_set = set() # ← already exists, not used here
...
def add_res(self, v, lres, bounds=None):
...
self.xl_maps.append(lres.x)
self.xl_maps_set.add(tuple(lres.x)) # maintained on every insert
```
The set is populated on every `add_res()` call but never consulted in
`minimizers()` — the code uses the slower list instead.
### Total cost per optimization run
`minimizers()` is called once per SHGO iteration (line 885). For a problem with
N function evaluations, the simplicial complex has V ~ O(N) vertices, and
L ~ O(N) local minima in the worst case. The loop is O(V×L) = O(N²) per
iteration × I iterations = O(I×N²).
For a typical optimization with N=500 sample points and 10 iterations:
- List path: 500 × 250 × 10 = 1,250,000 element comparisons (each involving
`np.all(np.array(x) == np.array(xlmi))` — not just a Python int comparison)
- Set path: 500 × 10 = 5,000 set probes
## Complexity table
| Vertices (V) | Local minima (L) | Iterations (I) | list np.all ops | set ops | Speedup |
|-------------|-----------------|---------------|----------------|---------|---------|
| 100 | 20 | 5 | 10,000 | 500 | 20× |
| 500 | 100 | 10 | 500,000 | 5,000 | 100× |
| 1,000 | 300 | 20 | 6,000,000 | 20,000 | 300× |
| 2,000 | 500 | 30 | 30,000,000 | 60,000 | 500× |
## Fix
Use `xl_maps_set` for the LMC membership check, and drop the redundant
`minimizer_pool` dedup (safe because the cache iteration visits each key once):
```python
def minimizers(self):
self.minimizer_pool = []
for x in self.HC.V.cache:
# FIX: O(1) set lookup instead of O(L) list scan
if tuple(x) in self.LMC.xl_maps_set:
continue
if self.HC.V[x].minimiser():
# No need for not-in check: each x is a unique cache key
self.minimizer_pool.append(self.HC.V[x])
```
`tuple(x)` is already the native key type (cache keys are tuples of coordinates),
matching the `tuple(lres.x)` stored by `add_res()`. `set.__contains__` is O(1)
amortised.
Note: `xl_maps_set` is invalidated by `sort_cache_result()` which converts
`xl_maps` to numpy array; ensure `xl_maps_set` is rebuilt or frozen at that point.

View file

@ -0,0 +1,127 @@
package unit;
import java.util.*;
/**
* ScipyTest CWE-407 benchmark for scipy-0001
*
* scipy-0001: SHGO.minimizers() xl_maps list scan ignores xl_maps_set O(V×L)
* Real code (scipy/optimize/_shgo.py:1155-1174):
* for x in self.HC.V.cache: # V vertices
* for xlmi in self.LMC.xl_maps: # O(L) list scan
* if np.all(np.array(x) == np.array(xlmi)):
* in_LMC = True
*
* xl_maps_set = set() is maintained by LMapCache.add_res() but never used here.
*
* Fix: replace list scan with xl_maps_set.contains(tuple(x)) O(1).
*/
public class ScipyTest {
/**
* Simulates SHGO.minimizers() with xl_maps list scan.
* @param V vertices in HC.V.cache
* @param L local minima already in LMC (xl_maps length)
* @param I optimizer iterations
* @return total comparisons
*/
static long slowShgoMinimizers(int V, int L, int I) {
// Build xl_maps as list of "tuples" (represented as Strings here)
List<String> xlMaps = new ArrayList<>();
for (int i = 0; i < L; i++) xlMaps.add("min_" + i);
long ops = 0;
for (int iter = 0; iter < I; iter++) {
List<Object> minimizerPool = new ArrayList<>();
for (int v = 0; v < V; v++) {
String x = "vertex_" + v;
boolean inLMC = false;
// DEFECT: O(L) scan
for (int k = 0; k < xlMaps.size(); k++) {
ops++;
if (xlMaps.get(k).startsWith("min_" + (v % L))) {
inLMC = true;
break;
}
}
if (inLMC) continue;
// if minimiser(): check not in pool (also a list)
boolean isMin = (v % 7 == 0); // ~14% of vertices are local minima
if (isMin) {
boolean inPool = false;
for (int k = 0; k < minimizerPool.size(); k++) {
ops++;
if (minimizerPool.get(k).equals(x)) { inPool = true; break; }
}
if (!inPool) minimizerPool.add(x);
}
}
}
return ops;
}
/**
* Simulates fixed SHGO.minimizers() using xl_maps_set.
* @return total ops
*/
static long fastShgoMinimizers(int V, int L, int I) {
Set<String> xlMapsSet = new HashSet<>();
for (int i = 0; i < L; i++) xlMapsSet.add("min_" + i);
long ops = 0;
for (int iter = 0; iter < I; iter++) {
List<Object> minimizerPool = new ArrayList<>();
for (int v = 0; v < V; v++) {
String x = "vertex_" + v;
ops++; // O(1) set probe
if (xlMapsSet.contains("min_" + (v % L))) continue;
boolean isMin = (v % 7 == 0);
if (isMin) minimizerPool.add(x); // no dedup needed (unique keys)
}
}
return ops;
}
static void bench(String label, Runnable slow, Runnable fast, long sOps, long fOps) {
slow.run(); fast.run();
long t0 = System.nanoTime(); slow.run(); long sMs = (System.nanoTime() - t0) / 1_000_000;
long t1 = System.nanoTime(); fast.run(); long fMs = (System.nanoTime() - t1) / 1_000_000;
double speedup = fMs > 0 ? (double) sMs / fMs : (double) sOps / fOps;
System.out.printf(" %-60s slow:%4dms (%,d ops) fast:%4dms (%,d ops) speedup:%.0fx%n",
label, sMs, sOps, fMs, fOps, speedup);
}
public static void main(String[] args) {
System.out.println("ScipyTest — scipy-0001: SHGO.minimizers() xl_maps list scan vs xl_maps_set");
System.out.println();
System.out.println(" [scipy-0001: SHGO minimizers() LMC lookup]");
int[][] cases = {{500, 100, 10}, {1000, 300, 20}, {2000, 500, 30}};
for (int[] c : cases) {
int V = c[0], L = c[1], I = c[2];
long sOps = slowShgoMinimizers(V, L, I);
long fOps = fastShgoMinimizers(V, L, I);
bench(
String.format("V=%d vertices, L=%d local-minima, I=%d iters", V, L, I),
() -> slowShgoMinimizers(V, L, I),
() -> fastShgoMinimizers(V, L, I),
sOps, fOps
);
}
System.out.println();
int pass = 0;
long s = slowShgoMinimizers(1000, 300, 20);
long f = fastShgoMinimizers(1000, 300, 20);
assert s > f * 50 : "scipy-0001 expected >50x ratio; slow=" + s + " fast=" + f;
pass++;
System.out.printf("%d/1 PASS%n", pass);
System.out.printf("scipy-0001: _shgo.SHGO.minimizers() xl_maps list → xl_maps_set O(V×L) → O(V)%n");
System.out.printf("Hotpath: every SHGO optimizer iteration; O(N²) per run on large sample sets%n");
}
}