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
//! Verification Utilities for Neuro-Symbolic Systems
//!
//! Provides fast plausibility checks and constraint validation
//! to avoid expensive SMT solver calls when possible.
use crate::ir::ConstraintIR;
use crate::surrogate::SolutionResult;
use anyhow::Result;
use serde::{Deserialize, Serialize};
/// Fast plausibility checker to validate solutions before SMT calls
pub struct PlausibilityChecker {
config: PlausibilityConfig,
}
/// Configuration for plausibility checking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlausibilityConfig {
/// Enable numeric range checks
pub check_numeric_ranges: bool,
/// Enable NaN/infinity checks
pub check_numeric_validity: bool,
/// Enable constraint satisfaction approximation
pub check_constraint_approximation: bool,
/// Tolerance for approximate constraint satisfaction
pub constraint_tolerance: f64,
}
impl Default for PlausibilityConfig {
fn default() -> Self {
Self {
check_numeric_ranges: true,
check_numeric_validity: true,
check_constraint_approximation: true,
constraint_tolerance: 1e-6,
}
}
}
/// Result of plausibility check
#[derive(Debug, Clone)]
pub struct PlausibilityResult {
/// Whether the solution passes plausibility checks
pub plausible: bool,
/// Confidence in plausibility assessment
pub confidence: f32,
/// Specific violations found
pub violations: Vec<PlausibilityViolation>,
}
/// Specific plausibility violation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlausibilityViolation {
/// Type of violation
pub violation_type: ViolationType,
/// Description of the violation
pub description: String,
/// Severity (0.0 = minor, 1.0 = major)
pub severity: f32,
}
/// Types of plausibility violations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ViolationType {
/// Numeric value out of expected range
NumericRange,
/// Invalid numeric value (NaN, infinity)
NumericInvalid,
/// Constraint approximately violated
ConstraintViolation,
/// Type mismatch
TypeMismatch,
/// Missing required variable
MissingVariable,
}
impl PlausibilityChecker {
pub fn new(config: PlausibilityConfig) -> Self {
Self { config }
}
/// Check if a solution is plausible given the constraints
pub fn check_plausibility(
&self,
constraints: &ConstraintIR,
solution: &SolutionResult,
) -> Result<PlausibilityResult> {
let mut violations = Vec::new();
if !solution.satisfiable {
// If solution claims unsatisfiable, always accept for now
// (Could add unsat core validation later)
return Ok(PlausibilityResult {
plausible: true,
confidence: 1.0,
violations,
});
}
// Check variable assignments against domains
if self.config.check_numeric_ranges {
self.check_variable_domains(constraints, solution, &mut violations)?;
}
// Check for invalid numeric values
if self.config.check_numeric_validity {
self.check_numeric_validity(solution, &mut violations)?;
}
// Approximately check constraint satisfaction
if self.config.check_constraint_approximation {
self.check_constraint_satisfaction(constraints, solution, &mut violations)?;
}
// Compute overall plausibility
let plausible = violations.is_empty() || violations.iter().all(|v| v.severity < 0.5);
let confidence = if violations.is_empty() {
0.95
} else {
let max_severity = violations.iter().map(|v| v.severity).fold(0.0, f32::max);
(1.0 - max_severity).max(0.1)
};
Ok(PlausibilityResult {
plausible,
confidence,
violations,
})
}
/// Check variable assignments against their domains
fn check_variable_domains(
&self,
constraints: &ConstraintIR,
solution: &SolutionResult,
violations: &mut Vec<PlausibilityViolation>,
) -> Result<()> {
for (var_id, variable) in &constraints.variables {
let var_name = format!("v{}", var_id.0);
if let Some(value) = solution.assignment.get(&var_name) {
match (&variable.domain, value) {
(
crate::ir::Domain::Real { min, max },
crate::surrogate::SolutionValue::Real(val),
) => {
if let Some(min_val) = min {
if val < min_val {
violations.push(PlausibilityViolation {
violation_type: ViolationType::NumericRange,
description: format!(
"Variable {} value {} below minimum {}",
var_name, val, min_val
),
severity: 0.8,
});
}
}
if let Some(max_val) = max {
if val > max_val {
violations.push(PlausibilityViolation {
violation_type: ViolationType::NumericRange,
description: format!(
"Variable {} value {} above maximum {}",
var_name, val, max_val
),
severity: 0.8,
});
}
}
}
(
crate::ir::Domain::Integer { min, max },
crate::surrogate::SolutionValue::Integer(val),
) => {
if let Some(min_val) = min {
if val < min_val {
violations.push(PlausibilityViolation {
violation_type: ViolationType::NumericRange,
description: format!(
"Variable {} value {} below minimum {}",
var_name, val, min_val
),
severity: 0.9,
});
}
}
if let Some(max_val) = max {
if val > max_val {
violations.push(PlausibilityViolation {
violation_type: ViolationType::NumericRange,
description: format!(
"Variable {} value {} above maximum {}",
var_name, val, max_val
),
severity: 0.9,
});
}
}
}
_ => {
// Type mismatch or unsupported domain type
violations.push(PlausibilityViolation {
violation_type: ViolationType::TypeMismatch,
description: format!(
"Variable {} has type mismatch between domain and assignment",
var_name
),
severity: 0.7,
});
}
}
} else {
// Missing variable assignment
violations.push(PlausibilityViolation {
violation_type: ViolationType::MissingVariable,
description: format!("Missing assignment for variable {}", var_name),
severity: 0.9,
});
}
}
Ok(())
}
/// Check for invalid numeric values (NaN, infinity)
fn check_numeric_validity(
&self,
solution: &SolutionResult,
violations: &mut Vec<PlausibilityViolation>,
) -> Result<()> {
for (var_name, value) in &solution.assignment {
match value {
crate::surrogate::SolutionValue::Real(val) => {
if val.is_nan() {
violations.push(PlausibilityViolation {
violation_type: ViolationType::NumericInvalid,
description: format!("Variable {} has NaN value", var_name),
severity: 1.0,
});
} else if val.is_infinite() {
violations.push(PlausibilityViolation {
violation_type: ViolationType::NumericInvalid,
description: format!("Variable {} has infinite value", var_name),
severity: 0.9,
});
}
}
_ => {}
}
}
Ok(())
}
/// Approximately check constraint satisfaction
fn check_constraint_satisfaction(
&self,
constraints: &ConstraintIR,
solution: &SolutionResult,
violations: &mut Vec<PlausibilityViolation>,
) -> Result<()> {
// Check each constraint against the solution
for (i, constraint) in constraints.constraints.iter().enumerate() {
match constraint {
crate::ir::Constraint::LessEqual { lhs, rhs } => {
// Evaluate both sides and check satisfaction
if let (Some(lhs_val), Some(rhs_val)) = (
self.evaluate_expr_approx(lhs, solution),
self.evaluate_expr_approx(rhs, solution),
) {
let tolerance = 1e-6;
if lhs_val > rhs_val + tolerance {
violations.push(PlausibilityViolation {
violation_type: ViolationType::ConstraintViolation,
description: format!(
"LessEqual constraint {} violated: {} > {} (difference: {})",
i,
lhs_val,
rhs_val,
lhs_val - rhs_val
),
severity: 0.9, // High severity
});
}
}
}
crate::ir::Constraint::Equal { lhs, rhs } => {
// Check equality within tolerance
if let (Some(lhs_val), Some(rhs_val)) = (
self.evaluate_expr_approx(lhs, solution),
self.evaluate_expr_approx(rhs, solution),
) {
let tolerance = self.config.constraint_tolerance;
if (lhs_val - rhs_val).abs() > tolerance {
violations.push(PlausibilityViolation {
violation_type: ViolationType::ConstraintViolation,
description: format!(
"Equality constraint {} violated: {} ≠ {} (difference: {})",
i,
lhs_val,
rhs_val,
(lhs_val - rhs_val).abs()
),
severity: 0.7, // Medium severity
});
}
}
}
_ => {
// Other constraint types not yet implemented
}
}
}
Ok(())
}
/// Approximately evaluate an expression given a solution
fn evaluate_expr_approx(
&self,
expr: &crate::ir::Expr,
solution: &SolutionResult,
) -> Option<f64> {
use crate::ir::Expr;
match expr {
Expr::Var(var_id) => {
// Look up variable value in solution (convert VarId to String)
let var_key = var_id.to_string();
solution.assignment.get(&var_key).and_then(|val| match val {
crate::surrogate::SolutionValue::Real(r) => Some(*r),
crate::surrogate::SolutionValue::Integer(i) => Some(*i as f64),
crate::surrogate::SolutionValue::Bool(b) => Some(if *b { 1.0 } else { 0.0 }),
_ => None, // String and BitVector not convertible to f64
})
}
Expr::Const(val) => match val {
crate::ir::ConstValue::Real(r) => Some(*r),
crate::ir::ConstValue::Integer(i) => Some(*i as f64),
crate::ir::ConstValue::Bool(b) => Some(if *b { 1.0 } else { 0.0 }),
crate::ir::ConstValue::String(_) => None,
crate::ir::ConstValue::BitVector { .. } => None,
},
Expr::Binary { op, lhs, rhs } => {
let lhs_val = self.evaluate_expr_approx(lhs, solution)?;
let rhs_val = self.evaluate_expr_approx(rhs, solution)?;
match op {
crate::ir::BinaryOp::Add => Some(lhs_val + rhs_val),
crate::ir::BinaryOp::Sub => Some(lhs_val - rhs_val),
crate::ir::BinaryOp::Mul => Some(lhs_val * rhs_val),
crate::ir::BinaryOp::Div => {
if rhs_val.abs() > 1e-10 {
Some(lhs_val / rhs_val)
} else {
None // Division by zero
}
}
_ => None, // Other operators not implemented
}
}
_ => None, // Other expression types not implemented
}
}
}
impl Default for PlausibilityChecker {
fn default() -> Self {
Self::new(PlausibilityConfig::default())
}
}