/// <summary> /// Updates the cost of the vertex using its existing cost plus the /// cost to traverse the specified edge. If the search is in simple /// path mode, only one path will be accrued. /// </summary> /// <param name="vertex">The vertex to update.</param> /// <param name="edge">The edge through which the vertex is reached.</param> /// <param name="cost">The current cost to reach the vertex from the source.</param> /// <param name="replace">True to indicate that any accrued edges are not to be cleared. /// False to indicate that the edge should be added to the previously accrued edges as they yield the same cost.</param> public void UpdateVertex(V vertex, E edge, IWeight cost, bool replace) { Costs.AddOrSet(vertex, cost); if (edge is null) { return; } ISet <E> edges = Parents.GetOrDefault(vertex); if (edges is null) { edges = new HashSet <E>(); Parents.Add(vertex, edges); } if (replace) { edges.Clear(); } if (MaxPaths == AllPaths || edges.Count < MaxPaths) { edges.Add(edge); } }
/// <summary> /// If possible, relax the given edge using the supplied base cost and edge-weight function. /// </summary> /// <param name="edge">The edge to relax.</param> /// <param name="cost">The base cost to reach the edge destination vertex.</param> /// <param name="ew">The edge weigher.</param> /// <param name="forbidNegatives">If true, negative values will forbid the link.</param> /// <returns>True if the edge was relaxed, otherwise false.</returns> public bool RelaxEdge(E edge, IWeight cost, IEdgeWeigher <V, E> ew, bool forbidNegatives = true) { V v = edge.Dst; IWeight hopCost = ew.GetWeight(edge); if ((!hopCost.IsViable) || (hopCost.IsNegative && forbidNegatives)) { return(false); } IWeight newCost = cost.Merge(hopCost); int compareResult = -1; if (HasCost(v)) { IWeight oldCost = GetCost(v); compareResult = newCost.CompareTo(oldCost); } if (compareResult <= 0) { UpdateVertex(v, edge, newCost, compareResult < 0); } return(compareResult < 0); }
/****************************************************** * MonoBehaviour methods, OnDrawGizmos ******************************************************/ #if UNITY_EDITOR void OnDrawGizmos() { if (manager.ShowPaths) { if (Selection.gameObjects.Length > 0 && Selection.gameObjects[0] == this.gameObject) { Gizmos.color = Color.red; Handles.color = Color.red; } else { Gizmos.color = Color.green; Handles.color = Color.green; } for (int i = 0; i < ToLocations.Count; i++) { Gizmos.DrawLine(this.transform.position, ToLocations[i].transform.position); Handles.ArrowCap(0, this.transform.position, Quaternion.LookRotation(-(this.transform.position - ToLocations[i].transform.position).normalized), 6); } IWeight weightInterface = (IWeight)GetComponent(typeof(IWeight)); if (weightInterface != null) { Gizmos.color = Color.blue; } else { Gizmos.color = Color.yellow; } Gizmos.DrawSphere(transform.position, 1); } }
public void CreateDirectedEdge(INode _StartNode, INode _EndNode, IWeight _Weight) { var hNewDirectedEdge = new DirectedEdge(_StartNode, _EndNode, _Weight); // Todo: Checken dass keine Duplikate entstehen? FEdgeIndices.Add(hNewDirectedEdge); _StartNode.AddEdge(hNewDirectedEdge); }
public Node(string Id, IWeight weight) { this.Id = Id; this.weight = weight; this.priority = 0; Successors = new List <Edge>(); Predecessors = new List <Edge>(); }
protected void ValidatePath(IPath <TestVertex, TestEdge> p, TestVertex src, TestVertex dst, int length, IWeight cost) { Assert.Equal(length, p.Edges.Count); Assert.Equal(src, p.Src); Assert.Equal(dst, p.Dst); Assert.Equal(cost, p.Cost); }
public IWeight Extend(CallCmd cmd, IWeight summary) { if (isZero || (summary as ConstantProp).isZero) { return(new ConstantProp()); } return(ApplyCall(cmd, summary as ConstantProp)); }
public Edge CreateUndirectedEdge(INode _NodeOne, INode _NodeTwo, IWeight _Weight) { var hNewUndirectedEdge = new UndirectedEdge(_NodeOne, _NodeTwo, _Weight); FEdgeIndices.Add(hNewUndirectedEdge); _NodeOne.AddEdge(hNewUndirectedEdge); _NodeTwo.AddEdge(hNewUndirectedEdge); return(hNewUndirectedEdge); }
/// <summary> /// Sums the given edges using the given weigher. /// </summary> /// <param name="weigher">The weigher to use.</param> /// <param name="edges">The edges to sum.</param> /// <returns>The sum of path cost between the given edges.</returns> private IWeight CalculatePathCost(IEdgeWeigher <V, E> weigher, IEnumerable <E> edges) { IWeight totalCost = weigher.InitialWeight; foreach (E edge in edges) { totalCost = totalCost.Merge(weigher.GetWeight(edge)); } return(totalCost); }
/// <summary> /// Initializes a new path from the give list of edges and cost. /// </summary> /// <param name="edges">The list of path edges.</param> /// <param name="cost">The path cost.</param> public DefaultPath(IList <E> edges, IWeight cost) { if (edges?.Count is 0) { throw new ArgumentException("There must be at least one edge."); } this.edges = edges; Src = Edges[0].Src; Dst = Edges.Last().Dst; Cost = cost; }
public Edge CreateDirectedEdge(int _StartNodeId, int _TargetNodeId, IWeight _Weight) { var hStartNode = FNodeIndices[_StartNodeId]; var hTargetNode = FNodeIndices[_TargetNodeId]; var hNewDirectedEdge = new DirectedEdge(hStartNode, hTargetNode, _Weight); FEdgeIndices.Add(hNewDirectedEdge); hStartNode.AddEdge(hNewDirectedEdge); return(hNewDirectedEdge); }
public Edge CreateUndirectedEdge(int _NodeOneId, int _NodeTwoId, IWeight _Weight) { var hNodeOne = FNodeIndices[_NodeOneId]; var hNodeTwo = FNodeIndices[_NodeTwoId]; var hNewUndirectedEdge = new UndirectedEdge(hNodeOne, hNodeTwo, _Weight); FEdgeIndices.Add(hNewUndirectedEdge); hNodeOne.AddEdge(hNewUndirectedEdge); hNodeTwo.AddEdge(hNewUndirectedEdge); return(hNewUndirectedEdge); }
void AddNode(IWeight node) { if (nodes.TryGetValue(node.Weight, out var list)) { list.Add(node); } else { nodes.Add(node.Weight, new List <IWeight> { node }); } }
/// <summary> /// Intializes the client and sets the request header user-agent to the consumingApplicationName /// </summary> /// <param name="consumingApplicationName">The name of the application that is consuming the SDK</param> /// <param name="metriksApiBaseAddress">The base address of the API</param> internal Client(string consumingApplicationName, string metriksApiBaseAddress, IApiClient client = null) { if (client == null) { apiClient = InitializeClient(consumingApplicationName, metriksApiBaseAddress); } else { apiClient = client; } Weather = new WeatherApiClient(apiClient); Weight = new WeightApiClient(apiClient); }
public ConveyorBelt(int numberOfBins, int targetWeight) { Items = new List <Item>(); _inFeeder = new InFeeder(); _weight = new Weight(); _portioner = new Portioner(numberOfBins); TotalBinWeight = new List <int>(); _targetWeight = targetWeight; for (int i = 0; i < numberOfBins; i++) { TotalBinWeight.Add(0); } }
void RemoveNode(IWeight node) { if (nodes.TryGetValue(node.Weight, out var list)) { if (list.Count >= 2) { list.Remove(node); } else { nodes.Remove(node.Weight); } } }
/// <inheritdoc/> protected override IResult <V, E> InternalSearch(IGraph <V, E> graph, V src, V dst, IEdgeWeigher <V, E> weigher, int maxPaths = -1) { // Use the default result to remember cumulative costs and // parent edges to each respective vertex. var result = new DefaultResult(src, dst, maxPaths); // Cost to reach the source vertex is 0, of course. result.UpdateVertex(src, null, weigher.InitialWeight, false); if (graph.Edges.Count is 0) { result.BuildPaths(); return(result); } // Use the min priority queue to progressively find each // nearest vertex until we reach the desired destination, // if one was given, or until we reach all possible destinations. Heap <V> minQueue = new Heap <V>(graph.Vertices, new PathCostComparer(result)); while (minQueue.Count > 0) { // Get the nearest vertex. V nearest = minQueue.ExtractExtreme(); if (nearest.Equals(dst)) { break; } // Find its cost and use it to determine if the vertex is reachable. if (result.HasCost(nearest)) { IWeight cost = result.GetCost(nearest); // If the vertex is reachable, relax all its egress edges. foreach (E e in graph.GetEdgesFrom(nearest)) { result.RelaxEdge(e, cost, weigher, true); } } // Reprioritize the min queue. minQueue.Heapify(); } // Build a set of paths from the results and return it. result.BuildPaths(); return(result); }
protected void ExecuteDefaultTest(int pathCount, int pathLength, IWeight pathCost) { Graph = new TestAdjacencyListsGraph(Vertices, Edges); IGraphPathSearch <TestVertex, TestEdge> search = GraphSearch; ISet <IPath <TestVertex, TestEdge> > paths = search.Search(Graph, A, H, Weigher).Paths; Assert.Equal(1, paths.Count); IPath <TestVertex, TestEdge> p = paths.First(); Assert.Equal(A, p.Src); Assert.Equal(H, p.Dst); Assert.Equal(pathLength, p.Edges.Count); Assert.Equal(pathCost, p.Cost); paths = search.Search(Graph, A, null, Weigher).Paths; PrintPaths(paths); Assert.Equal(pathCount, paths.Count); }
public bool Combine(IWeight iweight) { var weight = iweight as ConstantProp; Debug.Assert(weight != null); if (impl == null) { impl = weight.impl; } if (weight.isZero) { return(false); } var changed = false; if (isZero) { changed = true; } isZero = false; foreach (var kvp in weight.val) { var key = kvp.Key; var value = kvp.Value; if (!val.ContainsKey(key)) { if (!value.IsBot()) { val.Add(key, new Value(value)); changed = true; } } else { var t = val[key].UnionWith(value); changed = changed || t; } } return(changed); }
protected void ExecuteDefaultTest(int minLength, int maxLength, IWeight minCost, IWeight maxCost) { Graph = new TestAdjacencyListsGraph(Vertices, Edges); TestAbstractGraphPathSearch search = GraphSearch; SpanningTreeResult result = (SpanningTreeResult)search.Search(Graph, A, H, Weigher, 1); ISet <IPath <TestVertex, TestEdge> > paths = result.Paths; Assert.Equal(1, paths.Count); IPath <TestVertex, TestEdge> path = paths.First(); Console.WriteLine(path); Assert.Equal(A, path.Src); Assert.Equal(H, path.Dst); int l = path.Edges.Count; Assert.True(minLength <= l && l <= maxLength); Assert.True(path.Cost.CompareTo(minCost) >= 0 && path.Cost.CompareTo(maxCost) <= 0); Console.WriteLine(result.Edges); PrintPaths(paths); }
public UndirectedEdge(INode _NodeA, INode _NodeB, IWeight _Weight) : base(_Weight) { FNodeA = _NodeA; FNodeB = _NodeB; }
public Node(IWeight left, IWeight right) { Weight = left.Weight + right.Weight; Left = left; Right = right; }
public static double WeightPounds(this IWeight client) { return(client.Weight * 2.20462262); }
/// <summary> /// Creates a new path as the copy of the given path. /// </summary> /// <param name="path">The path to copy.</param> public DefaultMutablePath(IPath <V, E> path) { CheckNotNull(path, "The path cannot be null."); Cost = path.Cost; edges.AddRange(path.Edges); }
public static void GiftWeight(IWeight gift) { Console.WriteLine(gift.GetWeight()); }
public void AddWeight(IWeight _Weight) { FWeight.Add(_Weight); }
/// <summary> /// Creates a new edge between the specified source and destination vertices with the given weight. /// </summary> /// <param name="src">The source vertex.</param> /// <param name="dst">The destination vertex.</param> /// <param name="weight">The edge weight.</param> public TestEdge(TestVertex src, TestVertex dst, IWeight weight) : base(src, dst) { Weight = weight; }
public DirectedEdge(INode _StartNode, INode _EndNode, IWeight _Weight) : base(_Weight) { FStartNode = _StartNode; FEndNode = _EndNode; }
protected void ExecuteSinglePathSearch(IGraphPathSearch <TestVertex, TestEdge> search, IGraph <TestVertex, TestEdge> graph, TestVertex src, TestVertex dst, IEdgeWeigher <TestVertex, TestEdge> weigher, int pathCount, IWeight pathCost) { IResult <TestVertex, TestEdge> result = search.Search(graph, src, dst, weigher, 1); ISet <IPath <TestVertex, TestEdge> > paths = result.Paths; PrintPaths(paths); Assert.Equal(Math.Min(pathCount, 1), paths.Count); if (pathCount > 0) { IPath <TestVertex, TestEdge> path = paths.First(); Assert.Equal(pathCost, path.Cost); } }
protected Edge(IWeight _Weight) : this() { FWeight.Add(_Weight); }