+ 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);
+ }
+
+ double unit_profit= t->dst_price * loss_factor - t->src_price;
+ debugf(" unit profit %f\n", unit_profit);
+ if (unit_profit <= 0) continue;
+
+ int col= lpx_add_cols(lp,1);
+ lpx_set_col_bnds(lp, col, LPX_LO, 0, 0);
+ lpx_set_obj_coef(lp, col, unit_profit);
+ lpx_set_mat_col(lp, col, nconstraint_rows,
+ constraint_rows, constraint_coeffs);
+
+ if (DEBUGP(value)) {
+ char *name= masprintf("c%d_p%d_%d_p%d_%d", t->commodid,
+ s, t->src_price, d, t->dst_price);
+ lpx_set_col_name(lp, col, name);
+ free(name);
+ }
+ } /* inblock */
+ } /* block */
+
+ /*----- that's done adding these trades to the LP problem -----*/
+
+ next_d:;
+ } /* for (d) */
+ next_s:;
+ } /* for (s) */
+
+ double profit= 0;
+
+ if (lpx_get_num_cols(lp)) {
+ if (DEBUGP(lp))
+ lpx_write_cpxlp(lp, (char*)DEBUG_DEV);
+
+ int ipr= lpx_simplex(lp);
+ assert(ipr==LPX_E_OK);
+
+ if (DEBUGP(lp))
+ lpx_print_sol(lp, (char*)DEBUG_DEV);
+
+ int lpst= lpx_get_status(lp);
+ assert(lpst == LPX_OPT);
+ profit= lpx_get_obj_val(lp);
+ }
+
+ lpx_delete_prob(lp);
+ lp= 0;
+
+ return profit;
+}
+
+#define TRADE_FROM \
+ " FROM sell, buy\n" \
+ " WHERE sell.commodid=buy.commodid AND sell.price < buy.price\n"
+
+static void read_trades(void) {
+ /* We would like to use DISTINCT but sqlite3 is too stupid
+ * to notice that it could use the index to do the DISTINCT
+ * which makes it rather slow. */
+ sqlite3_stmt *ss_trades;
+
+#define TRADE_COLS \
+ "sell.commodid, sell.islandid, sell.price, buy.islandid, buy.price"
+ SQL_PREPARE(ss_trades,
+ " SELECT " TRADE_COLS "\n"
+ TRADE_FROM
+ " ORDER BY " TRADE_COLS);
+
+ SQL_DISTINCT_DECL(cols,5);
+ while (SQL_DISTINCT_STEP(ss_trades,cols,5)) {
+ IslandPair *ip= ipair_get_create(cols[1], cols[3]);
+ TradesBlock *block= ip->trades;
+ if (!block || ip->trades->ntrades >= TRADES_PER_BLOCK) {