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