pub struct Solver<N: Value, C: Value> { /* private fields */ }Expand description
A solver for a Problem instance.
Implementations§
Source§impl<N: Value, C: Value> Solver<N, C>
impl<N: Value, C: Value> Solver<N, C>
Sourcepub fn new(problem: Problem<N, C>) -> Solver<N, C>
pub fn new(problem: Problem<N, C>) -> Solver<N, C>
Creates a new solver that solves problem.
Examples found in repository?
examples/simple.rs (line 13)
3fn main() {
4 let mut prob = Problem::default();
5 prob.add_constraints(1..=3);
6 prob.add_subset("A", vec![1, 2, 3]);
7 prob.add_subset("B", vec![1]);
8 prob.add_subset("C", vec![2]);
9 prob.add_subset("D", vec![3]);
10 prob.add_subset("E", vec![1, 2]);
11 prob.add_subset("F", vec![2, 3]);
12
13 let mut solver = Solver::new(prob);
14 let mut solutions = vec![];
15 solver.run();
16
17 for event in solver {
18 if let SolverEvent::SolutionFound(sol) = event {
19 solutions.push(sol);
20 }
21 }
22
23 println!("{:?}", solutions);
24}More examples
examples/tetromino.rs (line 71)
32fn main() -> Result<(), Box<dyn Error>> {
33 let board = Board::from_bytes_array(&[
34 b"##...",
35 b"#....",
36 b"#....",
37 b"#....",
38 b".....",
39 ]);
40
41 let tet_i = Polyomino::from_bytes_array(&[
42 b"####",
43 ])?;
44 let tet_o = Polyomino::from_bytes_array(&[
45 b"##",
46 b"##",
47 ])?;
48 let tet_t = Polyomino::from_bytes_array(&[
49 b"###",
50 b".#.",
51 ])?;
52 let tet_l = Polyomino::from_bytes_array(&[
53 b"#..",
54 b"###",
55 ])?;
56 let tet_s = Polyomino::from_bytes_array(&[
57 b".##",
58 b"##.",
59 ])?;
60
61 let mut prob = PolyominoPacking::default();
62 *prob.board_mut() = board;
63 prob.add_piece("I", tet_i);
64 prob.add_piece("O", tet_o);
65 prob.add_piece("T", tet_t);
66 prob.add_piece("L", tet_l);
67 prob.add_piece("S", tet_s);
68
69 println!("Generating the problem...");
70 let gen_prob = prob.generate_problem();
71 let mut solver = Solver::new(gen_prob);
72
73 println!("Solving the problem...");
74 let start_time = Instant::now();
75 let mut solutions = vec![];
76 solver.run();
77
78 for event in solver {
79 if let SolverEvent::SolutionFound(sol) = event {
80 print_sol(&prob, &sol);
81 println!();
82 solutions.push(sol);
83 }
84 }
85
86 // This does not measure the exact time because printing the solutions takes up a nonnegligible fraction.
87 // To measure the exact time, print the solutions after this line.
88 let elapsed_time = start_time.elapsed();
89
90 println!(
91 "Found {:?} solutions, w/ rotations/reflections. ({:?}s)",
92 solutions.len(),
93 elapsed_time.as_millis() as f64 / 1000.
94 );
95
96 Ok(())
97}examples/pentomino.rs (line 115)
32fn main() -> Result<(), Box<dyn Error>> {
33 let board = Board::from_bytes_array(&[
34 b"........",
35 b"........",
36 b"........",
37 b"...##...",
38 b"...##...",
39 b"........",
40 b"........",
41 b"........",
42 ]);
43
44 let pento_f = Polyomino::from_bytes_array(&[
45 b".##",
46 b"##.",
47 b".#.",
48 ])?;
49 let pento_i = Polyomino::from_bytes_array(&[
50 b"#####",
51 ])?;
52 let pento_l = Polyomino::from_bytes_array(&[
53 b"####",
54 b"#...",
55 ])?;
56 let pento_n = Polyomino::from_bytes_array(&[
57 b".###",
58 b"##..",
59 ])?;
60 let pento_p = Polyomino::from_bytes_array(&[
61 b"###",
62 b".##",
63 ])?;
64 let pento_t = Polyomino::from_bytes_array(&[
65 b"###",
66 b".#.",
67 b".#.",
68 ])?;
69 let pento_u = Polyomino::from_bytes_array(&[
70 b"#.#",
71 b"###",
72 ])?;
73 let pento_v = Polyomino::from_bytes_array(&[
74 b"#..",
75 b"#..",
76 b"###",
77 ])?;
78 let pento_w = Polyomino::from_bytes_array(&[
79 b"#..",
80 b"##.",
81 b".##",
82 ])?;
83 let pento_x = Polyomino::from_bytes_array(&[
84 b".#.",
85 b"###",
86 b".#.",
87 ])?;
88 let pento_y = Polyomino::from_bytes_array(&[
89 b"####",
90 b".#..",
91 ])?;
92 let pento_z = Polyomino::from_bytes_array(&[
93 b"##.",
94 b".#.",
95 b".##",
96 ])?;
97
98 let mut prob = PolyominoPacking::default();
99 *prob.board_mut() = board;
100 prob.add_piece("F", pento_f);
101 prob.add_piece("I", pento_i);
102 prob.add_piece("L", pento_l);
103 prob.add_piece("N", pento_n);
104 prob.add_piece("P", pento_p);
105 prob.add_piece("T", pento_t);
106 prob.add_piece("U", pento_u);
107 prob.add_piece("V", pento_v);
108 prob.add_piece("W", pento_w);
109 prob.add_piece("X", pento_x);
110 prob.add_piece("Y", pento_y);
111 prob.add_piece("Z", pento_z);
112
113 println!("Generating the problem...");
114 let gen_prob = prob.generate_problem();
115 let mut solver = Solver::new(gen_prob);
116
117 println!("Solving the problem...");
118 let start_time = Instant::now();
119 let mut solutions = vec![];
120 solver.run();
121
122 for event in solver {
123 if let SolverEvent::SolutionFound(sol) = event {
124 print_sol(&prob, &sol);
125 println!();
126 solutions.push(sol);
127 }
128 }
129
130 // This does not measure the exact time because printing the solutions takes up a nonnegligible fraction.
131 // To measure the exact time, print the solutions after this line.
132 let elapsed_time = start_time.elapsed();
133
134 println!(
135 "Found {:?} solutions, w/ rotations/reflections. ({:?}s)",
136 solutions.len(),
137 elapsed_time.as_millis() as f64 / 1000.
138 );
139
140 Ok(())
141}pub fn generate_matrix(problem: &Problem<N, C>) -> Matrix
Sourcepub fn run(&mut self)
pub fn run(&mut self)
Runs the solver thread.
Examples found in repository?
examples/simple.rs (line 15)
3fn main() {
4 let mut prob = Problem::default();
5 prob.add_constraints(1..=3);
6 prob.add_subset("A", vec![1, 2, 3]);
7 prob.add_subset("B", vec![1]);
8 prob.add_subset("C", vec![2]);
9 prob.add_subset("D", vec![3]);
10 prob.add_subset("E", vec![1, 2]);
11 prob.add_subset("F", vec![2, 3]);
12
13 let mut solver = Solver::new(prob);
14 let mut solutions = vec![];
15 solver.run();
16
17 for event in solver {
18 if let SolverEvent::SolutionFound(sol) = event {
19 solutions.push(sol);
20 }
21 }
22
23 println!("{:?}", solutions);
24}More examples
examples/tetromino.rs (line 76)
32fn main() -> Result<(), Box<dyn Error>> {
33 let board = Board::from_bytes_array(&[
34 b"##...",
35 b"#....",
36 b"#....",
37 b"#....",
38 b".....",
39 ]);
40
41 let tet_i = Polyomino::from_bytes_array(&[
42 b"####",
43 ])?;
44 let tet_o = Polyomino::from_bytes_array(&[
45 b"##",
46 b"##",
47 ])?;
48 let tet_t = Polyomino::from_bytes_array(&[
49 b"###",
50 b".#.",
51 ])?;
52 let tet_l = Polyomino::from_bytes_array(&[
53 b"#..",
54 b"###",
55 ])?;
56 let tet_s = Polyomino::from_bytes_array(&[
57 b".##",
58 b"##.",
59 ])?;
60
61 let mut prob = PolyominoPacking::default();
62 *prob.board_mut() = board;
63 prob.add_piece("I", tet_i);
64 prob.add_piece("O", tet_o);
65 prob.add_piece("T", tet_t);
66 prob.add_piece("L", tet_l);
67 prob.add_piece("S", tet_s);
68
69 println!("Generating the problem...");
70 let gen_prob = prob.generate_problem();
71 let mut solver = Solver::new(gen_prob);
72
73 println!("Solving the problem...");
74 let start_time = Instant::now();
75 let mut solutions = vec![];
76 solver.run();
77
78 for event in solver {
79 if let SolverEvent::SolutionFound(sol) = event {
80 print_sol(&prob, &sol);
81 println!();
82 solutions.push(sol);
83 }
84 }
85
86 // This does not measure the exact time because printing the solutions takes up a nonnegligible fraction.
87 // To measure the exact time, print the solutions after this line.
88 let elapsed_time = start_time.elapsed();
89
90 println!(
91 "Found {:?} solutions, w/ rotations/reflections. ({:?}s)",
92 solutions.len(),
93 elapsed_time.as_millis() as f64 / 1000.
94 );
95
96 Ok(())
97}examples/pentomino.rs (line 120)
32fn main() -> Result<(), Box<dyn Error>> {
33 let board = Board::from_bytes_array(&[
34 b"........",
35 b"........",
36 b"........",
37 b"...##...",
38 b"...##...",
39 b"........",
40 b"........",
41 b"........",
42 ]);
43
44 let pento_f = Polyomino::from_bytes_array(&[
45 b".##",
46 b"##.",
47 b".#.",
48 ])?;
49 let pento_i = Polyomino::from_bytes_array(&[
50 b"#####",
51 ])?;
52 let pento_l = Polyomino::from_bytes_array(&[
53 b"####",
54 b"#...",
55 ])?;
56 let pento_n = Polyomino::from_bytes_array(&[
57 b".###",
58 b"##..",
59 ])?;
60 let pento_p = Polyomino::from_bytes_array(&[
61 b"###",
62 b".##",
63 ])?;
64 let pento_t = Polyomino::from_bytes_array(&[
65 b"###",
66 b".#.",
67 b".#.",
68 ])?;
69 let pento_u = Polyomino::from_bytes_array(&[
70 b"#.#",
71 b"###",
72 ])?;
73 let pento_v = Polyomino::from_bytes_array(&[
74 b"#..",
75 b"#..",
76 b"###",
77 ])?;
78 let pento_w = Polyomino::from_bytes_array(&[
79 b"#..",
80 b"##.",
81 b".##",
82 ])?;
83 let pento_x = Polyomino::from_bytes_array(&[
84 b".#.",
85 b"###",
86 b".#.",
87 ])?;
88 let pento_y = Polyomino::from_bytes_array(&[
89 b"####",
90 b".#..",
91 ])?;
92 let pento_z = Polyomino::from_bytes_array(&[
93 b"##.",
94 b".#.",
95 b".##",
96 ])?;
97
98 let mut prob = PolyominoPacking::default();
99 *prob.board_mut() = board;
100 prob.add_piece("F", pento_f);
101 prob.add_piece("I", pento_i);
102 prob.add_piece("L", pento_l);
103 prob.add_piece("N", pento_n);
104 prob.add_piece("P", pento_p);
105 prob.add_piece("T", pento_t);
106 prob.add_piece("U", pento_u);
107 prob.add_piece("V", pento_v);
108 prob.add_piece("W", pento_w);
109 prob.add_piece("X", pento_x);
110 prob.add_piece("Y", pento_y);
111 prob.add_piece("Z", pento_z);
112
113 println!("Generating the problem...");
114 let gen_prob = prob.generate_problem();
115 let mut solver = Solver::new(gen_prob);
116
117 println!("Solving the problem...");
118 let start_time = Instant::now();
119 let mut solutions = vec![];
120 solver.run();
121
122 for event in solver {
123 if let SolverEvent::SolutionFound(sol) = event {
124 print_sol(&prob, &sol);
125 println!();
126 solutions.push(sol);
127 }
128 }
129
130 // This does not measure the exact time because printing the solutions takes up a nonnegligible fraction.
131 // To measure the exact time, print the solutions after this line.
132 let elapsed_time = start_time.elapsed();
133
134 println!(
135 "Found {:?} solutions, w/ rotations/reflections. ({:?}s)",
136 solutions.len(),
137 elapsed_time.as_millis() as f64 / 1000.
138 );
139
140 Ok(())
141}pub fn request_progress(&self)
pub fn pause(&self)
pub fn abort(&self)
Trait Implementations§
Source§impl<N: Value, C: Value> IntoIterator for Solver<N, C>
impl<N: Value, C: Value> IntoIterator for Solver<N, C>
Source§fn into_iter(self) -> Self::IntoIter
fn into_iter(self) -> Self::IntoIter
Returns an iterator of SolverEvents that a solver emits.
Source§type Item = SolverEvent<N>
type Item = SolverEvent<N>
The type of the elements being iterated over.
Source§type IntoIter = SolverIter<N, C>
type IntoIter = SolverIter<N, C>
Which kind of iterator are we turning this into?
Auto Trait Implementations§
impl<N, C> Freeze for Solver<N, C>
impl<N, C> !RefUnwindSafe for Solver<N, C>
impl<N, C> Send for Solver<N, C>
impl<N, C> !Sync for Solver<N, C>
impl<N, C> Unpin for Solver<N, C>
impl<N, C> !UnwindSafe for Solver<N, C>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more