17 for (ij=0; ij<n_terms; ij++) {
18 float dx = pos[2*terms[ij].
i] - pos[2*terms[ij].
j];
19 float dy = pos[2*terms[ij].i+1] - pos[2*terms[ij].j+1];
20 float r = hypotf(
dx,
dy) - terms[ij].d;
21 stress += terms[ij].w * (r * r);
29 for (i=n_terms-1; i>=1; i--) {
44 size_t n_nodes = 0, n_edges = 0;
46 assert(
ND_id(np) == n_nodes);
61 assert(n_edges <= INT_MAX);
64 n_nodes = 0, n_edges = 0;
66 assert(n_edges <= INT_MAX);
67 graph->sources[n_nodes] = n_edges;
74 graph->targets[n_edges] = (size_t)
ND_id(target);
76 assert(
graph->weights[n_edges] > 0);
81 assert(n_nodes ==
graph->n);
82 assert(n_edges <= INT_MAX);
84 graph->sources[n_nodes] = n_edges;
93 for (
size_t i = 0; i <
graph->n; i++) {
95 for (
size_t x =
graph->sources[i]; x <
graph->sources[i + 1]; x++) {
96 size_t j =
graph->targets[x];
102 for (
size_t x =
graph->sources[i]; x <
graph->sources[i + 1]; x++) {
103 size_t j =
graph->targets[x];
106 for (
size_t y =
graph->sources[j]; y <
graph->sources[j + 1];
108 size_t k =
graph->targets[y];
118 assert(
graph->weights[x] > 0);
119 for (
size_t y =
graph->sources[j]; y <
graph->sources[j + 1];
121 size_t k =
graph->targets[y];
125 for (
size_t x =
graph->sources[i]; x <
graph->sources[i + 1]; x++) {
126 size_t j =
graph->targets[x];
151 agwarningf(
"circuit model not yet supported in Gmode=sgd, reverting to shortpath model\n");
155 agwarningf(
"mds model not yet supported in Gmode=sgd, reverting to shortpath model\n");
161 fprintf(stderr,
"calculating shortest paths and setting up stress terms:");
165 int i, n_fixed = 0, n_terms = 0;
166 for (i=0; i<n; i++) {
169 n_terms += n-n_fixed;
176 for (i=0; i<n; i++) {
181 assert(
offset == n_terms);
188 float w_min = terms[0].
w, w_max = terms[0].
w;
190 for (ij=1; ij<n_terms; ij++) {
191 if (terms[ij].w < w_min)
193 if (terms[ij].w > w_max)
198 float eta_max = 1 / w_min;
199 float eta_min =
Epsilon / w_max;
200 float lambda = log(eta_max/eta_min) / (
MaxIter-1);
205 float *pos =
gv_calloc(2 * n,
sizeof(
float));
206 bool *unfixed =
gv_calloc(n,
sizeof(
bool));
207 for (i=0; i<n; i++) {
216 fprintf(stderr,
"solving model:");
223 float eta = eta_max * exp(-lambda * t);
224 for (ij=0; ij<n_terms; ij++) {
226 float mu = eta * terms[ij].
w;
230 float dx = pos[2*terms[ij].
i] - pos[2*terms[ij].
j];
231 float dy = pos[2*terms[ij].i+1] - pos[2*terms[ij].j+1];
232 float mag = hypotf(
dx,
dy);
234 float r = (
mu * (mag-terms[ij].d)) / (2*mag);
238 if (unfixed[terms[ij].i]) {
239 pos[2*terms[ij].i] -= r_x;
240 pos[2*terms[ij].i+1] -= r_y;
242 if (unfixed[terms[ij].j]) {
243 pos[2*terms[ij].j] += r_x;
244 pos[2*terms[ij].j+1] += r_y;
252 fprintf(stderr,
"\nfinished in %.2f sec\n",
elapsed_sec());
257 for (i=0; i<n; i++) {
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
static void * gv_alloc(size_t size)
API for compacted arrays of booleans.
static bitarray_t bitarray_new(size_t size_bits)
create an array of the given element length
static bool bitarray_get(bitarray_t self, size_t index)
get the value of the given element
static void bitarray_set(bitarray_t *self, size_t index, bool value)
set or clear the value of the given element
static void bitarray_reset(bitarray_t *self)
free underlying resources and leave a bit array empty
int agnnodes(Agraph_t *g)
Agedge_t * agnxtedge(Agraph_t *g, Agedge_t *e, Agnode_t *n)
Agedge_t * agfstedge(Agraph_t *g, Agnode_t *n)
void agwarningf(const char *fmt,...)
#define GD_neato_nlist(g)
Agnode_t * agnxtnode(Agraph_t *g, Agnode_t *n)
Agnode_t * agfstnode(Agraph_t *g)
Agraph_t * graph(char *name)
swig_ptr_object_handlers offset
int dijkstra_sgd(graph_sgd *graph, int source, term_sgd *terms)
NEATOPROCS_API void initial_positions(graph_t *, int)
unsigned long rk_interval(unsigned long max, rk_state *state)
void rk_seed(unsigned long seed, rk_state *state)
static void free_adjacency(graph_sgd *graph)
static void fisheryates_shuffle(term_sgd *terms, int n_terms)
static float calculate_stress(float *pos, term_sgd *terms, int n_terms)
void sgd(graph_t *G, int model)
static graph_sgd * extract_adjacency(graph_t *G, int model)
static bool intersect(Ppoint_t a, Ppoint_t b, Ppoint_t c, Ppoint_t d)