#### /tutorial/backtracking/logigram/logigram.txt

Plain Text | 262 lines | 207 code | 55 blank | 0 comment | 0 complexity | d73e29581ed16af0e81f4790a9dff648 MD5 | raw file

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