119 |
|
if (pid < 0) { |
120 |
|
fprintf(stderr, "%s: cannot fork subprocess\n", |
121 |
|
progname); |
122 |
+ |
await_children(nchild); |
123 |
|
exit(1); |
124 |
|
} |
125 |
|
++nchild; /* subprocess started */ |
198 |
|
int j; |
199 |
|
|
200 |
|
if (amt2move <= FTINY) /* pre-emptive check */ |
201 |
< |
return(0.); |
201 |
> |
return(.0); |
202 |
|
/* move cheapest first */ |
203 |
|
for (j = 0; j < pm->ncols && amt2move > FTINY; j++) { |
204 |
|
int d = psortrow(pm,s)[j]; |
214 |
|
static double |
215 |
|
migration_step(MIGRATION *mig, double *src_rem, double *dst_rem, const PRICEMAT *pm) |
216 |
|
{ |
217 |
< |
const double maxamt = .1; |
217 |
> |
const double maxamt = 1./(double)pm->ncols; |
218 |
|
const double minamt = maxamt*5e-6; |
219 |
< |
static double *src_cost = NULL; |
219 |
< |
static int n_alloc = 0; |
219 |
> |
double *src_cost; |
220 |
|
struct { |
221 |
|
int s, d; /* source and destination */ |
222 |
|
double price; /* price estimate per amount moved */ |
223 |
|
double amt; /* amount we can move */ |
224 |
|
} cur, best; |
225 |
|
int i; |
226 |
< |
|
227 |
< |
if (pm->nrows > n_alloc) { /* allocate cost array */ |
228 |
< |
if (n_alloc) |
229 |
< |
free(src_cost); |
230 |
< |
src_cost = (double *)malloc(sizeof(double)*pm->nrows); |
231 |
< |
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; |
226 |
> |
/* allocate cost array */ |
227 |
> |
src_cost = (double *)malloc(sizeof(double)*pm->nrows); |
228 |
> |
if (src_cost == NULL) { |
229 |
> |
fprintf(stderr, "%s: Out of memory in migration_step()\n", |
230 |
> |
progname); |
231 |
> |
exit(1); |
232 |
|
} |
233 |
|
for (i = pm->nrows; i--; ) /* starting costs for diff. */ |
234 |
|
src_cost[i] = min_cost(src_rem[i], dst_rem, pm, i); |
236 |
|
/* find best source & dest. */ |
237 |
|
best.s = best.d = -1; best.price = FHUGE; best.amt = 0; |
238 |
|
for (cur.s = pm->nrows; cur.s--; ) { |
244 |
– |
const float *price = pricerow(pm,cur.s); |
239 |
|
double cost_others = 0; |
240 |
+ |
|
241 |
|
if (src_rem[cur.s] <= minamt) |
242 |
|
continue; |
243 |
< |
cur.d = -1; /* examine cheapest dest. */ |
244 |
< |
for (i = pm->ncols; i--; ) |
245 |
< |
if (dst_rem[i] > minamt && |
246 |
< |
(cur.d < 0 || price[i] < price[cur.d])) |
247 |
< |
cur.d = i; |
248 |
< |
if (cur.d < 0) |
249 |
< |
return(.0); |
250 |
< |
if ((cur.price = price[cur.d]) >= best.price) |
256 |
< |
continue; /* no point checking further */ |
243 |
> |
/* examine cheapest dest. */ |
244 |
> |
for (i = 0; i < pm->ncols; i++) |
245 |
> |
if (dst_rem[ cur.d = psortrow(pm,cur.s)[i] ] > minamt) |
246 |
> |
break; |
247 |
> |
if (i >= pm->ncols) |
248 |
> |
break; |
249 |
> |
if ((cur.price = pricerow(pm,cur.s)[cur.d]) >= best.price) |
250 |
> |
continue; /* no point checking further */ |
251 |
|
cur.amt = (src_rem[cur.s] < dst_rem[cur.d]) ? |
252 |
|
src_rem[cur.s] : dst_rem[cur.d]; |
253 |
|
if (cur.amt > maxamt) cur.amt = maxamt; |
261 |
|
if (cur.price < best.price) /* are we better than best? */ |
262 |
|
best = cur; |
263 |
|
} |
264 |
< |
if ((best.s < 0) | (best.d < 0)) |
264 |
> |
free(src_cost); /* finish up */ |
265 |
> |
|
266 |
> |
if ((best.s < 0) | (best.d < 0)) /* nothing left to move? */ |
267 |
|
return(.0); |
268 |
< |
/* make the actual move */ |
268 |
> |
/* else make the actual move */ |
269 |
|
mtx_coef(mig,best.s,best.d) += best.amt; |
270 |
|
src_rem[best.s] -= best.amt; |
271 |
|
dst_rem[best.d] -= best.amt; |
272 |
|
return(best.amt); |
273 |
|
} |
274 |
|
|
279 |
– |
#ifdef DEBUG |
280 |
– |
static char * |
281 |
– |
thetaphi(const FVECT v) |
282 |
– |
{ |
283 |
– |
static char buf[128]; |
284 |
– |
double theta, phi; |
285 |
– |
|
286 |
– |
theta = 180./M_PI*acos(v[2]); |
287 |
– |
phi = 180./M_PI*atan2(v[1],v[0]); |
288 |
– |
sprintf(buf, "(%.0f,%.0f)", theta, phi); |
289 |
– |
|
290 |
– |
return(buf); |
291 |
– |
} |
292 |
– |
#endif |
293 |
– |
|
275 |
|
/* Compute and insert migration along directed edge (may fork child) */ |
276 |
|
static MIGRATION * |
277 |
|
create_migration(RBFNODE *from_rbf, RBFNODE *to_rbf) |
281 |
|
MIGRATION *newmig; |
282 |
|
double *src_rem, *dst_rem; |
283 |
|
double total_rem = 1., move_amt; |
284 |
< |
int i; |
284 |
> |
int i, j; |
285 |
|
/* check if exists already */ |
286 |
|
for (newmig = from_rbf->ejl; newmig != NULL; |
287 |
|
newmig = nextedge(from_rbf,newmig)) |
300 |
|
exit(1); |
301 |
|
} |
302 |
|
#ifdef DEBUG |
303 |
< |
fprintf(stderr, "Building path from (theta,phi) %s ", |
304 |
< |
thetaphi(from_rbf->invec)); |
305 |
< |
fprintf(stderr, "to %s with %d x %d matrix\n", |
306 |
< |
thetaphi(to_rbf->invec), |
303 |
> |
fprintf(stderr, "Building path from (theta,phi) (%.0f,%.0f) ", |
304 |
> |
get_theta180(from_rbf->invec), |
305 |
> |
get_phi360(from_rbf->invec)); |
306 |
> |
fprintf(stderr, "to (%.0f,%.0f) with %d x %d matrix\n", |
307 |
> |
get_theta180(to_rbf->invec), |
308 |
> |
get_phi360(to_rbf->invec), |
309 |
|
from_rbf->nrbf, to_rbf->nrbf); |
310 |
|
#endif |
311 |
|
/* starting quantities */ |
312 |
|
memset(newmig->mtx, 0, sizeof(float)*from_rbf->nrbf*to_rbf->nrbf); |
313 |
|
for (i = from_rbf->nrbf; i--; ) |
314 |
|
src_rem[i] = rbf_volume(&from_rbf->rbfa[i]) / from_rbf->vtotal; |
315 |
< |
for (i = to_rbf->nrbf; i--; ) |
316 |
< |
dst_rem[i] = rbf_volume(&to_rbf->rbfa[i]) / to_rbf->vtotal; |
315 |
> |
for (j = to_rbf->nrbf; j--; ) |
316 |
> |
dst_rem[j] = rbf_volume(&to_rbf->rbfa[j]) / to_rbf->vtotal; |
317 |
> |
|
318 |
|
do { /* move a bit at a time */ |
319 |
|
move_amt = migration_step(newmig, src_rem, dst_rem, &pmtx); |
320 |
|
total_rem -= move_amt; |
337 |
– |
#ifdef DEBUG |
338 |
– |
if (!nchild) |
339 |
– |
fprintf(stderr, "\r%.9f remaining...", total_rem); |
340 |
– |
#endif |
321 |
|
} while ((total_rem > end_thresh) & (move_amt > 0)); |
322 |
< |
#ifdef DEBUG |
343 |
< |
if (!nchild) fputs("done.\n", stderr); |
344 |
< |
else fprintf(stderr, "finished with %.9f remaining\n", total_rem); |
345 |
< |
#endif |
322 |
> |
|
323 |
|
for (i = from_rbf->nrbf; i--; ) { /* normalize final matrix */ |
324 |
< |
float nf = rbf_volume(&from_rbf->rbfa[i]); |
348 |
< |
int j; |
324 |
> |
double nf = rbf_volume(&from_rbf->rbfa[i]); |
325 |
|
if (nf <= FTINY) continue; |
326 |
|
nf = from_rbf->vtotal / nf; |
327 |
|
for (j = to_rbf->nrbf; j--; ) |
328 |
< |
mtx_coef(newmig,i,j) *= nf; |
328 |
> |
mtx_coef(newmig,i,j) *= nf; /* row now sums to 1.0 */ |
329 |
|
} |
330 |
|
end_subprocess(); /* exit here if subprocess */ |
331 |
|
free_routes(&pmtx); /* free working arrays */ |