the old main is back
[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 DPRINT(fmt, as...) if(VERBOSE) fprintf(stderr, fmt, ## as);
14 #define REPORT(s, end, start) DPRINT(s, ((double)(end-start))/CLOCKS_PER_SEC);
15
16 //Global variables
17 bool VERBOSE = false;
18
19 void usage(char *prg)
20 {
21 printf("Usage:\n"
22 "\t%s [opts] [FILE]\n"
23 "\n"
24 "Options:\n"
25 "\t-l LURD lURD verification strategy\n"
26 "\t-v enable verbose output\n"
27 "\t-h show this help\n"
28 "\n"
29 "Positional arguments:\n"
30 "\tFILE zero or one sokoban screens\n"
31 "\t when no file is specified stdin will be used\n", prg);
32 }
33
34 BDD subsolve(trans_t *t, BDD new){
35 LACE_ME;
36 while (t != NULL){
37 new = sylvan_or(new, sylvan_relnext(new, t->bdd, t->varset.varset));
38 t = t->next_rel;
39 }
40 return new;
41 }
42
43 int solve(FILE *inputstream, char *lurd)
44 {
45 clock_t time_start_read, time_end_read, time_start_scr, time_end_scr,
46 time_start_goal, time_end_goal, time_start_rel, time_end_rel,
47 time_start_solve, time_end_solve;
48
49 //Read screen
50 time_start_read = clock();
51 sokoban_screen *screen = parse_screen(inputstream, false);
52 if (screen == NULL) {
53 printf("Something went wrong encoding the screen\n");
54 return 2;
55 }
56 time_end_read = clock();
57
58 //Lace and sylvan blork
59 lace_init(0, 1000000);
60 lace_startup(0, NULL, NULL);
61 LACE_ME;
62 sylvan_init_package(1LL<<21, 1LL<<27, 1LL<<20, 1LL<<26);
63 sylvan_init_bdd(6);
64
65 //Encode screen
66 time_start_scr = clock();
67 state *init = encode_screen(screen);
68 time_end_scr = clock();
69
70 //Encode goal
71 time_start_goal = clock();
72 state *goal = encode_goal(screen);
73 time_end_goal = clock();
74
75 //Encode transitions
76 time_start_rel = clock();
77 rels *rls = encode_rel(screen);
78 time_end_rel = clock();
79
80 //Actually solve
81 time_start_solve = clock();
82 BDD old = sylvan_false;
83 BDD new = init->bdd;
84 //Do lurd
85 while(*lurd != '\0'){
86 switch(*lurd){
87 case 'l':
88 new = subsolve(rls->rell, new);
89 break;
90 case 'u':
91 new = subsolve(rls->relu, new);
92 break;
93 case 'r':
94 new = subsolve(rls->relr, new);
95 break;
96 case 'd':
97 new = subsolve(rls->reld, new);
98 break;
99 default:
100 printf("Unknown character in lurd: '%c'\n", *lurd);
101 exit(2);
102 }
103 lurd++;
104 }
105 int iteration = 0;
106
107 bool found = false;
108
109 while(new != old){
110 DPRINT("Iteration %d\n", iteration++);
111 old = new;
112 if(sylvan_satcount(sylvan_and(goal->bdd, new), init->vars.varset) > 0){
113 found = true;
114 break;
115 }
116
117 //Left, Up, Right, Down moves
118 new = subsolve(rls->rell, new);
119 new = subsolve(rls->relu, new);
120 new = subsolve(rls->relr, new);
121 new = subsolve(rls->reld, new);
122 }
123
124 time_end_solve = clock();
125
126 //Free and print stats
127 sokoban_free(screen);
128 REPORT("Reading: %fs\n", time_end_read, time_start_read);
129 REPORT("Screen encoding: %fs\n", time_end_scr, time_start_scr);
130 REPORT("Goal encoding: %fs\n", time_end_goal, time_start_goal);
131 REPORT("Relation encoding: %fs\n", time_end_rel, time_start_rel);
132 REPORT("Solving encoding: %fs\n", time_end_solve, time_start_solve);
133
134
135 if(!found){
136 printf("no solution\n");
137 return 1;
138 }
139
140 return 0;
141 }
142
143 int main(int argc, char **argv)
144 {
145 int optchar;
146 char *lurd = "";
147
148 while((optchar = getopt(argc, argv, "vhl:")) != -1){
149 switch(optchar){
150 case 'l':
151 DPRINT("Lurd detected: `%s'\n", optarg);
152 lurd = optarg;
153 break;
154 case 'v':
155 VERBOSE = true;
156 DPRINT("Debug enabled\n");
157 break;
158 case 'h':
159 usage(argv[0]);
160 return 0;
161 case '?':
162 if(isprint(optopt)){
163 DPRINT("Skipping unknown option `-%c'.\n", optopt);
164 } else {
165 DPRINT("Skipping unknown option char `-\\x%x'.\n", optopt);
166 }
167 return 2;
168 default:
169 break;
170 }
171 }
172
173 if(optind == argc){
174 DPRINT("You have not specified a file, reading from stdin\n");
175 return solve(stdin, lurd);
176 } else {
177 DPRINT("Processing: %s\n", argv[optind]);
178 FILE *currentfile = fopen(argv[optind], "r");
179 DPRINT("Opening file\n");
180 return solve(currentfile, lurd);
181 DPRINT("Closing file\n");
182 fclose(currentfile);
183 }
184 }