/tutorial/backtracking/logigram/logigram.html

http://github.com/tybor/Liberty · HTML · 378 lines · 360 code · 18 blank · 0 comment · 0 complexity · 83b1a6195595d1a1fb701925b4e7b813 MD5 · raw file

  1. <html><head>
  2. <title>The tutorial logigram</title>
  3. <style>
  4. body {
  5. background-color: white;
  6. margin: 3px;
  7. }
  8. body * {
  9. /*
  10. margin: 0px 0px 5px 0px;
  11. */
  12. }
  13. h1 {
  14. background-color: #3366FF;
  15. font-size: 300%;
  16. color: white;
  17. text-align: center;
  18. font-weight: normal;
  19. padding: 10px;
  20. }
  21. h2 {
  22. background-color: #3366FF;
  23. font-size: 150%;
  24. color: white;
  25. text-align: left;
  26. font-weight: normal;
  27. padding: 5px;
  28. }
  29. .i {
  30. background-color: yellow;
  31. font-size: 110%;
  32. color: #3366FF;
  33. text-align: center;
  34. font-weight: bolder;
  35. padding: 10px 30px 10px 30px;
  36. }
  37. p {
  38. /*
  39. text-align: justify;
  40. */
  41. }
  42. pre {
  43. margin: 3px 50px 3px 50px;
  44. padding: 3px 5px 3px 5px;
  45. border: 2px dashed #3366FF;
  46. background: #eef;
  47. }
  48. .class {
  49. font-weight: bolder;
  50. color: #3366FF;
  51. }
  52. .sesign {
  53. text-align: center;
  54. font-size: small;
  55. font-style: italic;
  56. }
  57. .exercice { color: green; font: small-caps bold; }
  58. </style>
  59. </head><body>
  60. <h1>The tutorial logigram</h1>
  61. <div class="i">
  62. <p>This tutorial shows a typical use of the backtracking cluster.</p></div>
  63. <a name="sommaire"/><h2>Sommaire</h2>
  64. <div class="s"><ul class="s">
  65. <li><a href="#chap-1">How to compile?</a></li>
  66. <li><a href="#chap-2">What does it do?</a></li>
  67. <li><a href="#chap-3">How does it work?</a></li>
  68. <ul class="s">
  69. <li><a href="#chap-3.1">Description of the problem</a></li>
  70. <li><a href="#chap-3.2">Transformation of the description</a></li>
  71. <li><a href="#chap-3.3">Exploration of the solutions</a></li>
  72. </ul>
  73. </ul></div>
  74. <a name="chap-1"/><h2>How to compile?</h2>
  75. <div class="c2">
  76. <p>Just type:</p>
  77. <p><pre>
  78. se c -boost -clean -o logigram logigram
  79. </pre></p>
  80. </div>
  81. <a name="chap-2"/><h2>What does it do?</h2>
  82. <div class="c2">
  83. <p>That tutorial that shows how to solve problems
  84. sometimes called logigrams. The logigrams
  85. are made of a set of items (persons, date, places, ...)
  86. grouped into categories and set of true propositions
  87. about the items. From these propositions you must deduce
  88. how the given items are combined together.</p>
  89. <p>Here is an example:</p>
  90. <p>That program solves the following classic problem.</p>
  91. <p>Knowing that:</p>
  92. <ul class="hyphen">
  93. <li>
  94. <p>the house of the english is red,</p>
  95. </li>
  96. <li>
  97. <p>the spanish has a dog,</p>
  98. </li>
  99. <li>
  100. <p>one drink coffee in the green house,</p>
  101. </li>
  102. <li>
  103. <p>the ukrainian drinks tea,</p>
  104. </li>
  105. <li>
  106. <p>the green house is just at right of the ivory house,</p>
  107. </li>
  108. <li>
  109. <p>the man that smokes winstons have a snail,</p>
  110. </li>
  111. <li>
  112. <p>the man that smokes kools have the yellow house,</p>
  113. </li>
  114. <li>
  115. <p>one drinks milk in the house at the middle,</p>
  116. </li>
  117. <li>
  118. <p>the norvegian lives in the house at left,</p>
  119. </li>
  120. <li>
  121. <p>the one who smokes chesterfields is neibourgh of a fox,</p>
  122. </li>
  123. <li>
  124. <p>the one who smokes kools is neibourgh of a horse,</p>
  125. </li>
  126. <li>
  127. <p>the one who smokes luckystrike drinks orange juice,</p>
  128. </li>
  129. <li>
  130. <p>the japanese smokes parliaments,</p>
  131. </li>
  132. <li>
  133. <p>the norvegian is neibourgh of the blue house.</p>
  134. </li>
  135. </ul>
  136. <p>Tell who got the zebra and who drinks water?</p>
  137. <p>The output of the program is
  138. <pre>
  139. > logigram</p>
  140. <p>+-----------+-------------+--------+--------------+---------------+--------+
  141. | house | nationality | animal | drink | cigarette | color |
  142. +-----------+-------------+--------+--------------+---------------+--------+
  143. | left | norvegian | fox | water | kools | yellow |
  144. | mid-left | ukrainian | horse | tea | chesterfields | blue |
  145. | middle | english | snail | milk | winston | red |
  146. | mid-right | spanish | dog | orange juice | luckystrike | ivory |
  147. | right | japanese | zebra | coffee | parliaments | green |
  148. +-----------+-------------+--------+--------------+---------------+--------+</p>
  149. <p>1 solution</p>
  150. <p></pre></p>
  151. <p>There are three other problems that let you challenge the
  152. tutorial.</p>
  153. <p><span class="exercice">Exercice:</span> in file logigram.e, feature describe_problem_classic put
  154. in comment line that declares that the house of the english is red
  155. as below and re-run. How many solutions now? Happy chrismas!
  156. <pre>
  157. -- rule(yes(item("nationality", "english"), item("color", "red")))
  158. </pre></p>
  159. <p><span class="exercice">Exercice:</span> in file logigram.e, feature describe_problem_classic put
  160. line that declares the ordered group house at the end of the
  161. groups declarations and measure the difference of computing time
  162. with the command 'time' (under unix). Explain.</p>
  163. <p><span class="exercice">Exercice:</span> write a program that solves the same problem.</p>
  164. <p><span class="exercice">Exercice:</span> write a program that solves any problem of the same kind.</p>
  165. </div>
  166. <a name="chap-3"/><h2>How does it work?</h2>
  167. <div class="c2">
  168. <p>It works in three steps:</p>
  169. <ul class="hyphen">
  170. <li>
  171. <p>Creation of the problem description.</p>
  172. </li>
  173. <li>
  174. <p>Transformation of the description to a <span class="class">AND</span>/<span class="class">OR</span>
  175. tree of possible permutations.</p>
  176. </li>
  177. <li>
  178. <p>Exploration of the <span class="class">AND</span>/<span class="class">OR</span> tree by backtracking to
  179. retrieve the solutions.</p>
  180. </li>
  181. </ul>
  182. <p>The main idea is to use permutations for retrieving the solutions.</p>
  183. <a name="chap-3.1"/><h3>Description of the problem</h3>
  184. <div class="c3">
  185. <p>The description is managed with an object of the class <span class="class">DESCRIPTION</span>
  186. that mainly contains:</p>
  187. <ul class="hyphen">
  188. <li>
  189. <p>a set of groups;</p>
  190. </li>
  191. <li>
  192. <p>a set of constraints through an object of class <span class="class">CONSTRAINT_SET</span>.</p>
  193. </li>
  194. </ul>
  195. <p>First of all, the groups must be declared. There are 3 kind of
  196. groups:</p>
  197. <ul class="hyphen">
  198. <li>
  199. <p>the atomic groups;</p>
  200. </li>
  201. <li>
  202. <p>the ordered groups what means that the order of the items
  203. of the group cares and that each item receive a number that
  204. is its place, beginning to zero;</p>
  205. </li>
  206. <li>
  207. <p>the numeric groups that must contain numeric items.</p>
  208. </li>
  209. </ul>
  210. <p>The groups are all managed through objects of class <span class="class">GROUP</span>.</p>
  211. <p>The constraints (class <span class="class">CONSTRAINT</span>) are distinguished in two
  212. types: </p>
  213. <ul class="hyphen">
  214. <li>
  215. <p>Constraints on couples (class <span class="class">CONSTRAINT_COUPLE</span> association of a
  216. couple of two items that are not of the same group), that comprises:</p>
  217. </li>
  218. <ul class="hyphen">
  219. <li>
  220. <p>positive association of a couple (class <span class="class">CONSTRAINT_YES</span>) what meaning
  221. is that the 2 items are associated together (example: marie had
  222. 4 children);</p>
  223. </li>
  224. <li>
  225. <p>negative association of a couple (class <span class="class">CONSTRAINT_NO</span>) what meaning
  226. is that the 2 items are never associated together (example: marie
  227. didn't have 4 children).</p>
  228. </li>
  229. </ul>
  230. </li>
  231. <li>
  232. <p>Logical constraints (class <span class="class">CONSTRAINT_LOGICAL</span>) that currently only are
  233. the relationnal constraints (class <span class="class">CONSTRAINT_RELATIONAL</span>) on some integer
  234. expressions, that comprises equal, greater, lesser, and
  235. not equal, from the classes <span class="class">CONSTRAINT_EQUAL</span>, <span class="class">CONSTRAINT_GREATER</span>,
  236. <span class="class">CONSTRAINT_LESSER</span>, <span class="class">CONSTRAINT_NOT_EQUAL</span>.</p>
  237. </li>
  238. </ul>
  239. <p>The relational constraints are on expressions that are built using
  240. inheriters of class <span class="class">EXPR</span>, say:</p>
  241. <ul class="hyphen">
  242. <li>
  243. <p>constants from <span class="class">EXPR_VALUE</span>;</p>
  244. </li>
  245. <li>
  246. <p>addition, substraction, multiplication from <span class="class">EXPR_ADD</span>, <span class="class">EXPR_SUB</span>
  247. and <span class="class">EXPR_MUL</span>;</p>
  248. </li>
  249. <li>
  250. <p>absolute value from <span class="class">EXPR_ABS</span>;</p>
  251. </li>
  252. <li>
  253. <p>the conversion from an item to an integer (possible only for items
  254. of numeric or ordered groups) with <span class="class">EXPR_ITEM</span>.</p>
  255. </li>
  256. </ul>
  257. <p>The constraints on couple take 2 items and the item expression take
  258. one item. In any of these cases, items can be or true items (<span class="class">ITEM_ITEM</span>)
  259. or variable items (<span class="class">ITEM_VAR</span>). A variable is attached to a group and can
  260. take any value into it.</p>
  261. <p>The description is built by putting constraints into the the constraint
  262. set. The constraints set records the constraint in several groups of
  263. bound constraints. Two constraints are bound together if they share the
  264. same variable. The class <span class="class">ITEM_COLLECTOR</span> serves the purpose of enumerating
  265. the items of a constraint.</p>
  266. <p>Such a binding relation define equivalent classes that are
  267. used to group the constraints together into <span class="class">CONSTRAINT_GROUP</span>.
  268. At the end of the description the constraint set contains</p>
  269. <ul class="hyphen">
  270. <li>
  271. <p>an unbound constraint group that does not depend on any variables;</p>
  272. </li>
  273. <li>
  274. <p>a list of constraint groups that have variables such that any
  275. pair of group in the list have a separate set of variables.</p>
  276. </li>
  277. </ul>
  278. <p><span class="exercice">Exercice:</span> add some new logical operators like and, or, ...</p>
  279. </div>
  280. <a name="chap-3.2"/><h3>Transformation of the description</h3>
  281. <div class="c3">
  282. <p>In that step, the constraints are transformed to a <span class="class">AND</span>/<span class="class">OR</span>
  283. tree of the possible permutations.</p>
  284. <p>The possible permutations are recorded using a <span class="class">BIT_STRING</span>.
  285. Here is how.</p>
  286. <p>Let get two groups: A and X.
  287. The group A is made of the item a, b, c.
  288. The group X is made of the item x, y, z.
  289. The possible permutations from A and X are
  290. listed below:
  291. <pre>
  292. +-----+-----------+---------+
  293. | A | a | b | c | number |
  294. +-----+---+---+---+---------+
  295. | | x | y | z | 0 |
  296. | | x | z | y | 1 |
  297. | X | y | x | z | 2 |
  298. | | y | z | x | 3 |
  299. | | z | x | y | 4 |
  300. | | z | y | x | 5 |
  301. +-----+---+---+---+---------+
  302. </pre>
  303. Each of these permutation have received a number that identifies
  304. it. That number is used for the <span class="class">BIT_STRING</span> indexes.</p>
  305. <p>For example, the possible permutations where b is associated
  306. with z are the ones of number 1 and 3 then the corresponding
  307. bit string value is:
  308. <pre>
  309. index: 0 1 2 3 4 5
  310. value: 0 1 0 1 0 0
  311. </pre></p>
  312. <p>For example, the possible permutations where c is not associated
  313. with y are the ones of number 0, 2, 3, 5 and 3 then the corresponding
  314. bit string value is:
  315. <pre>
  316. index: 0 1 2 3 4 5
  317. value: 1 0 1 1 0 1
  318. </pre></p>
  319. <p>So if the problem is to find how to arrange A with X in a such
  320. way that b is with z and c is not with y, a sample or between the
  321. possible combinations gives the solution:
  322. <pre>
  323. index: 0 1 2 3 4 5</p>
  324. <p>(P1) b with x: 0 1 0 1 0 0
  325. (P2) c not with y: 1 0 1 1 0 1
  326. -------------
  327. (P1) and (P2): 0 0 0 1 0 0
  328. </pre>
  329. The solution is permutation 3: a with y, b with z, c with x.</p>
  330. <p>For N groups, the program manages (N * (N-1))/2 pair of
  331. possible permutations.</p>
  332. <p>The <span class="class">AND</span>/<span class="class">OR</span> tree is created by <span class="class">CONSTRAINT_SET</span> that simply make
  333. a and of the sub trees created by each of the group of constraint
  334. <span class="class">CONSTRAINT_GROUP</span> it contains.</p>
  335. <p>The <span class="class">CONSTRAINT_GROUP</span> enumerate all possible combination of
  336. the variables and when a combination is consistent for the
  337. set of logical constraints, it generates a <span class="class">AND</span> list of the
  338. possible permutations that the combination represent.
  339. The result is a <span class="class">OR</span> of all the detected possibilities.</p>
  340. <p>The masks are built by using an instance of <span class="class">MASK_BUILDER</span>.</p>
  341. <p><span class="exercice">Exercice:</span> explain how permutations are numbered.</p>
  342. <p><span class="exercice">Exercice:</span> try to improve the time used for the transformation by
  343. challenging the variables before each invocation of 'get_node'
  344. in 'get_node_of_var', class <span class="class">CONSTRAINT_GROUP</span>. Trick: add a deferred
  345. feature 'can_challenge' in <span class="class">CONSTRAINT_LOGICAL</span>.</p>
  346. </div>
  347. <a name="chap-3.3"/><h3>Exploration of the solutions</h3>
  348. <div class="c3">
  349. <p>During this step, the possible combinations of the <span class="class">AND</span>/<span class="class">OR</span> tre are
  350. enumerated using the <span class="class">BACKTRACKING</span> behaviors. When a solution is
  351. possible, it is checked to see if it is consistent. In effect,
  352. it is not possible to detect all impossibilities during the
  353. exploration.</p>
  354. <p>The class <span class="class">SITUATION</span> is used to do all that stuff.</p>
  355. <p><span class="exercice">Exercice:</span> try to improve the checking of the consistency of the
  356. presumed solutions. You wil find it in class <span class="class">SITUATION</span>, the feature
  357. is 'try_solution'.</p>
  358. </div>
  359. </div>
  360. </body></html>