5 DEBUG_DEFINE_DEBUGF(search);
6 DEBUG_DEFINE_SOME_DEBUGF(filter,fildebugf);
8 typedef struct Neighbour {
9 struct Neighbour *next;
14 static Neighbour **neighbours; /* neighbours[islandid]->islandid etc. */
15 static sqlite3_stmt *ss_neigh;
17 static int ports[MAX_ROUTELEN];
18 static int final_isle;
20 static Neighbour *get_neighbours(int isle) {
21 Neighbour **np= &neighbours[isle];
23 if (head) return head;
25 SQL_BIND(ss_neigh, 1, isle);
26 while (SQL_STEP(ss_neigh)) {
29 add->islandid= sqlite3_column_int(ss_neigh, 0);
30 add->dist= sqlite3_column_int(ss_neigh, 1);
34 SQL_MUST( sqlite3_reset(ss_neigh) );
41 static PotentialResult ***buckets_base[GRANUS];
44 static double process_route(int nports, int totaldist,
45 double overestimate_excepting_tail) {
47 int leagues_divisor= totaldist + nports;
49 ctr_routes_considered++;
51 int wrong_final= final_isle && ports[nports-1] != final_isle;
53 debugf("========== ROUTE");
54 for (i=0; i<nports; i++)
55 debugf(" %d",ports[i]);
58 double guess[AP]={0,0};
61 pair[1]= ports[nports-1];
62 guess[A]= overestimate_excepting_tail;
64 for (i=0; i<nports; i++) {
66 IslandPair *ip= ipair_get_maybe(pair[0], pair[1]);
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]);
72 guess[A] += ip->route_tail_value;
74 guess[P]= guess[A] / leagues_divisor;
77 ctr_routes_wrongfinalelim++;
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]);
90 int finisle= ports[nports-1];
91 int finarch= isle2arch(finisle);
93 int midisle= ports[nports/2];
94 int midarch= route2midarch(ports,nports);
96 PotentialResult *buckets[GRANUS];
98 for (granui=mingranu; granui<GRANUS; granui++) {
99 PotentialResult **buckets_fin;
102 case 0: fin=finisle; mid=midisle; break;
103 case 1: fin=finisle; mid=midarch; break;
104 case 2: fin=finarch; mid=midarch; break;
107 buckets_fin= ONDEMAND(buckets_base[granui][fin], granusz_mid[granui]);
108 buckets[granui]= ONDEMAND(buckets_fin[mid], 1);
112 if (guess[A] <= buckets[mingranu]->value[A] &&
113 guess[P] <= buckets[mingranu]->value[P]) {
114 ctr_routes_bucketelim++;
115 debugf(" ELIM %f %f\n", guess[A], guess[P]);
118 debugf(" COMPUTE %f %f\n", guess[A], guess[P]);
124 value[A]= value_route(nports, ports, 0);
125 value[P]= value[A] / leagues_divisor;
128 ctr_routes_wrongfinal++;
132 for (granui=mingranu; granui<GRANUS; granui++) {
133 PotentialResult *bucket= buckets[granui];
135 if (value[A] <= bucket->value[A] &&
136 value[P] <= bucket->value[P])
139 debugf(" SOMEHOW %d BEST\n",granui);
141 fildebugf("final %d:%3d mid %d ",finarch,finisle,midarch);
143 for (ap=0; ap<AP; ap++) {
144 HighScoreEntry *scores= highscores[granui][ap];
145 int *nscores= &nhighscores[granui][ap];
147 fildebugf("ap=%d %15f", ap, value[ap]);
148 if (value[ap] < bucket->value[ap]) {
152 ctr_newbests_granu[ap]++;
153 bucket->value[ap]= value[ap];
154 memcpy(bucket->ports[ap], ports, sizeof(*ports) * nports);
155 if (nports < MAX_ROUTELEN-1) bucket->ports[ap][nports]= -1;
157 for (pos=0; pos < *nscores; pos++)
158 if (scores[pos].pr == bucket) goto found;
164 if (pos >= *nscores-1) break; /* new top */
165 if (scores[pos].value >= value[ap]) break; /* found spot */
167 scores[pos-1]= scores[pos];
171 scores[pos].value= value[ap];
172 scores[pos].pr= bucket;
174 fildebugf("@%2d", pos);
181 for (i=0; i<nports; i++)
182 fildebugf(" %d",ports[i]);
188 static void recurse(int last_isle,
189 int nports /* excluding last_isle */,
190 int totaldist /* including last_isle */,
191 double last_estimate) {
192 ports[nports++]= last_isle;
193 double estimate= process_route(nports, totaldist, last_estimate);
194 if (nports >= MAX_ROUTELEN) return;
197 for (add= get_neighbours(last_isle); add; add=add->next) {
198 int newdist= totaldist + add->dist;
199 if (newdist > max_dist) continue;
201 recurse(add->islandid, nports, newdist, estimate);
205 void search(int start_isle, int final_isle_spec,
206 PotentialResult ****buckets_base_io[GRANUS]) {
208 for (granui=0; granui<GRANUS; granui++)
209 buckets_base[granui]=
210 ONDEMAND(*buckets_base_io[granui], granusz_fin[granui]);
212 final_isle= final_isle_spec <= 0 ? 0 : final_isle_spec;
213 recurse(start_isle,0,0,1e6);
216 int nhighscores[GRANUS][AP];
217 HighScoreEntry *highscores[GRANUS][AP];
218 int mingranu, granusz_fin[GRANUS], granusz_mid[GRANUS];
224 void setup_search(void) {
225 MCALLOC(neighbours, islandtablesz);
227 SQL_PREPARE(ss_neigh,
228 "SELECT biid, dist FROM routes WHERE aiid=?");
231 sql_single_int(" SELECT count(*) FROM (\n"
232 " SELECT DISTINCT archipelago\n"
235 MCALLOC(archnames, max_narches);
236 MCALLOC_INITEACH(islandid2arch, islandtablesz, *this=-1);
240 " SELECT islandid, archipelago\n"
242 " ORDER BY archipelago");
243 while (SQL_STEP(archs)) {
244 int isle= sqlite3_column_int(archs,0);
245 const char *archname= (const char*)sqlite3_column_text(archs,1);
247 for (arch=0; arch<narches; arch++)
248 if (!strcmp(archnames[arch], archname)) goto found;
249 assert(narches < max_narches);
251 archnames[arch]= masprintf("%s",archname);
253 islandid2arch[isle]= arch;
255 sqlite3_finalize(archs);
257 granusz_fin[0]= granusz_mid[0]= islandtablesz;
258 granusz_fin[1]= islandtablesz; granusz_mid[1]= narches;
259 granusz_fin[2]= granusz_mid[2]= narches;