Esempio n. 1
0
        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());
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        public static HeuristicSearchBase <Point, Point> ApplyHeuristicFunction(HeuristicSearchBase <Point, Point> queryable, string[] heuristicNames)
        {
            var orderBy = default(HeuristicSearchOrderBy <Point, Point>);
            var goal    = queryable.To;

            switch (heuristicNames.FirstOrDefault())
            {
            case nameof(PointExtensions.GetChebyshevDistance):
                orderBy = queryable.OrderBy(p => p.GetChebyshevDistance(goal));
                break;

            case nameof(PointExtensions.GetEuclideanDistance):
                orderBy = queryable.OrderBy(p => p.GetEuclideanDistance(goal));
                break;

            case nameof(PointExtensions.GetManhattanDistance):
                orderBy = queryable.OrderBy(p => p.GetManhattanDistance(goal));
                break;

            default:
                return(queryable);
            }
            foreach (var heuristicName in heuristicNames.Skip(1).Take(2))
            {
                switch (heuristicName)
                {
                case nameof(PointExtensions.GetChebyshevDistance):
                    orderBy = orderBy.ThenBy(p => p.GetChebyshevDistance(goal));
                    break;

                case nameof(PointExtensions.GetEuclideanDistance):
                    orderBy = orderBy.ThenBy(p => p.GetEuclideanDistance(goal));
                    break;

                case nameof(PointExtensions.GetManhattanDistance):
                    orderBy = orderBy.ThenBy(p => p.GetManhattanDistance(goal));
                    break;

                default:
                    continue;
                }
            }
            return(orderBy);
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
 internal ObservableRecursiveBestFirstSearch(HeuristicSearchBase <TFactor, TStep> source, IProgress <AlgorithmState <TFactor, TStep> > observer)
     : base(source)
 {
     _observer = observer;
 }
Esempio n. 6
0
 public static Node <TFactor, TStep> Run <TFactor, TStep>(HeuristicSearchBase <TFactor, TStep> source)
 {
     return(new RecursiveBestFirstSearch <TFactor, TStep>(source).Run());
 }
Esempio n. 7
0
 internal IterativeDeepeningAStarBase(HeuristicSearchBase <TFactor, TStep> source)
 {
     Source = source;
 }
Esempio n. 8
0
 internal RecursiveBestFirstSearchBase(HeuristicSearchBase <TFactor, TStep> source)
 {
     Source       = source;
     NodeComparer = source.NodeComparer.FactorOnlyComparer;
 }
Esempio n. 9
0
 internal ObservableIterativeDeepeningAStar(HeuristicSearchBase <TFactor, TStep> source, IProgress <AlgorithmState <TFactor, TStep> > observer)
     : base(source)
 {
     _observer = observer;
 }
Esempio n. 10
0
        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());
        }
Esempio n. 11
0
 Node <TFactor, TStep> IObservableAlgorithm.Run <TFactor, TStep>(HeuristicSearchBase <TFactor, TStep> source, IProgress <AlgorithmState <TFactor, TStep> > observer)
 {
     return(Run(source, observer));
 }
Esempio n. 12
0
 Node <TFactor, TStep> IAlgorithm.Run <TFactor, TStep>(HeuristicSearchBase <TFactor, TStep> source)
 {
     return(Run(source));
 }
 public static IEnumerable <TResult> Run <TResult, TStep>(HeuristicSearchBase <TResult, TStep> source)
 {
     return(new IterativeDeepeningAStar <TResult, TStep>(source));
 }
        IProgress <AlgorithmState <TFactor, Point> > IAlgorithmObserverFactory <Point> .Create <TFactor>(HeuristicSearchBase <TFactor, Point> source)
        {
            var progress = new AlgorithmObserver <TFactor>();

            Details.Clear();
            progress.Callback = ProgressCallback;

            return(progress);
        }
Esempio n. 15
0
        public static Node <TFactor, TStep> Run <TFactor, TStep>(HeuristicSearchBase <TFactor, TStep> source)
        {
            Debug.WriteLine("LINQ Expression Stack: {0}", source);

            return(new RecursiveBestFirstSearch <TFactor, TStep>(source).Run());
        }
Esempio n. 16
0
        internal static Node <TFactor, TStep> Run <TFactor, TStep>(HeuristicSearchBase <TFactor, TStep> source)
        {
            Debug.WriteLine("LINQ Expression Stack: {0}", source);

            return(new IterativeDeepeningAStar <TFactor, TStep>(source).Run());
        }
Esempio n. 17
0
        public static Node <TFactor, TStep> Run <TFactor, TStep>(HeuristicSearchBase <TFactor, TStep> source, IProgress <AlgorithmState <TFactor, TStep> > observer)
        {
            Debug.WriteLine("LINQ Expression Stack: {0}", source);

            return(new ObservableRecursiveBestFirstSearch <TFactor, TStep>(source, observer).Run());
        }
Esempio n. 18
0
        internal static Node <TFactor, TStep> Run <TFactor, TStep>(HeuristicSearchBase <TFactor, TStep> source, IProgress <AlgorithmState <TFactor, TStep> > observer)
        {
            Debug.WriteLine("LINQ Expression Stack: {0}", source);

            return(new ObservableIterativeDeepeningAStar <TFactor, TStep>(source, observer).Run());
        }
 public static IEnumerable <TResult> Run <TResult, TStep>(HeuristicSearchBase <TResult, TStep> source)
 {
     return(new RecursiveBestFirstSearch <TResult, TStep>(source));
 }
Esempio n. 20
0
 public static Node <TFactor, TStep> Run <TFactor, TStep>(HeuristicSearchBase <TFactor, TStep> source)
 {
     return(new IterativeDeepeningAStar <TFactor, TStep>(source).Run());
 }