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