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