+ ip->route_tail_value= -1;
+
+ debugf("VALUE ipair_get(i%d,i%d) running...\n", si,di);
+ SQL_MUST( sqlite3_bind_int(ss_ipair_dist, 1, si) );
+ SQL_MUST( sqlite3_bind_int(ss_ipair_dist, 2, di) );
+ assert(SQL_STEP(ss_ipair_dist));
+ int dist= sqlite3_column_int(ss_ipair_dist, 0);
+ ip->distance_loss_factor= pow(distance_loss_factor_per_league, dist);
+ sqlite3_reset(ss_ipair_dist);
+
+ return ip;
+}
+
+double value_route(int nislands, const int *islands, int exclude_arbitrage) {
+ int s,d;
+
+ ctr_subroutes_valued++;
+
+ /* We need to construct the LP problem. GLPK talks
+ * about rows and columns, which are numbered from 1.
+ *
+ * Each column is a `structural variable' ie one of the entries in
+ * the objective function. In our case the set of structural
+ * variable is, for each port, the set of Trades which collect at
+ * that island. (We use `port' to mean `specific visit to an
+ * island' so if an island appears more than once so do its trades.)
+ * We don't need to worry about crossing with all the possible
+ * delivery locations as we always deliver on the first port.
+ * We will call such a structural variable a Flow, for brevity.
+ *
+ * We iterate over the possible Flows adding them as columns as we
+ * go, and also adding their entries to the various constraints.
+ *
+ * Each row is an `auxiliary variable' ie one of the constraints.
+ * We have two kinds of constraint:
+ * - mass/volume/capital: one constraint for each sailed leg
+ * (unless relevant constraint is not satisfied)
+ * - quantity of commodity available for collection
+ * or delivery at particular price and island
+ * The former are numbered predictably: we have first all the mass
+ * limits, then all the volume limits, then all the capital limits
+ * (as applicable) - one for each leg, ie one for each entry
+ * in islands except the first.
+ *
+ * The latter are added as needed and the row numbers are stored in
+ * a data structure for later reuse.
+ */
+
+ assert(nislands >= 1);
+ assert(++generation);
+
+ assert(!lp);
+ lp= lpx_create_prob();
+ lpx_set_obj_dir(lp, LPX_MAX);
+ lpx_set_int_parm(lp, LPX_K_MSGLEV, DEBUGP(lp) ? 3 : 1);
+ lpx_set_int_parm(lp, LPX_K_PRESOL, 1);
+
+ if (DEBUGP(value)) {
+ lpx_set_prob_name(lp,(char*)"value_route");
+ lpx_set_obj_name(lp,(char*)"profit");
+ }
+
+ int legs= nislands-1;
+ int mass_constraints= setup_leg_constraints(max_mass, legs, "mass");
+ int volu_constraints= setup_leg_constraints(max_volu, legs, "volu");
+ int capi_constraints= setup_leg_constraints(max_capi, legs, "capi");
+
+ double delay_slot_loss_factor= 1.0;
+ for (s=0;
+ s<nislands;
+ s++, delay_slot_loss_factor *= LOSS_FACTOR_PER_DELAY_SLOT) {
+ int si= islands[s];
+
+ for (d= s + exclude_arbitrage;
+ d < nislands;
+ d++) {
+ int di= islands[d];
+ int already_d;
+ for (already_d=s+1; already_d<d; already_d++)
+ if (islands[already_d] == di)
+ /* visited this island already since we left s, uninteresting */
+ goto next_d;
+
+ if (d>s && di==si)
+ /* route has returned to si, no need to think more about s */
+ goto next_s;
+
+ /*----- actually add these trades to the LP problem -----*/
+
+ IslandPair *ip= ipair_get_maybe(islands[s], islands[d]);
+
+ if (!ip || !ip->trades)
+ goto next_d;
+
+ double loss_factor= delay_slot_loss_factor * ip->distance_loss_factor;
+ debugf(" SOME i%d#%d..i%d#%d dslf=%g dlf=%g lf=%g\n",
+ si,s, di,d,
+ delay_slot_loss_factor, ip->distance_loss_factor, loss_factor);
+
+ TradesBlock *block;
+ for (block=ip->trades; block; block=block->next) {
+ int inblock;
+ for (inblock=0; inblock<block->ntrades; inblock++) {
+ Trade *t= &block->t[inblock];
+
+ debugf(" TRADE i%d#%d..i%d#%d c%d %d-%d ",
+ si,s, di,d, t->commodid, t->src_price, t->dst_price);
+
+ nconstraint_rows=0;
+
+ avail_c(t, &itradeends[si].src, t->src_price, "src", si,ss_ite_sell);
+ avail_c(t, &itradeends[di].dst, t->dst_price, "dst", di,ss_ite_buy);
+
+ int leg;
+ for (leg=s; leg<d; leg++) {
+ add_leg_c(mass_constraints,leg, commodstab[t->commodid].mass*1e-3);
+ add_leg_c(volu_constraints,leg, commodstab[t->commodid].volu*1e-3);
+ add_leg_c(capi_constraints,leg, t->src_price);
+ }