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)) {
26 Neighbour *add= mmalloc(sizeof(*add));
27 add->islandid= sqlite3_column_int(ss_neigh, 0);
28 add->dist= sqlite3_column_int(ss_neigh, 1);
32 SQL_MUST( sqlite3_reset(ss_neigh) );
39 static PotentialResult ***strat_base;
42 static inline int isle2arch(int isle) {
43 int arch= islandid2arch[isle];
48 static double process_route(int nports, int totaldist,
49 double overestimate_excepting_tail) {
51 int leagues_divisor= totaldist + nports;
53 ctr_routes_considered++;
55 debugf("========== ROUTE");
56 for (i=0; i<nports; i++)
57 debugf(" %d",ports[i]);
60 int finisle= ports[nports-1]; int finarch= isle2arch(finisle);
61 int midisle= ports[nports/2]; int midarch= isle2arch(midisle);
63 PotentialResult **strat_fin= ONDEMAND(strat_base[finarch], narches);
64 PotentialResult *strat= ONDEMAND(strat_fin[midarch], 1);
68 pair[1]= ports[nports-1];
69 double guess_absolute= overestimate_excepting_tail;
71 for (i=0; i<nports; i++) {
73 IslandPair *ip= ipair_get_maybe(pair[0], pair[1]);
75 if (ip->route_tail_value < 0) {
76 ctr_subroute_tails_valued++;
77 ip->route_tail_value= value_route(2, pair, pair[0]!=pair[1]);
79 guess_absolute += ip->route_tail_value;
81 double guess_perleague= guess_absolute / leagues_divisor;
83 if (guess_absolute <= strat->absolute &&
84 guess_perleague <= strat->perleague) {
85 ctr_routes_eliminated++;
86 debugf(" ELIM %f %f\n", guess_absolute, guess_perleague);
87 return guess_absolute;
89 debugf(" COMPUTE %f %f\n", guess_absolute, guess_perleague);
94 double absolute= value_route(nports, ports, 0);
95 double perleague= absolute / leagues_divisor;
97 if (absolute <= strat->absolute &&
98 perleague <= strat->perleague)
101 debugf(" SOMEHOW BEST\n");
103 fildebugf("final %d:%3d mid %d:%3d ",finarch,finisle,midarch,midisle);
105 #define CHK(absperl) \
106 fildebugf(#absperl " %15f", absperl); \
107 if (absperl < strat->absperl) { \
110 strat->absperl= absperl; \
111 memcpy(strat->absperl##_ports, ports, sizeof(*ports) * nports); \
120 for (i=0; i<nports; i++)
121 fildebugf(" %d",ports[i]);
127 static void recurse(int last_isle,
128 int nports, /* excluding last_isle */
129 int totaldist /* including last_isle */,
130 double last_estimate) {
131 ports[nports++]= last_isle;
132 double estimate= process_route(nports, totaldist, last_estimate);
133 if (nports >= MAX_ROUTELEN) return;
136 for (add= get_neighbours(last_isle); add; add=add->next) {
137 int newdist= totaldist + add->dist;
138 if (newdist > max_dist) continue;
140 recurse(add->islandid, nports, newdist, estimate);
144 void search(int start_isle, PotentialResult ****strat_base_io) {
145 strat_base= ONDEMAND(*strat_base_io, narches);
146 recurse(start_isle,0,0,1e6);
154 void setup_search(void) {
155 neighbours= mcalloc(sizeof(*neighbours) * islandtablesz);
157 SQL_PREPARE(ss_neigh,
158 "SELECT biid, dist FROM routes WHERE aiid=?");
161 sql_single_int(" SELECT count(*) FROM (\n"
162 " SELECT DISTINCT archipelago\n"
165 archnames= mcalloc(sizeof(*archnames) * max_narches);
166 islandid2arch= mmalloc(sizeof(*islandid2arch) * islandtablesz);
168 for (i=0; i<islandtablesz; i++) islandid2arch[i]=-1;
172 " SELECT islandid, archipelago\n"
174 " ORDER BY archipelago");
175 while (SQL_STEP(archs)) {
176 int isle= sqlite3_column_int(archs,0);
177 const char *archname= (const char*)sqlite3_column_text(archs,1);
179 for (arch=0; arch<narches; arch++)
180 if (!strcmp(archnames[arch], archname)) goto found;
181 assert(narches < max_narches);
183 archnames[arch]= masprintf("%s",archname);
185 islandid2arch[isle]= arch;
187 sqlite3_finalize(archs);