chiark / gitweb /
routesearch: abandon higher granuarities when tables become full
[ypp-sc-tools.main.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 ***buckets_base[GRANUS];
42
43
44 static double process_route(int nports, int totaldist,
45                             double overestimate_excepting_tail) {
46   int i, ap;
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[AP]={0,0};
59   if (nports>=2) {
60     int pair[2], i;
61     pair[1]= ports[nports-1];
62     guess[A]= 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[A] += ip->route_tail_value;
73     }
74     guess[P]= guess[A] / leagues_divisor;
75
76     if (wrong_final) {
77       ctr_routes_wrongfinalelim++;
78       debugf(" WFELIM\n");
79       return guess[A];
80     }
81
82     if (guess[A] <= highscores[GRANUS-1][A][0].value &&
83         guess[P] <= highscores[GRANUS-1][P][0].value) {
84       ctr_routes_quickelim++;
85       debugf(" QELIM %f %f\n", guess[A], guess[P]);
86       return guess[A];
87     }
88   }
89
90   int finisle= ports[nports-1];
91   int finarch= isle2arch(finisle);
92
93   int midisle= ports[nports/2];
94   int midarch= route2midarch(ports,nports);
95
96   PotentialResult *buckets[GRANUS];
97   int granui;
98   for (granui=0; granui<granus; granui++) {
99     PotentialResult **buckets_fin;
100     int mid, fin;
101     switch (granui) {
102     case 0: fin=finarch; mid=midarch; break;
103     case 1: fin=finisle; mid=midarch; break;
104     case 2: fin=finisle; mid=midisle; break;
105     default: abort();
106     }
107     buckets_fin= ONDEMAND(buckets_base[granui][fin], granusz_mid[granui]);
108     buckets[granui]= ONDEMAND(buckets_fin[mid], 1);
109   }
110
111   if (nports>=2) {
112     if (guess[A] <= buckets[0]->value[A] &&
113         guess[P] <= buckets[0]->value[P]) {
114       ctr_routes_bucketelim++;
115       debugf(" ELIM %f %f\n", guess[A], guess[P]);
116       return guess[A];
117     }
118     debugf(" COMPUTE %f %f\n", guess[A], guess[P]);
119   }
120
121   ctr_routes_valued++;
122
123   double value[AP];
124   value[A]= value_route(nports, ports, 0);
125   value[P]= value[A] / leagues_divisor;
126
127   if (wrong_final) {
128     ctr_routes_wrongfinal++;
129     return value[0];
130   }
131
132   for (granui=granus-1; granui>=0; granui--) {
133     PotentialResult *bucket= buckets[granui];
134
135     if (value[A] <= bucket->value[A] &&
136         value[P] <= bucket->value[P])
137       continue;
138
139     debugf(" SOMEHOW %d BEST\n",granui);
140
141     fildebugf("final %d:%3d mid %d ",finarch,finisle,midarch);
142     int relevant=0;
143
144     for (ap=0; ap<AP; ap++) {
145       HighScoreEntry *scores= highscores[granui][ap];
146       int *nscores= &nhighscores[granui][ap];
147
148       fildebugf("ap=%d %15f", ap, value[ap]);
149       if (value[ap] < bucket->value[ap]) {
150         debugf("      ");
151       } else {
152         int pos;
153         ctr_newbests_granu[granui*AP+ap]++;
154         bucket->value[ap]= value[ap];
155         memcpy(bucket->ports[ap], ports, sizeof(*ports) * nports);
156         if (nports < MAX_ROUTELEN-1) bucket->ports[ap][nports]= -1;
157         fildebugf("** ");
158         for (pos=0; pos < *nscores; pos++)
159           if (scores[pos].pr == bucket) goto found;
160         /* not found */
161         pos= -1;
162       found:
163         for (;;) {
164           pos++;
165           if (pos >= *nscores-1) break; /* new top */
166           if (scores[pos].value >= value[ap]) break; /* found spot */
167           if (pos>0)
168             scores[pos-1]= scores[pos];
169         }
170         pos--;
171         if (pos>0) {
172           scores[pos].value= value[ap];
173           scores[pos].pr= bucket;
174           relevant=1;
175         }
176         fildebugf("@%2d", pos);
177       } /* new best */
178     } /* ap */
179     if (!relevant)
180       /* both absolute and perleague are full at this granularity,
181        * so we don't care about anything more granular */
182       granus= granui+1;
183   } /* granui */
184
185   fildebugf(" route");
186
187   for (i=0; i<nports; i++)
188     fildebugf(" %d",ports[i]);
189   fildebugf("\n");
190
191   return value[0];
192 }
193
194 static void recurse(int last_isle,
195                     int nports /* excluding last_isle */,
196                     int totaldist /* including last_isle */,
197                     double last_estimate) {
198   ports[nports++]= last_isle;
199   double estimate= process_route(nports, totaldist, last_estimate);
200   if (nports >= MAX_ROUTELEN) return;
201
202   Neighbour *add;
203   for (add= get_neighbours(last_isle); add; add=add->next) {
204     int newdist= totaldist + add->dist;
205     if (newdist > max_dist) continue;
206
207     recurse(add->islandid, nports, newdist, estimate);
208   }
209 }
210
211 void search(int start_isle, int final_isle_spec,
212             PotentialResult ****buckets_base_io[GRANUS]) {
213   int granui;
214   for (granui=0; granui<GRANUS; granui++)
215     buckets_base[granui]=
216       ONDEMAND(*buckets_base_io[granui], granusz_fin[granui]);
217
218   final_isle= final_isle_spec <= 0 ? 0 : final_isle_spec;
219   recurse(start_isle,0,0,1e6);
220 }
221
222 int nhighscores[GRANUS][AP];
223 HighScoreEntry *highscores[GRANUS][AP];
224 int granus=GRANUS, granusz_fin[GRANUS], granusz_mid[GRANUS];
225
226 int narches;
227 char **archnames;
228 int *islandid2arch;
229
230 void setup_search(void) {
231   MCALLOC(neighbours, islandtablesz);
232
233   SQL_PREPARE(ss_neigh,
234               "SELECT biid, dist FROM routes WHERE aiid=?");
235
236   int max_narches=
237     sql_single_int(" SELECT count(*) FROM (\n"
238                    "  SELECT DISTINCT archipelago\n"
239                    "   FROM islands\n"
240                    "  )");
241   MCALLOC(archnames, max_narches);
242   MCALLOC_INITEACH(islandid2arch, islandtablesz, *this=-1);
243
244   sqlite3_stmt *archs;
245   SQL_PREPARE(archs,
246               " SELECT islandid, archipelago\n"
247               "  FROM islands\n"
248               "  ORDER BY archipelago");
249   while (SQL_STEP(archs)) {
250     int isle= sqlite3_column_int(archs,0);
251     const char *archname= (const char*)sqlite3_column_text(archs,1);
252     int arch;
253     for (arch=0; arch<narches; arch++)
254       if (!strcmp(archnames[arch], archname)) goto found;
255     assert(narches < max_narches);
256     arch= narches++;
257     archnames[arch]= masprintf("%s",archname);
258   found:
259     islandid2arch[isle]= arch;
260   }
261   sqlite3_finalize(archs);
262
263   granusz_fin[0]=                granusz_mid[0]= narches;
264   granusz_fin[1]= islandtablesz; granusz_mid[1]= narches;
265   granusz_fin[2]=                granusz_mid[2]= islandtablesz;
266 }