chiark / gitweb /
87914a8cd89232748821b717791ade6810e6196e
[ypp-sc-tools.main.git] / yarrg / rssearch.c
1 /**/
2
3 #include "rscommon.h"
4
5 DEBUG_DEFINE_DEBUGF(search);
6
7 typedef struct Neighbour {
8   struct Neighbour *next;
9   int islandid;
10   int dist;
11 } Neighbour;
12
13 static Neighbour **neighbours; /* neighbours[islandid]->islandid etc. */
14 static sqlite3_stmt *ss_neigh;
15
16 static int ports[MAX_ROUTELEN];
17
18 static Neighbour *get_neighbours(int isle) {
19   Neighbour **np= &neighbours[isle];
20   Neighbour *head= *np;
21   if (head) return head;
22
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);
28     add->next= head;
29     head= add;
30   }
31   SQL_MUST( sqlite3_reset(ss_neigh) );
32
33   *np= head;
34   return head;
35 }
36
37 static double best_absolute, best_perleague;
38
39 static double process_route(int nports, int totaldist,
40                             double overestimate_excepting_tail) {
41   int i;
42   int leagues_divisor= totaldist + nports;
43
44   ctr_routes_considered++;
45
46   debugf("========== ROUTE");
47   for (i=0; i<nports; i++)
48     debugf(" %d",ports[i]);
49   debugf("\n");
50
51   if (nports>=2) {
52     int pair[2], i;
53     pair[1]= ports[nports-1];
54     double guess_absolute= overestimate_excepting_tail;
55     
56     for (i=0; i<nports; i++) {
57       pair[0]= ports[i];
58       IslandPair *ip= ipair_get_maybe(pair[0], pair[1]);
59       if (!ip) continue;
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]);
63       }
64       guess_absolute += ip->route_tail_value;
65     }
66     double guess_perleague= guess_absolute / leagues_divisor;
67
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;
72     }
73     debugf(" COMPUTE %f %f\n", guess_absolute, guess_perleague);
74   }
75
76   ctr_routes_valued++;
77
78   double absolute= value_route(nports, ports, 0);
79   double perleague= absolute / leagues_divisor;
80
81   if (absolute <= best_absolute && perleague <= best_perleague)
82     return absolute;
83
84   debugf(" SOMEHOW BEST\n");
85
86 #define CHK(absperl)                                    \
87   fprintf(stderr,#absperl " %15f", absperl);            \
88   if (absperl < best_##absperl) fputs("   ",stderr);    \
89   else { best_##absperl= absperl; fputs("** ",stderr); }
90
91   CHK(absolute)
92   CHK(perleague)
93
94   fputs(" route",stderr);
95
96   for (i=0; i<nports; i++)
97     fprintf(stderr," %d",ports[i]);
98   putc('\n',stderr);
99
100   return absolute;
101 }
102
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;
110
111   Neighbour *add;
112   for (add= get_neighbours(last_isle); add; add=add->next) {
113     int newdist= totaldist + add->dist;
114     if (newdist > max_dist) continue;
115
116     recurse(add->islandid, nports, newdist, estimate);
117   }
118 }
119
120 void search(int start_isle) {
121   recurse(start_isle,0,0,1e6);
122 }
123
124 void setup_search(void) {
125   neighbours= mcalloc(sizeof(*neighbours) * islandtablesz);
126
127   SQL_PREPARE(ss_neigh,
128               "SELECT biid, dist FROM routes WHERE aiid=?");
129 }