chiark / gitweb /
routesearch: allow constraining destination island
[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 #define CTR(x) int ctr_##x;
13   COUNTER_LIST
14 #undef CTR
15
16 static PotentialResult ****results;
17   /* results[start_isle_ix][finalisle][midisle]-> */
18
19 int main(int argc, const char **argv) {
20   const char *arg;
21
22 #ifndef debug_flags
23   debug_flags= ~( dbg_sql2 );
24 #endif
25   sysassert( !setvbuf(debug,0,_IOLBF,0) );
26   
27   for (;;) {
28     arg= *++argv;
29     if (arg[0] != '-') break;
30 #ifndef debug_flags
31     if (!strcmp(arg,"-DN")) {
32       debug_flags= 0;
33     } else
34 #endif
35     {
36       abort();
37     }
38   }
39
40   max_mass= atof(*argv++);
41   max_volu= atof(*argv++);
42   max_capi= atof(*argv++);
43   double loss_per_league= atof(*argv++);
44
45   if (!loss_per_league) loss_per_league= 1e-7;
46   distance_loss_factor_per_league= 1.0 - loss_per_league;
47
48   setup_sql();
49   setup_value();
50   setup_search();
51
52   fprintf(stderr,"setup complete, starting search\n");
53   
54   arg= *argv++;
55   if (!strcmp(arg,"specific")) {
56     int ia[argc], ni=0;
57     while ((arg= *argv++))
58       ia[ni++]= atoi(arg);
59
60     double val= value_route(ni, ia, 0);
61     printf("route value is %g\n", val);
62   } else if (!strcmp(arg,"search")) {
63     MCALLOC(results, argc);
64
65     max_dist= atoi(*argv++);
66     nhighscores_absolute= atoi(*argv++);
67     nhighscores_perleague= atoi(*argv++);
68     const char *final_isle_spec= *argv++;
69
70     MCALLOC(highscores_absolute, nhighscores_absolute);
71     MCALLOC(highscores_perleague, nhighscores_perleague);
72
73     int resultsix= 0;
74     while ((arg= argv[resultsix])) {
75       int init_isle= atoi(arg);
76       
77       int final_isle;
78       if (!strcmp(final_isle_spec,"circ")) final_isle= init_isle;
79       else if (!strcmp(final_isle_spec,"any")) final_isle= -1;
80       else final_isle= atoi(final_isle_spec);
81       assert(final_isle);
82
83       search(init_isle, final_isle, &results[resultsix]);
84       resultsix++;
85     }
86
87     int i, midarch, finarch;
88     for (i=0; i<resultsix; i++) {
89       fprintf(stderr,"============== start #%d %s [PARTIAL] ==============\n",
90               i, argv[i]);
91       PotentialResult ***strat_resultsix= results[i];
92       if (!strat_resultsix) continue;
93       fprintf(stderr,"  ");
94       for (midarch=0; midarch<narches; midarch++) {
95         fprintf(stderr,"|   mid %d  ",midarch);
96       }
97       fprintf(stderr,"\n");
98       for (finarch=0; finarch<narches; finarch++) {
99         PotentialResult **strat_finarch= strat_resultsix[finarch];
100         if (!strat_finarch) continue;
101         fprintf(stderr,"f%d",finarch);
102         for (midarch=0; midarch<narches; midarch++) {
103           PotentialResult *result= strat_finarch[midarch];
104           if (!result) {
105             fprintf(stderr,"|          ");
106           } else {
107             fprintf(stderr,"|%5d",(int)(result->absolute));
108             fprintf(stderr," ");
109             fprintf(stderr,"%4d",(int)(result->perleague));
110           }
111         }
112         fprintf(stderr,"\n");
113       }
114     }
115
116     int pos;
117 #define OUT(absperl)                                                          \
118     fprintf(stderr,"\n================== " #absperl " ==================\n"); \
119     for (pos=0; pos<nhighscores_##absperl; pos++) {                           \
120       HighScoreEntry *hs= &highscores_##absperl[pos];                         \
121       PotentialResult *pr= hs->pr;                                            \
122       if (!pr) continue;                                                      \
123       const int *const ports= pr->absperl##_ports;                            \
124       int nports;                                                             \
125       for (nports=0; nports<MAX_ROUTELEN && ports[nports]>=0; nports++);      \
126       int finisle= ports[nports-1]; int finarch= isle2arch(finisle);          \
127       int midarch= route2midarch(ports,nports);                               \
128       fprintf(stderr,                                                         \
129               " @%2d #%2d | start%3d mid%d f%d:%3d | %5d %5d %4d |",     \
130               pos, nhighscores_##absperl - 1 - pos,                           \
131               ports[0], midarch, finarch,finisle,                     \
132               (int)hs->value, (int)pr->absolute, (int)pr->perleague);         \
133       for (i=0; i<nports; i++) fprintf(stderr," %d",ports[i]);                \
134       fprintf(stderr,"\n");                                                   \
135     }
136     OUT(absolute)
137     OUT(perleague)
138     
139   } else {
140     abort();
141   }
142
143 #define CTR(x) fprintf(stderr,"  %-30s %10d\n",#x,ctr_##x);
144   COUNTER_LIST
145 #undef CTR
146
147   return 0;
148 }