chiark / gitweb /
WIP routesearch; stratify by archipelagoes - much better
[ypp-sc-tools.db-test.git] / yarrg / rssearch.c
1 /**/
2
3 #include "rscommon.h"
4
5 DEBUG_DEFINE_DEBUGF(search);
6 DEBUG_DEFINE_SOME_DEBUGF(filter,fildebugf);
7
8 typedef struct Neighbour {
9   struct Neighbour *next;
10   int islandid;
11   int dist;
12 } Neighbour;
13
14 static Neighbour **neighbours; /* neighbours[islandid]->islandid etc. */
15 static sqlite3_stmt *ss_neigh;
16
17 static int ports[MAX_ROUTELEN];
18
19 static Neighbour *get_neighbours(int isle) {
20   Neighbour **np= &neighbours[isle];
21   Neighbour *head= *np;
22   if (head) return head;
23
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);
29     add->next= head;
30     head= add;
31   }
32   SQL_MUST( sqlite3_reset(ss_neigh) );
33
34   *np= head;
35   return head;
36 }
37
38
39 static PotentialResult ***strat_base;
40
41
42 static inline int isle2arch(int isle) {
43   int arch= islandid2arch[isle];
44   assert(arch>=0);
45   return arch;
46 }
47
48 static double process_route(int nports, int totaldist,
49                             double overestimate_excepting_tail) {
50   int i;
51   int leagues_divisor= totaldist + nports;
52
53   ctr_routes_considered++;
54
55   debugf("========== ROUTE");
56   for (i=0; i<nports; i++)
57     debugf(" %d",ports[i]);
58   debugf("\n");
59
60   int finisle= ports[nports-1]; int finarch= isle2arch(finisle);
61   int midisle= ports[nports/2]; int midarch= isle2arch(midisle);
62
63   PotentialResult **strat_fin= ONDEMAND(strat_base[finarch], narches);
64   PotentialResult *strat= ONDEMAND(strat_fin[midarch], 1);
65
66   if (nports>=2) {
67     int pair[2], i;
68     pair[1]= ports[nports-1];
69     double guess_absolute= overestimate_excepting_tail;
70     
71     for (i=0; i<nports; i++) {
72       pair[0]= ports[i];
73       IslandPair *ip= ipair_get_maybe(pair[0], pair[1]);
74       if (!ip) continue;
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]);
78       }
79       guess_absolute += ip->route_tail_value;
80     }
81     double guess_perleague= guess_absolute / leagues_divisor;
82
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;
88     }
89     debugf(" COMPUTE %f %f\n", guess_absolute, guess_perleague);
90   }
91
92   ctr_routes_valued++;
93
94   double absolute= value_route(nports, ports, 0);
95   double perleague= absolute / leagues_divisor;
96
97   if (absolute <= strat->absolute &&
98       perleague <= strat->perleague)
99     return absolute;
100
101   debugf(" SOMEHOW BEST\n");
102
103   fildebugf("final %d:%3d mid %d:%3d ",finarch,finisle,midarch,midisle);
104
105 #define CHK(absperl)                                                    \
106   fildebugf(#absperl " %15f", absperl);                                 \
107   if (absperl < strat->absperl) {                                       \
108     debugf("   ");                                                      \
109   } else {                                                              \
110     strat->absperl= absperl;                                            \
111     memcpy(strat->absperl##_ports, ports, sizeof(*ports) * nports);     \
112     fildebugf("** ");                                                   \
113   }
114
115   CHK(absolute)
116   CHK(perleague)
117
118   fildebugf(" route");
119
120   for (i=0; i<nports; i++)
121     fildebugf(" %d",ports[i]);
122   fildebugf("\n");
123
124   return absolute;
125 }
126
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;
134
135   Neighbour *add;
136   for (add= get_neighbours(last_isle); add; add=add->next) {
137     int newdist= totaldist + add->dist;
138     if (newdist > max_dist) continue;
139
140     recurse(add->islandid, nports, newdist, estimate);
141   }
142 }
143
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);
147 }
148
149
150 int narches;
151 char **archnames;
152 int *islandid2arch;
153
154 void setup_search(void) {
155   neighbours= mcalloc(sizeof(*neighbours) * islandtablesz);
156
157   SQL_PREPARE(ss_neigh,
158               "SELECT biid, dist FROM routes WHERE aiid=?");
159
160   int max_narches=
161     sql_single_int(" SELECT count(*) FROM (\n"
162                    "  SELECT DISTINCT archipelago\n"
163                    "   FROM islands\n"
164                    "  )");
165   archnames= mcalloc(sizeof(*archnames) * max_narches);
166   islandid2arch= mmalloc(sizeof(*islandid2arch) * islandtablesz);
167   int i;
168   for (i=0; i<islandtablesz; i++) islandid2arch[i]=-1;
169
170   sqlite3_stmt *archs;
171   SQL_PREPARE(archs,
172               " SELECT islandid, archipelago\n"
173               "  FROM islands\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);
178     int arch;
179     for (arch=0; arch<narches; arch++)
180       if (!strcmp(archnames[arch], archname)) goto found;
181     assert(narches < max_narches);
182     arch= narches++;
183     archnames[arch]= masprintf("%s",archname);
184   found:
185     islandid2arch[isle]= arch;
186   }
187   sqlite3_finalize(archs);
188 }