35 namespace energybased {
70 template<
typename ForceFunc,
bool UseForcePrime>
74 template<
typename ForceFunc,
bool UseForcePrime>
78 template<
typename ForceFunc,
bool UseForcePrime>
82 template<
typename AttrForceFunc,
bool UseForcePrime>
86 void computeEdgeForcesSq();
95 template<
typename RepForceFunc>
97 double doIteration(RepForceFunc repForceFunc);
100 double doIteration() {
return doIteration(RepForceFunctionInvDist<Dim>); };
103 template<
typename RepForceFunc>
104 void doIterations(
int numIterations,
double epsilon, RepForceFunc repForceFunc);
107 template<
typename RepForceFunc>
108 void doIterations(
int numIterations,
double epsilon);
112 template<
typename RepForceFunc,
typename AttrForceFunc,
bool UseForcePrime>
113 void doIterationsTempl(
int numIterations,
double epsilon, RepForceFunc repForceFunc, AttrForceFunc attrForceFunc);
115 template<
typename RepForceFunc,
typename AttrForceFunc>
116 void doIterationsStandard(
int numIterations,
double epsilon, RepForceFunc repForceFunc, AttrForceFunc attrForceFunc);
118 template<
typename RepForceFunc,
typename AttrForceFunc>
119 void doIterationsNewton(
int numIterations,
double epsilon, RepForceFunc repForceFunc, AttrForceFunc attrForceFunc);
122 template<
typename RepForceFunc>
123 void doIterationsAdaptive(
int numIterations,
double epsilon, RepForceFunc repForceFunc);
139 double position[Dim];
192 template<
typename ForceFunc,
bool UseForcePrime>
200 double dist = computeDeltaAndDistance<Dim>(
m_nodeInfo[s].position,
m_nodeInfo[t].position, delta);
203 forceFunc(dist, force, force_prime);
205 force = force / dist *
mass(s) *
mass(t);
208 for (
int d = 0; d < Dim; d++) {
214 force_prime = force_prime *
mass(s) *
mass(t);
224 template<
typename ForceFunc>
231 double dist = computeDeltaAndDistance<Dim>(
m_nodeInfo[s].position,
m_nodeInfo[t].position, delta);
234 forceFunc(dist, force);
236 force = force / dist *
mass(s) *
mass(t);
239 for (
int d = 0; d < Dim; d++) {
249 template<
typename ForceFunc,
bool UseForcePrime>
256 for (
int d = 0; d < Dim; d++) {
266 m_pTreeForce->template computeForces<ForceFunc, UseForcePrime>(forceFunc);
274 for (
int d = 0; d < Dim; d++) {
288 template<
typename ForceFunc>
295 for (
int d = 0; d < Dim; d++) {
305 m_pTreeForce->template computeForces<ForceFunc, true>(forceFunc);
313 for (
int d = 0; d < Dim; d++) {
323 template<
typename ForceFunc,
bool UseForcePrime>
327 computeRepForcesExact<ForceFunc, UseForcePrime>(forceFunc);
329 computeRepForcesApprox<ForceFunc, UseForcePrime>(forceFunc);
335 template<
typename ForceFunc>
339 computeRepForcesExactNewton(forceFunc);
341 computeRepForcesApproxNewton(forceFunc);
347 template<
typename AttrForceFunc,
bool UseForcePrime>
352 node s = e->source();
353 node t = e->target();
364 double dist_sq = 0.0;
367 for (
int d = 0; d < Dim; d++) {
369 dist_sq += delta[d] * delta[d];
374 double f = log(dist_sq) * 0.5;
376 double dist = (sqrt(dist_sq));
382 attrForceFunc(dist, f, f_prime);
389 attrForceFunc(dist, f, f_prime);
395 for (
int d = 0; d < Dim; d++) {
396 m_nodeInfo[s].force[d] += f * delta[d] / dist;
397 m_nodeInfo[t].force[d] -= f * delta[d] / dist;
404 template<
typename AttrForceFunc>
409 node s = e->source();
410 node t = e->target();
421 double dist_sq = 0.0;
424 for (
int d = 0; d < Dim; d++) {
426 dist_sq += delta[d] * delta[d];
431 double f = log(dist_sq) * 0.5;
433 double dist = (sqrt(dist_sq));
438 double s_scale = 1.0/(double)s->
degree();
439 double t_scale = 1.0/(double)t->
degree();
446 for (
int d = 0; d < Dim; d++) {
447 m_nodeInfo[s].force[d] += f * delta[d] / dist;
448 m_nodeInfo[t].force[d] -= f * delta[d] / dist;
460 double sum_ddf = 0.0;
462 for (
int d = 0; d < Dim; d++) {
467 for (
adjEntry adj = s->firstAdj(); adj; adj = adj->
succ()) {
468 node t = adj->twinNode();
478 double dist_sq = 0.0;
481 for (
int d = 0; d < Dim; d++) {
483 dist_sq += delta[d] * delta[d];
486 double dist = sqrt(dist_sq);
491 for (
int d = 0; d < Dim; d++) {
492 sum_f[d] += f * delta[d] / dist;
497 for (
int d = 0; d < Dim; d++) {
508 double maxDispl = 0.0;
512 double displ_sq = 0.0;
514 for (
int d = 0; d < Dim; d++) {
517 displ_sq += displ * displ;
521 if (maxDispl < displ_sq) {
525 std::cout << sqrt(maxDispl) << std::endl;
526 return sqrt(maxDispl);
533 double maxDispl = 0.0;
537 double displ_sq = 0.0;
539 for (
int d = 0; d < Dim; d++) {
540 double displ =
m_nodeInfo[v].force[d] * timeStep;
542 displ_sq += displ * displ;
546 if (maxDispl < displ_sq) {
551 std::cout << sqrt(maxDispl) << std::endl;
552 return sqrt(maxDispl);
561 for (
int d = 0; d < Dim; d++) {
570 template<
typename RepForceFunc,
bool>
587 template<
typename RepForceFunc>
591 double maxDisplacement = epsilon;
594 for (
int i = 0; (i < numIterations) && (maxDisplacement >= epsilon); ++i) {
596 maxDisplacement = doIteration(repForceFunc);
601 template<
typename RepForceFunc>
604 int iterationsUsed = 0;
607 double maxTimeStep = 1.0;
615 double maxDisplacement = 100000000.0;
618 double lastMaxDisplacement = 0.0;
621 for (
int i = 0; (i < numIterations) && (maxDisplacement >= epsilon); ++i) {
636 if (maxDisplacement < lastMaxDisplacement * t) {
638 timeStep = std::min(timeStep / t, maxTimeStep);
639 }
else if (maxDisplacement > lastMaxDisplacement / t) {
640 timeStep = timeStep * t;
644 lastMaxDisplacement = maxDisplacement;
647 std::cout << maxDisplacement << std::endl;
650 std::cout <<
"IterationsUsed: " << iterationsUsed <<
" of " << numIterations <<
" energy: " << lastMaxDisplacement << std::endl;
661 template<
typename RepForceFunc,
typename AttrForceFunc,
bool UseForcePrime>
664 RepForceFunc repForceFunc,
665 AttrForceFunc attrForceFunc)
671 int numIterationsUsed = 0;
675 double maxDisplacement = 10000.0;
678 for (
int i = 0; (i < numIterations) && (maxDisplacement > epsilon) ; ++i) {
684 computeRepForces<RepForceFunc, UseForcePrime>(repForceFunc);
687 computeEdgeForces<AttrForceFunc, UseForcePrime>(attrForceFunc);
697 std::cout <<
"Needed " << numIterationsUsed <<
" of " << numIterations << std::endl;
701 template<
typename RepForceFunc,
typename AttrForceFunc>
704 doIterationsTempl<RepForceFunc, AttrForceFunc, false>(numIterations, epsilon, repForceFunc, attrForceFunc);
708 template<
typename RepForceFunc,
typename AttrForceFunc>
711 doIterationsTempl<RepForceFunc, AttrForceFunc, true>(numIterations, epsilon, repForceFunc, attrForceFunc);
721 for (
int d = 0; d < Dim; d++) {
727 for (
int d = 0; d < Dim; d++) {
728 bboxMin[d] = std::min(bboxMin[d],
position(v, d));
729 bboxMax[d] = std::max(bboxMax[d],
position(v, d));
734 for (
int d = 0; d < Dim; d++) {
735 delta[d] = -(bboxMin[d] + bboxMax[d]) * 0.5;
739 for (
int d = 0; d < Dim; d++) {
749 for (
int d = 0; d < Dim; d++) {
EdgeArray< double > m_edgeWeight
the weight of the edges
void doIterationsTempl(int numIterations, double epsilon, RepForceFunc repForceFunc, AttrForceFunc attrForceFunc)
does multiple iterations using the given repulsive force function
void init()
Reinitializes the array. Associates the array with no graph.
DTreeForce< Dim > * m_pTreeForce
the tree force approx
Data type for general directed graphs (adjacency list representation).
void doIterationsStandard(int numIterations, double epsilon, RepForceFunc repForceFunc, AttrForceFunc attrForceFunc)
void computeRepForcesApprox(ForceFunc forceFunc)
uses the tree code to approximate the repulsive forces in O(nlogn) for one iteration ...
int degree() const
Returns the degree of the node (indegree + outdegree).
DTreeEmbedder(const Graph &graph)
constructor with a given graph, allocates memory and does initialization
void setEdgeWeight(edge e, double weight)
sets the weight of an edge
void computeRepForces(ForceFunc forceFunc)
computes the repulsive forces
node firstNode() const
Returns the first node in the list of all nodes.
double moveNodes(double timeStep)
moves the nodes by the computed force vector
node succ() const
Returns the successor in the list of all nodes.
double mass(node v) const
returns the mass of node v
double mass
the mass of this node
double edgeWeight(edge e) const
returns the edge weight
int numberOfEdges() const
Returns the number of edges in the graph.
virtual ~DTreeEmbedder()
destructor
Dynamic arrays indexed with nodes.
const Graph & m_graph
the graph
edge firstEdge() const
Returns the first edge in the list of all edges.
void centerNodesAt(double centerBBox[Dim])
computes the bounding box and all nodes are translated such that bbox center is at centerBBox ...
void scaleNodes(double scaleFactor)
changes the position of nodes according to a given scale factor
Class for the representation of edges.
NodeArray< NodeInfo > m_nodeInfo
node states of all nodes
Class for the representation of nodes.
const Graph & graph() const
returns the graph
double moveNodesByForcePrime()
int m_maxNumNodesExactRepForces
double force_prime
sum of the derivs
void setPosition(node v, int d, double coord)
sets the d-th coordinate of node v to coord
edge succ() const
Returns the successor in the list of all edges.
void computeEdgeForces(AttrForceFunc attrForceFunc)
computes the edge forces for one iteration
adjEntry succ() const
Returns the successor in the adjacency list.
void resetForces()
sets the forces of all nodes to 0
The namespace for all OGDF objects.
int numberOfNodes() const
Returns the number of nodes in the graph.
double force[Dim]
the forces on a node
void doIterationsNewton(int numIterations, double epsilon, RepForceFunc repForceFunc, AttrForceFunc attrForceFunc)
double position(node v, int d) const
returns the d-th coordinate of node v
void computeRepForcesExact(ForceFunc forceFunc)
computes the repulsive forces for one iteration in O(n^2)
void setMass(node v, double mass)
sets the mass of a node v
Class for adjacency list elements.