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