public override void Clear() { ShortcutSet = null; Source = null; Targets = new JHashSet <TPoint2D>(); ShortcutSet = null; }
public override IShortcutSet GetShortcuts(int level, double epsilon) { var input = new MSSInput(Input.Trajectory, new List <double> { epsilon }) { PrunedPoints = prunedPoints, SearchIntervals = searchIntervals }; MSSOutput output; Algorithm.Compute(input, out output); var shortcuts = output.GetShortcuts(1); if (Cumulative) { if (prunedShortcuts != null) { shortcuts.Except(prunedShortcuts); prunedShortcuts.Union(shortcuts); } else { prunedShortcuts = shortcuts; } } //Output.LogLine(output.LogString); Output.LogObject("Number of shortcuts found on level " + level, shortcuts.Count); return(shortcuts); }
public override void Init(MSInput input, MSOutput output, bool cumulative) { base.Init(input, output, cumulative); prunedShortcuts = null; searchIntervals = null; prunedPoints = new HashSet <TPoint2D>(); }
public SPFInput(Trajectory2D Trajectory, IShortcutSet ShortcutSet, TPoint2D Source, JHashSet <TPoint2D> Targets, bool CreatePath = true) { this.Trajectory = Trajectory; this.ShortcutSet = ShortcutSet; this.Source = Source; this.Targets = Targets; this.CreatePath = CreatePath; }
public void Except(IShortcutSet set) { var graph = (ShortcutGraph)set; foreach (var shortcut in graph.Shortcuts.Keys) { RemoveShortcut(shortcut); } }
public void Union(IShortcutSet set) { var graph = (ShortcutGraph)set; foreach (var pair in graph.Shortcuts) { var weightedEdge = (WeightedEdge)pair.Value; AddShortcut(pair.Key, weightedEdge.Data); } }
public override Dictionary <TPoint2D, ShortcutPath> FindShortestPaths(IShortcutSet set, TPoint2D source, JHashSet <TPoint2D> targets, bool createPaths = true) { var input = new SPFInput { Trajectory = set.Trajectory, ShortcutSet = set, Source = source, Targets = targets, CreatePath = createPaths }; SPFOutput output; algorithm.Compute(input, out output); return(output.ShortcutPaths); }
public override ShortcutPath FindShortestPath(IShortcutSet set, TPoint2D source, TPoint2D target, bool createPath = true) { var input = new SPFInput { Trajectory = set.Trajectory, ShortcutSet = set, Source = source, Targets = new JHashSet <TPoint2D> { target }, CreatePath = createPath }; SPFOutput output; algorithm.Compute(input, out output); return(output.GetPath(target)); }
public void Intersect(IShortcutSet set) { var graph = (ShortcutGraph)set; var shortcutsToRemove = new HashSet <Shortcut>(); foreach (var shortcut in Shortcuts.Keys) { if (!graph.Shortcuts.ContainsKey(shortcut)) { shortcutsToRemove.Add(shortcut); } } foreach (var shortcut in shortcutsToRemove) { RemoveShortcut(shortcut, false); } }
public abstract Dictionary <TPoint2D, ShortcutPath> FindShortestPaths(IShortcutSet set, TPoint2D source, JHashSet <TPoint2D> targets, bool createPaths = true);
public abstract ShortcutPath FindShortestPath(IShortcutSet set, TPoint2D source, TPoint2D target, bool createPath = true);
//worst case O(n^2), expected O(n) public void Except(IShortcutSet set) { var otherIntervalMap = ((ShortcutIntervalSet)set).IntervalMap; foreach (var start in otherIntervalMap.Keys) { if (!IntervalMap.ContainsKey(start)) { continue; } var intervals = IntervalMap[start]; var otherIntervals = otherIntervalMap[start]; var intervalsNode = intervals.First; var otherNode = otherIntervals.First; //worst case O(n), expected O(1) while (intervalsNode != null) { var interval = intervalsNode.Value; if (otherNode == null) { break; } var otherInterval = otherNode.Value; //other interval too far back, consider next one for overlap if (otherInterval.End.Index < interval.Start.Index) { otherNode = otherNode.Next; continue; } //interval too far back, consider next one for overlap if (otherInterval.Start.Index > interval.End.Index) { intervalsNode = intervalsNode.Next; continue; } //it overlaps at the start: prune interval start if (otherInterval.End.Index < interval.End.Index && otherInterval.Start.Index <= interval.Start.Index) { interval.Start = Trajectory[otherInterval.End.Index + 1]; otherNode = otherNode.Next; continue; } //it overlaps at the end: prune interval end if (otherInterval.Start.Index > interval.Start.Index && otherInterval.End.Index >= interval.End.Index) { interval.End = Trajectory[otherInterval.Start.Index - 1]; intervalsNode = intervalsNode.Next; continue; } //spanning other interval if (otherInterval.Start.Index > interval.Start.Index && otherInterval.End.Index < interval.End.Index) { //second part intervalsNode = intervals.AddAfter(intervalsNode, new Interval(Trajectory[otherInterval.End.Index + 1], interval.End)); //first part interval.End = Trajectory[otherInterval.Start.Index - 1]; otherNode = otherNode.Next; continue; } //full overlap var nextNode = intervalsNode.Next; intervals.Remove(intervalsNode); intervalsNode = nextNode; } } }
//worst case O(n^2), expected O(n) public void Union(IShortcutSet set) { var otherIntervalMap = ((ShortcutIntervalSet)set).IntervalMap; foreach (var start in otherIntervalMap.Keys) { if (!IntervalMap.ContainsKey(start)) { continue; } var intervals = IntervalMap[start]; var otherIntervals = otherIntervalMap[start]; var intervalsNode = intervals.First; var otherNode = otherIntervals.First; while (otherNode != null) { if (intervalsNode == null) { intervals.AddLast(otherNode.Value.Clone()); otherNode = otherNode.Next; continue; } var interval = intervalsNode.Value; var otherInterval = otherNode.Value; //not yet reached if (otherInterval.Start.Index > interval.End.Index + 1) { intervalsNode = intervalsNode.Next; continue; } //we passed it: simply add the interval if (otherInterval.End.Index < interval.Start.Index - 1) { intervals.AddBefore(intervalsNode, new Interval(otherInterval.Start, otherInterval.End)); otherNode = otherNode.Next; continue; } //it connects at the start: extend interval backwards if (otherInterval.End.Index >= interval.Start.Index - 1) { interval.Start = otherInterval.Start; } //it connects at the end: extend interval forward if (otherInterval.Start.Index <= interval.End.Index + 1) { interval.End = otherInterval.End; var tempNode = intervalsNode.Next; while (tempNode != null) { var nextNode = tempNode.Next; var furtherInterval = tempNode.Value; //remove any fully overlapped intervals if (furtherInterval.End.Index <= interval.End.Index) { intervals.Remove(tempNode); tempNode = nextNode; continue; } //merge any interval that may connect at the end if (furtherInterval.Start.Index <= interval.End.Index + 1) { intervals.Remove(tempNode); interval.End = furtherInterval.End; } break; } } otherNode = otherNode.Next; } } }
//worst case O(n^2), expected O(n) public void Intersect(IShortcutSet set) { var otherIntervalMap = ((ShortcutIntervalSet)set).IntervalMap; foreach (var start in otherIntervalMap.Keys) { if (!IntervalMap.ContainsKey(start)) { continue; } var intervals = IntervalMap[start]; var otherIntervals = otherIntervalMap[start]; var intervalsNode = intervals.First; var otherNode = otherIntervals.First; //worst case O(n), expected O(1) while (intervalsNode != null) { var nextNode = intervalsNode.Next; var interval = intervalsNode.Value; if (otherNode == null) { intervals.Remove(intervalsNode); } else { var otherInterval = otherNode.Value; //other interval too far back, consider next one for overlap if (otherInterval.End.Index < interval.Start.Index) { otherNode = otherNode.Next; continue; } //other interval too far ahead if (otherInterval.Start.Index > interval.End.Index) { intervals.Remove(intervalsNode); } else //some overlap { var nextOtherInterval = otherNode.Next?.Value; var nextOverlaps = false; if (nextOtherInterval != null) { nextOverlaps = nextOtherInterval.Start.Index <= interval.End.Index; } if (nextOverlaps) //split interval { var newInterval = new Interval(interval.Start, interval.End); //it connects at the start: prune interval end if (otherInterval.End.Index < interval.End.Index) { newInterval.End = otherInterval.End; } //it connects at the end: prune interval start if (otherInterval.Start.Index > interval.Start.Index) { newInterval.Start = otherInterval.Start; } intervals.AddBefore(intervalsNode, newInterval); otherNode = otherNode.Next; continue; } //it connects at the start: prune interval end if (otherInterval.End.Index < interval.End.Index) { interval.End = otherInterval.End; } //it connects at the end: prune interval start if (otherInterval.Start.Index > interval.Start.Index) { interval.Start = otherInterval.Start; } } } intervalsNode = nextNode; } } }
protected override Dictionary <Shortcut, int> GetShortcutWeights(ShortcutGraph graph, IShortcutSet levelShortcuts) { var weights = new Dictionary <Shortcut, int>(); foreach (var shortcut in levelShortcuts) { var path = ShortestPathProvider.FindShortestPath(graph, shortcut.Start, shortcut.End); weights[shortcut] = path.Weight; } return(weights); }
protected override Dictionary <Shortcut, int> GetShortcutWeights(ShortcutGraph graph, IShortcutSet levelShortcuts) { var levelMap = levelShortcuts.AsMap(); var weights = new Dictionary <Shortcut, int>(); foreach (var pair in levelMap) { var start = pair.Key; var ends = pair.Value; var paths = ShortestPathProvider.FindShortestPaths(graph, start, ends, false); foreach (var end in ends) { weights[new Shortcut(start, end)] = paths[end].Weight; } } return(weights); }
protected abstract Dictionary <Shortcut, int> GetShortcutWeights(ShortcutGraph graph, IShortcutSet levelShortcuts);