public override void Init(double width, double height, Network network) { base.Init(width, height, network); foreach(Vertex v in network.Vertices) { _vertexPositions[v] = new Vector3(network.NextRandomDouble() * width, network.NextRandomDouble() * height, 1d); _newVertices.Add(v); } // Add vertex to _newVertices whenever one is added to the network network.OnVertexAdded+=new Network.VertexUpdateHandler( delegate(Vertex v) { _vertexPositions[v] = new Vector3(network.NextRandomDouble() * width, network.NextRandomDouble() * height, 1d); _newVertices.Add(v); }); }
public Kuramoto(Network n, double K, double UserDefinedcouplingProb, NetworkColorizer colorizer = null, Func<Vertex, Vertex[]> couplingSelector = null) : base(0d, new DenseVector((int) n.VertexCount)) { _network = n; _colorizer = colorizer; // Coupling Probability is taken from User CouplingProbability = UserDefinedcouplingProb; CouplingStrengths = new Dictionary<Tuple<Vertex, Vertex>, double>(); NaturalFrequencies = new ConcurrentDictionary<Vertex, double>(); foreach (Edge e in _network.Edges) { Tuple<Vertex,Vertex> t1 = new Tuple<Vertex, Vertex>(e.Source, e.Target); Tuple<Vertex,Vertex> t2 = new Tuple<Vertex, Vertex>(e.Target, e.Source); if(e.EdgeType == EdgeType.Undirected || e.EdgeType == EdgeType.DirectedAB) CouplingStrengths[t1] = K; if(e.EdgeType == EdgeType.Undirected || e.EdgeType == EdgeType.DirectedBA) CouplingStrengths[t2] = K; // My code modifications // Console.WriteLine(e.Source.Label+"......... "+e.Target.Label); } _mapping = new Dictionary<Vertex, int>(); int i= 0; foreach(Vertex v in _network.Vertices) { NaturalFrequencies[v] = 0.1d; _mapping[v] = i++; } // if no neighbor selector is given, just couple to all nearest neighbors if(couplingSelector==null) CouplingSelector = new Func<Vertex, Vertex[]>( v => { return v.Neigbors.ToArray(); }); else CouplingSelector = couplingSelector; // Initialize phases, colors and average degree foreach (Vertex v in _network.Vertices) { CurrentValues[_mapping[v]] = _network.NextRandomDouble() * Math.PI * 2d; if(_colorizer != null) _colorizer[v] = ColorFromPhase(CurrentValues[_mapping[v]]); _avgDeg += v.Degree; } _avgDeg /= (double) _network.VertexCount; Logger.AddMessage(LogEntryType.Info, string.Format("Sychchronization module initialized. Initial global order = {0:0.000}", GetOrder(_network.Vertices.ToArray()))); TimeDelta = Math.PI / 100d; OnStep+= new StepHandler(recolor); }
public void DoLayout(double width, double height, Network n) { double _area = width * height; double _k = Math.Sqrt(_area / (double)n.Vertices.Count()); _k *= 0.75d; // The displacement calculated for each vertex in each step ConcurrentDictionary<Vertex, Vector3> disp = new ConcurrentDictionary<Vertex, Vector3>(System.Environment.ProcessorCount, (int) n.VertexCount); _vertexPositions = new ConcurrentDictionary<Vertex, Vector3>(System.Environment.ProcessorCount, (int) n.VertexCount); double t = width/10; double tempstep = t / (double) _iterations; Parallel.ForEach(n.Vertices.ToArray(), v=> { _vertexPositions[v] = new Vector3(n.NextRandomDouble() * width, n.NextRandomDouble() * height, 1d); disp[v] = new Vector3(0d, 0d, 1d); }); _laidout = true; System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(delegate(object o) { for (int i=0; i<_iterations; i++) { // parallely Calculate repulsive forces for every pair of vertices Parallel.ForEach(n.Vertices.ToArray(), v => { disp[v] = new Vector3(0d, 0d, 1d); // computation of repulsive forces foreach(Vertex u in n.Vertices.ToArray()) { if (v != u) { Vector3 delta = _vertexPositions[v] - _vertexPositions[u]; disp[v] = disp[v] + (delta / Vector3.Length(delta)) * repulsion(Vector3.Length(delta), _k); } } }); // Parallely calculate attractive forces for all pairs of connected nodes Parallel.ForEach(n.Edges.ToArray(), e => { Vertex v = e.Source; Vertex w = e.Target; Vector3 delta = _vertexPositions[v] - _vertexPositions[w]; disp[v] = disp[v] - (delta / Vector3.Length(delta)) * attraction(Vector3.Length(delta), _k); disp[w] = disp[w] + (delta / Vector3.Length(delta)) * attraction(Vector3.Length(delta), _k); }); // Limit to frame and include temperature cooling that reduces displacement step by step Parallel.ForEach(n.Vertices.ToArray(), v => { Vector3 vPos = _vertexPositions[v] + (disp[v] / Vector3.Length(disp[v])) * Math.Min(Vector3.Length(disp[v]), t); vPos.X = Math.Min(width-10, Math.Max(10, vPos.X)); vPos.Y = Math.Min(height-10, Math.Max(10, vPos.Y)); _vertexPositions[v] = vPos; }); t-= tempstep; } })); }