relations skeleton
[mc1516pa.git] / modelchecker / coord.c
index c74c2fe..3c14e56 100644 (file)
@@ -50,6 +50,13 @@ typedef struct {
        bddvar_xy_map *t;
 } bimap;
 
+typedef struct {
+       BDD *rell;
+       BDD *relu;
+       BDD *relr;
+       BDD *reld;
+} rels;
+
 xy_bddvar_map *getxy(int x, int y, xy_bddvar_map *map)
 {
        xy_bddvar_map k, *r = NULL;
@@ -69,6 +76,7 @@ bddvar_xy_map *getbdd(int key, bddvar_xy_map *map)
        return r;
 }
 
+/*
 xy_bddvar_map *create_xy_bddvar_map(sokoban_screen *screen)
 {
        int varcount = 0;
@@ -112,6 +120,7 @@ bddvar_xy_map *create_bddvar_xy_map(sokoban_screen *screen)
        }
        return bddxy;
 }
+*/
 
 bimap *create_bimap_helper(sokoban_screen *screen)
 {
@@ -148,6 +157,24 @@ bimap *create_bimap_helper(sokoban_screen *screen)
        return bm;
 }
 
+int check_xy_exists(int x, int y, bimap *bm)
+{
+       int res = 0;
+       if (getxy(x, y, bm->f) != NULL) res = 1;
+       return res;
+}
+
+int check_space(int x, int y, direction d, int delta, bimap *bm)
+{
+       switch(d){
+       case LEFT: x = x - delta; break;
+       case UP: y = y - delta; break;
+       case RIGHT: x = x + delta; break;
+       case DOWN: y = y + delta; break;
+       }
+       return check_xy_exists(x, y, bm);
+}
+
 /*
  * Each coordinate has three related boolean variables. The combination of those boolean variables
  * defines tiles:
@@ -164,186 +191,231 @@ bimap *create_bimap_helper(sokoban_screen *screen)
  * directly in transition relations.
  */
 
-BDD encode_screen(sokoban_screen *screen)
+state *encode_screen(sokoban_screen *screen)
 {
-       BDD state = sylvan_false;
+       LACE_ME;
+
+       BDDVAR vars[HASH_COUNT(screen) * 3];
+       for (uint8_t i = 0; i < HASH_COUNT(screen) * 3; i++){
+               vars[i] = i * 2;
+       }
+
+       uint8_t st_enc[HASH_COUNT(screen) * 3];
+
+       BDDSET varset = sylvan_set_fromarray(vars, HASH_COUNT(screen) * 3);
+       BDD s;
+       state *fullState = NULL;
+       fullState = (state *)malloc(sizeof(state));
+       fullState->vars.varset = varset;
+       fullState->vars.size = HASH_COUNT(screen) * 3;
        int tile_index = 0;
        sokoban_screen *r;
-       LACE_ME;
        for(r=screen; r != NULL; r = (sokoban_screen *)(r->hh.next)){
                switch(r->tile){
-               case FREE:
-                       if (state == sylvan_false){
-                               state = sylvan_not(sylvan_ithvar(tile_index));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_not(sylvan_ithvar(tile_index)));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_ithvar(tile_index));
-                               tile_index++;
+               case FREE: //001
+                       st_enc[tile_index] = 0;
+                       tile_index++;
+                       st_enc[tile_index] = 0;
+                       tile_index++;
+                       st_enc[tile_index] = 1;
+                       tile_index++;
+                       break;
+               case WALL: //000
+                       st_enc[tile_index] = 0;
+                       tile_index++;
+                       st_enc[tile_index] = 0;
+                       tile_index++;
+                       st_enc[tile_index] = 0;
+                       tile_index++;
+                       break;
+               case BOX: //010
+                       st_enc[tile_index] = 0;
+                       tile_index++;
+                       st_enc[tile_index] = 1;
+                       tile_index++;
+                       st_enc[tile_index] = 0;
+                       tile_index++;
+                       break;
+               case TARGET: //011
+                       st_enc[tile_index] = 0;
+                       tile_index++;
+                       st_enc[tile_index] = 1;
+                       tile_index++;
+                       st_enc[tile_index] = 1;
+                       tile_index++;
+                       break;
+               case AGENT: //101
+                       st_enc[tile_index] = 1;
+                       tile_index++;
+                       st_enc[tile_index] = 0;
+                       tile_index++;
+                       st_enc[tile_index] = 1;
+                       tile_index++;
+                       break;
+               case TARGAGENT: //110
+                       st_enc[tile_index] = 1;
+                       tile_index++;
+                       st_enc[tile_index] = 1;
+                       tile_index++;
+                       st_enc[tile_index] = 0;
+                       tile_index++;
+                       break;
+               case TARGBOX: //100
+                       st_enc[tile_index] = 1;
+                       tile_index++;
+                       st_enc[tile_index] = 0;
+                       tile_index++;
+                       st_enc[tile_index] = 0;
+                       tile_index++;
+                       break;
+               }
+       }
+       s = sylvan_cube(varset, st_enc);
+       fullState->bdd = s;
+       printf("Initial state encoded\n");
+       return fullState;
+}
+
+int
+test_relprod()
+{
+    LACE_ME;
+
+    BDDVAR vars[] = {0,2,4};
+    BDDVAR all_vars[] = {4,5};
+
+    BDDSET vars_set = sylvan_set_fromarray(vars, 3);
+    BDDSET all_vars_set = sylvan_set_fromarray(all_vars, 2);
+
+    BDD s, t, next, prev;
+    BDD zeroes, ones;
+
+    // transition relation: 000 --> 111 and !000 --> 000
+    t = sylvan_false;
+    //t = sylvan_union_cube(t, all_vars_set, ((uint8_t[]){0,1,0,1,0,1}));
+    //t = sylvan_union_cube(t, all_vars_set, ((uint8_t[]){1,0,2,0,2,0}));
+    //t = sylvan_union_cube(t, all_vars_set, ((uint8_t[]){2,0,1,0,2,0}));
+    t = sylvan_union_cube(t, all_vars_set, ((uint8_t[]){1,0}));
+
+    s = sylvan_cube(vars_set, (uint8_t[]){0,0,1});
+    zeroes = sylvan_cube(vars_set, (uint8_t[]){0,0,0});
+    ones = sylvan_cube(vars_set, (uint8_t[]){0,0,1});
+
+    next = sylvan_relnext(s, t, all_vars_set);
+    prev = sylvan_relprev(t, next, all_vars_set);
+    if (next == zeroes) printf("Pass 1\n");
+    if (prev == ones || prev == zeroes) printf("Pass 2\n");
+
+    return 0;
+}
+
+BDD encode_rel(sokoban_screen *screen)
+{
+       int tile_count = 0;
+       BDD t = sylvan_false;
+       BDDVAR *relvars;
+       uint8_t *rel_enc;
+
+       bimap *bm = create_bimap_helper(screen);
+       int x = 0;
+       int y = 0;
+       bddvar_xy_map *bddxy = NULL;
+       tmp_scr *screen = NULL;
+       tmp_scr_d *screen = NULL;
+       tmp_scr_g *screen = NULL;
+
+       //left relation
+       for (unsigned int i = 0; i < HASH_COUNT(screen); i++){
+               printf("i:%d\n", i);
+               bddxy = getbdd(i*3, bm->t);
+               x = bddxy->value.x;
+               y = bddxy->value.y;
+               if (check_space(x, y, LEFT, 1, bm) == 0){
+                       tmp_scr = get_coord(x, y, screen);
+                       if (tmp_scr->tile == AGENT){
+
                        }
-                       else {
-                               state = sylvan_and(state, sylvan_not(sylvan_ithvar(tile_index)));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_not(sylvan_ithvar(tile_index)));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_ithvar(tile_index));
-                               tile_index++;
+                       if (tmp_scr->tile == TARGAGENT){
+
                        }
-                       break;
-               case WALL:
-                       if (state == sylvan_false){
-                               state = sylvan_not(sylvan_ithvar(tile_index));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_not(sylvan_ithvar(tile_index)));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_not(sylvan_ithvar(tile_index)));
-                               tile_index++;
+               }
+               else if (check_space(x, y, LEFT, 1, bm) == 1 && check_space(x, y, LEFT, 2, bm) == 0){
+                       tmp_scr = get_coord(x, y, screen);
+                       tmp_scr_d = get_coord(x - 1, y, screen);
+                       if (tmp_scr->tile == AGENT && tmp_scr_d->tile == FREE){
+                               //can move
                        }
-                       else {
-                               state = sylvan_and(state, sylvan_not(sylvan_ithvar(tile_index)));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_not(sylvan_ithvar(tile_index)));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_not(sylvan_ithvar(tile_index)));
-                               tile_index++;
+                       if (tmp_scr->tile == AGENT && tmp_scr_d->tile == TARGET){
+                               //can move
                        }
-                       break;
-               case BOX:
-                       if (state == sylvan_false){
-                               state = sylvan_not(sylvan_ithvar(tile_index));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_ithvar(tile_index));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_not(sylvan_ithvar(tile_index)));
-                               tile_index++;
+                       if (tmp_scr->tile == TARGAGENT && tmp_scr_d->tile == FREE){
+                               //can move
                        }
-                       else {
-                               state = sylvan_and(state, sylvan_not(sylvan_ithvar(tile_index)));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_ithvar(tile_index));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_not(sylvan_ithvar(tile_index)));
-                               tile_index++;
+                       if (tmp_scr->tile == TARGAGENT && tmp_scr_d->tile == TARGET){
+                               //can move
                        }
-                       break;
-               case TARGET:
-                       if (state == sylvan_false){
-                               state = sylvan_not(sylvan_ithvar(tile_index));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_ithvar(tile_index));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_ithvar(tile_index));
-                               tile_index++;
+                       if (tmp_scr->tile == AGENT && (tmp_scr_d->tile == BOX || tmp_scr_d->tile == TARGBOX)){
+                               //can't move
                        }
-                       else {
-                               state = sylvan_and(state, sylvan_not(sylvan_ithvar(tile_index)));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_ithvar(tile_index));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_ithvar(tile_index));
-                               tile_index++;
+                       if (tmp_scr->tile == TARGAGENT && (tmp_scr_d->tile == BOX || tmp_scr_d->tile == TARGBOX)){
+                               //can't move
                        }
-                       break;
-               case AGENT:
-                       if (state == sylvan_false){
-                               state = sylvan_ithvar(tile_index);
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_not(sylvan_ithvar(tile_index)));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_ithvar(tile_index));
-                               tile_index++;
+               }
+               else if (check_space(x, y, LEFT, 1, bm) == 1 && check_space(x, y, LEFT, 2, bm) == 1){
+                       tmp_scr = get_coord(x, y, screen);
+                       tmp_scr_d = get_coord(x - 1, y, screen);
+                       tmp_scr_g = get_coord(x - 2, y, screen);
+                       if (tmp_scr->tile == AGENT && tmp_scr_d->tile == FREE){
+                               //can move
                        }
-                       else {
-                               state = sylvan_and(state, sylvan_ithvar(tile_index));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_not(sylvan_ithvar(tile_index)));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_ithvar(tile_index));
-                               tile_index++;
+                       if (tmp_scr->tile == AGENT && tmp_scr_d->tile == TARGET){
+                               //can move
                        }
-                       break;
-               case TARGAGENT:
-                       if (state == sylvan_false){
-                               state = sylvan_ithvar(tile_index);
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_ithvar(tile_index));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_not(sylvan_ithvar(tile_index)));
-                               tile_index++;
+                       if (tmp_scr->tile == TARGAGENT && tmp_scr_d->tile == FREE){
+                               //can move
                        }
-                       else {
-                               state = sylvan_and(state, sylvan_ithvar(tile_index));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_ithvar(tile_index));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_not(sylvan_ithvar(tile_index)));
-                               tile_index++;
+                       if (tmp_scr->tile == TARGAGENT && tmp_scr_d->tile == TARGET){
+                               //can move
                        }
-                       break;
-               case TARGBOX:
-                       if (state == sylvan_false){
-                               state = sylvan_ithvar(tile_index);
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_not(sylvan_ithvar(tile_index)));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_not(sylvan_ithvar(tile_index)));
-                               tile_index++;
+                       if (tmp_scr->tile == AGENT && tmp_scr_d-> tile == BOX && (tmp_scr_g->tile == BOX || tmp_scr_g == TARGBOX)){
+                               //can't move
                        }
-                       else {
-                               state = sylvan_and(state, sylvan_ithvar(tile_index));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_not(sylvan_ithvar(tile_index)));
-                               tile_index++;
-                               state = sylvan_and(state, sylvan_not(sylvan_ithvar(tile_index)));
-                               tile_index++;
+                       if (tmp_scr->tile == TARGAGENT && tmp_scr_d-> tile == BOX && (tmp_scr_g->tile == BOX || tmp_scr_g == TARGBOX)){
+                               //can't move
+                       }
+                       if (tmp_scr->tile == AGENT && tmp_scr_d->tile == BOX && tmp_scr_g->tile == FREE){
+                               //can move
+                       }
+                       if (tmp_scr->tile == AGENT && tmp_scr_d->tile == TARGBOX && tmp_scr_g->tile == FREE){
+                               //can move
+                       }
+                       if (tmp_scr->tile == AGENT && tmp_scr_d->tile == BOX && tmp_scr_g->tile == TARGET){
+                               //can move
+                       }
+                       if (tmp_scr->tile == AGENT && tmp_scr_d->tile == TARGBOX && tmp_scr_g->tile == TARGET){
+                               //can move
+                       }
+                       if (tmp_scr->tile == TARGAGENT && tmp_scr_d->tile == BOX && tmp_scr_g->tile == FREE){
+                               //can move
+                       }
+                       if (tmp_scr->tile == TARGAGENT && tmp_scr_d->tile == TARGBOX && tmp_scr_g->tile == FREE){
+                               //can move
+                       }
+                       if (tmp_scr->tile == TARGAGENT && tmp_scr_d->tile == BOX && tmp_scr_g->tile == TARGET){
+                               //can move
+                       }
+                       if (tmp_scr->tile == TARGAGENT && tmp_scr_d->tile == TARGBOX && tmp_scr_g->tile == TARGET){
+                               //can move
                        }
-                       break;
                }
+               printf("x:%d y:%d\n", x, y);
        }
-       /*
-       xy_bddvar_map *map = NULL;
-       map = create_xy_bddvar_map(screen);
-       xy_bddvar_map *m = getxy(1, 1, map);
-       bddvar_xy_map *map2 = NULL;
-       map2 = create_bddvar_xy_map(screen);
-       bddvar_xy_map *m2 = getbdd(2, map2);
-       printf("Test1: %d %d\n", m->value.var[0], m->value.var[1]);
-       printf("Test2: %d %d\n", m2->value.x, m2->value.y);
-       */
-       /*
-       bimap *bm = NULL;
-       bm = create_bimap_helper(screen);
-       xy_bddvar_map *m = getxy(1, 1, bm->f);
-       bddvar_xy_map *m2 = getbdd(2, bm->t);
-       printf("Test1: %d %d\n", m->value.var[0], m->value.var[1]);
-       printf("Test2: %d %d\n", m2->value.x, m2->value.y);
-       printf("%d tiles were encoded\n", tile_index);
-       if (bm != NULL) printf ("WORKS!\n");
-       */
-       return state;
-}
 
-BDD encode_rel(sokoban_screen *screen)
-{
-       int num_tiles;
-       num_tiles = HASH_COUNT(screen);
-       printf("Number of tiles: %d\n", num_tiles);
-       return sylvan_true;
-}
+       //up relation
 
-void test()
-{
+       //right relation
 
-       printf("Test!\n");
-       BDD a = sylvan_true;
-       BDD b = sylvan_not(a);
-       if (b == sylvan_false){
-               printf("BDD works!\n");
-       } else {
-               printf("BDD does not work!\n");
-       }
-       LACE_ME;
-       BDD c = sylvan_ithvar(1);
-       if (sylvan_high(c) == sylvan_true && sylvan_low(c) == sylvan_false) printf("VAR works 1\n");
-       if (sylvan_var(c) == 1) printf("Var works 2\n");
+       //down relation
+
+       return sylvan_true;
 }