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 ***strat_base;
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_absolute=0, guess_perleague=0;
61 pair[1]= ports[nports-1];
62 guess_absolute= 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_absolute += ip->route_tail_value;
74 guess_perleague= guess_absolute / leagues_divisor;
77 ctr_routes_wrongfinalelim++;
79 return guess_absolute;
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;
90 int finisle= ports[nports-1]; int finarch= isle2arch(finisle);
91 int midarch= route2midarch(ports,nports);
93 PotentialResult **strat_fin= ONDEMAND(strat_base[finarch], narches);
94 PotentialResult *strat= ONDEMAND(strat_fin[midarch], 1);
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;
103 debugf(" COMPUTE %f %f\n", guess_absolute, guess_perleague);
108 double absolute= value_route(nports, ports, 0);
109 double perleague= absolute / leagues_divisor;
112 ctr_routes_wrongfinal++;
116 if (absolute <= strat->absolute &&
117 perleague <= strat->perleague)
120 debugf(" SOMEHOW BEST\n");
122 fildebugf("final %d:%3d mid %d ",finarch,finisle,midarch);
124 #define CHK(absperl) \
125 fildebugf(#absperl " %15f", absperl); \
126 if (absperl < strat->absperl) { \
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; \
135 for (pos=0; pos < nhighscores_##absperl; pos++) \
136 if (highscores_##absperl[pos].pr == strat) goto found_##absperl; \
142 if (pos >= nhighscores_##absperl-1) break; /* new top */ \
143 if (highscores_##absperl[pos].value >= absperl) break; /* found spot */ \
145 highscores_##absperl[pos-1]= highscores_##absperl[pos]; \
149 highscores_##absperl[pos].value= absperl; \
150 highscores_##absperl[pos].pr= strat; \
152 fildebugf("@%2d", pos); \
160 for (i=0; i<nports; i++)
161 fildebugf(" %d",ports[i]);
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;
176 for (add= get_neighbours(last_isle); add; add=add->next) {
177 int newdist= totaldist + add->dist;
178 if (newdist > max_dist) continue;
180 recurse(add->islandid, nports, newdist, estimate);
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);
191 int nhighscores_absolute, nhighscores_perleague;
192 HighScoreEntry *highscores_absolute;
193 HighScoreEntry *highscores_perleague;
199 void setup_search(void) {
200 MCALLOC(neighbours, islandtablesz);
202 SQL_PREPARE(ss_neigh,
203 "SELECT biid, dist FROM routes WHERE aiid=?");
206 sql_single_int(" SELECT count(*) FROM (\n"
207 " SELECT DISTINCT archipelago\n"
210 MCALLOC(archnames, max_narches);
211 MCALLOC_INITEACH(islandid2arch, islandtablesz, *this=-1);
215 " SELECT islandid, archipelago\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);
222 for (arch=0; arch<narches; arch++)
223 if (!strcmp(archnames[arch], archname)) goto found;
224 assert(narches < max_narches);
226 archnames[arch]= masprintf("%s",archname);
228 islandid2arch[isle]= arch;
230 sqlite3_finalize(archs);