chiark / gitweb /
routesearch: allow constraining destination island
[ypp-sc-tools.web-live.git] / yarrg / rssearch.c
1 /**/
2
3 #include "rscommon.h"
4
5 DEBUG_DEFINE_DEBUGF(search);
6 DEBUG_DEFINE_SOME_DEBUGF(filter,fildebugf);
7
8 typedef struct Neighbour {
9   struct Neighbour *next;
10   int islandid;
11   int dist;
12 } Neighbour;
13
14 static Neighbour **neighbours; /* neighbours[islandid]->islandid etc. */
15 static sqlite3_stmt *ss_neigh;
16
17 static int ports[MAX_ROUTELEN];
18 static int final_isle;
19
20 static Neighbour *get_neighbours(int isle) {
21   Neighbour **np= &neighbours[isle];
22   Neighbour *head= *np;
23   if (head) return head;
24
25   SQL_BIND(ss_neigh, 1, isle);
26   while (SQL_STEP(ss_neigh)) {
27     Neighbour *add;
28     NEW(add);
29     add->islandid= sqlite3_column_int(ss_neigh, 0);
30     add->dist= sqlite3_column_int(ss_neigh, 1);
31     add->next= head;
32     head= add;
33   }
34   SQL_MUST( sqlite3_reset(ss_neigh) );
35
36   *np= head;
37   return head;
38 }
39
40
41 static PotentialResult ***strat_base;
42
43
44 static double process_route(int nports, int totaldist,
45                             double overestimate_excepting_tail) {
46   int i;
47   int leagues_divisor= totaldist + nports;
48
49   ctr_routes_considered++;
50
51   int wrong_final= final_isle && ports[nports-1] != final_isle;
52
53   debugf("========== ROUTE");
54   for (i=0; i<nports; i++)
55     debugf(" %d",ports[i]);
56   debugf("\n");
57
58   double guess_absolute=0, guess_perleague=0;
59   if (nports>=2) {
60     int pair[2], i;
61     pair[1]= ports[nports-1];
62     guess_absolute= overestimate_excepting_tail;
63     
64     for (i=0; i<nports; i++) {
65       pair[0]= ports[i];
66       IslandPair *ip= ipair_get_maybe(pair[0], pair[1]);
67       if (!ip) continue;
68       if (ip->route_tail_value < 0) {
69         ctr_subroute_tails_valued++;
70         ip->route_tail_value= value_route(2, pair, pair[0]!=pair[1]);
71       }
72       guess_absolute += ip->route_tail_value;
73     }
74     guess_perleague= guess_absolute / leagues_divisor;
75
76     if (wrong_final) {
77       ctr_routes_wrongfinalelim++;
78       debugf(" WFELIM\n");
79       return guess_absolute;
80     }
81
82     if (guess_absolute <= highscores_absolute[0].value &&
83         guess_perleague <= highscores_perleague[0].value) {
84       ctr_routes_quickelim++;
85       debugf(" QELIM %f %f\n", guess_absolute, guess_perleague);
86       return guess_absolute;
87     }
88   }
89
90   int finisle= ports[nports-1]; int finarch= isle2arch(finisle);
91   int midarch= route2midarch(ports,nports);
92
93   PotentialResult **strat_fin= ONDEMAND(strat_base[finarch], narches);
94   PotentialResult *strat= ONDEMAND(strat_fin[midarch], 1);
95
96   if (nports>=2) {
97     if (guess_absolute <= strat->absolute &&
98         guess_perleague <= strat->perleague) {
99       ctr_routes_stratelim++;
100       debugf(" ELIM %f %f\n", guess_absolute, guess_perleague);
101       return guess_absolute;
102     }
103     debugf(" COMPUTE %f %f\n", guess_absolute, guess_perleague);
104   }
105
106   ctr_routes_valued++;
107
108   double absolute= value_route(nports, ports, 0);
109   double perleague= absolute / leagues_divisor;
110
111   if (wrong_final) {
112     ctr_routes_wrongfinal++;
113     return absolute;
114   }
115
116   if (absolute <= strat->absolute &&
117       perleague <= strat->perleague)
118     return absolute;
119
120   debugf(" SOMEHOW BEST\n");
121
122   fildebugf("final %d:%3d mid %d ",finarch,finisle,midarch);
123
124 #define CHK(absperl)                                                          \
125   fildebugf(#absperl " %15f", absperl);                                       \
126   if (absperl < strat->absperl) {                                             \
127     debugf("      ");                                                         \
128   } else {                                                                    \
129     int pos;                                                                  \
130     ctr_newbests_strat_##absperl++;                                           \
131     strat->absperl= absperl;                                                  \
132     memcpy(strat->absperl##_ports, ports, sizeof(*ports) * nports);           \
133     if (nports < MAX_ROUTELEN-1) strat->absperl##_ports[nports]= -1;          \
134     fildebugf("** ");                                                         \
135     for (pos=0; pos < nhighscores_##absperl; pos++)                           \
136       if (highscores_##absperl[pos].pr == strat) goto found_##absperl;        \
137     /* not found */                                                           \
138     pos= -1;                                                                  \
139    found_##absperl:                                                           \
140     for (;;) {                                                                \
141       pos++;                                                                  \
142       if (pos >= nhighscores_##absperl-1) break; /* new top */                \
143       if (highscores_##absperl[pos].value >= absperl) break; /* found spot */ \
144       if (pos>0)                                                              \
145         highscores_##absperl[pos-1]= highscores_##absperl[pos];               \
146     }                                                                         \
147     pos--;                                                                    \
148     if (pos>0) {                                                              \
149       highscores_##absperl[pos].value= absperl;                               \
150       highscores_##absperl[pos].pr= strat;                                    \
151     }                                                                         \
152     fildebugf("@%2d", pos);                                                   \
153   }
154
155   CHK(absolute)
156   CHK(perleague)
157
158   fildebugf(" route");
159
160   for (i=0; i<nports; i++)
161     fildebugf(" %d",ports[i]);
162   fildebugf("\n");
163
164   return absolute;
165 }
166
167 static void recurse(int last_isle,
168                     int nports, /* excluding last_isle */
169                     int totaldist /* including last_isle */,
170                     double last_estimate) {
171   ports[nports++]= last_isle;
172   double estimate= process_route(nports, totaldist, last_estimate);
173   if (nports >= MAX_ROUTELEN) return;
174
175   Neighbour *add;
176   for (add= get_neighbours(last_isle); add; add=add->next) {
177     int newdist= totaldist + add->dist;
178     if (newdist > max_dist) continue;
179
180     recurse(add->islandid, nports, newdist, estimate);
181   }
182 }
183
184 void search(int start_isle, int final_isle_spec,
185             PotentialResult ****strat_base_io) {
186   strat_base= ONDEMAND(*strat_base_io, narches);
187   final_isle= final_isle_spec <= 0 ? 0 : final_isle_spec;
188   recurse(start_isle,0,0,1e6);
189 }
190
191 int nhighscores_absolute, nhighscores_perleague;
192 HighScoreEntry *highscores_absolute;
193 HighScoreEntry *highscores_perleague;
194
195 int narches;
196 char **archnames;
197 int *islandid2arch;
198
199 void setup_search(void) {
200   MCALLOC(neighbours, islandtablesz);
201
202   SQL_PREPARE(ss_neigh,
203               "SELECT biid, dist FROM routes WHERE aiid=?");
204
205   int max_narches=
206     sql_single_int(" SELECT count(*) FROM (\n"
207                    "  SELECT DISTINCT archipelago\n"
208                    "   FROM islands\n"
209                    "  )");
210   MCALLOC(archnames, max_narches);
211   MCALLOC_INITEACH(islandid2arch, islandtablesz, *this=-1);
212
213   sqlite3_stmt *archs;
214   SQL_PREPARE(archs,
215               " SELECT islandid, archipelago\n"
216               "  FROM islands\n"
217               "  ORDER BY archipelago");
218   while (SQL_STEP(archs)) {
219     int isle= sqlite3_column_int(archs,0);
220     const char *archname= (const char*)sqlite3_column_text(archs,1);
221     int arch;
222     for (arch=0; arch<narches; arch++)
223       if (!strcmp(archnames[arch], archname)) goto found;
224     assert(narches < max_narches);
225     arch= narches++;
226     archnames[arch]= masprintf("%s",archname);
227   found:
228     islandid2arch[isle]= arch;
229   }
230   sqlite3_finalize(archs);
231 }