Skip to main content

quantrs2_tytan/visual_problem_builder/
functions.rs

1//! Auto-generated module
2//!
3//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
4
5use std::collections::HashMap;
6
7use super::types::{
8    BuilderConfig, ComparisonOperator, ConstraintType, ExportFormat, LabelPosition, LabelSettings,
9    ObjectiveExpression, OptimizationDirection, Position, ProblemValidator, ValidationSeverity,
10    VariableDomain, VariableShape, VariableType, VariableVisualProperties, VisualProblem,
11    VisualProblemBuilder, VisualVariable,
12};
13
14#[cfg(test)]
15mod tests {
16    use super::*;
17    #[test]
18    fn test_visual_problem_builder() -> Result<(), String> {
19        let config = BuilderConfig::default();
20        let mut builder = VisualProblemBuilder::new(config);
21        builder.new_problem("Test Problem")?;
22        let var1_id = builder.add_variable(
23            "x1",
24            VariableType::Binary,
25            Position {
26                x: 100.0,
27                y: 100.0,
28                z: None,
29            },
30        )?;
31        let var2_id = builder.add_variable(
32            "x2",
33            VariableType::Binary,
34            Position {
35                x: 200.0,
36                y: 100.0,
37                z: None,
38            },
39        )?;
40        assert_eq!(builder.problem().variables.len(), 2);
41        let _constraint_id = builder.add_constraint(
42            "Sum constraint",
43            ConstraintType::Linear {
44                coefficients: vec![1.0, 1.0],
45                operator: ComparisonOperator::LessEqual,
46                rhs: 1.0,
47            },
48            vec![var1_id.clone(), var2_id.clone()],
49        )?;
50        assert_eq!(builder.problem().constraints.len(), 1);
51        let mut coefficients = HashMap::new();
52        coefficients.insert(var1_id, 1.0);
53        coefficients.insert(var2_id, 2.0);
54        builder.set_objective(
55            "Linear objective",
56            ObjectiveExpression::Linear {
57                coefficients,
58                constant: 0.0,
59            },
60            OptimizationDirection::Maximize,
61        )?;
62        assert!(builder.problem().objective.is_some());
63        builder.undo()?;
64        assert!(builder.problem().objective.is_none());
65        builder.redo()?;
66        assert!(builder.problem().objective.is_some());
67        let python_code = builder.generate_code(ExportFormat::Python)?;
68        assert!(python_code.contains("symbols"));
69        assert!(python_code.contains("SASampler"));
70        let json = builder.save_problem()?;
71        assert!(json.contains("Test Problem"));
72        Ok(())
73    }
74    #[test]
75    fn test_validation() -> Result<(), String> {
76        let mut validator = ProblemValidator::new();
77        let mut problem = VisualProblem::new();
78        let errors = validator.validate(&problem)?;
79        assert!(!errors.is_empty());
80        problem.variables.push(VisualVariable {
81            id: "var1".to_string(),
82            name: "x1".to_string(),
83            var_type: VariableType::Binary,
84            domain: VariableDomain::Binary,
85            position: Position {
86                x: 0.0,
87                y: 0.0,
88                z: None,
89            },
90            visual_properties: VariableVisualProperties {
91                color: "#000000".to_string(),
92                size: 10.0,
93                shape: VariableShape::Circle,
94                visible: true,
95                label: LabelSettings {
96                    show: true,
97                    text: None,
98                    font_size: 12.0,
99                    position: LabelPosition::Bottom,
100                },
101            },
102            description: String::new(),
103            groups: Vec::new(),
104        });
105        let errors = validator.validate(&problem)?;
106        assert!(errors
107            .iter()
108            .any(|e| matches!(e.severity, ValidationSeverity::Warning)));
109        Ok(())
110    }
111}