+ 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(islands[s], islands[d]);
+ TradesBlock *block= ip->trades;
+ int tradestodo= ip->ntrades;
+ if (!tradestodo)
+ goto next_d;
+
+ int inblock= 0;
+ int col= lpx_add_cols(lp,ip->ntrades);
+
+ double loss_factor= delay_slot_loss_factor * ip->distance_loss_factor;
+
+ while (tradestodo-- >0) {
+ if (inblock >= TRADES_PER_BLOCK) {
+ block= block->next;
+ inblock= 0;
+ }
+ Trade *t= &block->t[inblock++];
+
+ debugf(" TRADE %d#%d..%d#%d %d %d-%d\n",
+ si,s, di,d, t->commodid, t->src_price, t->dst_price);
+
+ nconstraint_rows=0;
+
+ avail_c(t, &iteps[s]->collect, t->src_price,"src", si, ss_ite_sell);
+ avail_c(t, &iteps[d]->deliver, t->dst_price,"dst", di, ss_ite_buy);
+
+ int leg;
+ for (leg=s; leg<d; leg++) {
+ add_leg_c(mass_constraints, leg, commodstable[t->commodid].mass);
+ add_leg_c(volu_constraints, leg, commodstable[t->commodid].volu);
+ add_leg_c(capi_constraints, leg, t->src_price);
+ }
+
+ lpx_set_col_bnds(lp, col, LPX_LO, 0, 0);
+ lpx_set_obj_coef(lp, col,
+ (t->dst_price - t->src_price) * loss_factor);
+ lpx_set_mat_col(lp, col, nconstraint_rows,
+ constraint_rows, constraint_coeffs);
+ if (DEBUGP(value)) {
+ char *name= masprintf("trade_commod%d_port%d_at%d_port%d_at%d",
+ t->commodid, s, t->src_price, d, t->dst_price);
+ lpx_set_col_name(lp, col, name);
+ free(name);
+ }
+ } /* while (tradestodo-- >0) */
+
+ /*----- that's done adding these trades to the LP problem -----*/
+
+ next_d:;
+ } /* for (d) */
+ next_s:;
+ } /* for (s) */
+
+ lpx_delete_prob(lp);
+ lp= 0;
+}
+
+void setup_value(void) {
+ sqlite3_stmt *sst;
+ int i;
+
+ nislands= sql_single_int("SELECT max(islandid) FROM islands") + 1;
+ debugf("VALUE nislands=%d\n",nislands);
+
+ commodstablesz= sql_single_int("SELECT max(commodid) FROM commods") + 1;
+ commodstable= mmalloc(sizeof(*commodstable)*commodstablesz);
+ for (i=0; i<commodstablesz; i++)
+ commodstable[i].mass= commodstable[i].volu= -1;
+
+ SQL_MUST( sqlite3_prepare(db,
+ "SELECT commodid,unitmass,unitvolume FROM commods", -1,&sst,0) );
+ while (SQL_STEP(sst)) {
+ int id= sqlite3_column_int(sst,0);
+ assert(id>=0 && id<commodstablesz);
+ commodstable[id].mass= sqlite3_column_int(sst,1);
+ commodstable[id].volu= sqlite3_column_int(sst,2);