f6e22340bb2e0ebde55d3b223d25c73b0d1798e5
10 #include <gperftools/profiler.h>
21 * Global TODOs for now:
22 * - update the helper maps so it is a single bimap
23 * - make a data structure for relations
24 * - encode relations as a set of four relations for each move
53 xy_bddvar_map
*getxy(int x
, int y
, xy_bddvar_map
*map
)
55 xy_bddvar_map k
, *r
= NULL
;
56 memset(&k
, 0, sizeof(xy_bddvar_map
));
59 HASH_FIND(hh
, map
, &k
.key
, sizeof(xy
), r
);
63 bddvar_xy_map
*getbdd(int key
, bddvar_xy_map
*map
)
65 bddvar_xy_map k
, *r
= NULL
;
66 memset(&k
, 0, sizeof(bddvar_xy_map
));
68 HASH_FIND(hh
, map
, &k
.key
, sizeof(int), r
);
72 xy_bddvar_map
*create_xy_bddvar_map(sokoban_screen
*screen
)
76 xy_bddvar_map
*xybdd
= NULL
;
77 for(r
=screen
; r
!= NULL
; r
= (sokoban_screen
*)(r
->hh
.next
)){
78 xy_bddvar_map
*f
= NULL
;
79 //bddvar_xy_map *t = NULL;
81 f
= (xy_bddvar_map
*)malloc(sizeof(xy_bddvar_map
));
82 memset(f
, 0, sizeof(xy_bddvar_map
));
83 f
->key
.x
= r
->coord
.x
;
84 f
->key
.y
= r
->coord
.y
;
85 printf("test!: %d %d\n", r
->coord
.x
, r
->coord
.y
);
86 f
->value
.var
[0] = varcount
;
87 f
->value
.var
[1] = varcount
+ 1;
88 f
->value
.var
[2] = varcount
+ 2;
89 HASH_ADD(hh
, xybdd
, key
, sizeof(xy
), f
);
90 varcount
= varcount
+ 3;
95 bddvar_xy_map
*create_bddvar_xy_map(sokoban_screen
*screen
)
99 bddvar_xy_map
*bddxy
= NULL
;
100 for(r
=screen
; r
!= NULL
; r
= (sokoban_screen
*)(r
->hh
.next
)){
101 for (int i
= 0; i
<3; i
++){
102 bddvar_xy_map
*f
= NULL
;
103 //bddvar_xy_map *t = NULL;
105 f
= (bddvar_xy_map
*)malloc(sizeof(bddvar_xy_map
));
106 memset(f
, 0, sizeof(bddvar_xy_map
));
107 f
->key
= varcount
+ i
;
108 f
->value
.x
= r
->coord
.x
;
109 f
->value
.y
= r
->coord
.y
;
110 HASH_ADD(hh
, bddxy
, key
, sizeof(int), f
);
112 varcount
= varcount
+ 3;
118 * Each coordinate has three related boolean variables. The combination of those boolean variables
126 * 110: Agent on target
127 * In the BDD representation, the state is represented by n * 3 variables, where n is the number of
128 * tiles in the shrinked screen.
129 * It seems that the move variable is not necessary since non-deterministic moves can be emvedded
130 * directly in transition relations.
133 BDD
encode_screen(sokoban_screen
*screen
)
135 BDD state
= sylvan_false
;
139 for(r
=screen
; r
!= NULL
; r
= (sokoban_screen
*)(r
->hh
.next
)){
142 if (state
== sylvan_false
){
143 state
= sylvan_not(sylvan_ithvar(tile_index
));
145 state
= sylvan_and(state
, sylvan_not(sylvan_ithvar(tile_index
)));
147 state
= sylvan_and(state
, sylvan_ithvar(tile_index
));
151 state
= sylvan_and(state
, sylvan_not(sylvan_ithvar(tile_index
)));
153 state
= sylvan_and(state
, sylvan_not(sylvan_ithvar(tile_index
)));
155 state
= sylvan_and(state
, sylvan_ithvar(tile_index
));
160 if (state
== sylvan_false
){
161 state
= sylvan_not(sylvan_ithvar(tile_index
));
163 state
= sylvan_and(state
, sylvan_not(sylvan_ithvar(tile_index
)));
165 state
= sylvan_and(state
, sylvan_not(sylvan_ithvar(tile_index
)));
169 state
= sylvan_and(state
, sylvan_not(sylvan_ithvar(tile_index
)));
171 state
= sylvan_and(state
, sylvan_not(sylvan_ithvar(tile_index
)));
173 state
= sylvan_and(state
, sylvan_not(sylvan_ithvar(tile_index
)));
178 if (state
== sylvan_false
){
179 state
= sylvan_not(sylvan_ithvar(tile_index
));
181 state
= sylvan_and(state
, sylvan_ithvar(tile_index
));
183 state
= sylvan_and(state
, sylvan_not(sylvan_ithvar(tile_index
)));
187 state
= sylvan_and(state
, sylvan_not(sylvan_ithvar(tile_index
)));
189 state
= sylvan_and(state
, sylvan_ithvar(tile_index
));
191 state
= sylvan_and(state
, sylvan_not(sylvan_ithvar(tile_index
)));
196 if (state
== sylvan_false
){
197 state
= sylvan_not(sylvan_ithvar(tile_index
));
199 state
= sylvan_and(state
, sylvan_ithvar(tile_index
));
201 state
= sylvan_and(state
, sylvan_ithvar(tile_index
));
205 state
= sylvan_and(state
, sylvan_not(sylvan_ithvar(tile_index
)));
207 state
= sylvan_and(state
, sylvan_ithvar(tile_index
));
209 state
= sylvan_and(state
, sylvan_ithvar(tile_index
));
214 if (state
== sylvan_false
){
215 state
= sylvan_ithvar(tile_index
);
217 state
= sylvan_and(state
, sylvan_not(sylvan_ithvar(tile_index
)));
219 state
= sylvan_and(state
, sylvan_ithvar(tile_index
));
223 state
= sylvan_and(state
, sylvan_ithvar(tile_index
));
225 state
= sylvan_and(state
, sylvan_not(sylvan_ithvar(tile_index
)));
227 state
= sylvan_and(state
, sylvan_ithvar(tile_index
));
232 if (state
== sylvan_false
){
233 state
= sylvan_ithvar(tile_index
);
235 state
= sylvan_and(state
, sylvan_ithvar(tile_index
));
237 state
= sylvan_and(state
, sylvan_not(sylvan_ithvar(tile_index
)));
241 state
= sylvan_and(state
, sylvan_ithvar(tile_index
));
243 state
= sylvan_and(state
, sylvan_ithvar(tile_index
));
245 state
= sylvan_and(state
, sylvan_not(sylvan_ithvar(tile_index
)));
250 if (state
== sylvan_false
){
251 state
= sylvan_ithvar(tile_index
);
253 state
= sylvan_and(state
, sylvan_not(sylvan_ithvar(tile_index
)));
255 state
= sylvan_and(state
, sylvan_not(sylvan_ithvar(tile_index
)));
259 state
= sylvan_and(state
, sylvan_ithvar(tile_index
));
261 state
= sylvan_and(state
, sylvan_not(sylvan_ithvar(tile_index
)));
263 state
= sylvan_and(state
, sylvan_not(sylvan_ithvar(tile_index
)));
270 xy_bddvar_map *map = NULL;
271 map = create_xy_bddvar_map(screen);
272 xy_bddvar_map *m = getxy(1, 1, map);
273 bddvar_xy_map *map2 = NULL;
274 map2 = create_bddvar_xy_map(screen);
275 bddvar_xy_map *m2 = getbdd(2, map2);
276 printf("Test1: %d %d\n", m->value.var[0], m->value.var[1]);
277 printf("Test2: %d %d\n", m2->value.x, m2->value.y);
279 printf("%d tiles were encoded\n", tile_index
);
283 BDD
encode_rel(sokoban_screen
*screen
)
286 num_tiles
= HASH_COUNT(screen
);
287 printf("Number of tiles: %d\n", num_tiles
);
296 BDD b
= sylvan_not(a
);
297 if (b
== sylvan_false
){
298 printf("BDD works!\n");
300 printf("BDD does not work!\n");
303 BDD c
= sylvan_ithvar(1);
304 if (sylvan_high(c
) == sylvan_true
&& sylvan_low(c
) == sylvan_false
) printf("VAR works 1\n");
305 if (sylvan_var(c
) == 1) printf("Var works 2\n");