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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
//! SymbolicNeuralOptimizer — hybrid optimizer combining symbolic rule-based
//! parameter updates with gradient-based neural optimization.
//!
//! Symbolic constraints guide the search space and apply logical
//! post-corrections to neural gradient steps. Hard constraints are enforced
//! via clamping; soft constraints add a penalty term to the loss.
// ---------------------------------------------------------------------------
// Constraint expression operator tokens
// ---------------------------------------------------------------------------
const OP_GE: &str = ">=";
const OP_LE: &str = "<=";
const OP_EQ: &str = "==";
// ---------------------------------------------------------------------------
// OptimizationObjective
// ---------------------------------------------------------------------------
/// The high-level goal of the optimization run.
#[derive(Clone, Debug, Default, PartialEq)]
pub enum OptimizationObjective {
/// Minimize the scalar loss returned by the loss function.
#[default]
Minimize,
/// Maximize the scalar loss returned by the loss function (internally the
/// optimizer negates the value so that a minimiser can be reused).
Maximize,
/// Satisfy the named constraints; loss is the total weighted violation.
Satisfy(Vec<String>),
}
// ---------------------------------------------------------------------------
// SymbolicConstraint
// ---------------------------------------------------------------------------
/// A named symbolic constraint with a weight and hardness flag.
///
/// Hard constraints are enforced by clamping parameters. Soft constraints
/// contribute a penalty term to the loss function.
#[derive(Clone, Debug, PartialEq)]
pub struct SymbolicConstraint {
/// Unique identifier for this constraint.
pub name: String,
/// Simple single-variable expression, e.g. `"x >= 0.0"`.
pub expression: String,
/// Weight used for soft-constraint penalties and gradient nudges.
pub weight: f64,
/// When `true` the constraint is enforced by clamping (hard).
/// When `false` the constraint enters the loss as a penalty (soft).
pub is_hard: bool,
}
impl SymbolicConstraint {
/// Construct a new constraint.
pub fn new(
name: impl Into<String>,
expression: impl Into<String>,
weight: f64,
is_hard: bool,
) -> Self {
SymbolicConstraint {
name: name.into(),
expression: expression.into(),
weight,
is_hard,
}
}
/// Return a hard constraint.
pub fn hard(name: impl Into<String>, expression: impl Into<String>, weight: f64) -> Self {
Self::new(name, expression, weight, true)
}
/// Return a soft constraint.
pub fn soft(name: impl Into<String>, expression: impl Into<String>, weight: f64) -> Self {
Self::new(name, expression, weight, false)
}
}
// ---------------------------------------------------------------------------
// ParameterVector
// ---------------------------------------------------------------------------
/// A named parameter vector.
#[derive(Clone, Debug, PartialEq)]
pub struct ParameterVector {
/// Parameter values.
pub values: Vec<f64>,
/// Parameter names (parallel to `values`).
pub names: Vec<String>,
}
impl ParameterVector {
/// Construct a new `ParameterVector`.
///
/// If `names` and `values` lengths differ the shorter one determines the
/// logical length; the remainder is ignored.
pub fn new(names: Vec<String>, values: Vec<f64>) -> Self {
ParameterVector { names, values }
}
/// Return the value of the parameter with the given name, if present.
pub fn get(&self, name: &str) -> Option<f64> {
self.names
.iter()
.position(|n| n == name)
.map(|i| self.values[i])
}
/// Set the value of the parameter with the given name.
///
/// Returns `true` if the name was found and updated, `false` otherwise.
pub fn set(&mut self, name: &str, value: f64) -> bool {
if let Some(i) = self.names.iter().position(|n| n == name) {
self.values[i] = value;
true
} else {
false
}
}
/// Compute the L2 (Euclidean) norm of the value vector.
pub fn l2_norm(&self) -> f64 {
self.values.iter().map(|v| v * v).sum::<f64>().sqrt()
}
/// Number of parameters.
pub fn len(&self) -> usize {
self.values.len().min(self.names.len())
}
/// Return `true` when there are no parameters.
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Iterate over `(name, value)` pairs.
pub fn iter(&self) -> impl Iterator<Item = (&str, f64)> {
let n = self.len();
self.names[..n]
.iter()
.map(String::as_str)
.zip(self.values[..n].iter().copied())
}
}
// ---------------------------------------------------------------------------
// SnoOptimizationStep (renamed to avoid collision with OptimizationStep from
// optimization_history module)
// ---------------------------------------------------------------------------
/// A single recorded step produced by [`SymbolicNeuralOptimizer`].
#[derive(Clone, Debug, PartialEq)]
pub struct SnoOptimizationStep {
/// Monotonically increasing iteration counter (0-based).
pub iteration: u64,
/// Loss value after this step.
pub loss: f64,
/// L2 norm of the gradient at this step.
pub gradient_norm: f64,
/// Number of hard constraint violations after the update.
pub constraint_violations: usize,
/// Parameter state after the update.
pub params: ParameterVector,
}
// ---------------------------------------------------------------------------
// SnoOptimizationResult (renamed to avoid collision with OptimizationResult
// from query_optimizer module)
// ---------------------------------------------------------------------------
/// The final result returned by [`SymbolicNeuralOptimizer::optimize`].
#[derive(Clone, Debug, PartialEq)]
pub struct SnoOptimizationResult {
/// `true` when the run finished without error.
pub success: bool,
/// Total number of iterations executed.
pub iterations: u64,
/// Loss at the final iteration.
pub final_loss: f64,
/// Parameter values at the final iteration.
pub final_params: ParameterVector,
/// Number of hard constraint violations in the final parameter state.
pub constraint_violations: usize,
/// `true` when the run stopped due to convergence rather than hitting the
/// iteration limit.
pub converged: bool,
}
// ---------------------------------------------------------------------------
// OptimizerConfig
// ---------------------------------------------------------------------------
/// Configuration for [`SymbolicNeuralOptimizer`].
#[derive(Clone, Debug, PartialEq)]
pub struct SnoOptimizerConfig {
/// Gradient descent step size.
pub learning_rate: f64,
/// Maximum number of optimization iterations.
pub max_iterations: u64,
/// Convergence threshold: stop when |loss_prev − loss_curr| < threshold.
pub convergence_threshold: f64,
/// Penalty multiplier applied to soft constraint violations.
pub constraint_penalty: f64,
/// Weight applied to symbolic correction nudges relative to the gradient.
pub symbolic_correction_weight: f64,
/// High-level objective of the optimization.
pub objective: OptimizationObjective,
}
impl Default for SnoOptimizerConfig {
fn default() -> Self {
SnoOptimizerConfig {
learning_rate: 0.01,
max_iterations: 1000,
convergence_threshold: 1e-6,
constraint_penalty: 10.0,
symbolic_correction_weight: 0.5,
objective: OptimizationObjective::Minimize,
}
}
}
impl SnoOptimizerConfig {
/// Create a new config with default values.
pub fn new() -> Self {
Self::default()
}
/// Builder: set learning rate.
pub fn with_learning_rate(mut self, lr: f64) -> Self {
self.learning_rate = lr;
self
}
/// Builder: set max iterations.
pub fn with_max_iterations(mut self, n: u64) -> Self {
self.max_iterations = n;
self
}
/// Builder: set convergence threshold.
pub fn with_convergence_threshold(mut self, t: f64) -> Self {
self.convergence_threshold = t;
self
}
/// Builder: set constraint penalty.
pub fn with_constraint_penalty(mut self, p: f64) -> Self {
self.constraint_penalty = p;
self
}
/// Builder: set symbolic correction weight.
pub fn with_symbolic_correction_weight(mut self, w: f64) -> Self {
self.symbolic_correction_weight = w;
self
}
/// Builder: set optimization objective.
pub fn with_objective(mut self, obj: OptimizationObjective) -> Self {
self.objective = obj;
self
}
}
// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------
/// Xorshift64 pseudo-random number generator.
///
/// Advances `state` and returns the new value.
pub fn xorshift64(state: &mut u64) -> u64 {
let mut x = *state;
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
*state = x;
x
}
/// Parsed bound from a simple constraint expression.
#[derive(Clone, Debug, PartialEq)]
pub struct ConstraintBound {
/// Parameter name on the left-hand side.
pub param_name: String,
/// Operator: `">="`, `"<="`, or `"=="`.
pub operator: String,
/// Bound value on the right-hand side.
pub bound: f64,
}
/// Parse a simple single-variable constraint expression.
///
/// Recognised forms:
/// - `"x >= 0.0"` — lower bound
/// - `"x <= 1.0"` — upper bound
/// - `"x == 0.5"` — equality
///
/// Returns `None` for any expression that does not match these forms.
pub fn parse_constraint_bound(expr: &str) -> Option<ConstraintBound> {
let expr = expr.trim();
// Try each operator in longest-first order to avoid `>` matching `>=`.
for op in &[OP_GE, OP_LE, OP_EQ] {
if let Some(pos) = expr.find(op) {
let name_part = expr[..pos].trim();
let val_part = expr[pos + op.len()..].trim();
if name_part.is_empty() || val_part.is_empty() {
continue;
}
// The name must be a simple identifier (no spaces).
if name_part.contains(' ') {
continue;
}
if let Ok(bound) = val_part.parse::<f64>() {
return Some(ConstraintBound {
param_name: name_part.to_owned(),
operator: op.to_string(),
bound,
});
}
}
}
None
}
/// Compute the violation amount for a parsed bound given a parameter value.
///
/// Returns `0.0` when the constraint is satisfied, a positive amount when it
/// is violated.
fn violation_amount(bound: &ConstraintBound, value: f64) -> f64 {
match bound.operator.as_str() {
">=" if value < bound.bound => bound.bound - value,
"<=" if value > bound.bound => value - bound.bound,
"==" => (value - bound.bound).abs(),
_ => 0.0,
}
}
/// Clamp a value so that it satisfies a hard constraint bound.
fn clamp_to_bound(bound: &ConstraintBound, value: f64) -> f64 {
match bound.operator.as_str() {
">=" => value.max(bound.bound),
"<=" => value.min(bound.bound),
"==" => bound.bound,
_ => value,
}
}
// ---------------------------------------------------------------------------
// SymbolicNeuralOptimizer
// ---------------------------------------------------------------------------
/// A hybrid optimizer that combines symbolic rule-based parameter updates with
/// gradient-based neural optimization.
///
/// # How it works
///
/// 1. **Gradient step** — apply a standard gradient-descent update using the
/// configured learning rate.
/// 2. **Hard constraint enforcement** — for each hard constraint whose
/// expression can be parsed as a simple bound, clamp the relevant parameter
/// to the feasible region.
/// 3. **Soft constraint correction** — apply a small gradient nudge
/// proportional to `weight × constraint_penalty × symbolic_correction_weight`
/// for each violated soft constraint.
///
/// The [`SymbolicNeuralOptimizer::optimize`] method drives the loop, calling a
/// user-supplied `loss_fn` at each iteration and recording history.
pub struct SymbolicNeuralOptimizer {
config: SnoOptimizerConfig,
constraints: Vec<SymbolicConstraint>,
history: Vec<SnoOptimizationStep>,
iteration: u64,
rng_state: u64,
}
impl SymbolicNeuralOptimizer {
/// Create a new optimizer with the given configuration.
///
/// The internal PRNG is seeded to `12345`.
pub fn new(config: SnoOptimizerConfig) -> Self {
SymbolicNeuralOptimizer {
config,
constraints: Vec::new(),
history: Vec::new(),
iteration: 0,
rng_state: 12345,
}
}
/// Add a symbolic constraint.
pub fn add_constraint(&mut self, constraint: SymbolicConstraint) {
self.constraints.push(constraint);
}
/// Remove all constraints whose name equals `name`.
///
/// Returns `true` if at least one constraint was removed.
pub fn remove_constraint(&mut self, name: &str) -> bool {
let before = self.constraints.len();
self.constraints.retain(|c| c.name != name);
self.constraints.len() < before
}
/// Return all registered constraints.
pub fn constraints(&self) -> &[SymbolicConstraint] {
&self.constraints
}
/// Return the optimization history (one entry per completed step).
pub fn history(&self) -> &[SnoOptimizationStep] {
&self.history
}
/// Return the step with the lowest recorded loss, or `None` if no steps
/// have been taken yet.
pub fn best_step(&self) -> Option<&SnoOptimizationStep> {
self.history.iter().min_by(|a, b| {
a.loss
.partial_cmp(&b.loss)
.unwrap_or(std::cmp::Ordering::Equal)
})
}
/// Return the current iteration counter.
pub fn iteration(&self) -> u64 {
self.iteration
}
/// Compute the total loss for the given parameters and residuals.
///
/// The base loss is the mean squared residual. Each **soft** constraint
/// that is violated adds `weight × constraint_penalty × violation_amount`.
pub fn compute_loss(&self, params: &ParameterVector, residuals: &[f64]) -> f64 {
// Base loss: mean squared residual.
let base_loss = if residuals.is_empty() {
0.0
} else {
residuals.iter().map(|r| r * r).sum::<f64>() / residuals.len() as f64
};
// Sign factor for Maximize objective.
let sign = match &self.config.objective {
OptimizationObjective::Maximize => -1.0,
_ => 1.0,
};
let penalty: f64 = self
.constraints
.iter()
.filter(|c| !c.is_hard)
.map(|c| {
if let Some(bound) = parse_constraint_bound(&c.expression) {
let val = params.get(&bound.param_name).unwrap_or(0.0);
let viol = violation_amount(&bound, val);
c.weight * self.config.constraint_penalty * viol
} else {
0.0
}
})
.sum();
sign * base_loss + penalty
}
/// Count the number of **hard** constraints that are currently violated.
pub fn check_constraints(&self, params: &ParameterVector) -> usize {
self.constraints
.iter()
.filter(|c| c.is_hard)
.filter(|c| {
if let Some(bound) = parse_constraint_bound(&c.expression) {
let val = params.get(&bound.param_name).unwrap_or(0.0);
violation_amount(&bound, val) > 0.0
} else {
false
}
})
.count()
}
/// Perform a single optimization step.
///
/// 1. Apply the gradient step: `new_val = val − lr × grad_val`.
/// 2. Enforce hard constraints by clamping.
/// 3. Apply soft constraint gradient nudges.
pub fn step(
&mut self,
params: &ParameterVector,
gradient: &ParameterVector,
) -> ParameterVector {
let lr = self.config.learning_rate;
let penalty = self.config.constraint_penalty;
let corr_w = self.config.symbolic_correction_weight;
let n = params.len();
let mut new_values = Vec::with_capacity(n);
// --- 1. Gradient step ---
for i in 0..n {
let val = params.values[i];
// Look up gradient by name for robustness against ordering
// differences between the two vectors.
let grad = gradient.get(¶ms.names[i]).unwrap_or(0.0);
new_values.push(val - lr * grad);
}
let mut new_params = ParameterVector::new(params.names[..n].to_vec(), new_values);
// --- 2. Hard constraint clamping ---
for constraint in &self.constraints {
if !constraint.is_hard {
continue;
}
if let Some(bound) = parse_constraint_bound(&constraint.expression) {
let current = new_params.get(&bound.param_name).unwrap_or(f64::NAN);
if !current.is_nan() {
let clamped = clamp_to_bound(&bound, current);
new_params.set(&bound.param_name, clamped);
}
}
}
// --- 3. Soft constraint gradient nudge ---
for constraint in &self.constraints {
if constraint.is_hard {
continue;
}
if let Some(bound) = parse_constraint_bound(&constraint.expression) {
let val = new_params.get(&bound.param_name).unwrap_or(f64::NAN);
if val.is_nan() {
continue;
}
let viol = violation_amount(&bound, val);
if viol <= 0.0 {
continue;
}
// Nudge direction: towards the feasible side.
let nudge_dir = match bound.operator.as_str() {
">=" => 1.0, // must go up
"<=" => -1.0, // must go down
"==" => {
if val < bound.bound {
1.0
} else {
-1.0
}
}
_ => 0.0,
};
let nudge = nudge_dir * constraint.weight * penalty * corr_w * viol;
new_params.set(&bound.param_name, val + nudge);
}
}
self.iteration += 1;
new_params
}
/// Run the full optimization loop.
///
/// `loss_fn` receives the current `ParameterVector` and must return
/// `(loss, gradient)` where `gradient` is a `ParameterVector` with the
/// same names as the input, containing the partial derivatives of the loss
/// with respect to each parameter.
///
/// The loop terminates when:
/// - `|loss_prev − loss_curr| < convergence_threshold` (converged), or
/// - `iteration >= max_iterations`.
pub fn optimize(
&mut self,
initial_params: ParameterVector,
loss_fn: &dyn Fn(&ParameterVector) -> (f64, ParameterVector),
) -> SnoOptimizationResult {
let mut params = initial_params;
let mut prev_loss = f64::INFINITY;
let mut converged = false;
self.history.clear();
self.iteration = 0;
for _iter in 0..self.config.max_iterations {
let (loss, gradient) = loss_fn(¶ms);
let grad_norm = gradient.l2_norm();
let violations = self.check_constraints(¶ms);
let step_record = SnoOptimizationStep {
iteration: self.iteration,
loss,
gradient_norm: grad_norm,
constraint_violations: violations,
params: params.clone(),
};
self.history.push(step_record);
// Convergence check (before taking the next step).
if (prev_loss - loss).abs() < self.config.convergence_threshold {
converged = true;
break;
}
prev_loss = loss;
params = self.step(¶ms, &gradient);
}
// Record a final step after the last update so that `final_params`
// reflects the state after the last gradient step.
let (final_loss, _) = loss_fn(¶ms);
let final_violations = self.check_constraints(¶ms);
SnoOptimizationResult {
success: true,
iterations: self.iteration,
final_loss,
final_params: params,
constraint_violations: final_violations,
converged,
}
}
/// Access the raw PRNG state (useful for reproducibility testing).
pub fn rng_state(&self) -> u64 {
self.rng_state
}
/// Advance the internal PRNG and return a random `u64`.
pub fn rand_u64(&mut self) -> u64 {
xorshift64(&mut self.rng_state)
}
/// Advance the internal PRNG and return a random `f64` in `[0, 1)`.
pub fn rand_f64(&mut self) -> f64 {
let r = xorshift64(&mut self.rng_state);
(r as f64) / (u64::MAX as f64)
}
/// Reset the optimizer state (history, iteration counter) but keep the
/// configuration and constraints.
pub fn reset(&mut self) {
self.history.clear();
self.iteration = 0;
}
/// Return a reference to the configuration.
pub fn config(&self) -> &SnoOptimizerConfig {
&self.config
}
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
// -----------------------------------------------------------------------
// Helper factories
// -----------------------------------------------------------------------
fn default_config() -> SnoOptimizerConfig {
SnoOptimizerConfig::default()
}
fn optimizer() -> SymbolicNeuralOptimizer {
SymbolicNeuralOptimizer::new(default_config())
}
fn params(names: &[&str], values: &[f64]) -> ParameterVector {
ParameterVector::new(
names.iter().map(|s| s.to_string()).collect(),
values.to_vec(),
)
}
fn gradient_from(names: &[&str], grads: &[f64]) -> ParameterVector {
params(names, grads)
}
// -----------------------------------------------------------------------
// ParameterVector tests
// -----------------------------------------------------------------------
#[test]
fn test_parameter_vector_new() {
let pv = params(&["x", "y"], &[1.0, 2.0]);
assert_eq!(pv.len(), 2);
assert!(!pv.is_empty());
}
#[test]
fn test_parameter_vector_get_existing() {
let pv = params(&["x", "y"], &[3.0, 7.0]);
assert_eq!(pv.get("x"), Some(3.0));
assert_eq!(pv.get("y"), Some(7.0));
}
#[test]
fn test_parameter_vector_get_missing() {
let pv = params(&["x"], &[1.0]);
assert_eq!(pv.get("z"), None);
}
#[test]
fn test_parameter_vector_set_existing() {
let mut pv = params(&["x", "y"], &[1.0, 2.0]);
assert!(pv.set("x", 99.0));
assert_eq!(pv.get("x"), Some(99.0));
}
#[test]
fn test_parameter_vector_set_missing() {
let mut pv = params(&["x"], &[1.0]);
assert!(!pv.set("z", 0.0));
}
#[test]
fn test_parameter_vector_l2_norm_zero() {
let pv = params(&["x", "y"], &[0.0, 0.0]);
assert!((pv.l2_norm() - 0.0).abs() < 1e-12);
}
#[test]
fn test_parameter_vector_l2_norm_unit() {
let pv = params(&["x", "y"], &[1.0, 0.0]);
assert!((pv.l2_norm() - 1.0).abs() < 1e-12);
}
#[test]
fn test_parameter_vector_l2_norm_pythagorean() {
let pv = params(&["x", "y"], &[3.0, 4.0]);
assert!((pv.l2_norm() - 5.0).abs() < 1e-12);
}
#[test]
fn test_parameter_vector_iter() {
let pv = params(&["a", "b"], &[10.0, 20.0]);
let collected: Vec<_> = pv.iter().collect();
assert_eq!(collected, vec![("a", 10.0), ("b", 20.0)]);
}
#[test]
fn test_parameter_vector_empty() {
let pv = ParameterVector::new(vec![], vec![]);
assert!(pv.is_empty());
assert_eq!(pv.l2_norm(), 0.0);
}
// -----------------------------------------------------------------------
// SymbolicConstraint tests
// -----------------------------------------------------------------------
#[test]
fn test_symbolic_constraint_hard() {
let c = SymbolicConstraint::hard("lower_x", "x >= 0.0", 1.0);
assert!(c.is_hard);
assert_eq!(c.name, "lower_x");
assert!((c.weight - 1.0).abs() < 1e-12);
}
#[test]
fn test_symbolic_constraint_soft() {
let c = SymbolicConstraint::soft("upper_y", "y <= 1.0", 0.5);
assert!(!c.is_hard);
}
// -----------------------------------------------------------------------
// parse_constraint_bound tests
// -----------------------------------------------------------------------
#[test]
fn test_parse_ge() {
let b = parse_constraint_bound("x >= 0.0").expect("test: should succeed");
assert_eq!(b.param_name, "x");
assert_eq!(b.operator, ">=");
assert!((b.bound - 0.0).abs() < 1e-12);
}
#[test]
fn test_parse_le() {
let b = parse_constraint_bound("alpha <= 1.5").expect("test: should succeed");
assert_eq!(b.param_name, "alpha");
assert_eq!(b.operator, "<=");
assert!((b.bound - 1.5).abs() < 1e-12);
}
#[test]
fn test_parse_eq() {
let b = parse_constraint_bound("bias == 0.5").expect("test: should succeed");
assert_eq!(b.param_name, "bias");
assert_eq!(b.operator, "==");
assert!((b.bound - 0.5).abs() < 1e-12);
}
#[test]
fn test_parse_negative_bound() {
let b = parse_constraint_bound("x >= -1.0").expect("test: should succeed");
assert!((b.bound - (-1.0)).abs() < 1e-12);
}
#[test]
fn test_parse_no_spaces() {
// Tight format without spaces should still parse.
let b = parse_constraint_bound("x>=0.0").expect("test: should succeed");
assert_eq!(b.operator, ">=");
}
#[test]
fn test_parse_invalid_expr() {
// No operator
assert!(parse_constraint_bound("x 0.0").is_none());
}
#[test]
fn test_parse_invalid_value() {
assert!(parse_constraint_bound("x >= abc").is_none());
}
// -----------------------------------------------------------------------
// Optimizer construction and config tests
// -----------------------------------------------------------------------
#[test]
fn test_optimizer_default_config() {
let opt = optimizer();
assert!((opt.config().learning_rate - 0.01).abs() < 1e-12);
assert_eq!(opt.config().max_iterations, 1000);
assert_eq!(opt.config().objective, OptimizationObjective::Minimize);
}
#[test]
fn test_optimizer_rng_initial_state() {
let opt = optimizer();
assert_eq!(opt.rng_state(), 12345);
}
#[test]
fn test_optimizer_rand_u64_deterministic() {
let mut opt = optimizer();
let r1 = opt.rand_u64();
let mut opt2 = optimizer();
let r2 = opt2.rand_u64();
assert_eq!(r1, r2);
}
#[test]
fn test_optimizer_rand_f64_in_unit_interval() {
let mut opt = optimizer();
for _ in 0..100 {
let r = opt.rand_f64();
assert!((0.0..=1.0).contains(&r));
}
}
// -----------------------------------------------------------------------
// Constraint management tests
// -----------------------------------------------------------------------
#[test]
fn test_add_and_remove_constraint() {
let mut opt = optimizer();
opt.add_constraint(SymbolicConstraint::hard("c1", "x >= 0.0", 1.0));
assert_eq!(opt.constraints().len(), 1);
assert!(opt.remove_constraint("c1"));
assert_eq!(opt.constraints().len(), 0);
}
#[test]
fn test_remove_nonexistent_constraint() {
let mut opt = optimizer();
assert!(!opt.remove_constraint("ghost"));
}
#[test]
fn test_add_multiple_constraints() {
let mut opt = optimizer();
opt.add_constraint(SymbolicConstraint::hard("c1", "x >= 0.0", 1.0));
opt.add_constraint(SymbolicConstraint::soft("c2", "y <= 1.0", 0.5));
assert_eq!(opt.constraints().len(), 2);
}
// -----------------------------------------------------------------------
// check_constraints tests
// -----------------------------------------------------------------------
#[test]
fn test_check_constraints_none_violated() {
let mut opt = optimizer();
opt.add_constraint(SymbolicConstraint::hard("lb", "x >= 0.0", 1.0));
let p = params(&["x"], &[1.0]);
assert_eq!(opt.check_constraints(&p), 0);
}
#[test]
fn test_check_constraints_one_violated() {
let mut opt = optimizer();
opt.add_constraint(SymbolicConstraint::hard("lb", "x >= 0.0", 1.0));
let p = params(&["x"], &[-1.0]);
assert_eq!(opt.check_constraints(&p), 1);
}
#[test]
fn test_check_constraints_soft_not_counted() {
let mut opt = optimizer();
opt.add_constraint(SymbolicConstraint::soft("s", "x <= 0.5", 1.0));
let p = params(&["x"], &[2.0]);
// Soft constraints don't count in check_constraints.
assert_eq!(opt.check_constraints(&p), 0);
}
// -----------------------------------------------------------------------
// compute_loss tests
// -----------------------------------------------------------------------
#[test]
fn test_compute_loss_no_residuals() {
let opt = optimizer();
let p = params(&["x"], &[0.0]);
assert!((opt.compute_loss(&p, &[]) - 0.0).abs() < 1e-12);
}
#[test]
fn test_compute_loss_mse() {
let opt = optimizer();
let p = params(&["x"], &[0.0]);
// MSE([1.0, -1.0]) = (1+1)/2 = 1.0
let loss = opt.compute_loss(&p, &[1.0, -1.0]);
assert!((loss - 1.0).abs() < 1e-12);
}
#[test]
fn test_compute_loss_soft_penalty() {
let mut opt = SymbolicNeuralOptimizer::new(
SnoOptimizerConfig::default().with_constraint_penalty(10.0),
);
// x must be >= 1.0 (soft), weight=1.0
opt.add_constraint(SymbolicConstraint::soft("lb", "x >= 1.0", 1.0));
let p = params(&["x"], &[0.0]); // violation = 1.0
let loss = opt.compute_loss(&p, &[]);
// penalty = 1.0 * 10.0 * 1.0 = 10.0; base_loss = 0
assert!((loss - 10.0).abs() < 1e-9);
}
#[test]
fn test_compute_loss_hard_no_penalty() {
let mut opt = optimizer();
opt.add_constraint(SymbolicConstraint::hard("lb", "x >= 1.0", 1.0));
let p = params(&["x"], &[0.0]); // violated, but hard → no penalty
let loss = opt.compute_loss(&p, &[]);
assert!((loss - 0.0).abs() < 1e-12);
}
// -----------------------------------------------------------------------
// step tests
// -----------------------------------------------------------------------
#[test]
fn test_step_gradient_descent() {
let mut opt = optimizer(); // lr = 0.01
let p = params(&["x"], &[1.0]);
let g = gradient_from(&["x"], &[10.0]);
let new_p = opt.step(&p, &g);
// 1.0 - 0.01 * 10.0 = 0.9
assert!((new_p.get("x").unwrap_or(0.0) - 0.9).abs() < 1e-12);
}
#[test]
fn test_step_increments_iteration() {
let mut opt = optimizer();
let p = params(&["x"], &[0.0]);
let g = gradient_from(&["x"], &[0.0]);
opt.step(&p, &g);
assert_eq!(opt.iteration(), 1);
opt.step(&p, &g);
assert_eq!(opt.iteration(), 2);
}
#[test]
fn test_step_hard_constraint_clamp_lower() {
let mut opt = optimizer();
opt.add_constraint(SymbolicConstraint::hard("lb", "x >= 0.0", 1.0));
// Start at 0.05, gradient=10 → raw new = 0.05 - 0.01*10 = -0.05 → clamped to 0.0
let p = params(&["x"], &[0.05]);
let g = gradient_from(&["x"], &[10.0]);
let new_p = opt.step(&p, &g);
assert!(new_p.get("x").unwrap_or(f64::NAN) >= 0.0);
}
#[test]
fn test_step_hard_constraint_clamp_upper() {
let mut opt = optimizer();
opt.add_constraint(SymbolicConstraint::hard("ub", "x <= 1.0", 1.0));
// Start at 0.95, gradient=-10 → raw new = 0.95 + 0.1 = 1.05 → clamped to 1.0
let p = params(&["x"], &[0.95]);
let g = gradient_from(&["x"], &[-10.0]);
let new_p = opt.step(&p, &g);
assert!(new_p.get("x").unwrap_or(f64::NAN) <= 1.0);
}
#[test]
fn test_step_hard_constraint_equality() {
let mut opt = optimizer();
opt.add_constraint(SymbolicConstraint::hard("eq", "x == 0.5", 1.0));
let p = params(&["x"], &[0.0]);
let g = gradient_from(&["x"], &[0.0]);
let new_p = opt.step(&p, &g);
// Hard equality forces x to 0.5.
assert!((new_p.get("x").unwrap_or(0.0) - 0.5).abs() < 1e-12);
}
#[test]
fn test_step_soft_constraint_nudge() {
// Set up optimizer with a soft lower bound.
let config = SnoOptimizerConfig::default()
.with_learning_rate(0.0) // no gradient movement
.with_constraint_penalty(1.0)
.with_symbolic_correction_weight(1.0);
let mut opt = SymbolicNeuralOptimizer::new(config);
opt.add_constraint(SymbolicConstraint::soft("lb", "x >= 1.0", 1.0));
// x starts at 0.5 → violation = 0.5 → nudge = +1.0*1.0*1.0*0.5 = 0.5
let p = params(&["x"], &[0.5]);
let g = gradient_from(&["x"], &[0.0]);
let new_p = opt.step(&p, &g);
let x_after = new_p.get("x").unwrap_or(0.0);
// Should be nudged upward toward 1.0.
assert!(x_after > 0.5, "Expected x_after > 0.5, got {}", x_after);
}
// -----------------------------------------------------------------------
// optimize tests
// -----------------------------------------------------------------------
#[test]
fn test_optimize_converges_quadratic() {
// Minimize (x - 2)^2; analytic gradient = 2*(x-2).
let config = SnoOptimizerConfig::default()
.with_learning_rate(0.1)
.with_max_iterations(500)
.with_convergence_threshold(1e-8);
let mut opt = SymbolicNeuralOptimizer::new(config);
let init = params(&["x"], &[0.0]);
let result = opt.optimize(init, &|p| {
let x = p.get("x").unwrap_or(0.0);
let loss = (x - 2.0) * (x - 2.0);
let grad = gradient_from(&["x"], &[2.0 * (x - 2.0)]);
(loss, grad)
});
assert!(result.success);
let x_final = result.final_params.get("x").unwrap_or(0.0);
assert!((x_final - 2.0).abs() < 0.1, "x_final={}", x_final);
}
#[test]
fn test_optimize_respects_max_iterations() {
let config = SnoOptimizerConfig::default()
.with_max_iterations(5)
.with_convergence_threshold(f64::EPSILON);
let mut opt = SymbolicNeuralOptimizer::new(config);
let init = params(&["x"], &[0.0]);
let result = opt.optimize(init, &|p| {
let x = p.get("x").unwrap_or(0.0);
let grad = gradient_from(&["x"], &[2.0 * x]);
(x * x, grad)
});
assert!(result.iterations <= 5);
}
#[test]
fn test_optimize_history_populated() {
let config = SnoOptimizerConfig::default()
.with_max_iterations(10)
.with_convergence_threshold(f64::EPSILON);
let mut opt = SymbolicNeuralOptimizer::new(config);
let init = params(&["x"], &[5.0]);
opt.optimize(init, &|p| {
let x = p.get("x").unwrap_or(0.0);
let grad = gradient_from(&["x"], &[2.0 * x]);
(x * x, grad)
});
assert!(!opt.history().is_empty());
}
#[test]
fn test_optimize_best_step_is_minimum_loss() {
let config = SnoOptimizerConfig::default()
.with_max_iterations(20)
.with_convergence_threshold(f64::EPSILON);
let mut opt = SymbolicNeuralOptimizer::new(config);
let init = params(&["x"], &[5.0]);
opt.optimize(init, &|p| {
let x = p.get("x").unwrap_or(0.0);
let grad = gradient_from(&["x"], &[2.0 * x]);
(x * x, grad)
});
let best = opt.best_step().expect("history non-empty");
let min_loss = opt
.history()
.iter()
.map(|s| s.loss)
.fold(f64::INFINITY, f64::min);
assert!((best.loss - min_loss).abs() < 1e-12);
}
#[test]
fn test_optimize_with_hard_constraint_satisfied() {
// Minimize x^2 with hard constraint x >= 1.0 → minimum is 1.0.
let config = SnoOptimizerConfig::default()
.with_learning_rate(0.1)
.with_max_iterations(200)
.with_convergence_threshold(1e-8);
let mut opt = SymbolicNeuralOptimizer::new(config);
opt.add_constraint(SymbolicConstraint::hard("lb", "x >= 1.0", 1.0));
let init = params(&["x"], &[5.0]);
let result = opt.optimize(init, &|p| {
let x = p.get("x").unwrap_or(0.0);
let grad = gradient_from(&["x"], &[2.0 * x]);
(x * x, grad)
});
let x_final = result.final_params.get("x").unwrap_or(0.0);
// Hard constraint forces x >= 1.0.
assert!(x_final >= 1.0 - 1e-9, "x_final={}", x_final);
assert_eq!(result.constraint_violations, 0);
}
#[test]
fn test_optimize_maximize() {
// Maximize -(x-3)^2 + 9 (peak at x=3).
let config = SnoOptimizerConfig::default()
.with_learning_rate(0.1)
.with_max_iterations(500)
.with_convergence_threshold(1e-8)
.with_objective(OptimizationObjective::Maximize);
let mut opt = SymbolicNeuralOptimizer::new(config);
let init = params(&["x"], &[0.0]);
let result = opt.optimize(init, &|p| {
let x = p.get("x").unwrap_or(0.0);
// For Maximize the optimizer negates internally, so loss_fn should
// return the value we want to maximise (not the negation).
let val = -(x - 3.0) * (x - 3.0) + 9.0;
// Gradient of val w.r.t. x = -2*(x-3)
let grad_val = -2.0 * (x - 3.0);
let grad = gradient_from(&["x"], &[grad_val]);
(val, grad)
});
assert!(result.success);
}
#[test]
fn test_optimize_satisfy_objective_type() {
let config = SnoOptimizerConfig::default()
.with_objective(OptimizationObjective::Satisfy(vec!["x_pos".to_string()]));
let mut opt = SymbolicNeuralOptimizer::new(config);
opt.add_constraint(SymbolicConstraint::soft("x_pos", "x >= 0.0", 1.0));
let init = params(&["x"], &[-2.0]);
let result = opt.optimize(init, &|p| {
let x = p.get("x").unwrap_or(0.0);
let grad = gradient_from(&["x"], &[0.0]);
(x.abs(), grad)
});
assert!(result.success);
}
#[test]
fn test_reset_clears_history() {
let mut opt = optimizer();
let p = params(&["x"], &[0.0]);
let g = gradient_from(&["x"], &[0.0]);
opt.step(&p, &g);
opt.reset();
assert!(opt.history().is_empty());
assert_eq!(opt.iteration(), 0);
}
#[test]
fn test_best_step_none_when_empty() {
let opt = optimizer();
assert!(opt.best_step().is_none());
}
#[test]
fn test_optimize_multi_param() {
// Minimize (x-1)^2 + (y+2)^2; analytic minimum: x=1, y=-2.
let config = SnoOptimizerConfig::default()
.with_learning_rate(0.05)
.with_max_iterations(1000)
.with_convergence_threshold(1e-9);
let mut opt = SymbolicNeuralOptimizer::new(config);
let init = params(&["x", "y"], &[5.0, 5.0]);
let result = opt.optimize(init, &|p| {
let x = p.get("x").unwrap_or(0.0);
let y = p.get("y").unwrap_or(0.0);
let loss = (x - 1.0) * (x - 1.0) + (y + 2.0) * (y + 2.0);
let gx = 2.0 * (x - 1.0);
let gy = 2.0 * (y + 2.0);
let grad = gradient_from(&["x", "y"], &[gx, gy]);
(loss, grad)
});
let x_f = result.final_params.get("x").unwrap_or(0.0);
let y_f = result.final_params.get("y").unwrap_or(0.0);
assert!((x_f - 1.0).abs() < 0.5, "x_f={}", x_f);
assert!((y_f + 2.0).abs() < 0.5, "y_f={}", y_f);
}
#[test]
fn test_optimizer_config_builder() {
let cfg = SnoOptimizerConfig::new()
.with_learning_rate(0.001)
.with_max_iterations(2000)
.with_convergence_threshold(1e-10)
.with_constraint_penalty(5.0)
.with_symbolic_correction_weight(0.3)
.with_objective(OptimizationObjective::Maximize);
assert!((cfg.learning_rate - 0.001).abs() < 1e-15);
assert_eq!(cfg.max_iterations, 2000);
assert!((cfg.convergence_threshold - 1e-10).abs() < 1e-20);
assert!((cfg.constraint_penalty - 5.0).abs() < 1e-12);
assert!((cfg.symbolic_correction_weight - 0.3).abs() < 1e-12);
assert_eq!(cfg.objective, OptimizationObjective::Maximize);
}
#[test]
fn test_xorshift64_not_stuck() {
let mut state: u64 = 12345;
let r1 = xorshift64(&mut state);
let r2 = xorshift64(&mut state);
let r3 = xorshift64(&mut state);
assert_ne!(r1, r2);
assert_ne!(r2, r3);
}
#[test]
fn test_xorshift64_zero_seed_skips() {
// xorshift64 with state=0 would be stuck; confirm non-zero seed works.
let mut state: u64 = 1;
let r = xorshift64(&mut state);
assert_ne!(r, 0);
}
#[test]
fn test_constraint_bound_eq_clamp() {
let mut opt =
SymbolicNeuralOptimizer::new(SnoOptimizerConfig::default().with_learning_rate(0.0));
opt.add_constraint(SymbolicConstraint::hard("fix", "w == 2.0", 1.0));
let p = params(&["w"], &[5.0]);
let g = gradient_from(&["w"], &[0.0]);
let new_p = opt.step(&p, &g);
assert!((new_p.get("w").unwrap_or(0.0) - 2.0).abs() < 1e-12);
}
#[test]
fn test_step_unknown_param_in_gradient() {
// Gradient has a name not in params — should default to 0.0 grad.
let mut opt = optimizer();
let p = params(&["x"], &[1.0]);
let g = gradient_from(&["z"], &[100.0]); // "z" not in params
let new_p = opt.step(&p, &g);
// No gradient applied for "x" since "z" doesn't match.
assert!((new_p.get("x").unwrap_or(0.0) - 1.0).abs() < 1e-12);
}
#[test]
fn test_history_loss_monotone_tendency() {
// For a simple convex problem without constraints the loss should
// generally decrease.
let config = SnoOptimizerConfig::default()
.with_learning_rate(0.05)
.with_max_iterations(50)
.with_convergence_threshold(1e-15);
let mut opt = SymbolicNeuralOptimizer::new(config);
let init = params(&["x"], &[10.0]);
opt.optimize(init, &|p| {
let x = p.get("x").unwrap_or(0.0);
let grad = gradient_from(&["x"], &[2.0 * x]);
(x * x, grad)
});
let first_loss = opt
.history()
.first()
.map(|s| s.loss)
.unwrap_or(f64::INFINITY);
let last_loss = opt.history().last().map(|s| s.loss).unwrap_or(0.0);
assert!(
last_loss <= first_loss,
"first={} last={}",
first_loss,
last_loss
);
}
#[test]
fn test_remove_constraint_removes_all_matching() {
let mut opt = optimizer();
opt.add_constraint(SymbolicConstraint::hard("c", "x >= 0.0", 1.0));
opt.add_constraint(SymbolicConstraint::hard("c", "x <= 5.0", 1.0));
opt.add_constraint(SymbolicConstraint::soft("d", "y <= 1.0", 0.5));
assert!(opt.remove_constraint("c"));
assert_eq!(opt.constraints().len(), 1);
assert_eq!(opt.constraints()[0].name, "d");
}
#[test]
fn test_sno_optimization_result_fields() {
let r = SnoOptimizationResult {
success: true,
iterations: 42,
final_loss: 0.001,
final_params: params(&["x"], &[1.0]),
constraint_violations: 0,
converged: true,
};
assert!(r.success);
assert_eq!(r.iterations, 42);
assert!(r.converged);
}
#[test]
fn test_sno_optimization_step_fields() {
let s = SnoOptimizationStep {
iteration: 7,
loss: std::f64::consts::PI,
gradient_norm: 0.5,
constraint_violations: 2,
params: params(&["a"], &[9.9]),
};
assert_eq!(s.iteration, 7);
assert_eq!(s.constraint_violations, 2);
}
}