llm-optimizer-decision 0.1.1

Intelligent decision-making engine
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
//! Contextual bandit algorithms for adaptive model selection
//!
//! This module implements LinUCB (Linear Upper Confidence Bound) and
//! Contextual Thompson Sampling for context-aware variant selection.

use crate::context::RequestContext;
use crate::errors::{DecisionError, Result};
use rand_distr::{Distribution, Normal};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use uuid::Uuid;

/// Linear Upper Confidence Bound (LinUCB) arm
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LinUCBArm {
    /// Arm identifier (variant ID)
    pub arm_id: Uuid,
    /// Design matrix A = D^T D + I
    /// Where D is the matrix of observed contexts
    pub a_matrix: Vec<Vec<f64>>,
    /// b vector = D^T c where c is the vector of rewards
    pub b_vector: Vec<f64>,
    /// Dimension of context features
    pub d: usize,
    /// Number of times this arm was selected
    pub num_selections: u64,
    /// Total reward received
    pub total_reward: f64,
}

impl LinUCBArm {
    /// Create a new LinUCB arm with given feature dimension
    pub fn new(arm_id: Uuid, dimension: usize) -> Self {
        // Initialize A as identity matrix
        let mut a_matrix = vec![vec![0.0; dimension]; dimension];
        for i in 0..dimension {
            a_matrix[i][i] = 1.0;
        }

        Self {
            arm_id,
            a_matrix,
            b_vector: vec![0.0; dimension],
            d: dimension,
            num_selections: 0,
            total_reward: 0.0,
        }
    }

    /// Calculate UCB score for given context
    pub fn calculate_ucb(&self, context: &[f64], alpha: f64) -> Result<f64> {
        if context.len() != self.d {
            return Err(DecisionError::InvalidConfig(format!(
                "Context dimension {} doesn't match arm dimension {}",
                context.len(),
                self.d
            )));
        }

        // Calculate A^{-1}
        let a_inv = self.invert_matrix(&self.a_matrix)?;

        // theta = A^{-1} * b
        let theta = matrix_vector_multiply(&a_inv, &self.b_vector);

        // Expected reward: theta^T * x
        let expected_reward = dot_product(&theta, context);

        // Uncertainty: sqrt(x^T * A^{-1} * x)
        let a_inv_x = matrix_vector_multiply(&a_inv, context);
        let uncertainty = dot_product(context, &a_inv_x).sqrt();

        // UCB = expected_reward + alpha * uncertainty
        Ok(expected_reward + alpha * uncertainty)
    }

    /// Update arm with observed context and reward
    pub fn update(&mut self, context: &[f64], reward: f64) -> Result<()> {
        if context.len() != self.d {
            return Err(DecisionError::InvalidConfig(format!(
                "Context dimension {} doesn't match arm dimension {}",
                context.len(),
                self.d
            )));
        }

        // A = A + x * x^T
        for i in 0..self.d {
            for j in 0..self.d {
                self.a_matrix[i][j] += context[i] * context[j];
            }
        }

        // b = b + reward * x
        for i in 0..self.d {
            self.b_vector[i] += reward * context[i];
        }

        self.num_selections += 1;
        self.total_reward += reward;

        Ok(())
    }

    /// Get average reward
    pub fn average_reward(&self) -> f64 {
        if self.num_selections == 0 {
            0.0
        } else {
            self.total_reward / self.num_selections as f64
        }
    }

    /// Invert matrix using Gauss-Jordan elimination
    fn invert_matrix(&self, matrix: &[Vec<f64>]) -> Result<Vec<Vec<f64>>> {
        let n = matrix.len();
        let mut aug = vec![vec![0.0; 2 * n]; n];

        // Create augmented matrix [A | I]
        for i in 0..n {
            for j in 0..n {
                aug[i][j] = matrix[i][j];
            }
            aug[i][i + n] = 1.0;
        }

        // Forward elimination
        for i in 0..n {
            // Find pivot
            let mut max_row = i;
            for k in i + 1..n {
                if aug[k][i].abs() > aug[max_row][i].abs() {
                    max_row = k;
                }
            }

            // Swap rows
            aug.swap(i, max_row);

            // Check for singular matrix
            if aug[i][i].abs() < 1e-10 {
                return Err(DecisionError::StatisticalError(
                    "Matrix is singular or nearly singular".to_string(),
                ));
            }

            // Scale pivot row
            let pivot = aug[i][i];
            for j in 0..2 * n {
                aug[i][j] /= pivot;
            }

            // Eliminate column
            for k in 0..n {
                if k != i {
                    let factor = aug[k][i];
                    for j in 0..2 * n {
                        aug[k][j] -= factor * aug[i][j];
                    }
                }
            }
        }

        // Extract inverse from augmented matrix
        let mut inv = vec![vec![0.0; n]; n];
        for i in 0..n {
            for j in 0..n {
                inv[i][j] = aug[i][j + n];
            }
        }

        Ok(inv)
    }
}

/// LinUCB (Linear Upper Confidence Bound) contextual bandit
#[derive(Debug, Clone)]
pub struct LinUCB {
    /// Arms in the bandit
    arms: HashMap<Uuid, LinUCBArm>,
    /// Exploration parameter (typically 0.1 to 2.0)
    alpha: f64,
    /// Feature dimension
    dimension: usize,
}

impl LinUCB {
    /// Create a new LinUCB bandit
    pub fn new(alpha: f64, dimension: usize) -> Self {
        Self {
            arms: HashMap::new(),
            alpha,
            dimension,
        }
    }

    /// Add a new arm
    pub fn add_arm(&mut self, arm_id: Uuid) {
        self.arms.insert(arm_id, LinUCBArm::new(arm_id, self.dimension));
    }

    /// Remove an arm
    pub fn remove_arm(&mut self, arm_id: &Uuid) {
        self.arms.remove(arm_id);
    }

    /// Select best arm for given context
    pub fn select_arm(&self, context: &RequestContext) -> Result<Uuid> {
        if self.arms.is_empty() {
            return Err(DecisionError::InvalidState(
                "No arms available for selection".to_string(),
            ));
        }

        let features = context.to_feature_vector();

        let mut best_arm = None;
        let mut best_ucb = f64::NEG_INFINITY;

        for (arm_id, arm) in &self.arms {
            let ucb = arm.calculate_ucb(&features, self.alpha)?;
            if ucb > best_ucb {
                best_ucb = ucb;
                best_arm = Some(*arm_id);
            }
        }

        best_arm.ok_or_else(|| DecisionError::AllocationError("Failed to select arm".to_string()))
    }

    /// Update arm with observed reward
    pub fn update(&mut self, arm_id: &Uuid, context: &RequestContext, reward: f64) -> Result<()> {
        let arm = self
            .arms
            .get_mut(arm_id)
            .ok_or_else(|| DecisionError::VariantNotFound(arm_id.to_string()))?;

        let features = context.to_feature_vector();
        arm.update(&features, reward)
    }

    /// Get arm statistics
    pub fn get_arm(&self, arm_id: &Uuid) -> Option<&LinUCBArm> {
        self.arms.get(arm_id)
    }

    /// Get all arms
    pub fn get_arms(&self) -> &HashMap<Uuid, LinUCBArm> {
        &self.arms
    }

    /// Get average rewards for all arms
    pub fn get_average_rewards(&self) -> HashMap<Uuid, f64> {
        self.arms
            .iter()
            .map(|(id, arm)| (*id, arm.average_reward()))
            .collect()
    }

    /// Set exploration parameter
    pub fn set_alpha(&mut self, alpha: f64) {
        self.alpha = alpha;
    }

    /// Get exploration parameter
    pub fn alpha(&self) -> f64 {
        self.alpha
    }
}

/// Contextual Thompson Sampling arm
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContextualThompsonArm {
    /// Arm identifier
    pub arm_id: Uuid,
    /// Weight mean (theta)
    pub theta_mean: Vec<f64>,
    /// Weight covariance (simplified: diagonal only for efficiency)
    pub theta_variance: Vec<f64>,
    /// Feature dimension
    pub d: usize,
    /// Number of selections
    pub num_selections: u64,
    /// Total reward
    pub total_reward: f64,
    /// Learning rate
    pub learning_rate: f64,
}

impl ContextualThompsonArm {
    /// Create a new contextual Thompson sampling arm
    pub fn new(arm_id: Uuid, dimension: usize) -> Self {
        Self {
            arm_id,
            theta_mean: vec![0.0; dimension],
            theta_variance: vec![1.0; dimension],
            d: dimension,
            num_selections: 0,
            total_reward: 0.0,
            learning_rate: 0.1,
        }
    }

    /// Sample theta from posterior
    pub fn sample_theta(&self) -> Result<Vec<f64>> {
        let mut rng = rand::thread_rng();
        let mut theta = Vec::with_capacity(self.d);

        for i in 0..self.d {
            let normal = Normal::new(self.theta_mean[i], self.theta_variance[i].sqrt())
                .map_err(|e| DecisionError::StatisticalError(e.to_string()))?;
            theta.push(normal.sample(&mut rng));
        }

        Ok(theta)
    }

    /// Calculate expected reward for context
    pub fn expected_reward(&self, context: &[f64]) -> f64 {
        dot_product(&self.theta_mean, context)
    }

    /// Update arm with context and reward (Bayesian update)
    pub fn update(&mut self, context: &[f64], reward: f64) -> Result<()> {
        if context.len() != self.d {
            return Err(DecisionError::InvalidConfig(format!(
                "Context dimension {} doesn't match arm dimension {}",
                context.len(),
                self.d
            )));
        }

        let predicted = self.expected_reward(context);
        let error = reward - predicted;

        // Update theta mean (gradient descent-style update)
        for i in 0..self.d {
            self.theta_mean[i] += self.learning_rate * error * context[i];
        }

        // Update variance (decrease with more observations)
        for i in 0..self.d {
            self.theta_variance[i] *= 0.99; // Gradual decay
            self.theta_variance[i] = self.theta_variance[i].max(0.01); // Lower bound
        }

        self.num_selections += 1;
        self.total_reward += reward;

        Ok(())
    }

    /// Get average reward
    pub fn average_reward(&self) -> f64 {
        if self.num_selections == 0 {
            0.0
        } else {
            self.total_reward / self.num_selections as f64
        }
    }
}

/// Contextual Thompson Sampling bandit
#[derive(Debug, Clone)]
pub struct ContextualThompson {
    /// Arms in the bandit
    arms: HashMap<Uuid, ContextualThompsonArm>,
    /// Feature dimension
    dimension: usize,
}

impl ContextualThompson {
    /// Create a new contextual Thompson sampling bandit
    pub fn new(dimension: usize) -> Self {
        Self {
            arms: HashMap::new(),
            dimension,
        }
    }

    /// Add a new arm
    pub fn add_arm(&mut self, arm_id: Uuid) {
        self.arms
            .insert(arm_id, ContextualThompsonArm::new(arm_id, self.dimension));
    }

    /// Remove an arm
    pub fn remove_arm(&mut self, arm_id: &Uuid) {
        self.arms.remove(arm_id);
    }

    /// Select arm using Thompson sampling
    pub fn select_arm(&self, context: &RequestContext) -> Result<Uuid> {
        if self.arms.is_empty() {
            return Err(DecisionError::InvalidState(
                "No arms available for selection".to_string(),
            ));
        }

        let features = context.to_feature_vector();

        let mut best_arm = None;
        let mut best_reward = f64::NEG_INFINITY;

        for (arm_id, arm) in &self.arms {
            let theta = arm.sample_theta()?;
            let reward = dot_product(&theta, &features);

            if reward > best_reward {
                best_reward = reward;
                best_arm = Some(*arm_id);
            }
        }

        best_arm.ok_or_else(|| DecisionError::AllocationError("Failed to select arm".to_string()))
    }

    /// Update arm with observed reward
    pub fn update(&mut self, arm_id: &Uuid, context: &RequestContext, reward: f64) -> Result<()> {
        let arm = self
            .arms
            .get_mut(arm_id)
            .ok_or_else(|| DecisionError::VariantNotFound(arm_id.to_string()))?;

        let features = context.to_feature_vector();
        arm.update(&features, reward)
    }

    /// Get arm statistics
    pub fn get_arm(&self, arm_id: &Uuid) -> Option<&ContextualThompsonArm> {
        self.arms.get(arm_id)
    }

    /// Get all arms
    pub fn get_arms(&self) -> &HashMap<Uuid, ContextualThompsonArm> {
        &self.arms
    }

    /// Get average rewards for all arms
    pub fn get_average_rewards(&self) -> HashMap<Uuid, f64> {
        self.arms
            .iter()
            .map(|(id, arm)| (*id, arm.average_reward()))
            .collect()
    }
}

// Helper functions

fn dot_product(a: &[f64], b: &[f64]) -> f64 {
    a.iter().zip(b.iter()).map(|(x, y)| x * y).sum()
}

fn matrix_vector_multiply(matrix: &[Vec<f64>], vector: &[f64]) -> Vec<f64> {
    matrix.iter().map(|row| dot_product(row, vector)).collect()
}

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

    #[test]
    fn test_linucb_arm_creation() {
        let arm = LinUCBArm::new(Uuid::new_v4(), 5);
        assert_eq!(arm.d, 5);
        assert_eq!(arm.num_selections, 0);
        assert_eq!(arm.total_reward, 0.0);
    }

    #[test]
    fn test_linucb_arm_update() {
        let mut arm = LinUCBArm::new(Uuid::new_v4(), 3);
        let context = vec![1.0, 0.5, 0.2];
        let reward = 0.8;

        arm.update(&context, reward).unwrap();
        assert_eq!(arm.num_selections, 1);
        assert_eq!(arm.total_reward, reward);
    }

    #[test]
    fn test_linucb_creation() {
        let bandit = LinUCB::new(1.0, 10);
        assert_eq!(bandit.alpha, 1.0);
        assert_eq!(bandit.dimension, 10);
        assert_eq!(bandit.arms.len(), 0);
    }

    #[test]
    fn test_linucb_add_remove_arm() {
        let mut bandit = LinUCB::new(1.0, 5);
        let arm_id = Uuid::new_v4();

        bandit.add_arm(arm_id);
        assert_eq!(bandit.arms.len(), 1);

        bandit.remove_arm(&arm_id);
        assert_eq!(bandit.arms.len(), 0);
    }

    #[test]
    fn test_linucb_select_arm() {
        let mut bandit = LinUCB::new(1.0, RequestContext::feature_dimension());

        let arm1 = Uuid::new_v4();
        let arm2 = Uuid::new_v4();

        bandit.add_arm(arm1);
        bandit.add_arm(arm2);

        let context = RequestContext::new(100);
        let selected = bandit.select_arm(&context).unwrap();

        assert!(selected == arm1 || selected == arm2);
    }

    #[test]
    fn test_linucb_update_and_select() {
        let mut bandit = LinUCB::new(0.5, RequestContext::feature_dimension());

        let arm1 = Uuid::new_v4();
        let arm2 = Uuid::new_v4();

        bandit.add_arm(arm1);
        bandit.add_arm(arm2);

        // Simulate arm1 being better
        for _ in 0..10 {
            let context = RequestContext::new(100);
            bandit.update(&arm1, &context, 0.9).unwrap();
            bandit.update(&arm2, &context, 0.3).unwrap();
        }

        // After updates, should prefer arm1
        let rewards = bandit.get_average_rewards();
        assert!(rewards[&arm1] > rewards[&arm2]);
    }

    #[test]
    fn test_contextual_thompson_arm_creation() {
        let arm = ContextualThompsonArm::new(Uuid::new_v4(), 5);
        assert_eq!(arm.d, 5);
        assert_eq!(arm.num_selections, 0);
    }

    #[test]
    fn test_contextual_thompson_sample_theta() {
        let arm = ContextualThompsonArm::new(Uuid::new_v4(), 5);
        let theta = arm.sample_theta().unwrap();
        assert_eq!(theta.len(), 5);
    }

    #[test]
    fn test_contextual_thompson_update() {
        let mut arm = ContextualThompsonArm::new(Uuid::new_v4(), 3);
        let context = vec![1.0, 0.5, 0.2];

        arm.update(&context, 0.8).unwrap();
        assert_eq!(arm.num_selections, 1);
    }

    #[test]
    fn test_contextual_thompson_creation() {
        let bandit = ContextualThompson::new(10);
        assert_eq!(bandit.dimension, 10);
    }

    #[test]
    fn test_contextual_thompson_select() {
        let mut bandit = ContextualThompson::new(RequestContext::feature_dimension());

        let arm1 = Uuid::new_v4();
        let arm2 = Uuid::new_v4();

        bandit.add_arm(arm1);
        bandit.add_arm(arm2);

        let context = RequestContext::new(100);
        let selected = bandit.select_arm(&context).unwrap();

        assert!(selected == arm1 || selected == arm2);
    }

    #[test]
    fn test_contextual_thompson_convergence() {
        let mut bandit = ContextualThompson::new(RequestContext::feature_dimension());

        let good_arm = Uuid::new_v4();
        let bad_arm = Uuid::new_v4();

        bandit.add_arm(good_arm);
        bandit.add_arm(bad_arm);

        // Train: good_arm gets high rewards, bad_arm gets low rewards
        for _ in 0..50 {
            let context = RequestContext::new(100);
            bandit.update(&good_arm, &context, 0.9).unwrap();
            bandit.update(&bad_arm, &context, 0.2).unwrap();
        }

        let rewards = bandit.get_average_rewards();
        assert!(rewards[&good_arm] > rewards[&bad_arm]);
    }

    #[test]
    fn test_dot_product() {
        let a = vec![1.0, 2.0, 3.0];
        let b = vec![4.0, 5.0, 6.0];
        let result = dot_product(&a, &b);
        assert_eq!(result, 32.0);
    }

    #[test]
    fn test_matrix_vector_multiply() {
        let matrix = vec![vec![1.0, 2.0], vec![3.0, 4.0]];
        let vector = vec![5.0, 6.0];
        let result = matrix_vector_multiply(&matrix, &vector);
        assert_eq!(result, vec![17.0, 39.0]);
    }

    #[test]
    fn test_matrix_inversion() {
        let arm = LinUCBArm::new(Uuid::new_v4(), 2);
        let matrix = vec![vec![4.0, 3.0], vec![3.0, 2.0]];

        let inv = arm.invert_matrix(&matrix).unwrap();

        // A * A^{-1} should be approximately I
        for i in 0..2 {
            for j in 0..2 {
                let mut sum = 0.0;
                for k in 0..2 {
                    sum += matrix[i][k] * inv[k][j];
                }
                let expected = if i == j { 1.0 } else { 0.0 };
                assert!((sum - expected).abs() < 1e-10);
            }
        }
    }
}