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 debugf("========== ROUTE");
45 for (i=0; i<nports; i++)
46 debugf(" %d",ports[i]);
51 pair[1]= ports[nports-1];
52 double guess_absolute= overestimate_excepting_tail;
54 for (i=0; i<nports; i++) {
56 IslandPair *ip= ipair_get_maybe(ports[nports-2], ports[nports-1]);
58 if (ip->route_tail_value < 0)
59 ip->route_tail_value= value_route(2, pair, pair[0]!=pair[1]);
60 guess_absolute += ip->route_tail_value;
62 double guess_perleague= guess_absolute / leagues_divisor;
64 if (guess_absolute <= best_absolute && guess_perleague <= best_perleague) {
65 debugf(" ELIM %f %f\n", guess_absolute, guess_perleague);
66 return guess_absolute;
68 debugf(" COMPUTE %f %f\n", guess_absolute, guess_perleague);
71 double absolute= value_route(nports, ports, 0);
72 double perleague= absolute / leagues_divisor;
74 if (absolute <= best_absolute && perleague <= best_perleague)
77 debugf(" SOMEHOW BEST\n");
79 #define CHK(absperl) \
80 fprintf(stderr,#absperl " %15f", absperl); \
81 if (absperl < best_##absperl) fputs(" ",stderr); \
82 else { best_##absperl= absperl; fputs("** ",stderr); }
87 fputs(" route",stderr);
89 for (i=0; i<nports; i++)
90 fprintf(stderr," %d",ports[i]);
96 static void recurse(int last_isle,
97 int nports, /* excluding last_isle */
98 int totaldist /* including last_isle */,
99 double last_estimate) {
100 ports[nports++]= last_isle;
101 double estimate= process_route(nports, totaldist, last_estimate);
102 if (nports >= MAX_ROUTELEN) return;
105 for (add= get_neighbours(last_isle); add; add=add->next) {
106 int newdist= totaldist + add->dist;
107 if (newdist > max_dist) continue;
109 recurse(add->islandid, nports, newdist, estimate);
113 void search(int start_isle) {
114 recurse(start_isle,0,0,1e6);
117 void setup_search(void) {
118 neighbours= mcalloc(sizeof(*neighbours) * islandtablesz);
120 SQL_PREPARE(ss_neigh,
121 "SELECT biid, dist FROM routes WHERE aiid=?");