quantrs2-tytan 0.1.3

High-level quantum annealing interface inspired by Tytan for the QuantRS2 framework
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
712
//! Multi-Objective QUBO Optimization
//!
//! This module implements advanced multi-objective optimization algorithms for QUBO problems,
//! allowing users to optimize multiple conflicting objectives simultaneously and explore
//! Pareto frontiers.
//!
//! # Features
//!
//! - Pareto frontier computation
//! - Multiple scalarization methods (weighted sum, epsilon-constraint, Tchebycheff)
//! - Evolutionary multi-objective algorithms (NSGA-II, MOEA/D)
//! - Interactive objective exploration
//! - Trade-off visualization and analysis
//!
//! # Examples
//!
//! ```rust
//! use quantrs2_tytan::multi_objective_optimization::*;
//! use std::collections::HashMap;
//!
//! // Define multiple objectives
//! let objectives = vec![
//!     Objective::new("cost", ObjectiveDirection::Minimize, 1.0),
//!     Objective::new("quality", ObjectiveDirection::Maximize, 1.0),
//! ];
//!
//! // Create multi-objective optimizer
//! let config = MultiObjectiveConfig::default();
//! let mut optimizer = MultiObjectiveOptimizer::new(objectives, config);
//! ```

use crate::sampler::{SampleResult, Sampler};
use quantrs2_anneal::QuboModel;
use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::parallel_ops;
use scirs2_core::random::prelude::*;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;

/// Objective direction
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ObjectiveDirection {
    /// Minimize the objective
    Minimize,
    /// Maximize the objective
    Maximize,
}

/// Objective definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Objective {
    /// Objective name
    pub name: String,
    /// Optimization direction
    pub direction: ObjectiveDirection,
    /// Weight for scalarization
    pub weight: f64,
    /// QUBO matrix for this objective
    pub qubo_matrix: Option<Array2<f64>>,
}

impl Objective {
    /// Create a new objective
    pub fn new(name: impl Into<String>, direction: ObjectiveDirection, weight: f64) -> Self {
        Self {
            name: name.into(),
            direction,
            weight,
            qubo_matrix: None,
        }
    }

    /// Set QUBO matrix for this objective
    pub fn with_qubo(mut self, qubo_matrix: Array2<f64>) -> Self {
        self.qubo_matrix = Some(qubo_matrix);
        self
    }

    /// Evaluate this objective for a given solution
    pub fn evaluate(&self, solution: &HashMap<String, bool>) -> f64 {
        if let Some(ref matrix) = self.qubo_matrix {
            // Compute QUBO energy
            let n = matrix.nrows();
            let mut energy = 0.0;

            for i in 0..n {
                for j in 0..n {
                    let x_i = if solution.get(&format!("x{i}")).copied().unwrap_or(false) {
                        1.0
                    } else {
                        0.0
                    };
                    let x_j = if solution.get(&format!("x{j}")).copied().unwrap_or(false) {
                        1.0
                    } else {
                        0.0
                    };
                    energy += matrix[[i, j]] * x_i * x_j;
                }
            }

            match self.direction {
                ObjectiveDirection::Minimize => energy,
                ObjectiveDirection::Maximize => -energy,
            }
        } else {
            0.0
        }
    }
}

/// A solution in objective space
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultiObjectiveSolution {
    /// Variable assignments
    pub assignments: HashMap<String, bool>,
    /// Objective values
    pub objective_values: Vec<f64>,
    /// Dominance rank (lower is better)
    pub rank: usize,
    /// Crowding distance (higher is better for diversity)
    pub crowding_distance: f64,
}

impl MultiObjectiveSolution {
    /// Check if this solution dominates another
    pub fn dominates(&self, other: &Self) -> bool {
        let mut at_least_one_better = false;

        for (a, b) in self.objective_values.iter().zip(&other.objective_values) {
            if a > b {
                return false; // Worse in at least one objective (assuming minimization)
            }
            if a < b {
                at_least_one_better = true;
            }
        }

        at_least_one_better
    }

    /// Compute Euclidean distance in objective space
    pub fn distance_to(&self, other: &Self) -> f64 {
        self.objective_values
            .iter()
            .zip(&other.objective_values)
            .map(|(a, b)| (a - b).powi(2))
            .sum::<f64>()
            .sqrt()
    }
}

/// Scalarization method for multi-objective optimization
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ScalarizationMethod {
    /// Weighted sum of objectives
    WeightedSum,
    /// Epsilon-constraint method
    EpsilonConstraint,
    /// Tchebycheff method
    Tchebycheff,
    /// Augmented Tchebycheff
    AugmentedTchebycheff,
    /// Achievement scalarizing function
    AchievementFunction,
}

/// Multi-objective optimization configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultiObjectiveConfig {
    /// Population size for evolutionary algorithms
    pub population_size: usize,
    /// Number of generations
    pub max_generations: usize,
    /// Scalarization method
    pub scalarization: ScalarizationMethod,
    /// Crossover probability
    pub crossover_prob: f64,
    /// Mutation probability
    pub mutation_prob: f64,
    /// Number of reference points for decomposition
    pub num_reference_points: usize,
    /// Enable parallel evaluation
    pub parallel: bool,
}

impl Default for MultiObjectiveConfig {
    fn default() -> Self {
        Self {
            population_size: 100,
            max_generations: 100,
            scalarization: ScalarizationMethod::WeightedSum,
            crossover_prob: 0.9,
            mutation_prob: 0.1,
            num_reference_points: 10,
            parallel: true,
        }
    }
}

/// Multi-objective optimizer
pub struct MultiObjectiveOptimizer {
    /// Objectives to optimize
    objectives: Vec<Objective>,
    /// Configuration
    config: MultiObjectiveConfig,
    /// Current population
    population: Vec<MultiObjectiveSolution>,
    /// Pareto front
    pareto_front: Vec<MultiObjectiveSolution>,
    /// Random number generator
    rng: Box<dyn RngCore>,
}

impl MultiObjectiveOptimizer {
    /// Create a new multi-objective optimizer
    pub fn new(objectives: Vec<Objective>, config: MultiObjectiveConfig) -> Self {
        Self {
            objectives,
            config,
            population: Vec::new(),
            pareto_front: Vec::new(),
            rng: Box::new(thread_rng()),
        }
    }

    /// Initialize population randomly
    pub fn initialize_population(&mut self, num_variables: usize) {
        self.population.clear();

        for _ in 0..self.config.population_size {
            let mut assignments = HashMap::new();

            for i in 0..num_variables {
                assignments.insert(format!("x{i}"), self.rng.random::<bool>());
            }

            let objective_values = self.evaluate_objectives(&assignments);

            self.population.push(MultiObjectiveSolution {
                assignments,
                objective_values,
                rank: 0,
                crowding_distance: 0.0,
            });
        }

        self.compute_pareto_ranks();
        self.compute_crowding_distances();
    }

    /// Evaluate all objectives for a solution
    fn evaluate_objectives(&self, solution: &HashMap<String, bool>) -> Vec<f64> {
        self.objectives
            .iter()
            .map(|obj| obj.evaluate(solution))
            .collect()
    }

    /// Compute Pareto ranks using fast non-dominated sorting
    fn compute_pareto_ranks(&mut self) {
        let n = self.population.len();
        let mut domination_count = vec![0; n];
        let mut dominated_solutions: Vec<Vec<usize>> = vec![Vec::new(); n];
        let mut fronts: Vec<Vec<usize>> = Vec::new();

        // Count dominations
        for i in 0..n {
            for j in 0..n {
                if i == j {
                    continue;
                }

                if self.population[i].dominates(&self.population[j]) {
                    dominated_solutions[i].push(j);
                } else if self.population[j].dominates(&self.population[i]) {
                    domination_count[i] += 1;
                }
            }

            if domination_count[i] == 0 {
                self.population[i].rank = 0;
                if fronts.is_empty() {
                    fronts.push(Vec::new());
                }
                fronts[0].push(i);
            }
        }

        // Build subsequent fronts
        let mut current_front = 0;
        while current_front < fronts.len() && !fronts[current_front].is_empty() {
            let mut next_front = Vec::new();

            for &i in &fronts[current_front] {
                for &j in &dominated_solutions[i] {
                    domination_count[j] -= 1;
                    if domination_count[j] == 0 {
                        self.population[j].rank = current_front + 1;
                        next_front.push(j);
                    }
                }
            }

            if !next_front.is_empty() {
                fronts.push(next_front);
            }
            current_front += 1;
        }

        // Update Pareto front
        if !fronts.is_empty() && !fronts[0].is_empty() {
            self.pareto_front = fronts[0]
                .iter()
                .map(|&i| self.population[i].clone())
                .collect();
        }
    }

    /// Compute crowding distances for diversity preservation
    fn compute_crowding_distances(&mut self) {
        let n = self.population.len();
        let m = self.objectives.len();

        // Initialize distances to zero
        for sol in &mut self.population {
            sol.crowding_distance = 0.0;
        }

        // For each objective
        for obj_idx in 0..m {
            // Sort by objective value
            let mut indices: Vec<usize> = (0..n).collect();
            indices.sort_by(|&a, &b| {
                self.population[a].objective_values[obj_idx]
                    .partial_cmp(&self.population[b].objective_values[obj_idx])
                    .unwrap_or(std::cmp::Ordering::Equal)
            });

            // Boundary solutions get infinite distance
            self.population[indices[0]].crowding_distance = f64::INFINITY;
            self.population[indices[n - 1]].crowding_distance = f64::INFINITY;

            // Compute range
            let f_max = self.population[indices[n - 1]].objective_values[obj_idx];
            let f_min = self.population[indices[0]].objective_values[obj_idx];
            let range = f_max - f_min;

            if range > 1e-10 {
                for i in 1..n - 1 {
                    let idx = indices[i];
                    let f_next = self.population[indices[i + 1]].objective_values[obj_idx];
                    let f_prev = self.population[indices[i - 1]].objective_values[obj_idx];

                    self.population[idx].crowding_distance += (f_next - f_prev) / range;
                }
            }
        }
    }

    /// Perform binary tournament selection
    fn tournament_selection(&mut self) -> usize {
        let i = self.rng.random_range(0..self.population.len());
        let j = self.rng.random_range(0..self.population.len());

        if self.population[i].rank < self.population[j].rank {
            i
        } else if self.population[i].rank > self.population[j].rank {
            j
        } else if self.population[i].crowding_distance > self.population[j].crowding_distance {
            i
        } else {
            j
        }
    }

    /// Perform crossover
    fn crossover(
        &mut self,
        parent1: &MultiObjectiveSolution,
        parent2: &MultiObjectiveSolution,
    ) -> (MultiObjectiveSolution, MultiObjectiveSolution) {
        if self.rng.random::<f64>() > self.config.crossover_prob {
            return (parent1.clone(), parent2.clone());
        }

        let mut child1_assignments = parent1.assignments.clone();
        let mut child2_assignments = parent2.assignments.clone();

        // Uniform crossover
        for key in parent1.assignments.keys() {
            if self.rng.random::<bool>() {
                if let Some(&val) = parent2.assignments.get(key) {
                    child1_assignments.insert(key.clone(), val);
                }
                if let Some(&val) = parent1.assignments.get(key) {
                    child2_assignments.insert(key.clone(), val);
                }
            }
        }

        let child1 = MultiObjectiveSolution {
            assignments: child1_assignments,
            objective_values: Vec::new(),
            rank: 0,
            crowding_distance: 0.0,
        };

        let child2 = MultiObjectiveSolution {
            assignments: child2_assignments,
            objective_values: Vec::new(),
            rank: 0,
            crowding_distance: 0.0,
        };

        (child1, child2)
    }

    /// Perform mutation
    fn mutate(&mut self, solution: &mut MultiObjectiveSolution) {
        for value in solution.assignments.values_mut() {
            if self.rng.random::<f64>() < self.config.mutation_prob {
                *value = !*value;
            }
        }
    }

    /// Run NSGA-II algorithm
    pub fn optimize_nsga2(
        &mut self,
        num_variables: usize,
    ) -> Result<Vec<MultiObjectiveSolution>, String> {
        self.initialize_population(num_variables);

        for generation in 0..self.config.max_generations {
            let mut offspring = Vec::new();

            // Generate offspring
            while offspring.len() < self.config.population_size {
                let parent1_idx = self.tournament_selection();
                let parent2_idx = self.tournament_selection();

                // Clone parents to avoid borrowing issues
                let parent1 = self.population[parent1_idx].clone();
                let parent2 = self.population[parent2_idx].clone();

                let (mut child1, mut child2) = self.crossover(&parent1, &parent2);

                self.mutate(&mut child1);
                self.mutate(&mut child2);

                // Evaluate offspring
                child1.objective_values = self.evaluate_objectives(&child1.assignments);
                child2.objective_values = self.evaluate_objectives(&child2.assignments);

                offspring.push(child1);
                if offspring.len() < self.config.population_size {
                    offspring.push(child2);
                }
            }

            // Combine parent and offspring populations
            self.population.extend(offspring);

            // Non-dominated sorting
            self.compute_pareto_ranks();
            self.compute_crowding_distances();

            // Environmental selection
            self.population.sort_by(|a, b| match a.rank.cmp(&b.rank) {
                std::cmp::Ordering::Equal => b
                    .crowding_distance
                    .partial_cmp(&a.crowding_distance)
                    .unwrap_or(std::cmp::Ordering::Equal),
                other => other,
            });

            self.population.truncate(self.config.population_size);

            if generation % 10 == 0 {
                println!(
                    "Generation {}: Pareto front size = {}",
                    generation,
                    self.pareto_front.len()
                );
            }
        }

        Ok(self.pareto_front.clone())
    }

    /// Get the Pareto front
    pub fn get_pareto_front(&self) -> &[MultiObjectiveSolution] {
        &self.pareto_front
    }

    /// Scalarize multiple objectives into a single objective
    pub fn scalarize(&self, objective_values: &[f64], weights: &[f64]) -> f64 {
        match self.config.scalarization {
            ScalarizationMethod::WeightedSum => objective_values
                .iter()
                .zip(weights)
                .map(|(val, weight)| val * weight)
                .sum(),
            ScalarizationMethod::Tchebycheff => {
                let mut max_weighted = f64::NEG_INFINITY;
                for (val, weight) in objective_values.iter().zip(weights) {
                    let weighted = weight * val;
                    if weighted > max_weighted {
                        max_weighted = weighted;
                    }
                }
                max_weighted
            }
            ScalarizationMethod::AugmentedTchebycheff => {
                let rho = 0.00001;
                let max_weighted = objective_values
                    .iter()
                    .zip(weights)
                    .map(|(val, weight)| weight * val)
                    .fold(f64::NEG_INFINITY, f64::max);
                let sum: f64 = objective_values.iter().sum();
                max_weighted + rho * sum
            }
            _ => objective_values
                .iter()
                .zip(weights)
                .map(|(val, weight)| val * weight)
                .sum(),
        }
    }
}

/// Result of multi-objective optimization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultiObjectiveResult {
    /// Pareto-optimal solutions
    pub pareto_solutions: Vec<MultiObjectiveSolution>,
    /// Statistics
    pub statistics: OptimizationStatistics,
}

/// Statistics from multi-objective optimization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationStatistics {
    /// Total number of evaluations
    pub num_evaluations: usize,
    /// Total runtime in seconds
    pub runtime_seconds: f64,
    /// Size of final Pareto front
    pub pareto_front_size: usize,
    /// Hypervolume indicator
    pub hypervolume: f64,
    /// Spacing metric
    pub spacing: f64,
}

impl OptimizationStatistics {
    /// Compute hypervolume for the Pareto front
    pub fn compute_hypervolume(
        solutions: &[MultiObjectiveSolution],
        reference_point: &[f64],
    ) -> f64 {
        // Simplified 2D hypervolume computation
        if solutions.is_empty() || reference_point.len() != 2 {
            return 0.0;
        }

        let mut sorted_solutions: Vec<_> = solutions.to_vec();
        sorted_solutions.sort_by(|a, b| {
            a.objective_values[0]
                .partial_cmp(&b.objective_values[0])
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        let mut hypervolume = 0.0;
        let mut prev_y = reference_point[1];

        for sol in &sorted_solutions {
            let width = reference_point[0] - sol.objective_values[0];
            let height = prev_y - sol.objective_values[1];

            if width > 0.0 && height > 0.0 {
                hypervolume += width * height;
                prev_y = sol.objective_values[1];
            }
        }

        hypervolume
    }

    /// Compute spacing metric for distribution quality
    pub fn compute_spacing(solutions: &[MultiObjectiveSolution]) -> f64 {
        if solutions.len() < 2 {
            return 0.0;
        }

        let mut distances = Vec::new();

        for i in 0..solutions.len() {
            let mut min_distance = f64::INFINITY;

            for j in 0..solutions.len() {
                if i != j {
                    let dist = solutions[i].distance_to(&solutions[j]);
                    if dist < min_distance {
                        min_distance = dist;
                    }
                }
            }

            distances.push(min_distance);
        }

        let mean: f64 = distances.iter().sum::<f64>() / distances.len() as f64;
        let variance: f64 =
            distances.iter().map(|d| (d - mean).powi(2)).sum::<f64>() / distances.len() as f64;

        variance.sqrt()
    }
}

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

    #[test]
    fn test_objective_creation() {
        let obj = Objective::new("test", ObjectiveDirection::Minimize, 1.0);
        assert_eq!(obj.name, "test");
        assert_eq!(obj.direction, ObjectiveDirection::Minimize);
        assert_eq!(obj.weight, 1.0);
    }

    #[test]
    fn test_dominance() {
        let sol1 = MultiObjectiveSolution {
            assignments: HashMap::new(),
            objective_values: vec![1.0, 2.0],
            rank: 0,
            crowding_distance: 0.0,
        };

        let sol2 = MultiObjectiveSolution {
            assignments: HashMap::new(),
            objective_values: vec![2.0, 3.0],
            rank: 0,
            crowding_distance: 0.0,
        };

        assert!(sol1.dominates(&sol2));
        assert!(!sol2.dominates(&sol1));
    }

    #[test]
    fn test_multi_objective_optimizer_initialization() {
        let objectives = vec![
            Objective::new("obj1", ObjectiveDirection::Minimize, 1.0),
            Objective::new("obj2", ObjectiveDirection::Minimize, 1.0),
        ];

        let config = MultiObjectiveConfig::default();
        let mut optimizer = MultiObjectiveOptimizer::new(objectives, config);

        optimizer.initialize_population(10);
        assert_eq!(optimizer.population.len(), 100);
    }

    #[test]
    fn test_scalarization() {
        let objectives = vec![
            Objective::new("obj1", ObjectiveDirection::Minimize, 0.5),
            Objective::new("obj2", ObjectiveDirection::Minimize, 0.5),
        ];

        let mut config = MultiObjectiveConfig::default();
        config.scalarization = ScalarizationMethod::WeightedSum;

        let optimizer = MultiObjectiveOptimizer::new(objectives, config);

        let values = vec![1.0, 2.0];
        let weights = vec![0.5, 0.5];
        let result = optimizer.scalarize(&values, &weights);

        assert!((result - 1.5).abs() < 1e-10);
    }

    #[test]
    fn test_hypervolume_computation() {
        let solutions = vec![
            MultiObjectiveSolution {
                assignments: HashMap::new(),
                objective_values: vec![1.0, 3.0],
                rank: 0,
                crowding_distance: 0.0,
            },
            MultiObjectiveSolution {
                assignments: HashMap::new(),
                objective_values: vec![2.0, 2.0],
                rank: 0,
                crowding_distance: 0.0,
            },
        ];

        let reference = vec![4.0, 4.0];
        let hv = OptimizationStatistics::compute_hypervolume(&solutions, &reference);

        assert!(hv > 0.0);
    }
}