/tutorial/backtracking/logigram/logigram.txt

http://github.com/tybor/Liberty · Plain Text · 262 lines · 207 code · 55 blank · 0 comment · 0 complexity · d73e29581ed16af0e81f4790a9dff648 MD5 · raw file

  1. The tutorial logigram
  2. #####################
  3. This tutorial shows a typical use of the backtracking cluster.
  4. How to compile?
  5. ===============
  6. Just type:
  7. {{{{
  8. se c -boost -clean -o logigram logigram
  9. }}}}
  10. What does it do?
  11. ================
  12. That tutorial that shows how to solve problems
  13. sometimes called logigrams. The logigrams
  14. are made of a set of items (persons, date, places, ...)
  15. grouped into categories and set of true propositions
  16. about the items. From these propositions you must deduce
  17. how the given items are combined together.
  18. Here is an example:
  19. That program solves the following classic problem.
  20. Knowing that:
  21. - the house of the english is red,
  22. - the spanish has a dog,
  23. - one drink coffee in the green house,
  24. - the ukrainian drinks tea,
  25. - the green house is just at right of the ivory house,
  26. - the man that smokes winstons have a snail,
  27. - the man that smokes kools have the yellow house,
  28. - one drinks milk in the house at the middle,
  29. - the norvegian lives in the house at left,
  30. - the one who smokes chesterfields is neibourgh of a fox,
  31. - the one who smokes kools is neibourgh of a horse,
  32. - the one who smokes luckystrike drinks orange juice,
  33. - the japanese smokes parliaments,
  34. - the norvegian is neibourgh of the blue house.
  35. Tell who got the zebra and who drinks water?
  36. The output of the program is
  37. {{{{
  38. > logigram
  39. +-----------+-------------+--------+--------------+---------------+--------+
  40. | house | nationality | animal | drink | cigarette | color |
  41. +-----------+-------------+--------+--------------+---------------+--------+
  42. | left | norvegian | fox | water | kools | yellow |
  43. | mid-left | ukrainian | horse | tea | chesterfields | blue |
  44. | middle | english | snail | milk | winston | red |
  45. | mid-right | spanish | dog | orange juice | luckystrike | ivory |
  46. | right | japanese | zebra | coffee | parliaments | green |
  47. +-----------+-------------+--------+--------------+---------------+--------+
  48. 1 solution
  49. }}}}
  50. There are three other problems that let you challenge the
  51. tutorial.
  52. Exercice: in file logigram.e, feature describe_problem_classic put
  53. in comment line that declares that the house of the english is red
  54. as below and re-run. How many solutions now? Happy chrismas!
  55. {{{{
  56. -- rule(yes(item("nationality", "english"), item("color", "red")))
  57. }}}}
  58. Exercice: in file logigram.e, feature describe_problem_classic put
  59. line that declares the ordered group house at the end of the
  60. groups declarations and measure the difference of computing time
  61. with the command 'time' (under unix). Explain.
  62. Exercice: write a program that solves the same problem.
  63. Exercice: write a program that solves any problem of the same kind.
  64. How does it work?
  65. =================
  66. It works in three steps:
  67. - Creation of the problem description.
  68. - Transformation of the description to a AND/OR
  69. tree of possible permutations.
  70. - Exploration of the AND/OR tree by backtracking to
  71. retrieve the solutions.
  72. The main idea is to use permutations for retrieving the solutions.
  73. Description of the problem
  74. --------------------------
  75. The description is managed with an object of the class DESCRIPTION
  76. that mainly contains:
  77. - a set of groups;
  78. - a set of constraints through an object of class CONSTRAINT_SET.
  79. First of all, the groups must be declared. There are 3 kind of
  80. groups:
  81. - the atomic groups;
  82. - the ordered groups what means that the order of the items
  83. of the group cares and that each item receive a number that
  84. is its place, beginning to zero;
  85. - the numeric groups that must contain numeric items.
  86. The groups are all managed through objects of class GROUP.
  87. The constraints (class CONSTRAINT) are distinguished in two
  88. types:
  89. - Constraints on couples (class CONSTRAINT_COUPLE association of a
  90. couple of two items that are not of the same group), that comprises:
  91. - positive association of a couple (class CONSTRAINT_YES) what meaning
  92. is that the 2 items are associated together (example: marie had
  93. 4 children);
  94. - negative association of a couple (class CONSTRAINT_NO) what meaning
  95. is that the 2 items are never associated together (example: marie
  96. didn't have 4 children).
  97. - Logical constraints (class CONSTRAINT_LOGICAL) that currently only are
  98. the relationnal constraints (class CONSTRAINT_RELATIONAL) on some integer
  99. expressions, that comprises equal, greater, lesser, and
  100. not equal, from the classes CONSTRAINT_EQUAL, CONSTRAINT_GREATER,
  101. CONSTRAINT_LESSER, CONSTRAINT_NOT_EQUAL.
  102. The relational constraints are on expressions that are built using
  103. inheriters of class EXPR, say:
  104. - constants from EXPR_VALUE;
  105. - addition, substraction, multiplication from EXPR_ADD, EXPR_SUB
  106. and EXPR_MUL;
  107. - absolute value from EXPR_ABS;
  108. - the conversion from an item to an integer (possible only for items
  109. of numeric or ordered groups) with EXPR_ITEM.
  110. The constraints on couple take 2 items and the item expression take
  111. one item. In any of these cases, items can be or true items (ITEM_ITEM)
  112. or variable items (ITEM_VAR). A variable is attached to a group and can
  113. take any value into it.
  114. The description is built by putting constraints into the the constraint
  115. set. The constraints set records the constraint in several groups of
  116. bound constraints. Two constraints are bound together if they share the
  117. same variable. The class ITEM_COLLECTOR serves the purpose of enumerating
  118. the items of a constraint.
  119. Such a binding relation define equivalent classes that are
  120. used to group the constraints together into CONSTRAINT_GROUP.
  121. At the end of the description the constraint set contains
  122. - an unbound constraint group that does not depend on any variables;
  123. - a list of constraint groups that have variables such that any
  124. pair of group in the list have a separate set of variables.
  125. Exercice: add some new logical operators like and, or, ...
  126. Transformation of the description
  127. ---------------------------------
  128. In that step, the constraints are transformed to a AND/OR
  129. tree of the possible permutations.
  130. The possible permutations are recorded using a BIT_STRING.
  131. Here is how.
  132. Let get two groups: A and X.
  133. The group A is made of the item a, b, c.
  134. The group X is made of the item x, y, z.
  135. The possible permutations from A and X are
  136. listed below:
  137. {{{{
  138. +-----+-----------+---------+
  139. | A | a | b | c | number |
  140. +-----+---+---+---+---------+
  141. | | x | y | z | 0 |
  142. | | x | z | y | 1 |
  143. | X | y | x | z | 2 |
  144. | | y | z | x | 3 |
  145. | | z | x | y | 4 |
  146. | | z | y | x | 5 |
  147. +-----+---+---+---+---------+
  148. }}}}
  149. Each of these permutation have received a number that identifies
  150. it. That number is used for the BIT_STRING indexes.
  151. For example, the possible permutations where b is associated
  152. with z are the ones of number 1 and 3 then the corresponding
  153. bit string value is:
  154. {{{{
  155. index: 0 1 2 3 4 5
  156. value: 0 1 0 1 0 0
  157. }}}}
  158. For example, the possible permutations where c is not associated
  159. with y are the ones of number 0, 2, 3, 5 and 3 then the corresponding
  160. bit string value is:
  161. {{{{
  162. index: 0 1 2 3 4 5
  163. value: 1 0 1 1 0 1
  164. }}}}
  165. So if the problem is to find how to arrange A with X in a such
  166. way that b is with z and c is not with y, a sample or between the
  167. possible combinations gives the solution:
  168. {{{{
  169. index: 0 1 2 3 4 5
  170. (P1) b with x: 0 1 0 1 0 0
  171. (P2) c not with y: 1 0 1 1 0 1
  172. -------------
  173. (P1) and (P2): 0 0 0 1 0 0
  174. }}}}
  175. The solution is permutation 3: a with y, b with z, c with x.
  176. For N groups, the program manages (N * (N-1))/2 pair of
  177. possible permutations.
  178. The AND/OR tree is created by CONSTRAINT_SET that simply make
  179. a and of the sub trees created by each of the group of constraint
  180. CONSTRAINT_GROUP it contains.
  181. The CONSTRAINT_GROUP enumerate all possible combination of
  182. the variables and when a combination is consistent for the
  183. set of logical constraints, it generates a AND list of the
  184. possible permutations that the combination represent.
  185. The result is a OR of all the detected possibilities.
  186. The masks are built by using an instance of MASK_BUILDER.
  187. Exercice: explain how permutations are numbered.
  188. Exercice: try to improve the time used for the transformation by
  189. challenging the variables before each invocation of 'get_node'
  190. in 'get_node_of_var', class CONSTRAINT_GROUP. Trick: add a deferred
  191. feature 'can_challenge' in CONSTRAINT_LOGICAL.
  192. Exploration of the solutions
  193. ----------------------------
  194. During this step, the possible combinations of the AND/OR tre are
  195. enumerated using the BACKTRACKING behaviors. When a solution is
  196. possible, it is checked to see if it is consistent. In effect,
  197. it is not possible to detect all impossibilities during the
  198. exploration.
  199. The class SITUATION is used to do all that stuff.
  200. Exercice: try to improve the checking of the consistency of the
  201. presumed solutions. You wil find it in class SITUATION, the feature
  202. is 'try_solution'.
  203. .def [[:upper:]_]\{2,\} <span class="class">&</span>
  204. .def {{{{ <pre>
  205. .def }}}} </pre>
  206. .def ^Exercice: <span class="exercice">&</span>
  207. .sty .exercice { color: green; font: small-caps bold; }