-
Notifications
You must be signed in to change notification settings - Fork 100
/
ISOMLayoutAlgorithm.cs
202 lines (174 loc) · 5.86 KB
/
ISOMLayoutAlgorithm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using GraphX.Measure;
using GraphX.PCL.Common.Exceptions;
using QuickGraph;
namespace GraphX.PCL.Logic.Algorithms.LayoutAlgorithms
{
public class ISOMLayoutAlgorithm<TVertex, TEdge, TGraph> : DefaultParameterizedLayoutAlgorithmBase<TVertex, TEdge, TGraph, ISOMLayoutParameters>
where TVertex : class
where TEdge : IEdge<TVertex>
where TGraph : IBidirectionalGraph<TVertex, TEdge>, IMutableVertexAndEdgeSet<TVertex, TEdge>
{
#region Private fields
private Queue<TVertex> _queue;
private Dictionary<TVertex, ISOMData> _isomDataDict;
private Point _tempPos;
private double _adaptation;
private int _radius;
#endregion
#region Constructors
public ISOMLayoutAlgorithm( TGraph visitedGraph, ISOMLayoutParameters oldParameters )
: base( visitedGraph )
{
Init( oldParameters );
}
public ISOMLayoutAlgorithm( TGraph visitedGraph, IDictionary<TVertex, Point> vertexPositions,
ISOMLayoutParameters oldParameters )
: base( visitedGraph, vertexPositions, oldParameters )
{
Init( oldParameters );
}
protected void Init( ISOMLayoutParameters oldParameters )
{
//init _parameters
InitParameters( oldParameters );
_queue = new Queue<TVertex>();
_isomDataDict = new Dictionary<TVertex, ISOMData>();
_adaptation = Parameters.InitialAdaption;
}
#endregion
public override void Compute(CancellationToken cancellationToken)
{
if (VisitedGraph.VertexCount == 1)
{
VertexPositions.Add(VisitedGraph.Vertices.First(), new Point(0, 0));
return;
}
//initialize vertex positions
InitializeWithRandomPositions( Parameters.Width, Parameters.Height );
//initialize ISOM data
foreach ( var vertex in VisitedGraph.Vertices )
{
ISOMData isomData;
if ( !_isomDataDict.TryGetValue( vertex, out isomData ) )
{
isomData = new ISOMData();
_isomDataDict[vertex] = isomData;
}
}
_radius = Parameters.InitialRadius;
var rnd = new Random(Parameters.Seed);
for ( var epoch = 0; epoch < Parameters.MaxEpoch; epoch++ )
{
cancellationToken.ThrowIfCancellationRequested();
Adjust(cancellationToken,rnd);
//Update Parameters
var factor = Math.Exp( -1 * Parameters.CoolingFactor * ( 1.0 * epoch / Parameters.MaxEpoch ) );
_adaptation = Math.Max( Parameters.MinAdaption, factor * Parameters.InitialAdaption );
if ( _radius > Parameters.MinRadius && epoch % Parameters.RadiusConstantTime == 0 )
{
_radius--;
}
//report
/*if ( ReportOnIterationEndNeeded )
OnIterationEnded( epoch, (double)epoch / (double)Parameters.MaxEpoch, "Iteration " + epoch + " finished.", true );
else if (ReportOnProgressChangedNeeded)
OnProgressChanged( (double)epoch / (double)Parameters.MaxEpoch * 100 );*/
}
}
public override void ResetGraph(IEnumerable<TVertex> vertices, IEnumerable<TEdge> edges)
{
if (VisitedGraph == null && !TryCreateNewGraph())
throw new GX_GeneralException("Can't create new graph through reflection. Make sure it support default constructor.");
VisitedGraph.Clear();
VisitedGraph.AddVertexRange(vertices);
VisitedGraph.AddEdgeRange(edges);
}
/// <summary>
/// Rántsunk egyet az összes ponton.
/// </summary>
protected void Adjust(CancellationToken cancellationToken, System.Random rnd)
{
_tempPos = new Point {
X = 0.1 * Parameters.Width + (rnd.NextDouble() * 0.8 * Parameters.Width),
Y = 0.1 * Parameters.Height + (rnd.NextDouble() * 0.8 * Parameters.Height)
};
//get a random point in the container
//find the closest vertex to this random point
var closest = GetClosest( _tempPos );
//adjust the vertices to the selected vertex
foreach ( var v in VisitedGraph.Vertices )
{
var vid = _isomDataDict[v];
vid.Distance = 0;
vid.Visited = false;
}
AdjustVertex( closest, cancellationToken );
}
private void AdjustVertex( TVertex closest, CancellationToken cancellationToken )
{
_queue.Clear();
var vid = _isomDataDict[closest];
vid.Distance = 0;
vid.Visited = true;
_queue.Enqueue( closest );
while ( _queue.Count > 0 )
{
cancellationToken.ThrowIfCancellationRequested();
var current = _queue.Dequeue();
var currentVid = _isomDataDict[current];
var pos = VertexPositions[current];
var force = _tempPos - pos;
var factor = _adaptation / Math.Pow( 2, currentVid.Distance );
pos += factor * force;
VertexPositions[current] = pos;
//ha még a hatókörön belül van
if ( currentVid.Distance < _radius )
{
//akkor a szomszedokra is hatassal vagyunk
foreach ( var neighbour in VisitedGraph.GetNeighbours( current ) )
{
cancellationToken.ThrowIfCancellationRequested();
var nvid = _isomDataDict[neighbour];
if ( !nvid.Visited )
{
nvid.Visited = true;
nvid.Distance = currentVid.Distance + 1;
_queue.Enqueue( neighbour );
}
}
}
}
}
/// <summary>
/// Finds the the closest vertex to the given position.
/// </summary>
/// <param name="tempPos">The position.</param>
/// <returns>Returns with the reference of the closest vertex.</returns>
private TVertex GetClosest( Point tempPos )
{
var vertex = default( TVertex );
var distance = double.MaxValue;
//find the closest vertex
foreach ( var v in VisitedGraph.Vertices )
{
var d = ( tempPos - VertexPositions[v] ).Length;
if ( d < distance )
{
vertex = v;
distance = d;
}
}
return vertex;
}
private class ISOMData
{
public Vector Force = new Vector();
public bool Visited;
public double Distance;
}
}
}