chiark / gitweb /
2c530fd70eeab4219fa4534726129458ff47d474
[ypp-sc-tools.db-live.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;
27     NEW(add);
28     add->islandid= sqlite3_column_int(ss_neigh, 0);
29     add->dist= sqlite3_column_int(ss_neigh, 1);
30     add->next= head;
31     head= add;
32   }
33   SQL_MUST( sqlite3_reset(ss_neigh) );
34
35   *np= head;
36   return head;
37 }
38
39
40 static PotentialResult ***strat_base;
41
42
43 static double process_route(int nports, int totaldist,
44                             double overestimate_excepting_tail) {
45   int i;
46   int leagues_divisor= totaldist + nports;
47
48   ctr_routes_considered++;
49
50   debugf("========== ROUTE");
51   for (i=0; i<nports; i++)
52     debugf(" %d",ports[i]);
53   debugf("\n");
54
55   double guess_absolute=0, guess_perleague=0;
56   if (nports>=2) {
57     int pair[2], i;
58     pair[1]= ports[nports-1];
59     guess_absolute= overestimate_excepting_tail;
60     
61     for (i=0; i<nports; i++) {
62       pair[0]= ports[i];
63       IslandPair *ip= ipair_get_maybe(pair[0], pair[1]);
64       if (!ip) continue;
65       if (ip->route_tail_value < 0) {
66         ctr_subroute_tails_valued++;
67         ip->route_tail_value= value_route(2, pair, pair[0]!=pair[1]);
68       }
69       guess_absolute += ip->route_tail_value;
70     }
71     guess_perleague= guess_absolute / leagues_divisor;
72
73     if (guess_absolute <= highscores_absolute[0].value &&
74         guess_perleague <= highscores_perleague[0].value) {
75       ctr_routes_quickelim++;
76       debugf(" QELIM %f %f\n", guess_absolute, guess_perleague);
77       return guess_absolute;
78     }
79   }
80
81   int finisle= ports[nports-1]; int finarch= isle2arch(finisle);
82   int midisle= ports[nports/2]; int midarch= isle2arch(midisle);
83
84   PotentialResult **strat_fin= ONDEMAND(strat_base[finarch], narches);
85   PotentialResult *strat= ONDEMAND(strat_fin[midarch], 1);
86
87   if (nports>=2) {
88     if (guess_absolute <= strat->absolute &&
89         guess_perleague <= strat->perleague) {
90       ctr_routes_stratelim++;
91       debugf(" ELIM %f %f\n", guess_absolute, guess_perleague);
92       return guess_absolute;
93     }
94     debugf(" COMPUTE %f %f\n", guess_absolute, guess_perleague);
95   }
96
97   ctr_routes_valued++;
98
99   double absolute= value_route(nports, ports, 0);
100   double perleague= absolute / leagues_divisor;
101
102   if (absolute <= strat->absolute &&
103       perleague <= strat->perleague)
104     return absolute;
105
106   debugf(" SOMEHOW BEST\n");
107
108   fildebugf("final %d:%3d mid %d:%3d ",finarch,finisle,midarch,midisle);
109
110 #define CHK(absperl)                                                          \
111   fildebugf(#absperl " %15f", absperl);                                       \
112   if (absperl < strat->absperl) {                                             \
113     debugf("      ");                                                         \
114   } else {                                                                    \
115     int pos;                                                                  \
116     ctr_newbests_strat_##absperl++;                                           \
117     strat->absperl= absperl;                                                  \
118     memcpy(strat->absperl##_ports, ports, sizeof(*ports) * nports);           \
119     if (nports < MAX_ROUTELEN-1) strat->absperl##_ports[nports]= -1;          \
120     fildebugf("** ");                                                         \
121     for (pos=0; pos < nhighscores_##absperl; pos++)                           \
122       if (highscores_##absperl[pos].pr == strat) goto found_##absperl;        \
123     /* not found */                                                           \
124     pos= -1;                                                                  \
125    found_##absperl:                                                           \
126     for (;;) {                                                                \
127       pos++;                                                                  \
128       if (pos >= nhighscores_##absperl-1) break; /* new top */                \
129       if (highscores_##absperl[pos].value >= absperl) break; /* found spot */ \
130       if (pos>0)                                                              \
131         highscores_##absperl[pos-1]= highscores_##absperl[pos];               \
132     }                                                                         \
133     pos--;                                                                    \
134     if (pos>0) {                                                              \
135       highscores_##absperl[pos].value= absperl;                               \
136       highscores_##absperl[pos].pr= strat;                                    \
137     }                                                                         \
138     fildebugf("@%2d", pos);                                                   \
139   }
140
141   CHK(absolute)
142   CHK(perleague)
143
144   fildebugf(" route");
145
146   for (i=0; i<nports; i++)
147     fildebugf(" %d",ports[i]);
148   fildebugf("\n");
149
150   return absolute;
151 }
152
153 static void recurse(int last_isle,
154                     int nports, /* excluding last_isle */
155                     int totaldist /* including last_isle */,
156                     double last_estimate) {
157   ports[nports++]= last_isle;
158   double estimate= process_route(nports, totaldist, last_estimate);
159   if (nports >= MAX_ROUTELEN) return;
160
161   Neighbour *add;
162   for (add= get_neighbours(last_isle); add; add=add->next) {
163     int newdist= totaldist + add->dist;
164     if (newdist > max_dist) continue;
165
166     recurse(add->islandid, nports, newdist, estimate);
167   }
168 }
169
170 void search(int start_isle, PotentialResult ****strat_base_io) {
171   strat_base= ONDEMAND(*strat_base_io, narches);
172   recurse(start_isle,0,0,1e6);
173 }
174
175 int nhighscores_absolute, nhighscores_perleague;
176 HighScoreEntry *highscores_absolute;
177 HighScoreEntry *highscores_perleague;
178
179 int narches;
180 char **archnames;
181 int *islandid2arch;
182
183 void setup_search(void) {
184   MCALLOC(neighbours, islandtablesz);
185
186   SQL_PREPARE(ss_neigh,
187               "SELECT biid, dist FROM routes WHERE aiid=?");
188
189   int max_narches=
190     sql_single_int(" SELECT count(*) FROM (\n"
191                    "  SELECT DISTINCT archipelago\n"
192                    "   FROM islands\n"
193                    "  )");
194   MCALLOC(archnames, max_narches);
195   MCALLOC_INITEACH(islandid2arch, islandtablesz, *this=-1);
196
197   sqlite3_stmt *archs;
198   SQL_PREPARE(archs,
199               " SELECT islandid, archipelago\n"
200               "  FROM islands\n"
201               "  ORDER BY archipelago");
202   while (SQL_STEP(archs)) {
203     int isle= sqlite3_column_int(archs,0);
204     const char *archname= (const char*)sqlite3_column_text(archs,1);
205     int arch;
206     for (arch=0; arch<narches; arch++)
207       if (!strcmp(archnames[arch], archname)) goto found;
208     assert(narches < max_narches);
209     arch= narches++;
210     archnames[arch]= masprintf("%s",archname);
211   found:
212     islandid2arch[isle]= arch;
213   }
214   sqlite3_finalize(archs);
215 }