177 |
|
srow = psortrow(pm,i); |
178 |
|
for (j = to_rbf->nrbf; j--; ) { |
179 |
|
double d; /* quadratic cost function */ |
180 |
< |
d = DOT(vfrom, vto[j]); |
181 |
< |
d = (d >= 1.) ? .0 : acos(d); |
180 |
> |
d = Acos(DOT(vfrom, vto[j])); |
181 |
|
pm->prow[j] = d*d; |
182 |
|
d = R2ANG(to_rbf->rbfa[j].crad) - from_ang; |
183 |
|
pm->prow[j] += d*d; |
215 |
|
return(total_cost); |
216 |
|
} |
217 |
|
|
218 |
< |
/* Compare entries by moving price */ |
218 |
> |
typedef struct { |
219 |
> |
short s, d; /* source and destination */ |
220 |
> |
float dc; /* discount to push inventory */ |
221 |
> |
} ROWSENT; /* row sort entry */ |
222 |
> |
|
223 |
> |
/* Compare entries by discounted moving price */ |
224 |
|
static int |
225 |
|
rmovcmp(void *b, const void *p1, const void *p2) |
226 |
|
{ |
227 |
|
PRICEMAT *pm = (PRICEMAT *)b; |
228 |
< |
const short *ij1 = (const short *)p1; |
229 |
< |
const short *ij2 = (const short *)p2; |
230 |
< |
float price_diff; |
228 |
> |
const ROWSENT *re1 = (const ROWSENT *)p1; |
229 |
> |
const ROWSENT *re2 = (const ROWSENT *)p2; |
230 |
> |
double price_diff; |
231 |
|
|
232 |
< |
if (ij1[1] < 0) return(ij2[1] >= 0); |
233 |
< |
if (ij2[1] < 0) return(-1); |
234 |
< |
price_diff = pricerow(pm,ij1[0])[ij1[1]] - pricerow(pm,ij2[0])[ij2[1]]; |
232 |
> |
if (re1->d < 0) return(re2->d >= 0); |
233 |
> |
if (re2->d < 0) return(-1); |
234 |
> |
price_diff = re1->dc*pricerow(pm,re1->s)[re1->d] - |
235 |
> |
re2->dc*pricerow(pm,re2->s)[re2->d]; |
236 |
|
if (price_diff > 0) return(1); |
237 |
|
if (price_diff < 0) return(-1); |
238 |
|
return(0); |
246 |
|
const double maxamt = 1./(double)pm->ncols; |
247 |
|
const double minamt = maxamt*1e-4; |
248 |
|
double *src_cost; |
249 |
< |
short (*rord)[2]; |
249 |
> |
ROWSENT *rord; |
250 |
|
struct { |
251 |
|
int s, d; /* source and destination */ |
252 |
< |
double price; /* price estimate per amount moved */ |
252 |
> |
double price; /* cost per amount moved */ |
253 |
|
double amt; /* amount we can move */ |
254 |
|
} cur, best; |
255 |
|
int r2check, i, ri; |
258 |
|
* destination price implies that another source is closer, so |
259 |
|
* we can hold off considering more expensive options until |
260 |
|
* some other (hopefully better) moves have been made. |
261 |
+ |
* A discount based on source remaining is supposed to prioritize |
262 |
+ |
* movement from large lobes, but it doesn't seem to do much, |
263 |
+ |
* so we have it set to 1.0 at the moment. |
264 |
|
*/ |
265 |
+ |
#define discount(qr) 1.0 |
266 |
|
/* most promising row order */ |
267 |
< |
rord = (short (*)[2])malloc(sizeof(short)*2*pm->nrows); |
267 |
> |
rord = (ROWSENT *)malloc(sizeof(ROWSENT)*pm->nrows); |
268 |
|
if (rord == NULL) |
269 |
|
goto memerr; |
270 |
|
for (ri = pm->nrows; ri--; ) { |
271 |
< |
rord[ri][0] = ri; |
272 |
< |
rord[ri][1] = -1; |
271 |
> |
rord[ri].s = ri; |
272 |
> |
rord[ri].d = -1; |
273 |
> |
rord[ri].dc = 1.f; |
274 |
|
if (src_rem[ri] <= minamt) /* enough source material? */ |
275 |
|
continue; |
276 |
|
for (i = 0; i < pm->ncols; i++) |
277 |
< |
if (dst_rem[ rord[ri][1] = psortrow(pm,ri)[i] ] > minamt) |
277 |
> |
if (dst_rem[ rord[ri].d = psortrow(pm,ri)[i] ] > minamt) |
278 |
|
break; |
279 |
|
if (i >= pm->ncols) { /* moved all we can? */ |
280 |
|
free(rord); |
281 |
|
return(.0); |
282 |
|
} |
283 |
+ |
rord[ri].dc = discount(src_rem[ri]); |
284 |
|
} |
285 |
|
if (pm->nrows > max2check) /* sort if too many sources */ |
286 |
< |
qsort_r(rord, pm->nrows, sizeof(short)*2, pm, &rmovcmp); |
286 |
> |
qsort_r(rord, pm->nrows, sizeof(ROWSENT), pm, &rmovcmp); |
287 |
|
/* allocate cost array */ |
288 |
|
src_cost = (double *)malloc(sizeof(double)*pm->nrows); |
289 |
|
if (src_cost == NULL) |
296 |
|
r2check = max2check; /* put a limit on search */ |
297 |
|
for (ri = 0; ri < r2check; ri++) { /* check each source row */ |
298 |
|
double cost_others = 0; |
299 |
< |
cur.s = rord[ri][0]; |
300 |
< |
if ((cur.d = rord[ri][1]) < 0 || |
301 |
< |
(cur.price = pricerow(pm,cur.s)[cur.d]) >= best.price) { |
299 |
> |
cur.s = rord[ri].s; |
300 |
> |
if ((cur.d = rord[ri].d) < 0 || |
301 |
> |
rord[ri].dc*pricerow(pm,cur.s)[cur.d] >= best.price) { |
302 |
|
if (pm->nrows > max2check) break; /* sorted end */ |
303 |
|
continue; /* else skip this one */ |
304 |
|
} |
312 |
|
cost_others += min_cost(src_rem[i], dst_rem, pm, i) |
313 |
|
- src_cost[i]; |
314 |
|
dst_rem[cur.d] += cur.amt; /* undo trial move */ |
315 |
< |
cur.price += cost_others/cur.amt; /* adjust effective price */ |
315 |
> |
/* discount effective price */ |
316 |
> |
cur.price = ( pricerow(pm,cur.s)[cur.d] + cost_others/cur.amt ) * |
317 |
> |
rord[ri].dc; |
318 |
|
if (cur.price < best.price) /* are we better than best? */ |
319 |
|
best = cur; |
320 |
|
} |
330 |
|
memerr: |
331 |
|
fprintf(stderr, "%s: Out of memory in migration_step()\n", progname); |
332 |
|
exit(1); |
333 |
+ |
#undef discount |
334 |
|
} |
335 |
|
|
336 |
|
/* Compute and insert migration along directed edge (may fork child) */ |
503 |
|
static void |
504 |
|
check_normal_incidence(void) |
505 |
|
{ |
506 |
< |
const int saved_nprocs = nprocs; |
507 |
< |
RBFNODE *near_rbf, *mir_rbf, *rbf; |
508 |
< |
double bestd; |
509 |
< |
int n, i, j; |
506 |
> |
static const FVECT norm_vec = {.0, .0, 1.}; |
507 |
> |
const int saved_nprocs = nprocs; |
508 |
> |
RBFNODE *near_rbf, *mir_rbf, *rbf; |
509 |
> |
double bestd; |
510 |
> |
int n; |
511 |
|
|
512 |
|
if (dsf_list == NULL) |
513 |
|
return; /* XXX should be error? */ |
557 |
|
nprocs = 1; /* compute migration matrix */ |
558 |
|
if (mig_list != create_migration(mir_rbf, near_rbf)) |
559 |
|
exit(1); /* XXX should never happen! */ |
560 |
< |
n = 0; /* count migrating particles */ |
561 |
< |
for (i = 0; i < mtx_nrows(mig_list); i++) |
547 |
< |
for (j = 0; j < mtx_ncols(mig_list); j++) |
548 |
< |
n += (mtx_coef(mig_list,i,j) > FTINY); |
549 |
< |
rbf = (RBFNODE *)malloc(sizeof(RBFNODE) + sizeof(RBFVAL)*(n-1)); |
550 |
< |
if (rbf == NULL) |
551 |
< |
goto memerr; |
552 |
< |
rbf->next = NULL; rbf->ejl = NULL; |
553 |
< |
rbf->invec[0] = rbf->invec[1] = 0; rbf->invec[2] = 1.; |
554 |
< |
rbf->nrbf = n; |
555 |
< |
rbf->vtotal = .5 + .5*mig_list->rbfv[1]->vtotal/mig_list->rbfv[0]->vtotal; |
556 |
< |
n = 0; /* advect RBF lobes halfway */ |
557 |
< |
for (i = 0; i < mtx_nrows(mig_list); i++) { |
558 |
< |
const RBFVAL *rbf0i = &mig_list->rbfv[0]->rbfa[i]; |
559 |
< |
const float peak0 = rbf0i->peak; |
560 |
< |
const double rad0 = R2ANG(rbf0i->crad); |
561 |
< |
FVECT v0; |
562 |
< |
float mv; |
563 |
< |
ovec_from_pos(v0, rbf0i->gx, rbf0i->gy); |
564 |
< |
for (j = 0; j < mtx_ncols(mig_list); j++) |
565 |
< |
if ((mv = mtx_coef(mig_list,i,j)) > FTINY) { |
566 |
< |
const RBFVAL *rbf1j = &mig_list->rbfv[1]->rbfa[j]; |
567 |
< |
double rad2; |
568 |
< |
FVECT v; |
569 |
< |
int pos[2]; |
570 |
< |
rad2 = R2ANG(rbf1j->crad); |
571 |
< |
rad2 = .5*(rad0*rad0 + rad2*rad2); |
572 |
< |
rbf->rbfa[n].peak = peak0 * mv * rbf->vtotal * |
573 |
< |
rad0*rad0/rad2; |
574 |
< |
rbf->rbfa[n].crad = ANG2R(sqrt(rad2)); |
575 |
< |
ovec_from_pos(v, rbf1j->gx, rbf1j->gy); |
576 |
< |
geodesic(v, v0, v, .5, GEOD_REL); |
577 |
< |
pos_from_vec(pos, v); |
578 |
< |
rbf->rbfa[n].gx = pos[0]; |
579 |
< |
rbf->rbfa[n].gy = pos[1]; |
580 |
< |
++n; |
581 |
< |
} |
582 |
< |
} |
583 |
< |
rbf->vtotal *= mig_list->rbfv[0]->vtotal; |
560 |
> |
/* interpolate normal dist. */ |
561 |
> |
rbf = e_advect_rbf(mig_list, norm_vec, 2*near_rbf->nrbf); |
562 |
|
nprocs = saved_nprocs; /* final clean-up */ |
563 |
|
free(mir_rbf); |
564 |
|
free(mig_list); |