quantrs2_tytan/problem_dsl/
examples.rs

1//! Example problems for the problem DSL.
2
3/// Example problems
4pub struct Examples;
5
6impl Examples {
7    /// Simple binary problem
8    pub const SIMPLE_BINARY: &str = r"
9        var x binary;
10        var y binary;
11
12        minimize x + y;
13
14        subject to
15            x + y >= 1;
16    ";
17
18    /// Traveling salesman problem
19    pub const TSP: &str = r"
20        param n = 4;
21        param distances = [
22            [0, 10, 15, 20],
23            [10, 0, 35, 25],
24            [15, 35, 0, 30],
25            [20, 25, 30, 0]
26        ];
27
28        var x[n, n] binary;
29
30        minimize sum(i in 0..n, j in 0..n: distances[i][j] * x[i,j]);
31
32        subject to
33            // Each city visited exactly once
34            forall(i in 0..n): sum(j in 0..n: x[i,j]) == 1;
35            forall(j in 0..n): sum(i in 0..n: x[i,j]) == 1;
36    ";
37
38    /// Graph coloring
39    pub const GRAPH_COLORING: &str = r"
40        param n_vertices = 5;
41        param n_colors = 3;
42        param edges = [(0,1), (1,2), (2,3), (3,4), (4,0)];
43
44        var color[n_vertices, n_colors] binary;
45
46        minimize sum(v in 0..n_vertices, c in 0..n_colors: c * color[v,c]);
47
48        subject to
49            // Each vertex has exactly one color
50            forall(v in 0..n_vertices): sum(c in 0..n_colors: color[v,c]) == 1;
51
52            // Adjacent vertices have different colors
53            forall((u,v) in edges, c in 0..n_colors):
54                color[u,c] + color[v,c] <= 1;
55    ";
56}
57
58/// Get example by name
59pub fn get_example(name: &str) -> Option<&str> {
60    match name {
61        "simple_binary" => Some(Examples::SIMPLE_BINARY),
62        "tsp" => Some(Examples::TSP),
63        "graph_coloring" => Some(Examples::GRAPH_COLORING),
64        _ => None,
65    }
66}