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
//! The description of a problem: variables with finite integer domains, and constraints over them.
//!
//! # Meaning
//!
//! A [`Problem`] owns its variables and its constraints. Variables and constraints are addressed
//! by [`VariableIndex`] and [`ConstraintIndex`], which count from 0 in the order they were added;
//! nothing is ever removed or renumbered. A [`ValueIndex`] is different: it is the position of a
//! value in the domain of one variable, not the value itself.
//!
//! An assignment is a slice with one value per variable, in variable order, holding the values
//! themselves and not their positions in the domain. A slice is a solution
//! ([`Problem::is_solution`]) when it has the right length, every value is in the domain of its
//! variable, and every constraint is satisfied: that is the specification the rest of the crate
//! is tested against.
//!
//! The traits that a constraint implements, [`Constraint`] and [`ConstraintProperty`], are
//! defined here because a problem stores its constraints as trait objects. The constraints
//! themselves are in [`constraints`](crate::constraints).
//!
//! # Design notes
//!
//! - **Assignments hold values, not domain positions.** A constraint can then be checked against
//! a solution as it is, whatever the domains; the positions are what the diagrams and the
//! weights over the values are indexed by.
//! - **Misuse panics at once, with a message.** An empty domain, a repeated value in a domain, a
//! constraint over a variable that is not in the problem, or fixing a variable to a value
//! outside its domain stop the program where the mistake is made, instead of giving wrong
//! results later.
//! - **Variables carry an optional position.** It places a variable in a structure of the problem
//! (for example the row and column of a Sudoku cell) and is read only by the positional
//! encoding of the learned models; it plays no part in the compilation.
//!
//! # Example
//!
//! ```
//! use aicad::constraints::all_different;
//! use aicad::modelling::*;
//!
//! let mut problem = Problem::default();
//! let x = problem.add_variables(3, vec![1, 2, 3]);
//! all_different(&mut problem, x.clone());
//!
//! assert!(problem.is_solution(&[1, 2, 3]));
//! assert!(!problem.is_solution(&[1, 1, 3]));
//!
//! equal(&mut problem, x[0], 2);
//! assert!(!problem.is_solution(&[1, 2, 3]));
//! assert!(problem.is_solution(&[2, 1, 3]));
//! ```
pub
pub
pub
pub
pub use ;
pub use Problem;
pub use ;
pub use Variable;
/// Fixes `variable` to `value` by shrinking its domain to that value alone.
/// Panics if `value` is not in the domain of `variable`.
/// Position of a variable in its problem, in the order the variables were added.
;
/// Position of a value in the domain of one variable. It is not the value itself.
;
/// Position of a constraint in its problem, in the order the constraints were added.
;