5 DEBUG_DEFINE_DEBUGF(search);
7 typedef struct Neighbour {
8 struct Neighbour *next;
13 static Neighbour **neighbours; /* neighbours[islandid]->islandid etc. */
14 static sqlite3_stmt *ss_neigh;
16 static int ports[MAX_ROUTELEN];
18 static Neighbour *get_neighbours(int isle) {
19 Neighbour **np= &neighbours[isle];
21 if (head) return head;
23 SQL_BIND(ss_neigh, 1, isle);
24 while (SQL_STEP(ss_neigh)) {
25 Neighbour *add= mmalloc(sizeof(*add));
26 add->islandid= sqlite3_column_int(ss_neigh, 0);
27 add->dist= sqlite3_column_int(ss_neigh, 1);
31 SQL_MUST( sqlite3_reset(ss_neigh) );
37 static double best_absolute, best_perleague;
39 static double process_route(int nports, int totaldist,
40 double overestimate_excepting_tail) {
42 int leagues_divisor= totaldist + nports;
44 ctr_routes_considered++;
46 debugf("========== ROUTE");
47 for (i=0; i<nports; i++)
48 debugf(" %d",ports[i]);
53 pair[1]= ports[nports-1];
54 double guess_absolute= overestimate_excepting_tail;
56 for (i=0; i<nports; i++) {
58 IslandPair *ip= ipair_get_maybe(pair[0], pair[1]);
60 if (ip->route_tail_value < 0) {
61 ctr_subroute_tails_valued++;
62 ip->route_tail_value= value_route(2, pair, pair[0]!=pair[1]);
64 guess_absolute += ip->route_tail_value;
66 double guess_perleague= guess_absolute / leagues_divisor;
68 if (guess_absolute <= best_absolute && guess_perleague <= best_perleague) {
69 ctr_routes_eliminated++;
70 debugf(" ELIM %f %f\n", guess_absolute, guess_perleague);
71 return guess_absolute;
73 debugf(" COMPUTE %f %f\n", guess_absolute, guess_perleague);
78 double absolute= value_route(nports, ports, 0);
79 double perleague= absolute / leagues_divisor;
81 if (absolute <= best_absolute && perleague <= best_perleague)
84 debugf(" SOMEHOW BEST\n");
86 #define CHK(absperl) \
87 fprintf(stderr,#absperl " %15f", absperl); \
88 if (absperl < best_##absperl) fputs(" ",stderr); \
89 else { best_##absperl= absperl; fputs("** ",stderr); }
94 fputs(" route",stderr);
96 for (i=0; i<nports; i++)
97 fprintf(stderr," %d",ports[i]);
103 static void recurse(int last_isle,
104 int nports, /* excluding last_isle */
105 int totaldist /* including last_isle */,
106 double last_estimate) {
107 ports[nports++]= last_isle;
108 double estimate= process_route(nports, totaldist, last_estimate);
109 if (nports >= MAX_ROUTELEN) return;
112 for (add= get_neighbours(last_isle); add; add=add->next) {
113 int newdist= totaldist + add->dist;
114 if (newdist > max_dist) continue;
116 recurse(add->islandid, nports, newdist, estimate);
120 void search(int start_isle) {
121 recurse(start_isle,0,0,1e6);
124 void setup_search(void) {
125 neighbours= mcalloc(sizeof(*neighbours) * islandtablesz);
127 SQL_PREPARE(ss_neigh,
128 "SELECT biid, dist FROM routes WHERE aiid=?");