27 |
|
int nrows, ncols; /* array size (matches migration) */ |
28 |
|
float *price; /* migration prices */ |
29 |
|
short *sord; /* sort for each row, low to high */ |
30 |
+ |
float *prow; /* current price row */ |
31 |
|
} PRICEMAT; /* sorted pricing matrix */ |
32 |
|
|
33 |
|
#define pricerow(p,i) ((p)->price + (i)*(p)->ncols) |
120 |
|
if (pid < 0) { |
121 |
|
fprintf(stderr, "%s: cannot fork subprocess\n", |
122 |
|
progname); |
123 |
+ |
await_children(nchild); |
124 |
|
exit(1); |
125 |
|
} |
126 |
|
++nchild; /* subprocess started */ |
140 |
|
msrt_cmp(void *b, const void *p1, const void *p2) |
141 |
|
{ |
142 |
|
PRICEMAT *pm = (PRICEMAT *)b; |
143 |
< |
int ri = ((const short *)p1 - pm->sord) / pm->ncols; |
144 |
< |
float c1 = pricerow(pm,ri)[*(const short *)p1]; |
143 |
< |
float c2 = pricerow(pm,ri)[*(const short *)p2]; |
143 |
> |
float c1 = pm->prow[*(const short *)p1]; |
144 |
> |
float c2 = pm->prow[*(const short *)p2]; |
145 |
|
|
146 |
|
if (c1 > c2) return(1); |
147 |
|
if (c1 < c2) return(-1); |
171 |
|
for (i = from_rbf->nrbf; i--; ) { |
172 |
|
const double from_ang = R2ANG(from_rbf->rbfa[i].crad); |
173 |
|
FVECT vfrom; |
174 |
+ |
short *srow; |
175 |
|
ovec_from_pos(vfrom, from_rbf->rbfa[i].gx, from_rbf->rbfa[i].gy); |
176 |
+ |
pm->prow = pricerow(pm,i); |
177 |
+ |
srow = psortrow(pm,i); |
178 |
|
for (j = to_rbf->nrbf; j--; ) { |
179 |
< |
pricerow(pm,i)[j] = acos(DOT(vfrom, vto[j])) + |
179 |
> |
double dprod = DOT(vfrom, vto[j]); |
180 |
> |
pm->prow[j] = ((dprod >= 1.) ? .0 : acos(dprod)) + |
181 |
|
fabs(R2ANG(to_rbf->rbfa[j].crad) - from_ang); |
182 |
< |
psortrow(pm,i)[j] = j; |
182 |
> |
srow[j] = j; |
183 |
|
} |
184 |
< |
qsort_r(psortrow(pm,i), pm->ncols, sizeof(short), pm, &msrt_cmp); |
184 |
> |
qsort_r(srow, pm->ncols, sizeof(short), pm, &msrt_cmp); |
185 |
|
} |
186 |
|
free(vto); |
187 |
|
} |
276 |
|
return(best.amt); |
277 |
|
} |
278 |
|
|
274 |
– |
#ifdef DEBUG |
275 |
– |
static char * |
276 |
– |
thetaphi(const FVECT v) |
277 |
– |
{ |
278 |
– |
static char buf[128]; |
279 |
– |
double theta, phi; |
280 |
– |
|
281 |
– |
theta = 180./M_PI*acos(v[2]); |
282 |
– |
phi = 180./M_PI*atan2(v[1],v[0]); |
283 |
– |
sprintf(buf, "(%.0f,%.0f)", theta, phi); |
284 |
– |
|
285 |
– |
return(buf); |
286 |
– |
} |
287 |
– |
#endif |
288 |
– |
|
279 |
|
/* Compute and insert migration along directed edge (may fork child) */ |
280 |
|
static MIGRATION * |
281 |
|
create_migration(RBFNODE *from_rbf, RBFNODE *to_rbf) |
285 |
|
MIGRATION *newmig; |
286 |
|
double *src_rem, *dst_rem; |
287 |
|
double total_rem = 1., move_amt; |
288 |
< |
int i; |
288 |
> |
int i, j; |
289 |
|
/* check if exists already */ |
290 |
|
for (newmig = from_rbf->ejl; newmig != NULL; |
291 |
|
newmig = nextedge(from_rbf,newmig)) |
292 |
|
if (newmig->rbfv[1] == to_rbf) |
293 |
|
return(NULL); |
294 |
|
/* else allocate */ |
295 |
+ |
#ifdef DEBUG |
296 |
+ |
fprintf(stderr, "Building path from (theta,phi) (%.0f,%.0f) ", |
297 |
+ |
get_theta180(from_rbf->invec), |
298 |
+ |
get_phi360(from_rbf->invec)); |
299 |
+ |
fprintf(stderr, "to (%.0f,%.0f) with %d x %d matrix\n", |
300 |
+ |
get_theta180(to_rbf->invec), |
301 |
+ |
get_phi360(to_rbf->invec), |
302 |
+ |
from_rbf->nrbf, to_rbf->nrbf); |
303 |
+ |
#endif |
304 |
|
newmig = new_migration(from_rbf, to_rbf); |
305 |
|
if (run_subprocess()) |
306 |
|
return(newmig); /* child continues */ |
312 |
|
progname); |
313 |
|
exit(1); |
314 |
|
} |
316 |
– |
#ifdef DEBUG |
317 |
– |
fprintf(stderr, "Building path from (theta,phi) %s ", |
318 |
– |
thetaphi(from_rbf->invec)); |
319 |
– |
fprintf(stderr, "to %s with %d x %d matrix\n", |
320 |
– |
thetaphi(to_rbf->invec), |
321 |
– |
from_rbf->nrbf, to_rbf->nrbf); |
322 |
– |
#endif |
315 |
|
/* starting quantities */ |
316 |
|
memset(newmig->mtx, 0, sizeof(float)*from_rbf->nrbf*to_rbf->nrbf); |
317 |
|
for (i = from_rbf->nrbf; i--; ) |
318 |
|
src_rem[i] = rbf_volume(&from_rbf->rbfa[i]) / from_rbf->vtotal; |
319 |
< |
for (i = to_rbf->nrbf; i--; ) |
320 |
< |
dst_rem[i] = rbf_volume(&to_rbf->rbfa[i]) / to_rbf->vtotal; |
319 |
> |
for (j = to_rbf->nrbf; j--; ) |
320 |
> |
dst_rem[j] = rbf_volume(&to_rbf->rbfa[j]) / to_rbf->vtotal; |
321 |
> |
|
322 |
|
do { /* move a bit at a time */ |
323 |
|
move_amt = migration_step(newmig, src_rem, dst_rem, &pmtx); |
324 |
|
total_rem -= move_amt; |
332 |
– |
#ifdef DEBUG |
333 |
– |
if (!nchild) |
334 |
– |
fprintf(stderr, "\r%.9f remaining...", total_rem); |
335 |
– |
#endif |
325 |
|
} while ((total_rem > end_thresh) & (move_amt > 0)); |
326 |
< |
#ifdef DEBUG |
338 |
< |
if (!nchild) fputs("done.\n", stderr); |
339 |
< |
else fprintf(stderr, "finished with %.9f remaining\n", total_rem); |
340 |
< |
#endif |
326 |
> |
|
327 |
|
for (i = from_rbf->nrbf; i--; ) { /* normalize final matrix */ |
328 |
< |
float nf = rbf_volume(&from_rbf->rbfa[i]); |
343 |
< |
int j; |
328 |
> |
double nf = rbf_volume(&from_rbf->rbfa[i]); |
329 |
|
if (nf <= FTINY) continue; |
330 |
|
nf = from_rbf->vtotal / nf; |
331 |
|
for (j = to_rbf->nrbf; j--; ) |
332 |
< |
mtx_coef(newmig,i,j) *= nf; |
332 |
> |
mtx_coef(newmig,i,j) *= nf; /* row now sums to 1.0 */ |
333 |
|
} |
334 |
|
end_subprocess(); /* exit here if subprocess */ |
335 |
|
free_routes(&pmtx); /* free working arrays */ |