model checker fix
[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);
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 BDD old = sylvan_false;
66 BDD new = init->bdd;
67 int iteration = 0;
68 while(new != old){
69 ERRPRINT("Iteration %d\n", iteration++);
70 old = new;
71 trans_t *t = rls->rell;
72 while (t != NULL){
73 new = sylvan_or(new, sylvan_relnext(new, t->bdd, t->varset.varset));
74 t = t->next_rel;
75 }
76 t = rls->relu;
77 while (t != NULL){
78 new = sylvan_or(new, sylvan_relnext(new, t->bdd, t->varset.varset));
79 t = t->next_rel;
80 }
81 t = rls->relr;
82 while (t != NULL){
83 new = sylvan_or(new, sylvan_relnext(new, t->bdd, t->varset.varset));
84 t = t->next_rel;
85 }
86 t = rls->reld;
87 while (t != NULL){
88 new = sylvan_or(new, sylvan_relnext(new, t->bdd, t->varset.varset));
89 t = t->next_rel;
90 }
91 }
92 //sylvan_printdot_nc(old);
93 //switch(strat){
94 // case COORD:
95 // DPRINT("Encoding coordinate based\n");
96 // break;
97 // case OBJECT:
98 // DPRINT("Encoding object based\n");
99 // solve_object(screen);
100 // break;
101 // case HYBRID:
102 // DPRINT("Encoding hybrid based\n");
103 // DPRINT("Not implemented yet...\n");
104 // break;
105 // default:
106 // ERRPRINT("Huh?");
107 // exit(2);
108 //}
109 time_end_encode = clock();
110
111 //SOLVE???
112
113 sokoban_free(screen);
114 ERRPRINT("Reading: %fs\n",
115 ((double) (time_end_read-time_start_read))/CLOCKS_PER_SEC);
116 ERRPRINT("Encoding: %fs\n",
117 ((double) (time_end_encode-time_start_encode))/CLOCKS_PER_SEC);
118 }
119
120 int main(int argc, char **argv)
121 {
122 int optchar;
123
124 while((optchar = getopt(argc, argv, "cdhoy")) != -1){
125 switch(optchar){
126 case 'c':
127 strat = COORD;
128 DPRINT("Strategy changed to Coordinate based\n");
129 break;
130 case 'd':
131 DEBUG = true;
132 DPRINT("Debug enabled\n");
133 break;
134 case 'h':
135 usage(argv[0]);
136 return 0;
137 case 'o':
138 strat = OBJECT;
139 DPRINT("Strategy changed to Object based\n");
140 break;
141 case 'y':
142 strat = HYBRID;
143 DPRINT("Strategy changed to Hybrid\n");
144 break;
145 case '?':
146 if(isprint(optopt)){
147 ERRPRINT("Unknown option `-%c'.\n", optopt);
148 } else {
149 ERRPRINT("Unknown option char `-\\x%x'.\n", optopt);
150 }
151 return 2;
152 default:
153 break;
154 }
155 }
156
157 if(optind == argc){
158 ERRPRINT("You have not specified a file, reading from stdin\n");
159 solve(stdin);
160 }
161
162 for(int filepathindex = optind; filepathindex < argc; filepathindex++){
163 char *currentfilepath = argv[filepathindex];
164 ERRPRINT("Processing: %s\n", currentfilepath);
165 FILE *currentfile = fopen(currentfilepath, "r");
166 DPRINT("Opening file\n");
167 solve(currentfile);
168 DPRINT("Closing file\n");
169 fclose(currentfile);
170 }
171 return 0;
172 }