197 |
|
int j; |
198 |
|
|
199 |
|
if (amt2move <= FTINY) /* pre-emptive check */ |
200 |
< |
return(0.); |
200 |
> |
return(.0); |
201 |
|
/* move cheapest first */ |
202 |
|
for (j = 0; j < pm->ncols && amt2move > FTINY; j++) { |
203 |
|
int d = psortrow(pm,s)[j]; |
213 |
|
static double |
214 |
|
migration_step(MIGRATION *mig, double *src_rem, double *dst_rem, const PRICEMAT *pm) |
215 |
|
{ |
216 |
< |
const double maxamt = .1; |
216 |
> |
const double maxamt = 1./(double)pm->ncols; |
217 |
|
const double minamt = maxamt*5e-6; |
218 |
< |
static double *src_cost = NULL; |
219 |
< |
static int n_alloc = 0; |
218 |
> |
double *src_cost; |
219 |
|
struct { |
220 |
|
int s, d; /* source and destination */ |
221 |
|
double price; /* price estimate per amount moved */ |
222 |
|
double amt; /* amount we can move */ |
223 |
|
} cur, best; |
224 |
|
int i; |
225 |
< |
|
226 |
< |
if (pm->nrows > n_alloc) { /* allocate cost array */ |
227 |
< |
if (n_alloc) |
228 |
< |
free(src_cost); |
229 |
< |
src_cost = (double *)malloc(sizeof(double)*pm->nrows); |
230 |
< |
if (src_cost == NULL) { |
232 |
< |
fprintf(stderr, "%s: Out of memory in migration_step()\n", |
233 |
< |
progname); |
234 |
< |
exit(1); |
235 |
< |
} |
236 |
< |
n_alloc = pm->nrows; |
225 |
> |
/* allocate cost array */ |
226 |
> |
src_cost = (double *)malloc(sizeof(double)*pm->nrows); |
227 |
> |
if (src_cost == NULL) { |
228 |
> |
fprintf(stderr, "%s: Out of memory in migration_step()\n", |
229 |
> |
progname); |
230 |
> |
exit(1); |
231 |
|
} |
232 |
|
for (i = pm->nrows; i--; ) /* starting costs for diff. */ |
233 |
|
src_cost[i] = min_cost(src_rem[i], dst_rem, pm, i); |
235 |
|
/* find best source & dest. */ |
236 |
|
best.s = best.d = -1; best.price = FHUGE; best.amt = 0; |
237 |
|
for (cur.s = pm->nrows; cur.s--; ) { |
244 |
– |
const float *price = pricerow(pm,cur.s); |
238 |
|
double cost_others = 0; |
239 |
+ |
|
240 |
|
if (src_rem[cur.s] <= minamt) |
241 |
|
continue; |
242 |
< |
cur.d = -1; /* examine cheapest dest. */ |
243 |
< |
for (i = pm->ncols; i--; ) |
244 |
< |
if (dst_rem[i] > minamt && |
245 |
< |
(cur.d < 0 || price[i] < price[cur.d])) |
246 |
< |
cur.d = i; |
247 |
< |
if (cur.d < 0) |
248 |
< |
return(.0); |
249 |
< |
if ((cur.price = price[cur.d]) >= best.price) |
256 |
< |
continue; /* no point checking further */ |
242 |
> |
/* examine cheapest dest. */ |
243 |
> |
for (i = 0; i < pm->ncols; i++) |
244 |
> |
if (dst_rem[ cur.d = psortrow(pm,cur.s)[i] ] > minamt) |
245 |
> |
break; |
246 |
> |
if (i >= pm->ncols) |
247 |
> |
break; |
248 |
> |
if ((cur.price = pricerow(pm,cur.s)[cur.d]) >= best.price) |
249 |
> |
continue; /* no point checking further */ |
250 |
|
cur.amt = (src_rem[cur.s] < dst_rem[cur.d]) ? |
251 |
|
src_rem[cur.s] : dst_rem[cur.d]; |
252 |
|
if (cur.amt > maxamt) cur.amt = maxamt; |
260 |
|
if (cur.price < best.price) /* are we better than best? */ |
261 |
|
best = cur; |
262 |
|
} |
263 |
< |
if ((best.s < 0) | (best.d < 0)) |
263 |
> |
free(src_cost); /* finish up */ |
264 |
> |
|
265 |
> |
if ((best.s < 0) | (best.d < 0)) /* nothing left to move? */ |
266 |
|
return(.0); |
267 |
< |
/* make the actual move */ |
267 |
> |
/* else make the actual move */ |
268 |
|
mtx_coef(mig,best.s,best.d) += best.amt; |
269 |
|
src_rem[best.s] -= best.amt; |
270 |
|
dst_rem[best.d] -= best.amt; |