goal state encoder added
[mc1516pa.git] / modelchecker / main.c
1 #include <stdio.h>
2 #include <stdbool.h>
3 #include <ctype.h>
4 #include <time.h>
5 #include <unistd.h>
6
7 #include <sylvan.h>
8
9 #include "sokoban.h"
10 #include "coord.h"
11 #include "object.h"
12
13 #define ERRPRINT(fmt, as...) fprintf(stderr, fmt, ## as);
14 #define DPRINT(fmt, as...) if(DEBUG) ERRPRINT(fmt, ## as);
15
16 typedef enum {OBJECT, COORD, HYBRID} strategy;
17
18 //Global variables
19 bool DEBUG = false;
20 strategy strat = HYBRID;
21
22 void usage(char *prg)
23 {
24 ERRPRINT("Usage:\n"
25 "\t%s [opts] [FILE [FILE [...]]]\n"
26 "\n"
27 "Options:\n"
28 "\tAll strategies are mutually exclusive\n"
29 "\t-c coordinate based strategy\n"
30 "\t-o object based strategy\n"
31 "\t-y hybrid strategy\n"
32 // "\t-l LURD lURD verification strategy\n"
33 // "\t-r show all positions that are a valid solution\n"
34 "\n"
35 "\t-d enable verbose debug output\n"
36 "\t-h show this help\n"
37 "\n"
38 "Positional arguments:\n"
39 "\tFILE zero or more sokoban screens\n"
40 "\t when no file is specified stdin will be used\n", prg);
41 }
42
43 void solve(FILE *inputstream)
44 {
45 clock_t time_start_read, time_end_read;
46 clock_t time_start_encode, time_end_encode;
47
48 time_start_read = clock();
49 sokoban_screen *screen = parse_screen(inputstream, false);
50 if (screen == NULL) printf("Something went wrong...\n");
51 //sokoban_print(screen);
52 time_end_read = clock();
53
54 time_start_encode = clock();
55
56 lace_init(0, 1000000);
57 lace_startup(0, NULL, NULL);
58 LACE_ME;
59 sylvan_init_package(1LL<<21, 1LL<<27, 1LL<<20, 1LL<<26);
60 sylvan_init_bdd(6);
61
62 state *init = encode_screen(screen);
63 rels *rls = encode_rel(screen);
64 /*
65 printf("Doing left\n");
66 test_trans(init, rls->rell);
67 printf("Doing up\n");
68 test_trans(init, rls->relu);
69 printf("Doing right\n");
70 test_trans(init, rls->relr);
71 printf("Doing down\n");
72 test_trans(init, rls->reld);
73 */
74
75 BDD old = sylvan_false;
76 BDD new = init->bdd;
77 int iteration = 0;
78 while(new != old){
79 old = new;
80 ERRPRINT("Iteration %d\n", iteration++);
81 trans_t *t = rls->rell;
82
83 //for testing
84 /*
85 BDDSET testvars = sylvan_set_fromarray(((BDDVAR[]){0,2,4,6,8,10,12,14,16}), 9);
86 BDD teststate = sylvan_cube(testvars, (uint8_t[]){1,0,1,0,0,1,0,1,0});
87 BDD teststate1 = sylvan_or(teststate, sylvan_cube(testvars, (uint8_t[]){0,0,1,1,0,1,0,1,0}));
88 BDD teststate2 = sylvan_or(teststate1, sylvan_cube(testvars, (uint8_t[]){0,1,0,0,0,1,1,0,1}));
89 if (new == teststate2) printf("Wrong!\n");
90 */
91 while (t != NULL){
92 new = sylvan_or(new, sylvan_relnext(new, t->bdd, t->varset.varset));
93 t = t->next_rel;
94 //ERRPRINT("Satcount: %f\n", sylvan_satcount(new, init->vars.varset));
95 }
96
97 t = rls->relu;
98 while (t != NULL){
99 new = sylvan_or(new, sylvan_relnext(new, t->bdd, t->varset.varset));
100 t = t->next_rel;
101 //ERRPRINT("Satcount: %f\n", sylvan_satcount(new, init->vars.varset));
102 }
103
104
105 t = rls->relr;
106 while (t != NULL){
107 new = sylvan_or(new, sylvan_relnext(new, t->bdd, t->varset.varset));
108 t = t->next_rel;
109 //ERRPRINT("Satcount: %f\n", sylvan_satcount(new, init->vars.varset));
110 }
111
112
113 t = rls->reld;
114 while (t != NULL){
115 new = sylvan_or(new, sylvan_relnext(new, t->bdd, t->varset.varset));
116 t = t->next_rel;
117 //ERRPRINT("Satcount: %f\n", sylvan_satcount(new, init->vars.varset));
118 }
119
120 }
121 ERRPRINT("Satcount: %f\n", sylvan_satcount(old, init->vars.varset));
122 // sylvan_enum(old, init->vars.varset, (enum_cb)callback, NULL);
123 // sylvan_printdot_nc(old);
124 time_end_encode = clock();
125
126 //SOLVE???
127
128 sokoban_free(screen);
129 ERRPRINT("Reading: %fs\n",
130 ((double) (time_end_read-time_start_read))/CLOCKS_PER_SEC);
131 ERRPRINT("Encoding: %fs\n",
132 ((double) (time_end_encode-time_start_encode))/CLOCKS_PER_SEC);
133 }
134
135 int main(int argc, char **argv)
136 {
137 int optchar;
138
139 while((optchar = getopt(argc, argv, "cdhoy")) != -1){
140 switch(optchar){
141 case 'c':
142 strat = COORD;
143 DPRINT("Strategy changed to Coordinate based\n");
144 break;
145 case 'd':
146 DEBUG = true;
147 DPRINT("Debug enabled\n");
148 break;
149 case 'h':
150 usage(argv[0]);
151 return 0;
152 case 'o':
153 strat = OBJECT;
154 DPRINT("Strategy changed to Object based\n");
155 break;
156 case 'y':
157 strat = HYBRID;
158 DPRINT("Strategy changed to Hybrid\n");
159 break;
160 case '?':
161 if(isprint(optopt)){
162 ERRPRINT("Unknown option `-%c'.\n", optopt);
163 } else {
164 ERRPRINT("Unknown option char `-\\x%x'.\n", optopt);
165 }
166 return 2;
167 default:
168 break;
169 }
170 }
171
172 if(optind == argc){
173 ERRPRINT("You have not specified a file, reading from stdin\n");
174 solve(stdin);
175 }
176
177 for(int filepathindex = optind; filepathindex < argc; filepathindex++){
178 char *currentfilepath = argv[filepathindex];
179 ERRPRINT("Processing: %s\n", currentfilepath);
180 FILE *currentfile = fopen(currentfilepath, "r");
181 DPRINT("Opening file\n");
182 solve(currentfile);
183 DPRINT("Closing file\n");
184 fclose(currentfile);
185 }
186 return 0;
187 }