PageRenderTime 18ms CodeModel.GetById 1ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

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