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