public override void Compute(CancellationToken cancellationToken) { if (VisitedGraph.VertexCount == 1) { if (!VertexPositions.ContainsKey(VisitedGraph.Vertices.First())) { 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 );*/ } }
/// <inheritdoc /> protected override void Initialize() { base.Initialize(); // Initialize vertices positions InitializeWithRandomPositions(Parameters.Width, Parameters.Height); // Initialize ISOM data foreach (TVertex vertex in VisitedGraph.Vertices) { if (!_isomDataDict.ContainsKey(vertex)) { _isomDataDict[vertex] = new ISOMData(); } } _radius = Parameters.InitialRadius; }
private void AdjustVertex([NotNull] TVertex closest) { Debug.Assert(closest != null); _queue.Clear(); ISOMData vid = _isomDataDict[closest]; vid.Distance = 0; vid.Visited = true; _queue.Enqueue(closest); while (_queue.Count > 0) { ThrowIfCancellationRequested(); TVertex current = _queue.Dequeue(); ISOMData currentData = _isomDataDict[current]; Point position = VerticesPositions[current]; Vector force = _tempPos - position; double factor = _adaptation / Math.Pow(2, currentData.Distance); position += factor * force; VerticesPositions[current] = position; // If it is in the radius if (currentData.Distance < _radius) { // Also consider neighbors foreach (TVertex neighbor in VisitedGraph.GetNeighbors(current)) { ThrowIfCancellationRequested(); ISOMData neighborData = _isomDataDict[neighbor]; if (!neighborData.Visited) { neighborData.Visited = true; neighborData.Distance = currentData.Distance + 1; _queue.Enqueue(neighbor); } } } } }
/// <summary> /// Rántsunk egyet az összes ponton. /// </summary> protected void Adjust() { _tempPos = new Point(); //get a random point in the container _tempPos.X = 0.1 * Parameters.Width + (_rnd.NextDouble() * 0.8 * Parameters.Width); _tempPos.Y = 0.1 * Parameters.Height + (_rnd.NextDouble() * 0.8 * Parameters.Height); //find the closest vertex to this random point TVertex closest = GetClosest(_tempPos); //adjust the vertices to the selected vertex foreach (TVertex v in VisitedGraph.Vertices) { ISOMData vid = _isomDataDict[v]; vid.Distance = 0; vid.Visited = false; } AdjustVertex(closest); }
protected override void InternalCompute() { //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; for (int epoch = 0; epoch < Parameters.MaxEpoch; epoch++) { Adjust(); //Update Parameters double 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); } } }
/// <summary> /// Adjust all vertices to a random chosen one. /// </summary> protected void Adjust() { // Get a random point in the container _tempPos = new Point { X = 0.1 * Parameters.Width + Rand.NextDouble() * 0.8 * Parameters.Width, Y = 0.1 * Parameters.Height + Rand.NextDouble() * 0.8 * Parameters.Height }; // Find the closest vertex to this random point TVertex closest = GetClosest(_tempPos); // Adjust the vertices to the selected vertex foreach (TVertex vertex in VisitedGraph.Vertices) { ISOMData data = _isomDataDict[vertex]; data.Distance = 0; data.Visited = false; } AdjustVertex(closest); }
protected void Adjust() { _tempPos = new Point(); //get a random point in the container _tempPos.X = 0.1 * (HorizontalSpacing * 9) + (_rnd.NextDouble() * 0.8 * (HorizontalSpacing * 9)); _tempPos.Y = 0.1 * (VerticalSpacing * 9) + (_rnd.NextDouble() * 0.8 * (VerticalSpacing * 9)); //find the closest vertex to this random point Representation.node closest = GetClosest(_tempPos); //adjust the vertices to the selected vertex for (var i = 0; i < nodeList.Count; i++) { if (_isomDataDict.Keys.Contains(nodeList[i])) { ISOMData vid = _isomDataDict[nodeList[i]]; vid.Distance = 0; vid.Visited = false; } } AdjustVertex(closest); }
private void AdjustVertex(TVertex closest) { _queue.Clear(); ISOMData vid = _isomDataDict[closest]; vid.Distance = 0; vid.Visited = true; _queue.Enqueue(closest); while (_queue.Count > 0) { TVertex current = _queue.Dequeue(); ISOMData currentVid = _isomDataDict[current]; Point pos = VertexPositions[current]; Vector force = _tempPos - pos; double 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 (TVertex neighbour in VisitedGraph.GetNeighbours <TVertex, TEdge>(current)) { ISOMData nvid = _isomDataDict[neighbour]; if (!nvid.Visited) { nvid.Visited = true; nvid.Distance = currentVid.Distance + 1; _queue.Enqueue(neighbour); } } } } }
private void AdjustVertex(Representation.node closest) { if (_isomDataDict.Keys.Contains(closest)) { _queue.Clear(); ISOMData vid = _isomDataDict[closest]; vid.Distance = 0; vid.Visited = true; _queue.Enqueue(closest); while (_queue.Count > 0) { if (_queue.Count != 0) { Representation.node current = _queue.Dequeue(); if (current != null) { ISOMData currentVid = _isomDataDict[current]; Point pos = VertexPositions[current]; Vector force = _tempPos - pos; double factor = adaptation / Math.Pow(2, currentVid.Distance); pos += factor * force; VertexPositions[current] = pos; List <node> neighbors = new List <node>(GetNeighbors(current)); if (currentVid.Distance < radius) { for (int i = 0; i < neighbors.Count; i++) { ISOMData nvid = _isomDataDict[neighbors[i]]; if (!nvid.Visited) { nvid.Visited = true; nvid.Distance = currentVid.Distance + 1; _queue.Enqueue(neighbors[i]); } } } int index = 0; /* * for (int i = 0; graph.nodes[i] != current; i++) * { * index++; * } * * graph.nodes[index].X = VertexPositions[current].X; * graph.nodes[index].Y = VertexPositions[current].Y; */ //PORT VERTEXPOSITIONS TO graph.node (UPDATE LOCATIONS OF NODES) foreach (Point point in VertexPositions.Values.ToList()) { if (graph.nodes[index].name != null) { if (graph.nodes[index].arcs != null) { if (point.X == double.NaN || point.Y == double.NaN) { return; } graph.nodes[index].X = point.X; graph.nodes[index].Y = point.Y; index++; } } } } } } } }
protected override bool RunLayout() { Thread.Sleep(1000); _isomDataDict.Clear(); //Clearing storage elements in case user chooses to "Relayout" _queue.Clear(); VertexPositions.Clear(); populateVertexDictionary(); nodeList.Clear(); nodeList = VertexPositions.Keys.ToList(); for (var i = 0; i < VertexPositions.Keys.Count; i++) { ISOMData isomData; if (!_isomDataDict.TryGetValue(nodeList[i], out isomData)) { isomData = new ISOMData(); _isomDataDict[nodeList[i]] = isomData; } } backgroundWorker.ReportProgress(25); if (backgroundWorker.CancellationPending) { return(false); } radius = InitialRadius; //Default value is 5 int updateEvery = (int)MaxEpoch / 200; for (int epoch = 0; epoch < MaxEpoch; epoch++) //MaxEpoch is adjustable by user (using slider) { if (updateEvery == 0) { Progress = Progress + 3; if (Progress > 85) { Progress = 85; } backgroundWorker.ReportProgress(Progress); if (backgroundWorker.CancellationPending) { return(false); } updateEvery = (int)MaxEpoch / 200; } Adjust(); //Update Parameters double factor = Math.Exp(-1 * CoolingFactor * (1.0 * epoch / MaxEpoch)); adaptation = Math.Max(MinAdaption, factor * InitialAdaption); if (radius > MinRadius && epoch % RadiusConstantTime == 0) { radius--; } updateEvery--; } backgroundWorker.ReportProgress(90); if (backgroundWorker.CancellationPending) { return(false); } return(true); }