private bool Resume(AbstractTile n) { while (_open.Count > 0) { var p = _open.Dequeue(); _closed.Add(p); if (p == n) { return(true); } foreach (var q in _graph.AdjacentVertices(p).Reverse()) { var gScore = _gScore.GetValueOrDefault(p, double.PositiveInfinity) + Metrics.Octile(p, q); var fScore = gScore + Metrics.Octile(q, _start); if (!_open.Contains(q) && !_closed.Contains(q)) { _open.Enqueue(q, double.PositiveInfinity); } if (_open.Contains(q) && fScore < _open.GetPriority(q)) { _gScore[q] = gScore; _open.UpdatePriority(q, fScore); } } } return(false); }
/// <summary> /// Helper utility that generates an uniform string representation of the graph /// </summary> /// <param name="graph">The graph to be represented in string format (must implement IGraph)</param> /// <returns>string</returns> internal static string Stringify(IGraph graph) { StringBuilder sb = new StringBuilder(); sb.AppendLine($"{graph.Vertices()} vertices, {graph.Edges()} edges"); for (int v = 0; v < graph.Vertices(); v++) { sb.AppendLine($"Vertex {v} connects to: {string.Join(" ", graph.AdjacentVertices(v))}"); } return(sb.ToString()); }
/// <summary> /// Ported from commodity_history_impl_t::find_price /// </summary> public PricePoint?FindPrice(Commodity source, DateTime moment, DateTime oldest = default(DateTime)) { RecentEdgeWeight recentEdgeWeight = new RecentEdgeWeight(moment, oldest); IGraph <Commodity, PriceGraphEdge> filtered = PriceGraph.Filter((comm1, comm2, edge) => recentEdgeWeight.Filter(comm1, comm2, edge)); Logger.Current.Debug("history.find", () => String.Format("sv commodity = {0}", source.Symbol)); if (source.Flags.HasFlag(CommodityFlagsEnum.COMMODITY_PRIMARY)) { Logger.Current.Debug("history.find", () => "sv commodity is primary"); } Logger.Current.Debug("history.find", () => "tv commodity = none "); DateTime mostRecent = moment; Amount price = null; foreach (EdgeDescriptor <Commodity, PriceGraphEdge> edgeDescriptor in filtered.AdjacentVertices(source)) { Logger.Current.Debug("history.find", () => String.Format("u commodity = {0}", edgeDescriptor.Vertex1.Symbol)); Logger.Current.Debug("history.find", () => String.Format("v commodity = {0}", edgeDescriptor.Vertex2.Symbol)); PricePoint point = edgeDescriptor.Edge.PricePoint; if (price == null || point.When > mostRecent) { mostRecent = point.When; price = point.Price; } Logger.Current.Debug("history.find", () => String.Format("price was = {0}", price.Unrounded())); if (price.Commodity == source) { price = new Amount(price.GetInvertedQuantity(), edgeDescriptor.GetInvertedVertex(source)); } Logger.Current.Debug("history.find", () => String.Format("price is = {0}", price.Unrounded())); } if (Amount.IsNullOrEmpty(price)) { Logger.Current.Debug("history.find", () => "there is no final price"); return(null); } else { Logger.Current.Debug("history.find", () => String.Format("final price is = {0}", price.Unrounded())); return(new PricePoint(mostRecent, price)); } }
public void Search(IGraph graph) { Path.RemoveRange(_posIndex, Path.Count - _posIndex); var cameFrom = new Dictionary <AbstractTile, AbstractTile>(); var gScore = new Dictionary <AbstractTile, double>(); var closed = new HashSet <AbstractTile>(); var open = new SimplePriorityQueue <AbstractTile, double>(); gScore[Position] = 0; open.Enqueue(Position, Helpers.Metrics.Octile(Position, Target)); // TODO add agitation noise (see Silver) AbstractTile current; while (open.Count > 0) { current = open.Dequeue(); closed.Add(current); if (current == Target) { Path.AddRange(ReconstructPath(cameFrom, current)); return; } foreach (var neighbor in graph.AdjacentVertices(current)) { if (closed.Contains(neighbor)) { continue; } var tentative_gScore = gScore.GetValueOrDefault(current, double.PositiveInfinity) + Helpers.Metrics.Octile(current, neighbor); // TODO add agitation noise (see Silver) if (tentative_gScore < gScore.GetValueOrDefault(neighbor, double.PositiveInfinity)) { cameFrom[neighbor] = current; gScore[neighbor] = tentative_gScore; var fScore = tentative_gScore + Helpers.Metrics.Octile(neighbor, Target); if (!open.TryUpdatePriority(neighbor, fScore)) { open.Enqueue(neighbor, fScore); } } } } }
public void MapPrices(Action <DateTime, Amount> fn, Commodity source, DateTime moment, DateTime oldest = default(DateTime), bool bidirectionally = false) { Logger.Current.Debug("history.map", () => String.Format("Mapping prices for source commodity: {0}", source)); RecentEdgeWeight recentEdgeWeight = new RecentEdgeWeight(moment, oldest); IGraph <Commodity, PriceGraphEdge> filtered = PriceGraph.Filter((comm1, comm2, edge) => recentEdgeWeight.Filter(comm1, comm2, edge)); foreach (EdgeDescriptor <Commodity, PriceGraphEdge> edgeDescriptor in filtered.AdjacentVertices(source)) { foreach (KeyValuePair <DateTime, Amount> pricePair in edgeDescriptor.Edge.Prices) { DateTime when = pricePair.Key; Amount price = pricePair.Value; Logger.Current.Debug("history.map", () => String.Format("Price {0} on {1}", price, when)); if ((oldest.IsNotADateTime() || when >= oldest) && when <= moment) { if (price.Commodity == source) { if (bidirectionally) { price = new Amount(price.GetInvertedQuantity(), edgeDescriptor.GetInvertedVertex(source)); Logger.Current.Debug("history.map", () => String.Format("Inverted price is {0}", price)); Logger.Current.Debug("history.map", () => String.Format("fn({0}, {1})", when, price)); fn(when, price); } } else { Logger.Current.Debug("history.map", () => String.Format("fn({0}, {1})", when, price)); fn(when, price); } } } } }