1f518eba9cdbb6c1958a09aa4d43e4079687103a
[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
81 //Actually solve
82 time_start_solve = clock();
83 BDD old = sylvan_false;
84 BDD new = init->bdd;
85 //Do lurd
86 while(*lurd != '\0'){
87 switch(*lurd){
88 case 'l':
89 new = subsolve(rls->rell, new);
90 break;
91 case 'u':
92 new = subsolve(rls->relu, new);
93 break;
94 case 'r':
95 new = subsolve(rls->relr, new);
96 break;
97 case 'd':
98 new = subsolve(rls->reld, new);
99 break;
100 default:
101 printf("Unknown character in lucd: '%c'\n", *lurd);
102 exit(2);
103 }
104 lurd++;
105 }
106 int iteration = 0;
107 bool found = false;
108 while(new != old){
109 DPRINT("Iteration %d\n", iteration++);
110 old = new;
111 if(sylvan_satcount(sylvan_and(goal->bdd, new), init->vars.varset) > 0){
112 found = true;
113 break;
114 }
115
116 //Left, Up, Right, Down moves
117 new = subsolve(rls->rell, new);
118 new = subsolve(rls->relu, new);
119 new = subsolve(rls->relr, new);
120 new = subsolve(rls->reld, new);
121 }
122 time_end_solve = clock();
123
124 //Free and print stats
125 sokoban_free(screen);
126 REPORT("Reading: %fs\n", time_end_read, time_start_read);
127 REPORT("Screen encoding: %fs\n", time_end_scr, time_start_scr);
128 REPORT("Goal encoding: %fs\n", time_end_goal, time_start_goal);
129 REPORT("Relation encoding: %fs\n", time_end_rel, time_start_rel);
130 REPORT("Solving encoding: %fs\n", time_end_solve, time_start_solve);
131
132 if(!found){
133 printf("no solution\n");
134 return 1;
135 }
136 return 0;
137 }
138
139 int main(int argc, char **argv)
140 {
141 int optchar;
142 char *lurd = "";
143
144 while((optchar = getopt(argc, argv, "vhl:")) != -1){
145 switch(optchar){
146 case 'l':
147 DPRINT("Lurd detected: `%s'\n", optarg);
148 lurd = optarg;
149 break;
150 case 'v':
151 VERBOSE = true;
152 DPRINT("Debug enabled\n");
153 break;
154 case 'h':
155 usage(argv[0]);
156 return 0;
157 case '?':
158 if(isprint(optopt)){
159 DPRINT("Skipping unknown option `-%c'.\n", optopt);
160 } else {
161 DPRINT("Skipping unknown option char `-\\x%x'.\n", optopt);
162 }
163 return 2;
164 default:
165 break;
166 }
167 }
168
169 if(optind == argc){
170 DPRINT("You have not specified a file, reading from stdin\n");
171 return solve(stdin, lurd);
172 } else {
173 DPRINT("Processing: %s\n", argv[optind]);
174 FILE *currentfile = fopen(argv[optind], "r");
175 DPRINT("Opening file\n");
176 return solve(currentfile, lurd);
177 DPRINT("Closing file\n");
178 fclose(currentfile);
179 }
180 }