X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=energy.c;h=5aa1c602b8387cd75f6c0e75437f7f63bd6f3edc;hb=21a2bd20afe0f4673494f4841a5675ca68cbd2d7;hp=bba938d45ae7fc2e4e15cc7e4cbee7f0d7998cdf;hpb=abb4c2044743cd5cb2da0675b263388461a64de9;p=moebius2.git diff --git a/energy.c b/energy.c index bba938d..5aa1c60 100644 --- a/energy.c +++ b/energy.c @@ -5,36 +5,97 @@ #include "common.h" #include "minimise.h" #include "mgraph.h" +#include "parallel.h" double vertex_areas[N], vertex_mean_edge_lengths[N], edge_lengths[N][V6]; static double best_energy= DBL_MAX; static void addcost(double *energy, double tweight, double tcost, int pr); -#define COST(weight, compute) addcost(&energy, (weight), (compute), printing) + +/*---------- main energy computation, weights, etc. ----------*/ + +typedef double CostComputation(const Vertices vertices, int section); + +typedef struct { + double weight; + CostComputation *fn; +} CostContribution; + +static const CostContribution costs[]= { +#define COST(weight, compute) { (weight),(compute) }, + +#if XBITS==3 +#define STOP_EPSILON 1e-6; + COST( 3e2, line_bending_cost) + COST( 1e3, edge_length_variation_cost) + COST( 0.2e3, rim_proximity_cost) + COST( 1e8, noncircular_rim_cost) +#endif + +#if XBITS==4 +#define STOP_EPSILON 1e-5; + COST( 3e2, line_bending_cost) + COST( 3e3, edge_length_variation_cost) + COST( 3.8e1, rim_proximity_cost) // 5e1 is too much + // 2.5e1 is too little + // 0.2e1 grows compared to previous ? + // 0.6e0 shrinks compared to previous ? + COST( 1e12, noncircular_rim_cost) +#endif +}; + +#define NCOSTS ((sizeof(costs)/sizeof(costs[0]))) void energy_init(void) { + stop_epsilon= STOP_EPSILON; +} + +void compute_energy_separately(const struct Vertices *vs, + int section, void *energies_v, void *totals_v) { + double *energies= energies_v; + int ci; + + compute_edge_lengths(vs->a, section); + compute_vertex_areas(vs->a, section); + + for (ci=0; cia, section); } -/*---------- main energy computation and subroutines ----------*/ +/*---------- energy computation machinery ----------*/ + +void compute_energy_combine(const struct Vertices *vertices, + int section, void *energies_v, void *totals_v) { + int ci; + + double *energies= energies_v; + double *totals= totals_v; + + for (ci=0; cia); - compute_vertex_areas(vs->a); - energy= 0; + double totals[NCOSTS], energy; + int ci, printing; printing= printing_check(pr_cost,0); - if (printing) printf("cost > energy |"); + if (printing) printf("%15lld c>e |", evaluations); - COST(3e2, line_bending_cost(vs->a)); - COST(1e3, edge_length_variation_cost(vs->a)); - COST(0.2e3, rim_proximity_cost(vs->a)); -// COST(1e2, graph_layout_cost(vs->a)); - COST(1e8, noncircular_rim_cost(vs->a)); + inparallel(vs, + compute_energy_separately, + compute_energy_combine, + sizeof(totals) /* really, size of energies */, + totals); + + energy= 0; + + for (ci=0; cia,sizeof(vs->a),1,best_f); if (r!=1) diee("fwrite"); if (fclose(best_f)) diee("fclose new best"); - if (rename(output_file_tmp,output_file)) diee("rename install new best"); + if (rename(best_file_tmp,best_file)) diee("rename install new best"); best_energy= energy; } @@ -56,29 +123,30 @@ double compute_energy(const struct Vertices *vs) { flushoutput(); } + evaluations++; return energy; } static void addcost(double *energy, double tweight, double tcost, int pr) { double tenergy= tweight * tcost; - if (pr) printf(" %# e x %# e > %# e* |", tcost, tweight, tenergy); + if (pr) printf(" %# e x %g > %# e* |", tcost, tweight, tenergy); *energy += tenergy; } /*---------- Precomputations ----------*/ -void compute_edge_lengths(const Vertices vertices) { +void compute_edge_lengths(const Vertices vertices, int section) { int v1,e,v2; - FOR_EDGE(v1,e,v2) + FOR_EDGE(v1,e,v2,OUTER) edge_lengths[v1][e]= hypotD(vertices[v1],vertices[v2]); } -void compute_vertex_areas(const Vertices vertices) { +void compute_vertex_areas(const Vertices vertices, int section) { int v0,v1,v2, e1,e2; // int k; - FOR_VERTEX(v0) { + FOR_VERTEX(v0,OUTER) { double total= 0.0, edges_total=0; int count= 0; @@ -140,7 +208,7 @@ void compute_vertex_areas(const Vertices vertices) { * Q,e */ -double line_bending_cost(const Vertices vertices) { +double line_bending_cost(const Vertices vertices, int section) { static const double axb_epsilon= 1e-6; static const double exponent_r= 3; @@ -148,7 +216,7 @@ double line_bending_cost(const Vertices vertices) { double a[D3], b[D3], axb[D3]; double total_cost= 0; - FOR_EDGE(qi,e,ri) { + FOR_EDGE(qi,e,ri, OUTER) { pi= EDGE_END2(qi,(e+3)%V6); if (pi<0) continue; K a[k]= -vertices[pi][k] + vertices[qi][k]; @@ -159,7 +227,7 @@ double line_bending_cost(const Vertices vertices) { double delta= atan2(magnD(axb) + axb_epsilon, dotprod(a,b)); double cost= pow(delta,exponent_r); - if (!e && !(qi & YMASK)) + if (!e && !(qi & ~XMASK)) cost *= 10; total_cost += cost; @@ -178,11 +246,11 @@ double line_bending_cost(const Vertices vertices) { * Q,e */ -double edge_length_variation_cost(const Vertices vertices) { +double edge_length_variation_cost(const Vertices vertices, int section) { double diff, cost= 0, exponent_r= 2; int q, e,r, eback; - FOR_EDGE(q,e,r) { + FOR_EDGE(q,e,r, OUTER) { eback= edge_reverse(q,e); diff= edge_lengths[q][e] - edge_lengths[q][eback]; cost += pow(diff,exponent_r); @@ -203,11 +271,11 @@ static void find_nearest_oncircle(double oncircle[D3], const double p[D3]) { oncircle[1] *= mult; } -double rim_proximity_cost(const Vertices vertices) { +double rim_proximity_cost(const Vertices vertices, int section) { double oncircle[3], cost=0; int v; - FOR_VERTEX(v) { + FOR_VERTEX(v, OUTER) { int y= v >> YSHIFT; int nominal_edge_distance= y <= Y/2 ? y : Y-1-y; if (nominal_edge_distance==0) continue; @@ -224,12 +292,12 @@ double rim_proximity_cost(const Vertices vertices) { /*---------- noncircular rim cost ----------*/ -double noncircular_rim_cost(const Vertices vertices) { +double noncircular_rim_cost(const Vertices vertices, int section) { int vy,vx,v; double cost= 0.0; double oncircle[3]; - FOR_RIM_VERTEX(vy,vx,v) { + FOR_RIM_VERTEX(vy,vx,v, OUTER) { find_nearest_oncircle(oncircle, vertices[v]); double d2= hypotD2(vertices[v], oncircle);