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