public static Node <TFactor, TStep> Run <TFactor, TStep>(HeuristicSearchBase <TFactor, TStep> source) { var open = new List <Node <TFactor, TStep> >(source.ConvertToNodes(source.From, 0)); if (open.Count == 0) { return(null); } open.Sort(source.NodeComparer); var closed = new HashSet <TStep>(source.StepComparer); var sortAt = 0; while (open.Count > 0) { var current = open[sortAt]; var hasNext = false; if (source.StepComparer.Equals(current.Step, source.To)) { return(current); } sortAt++; // open.RemoveAt(0); closed.Add(current.Step); foreach (var next in source.Expands(current.Step, current.Level)) { if (closed.Contains(next.Step)) { continue; } if (open.Find(step => source.StepComparer.Equals(next.Step, step.Step)) == null) { Debug.WriteLine($"{current.Step}\t{current.Level} -> {next.Step}\t{next.Level}"); next.Previous = current; open.Add(next); hasNext = true; } } if (hasNext) { open.Sort(sortAt, open.Count - sortAt, source.NodeComparer); } } return(null); }
public static Node <TFactor, TStep> Run <TFactor, TStep>(HeuristicSearchBase <TFactor, TStep> source, IProgress <AlgorithmState <TFactor, TStep> > observer) { Debug.WriteLine("LINQ Expression Stack: {0}", source); var nc = source.NodeComparer.FactorOnlyComparer; var sc = source.StepComparer; var nexts = new List <Node <TFactor, TStep> >(source.ConvertToNodes(source.From, 0)); if (nexts.Count == 0) { return(observer.NotFound()); } var visited = new HashSet <TStep>(sc); var sortAt = 0; while (nexts.Count - sortAt > 0) { var best = observer.InProgress(nexts[sortAt], nexts.GetRange(sortAt, nexts.Count - sortAt)); // nexts.First(); var sortAll = false; if (sc.Equals(best.Step, source.To)) { return(observer.Found(best, nexts.GetRange(sortAt, nexts.Count - sortAt))); } sortAt++; // nexts.RemoveAt(0); foreach (var next in source.Expands(best.Step, best.Level, visited.Add)) { next.Previous = best; if (sc.Equals(next.Step, source.To)) { return(observer.Found(next, nexts.GetRange(sortAt, nexts.Count - sortAt))); } sortAll = sortAll || nc.Compare(nexts[nexts.Count - 1], next) > 0; nexts.Add(next); Debug.WriteLine($"{best.Step}\t{best.Level} -> {next.Step}\t{next.Level}"); } if (sortAll) { nexts.Sort(sortAt, nexts.Count - sortAt, nc); } } return(observer.NotFound()); }
public static Node <TFactor, TStep> Run <TFactor, TStep>(HeuristicSearchBase <TFactor, TStep> source) { var nexts = new List <Node <TFactor, TStep> >(source.ConvertToNodes(source.From, 0)); if (nexts.Count == 0) { return(null); } var visited = new HashSet <TStep>(source.StepComparer); var sortAt = 0; while (nexts.Count > 0) { var best = nexts[sortAt]; // nexts.First(); var hasNext = false; if (source.StepComparer.Equals(best.Step, source.To)) { return(best); } sortAt++; // nexts.RemoveAt(0); foreach (var next in source.Expands(best.Step, best.Level, visited.Add)) { Debug.WriteLine($"{best.Step}\t{best.Level} -> {next.Step}\t{next.Level}"); next.Previous = best; nexts.Add(next); hasNext = true; } if (hasNext) { nexts.Sort(sortAt, nexts.Count - sortAt, source.NodeComparer.FactorOnlyComparer); } } return(null); }
public static Node <TFactor, TStep> Run <TFactor, TStep>(HeuristicSearchBase <TFactor, TStep> source, IProgress <AlgorithmState <TFactor, TStep> > observer) { Debug.WriteLine("LINQ Expression Stack: {0}", source); var nc = source.NodeComparer; var sc = source.StepComparer; var open = new List <Node <TFactor, TStep> >(source.ConvertToNodes(source.From, 0)); if (open.Count == 0) { return(observer.NotFound()); } open.Sort(nc); #if HASHSET_AS_CLOSED_LIST Debug.WriteLine($"Using {typeof(HashSet<TStep>).Name} as closed list."); var closed = new HashSet <TStep>(sc); #endif var sortAt = 0; while (open.Count - sortAt > 0) { var current = observer.InProgress(open[sortAt], open.GetRange(sortAt, open.Count - sortAt)); var sortAll = false; if (sc.Equals(current.Step, source.To)) { return(observer.Found(current, open.GetRange(sortAt, open.Count - sortAt))); } sortAt++; #if HASHSET_AS_CLOSED_LIST closed.Add(current.Step); #endif foreach (var next in source.Expands(current.Step, current.Level)) { // 1st if: search in closed list. // 2nd if: search in open list. #if HASHSET_AS_CLOSED_LIST if (closed.Contains(next.Step)) { continue; } #else if (open.FindLastIndex(sortAt - 1, step => sc.Equals(next.Step, step.Step)) != -1) { continue; } #endif if (open.FindIndex(sortAt, step => sc.Equals(next.Step, step.Step)) == -1) { next.Previous = current; if (sc.Equals(next.Step, source.To)) { return(observer.Found(next, open.GetRange(sortAt, open.Count - sortAt))); } sortAll = sortAll || nc.Compare(open[open.Count - 1], next) > 0; open.Add(next); Debug.WriteLine($"{current.Step}\t{current.Level} -> {next.Step}\t{next.Level}"); } } if (sortAll) { open.Sort(sortAt, open.Count - sortAt, nc); } } return(observer.NotFound()); }