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