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];
19 static Neighbour *get_neighbours(int isle) {
20 Neighbour **np= &neighbours[isle];
22 if (head) return head;
24 SQL_BIND(ss_neigh, 1, isle);
25 while (SQL_STEP(ss_neigh)) {
28 add->islandid= sqlite3_column_int(ss_neigh, 0);
29 add->dist= sqlite3_column_int(ss_neigh, 1);
33 SQL_MUST( sqlite3_reset(ss_neigh) );
40 static PotentialResult ***strat_base;
43 static double process_route(int nports, int totaldist,
44 double overestimate_excepting_tail) {
46 int leagues_divisor= totaldist + nports;
48 ctr_routes_considered++;
50 debugf("========== ROUTE");
51 for (i=0; i<nports; i++)
52 debugf(" %d",ports[i]);
55 double guess_absolute=0, guess_perleague=0;
58 pair[1]= ports[nports-1];
59 guess_absolute= overestimate_excepting_tail;
61 for (i=0; i<nports; i++) {
63 IslandPair *ip= ipair_get_maybe(pair[0], pair[1]);
65 if (ip->route_tail_value < 0) {
66 ctr_subroute_tails_valued++;
67 ip->route_tail_value= value_route(2, pair, pair[0]!=pair[1]);
69 guess_absolute += ip->route_tail_value;
71 guess_perleague= guess_absolute / leagues_divisor;
73 if (guess_absolute <= highscores_absolute[0].value &&
74 guess_perleague <= highscores_perleague[0].value) {
75 ctr_routes_quickelim++;
76 debugf(" QELIM %f %f\n", guess_absolute, guess_perleague);
77 return guess_absolute;
81 int finisle= ports[nports-1]; int finarch= isle2arch(finisle);
82 int midisle= ports[nports/2]; int midarch= isle2arch(midisle);
84 PotentialResult **strat_fin= ONDEMAND(strat_base[finarch], narches);
85 PotentialResult *strat= ONDEMAND(strat_fin[midarch], 1);
88 if (guess_absolute <= strat->absolute &&
89 guess_perleague <= strat->perleague) {
90 ctr_routes_stratelim++;
91 debugf(" ELIM %f %f\n", guess_absolute, guess_perleague);
92 return guess_absolute;
94 debugf(" COMPUTE %f %f\n", guess_absolute, guess_perleague);
99 double absolute= value_route(nports, ports, 0);
100 double perleague= absolute / leagues_divisor;
102 if (absolute <= strat->absolute &&
103 perleague <= strat->perleague)
106 debugf(" SOMEHOW BEST\n");
108 fildebugf("final %d:%3d mid %d:%3d ",finarch,finisle,midarch,midisle);
110 #define CHK(absperl) \
111 fildebugf(#absperl " %15f", absperl); \
112 if (absperl < strat->absperl) { \
116 ctr_newbests_strat_##absperl++; \
117 strat->absperl= absperl; \
118 memcpy(strat->absperl##_ports, ports, sizeof(*ports) * nports); \
119 if (nports < MAX_ROUTELEN-1) strat->absperl##_ports[nports]= -1; \
121 for (pos=0; pos < nhighscores_##absperl; pos++) \
122 if (highscores_##absperl[pos].pr == strat) goto found_##absperl; \
128 if (pos >= nhighscores_##absperl-1) break; /* new top */ \
129 if (highscores_##absperl[pos].value >= absperl) break; /* found spot */ \
131 highscores_##absperl[pos-1]= highscores_##absperl[pos]; \
135 highscores_##absperl[pos].value= absperl; \
136 highscores_##absperl[pos].pr= strat; \
138 fildebugf("@%2d", pos); \
146 for (i=0; i<nports; i++)
147 fildebugf(" %d",ports[i]);
153 static void recurse(int last_isle,
154 int nports, /* excluding last_isle */
155 int totaldist /* including last_isle */,
156 double last_estimate) {
157 ports[nports++]= last_isle;
158 double estimate= process_route(nports, totaldist, last_estimate);
159 if (nports >= MAX_ROUTELEN) return;
162 for (add= get_neighbours(last_isle); add; add=add->next) {
163 int newdist= totaldist + add->dist;
164 if (newdist > max_dist) continue;
166 recurse(add->islandid, nports, newdist, estimate);
170 void search(int start_isle, PotentialResult ****strat_base_io) {
171 strat_base= ONDEMAND(*strat_base_io, narches);
172 recurse(start_isle,0,0,1e6);
175 int nhighscores_absolute, nhighscores_perleague;
176 HighScoreEntry *highscores_absolute;
177 HighScoreEntry *highscores_perleague;
183 void setup_search(void) {
184 MCALLOC(neighbours, islandtablesz);
186 SQL_PREPARE(ss_neigh,
187 "SELECT biid, dist FROM routes WHERE aiid=?");
190 sql_single_int(" SELECT count(*) FROM (\n"
191 " SELECT DISTINCT archipelago\n"
194 MCALLOC(archnames, max_narches);
195 MCALLOC_INITEACH(islandid2arch, islandtablesz, *this=-1);
199 " SELECT islandid, archipelago\n"
201 " ORDER BY archipelago");
202 while (SQL_STEP(archs)) {
203 int isle= sqlite3_column_int(archs,0);
204 const char *archname= (const char*)sqlite3_column_text(archs,1);
206 for (arch=0; arch<narches; arch++)
207 if (!strcmp(archnames[arch], archname)) goto found;
208 assert(narches < max_narches);
210 archnames[arch]= masprintf("%s",archname);
212 islandid2arch[isle]= arch;
214 sqlite3_finalize(archs);