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
//! Core types for kernel operations.
use serde::{Deserialize, Serialize};
use crate::error::Result;
/// Trait for kernel functions that compute similarity between inputs.
///
/// Kernels map pairs of inputs to scalar similarity values.
pub trait Kernel: Send + Sync {
/// Compute kernel value between two inputs.
///
/// # Arguments
/// * `x` - First input (feature representation)
/// * `y` - Second input (feature representation)
///
/// # Returns
/// Similarity score (typically in range [0, 1] or [-1, 1])
fn compute(&self, x: &[f64], y: &[f64]) -> Result<f64>;
/// Compute kernel matrix for a set of inputs.
///
/// # Arguments
/// * `inputs` - Slice of input feature vectors
///
/// # Returns
/// Kernel matrix K where `K[i,j] = kernel(inputs[i], inputs[j])`
fn compute_matrix(&self, inputs: &[Vec<f64>]) -> Result<Vec<Vec<f64>>> {
let n = inputs.len();
let mut matrix = vec![vec![0.0; n]; n];
for i in 0..n {
for j in 0..n {
matrix[i][j] = self.compute(&inputs[i], &inputs[j])?;
}
}
Ok(matrix)
}
/// Get kernel name for identification.
fn name(&self) -> &str;
/// Check if kernel is positive semi-definite.
fn is_psd(&self) -> bool {
true // Most kernels are PSD by construction
}
}
/// Configuration for rule-based similarity kernel.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct RuleSimilarityConfig {
/// Weight for satisfied rules (both inputs satisfy)
pub satisfied_weight: f64,
/// Weight for violated rules (both inputs violate)
pub violated_weight: f64,
/// Weight for mixed cases (one satisfies, one violates)
pub mixed_weight: f64,
/// Normalization strategy
pub normalize: bool,
}
impl RuleSimilarityConfig {
/// Create default configuration
pub fn new() -> Self {
Self {
satisfied_weight: 1.0,
violated_weight: 0.5,
mixed_weight: 0.0,
normalize: true,
}
}
/// Set satisfied weight
pub fn with_satisfied_weight(mut self, weight: f64) -> Self {
self.satisfied_weight = weight;
self
}
/// Set violated weight
pub fn with_violated_weight(mut self, weight: f64) -> Self {
self.violated_weight = weight;
self
}
/// Set mixed weight
pub fn with_mixed_weight(mut self, weight: f64) -> Self {
self.mixed_weight = weight;
self
}
/// Set normalization flag
pub fn with_normalize(mut self, normalize: bool) -> Self {
self.normalize = normalize;
self
}
}
impl Default for RuleSimilarityConfig {
fn default() -> Self {
Self::new()
}
}
/// Configuration for predicate overlap kernel.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct PredicateOverlapConfig {
/// Weight for exact predicate matches
pub exact_match_weight: f64,
/// Weight for predicate co-occurrence
pub cooccurrence_weight: f64,
/// Minimum overlap threshold
pub min_overlap: f64,
}
impl PredicateOverlapConfig {
/// Create default configuration
pub fn new() -> Self {
Self {
exact_match_weight: 1.0,
cooccurrence_weight: 0.5,
min_overlap: 0.0,
}
}
/// Set exact match weight
pub fn with_exact_match_weight(mut self, weight: f64) -> Self {
self.exact_match_weight = weight;
self
}
/// Set co-occurrence weight
pub fn with_cooccurrence_weight(mut self, weight: f64) -> Self {
self.cooccurrence_weight = weight;
self
}
/// Set minimum overlap threshold
pub fn with_min_overlap(mut self, threshold: f64) -> Self {
self.min_overlap = threshold;
self
}
}
impl Default for PredicateOverlapConfig {
fn default() -> Self {
Self::new()
}
}
/// Configuration for RBF (Gaussian) kernel.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct RbfKernelConfig {
/// Bandwidth parameter (gamma = 1 / (2 * sigma^2))
pub gamma: f64,
}
impl RbfKernelConfig {
/// Create configuration with specified gamma
pub fn new(gamma: f64) -> Self {
Self { gamma }
}
/// Create from bandwidth (sigma)
pub fn from_sigma(sigma: f64) -> Self {
Self {
gamma: 1.0 / (2.0 * sigma * sigma),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_rule_similarity_config() {
let config = RuleSimilarityConfig::new()
.with_satisfied_weight(2.0)
.with_violated_weight(1.0)
.with_mixed_weight(0.5);
assert_eq!(config.satisfied_weight, 2.0);
assert_eq!(config.violated_weight, 1.0);
assert_eq!(config.mixed_weight, 0.5);
assert!(config.normalize);
}
#[test]
fn test_predicate_overlap_config() {
let config = PredicateOverlapConfig::new()
.with_exact_match_weight(1.5)
.with_cooccurrence_weight(0.8);
assert_eq!(config.exact_match_weight, 1.5);
assert_eq!(config.cooccurrence_weight, 0.8);
}
#[test]
fn test_rbf_kernel_config() {
let config = RbfKernelConfig::new(0.5);
assert_eq!(config.gamma, 0.5);
let config = RbfKernelConfig::from_sigma(2.0);
assert!((config.gamma - 0.125).abs() < 1e-10);
}
}