public void EdmondsKarpAlgorithmExecuteTest() { Func <IGraphEdge, Int32> capacityFunction = edge => (Int32)(edge["Capacity"]); IDictionary <OperationParameter, Object> parameters = new Dictionary <OperationParameter, Object>(); parameters[GraphOperationParameters.SourceVertex] = _sourceVertex; parameters[GraphOperationParameters.TargetVertex] = _targetVertex; parameters[GraphOperationParameters.CapacityMetric] = capacityFunction; EdmondsKarpAlgorithm operation = new EdmondsKarpAlgorithm(_sourceGraph, parameters); operation.Execute(); Assert.AreEqual(_resultGraph.VertexCount, operation.Result.VertexCount); Assert.AreEqual(5, operation.Result["MaximumFlow"]); foreach (IGraphEdge edge in _resultGraph.Edges) { IGraphEdge resultEdge = operation.Result.Edges.FirstOrDefault(e => e.Source.Coordinate.Equals(edge.Source.Coordinate) && e.Target.Coordinate.Equals(edge.Target.Coordinate)); Assert.IsNotNull(resultEdge); Assert.AreEqual(edge["ResidualCapacity"], resultEdge["ResidualCapacity"]); } }
public IList <IGraphEdge <T> > ShortestPath(IGraph <T> graph, T start, T end) { IGraphNode <T> startNode = graph.GetNode(start); IGraphNode <T> endNode = graph.GetNode(end); destination = endNode; IDictionary <IGraphNode <T>, IGraphEdge <T> > parents = RelaxEdges(graph, startNode, m_h(start, end)); IList <IGraphEdge <T> > path = new List <IGraphEdge <T> >(); IGraphNode <T> node = endNode; while (parents.ContainsKey(node)) { IGraphEdge <T> edge = parents[node]; path.Add(edge); node = edge.node1; } for (int i = 0; i < path.Count / 2; ++i) { IGraphEdge <T> temp = path[i]; path[i] = path[path.Count - i - 1]; path[path.Count - i - 1] = temp; } return(path); }
public ICollection <IGraphEdge <T> > MinimumSpanningTree(IGraph <T> graph) { DisjointSet <IGraphNode <T> > nodes = new DisjointSet <IGraphNode <T> >(); foreach (IGraphNode <T> node in graph.Nodes) { nodes.Add(node); } PriorityQueue <QueueType> edges = new PriorityQueue <QueueType>(graph.EdgeCount); foreach (IGraphEdge <T> edge in graph.Edges) { edges.Add(new QueueType(edge.weight, edge)); } List <IGraphEdge <T> > ret = new List <IGraphEdge <T> >(graph.NodeCount); while (edges.Count > 0) { IGraphEdge <T> edge = edges.Pop().Second; if (nodes.AreUnited(edge.node1, edge.node2)) { continue; } ret.Add(edge); nodes.Union(edge.node1, edge.node2); } return(ret); }
public void BoruvkasAlgorithmExecuteTest() { Func <IGraphEdge, Double> weightFunction = edge => Convert.ToDouble(edge["Weight"]); IDictionary <OperationParameter, Object> parameters = new Dictionary <OperationParameter, Object>(); parameters[GraphOperationParameters.WeightMetric] = weightFunction; BoruvkasAlgorithm operation = new BoruvkasAlgorithm(_sourceGraph, parameters); operation.Execute(); Assert.AreEqual(_resultGraph.VertexCount, operation.Result.VertexCount); Assert.AreEqual(_resultGraph.EdgeCount, operation.Result.EdgeCount); foreach (IGraphEdge resultEdge in operation.Result.Edges) { IGraphEdge edge = _resultGraph.GetEdge(resultEdge.Source.Coordinate, resultEdge.Target.Coordinate); Assert.IsNotNull(edge); Assert.AreEqual(resultEdge.Source.Coordinate, edge.Source.Coordinate); Assert.AreEqual(resultEdge.Target.Coordinate, edge.Target.Coordinate); Assert.AreEqual(resultEdge.Metadata, edge.Metadata); } }
public IList <IGraphEdge <T> > ShortestPath(IGraph <T> graph, T start, T end) { Debug.Assert(m_graph == graph); Debug.Assert(m_start.Equals(start)); IGraphNode <T> node = graph.GetNode(end); IList <IGraphEdge <T> > ret = new List <IGraphEdge <T> >(); if (!parentsInPath.ContainsKey(node)) { return(ret); } while (!node.storedData.Equals(start)) { IGraphEdge <T> edge = parentsInPath[node]; ret.Add(edge); node = node == edge.node2 ? edge.node1 : edge.node2; } for (int i = 0; i < ret.Count / 2; ++i) { IGraphEdge <T> temp = ret[i]; ret[i] = ret[ret.Count - i - 1]; ret[ret.Count - i - 1] = temp; } return(ret); }
public void OnEvent(object MapEvent) { if (_module == null || _module.SelectedNetworkFeatureClass == null || !(MapEvent is MapEventClick)) { return; } MapEventClick ev = (MapEventClick)MapEvent; Point p = new Point(ev.x, ev.y); double tol = 5 * ((IDisplay)ev.Map).mapScale / (96 / 0.0254); // [m] _module.RemoveNetworkStartGraphicElement((IDisplay)ev.Map); IGraphEdge graphEdge = _module.SelectedNetworkFeatureClass.GetGraphEdge(p, tol); if (graphEdge != null) { _module.StartEdgeIndex = graphEdge.Eid; ((IDisplay)ev.Map).GraphicsContainer.Elements.Add(new GraphicStartPoint(p)); _module.StartPoint = p; } ((MapEvent)MapEvent).drawPhase = DrawPhase.Graphics; ((MapEvent)MapEvent).refreshMap = true; }
public static void DepthFirstTraversal <T>(this IGraph <T> graph, IGraphNode <T> startingNode, IGraphVisitor <T> visitor) { Stack <Pair <IGraphNode <T>, IEnumerator <IGraphEdge <T> > > > openNodes = new Stack <Pair <IGraphNode <T>, IEnumerator <IGraphEdge <T> > > >(); HashSet <IGraphNode <T> > seenNodes = new HashSet <IGraphNode <T> >(); openNodes.Push(new Pair <IGraphNode <T>, IEnumerator <IGraphEdge <T> > >(startingNode, graph.GetEdges(startingNode).GetEnumerator())); seenNodes.Add(startingNode); visitor.VisitNodeOpen(startingNode); while (openNodes.Count > 0) { var top = openNodes.Peek(); if (top.Second.MoveNext()) { IGraphEdge <T> edge = top.Second.Current; IGraphNode <T> node = edge.node2; if (!seenNodes.Add(node)) { continue; } openNodes.Push(new Pair <IGraphNode <T>, IEnumerator <IGraphEdge <T> > >(node, graph.GetEdges(node).GetEnumerator())); visitor.VisitNodeOpen(node); } else { visitor.VisitNodeClose(openNodes.Pop().First); } } }
public void AddOutgoingEdge(IGraphEdge <Node> outgoingEdge) { if (!outgoingEdge.SourceNode.Equals(this)) { throw new ArgumentException("outgoingEdge must have this Node as its source", "outgoingEdge"); } this.outgoingEdges.Add(outgoingEdge); }
public void RemoveEdge(IGraphEdge edge) { while (edges.Remove(edge)) { ; } edge.Destroy(); Analyze(); }
public IGraphEdge <T> GetEdge(IGraphNode <T> node1, IGraphNode <T> node2) { IGraphEdge <T> edge = baseGraph.GetEdge(node1, node2); if (mstEdges.Contains(edge)) { return(edge); } return(null); }
public void UnregisterConnection(IGraphEdge edge) { while (registeredEdges.Remove(edge)) { ; } while (criticalPaths.Remove(edge)) { ; } while (loosePaths.Remove(edge)) { ; } }
/// <summary> /// Computes the result of the operation. /// </summary> protected override void ComputeResult() { // source: http://en.wikipedia.org/wiki/Edmonds%E2%80%93Karp_algorithm Int32 capacity; Dictionary <IGraphVertex, IGraphVertex> parent; while (BreadthFirstSearch(out capacity, out parent)) { _maximumFlow += capacity; // backtrack search IGraphVertex currentVertex = _targetVertex; while (!Source.VertexComparer.Equals(currentVertex, _sourceVertex)) { IGraphVertex parentVertex = parent[currentVertex]; IGraphEdge forwardEdge = Source.GetEdge(parentVertex, currentVertex); IGraphEdge reverseEdge = Source.GetEdge(currentVertex, parentVertex); // modify used capacity if (!_usedCapacity.ContainsKey(forwardEdge)) { _usedCapacity.Add(forwardEdge, 0); } _usedCapacity[forwardEdge] += capacity; if (reverseEdge != null) { if (!_usedCapacity.ContainsKey(reverseEdge)) { _usedCapacity.Add(reverseEdge, 0); } _usedCapacity[reverseEdge] -= capacity; } currentVertex = parentVertex; } _isTargetReached = true; } }
private void PlaceWalls(IGraph <Vector2> graph) { for (int i = 1; i < size; ++i) { IGraphNode <Vector2> n1 = graph.GetNode(new Vector2(i - 1, 0)); IGraphNode <Vector2> n2 = graph.GetNode(new Vector2(i, 0)); IGraphEdge <Vector2> edge = graph.GetEdge(n1, n2); if (edge == null) { PlaceWall(n1, n2); } n1 = graph.GetNode(new Vector2(0, i - 1)); n2 = graph.GetNode(new Vector2(0, i)); edge = graph.GetEdge(n1, n2); if (edge == null) { PlaceWall(n1, n2); } } for (int y = 1; y < size; ++y) { for (int x = 1; x < size; ++x) { IGraphNode <Vector2> n1 = graph.GetNode(new Vector2(y - 1, x)); IGraphNode <Vector2> n2 = graph.GetNode(new Vector2(y, x)); IGraphEdge <Vector2> edge = graph.GetEdge(n1, n2); if (edge == null) { PlaceWall(n1, n2); } n1 = graph.GetNode(new Vector2(y, x - 1)); n2 = graph.GetNode(new Vector2(y, x)); edge = graph.GetEdge(n1, n2); if (edge == null) { PlaceWall(n1, n2); } } } }
async public Task <IFeature> GetEdgeFeature(int eid) { RowIDFilter filter = new RowIDFilter(String.Empty); filter.IDs.Add(eid); filter.AddField("*"); IFeatureCursor cursor = await GetEdgeFeatures(filter); if (cursor == null) { return(null); } IFeature feature = await cursor.NextFeature(); cursor.Dispose(); if (feature != null && feature.FindField("FCID") != null && feature.FindField("OID") != null && _edgeFcs.ContainsKey((int)feature["FCID"])) { IGraphEdge edge = _pageManager.GetEdge(eid); try { if (edge != null && edge.FcId == -1) // Complex Edge { filter = new RowIDFilter("FDB_OID"); filter.IDs.Add((int)feature["OID"]); filter.AddField("*"); IFeatureClass fc = _edgeFcs[(int)feature["FCID"]]; using (IFeatureCursor c = await fc.GetFeatures(filter)) { return(await c.NextFeature()); } } } catch { } } return(feature); }
public IFeature GetEdgeFeature(int eid) { RowIDFilter filter = new RowIDFilter(String.Empty); filter.IDs.Add(eid); filter.AddField("*"); IFeature feature = null; using (IFeatureCursor cursor = GetEdgeFeatures(filter)) { feature = cursor.NextFeature; } if (feature != null && feature.FindField("FCID") != null && feature.FindField("OID") != null && _edgeFcs.ContainsKey(Convert.ToInt32(feature["FCID"]))) { IGraphEdge edge = _pageManager.GetEdge(eid); try { if (edge != null && edge.FcId == -1) // Complex Edge { filter = new RowIDFilter("FDB_OID"); filter.IDs.Add(Convert.ToInt32(feature["OID"])); filter.AddField("*"); IFeatureClass fc = _edgeFcs[Convert.ToInt32(feature["FCID"])]; using (IFeatureCursor c = fc.GetFeatures(filter)) { return(c.NextFeature); } } } catch { } } return(feature); }
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); }
/// <summary> /// Adds an edge to the graph, if not already contained. /// </summary> /// <param name="edge">The edge to be added.</param> /// <returns> /// <c>true</c> if the edge was added, <c>false</c> if it was already contained. /// </returns> protected internal bool AddEdge(IGraphEdge edge) { Contract.Requires(edge != null); if (this.edges.Contains(edge)) { return false; } this.edges.Add(edge); return true; }
async public Task <IFeatureCursor> GetEdgeFeatures(IQueryFilter filter) { if (_edgeFcs.Count == 0) { return(null); } if (filter is SpatialFilter) { List <IFeatureClass> edgeFcs = new List <IFeatureClass>(); if (_edgeFcs != null) { foreach (IFeatureClass fc in _edgeFcs.Values) { edgeFcs.Add(fc); } } return(new NetworkFeatureCursor(_fdb, _name, edgeFcs, null, filter)); } if (filter is RowIDFilter) { RowIDFilter idFilter = (RowIDFilter)filter; Dictionary <int, QueryFilter> rfilters = new Dictionary <int, QueryFilter>(); Dictionary <int, Dictionary <int, List <FieldValue> > > additionalFields = new Dictionary <int, Dictionary <int, List <FieldValue> > >(); foreach (int eid in idFilter.IDs) { IGraphEdge edge = _pageManager.GetEdge(eid); if (edge == null || _edgeFcs.ContainsKey(edge.FcId) == false) { continue; } if (!rfilters.ContainsKey(edge.FcId)) { string idFieldName = "FDB_OID"; string shapeFieldName = "FDB_SHAPE"; IFeatureClass fc = edge.FcId >= 0 ? await _fdb.GetFeatureclass(edge.FcId) : null; if (fc != null) { idFieldName = fc.IDFieldName; shapeFieldName = fc.ShapeFieldName; } rfilters.Add(edge.FcId, new RowIDFilter(edge.FcId >= 0 ? idFieldName : "EID")); rfilters[edge.FcId].AddField(shapeFieldName); additionalFields.Add(edge.FcId, new Dictionary <int, List <FieldValue> >()); rfilters[edge.FcId].IgnoreUndefinedFields = true; rfilters[edge.FcId].AddField(shapeFieldName); if (filter.SubFields.IndexOf("*") != -1) { rfilters[edge.FcId].AddField("*"); } else { foreach (string field in filter.SubFields.Split(' ')) { if (field == shapeFieldName) { continue; } if (fc.Fields.FindField(field) != null) { rfilters[edge.FcId].AddField(fc.Fields.FindField(field).name); } } } } ((RowIDFilter)rfilters[edge.FcId]).IDs.Add(edge.FcId >= 0 ? edge.Oid : edge.Eid); additionalFields[edge.FcId].Add(edge.FcId >= 0 ? edge.Oid : edge.Eid, new List <FieldValue>() { new FieldValue("_eid", edge.Eid) }); } if (rfilters.ContainsKey(-1)) { RowIDFilter complexEdgeFilter = (RowIDFilter)rfilters[-1]; QueryFilter ceFilter = new QueryFilter(complexEdgeFilter); ceFilter.WhereClause = complexEdgeFilter.RowIDWhereClause; rfilters[-1] = ceFilter; } return(new CursorCollection <int>(_edgeFcs, rfilters, additionalFields)); } return(null); }
public void AppendEdge(IGraphEdge <TGraphNode> edge, double estimatedRemainingCost) { edges.Add(edge); EstimatedTotalCost = CostSoFar + estimatedRemainingCost; }
public void AppendEdge(IGraphEdge <GraphNode> edge, double estimatedRemainingCost) { this.edges.Add(edge); //this.costSoFar = null; this.EstimatedTotalCost = this.CostSoFar + edge.Cost + estimatedRemainingCost; }
public void AddEdge(IGraphEdge edge) { edges.Add(edge); Analyze(); }
public bool IsRegistered(IGraphEdge edge) { return(registeredEdges.Contains(edge)); }
protected override float GetCost(float prevNodeCost, IGraphEdge <T> edge) { return(edge.weight); }
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); }
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); }
/// <summary> /// Adds a connected edge. /// </summary> /// <param name="edge">The edge.</param> /// <returns> /// <c>true</c> if the edge was added, <c>false</c> if it was already contained. /// </returns> protected virtual bool AddConnectedEdge(IGraphEdge edge) { if (this.connectedEdges.Contains(edge)) { return false; } this.connectedEdges.Add(edge); return true; }
public void RegisterConnection(IGraphEdge edge) { registeredEdges.Add(edge); }
public QueueType(float first, IGraphEdge <T> second) : base(first, second) { }
public BFSNode(IGraphNode <IGraphEdge> node, IGraphEdge edge, BFSNode parent) { this.Node = node; this.Edge = edge; this.Parent = parent; }
protected override float GetCost(float prevNodeCost, IGraphEdge <T> edge) { return(prevNodeCost + edge.weight + m_h(edge.node2.storedData, destination.storedData)); }
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); }
protected abstract float GetCost(float prevNodeCost, IGraphEdge <T> edge);