kizzasi-logic 0.2.1

TensorLogic bridge for Kizzasi - constraint enforcement and safety guardrails
Documentation
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
//! Advanced Parallelization for constraint evaluation
//!
//! Provides CPU-parallel (rayon) batch projection, parallel constraint graph traversal
//! with work-stealing, SIMD-optimized batch constraint checking, and multi-threaded
//! incremental solving.
//!
//! # Key Components
//! - [`ConstraintGraph`] — topological layer parallel traversal with Kahn's BFS
//! - [`ParallelBatchProjector`] — batch point projection via rayon work-stealing
//! - [`check_range_constraints_simd`] — auto-vectorization friendly range checking
//! - [`check_constraints_parallel`] — parallel group constraint checking
//! - [`ParallelIncrementalSolver`] — multi-threaded incremental projected gradient solving

use rayon::prelude::*;
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};

// ============================================================================
// Type aliases to satisfy clippy::type_complexity
// ============================================================================

/// Shared constraint violation function: given variable values, returns violation (0 = satisfied).
pub type ConstraintFn = Arc<dyn Fn(&[f64]) -> f64 + Send + Sync>;

/// Shared gradient function: given variable values, returns the gradient vector.
pub type GradientFn = Arc<dyn Fn(&[f64]) -> Vec<f64> + Send + Sync>;

// ============================================================================
// ConstraintNode
// ============================================================================

/// A node in a constraint dependency graph.
///
/// Each node encapsulates a single constraint function along with its dependency
/// edges (which nodes must be evaluated before this one) and which variable
/// indices it reads.
#[derive(Clone)]
pub struct ConstraintNode {
    /// Unique identifier for this node (used as index into the graph's node list)
    pub id: usize,
    /// The constraint function: returns violation amount (0.0 = satisfied)
    pub constraint_fn: ConstraintFn,
    /// IDs of nodes this node depends on (must be evaluated first)
    pub dependencies: Vec<usize>,
    /// Variable indices consumed by this constraint
    pub variables: Vec<usize>,
}

impl std::fmt::Debug for ConstraintNode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ConstraintNode")
            .field("id", &self.id)
            .field("dependencies", &self.dependencies)
            .field("variables", &self.variables)
            .finish()
    }
}

// ============================================================================
// ConstraintGraph
// ============================================================================

/// Constraint dependency graph for parallel traversal.
///
/// Uses Kahn's BFS-based topological sort to group nodes into layers. Nodes
/// within the same layer have no mutual dependencies and can be evaluated
/// concurrently using rayon's work-stealing thread pool.
pub struct ConstraintGraph {
    nodes: Vec<ConstraintNode>,
    /// Topological layers: nodes in the same layer can be evaluated in parallel
    layers: Vec<Vec<usize>>,
}

impl ConstraintGraph {
    /// Create an empty constraint graph.
    pub fn new() -> Self {
        Self {
            nodes: Vec::new(),
            layers: Vec::new(),
        }
    }

    /// Add a node to the graph. Invalidates any previously computed layers.
    pub fn add_node(&mut self, node: ConstraintNode) {
        self.nodes.push(node);
        self.layers.clear();
    }

    /// Compute topological layers using Kahn's BFS algorithm.
    ///
    /// After this call, `self.layers` is populated such that all nodes in
    /// layer `k` depend only on nodes in layers `0..k`.
    pub fn compute_layers(&mut self) {
        let n = self.nodes.len();
        if n == 0 {
            self.layers = Vec::new();
            return;
        }

        // Build in-degree count and adjacency list (dependency edges reversed)
        let mut in_degree = vec![0usize; n];
        // For each node, track which node positions appear as dependencies
        // We use node.id as the index (assuming ids are 0..n and inserted in order)
        // Build a map: node_id -> position in self.nodes
        let mut id_to_pos = std::collections::HashMap::new();
        for (pos, node) in self.nodes.iter().enumerate() {
            id_to_pos.insert(node.id, pos);
        }

        // adjacency[pos] = list of positions that depend on nodes[pos]
        let mut adjacency: Vec<Vec<usize>> = vec![Vec::new(); n];
        for (pos, node) in self.nodes.iter().enumerate() {
            for &dep_id in &node.dependencies {
                if let Some(&dep_pos) = id_to_pos.get(&dep_id) {
                    in_degree[pos] += 1;
                    adjacency[dep_pos].push(pos);
                }
                // Ignore dependency ids not present in graph
            }
        }

        // Kahn's BFS: start with all zero in-degree nodes
        let mut queue: VecDeque<usize> = (0..n).filter(|&i| in_degree[i] == 0).collect();
        let mut layers: Vec<Vec<usize>> = Vec::new();

        while !queue.is_empty() {
            let layer_size = queue.len();
            let mut layer = Vec::with_capacity(layer_size);
            for _ in 0..layer_size {
                let pos = match queue.pop_front() {
                    Some(p) => p,
                    None => break,
                };
                layer.push(pos);
                for &next_pos in &adjacency[pos] {
                    in_degree[next_pos] -= 1;
                    if in_degree[next_pos] == 0 {
                        queue.push_back(next_pos);
                    }
                }
            }
            layers.push(layer);
        }

        self.layers = layers;
    }

    /// Evaluate all constraints in parallel, layer by layer.
    ///
    /// Returns a vector of violation values indexed by node position in the graph.
    /// Nodes in the same topological layer are evaluated concurrently.
    ///
    /// # Panics
    /// Panics if `compute_layers` has not been called since the last `add_node`.
    pub fn evaluate_parallel(&self, values: &[f64]) -> Vec<f64> {
        if self.nodes.is_empty() {
            return Vec::new();
        }

        let mut violations = vec![0.0f64; self.nodes.len()];

        for layer in &self.layers {
            let layer_results: Vec<(usize, f64)> = layer
                .par_iter()
                .map(|&idx| (idx, (self.nodes[idx].constraint_fn)(values)))
                .collect();
            for (idx, v) in layer_results {
                violations[idx] = v;
            }
        }

        violations
    }

    /// Check if all constraints are satisfied (parallel evaluation).
    ///
    /// Returns `true` if every node's violation is at most `tolerance`.
    pub fn all_satisfied_parallel(&self, values: &[f64], tolerance: f64) -> bool {
        let violations = self.evaluate_parallel(values);
        violations.iter().all(|&v| v <= tolerance)
    }
}

impl Default for ConstraintGraph {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// ParallelBatchProjector
// ============================================================================

/// Batch projection using parallel rayon workers.
///
/// Projects multiple points onto a constraint set concurrently via rayon's
/// work-stealing thread pool (simulating GPU-style batch parallelism in
/// 100% Pure Rust).
pub struct ParallelBatchProjector {
    /// Number of threads hint (0 = rayon global pool default)
    pub num_threads: usize,
    /// Convergence tolerance for projection
    pub tolerance: f64,
    /// Maximum gradient descent iterations per point
    pub max_iterations: usize,
}

impl ParallelBatchProjector {
    /// Create a new projector. `num_threads` is advisory (rayon manages the pool).
    pub fn new(num_threads: usize) -> Self {
        Self {
            num_threads,
            tolerance: 1e-6,
            max_iterations: 100,
        }
    }

    /// Project multiple points onto the constraint set in parallel.
    ///
    /// Each point is projected independently using projected gradient descent.
    /// `constraint` returns `true` if the point is feasible.
    /// `gradient` returns the constraint violation gradient at a point.
    pub fn project_batch(
        &self,
        points: &[Vec<f64>],
        constraint: &(impl Fn(&[f64]) -> bool + Sync),
        gradient: &(impl Fn(&[f64]) -> Vec<f64> + Sync),
    ) -> Vec<Vec<f64>> {
        let step_size = 0.01f64;
        let max_iter = self.max_iterations;

        points
            .par_iter()
            .map(|point| {
                let mut p = point.clone();
                for _ in 0..max_iter {
                    if constraint(&p) {
                        break;
                    }
                    let grad = gradient(&p);
                    p.iter_mut()
                        .zip(grad.iter())
                        .for_each(|(x, g)| *x -= step_size * g);
                }
                p
            })
            .collect()
    }

    /// Compute violations for a batch of points in parallel.
    ///
    /// Returns `violations[point_idx][constraint_idx]` = violation amount.
    /// Points and constraints are evaluated in a parallel outer loop over points.
    pub fn compute_violations_parallel(
        &self,
        points: &[Vec<f64>],
        constraints: &[ConstraintFn],
    ) -> Vec<Vec<f64>> {
        points
            .par_iter()
            .map(|point| {
                constraints
                    .iter()
                    .map(|c| c(point.as_slice()))
                    .collect::<Vec<f64>>()
            })
            .collect()
    }
}

// ============================================================================
// SIMD-optimized range constraint checking
// ============================================================================

/// Check whether all values in a slice are within `[min, max]`.
///
/// Written in an auto-vectorization-friendly pattern so LLVM can emit
/// SSE/AVX/NEON SIMD instructions. Returns `(all_satisfied, violation_indices)`.
pub fn check_range_constraints_simd(values: &[f64], min: f64, max: f64) -> (bool, Vec<usize>) {
    let violations: Vec<usize> = values
        .iter()
        .enumerate()
        .filter(|(_, &v)| v < min || v > max)
        .map(|(i, _)| i)
        .collect();
    (violations.is_empty(), violations)
}

// ============================================================================
// Parallel range constraint checking for multiple variable groups
// ============================================================================

/// Check whether each variable group satisfies its corresponding `(min, max)` bound.
///
/// Groups and bounds are evaluated in parallel using rayon. Returns a boolean
/// per group indicating satisfaction.
pub fn check_constraints_parallel(
    variable_groups: &[Vec<f64>],
    bounds: &[(f64, f64)],
) -> Vec<bool> {
    variable_groups
        .par_iter()
        .zip(bounds.par_iter())
        .map(|(group, &(min, max))| group.iter().all(|&v| v >= min && v <= max))
        .collect()
}

// ============================================================================
// ParallelIncrementalSolver
// ============================================================================

/// Multi-threaded incremental constraint solver using parallel projected gradient descent.
///
/// Constraints and gradients can be added/removed dynamically. Solving runs a
/// parallel gradient aggregation step over all active constraints, enabling
/// work-stealing across threads for large constraint sets.
pub struct ParallelIncrementalSolver {
    /// Current solution vector (shared, mutex-protected)
    pub solution: Arc<Mutex<Vec<f64>>>,
    /// Indices into `constraints`/`gradients` that are currently active
    active_constraints: Arc<Mutex<Vec<usize>>>,
    /// All registered constraint violation functions
    constraints: Vec<ConstraintFn>,
    /// Gradient functions corresponding to each constraint
    gradients: Vec<GradientFn>,
    /// Convergence tolerance
    pub tolerance: f64,
    /// Maximum number of solver iterations
    pub max_iterations: usize,
}

impl ParallelIncrementalSolver {
    /// Create a new solver for a problem of the given `dimension`.
    ///
    /// The initial solution is the zero vector.
    pub fn new(dimension: usize) -> Self {
        Self {
            solution: Arc::new(Mutex::new(vec![0.0f64; dimension])),
            active_constraints: Arc::new(Mutex::new(Vec::new())),
            constraints: Vec::new(),
            gradients: Vec::new(),
            tolerance: 1e-4,
            max_iterations: 1000,
        }
    }

    /// Register a new constraint and its gradient function.
    ///
    /// Returns the constraint id (its index), which can be used with
    /// [`remove_constraint`](Self::remove_constraint).
    pub fn add_constraint(&mut self, constraint: ConstraintFn, gradient: GradientFn) -> usize {
        let id = self.constraints.len();
        self.constraints.push(constraint);
        self.gradients.push(gradient);
        // Activate immediately
        match self.active_constraints.lock() {
            Ok(mut active) => active.push(id),
            Err(poisoned) => poisoned.into_inner().push(id),
        }
        id
    }

    /// Deactivate a constraint by its id.
    ///
    /// Returns `true` if the constraint was active and has been removed.
    pub fn remove_constraint(&mut self, id: usize) -> bool {
        match self.active_constraints.lock() {
            Ok(mut active) => {
                if let Some(pos) = active.iter().position(|&x| x == id) {
                    active.remove(pos);
                    true
                } else {
                    false
                }
            }
            Err(poisoned) => {
                let mut active = poisoned.into_inner();
                if let Some(pos) = active.iter().position(|&x| x == id) {
                    active.remove(pos);
                    true
                } else {
                    false
                }
            }
        }
    }

    /// Execute one step of parallel projected gradient descent.
    ///
    /// For each active constraint, violation and gradient are evaluated in
    /// parallel (rayon). Gradients are weighted by violation and summed. The
    /// solution is updated by a fixed step along the aggregate gradient.
    ///
    /// Returns the total constraint violation after the step.
    pub fn step(&self) -> Result<f64, String> {
        let step_size = 0.01f64;

        // Snapshot the current solution
        let current = self.solution.lock().map_err(|e| {
            format!(
                "ParallelIncrementalSolver::step — solution lock poisoned: {}",
                e
            )
        })?;
        let sol_snapshot = current.clone();
        drop(current);

        // Snapshot active constraint indices
        let active_ids: Vec<usize> = self
            .active_constraints
            .lock()
            .map_err(|e| {
                format!(
                    "ParallelIncrementalSolver::step — active_constraints lock poisoned: {}",
                    e
                )
            })?
            .clone();

        if active_ids.is_empty() {
            return Ok(0.0);
        }

        let dim = sol_snapshot.len();

        // Parallel evaluation: (violation, gradient) per active constraint
        let evaluated: Vec<(f64, Vec<f64>)> = active_ids
            .par_iter()
            .map(|&idx| {
                let violation = (self.constraints[idx])(&sol_snapshot);
                let grad = (self.gradients[idx])(&sol_snapshot);
                (violation, grad)
            })
            .collect();

        // Aggregate: total violation and weighted gradient sum
        let total_violation: f64 = evaluated.iter().map(|(v, _)| *v).sum();

        // Sum gradients weighted by their violation (only positive violation contributes)
        let mut aggregate_grad = vec![0.0f64; dim];
        for (violation, grad) in &evaluated {
            if *violation > 0.0 {
                for (ag, gv) in aggregate_grad.iter_mut().zip(grad.iter()) {
                    *ag += violation * gv;
                }
            }
        }

        // Apply update
        let mut sol = self.solution.lock().map_err(|e| {
            format!(
                "ParallelIncrementalSolver::step — solution write lock poisoned: {}",
                e
            )
        })?;
        for (x, g) in sol.iter_mut().zip(aggregate_grad.iter()) {
            *x -= step_size * g;
        }

        Ok(total_violation)
    }

    /// Run the solver until convergence or `max_iterations` is reached.
    ///
    /// Returns the converged solution vector, or an error string if a lock
    /// was poisoned.
    pub fn solve(&self) -> Result<Vec<f64>, String> {
        for _iter in 0..self.max_iterations {
            let total_violation = self.step()?;
            if total_violation < self.tolerance {
                break;
            }
        }
        Ok(self.solution())
    }

    /// Return a snapshot of the current solution.
    pub fn solution(&self) -> Vec<f64> {
        match self.solution.lock() {
            Ok(sol) => sol.clone(),
            Err(poisoned) => poisoned.into_inner().clone(),
        }
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_range_constraint_simd_all_satisfied() {
        let vals = vec![1.0f64, 2.0, 3.0];
        let (ok, violations) = check_range_constraints_simd(&vals, 0.0, 5.0);
        assert!(ok);
        assert!(violations.is_empty());
    }

    #[test]
    fn test_range_constraint_simd_violations() {
        let vals = vec![-1.0f64, 2.0, 6.0];
        let (ok, violations) = check_range_constraints_simd(&vals, 0.0, 5.0);
        assert!(!ok);
        assert_eq!(violations, vec![0, 2]);
    }

    #[test]
    fn test_constraint_graph_empty_evaluate() {
        let mut graph = ConstraintGraph::new();
        graph.compute_layers();
        let violations = graph.evaluate_parallel(&[1.0, 2.0]);
        assert!(violations.is_empty());
    }

    #[test]
    fn test_constraint_graph_parallel_evaluation() {
        let mut graph = ConstraintGraph::new();
        let n0 = ConstraintNode {
            id: 0,
            constraint_fn: Arc::new(|v: &[f64]| (v[0] - 1.0).max(0.0)),
            dependencies: vec![],
            variables: vec![0],
        };
        let n1 = ConstraintNode {
            id: 1,
            constraint_fn: Arc::new(|v: &[f64]| (-v[1]).max(0.0)),
            dependencies: vec![],
            variables: vec![1],
        };
        graph.add_node(n0);
        graph.add_node(n1);
        graph.compute_layers();

        // x = [0.5, 1.0] — both satisfied
        let violations = graph.evaluate_parallel(&[0.5, 1.0]);
        assert_eq!(violations.len(), 2);
        assert!(violations[0].abs() < 1e-9);
        assert!(violations[1].abs() < 1e-9);

        // x = [2.0, -1.0] — both violated
        let violations2 = graph.evaluate_parallel(&[2.0, -1.0]);
        assert!(violations2[0] > 0.0);
        assert!(violations2[1] > 0.0);
    }

    #[test]
    fn test_constraint_graph_with_dependencies() {
        let mut graph = ConstraintGraph::new();
        // n0: no deps
        let n0 = ConstraintNode {
            id: 0,
            constraint_fn: Arc::new(|v: &[f64]| (v[0] - 1.0).max(0.0)),
            dependencies: vec![],
            variables: vec![0],
        };
        // n1: depends on n0
        let n1 = ConstraintNode {
            id: 1,
            constraint_fn: Arc::new(|v: &[f64]| (v[1] - 2.0).max(0.0)),
            dependencies: vec![0],
            variables: vec![1],
        };
        graph.add_node(n0);
        graph.add_node(n1);
        graph.compute_layers();

        // Should have 2 layers: layer 0 = [0], layer 1 = [1]
        assert_eq!(graph.layers.len(), 2);
        assert_eq!(graph.layers[0], vec![0]);
        assert_eq!(graph.layers[1], vec![1]);

        let violations = graph.evaluate_parallel(&[0.5, 1.5]);
        assert_eq!(violations.len(), 2);
        // Both satisfied
        assert!(violations[0].abs() < 1e-9);
        assert!(violations[1].abs() < 1e-9);
    }

    #[test]
    fn test_constraint_graph_all_satisfied_parallel() {
        let mut graph = ConstraintGraph::new();
        let n0 = ConstraintNode {
            id: 0,
            constraint_fn: Arc::new(|v: &[f64]| (v[0] - 1.0).max(0.0)),
            dependencies: vec![],
            variables: vec![0],
        };
        graph.add_node(n0);
        graph.compute_layers();

        assert!(graph.all_satisfied_parallel(&[0.5], 1e-9));
        assert!(!graph.all_satisfied_parallel(&[2.0], 1e-9));
    }

    #[test]
    fn test_parallel_batch_projector_project_batch() {
        let projector = ParallelBatchProjector::new(2);
        let points = vec![vec![2.0f64], vec![0.5f64]];
        // Constraint: x[0] <= 1.0 (returns true if satisfied)
        let constraint = |v: &[f64]| v[0] <= 1.0;
        // Gradient: pushes x[0] toward 1.0 when > 1.0
        let gradient = |v: &[f64]| if v[0] > 1.0 { vec![1.0] } else { vec![0.0] };

        let projected = projector.project_batch(&points, &constraint, &gradient);
        assert_eq!(projected.len(), 2);
        // Second point was already feasible
        assert!((projected[1][0] - 0.5).abs() < 1e-9);
        // First point should have been pushed down
        assert!(projected[0][0] <= 1.0 + 1e-6);
    }

    type ConstraintFn = Arc<dyn Fn(&[f64]) -> f64 + Send + Sync>;

    #[test]
    fn test_parallel_batch_projector_violations() {
        let projector = ParallelBatchProjector::new(2);
        let points = vec![vec![0.5f64, 0.5], vec![2.0, 2.0]];
        let constraint: ConstraintFn =
            Arc::new(|v: &[f64]| (v[0] - 1.0).max(0.0) + (v[1] - 1.0).max(0.0));
        let violations = projector.compute_violations_parallel(&points, &[constraint]);
        assert_eq!(violations.len(), 2);
        assert!(violations[0][0].abs() < 1e-9);
        assert!(violations[1][0] > 0.0);
    }

    #[test]
    fn test_check_constraints_parallel() {
        let groups = vec![vec![0.5f64, 0.8], vec![2.0f64, 3.0]];
        let bounds = vec![(0.0, 1.0), (0.0, 1.0)];
        let results = check_constraints_parallel(&groups, &bounds);
        assert_eq!(results.len(), 2);
        assert!(results[0]); // [0.5, 0.8] within [0, 1]
        assert!(!results[1]); // [2.0, 3.0] outside [0, 1]
    }

    #[test]
    fn test_check_constraints_parallel_empty() {
        let results = check_constraints_parallel(&[], &[]);
        assert!(results.is_empty());
    }

    #[test]
    fn test_parallel_incremental_solver_basic() {
        let mut solver = ParallelIncrementalSolver::new(2);
        // Constraint: x[0] <= 0.5 (violation = max(0, x[0] - 0.5))
        solver.add_constraint(
            Arc::new(|v: &[f64]| (v[0] - 0.5).max(0.0)),
            Arc::new(|v: &[f64]| {
                if v[0] > 0.5 {
                    vec![1.0, 0.0]
                } else {
                    vec![0.0, 0.0]
                }
            }),
        );
        // Start with x = [1.0, 0.0]
        {
            let mut sol = solver.solution.lock().expect("lock failed in test setup");
            *sol = vec![1.0, 0.0];
        }
        let result = solver.solve();
        assert!(result.is_ok());
        let sol = result.unwrap();
        assert!(sol[0] <= 0.5 + 0.01); // within tolerance
    }

    #[test]
    fn test_parallel_incremental_solver_no_constraints() {
        let solver = ParallelIncrementalSolver::new(3);
        let result = solver.solve();
        assert!(result.is_ok());
        let sol = result.unwrap();
        assert_eq!(sol.len(), 3);
    }

    #[test]
    fn test_parallel_incremental_solver_add_remove() {
        let mut solver = ParallelIncrementalSolver::new(2);
        let id = solver.add_constraint(
            Arc::new(|v: &[f64]| (v[0] - 0.5).max(0.0)),
            Arc::new(|_v: &[f64]| vec![1.0, 0.0]),
        );
        assert_eq!(id, 0);
        let removed = solver.remove_constraint(id);
        assert!(removed);
        // Removing again should return false
        let removed_again = solver.remove_constraint(id);
        assert!(!removed_again);
    }

    #[test]
    fn test_parallel_incremental_solver_step() {
        let mut solver = ParallelIncrementalSolver::new(1);
        solver.add_constraint(
            Arc::new(|v: &[f64]| (v[0] - 0.0).max(0.0)), // x[0] <= 0
            Arc::new(|_v: &[f64]| vec![1.0]),
        );
        {
            let mut sol = solver.solution.lock().expect("lock in test");
            *sol = vec![1.0];
        }
        let violation = solver.step().expect("step should succeed");
        // Violation was 1.0 initially; after one step it should decrease
        assert!(violation >= 0.0);
        let sol = solver.solution();
        assert!(sol[0] < 1.0); // solution moved toward constraint
    }
}