|
11 | 11 | // See the License for the specific language governing permissions and |
12 | 12 | // limitations under the License. |
13 | 13 |
|
14 | | -/// Declaration of the core objects for the constraint solver. |
15 | | -/// |
16 | | -/// The literature around constraint programming is extremely dense but one |
17 | | -/// can find some basic introductions in the following links: |
18 | | -/// - http://en.wikipedia.org/wiki/Constraint_programming |
19 | | -/// - http://kti.mff.cuni.cz/~bartak/constraints/index.html |
20 | | -/// |
21 | | -/// Here is a very simple Constraint Programming problem: |
22 | | -/// |
23 | | -/// If we see 56 legs and 20 heads, how many two-legged pheasants |
24 | | -/// and four-legged rabbits are we looking at? |
25 | | -/// |
26 | | -/// Here is some simple Constraint Programming code to find out: |
27 | | -/// |
28 | | -/// void pheasant() { |
29 | | -/// Solver s("pheasant"); |
30 | | -/// // Create integer variables to represent the number of pheasants and |
31 | | -/// // rabbits, with a minimum of 0 and a maximum of 20. |
32 | | -/// IntVar* const p = s.MakeIntVar(0, 20, "pheasant")); |
33 | | -/// IntVar* const r = s.MakeIntVar(0, 20, "rabbit")); |
34 | | -/// // The number of heads is the sum of pheasants and rabbits. |
35 | | -/// IntExpr* const heads = s.MakeSum(p, r); |
36 | | -/// // The number of legs is the sum of pheasants * 2 and rabbits * 4. |
37 | | -/// IntExpr* const legs = s.MakeSum(s.MakeProd(p, 2), s.MakeProd(r, 4)); |
38 | | -/// // Constraints: the number of legs is 56 and heads is 20. |
39 | | -/// Constraint* const ct_legs = s.MakeEquality(legs, 56); |
40 | | -/// Constraint* const ct_heads = s.MakeEquality(heads, 20); |
41 | | -/// s.AddConstraint(ct_legs); |
42 | | -/// s.AddConstraint(ct_heads); |
43 | | -/// DecisionBuilder* const db = s.MakePhase(p, r, |
44 | | -/// Solver::CHOOSE_FIRST_UNBOUND, |
45 | | -/// Solver::ASSIGN_MIN_VALUE); |
46 | | -/// s.NewSearch(db); |
47 | | -/// CHECK(s.NextSolution()); |
48 | | -/// LOG(INFO) << "rabbits -> " << r->Value() << ", pheasants -> " |
49 | | -/// << p->Value(); |
50 | | -/// LOG(INFO) << s.DebugString(); |
51 | | -/// s.EndSearch(); |
52 | | -/// } |
53 | | -/// |
54 | | -/// which outputs: |
55 | | -/// |
56 | | -/// rabbits -> 8, pheasants -> 12 |
57 | | -/// Solver(name = "pheasant", |
58 | | -/// state = OUTSIDE_SEARCH, |
59 | | -/// branches = 0, |
60 | | -/// fails = 0, |
61 | | -/// decisions = 0 |
62 | | -/// propagation loops = 11, |
63 | | -/// demons Run = 25, |
64 | | -/// Run time = 0 ms) |
65 | | -/// |
66 | | -/// |
| 14 | +/** @file constraint_solver.h |
| 15 | +* Declaration of the core objects for the constraint solver. |
| 16 | +* |
| 17 | +* The literature around constraint programming is extremely dense but one |
| 18 | +* can find some basic introductions in the following links: |
| 19 | +* - http://en.wikipedia.org/wiki/Constraint_programming |
| 20 | +* - http://kti.mff.cuni.cz/~bartak/constraints/index.html |
| 21 | +* |
| 22 | +* Here is a very simple Constraint Programming problem: |
| 23 | +* |
| 24 | +* If we see 56 legs and 20 heads, how many two-legged pheasants |
| 25 | +* and four-legged rabbits are we looking at? |
| 26 | +* |
| 27 | +* Here is some simple Constraint Programming code to find out: |
| 28 | +* @code{.cpp} |
| 29 | +* void pheasant() { |
| 30 | +* Solver s("pheasant"); |
| 31 | +* // Create integer variables to represent the number of pheasants and |
| 32 | +* // rabbits, with a minimum of 0 and a maximum of 20. |
| 33 | +* IntVar* const p = s.MakeIntVar(0, 20, "pheasant")); |
| 34 | +* IntVar* const r = s.MakeIntVar(0, 20, "rabbit")); |
| 35 | +* // The number of heads is the sum of pheasants and rabbits. |
| 36 | +* IntExpr* const heads = s.MakeSum(p, r); |
| 37 | +* // The number of legs is the sum of pheasants * 2 and rabbits * 4. |
| 38 | +* IntExpr* const legs = s.MakeSum(s.MakeProd(p, 2), s.MakeProd(r, 4)); |
| 39 | +* // Constraints: the number of legs is 56 and heads is 20. |
| 40 | +* Constraint* const ct_legs = s.MakeEquality(legs, 56); |
| 41 | +* Constraint* const ct_heads = s.MakeEquality(heads, 20); |
| 42 | +* s.AddConstraint(ct_legs); |
| 43 | +* s.AddConstraint(ct_heads); |
| 44 | +* DecisionBuilder* const db = s.MakePhase(p, r, |
| 45 | +* Solver::CHOOSE_FIRST_UNBOUND, |
| 46 | +* Solver::ASSIGN_MIN_VALUE); |
| 47 | +* s.NewSearch(db); |
| 48 | +* CHECK(s.NextSolution()); |
| 49 | +* LOG(INFO) << "rabbits -> " << r->Value() << ", pheasants -> " |
| 50 | +* << p->Value(); |
| 51 | +* LOG(INFO) << s.DebugString(); |
| 52 | +* s.EndSearch(); |
| 53 | +* } |
| 54 | +* @endcode |
| 55 | +* which outputs: |
| 56 | +@verbatim |
| 57 | +rabbits -> 8, pheasants -> 12 |
| 58 | +Solver(name = "pheasant", |
| 59 | + state = OUTSIDE_SEARCH, |
| 60 | + branches = 0, |
| 61 | + fails = 0, |
| 62 | + decisions = 0 |
| 63 | + propagation loops = 11, |
| 64 | + demons Run = 25, |
| 65 | + Run time = 0 ms) |
| 66 | +@endverbatim |
| 67 | +*/ |
67 | 68 |
|
68 | 69 | #ifndef ORTOOLS_CONSTRAINT_SOLVER_CONSTRAINT_SOLVER_H_ |
69 | 70 | #define ORTOOLS_CONSTRAINT_SOLVER_CONSTRAINT_SOLVER_H_ |
|
0 commit comments