chiark / gitweb /
WIP routesearch: actually find the database
[ypp-sc-tools.db-live.git] / yarrg / rsmain.c
1 /**/
2
3 #include "rscommon.h"
4
5 #include <ctype.h>
6
7 int o_quiet= 0;
8 double max_mass=-1, max_volu=-1, max_capi=-1;
9 double distance_loss_factor_per_league;
10 int max_dist= -1;
11
12 FILE *debug_file;
13 FILE *output;
14
15 DEBUG_DEFINE_SOME_DEBUGF(tableau,tabdebugf);
16
17
18 #define CTR(x)    int ctr_##x;
19 #define CTRA(x,n) int ctr_##x[n];
20   COUNTER_LIST
21 #undef CTR
22 #undef CTRA
23
24 static PotentialResult ****results;
25   /* results[start_isle_ix][finalisle][midisle]-> */
26
27 static pid_t debugoutpid;
28
29 int main(int argc, const char **argv) {
30   const char *arg;
31   int i, ap;
32
33 #ifndef debug_flags
34   debug_flags= ~( dbg_sql2 );
35 #endif
36
37   for (;;) {
38     arg= *++argv;
39     if (arg[0] != '-') break;
40 #ifndef debug_flags
41     if (!strcmp(arg,"-DN")) {
42       debug_flags= 0;
43     } else
44 #endif
45     {
46       abort();
47     }
48   }
49
50   if (debug_flags) {
51     /* glpk insists on writing stuff to stdout, and it does buffering,
52      * so we route all our debug through it this too */
53     int realstdout;
54     sysassert( (realstdout= dup(1)) > 2 );
55     sysassert( output= fdopen(realstdout,"w") );
56
57     int pfd[2];
58     sysassert(! pipe(pfd) );
59     sysassert( (debugoutpid= fork()) >=0 );
60     if (!debugoutpid) {
61       sysassert( dup2(pfd[0],0)==0 );
62       sysassert( dup2(2,1)==1 );
63       sysassert(! close(pfd[0]) );
64       sysassert(! close(pfd[1]) );
65       sysassert(! execlp("cat","cat",(char*)0) );
66     }
67     sysassert( dup2(pfd[1],1)==1 );
68     sysassert(! close(pfd[0]) );
69     sysassert(! close(pfd[1]) );
70
71     debug_file= stdout;
72   } else {
73     output= stdout;
74     debug_file= stderr;
75   }
76
77   const char *database= *argv++;
78
79   sysassert( !setvbuf(debug,0,_IOLBF,0) );
80
81   max_mass= atof(*argv++);
82   max_volu= atof(*argv++);
83   max_capi= atof(*argv++);
84   double loss_per_league= atof(*argv++);
85   distance_loss_factor_per_league= 1.0 - loss_per_league;
86
87   setup_sql(database);
88   setup_value();
89   setup_search();
90
91   for (i=0; i<narches; i++)
92     fprintf(output,"arch %d %s\n",i,archnames[i]);
93   fprintf(output,"setup complete, starting search\n");
94
95   arg= *argv++;
96   if (!strcmp(arg,"specific")) {
97     int ia[argc], ni=0;
98     while ((arg= *argv++))
99       ia[ni++]= atoi(arg);
100
101     double val= value_route(ni, ia, 0);
102     fprintf(output, "route value is %g\n", val);
103   } else if (!strcmp(arg,"search")) {
104     MCALLOC(results, argc);
105
106     max_dist= atoi(*argv++);
107     for (ap=0; ap<AP; ap++) {
108       nhighscores[ap]= atoi(*argv++);
109       MCALLOC(highscores[ap], nhighscores[ap]);
110     }
111     const char *final_isle_spec= *argv++;
112
113     int resultsix= 0;
114     while ((arg= argv[resultsix])) {
115       int init_isle= atoi(arg);
116
117       int final_isle;
118       if (!strcmp(final_isle_spec,"circ")) final_isle= init_isle;
119       else if (!strcmp(final_isle_spec,"any")) final_isle= -1;
120       else final_isle= atoi(final_isle_spec);
121       assert(final_isle);
122
123       search(init_isle, final_isle, &results[resultsix]);
124       resultsix++;
125     }
126
127     int midarch, finarch;
128     for (i=0; i<resultsix; i++) {
129       tabdebugf("============== start #%d %s [PARTIAL] ==============\n",
130                 i, argv[i]);
131       PotentialResult ***strat_resultsix= results[i];
132       if (!strat_resultsix) continue;
133       tabdebugf("  ");
134       for (midarch=0; midarch<narches; midarch++) {
135         tabdebugf("|   mid %d  ",midarch);
136       }
137       tabdebugf("\n");
138       for (finarch=0; finarch<narches; finarch++) {
139         PotentialResult **strat_finarch= strat_resultsix[finarch];
140         if (!strat_finarch) continue;
141         tabdebugf("f%d",finarch);
142         for (midarch=0; midarch<narches; midarch++) {
143           PotentialResult *result= strat_finarch[midarch];
144           if (!result) {
145             tabdebugf("|          ");
146           } else {
147             tabdebugf("|%5d",(int)(result->value[A]));
148             tabdebugf(" ");
149             tabdebugf("%4d",(int)(result->value[P]));
150           }
151         }
152         tabdebugf("\n");
153       }
154     }
155
156     for (ap=0; ap<AP; ap++) {
157       int pos;
158       fprintf(output,"\n================== ap=%d ==================\n", ap);
159       for (pos=0; pos<nhighscores[ap]; pos++) {
160         HighScoreEntry *hs= &highscores[ap][pos];
161         PotentialResult *pr= hs->pr;
162         if (!pr) continue;
163         const int *const ports= pr->ports[ap];
164         int nports;
165         for (nports=0; nports<MAX_ROUTELEN && ports[nports]>=0; nports++);
166         int finisle= ports[nports-1]; int finarch= isle2arch(finisle);
167         int midarch= route2midarch(ports,nports);
168         fprintf(output,
169                 " @%2d #%2d | start%3d mid%d f%d:%3d | %5d %5d %4d |",     \
170                 pos, nhighscores[ap] - 1 - pos,
171                 ports[0], midarch, finarch,finisle,                   \
172                 (int)hs->value, (int)pr->value[A], (int)pr->value[P]);
173         for (i=0; i<nports; i++) fprintf(output," %d",ports[i]);
174         fprintf(output,"\n");
175       }
176     }
177     fprintf(output,"\n");
178
179   } else {
180     abort();
181   }
182
183 #define CTR(x) fprintf(output,"  %-30s %10d\n",#x,ctr_##x);
184 #define CTRA(x,n) for (i=0;i<n;i++) \
185   fprintf(output,"  %-27s[%d] %10d\n",#x,i,ctr_##x[i]);
186   COUNTER_LIST
187 #undef CTR
188
189   if (debug_flags) {
190     sysassert(! fclose(debug) );
191     waitpid_check_exitstatus(debugoutpid,"debug cat",1);
192   }
193   sysassert(! fclose(output) );
194
195   return 0;
196 }