public void TestShortestWayHeavy() { Graph graph = new Graph(5); graph.AddBidirectionnalLink(0, 1, 2); graph.AddBidirectionnalLink(1, 2, 3); graph.AddBidirectionnalLink(2, 3, 5); graph.AddBidirectionnalLink(3, 4, 10); graph.AddBidirectionnalLink(0, 4, 21); int dist; var result = Dijkstra.Calculate(graph, 0, 4); List <int> expectedResult = new List <int>(); expectedResult.Add(1); expectedResult.Add(2); expectedResult.Add(3); expectedResult.Add(4); Assert.AreEqual(20, result.Dist); for (int i = 0; i < expectedResult.Count; i++) { Assert.AreEqual(expectedResult[i], result.Path[i]); } }
public string Solve() { var d = new Dijkstra(graph); graph = d.Calculate(); SelectResult(graph); return result.Max(g => g.PathLength).ToString(CultureInfo.InvariantCulture); }
public static string Play(Context context) { int closestExit = -1; double closestExitDist = -1; List <int> closetsExitPath = null; foreach (var exit in context.Exits) { var result = Dijkstra.Calculate(context.Graph, context.SkynetNode, exit); if (result.Path != null && (closestExit == -1 || result.Dist < closestExitDist)) { closestExit = exit; closestExitDist = result.Dist; closetsExitPath = result.Path; } } int node1 = -1; int node2 = -1; FillDoubleExitAccessNode(context); if (closestExitDist > 1 && doubleSiblingNodes.Any()) { var newGraph = ReduceGraph(context); int closestSibling = -1; double closestSiblingDist = -1; List <int> closetsSiblingPath = null; foreach (var doubleSibling in doubleSiblingNodes) { var result = Dijkstra.Calculate(newGraph, context.SkynetNode, doubleSibling); if (result.Path != null && (closestSibling == -1 || result.Dist < closestSiblingDist)) { closestSibling = doubleSibling; closestSiblingDist = result.Dist; closetsSiblingPath = result.Path; } } if (closestSibling != -1) { var exitRelated = context.Graph.GetSiblingNodes(closestSibling).Intersect(context.Exits).First(); node1 = closestSibling; node2 = exitRelated; } } if (node1 == -1 || node2 == -1) { var exitIndex = closetsExitPath.Count() - 1; node1 = exitIndex >= 1 ? closetsExitPath[exitIndex - 1] : context.SkynetNode; node2 = closetsExitPath[exitIndex]; } context.Graph.DeleteBidirectionnalLink(node1, node2); return(node1 + " " + node2); }
public void TestNoWay() { Graph graph = new Graph(5); graph.AddBidirectionnalLink(0, 1); graph.AddBidirectionnalLink(1, 2); graph.AddBidirectionnalLink(2, 3); var result = Dijkstra.Calculate(graph, 0, 4); Assert.AreEqual(-1, result.Dist); Assert.AreEqual(null, result.Path); }
async public Task <NetworkTracerOutputCollection> Trace(INetworkFeatureClass network, NetworkTracerInputCollection input, ICancelTracker cancelTraker) { if (network == null || !CanTrace(input)) { return(null); } GraphTable gt = new GraphTable(network.GraphTableAdapter()); NetworkSourceInput source = input.Collect(NetworkTracerInputType.SourceNode)[0] as NetworkSourceInput; NetworkSinkInput sink = input.Collect(NetworkTracerInputType.SinkNode)[0] as NetworkSinkInput; Dijkstra dijkstra = new Dijkstra(cancelTraker); dijkstra.reportProgress += this.ReportProgress; dijkstra.ApplySwitchState = input.Contains(NetworkTracerInputType.IgnoreSwitches) == false && network.HasDisabledSwitches; Dijkstra.ApplyInputIds(dijkstra, input); // Kürzesten Weg berechenen if (!dijkstra.Calculate(gt, source.NodeId, sink.NodeId)) { return(null); } Dijkstra.Nodes initialNodes = dijkstra.DijkstraPathNodes(sink.NodeId); dijkstra = new Dijkstra(initialNodes, cancelTraker); dijkstra.ForbiddenTargetNodeIds = initialNodes.IdsToList(); for (int i = 1; i < initialNodes.Count - 1; i++) { dijkstra.Calculate(gt, initialNodes[i].Id); } NetworkTracerOutputCollection output = new NetworkTracerOutputCollection(); NetworkPathOutput pathOutput = new NetworkPathOutput(); Dijkstra.Nodes nodes = dijkstra.DijkstraNodes; foreach (Dijkstra.Node node in nodes) { pathOutput.Add(new NetworkEdgeOutput(node.EId)); } output.Add(pathOutput); if (input.Collect(NetworkTracerInputType.AppendNodeFlags).Count > 0) { await Helper.AppendNodeFlags(network, gt, Helper.NodeIds(nodes), output); } return(output); }
public static string Play(Context context) { int closestExit = -1; double closestExitDist = -1; List <int> closetsExitPath = null; foreach (var exit in context.Exits) { var result = Dijkstra.Calculate(context.Graph, context.SkynetNode, exit); if (result.Path != null && (closestExit == -1 || result.Dist < closestExitDist)) { closestExit = exit; closestExitDist = result.Dist; closetsExitPath = result.Path; } } context.Graph.DeleteBidirectionnalLink(context.SkynetNode, closetsExitPath.First()); return(context.SkynetNode + " " + closetsExitPath.First()); }
public static void CompareAlgorithms(string outputDirectory) { //pick random start point var rnd = new Random(); ulong startId = SvgHelper.Dictionary.Keys.ElementAt(rnd.Next(SvgHelper.Dictionary.Count)); var(distD, pD) = TimeHelper.MeasureTimeAlgorithm(() => Dijkstra.Calculate(startId), "Dijkstra"); var(distL, pL) = TimeHelper.MeasureTimeAlgorithm(() => Levit.Calculate(startId), "Levit"); //var timeD = new List<long>(); //var timeL = new List<long>(); //for (int i = 0; i < 100; i++) //{ // ulong startId = SvgHelper.Dictionary.Keys.ElementAt(rnd.Next(SvgHelper.Dictionary.Count)); // var tD = Stopwatch.StartNew(); // var (distD, pD) = Dijkstra.Calculate(startId);// TimeHelper.MeasureTimeAlgorithm(() => Dijkstra.Calculate(startId), "Dijkstra"); // timeD.Add(tD.ElapsedTicks); // var tL = Stopwatch.StartNew(); // var (distL, pL) = // Levit.Calculate( // startId); //TimeHelper.MeasureTimeAlgorithm(() => Levit.Calculate(startId), "Levit"); // timeL.Add(tL.ElapsedTicks); //} //Console.WriteLine($"timing information:\tsum - {new TimeSpan(timeD.Sum())}\t max - {new TimeSpan(timeD.Max())}\t avg - {new TimeSpan(Convert.ToInt64(timeD.Average()))}"); //Console.WriteLine($"timing information:\tsum - {new TimeSpan(timeL.Sum())}\t max - {new TimeSpan(timeL.Max())}\t avg - {new TimeSpan(Convert.ToInt64(timeL.Average()))}"); // compare Dijkstra and Levit var differences = distL.Values.Zip(distD.Values, (d, l) => Math.Abs(d - l)).ToList(); Console.WriteLine("Compare results for Dijkstra and Levit algorithms:"); Console.WriteLine($"Difference between distances:\t sum - {differences.Sum()}\t max - {differences.Max()}\t avg - {differences.Average()}"); Console.WriteLine($"Ancestors lists are {(pD.OrderBy(x => x.Key).SequenceEqual(pL.OrderBy(x => x.Key)) ? "equal" : "not equal")}"); TimeHelper.MeasureTime(() => CsvHelper.WriteShortestPathes(outputDirectory, startId, Destinations.Keys.OrderBy(x => distD[x]), distD, pD), "writing shortest pathes to csv"); TimeHelper.MeasureTime(() => SvgHelper.DisplayShortestPathes(outputDirectory, startId, Destinations.Keys.OrderBy(x => distD[x]), pD), "writing shortest pathes to svg"); var heuristicsFunctions = new Dictionary <Func <GeoPoint, GeoPoint, double>, (List <double> differences, List <long> timings)> {
public NetworkTracerOutputCollection Trace(INetworkFeatureClass network, NetworkTracerInputCollection input, ICancelTracker cancelTraker) { if (network == null || !CanTrace(input)) { return(null); } GraphTable gt = new GraphTable(network.GraphTableAdapter()); NetworkSourceInput sourceNode = null; NetworkSourceEdgeInput sourceEdge = null; if (input.Collect(NetworkTracerInputType.SourceNode).Count == 1) { sourceNode = input.Collect(NetworkTracerInputType.SourceNode)[0] as NetworkSourceInput; } else if (input.Collect(NetworkTracerInputType.SoruceEdge).Count == 1) { sourceEdge = input.Collect(NetworkTracerInputType.SoruceEdge)[0] as NetworkSourceEdgeInput; } else { return(null); } Dijkstra dijkstra = new Dijkstra(cancelTraker); dijkstra.reportProgress += this.ReportProgress; dijkstra.ApplySwitchState = input.Contains(NetworkTracerInputType.IgnoreSwitches) == false && network.HasDisabledSwitches; Dijkstra.ApplyInputIds(dijkstra, input); if (sourceNode != null) { dijkstra.Calculate(gt, sourceNode.NodeId); } else if (sourceEdge != null) { IGraphEdge graphEdge = gt.QueryEdge(sourceEdge.EdgeId); if (graphEdge == null) { return(null); } bool n1_2_n2 = gt.QueryN1ToN2(graphEdge.N1, graphEdge.N2) != null; bool n2_2_n1 = gt.QueryN1ToN2(graphEdge.N2, graphEdge.N1) != null; bool n1switchState = dijkstra.ApplySwitchState ? gt.SwitchState(graphEdge.N1) : true; bool n2switchState = dijkstra.ApplySwitchState ? gt.SwitchState(graphEdge.N2) : true; if (n1_2_n2 && n1switchState == true) { dijkstra.Calculate(gt, graphEdge.N1); } else if (n2_2_n1 && n2switchState == true) { dijkstra.Calculate(gt, graphEdge.N2); } else { return(null); } } Dijkstra.Nodes dijkstraNodes = dijkstra.DijkstraNodesWithMaxDistance(double.MaxValue); if (dijkstraNodes == null) { return(null); } ProgressReport report = (ReportProgress != null ? new ProgressReport() : null); #region Collect EdgedIds if (report != null) { report.Message = "Collected Edges..."; report.featurePos = 0; report.featureMax = dijkstraNodes.Count; ReportProgress(report); } NetworkInputForbiddenEdgeIds forbiddenEdgeIds = (input.Contains(NetworkTracerInputType.ForbiddenEdgeIds)) ? input.Collect(NetworkTracerInputType.ForbiddenEdgeIds)[0] as NetworkInputForbiddenEdgeIds : null; NetworkInputForbiddenStartNodeEdgeIds forbiddenStartNodeEdgeIds = (input.Contains(NetworkTracerInputType.ForbiddenStartNodeEdgeIds)) ? input.Collect(NetworkTracerInputType.ForbiddenStartNodeEdgeIds)[0] as NetworkInputForbiddenStartNodeEdgeIds : null; int counter = 0; List <int> edgeIds = new List <int>(); foreach (Dijkstra.Node dijkstraNode in dijkstraNodes) { if (dijkstra.ApplySwitchState) { if (gt.SwitchState(dijkstraNode.Id) == false) // hier ist Schluss!! { continue; } } GraphTableRows gtRows = gt.QueryN1(dijkstraNode.Id); if (gtRows == null) { continue; } foreach (IGraphTableRow gtRow in gtRows) { int eid = gtRow.EID; if (sourceNode != null && forbiddenStartNodeEdgeIds != null && dijkstraNode.Id == sourceNode.NodeId && forbiddenStartNodeEdgeIds.Ids.Contains(eid)) { continue; } if (forbiddenEdgeIds != null && forbiddenEdgeIds.Ids.Contains(eid)) { continue; } int index = edgeIds.BinarySearch(eid); if (index < 0) { edgeIds.Insert(~index, eid); } } counter++; if (report != null && counter % 1000 == 0) { report.featurePos = counter; ReportProgress(report); } } #endregion NetworkTracerOutputCollection output = new NetworkTracerOutputCollection(); if (report != null) { report.Message = "Add Edges..."; report.featurePos = 0; report.featureMax = edgeIds.Count; ReportProgress(report); } counter = 0; NetworkPathOutput pathOutput = new NetworkPathOutput(); foreach (int edgeId in edgeIds) { pathOutput.Add(new NetworkEdgeOutput(edgeId)); counter++; if (report != null && counter % 1000 == 0) { report.featurePos = counter; ReportProgress(report); } //var x = network.GetEdgeFeatureAttributes(edgeId, null); } output.Add(pathOutput); if (input.Collect(NetworkTracerInputType.AppendNodeFlags).Count > 0) { Helper.AppendNodeFlags(network, gt, Helper.NodeIds(dijkstraNodes), output); } return(output); }
public NetworkTracerOutputCollection Trace(INetworkFeatureClass network, NetworkTracerInputCollection input, gView.Framework.system.ICancelTracker cancelTraker) { if (network == null || !CanTrace(input)) { return(null); } GraphTable gt = new GraphTable(network.GraphTableAdapter()); NetworkSourceInput source = input.Collect(NetworkTracerInputType.SourceNode)[0] as NetworkSourceInput; NetworkSinkInput sink = input.Collect(NetworkTracerInputType.SinkNode)[0] as NetworkSinkInput; NetworkWeighInput weight = input.Contains(NetworkTracerInputType.Weight) ? input.Collect(NetworkTracerInputType.Weight)[0] as NetworkWeighInput : null; Dijkstra dijkstra = new Dijkstra(cancelTraker); dijkstra.reportProgress += this.ReportProgress; if (weight != null) { dijkstra.GraphWeight = weight.Weight; dijkstra.WeightApplying = weight.WeightApplying; } dijkstra.ApplySwitchState = input.Contains(NetworkTracerInputType.IgnoreSwitches) == false && network.HasDisabledSwitches; Dijkstra.ApplyInputIds(dijkstra, input); dijkstra.Calculate(gt, source.NodeId, sink.NodeId); Dijkstra.NetworkPath networkPath = dijkstra.DijkstraPath(sink.NodeId); if (networkPath == null) { return(null); } NetworkTracerOutputCollection output = new NetworkTracerOutputCollection(); NetworkPathOutput pathOutput = new NetworkPathOutput(); foreach (Dijkstra.NetworkPathEdge pathEdge in networkPath) { pathOutput.Add(new NetworkEdgeOutput(pathEdge.EId)); } output.Add(pathOutput); if (input.Collect(NetworkTracerInputType.AppendNodeFlags).Count > 0) { Dijkstra.Nodes pathNodes = dijkstra.DijkstraPathNodes(sink.NodeId); Helper.AppendNodeFlags(network, gt, Helper.NodeIds(pathNodes), output); } //if (pathNodes != null) //{ // foreach (Dijkstra.Node node in pathNodes) // { // string label = node.Dist.ToString(); // if (weight != null) // { // label += "\n(" + (Math.Round(node.GeoDist, 2)).ToString() + ")"; // } // output.Add(new NetworkNodeFlagOuput(node.Id)); // } //} return(output); }
async public Task <NetworkTracerOutputCollection> Trace(INetworkFeatureClass network, NetworkTracerInputCollection input, gView.Framework.system.ICancelTracker cancelTraker) { if (network == null || !CanTrace(input)) { return(null); } GraphTable gt = new GraphTable(network.GraphTableAdapter()); NetworkSourceInput source = input.Collect(NetworkTracerInputType.SourceNode)[0] as NetworkSourceInput; NetworkWeighInput weight = input.Contains(NetworkTracerInputType.Weight) ? input.Collect(NetworkTracerInputType.Weight)[0] as NetworkWeighInput : null; Dijkstra dijkstra = new Dijkstra(cancelTraker); dijkstra.reportProgress += this.ReportProgress; dijkstra.MaxDistance = _properties.Distance; if (weight != null) { dijkstra.GraphWeight = weight.Weight; dijkstra.WeightApplying = weight.WeightApplying; } dijkstra.ApplySwitchState = input.Contains(NetworkTracerInputType.IgnoreSwitches) == false && network.HasDisabledSwitches; Dijkstra.ApplyInputIds(dijkstra, input); dijkstra.Calculate(gt, source.NodeId); NetworkTracerOutputCollection output = new NetworkTracerOutputCollection(); #region Knoten/Kanten <= Distance Dijkstra.Nodes dijkstraNodes = dijkstra.DijkstraNodesWithMaxDistance(_properties.Distance); if (dijkstraNodes == null) { return(null); } List <int> edgeIds = new List <int>(); foreach (Dijkstra.Node dijkstraNode in dijkstraNodes) { if (dijkstraNode.EId < 0) { continue; } int index = edgeIds.BinarySearch(dijkstraNode.EId); if (index < 0) { edgeIds.Insert(~index, dijkstraNode.EId); } if (Math.Abs(dijkstraNode.Dist - _properties.Distance) < double.Epsilon) { // ToDo: Flag einfügen!! } } output.Add(new NetworkEdgeCollectionOutput(edgeIds)); #endregion #region Knoten/Kanten > Distance Dijkstra.Nodes cnodes = dijkstra.DijkstraNodesDistanceGreaterThan(_properties.Distance); foreach (Dijkstra.Node cnode in cnodes) { Dijkstra.Node node = dijkstra.DijkstraNodes.ById(cnode.Pre); if (node == null) { continue; } IGraphEdge graphEdge = gt.QueryEdge(cnode.EId); if (graphEdge == null) { continue; } RowIDFilter filter = new RowIDFilter(String.Empty); filter.IDs.Add(graphEdge.Eid); IFeatureCursor cursor = await network.GetEdgeFeatures(filter); if (cursor == null) { continue; } IFeature feature = await cursor.NextFeature(); if (feature == null) { continue; } IPath path = null; if (cnode.Id != graphEdge.N2 && cnode.Id == graphEdge.N1) { ((Polyline)feature.Shape)[0].ChangeDirection(); } double trimDist = _properties.Distance - node.Dist; string label = _properties.Distance.ToString(); if (weight != null) { double w = gt.QueryEdgeWeight(weight.Weight.Guid, cnode.EId); switch (weight.WeightApplying) { case WeightApplying.Weight: trimDist *= w; break; case WeightApplying.ActualCosts: trimDist = ((IPolyline)feature.Shape)[0].Length * trimDist * w; // ??? Prüfen break; } label += "\n(" + Math.Round(node.GeoDist + trimDist, 2).ToString() + ")"; } path = ((IPolyline)feature.Shape)[0].Trim(trimDist); Polyline polyline = new Polyline(); polyline.AddPath(path); output.Add(new NetworkEdgePolylineOutput(cnode.EId, polyline)); output.Add(new NetworkFlagOutput( polyline[0][polyline[0].PointCount - 1], label)); } #endregion return(output); }
async public Task <NetworkTracerOutputCollection> Trace(INetworkFeatureClass network, NetworkTracerInputCollection input, gView.Framework.system.ICancelTracker cancelTraker) { if (network == null || !CanTrace(input)) { return(null); } GraphTable gt = new GraphTable(network.GraphTableAdapter()); NetworkSourceInput sourceNode = null; NetworkSourceEdgeInput sourceEdge = null; if (input.Collect(NetworkTracerInputType.SourceNode).Count == 1) { sourceNode = input.Collect(NetworkTracerInputType.SourceNode)[0] as NetworkSourceInput; } else if (input.Collect(NetworkTracerInputType.SoruceEdge).Count == 1) { sourceEdge = input.Collect(NetworkTracerInputType.SoruceEdge)[0] as NetworkSourceEdgeInput; } else { return(null); } Dijkstra dijkstra = new Dijkstra(cancelTraker); dijkstra.reportProgress += this.ReportProgress; dijkstra.ApplySwitchState = input.Contains(NetworkTracerInputType.IgnoreSwitches) == false && network.HasDisabledSwitches; Dijkstra.ApplyInputIds(dijkstra, input); if (sourceNode != null) { dijkstra.Calculate(gt, sourceNode.NodeId); } else if (sourceEdge != null) { IGraphEdge graphEdge = gt.QueryEdge(sourceEdge.EdgeId); if (graphEdge == null) { return(null); } bool n1_2_n2 = gt.QueryN1ToN2(graphEdge.N1, graphEdge.N2) != null; bool n2_2_n1 = gt.QueryN1ToN2(graphEdge.N2, graphEdge.N1) != null; bool n1switchState = dijkstra.ApplySwitchState ? gt.SwitchState(graphEdge.N1) : true; bool n2switchState = dijkstra.ApplySwitchState ? gt.SwitchState(graphEdge.N2) : true; if (n1_2_n2 && n1switchState == true) { dijkstra.Calculate(gt, graphEdge.N1); } else if (n2_2_n1 && n2switchState == true) { dijkstra.Calculate(gt, graphEdge.N2); } else { return(null); } } Dijkstra.Nodes dijkstraNodes = dijkstra.DijkstraNodesWithMaxDistance(double.MaxValue); if (dijkstraNodes == null) { return(null); } ProgressReport report = (ReportProgress != null ? new ProgressReport() : null); int counter = 0; #region Collect Disconnected Nodes int maxNodeId = network.MaxNodeId; if (report != null) { report.Message = "Collected Disconnected Nodes..."; report.featurePos = 0; report.featureMax = maxNodeId; ReportProgress(report); } List <int> connectedNodeIds = dijkstraNodes.IdsToList(); connectedNodeIds.Sort(); List <int> disconnectedNodeIds = new List <int>(); for (int id = 1; id <= maxNodeId; id++) { counter++; if (report != null && counter % 1000 == 0) { report.featurePos = counter; ReportProgress(report); } if (connectedNodeIds.BinarySearch(id) >= 0) { continue; } disconnectedNodeIds.Add(id); } #endregion #region Collect EdgedIds if (report != null) { report.Message = "Collected Edges..."; report.featurePos = 0; report.featureMax = dijkstraNodes.Count; ReportProgress(report); } List <int> edgeIds = new List <int>(); foreach (int id in disconnectedNodeIds) { GraphTableRows gtRows = gt.QueryN1(id); if (gtRows == null) { continue; } foreach (IGraphTableRow gtRow in gtRows) { int index = edgeIds.BinarySearch(gtRow.EID); if (index < 0) { edgeIds.Insert(~index, gtRow.EID); } } counter++; if (report != null && counter % 1000 == 0) { report.featurePos = counter; ReportProgress(report); } } #endregion NetworkTracerOutputCollection output = new NetworkTracerOutputCollection(); if (report != null) { report.Message = "Add Edges..."; report.featurePos = 0; report.featureMax = edgeIds.Count; ReportProgress(report); } counter = 0; NetworkPathOutput pathOutput = new NetworkPathOutput(); foreach (int edgeId in edgeIds) { pathOutput.Add(new NetworkEdgeOutput(edgeId)); counter++; if (report != null && counter % 1000 == 0) { report.featurePos = counter; ReportProgress(report); } } output.Add(pathOutput); if (input.Collect(NetworkTracerInputType.AppendNodeFlags).Count > 0) { await Helper.AppendNodeFlags(network, gt, disconnectedNodeIds, output); } return(output); }
async public Task <NetworkTracerOutputCollection> Trace(INetworkFeatureClass network, NetworkTracerInputCollection input, ICancelTracker cancelTraker) { if (network == null || !CanTrace(input)) { return(null); } GraphTable gt = new GraphTable(network.GraphTableAdapter()); NetworkSourceInput sourceNode = null; NetworkSourceEdgeInput sourceEdge = null; if (input.Collect(NetworkTracerInputType.SourceNode).Count == 1) { sourceNode = input.Collect(NetworkTracerInputType.SourceNode)[0] as NetworkSourceInput; } else if (input.Collect(NetworkTracerInputType.SoruceEdge).Count == 1) { sourceEdge = input.Collect(NetworkTracerInputType.SoruceEdge)[0] as NetworkSourceEdgeInput; } else { return(null); } input.Collect(NetworkTracerInputType.BarrierNodes); NetworkTracerOutputCollection outputCollection = new NetworkTracerOutputCollection(); List <int> edgeIds = new List <int>(); NetworkPathOutput pathOutput = new NetworkPathOutput(); List <int> neighborNodeFcIds = new List <int>(); Dictionary <int, string> neighborFcs = new Dictionary <int, string>(); foreach (var networkClass in await network.NetworkClasses()) { if (networkClass.GeometryType != geometryType.Point) { continue; } int fcid = await network.NetworkClassId(networkClass.Name); neighborNodeFcIds.Add(fcid); neighborFcs.Add(fcid, networkClass.Name); } Dijkstra dijkstra = new Dijkstra(cancelTraker); dijkstra.reportProgress += this.ReportProgress; dijkstra.ApplySwitchState = input.Contains(NetworkTracerInputType.IgnoreSwitches) == false && network.HasDisabledSwitches; dijkstra.TargetNodeFcIds = neighborNodeFcIds; dijkstra.TargetNodeType = TargetNodeType; Dijkstra.ApplyInputIds(dijkstra, input); Dijkstra.Nodes dijkstraEndNodes = null; if (sourceNode != null) { dijkstra.Calculate(gt, sourceNode.NodeId); dijkstraEndNodes = dijkstra.DijkstraEndNodes; } else if (sourceEdge != null) { IGraphEdge graphEdge = gt.QueryEdge(sourceEdge.EdgeId); if (graphEdge == null) { return(null); } bool n1_2_n2 = gt.QueryN1ToN2(graphEdge.N1, graphEdge.N2) != null; bool n2_2_n1 = gt.QueryN1ToN2(graphEdge.N2, graphEdge.N1) != null; if (n1_2_n2 == false && n2_2_n1 == false) { return(null); } bool n1switchState = dijkstra.ApplySwitchState ? gt.SwitchState(graphEdge.N1) : true; bool n2switchState = dijkstra.ApplySwitchState ? gt.SwitchState(graphEdge.N2) : true; bool n1isNeighbor = neighborNodeFcIds.Contains(gt.GetNodeFcid(graphEdge.N1)); bool n2isNeighbor = neighborNodeFcIds.Contains(gt.GetNodeFcid(graphEdge.N2)); if (n1isNeighbor && n2isNeighbor) { dijkstraEndNodes = new Dijkstra.Nodes(); dijkstraEndNodes.Add(new Dijkstra.Node(graphEdge.N1)); dijkstraEndNodes.Add(new Dijkstra.Node(graphEdge.N2)); dijkstraEndNodes[0].EId = graphEdge.Eid; edgeIds.Add(graphEdge.Eid); pathOutput.Add(new NetworkEdgeOutput(graphEdge.Eid)); } else { if (!n1isNeighbor && n1switchState == true) { dijkstra.Calculate(gt, graphEdge.N1); dijkstraEndNodes = dijkstra.DijkstraEndNodes; if (!n1_2_n2 && n2isNeighbor) { Dijkstra.Node n1Node = new Dijkstra.Node(graphEdge.N2); n1Node.EId = graphEdge.Eid; dijkstraEndNodes.Add(n1Node); edgeIds.Add(graphEdge.Eid); pathOutput.Add(new NetworkEdgeOutput(graphEdge.Eid)); } } else if (!n2isNeighbor && n2switchState == true) { dijkstra.Calculate(gt, graphEdge.N2); dijkstraEndNodes = dijkstra.DijkstraEndNodes; if (!n2_2_n1 && n1isNeighbor) { Dijkstra.Node n1Node = new Dijkstra.Node(graphEdge.N1); n1Node.EId = graphEdge.Eid; dijkstraEndNodes.Add(n1Node); edgeIds.Add(graphEdge.Eid); pathOutput.Add(new NetworkEdgeOutput(graphEdge.Eid)); } } } } #region Create Output if (dijkstraEndNodes == null) { return(null); } ProgressReport report = (ReportProgress != null ? new ProgressReport() : null); #region Collect End Nodes if (report != null) { report.Message = "Collect End Nodes..."; report.featurePos = 0; report.featureMax = dijkstraEndNodes.Count; ReportProgress(report); } int counter = 0; foreach (Dijkstra.Node endNode in dijkstraEndNodes) { int fcId = gt.GetNodeFcid(endNode.Id); if (neighborNodeFcIds.Contains(fcId) && gt.GetNodeType(endNode.Id) == this.TargetNodeType) { IFeature nodeFeature = await network.GetNodeFeature(endNode.Id); if (nodeFeature != null && nodeFeature.Shape is IPoint) { string fcName = neighborFcs.ContainsKey(fcId) ? neighborFcs[fcId] : String.Empty; //outputCollection.Add(new NetworkNodeFlagOuput(endNode.Id, nodeFeature.Shape as IPoint)); outputCollection.Add(new NetworkFlagOutput(nodeFeature.Shape as IPoint, new NetworkFlagOutput.NodeFeatureData(endNode.Id, fcId, Convert.ToInt32(nodeFeature["OID"]), fcName))); } } counter++; if (report != null && counter % 1000 == 0) { report.featurePos = counter; ReportProgress(report); } } #endregion #region Collect EdgedIds if (report != null) { report.Message = "Collect Edges..."; report.featurePos = 0; report.featureMax = dijkstra.DijkstraNodes.Count; ReportProgress(report); } counter = 0; foreach (Dijkstra.Node dijkstraEndNode in dijkstraEndNodes) { Dijkstra.NetworkPath networkPath = dijkstra.DijkstraPath(dijkstraEndNode.Id); if (networkPath == null) { continue; } foreach (Dijkstra.NetworkPathEdge pathEdge in networkPath) { int index = edgeIds.BinarySearch(pathEdge.EId); if (index >= 0) { continue; } edgeIds.Insert(~index, pathEdge.EId); pathOutput.Add(new NetworkEdgeOutput(pathEdge.EId)); counter++; if (report != null && counter % 1000 == 0) { report.featurePos = counter; ReportProgress(report); } } } if (pathOutput.Count > 0) { outputCollection.Add(pathOutput); } #endregion #endregion return(outputCollection); }
void WCR_function() { GameObject[] nodelist; int node_length; nodelist = GameObject.FindGameObjectsWithTag("Node"); node_length = nodelist.Length; foreach (GameObject item in nodelist) { if (debug) { print(item.transform.position); //item.transform.position = new Vector3(Random.value, Random.value, Random.value); } } for (int n = 0; n < 20; n++) { if (!debug) { print("n: " + n); } for (int i = 0; i < node_length; i++) { for (int j = 0; j < node_length; j++) { if (j < i) { Dijkstra eScript = GameObject.FindGameObjectWithTag("GameController").GetComponent <Dijkstra>(); dist_ij = 5 * eScript.Calculate(nodelist[i], nodelist[j], false); if (debug) { print("dist_ij: " + dist_ij); } weight_ij = (float)Mathf.Pow(dist_ij, -2); weight = weight_ij * c; if (weight > 2) { weight = 2; } if (debug) { print("weight: " + weight); } if (debug) { print("weight_ij: " + weight_ij); } // dist_ij - ||Xi-Xj|| Xi-Xj // move = ------------------- ------- // 2 ||Xi-Xj|| // Separate out x,y and z xi = nodelist[i].transform.position.x; xj = nodelist[j].transform.position.x; yi = nodelist[i].transform.position.y; yj = nodelist[j].transform.position.y; zi = nodelist[i].transform.position.z; zj = nodelist[j].transform.position.z; if (!debug) { print("xi: " + xi); } //moveX = (float)((0.5)*(dist_ij - Mathf.Abs(xi-xj)) * ((xi-xj)/Mathf.Abs(xi-xj))); //could use vector3.Distance moveX = move(dist_ij, xi, xj); moveY = move(dist_ij, yi, yj); moveZ = move(dist_ij, zi, zj); if (!debug) { print("moveX: " + moveX); print("moveY: " + moveY); print("moveZ: " + moveZ); } currentPositioni.Set(nodelist[i].transform.position.x, nodelist[i].transform.position.y, nodelist[i].transform.position.z); currentPositionj.Set(nodelist[j].transform.position.x, nodelist[j].transform.position.y, nodelist[j].transform.position.z); nodelist[i].transform.position = new Vector3(currentPositioni.x + (weight * moveX), currentPositioni.y + (weight * moveY), currentPositioni.z + (weight * moveZ)); //print("Test:"); //print(currentPositioni.x + (weight * moveX)); nodelist[j].transform.position = new Vector3(currentPositionj.x - (weight * moveX), currentPositionj.y - (weight * moveY), currentPositionj.z - (weight * moveZ)); c = c / 2; } } } } }