X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=energy.c;h=5aa1c602b8387cd75f6c0e75437f7f63bd6f3edc;hb=21a2bd20afe0f4673494f4841a5675ca68cbd2d7;hp=07a410009467ac646811427d0beb875f2c98dc54;hpb=28782fcd01e38d28c19f65b0c6f02ee1c55a1255;p=moebius2.git diff --git a/energy.c b/energy.c index 07a4100..5aa1c60 100644 --- a/energy.c +++ b/energy.c @@ -3,65 +3,151 @@ */ #include "common.h" -#include "bgl.h" +#include "minimise.h" #include "mgraph.h" +#include "parallel.h" -#include -#include +double vertex_areas[N], vertex_mean_edge_lengths[N], edge_lengths[N][V6]; -static const char *input_file, *output_file; -static char *output_file_tmp; - -static void compute_vertex_areas(const Vertices vertices, double areas[N]); static double best_energy= DBL_MAX; -static void addcost(double *energy, double tweight, double tcost); -#define COST(weight, compute) addcost(&energy, (weight), (compute)) +static void addcost(double *energy, double tweight, double tcost, int pr); + +/*---------- 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); +} + +/*---------- 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; cie |", evaluations); + + inparallel(vs, + compute_energy_separately, + compute_energy_combine, + sizeof(totals) /* really, size of energies */, + totals); - compute_vertex_areas(vertices,vertex_areas); energy= 0; - printf("cost > energy |"); - COST(1e4, edgewise_vertex_displacement_cost(vertices)); - COST(1e2, graph_layout_cost(vertices,vertex_areas)); -// COST(1e4, noncircular_rim_cost(vertices)); + 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; } - putchar('\n'); - flushoutput(); + if (printing) { + putchar('\n'); + flushoutput(); + } + evaluations++; return energy; } -static void addcost(double *energy, double tweight, double tcost) { +static void addcost(double *energy, double tweight, double tcost, int pr) { double tenergy= tweight * tcost; - printf(" %# e > %# e |", tcost, tenergy); + if (pr) printf(" %# e x %g > %# e* |", tcost, tweight, tenergy); *energy += tenergy; } -static void compute_vertex_areas(const Vertices vertices, double areas[N]) { - int v0,v1,v2, e1,e2, k; +/*---------- Precomputations ----------*/ - FOR_VERTEX(v0) { - double total= 0.0; +void compute_edge_lengths(const Vertices vertices, int section) { + int 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, int section) { + int v0,v1,v2, e1,e2; +// int k; + + FOR_VERTEX(v0,OUTER) { + double total= 0.0, edges_total=0; int count= 0; FOR_VEDGE(v0,e1,v1) { @@ -69,199 +155,151 @@ static void compute_vertex_areas(const Vertices vertices, double areas[N]) { v2= EDGE_END2(v0,e2); if (v2<0) continue; - double e1v[D3], e2v[D3], av[D3]; - K { - e1v[k]= vertices[v1][k] - vertices[v0][k]; - e2v[k]= vertices[v2][k] - vertices[v0][k]; - } - xprod(av, e1v, e2v); - total += magnD(av); + edges_total += edge_lengths[v0][e1]; + +// double e1v[D3], e2v[D3], av[D3]; +// K { +// e1v[k]= vertices[v1][k] - vertices[v0][k]; +// e2v[k]= vertices[v2][k] - vertices[v0][k]; +// } +// xprod(av, e1v, e2v); +// total += magnD(av); + count++; } - areas[v0]= total / count; + vertex_areas[v0]= total / count; + vertex_mean_edge_lengths[v0]= edges_total / count; } } -/*---------- use of GSL ----------*/ +/*---------- Edgewise vertex displacement ----------*/ - /* We want to do multidimensional minimisation. + /* + * Definition: + * + * At each vertex Q, in each direction e: + * + * e + * Q ----->----- R + * _,-'\__/ + * _,-' delta + * P ' * - * We don't think there are any local minima. Or at least, if there - * are, the local minimum which will be found from the starting - * state is the one we want. + * r + * cost = delta (we use r=3) + * Q,e * - * We don't want to try to provide a derivative of the cost - * function. That's too tedious (and anyway the polynomial - * approximation to our our cost function sometimes has high degree - * in the inputs which means the quadratic model implied by most of - * the gradient descent minimisers is not ideal). * - * This eliminates most of the algorithms. Nelder and Mead's - * simplex algorithm is still available and we will try that. + * Calculation: * - * In our application we are searching for the optimal locations of - * N actualvertices in D3 (3) dimensions - ie, we are searching for - * the optimal metapoint in an N*D3-dimensional space. + * Let vector A = PQ + * B = QR * - * So eg with X=Y=100, the simplex will contain 300 metavertices - * each of which is an array of 300 doubles for the actualvertex - * coordinates. Hopefully this won't be too slow ... + * -1 A . B + * delta = tan ------- + * | A x B | + * + * which is always in the range 0..pi because the denominator + * is nonnegative. We add epsilon to |AxB| to avoid division + * by zero. + * + * r + * cost = delta + * Q,e */ -static gsl_multimin_fminimizer *minimiser; - -static const double stop_epsilon= 1e-4; - -static double minfunc_f(const gsl_vector *x, void *params) { - assert(x->size == DIM); - assert(x->stride == 1); - return compute_energy((const double(*)[D3])x->data); -} - -int main(int argc, const char *const *argv) { - gsl_multimin_function multimin_function; - double size; - Vertices initial, step_size; - FILE *initial_f; - gsl_vector initial_gsl, step_size_gsl; - int r, v, k; - - if (argc!=3 || argv[1][0]=='-' || strncmp(argv[2],"-o",2)) - { fputs("usage: minimise -o> YSHIFT; + int nominal_edge_distance= y <= Y/2 ? y : Y-1-y; + if (nominal_edge_distance==0) continue; + + find_nearest_oncircle(oncircle, vertices[v]); + + cost += + vertex_mean_edge_lengths[v] * + (nominal_edge_distance*nominal_edge_distance) / + (hypotD2(vertices[v], oncircle) + 1e-6); } - return total_cost; + return cost; } /*---------- 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, OUTER) { + find_nearest_oncircle(oncircle, vertices[v]); - FOR_RIM_VERTEX(vy,vx,v) { - double oncircle[3]; - /* By symmetry, nearest point on circle is the one with - * the same angle subtended at the z axis. */ - oncircle[0]= vertices[v][0]; - oncircle[1]= vertices[v][1]; - oncircle[2]= 0; - double mult= 1.0/ magnD(oncircle); - oncircle[0] *= mult; - oncircle[1] *= mult; double d2= hypotD2(vertices[v], oncircle); cost += d2*d2; }