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[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[A][0].value &&
83 guess[P] <= highscores[P][0].value) {
84 ctr_routes_quickelim++;
85 debugf(" QELIM %f %f\n", guess[A], guess[P]);
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[A] <= strat->value[A] &&
98 guess[P] <= strat->value[P]) {
99 ctr_routes_stratelim++;
100 debugf(" ELIM %f %f\n", guess[A], guess[P]);
103 debugf(" COMPUTE %f %f\n", guess[A], guess[P]);
109 value[A]= value_route(nports, ports, 0);
110 value[P]= value[A] / leagues_divisor;
113 ctr_routes_wrongfinal++;
117 if (value[A] <= strat->value[A] &&
118 value[P] <= strat->value[P])
121 debugf(" SOMEHOW BEST\n");
123 fildebugf("final %d:%3d mid %d ",finarch,finisle,midarch);
125 for (ap=0; ap<AP; ap++) {
126 fildebugf("ap=%d %15f", ap, value[ap]);
127 if (value[ap] < strat->value[ap]) {
131 ctr_newbests_strat[ap]++;
132 strat->value[ap]= value[ap];
133 memcpy(strat->ports[ap], ports, sizeof(*ports) * nports);
134 if (nports < MAX_ROUTELEN-1) strat->ports[ap][nports]= -1;
136 for (pos=0; pos < nhighscores[ap]; pos++)
137 if (highscores[ap][pos].pr == strat) goto found;
143 if (pos >= nhighscores[ap]-1) break; /* new top */
144 if (highscores[ap][pos].value >= value[ap]) break; /* found spot */
146 highscores[ap][pos-1]= highscores[ap][pos];
150 highscores[ap][pos].value= value[ap];
151 highscores[ap][pos].pr= strat;
153 fildebugf("@%2d", pos);
159 for (i=0; i<nports; i++)
160 fildebugf(" %d",ports[i]);
166 static void recurse(int last_isle,
167 int nports, /* excluding last_isle */
168 int totaldist /* including last_isle */,
169 double last_estimate) {
170 ports[nports++]= last_isle;
171 double estimate= process_route(nports, totaldist, last_estimate);
172 if (nports >= MAX_ROUTELEN) return;
175 for (add= get_neighbours(last_isle); add; add=add->next) {
176 int newdist= totaldist + add->dist;
177 if (newdist > max_dist) continue;
179 recurse(add->islandid, nports, newdist, estimate);
183 void search(int start_isle, int final_isle_spec,
184 PotentialResult ****strat_base_io) {
185 strat_base= ONDEMAND(*strat_base_io, narches);
186 final_isle= final_isle_spec <= 0 ? 0 : final_isle_spec;
187 recurse(start_isle,0,0,1e6);
191 HighScoreEntry *highscores[AP];
197 void setup_search(void) {
198 MCALLOC(neighbours, islandtablesz);
200 SQL_PREPARE(ss_neigh,
201 "SELECT biid, dist FROM routes WHERE aiid=?");
204 sql_single_int(" SELECT count(*) FROM (\n"
205 " SELECT DISTINCT archipelago\n"
208 MCALLOC(archnames, max_narches);
209 MCALLOC_INITEACH(islandid2arch, islandtablesz, *this=-1);
213 " SELECT islandid, archipelago\n"
215 " ORDER BY archipelago");
216 while (SQL_STEP(archs)) {
217 int isle= sqlite3_column_int(archs,0);
218 const char *archname= (const char*)sqlite3_column_text(archs,1);
220 for (arch=0; arch<narches; arch++)
221 if (!strcmp(archnames[arch], archname)) goto found;
222 assert(narches < max_narches);
224 archnames[arch]= masprintf("%s",archname);
226 islandid2arch[isle]= arch;
228 sqlite3_finalize(archs);