예제 #1
0
        public void testNormalSearchGraphSearchMinFrontier()
        {
            MapEnvironment me = new MapEnvironment(aMap);
            UniformCostSearch <string, MoveToAction> ucSearch = new UniformCostSearch <string, MoveToAction>(new GraphSearchReducedFrontier <string, MoveToAction>());

            SimpleMapAgent ma = new SimpleMapAgent(me.getMap(), me, ucSearch, new string[] { "D" });

            me.addAgent(ma, "A");
            me.AddEnvironmentView(new TestEnvironmentView(envChanges));
            me.StepUntilDone();

            Assert.AreEqual(
                "CurrentLocation=In(A), Goal=In(D):Action[name==moveTo, location==C]:Action[name==moveTo, location==D]:METRIC[nodesExpanded]=3:METRIC[queueSize]=1:METRIC[maxQueueSize]=2:METRIC[pathCost]=13:Action[name==NoOp]:",
                envChanges.ToString());
        }
예제 #2
0
        private void think()
        {
            /*
             *   Si le manoir est propre, pas besoin de faire d'exploration
             * */
            if (goalCompleted())
            {
                intention.Clear();
                Thread.Sleep(1000);
            }


            if (intention.Count == 0)
            {
                updateBelief();
                Problem p = new Problem(belief);
                //Choisit une action
                Position robotIterationPos = new Position(this.belief.robotPos);

                /*
                 * Changement de stratégie quand on clique sur le boutton switch strategy
                 *
                 */
                SearchStrategy strategy;
                if (uniformAlgo)
                {
                    strategy = new UniformCostSearch();
                }
                else
                {
                    strategy = new Asearch();
                }

                /*
                 * Exploration et planification de ses intentions
                 *
                 */
                foreach (Action action in TreeSearch(p, strategy).ToList())
                {
                    foreach (SimpleActionType simpleAction in action.generateSimpleAction(robotIterationPos))
                    {
                        intention.Enqueue(simpleAction);
                    }
                }
            }
        }
예제 #3
0
        public aima.core.search.framework.Search GetSearch(Object owner, IContextLookup globalVars, HeuristicFunction objHeuristicFunction)
        {
            aima.core.search.framework.Search toReturn;
            QueueSearch objQueueSearch;

            switch (QueueSearchType)
            {
            case QueueSearchType.TreeSearch:
                objQueueSearch = new TreeSearch();
                break;

            default:
                objQueueSearch = new GraphSearch();
                break;
            }
            switch (AlgorithmType)
            {
            case SearchAlgorithmType.KnownInformed:
                switch (KnownInformedAlgorithm)
                {
                case KnownInformedSearch.GreedyBestFirst:
                    toReturn = new GreedyBestFirstSearch(objQueueSearch, objHeuristicFunction);
                    break;

                case KnownInformedSearch.RecursiveGreedyBestFirst:
                    toReturn = new RecursiveBestFirstSearch(new GreedyBestFirstEvaluationFunction(objHeuristicFunction));
                    break;

                case KnownInformedSearch.RecursiveAStar:
                    toReturn = new RecursiveBestFirstSearch(new AStarEvaluationFunction(objHeuristicFunction));
                    break;

                case KnownInformedSearch.HillClimbing:
                    toReturn = new HillClimbingSearch(objHeuristicFunction);
                    break;

                case KnownInformedSearch.SimulatedAnnealing:
                    toReturn = new SimulatedAnnealingSearch(objHeuristicFunction);
                    break;

                default:
                    toReturn = new AStarSearch(objQueueSearch, objHeuristicFunction);
                    break;
                }
                break;

            case SearchAlgorithmType.KnownUninformed:
                switch (KnownUninformedAlgorithm)
                {
                case KnownUninformedSearch.DepthFirst:
                    toReturn = new DepthFirstSearch(objQueueSearch);
                    break;

                case KnownUninformedSearch.DepthLimited:
                    toReturn = new DepthLimitedSearch(DepthLimit);
                    break;

                case KnownUninformedSearch.IterativeDeepening:
                    toReturn = new IterativeDeepeningSearch();
                    break;

                case KnownUninformedSearch.UniformCost:
                    toReturn = new UniformCostSearch(objQueueSearch);
                    break;

                case KnownUninformedSearch.Bidirectional:
                    toReturn = new BidirectionalSearch();
                    break;

                default:
                    toReturn = new BreadthFirstSearch(objQueueSearch);
                    break;
                }
                break;

            default:
                toReturn = CustomSearchAlgorithm.EvaluateTyped(owner, globalVars);
                break;
            }
            return(toReturn);
        }
예제 #4
0
        public static void UninformedAlgorithms()
        {
            Console.WriteLine("Welcome\nARTIFICIAL INTELLIGENCE SEARCH ALGORITHMS");
            ShowOptions();

            var input = Console.ReadLine();
            int res   = 1;

            while (int.TryParse(input, out res) == false)
            {
                ShowOptions();
                input = Console.ReadLine();
            }
            Node <string> startNode = null;
            Node <string> goalNode  = null;

            switch (Convert.ToInt32(input))
            {
            case 1:
            {
                startNode = GetRootNode();
                goalNode  = GetGoalNode();
                Console.WriteLine("BREADTH FIRST SEARCH IMPLEMENTATION------------");
                var bfs = new BreadthFirstSearch <string>(goalNode, startNode);
                bfs.Search();
            }
            break;

            case 2:
            {
                startNode = GetRootNode();
                goalNode  = GetGoalNode();
                Console.WriteLine("DEPTH FIRST SEARCH IMPLEMENTATION------------");
                var dfs = new DepthFirstSearch <string>(goalNode, startNode);
                dfs.Search();
            }
            break;

            case 3:
            {
                startNode = GetRootNode();
                goalNode  = GetGoalNode();
                Console.WriteLine("\nEnd\n");
                Console.WriteLine("ITERATIVE DEEPENING SEARCH IMPLEMENTATION------------");
                var ids = new IterativeDeepingSearch <string>(goalNode, startNode);
                ids.Search();
            }
            break;

            case 4:
            {
                startNode = GetRootNode();
                goalNode  = GetGoalNode();
                Console.WriteLine("DEPTH LIMITED SEARCH IMPLEMENTATION------------");
                Console.Write("Enter limit to search: ");
                var limit = int.Parse(Console.ReadLine());
                var dls   = new DepthLimitedSearch <string>(goalNode, startNode, limit);
                dls.Search();
            }
            break;

            case 5:
            {
                startNode = GetRootNode();
                goalNode  = GetGoalNode();

                Console.WriteLine("Enter the cost for the nodes");
                foreach (var node in Nodes)
                {
                    Console.Write($"Enter cost for node: {node.NodeName}: ");
                    node.Cost = double.Parse(Console.ReadLine());
                }
                Console.WriteLine("UNIFORM COST SEARCH IMPLEMENTATION------------");

                var ucs = new UniformCostSearch <string>(goalNode, startNode);
                ucs.Search();
            }
            break;

            case 6:
            {
                startNode = GetRootNode();
                goalNode  = GetGoalNode();
                Console.WriteLine("BIDIRECTIONAL SEARCH IMPLEMENTATION------------");
                var bidi = new BidirectionalSearch <string>(goalNode, startNode);
                bidi.Search();
            }
            break;

            default:
                ShowOptions();
                break;
            }
        }
예제 #5
0
        /**
         * Creates a search instance.
         *
         * @param strategy
         *            search strategy. See static constants.
         * @param qSearchImpl
         *            queue search implementation: e.g. {@link #TREE_SEARCH}, {@link #GRAPH_SEARCH}
         *
         */
        public ISearchForActions <S, A> createSearch <S, A>(int strategy, int qSearchImpl, IToDoubleFunction <Node <S, A> > h)
        {
            QueueSearch <S, A>       qs     = null;
            ISearchForActions <S, A> result = null;

            switch (qSearchImpl)
            {
            case TREE_SEARCH:
                qs = new TreeSearch <S, A>();
                break;

            case GRAPH_SEARCH:
                qs = new GraphSearch <S, A>();
                break;

            case GRAPH_SEARCH_RED_FRONTIER:
                qs = new GraphSearchReducedFrontier <S, A>();
                break;

            case GRAPH_SEARCH_BFS:
                qs = new GraphSearchBFS <S, A>();
                break;

            case BIDIRECTIONAL_SEARCH:
                qs = new BidirectionalSearch <S, A>();
                break;
            }
            switch (strategy)
            {
            case DF_SEARCH:
                result = new DepthFirstSearch <S, A>(qs);
                break;

            case BF_SEARCH:
                result = new BreadthFirstSearch <S, A>(qs);
                break;

            case ID_SEARCH:
                result = new IterativeDeepeningSearch <S, A>();
                break;

            case UC_SEARCH:
                result = new UniformCostSearch <S, A>(qs);
                break;

            case GBF_SEARCH:
                result = new GreedyBestFirstSearch <S, A>(qs, h);
                break;

            case ASTAR_SEARCH:
                result = new AStarSearch <S, A>(qs, h);
                break;

            case RBF_SEARCH:
                result = new RecursiveBestFirstSearch <S, A>(new AStarSearch <S, A> .EvalFunction(h));
                break;

            case RBF_AL_SEARCH:
                result = new RecursiveBestFirstSearch <S, A>(new AStarSearch <S, A> .EvalFunction(h), true);
                break;

            case HILL_SEARCH:
                result = new HillClimbingSearch <S, A>(h);
                break;
            }
            return(result);
        }
예제 #6
0
        private static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("Invalid number of arguments!");
                Console.WriteLine("Syntax: search <path> <method>");

                return;
            }

            string      path = args[0], method = args[1].ToUpper();
            Environment environment = Environment.Parse(path);

            Console.WriteLine("Environment: ");

            for (int y = 0; y < environment.Rows; y++)
            {
                for (int x = 0; x < environment.Columns; x++)
                {
                    CellState cellState = environment.GetCellAt(x, y).State;

                    if (cellState == CellState.Agent)
                    {
                        Console.Write(">");
                    }
                    else if (cellState == CellState.Goal)
                    {
                        Console.Write("*");
                    }
                    else if (cellState == CellState.Wall)
                    {
                        Console.Write("#");
                    }
                    else
                    {
                        Console.Write("-");
                    }
                }

                Console.WriteLine();
            }

            ISearch search;

            switch (method)
            {
            case "DFS":
                search = new DepthFirstSearch(environment);
                break;

            case "BFS":
                search = new BreadthFirstSearch(environment);
                break;

            case "GBFS":
                search = new GreedyBestFirstSearch(environment);
                break;

            case "AS":
                search = new AStarSearch(environment);
                break;

            case "CUS1":
                search = new UniformCostSearch(environment);
                break;

            case "CUS2":
                search = new BidirectionalAStarSearch(environment);
                break;

            default:
                Console.WriteLine("Invalid method!");

                Console.WriteLine("Methods:");
                Console.WriteLine("- DFS: Depth-first search");
                Console.WriteLine("- BFS: Breadth-first search");
                Console.WriteLine("- GBFS: Greedy best-first search");
                Console.WriteLine("- AS: A* search");
                Console.WriteLine("- CUS1: Uniform-cost search");
                Console.WriteLine("- CUS2: Bidirectional (A*) search");

                return;
            }

            List <Cell> solution = search.Search();

            if (solution == null)
            {
                Console.WriteLine("Unable to find a solution!");
                return;
            }

            Console.WriteLine($"Solution (using {search.Name}): ");

            for (int y = 0; y < environment.Rows; y++)
            {
                for (int x = 0; x < environment.Columns; x++)
                {
                    Cell cell = environment.GetCellAt(x, y);

                    if (solution.Contains(cell) && cell.State != CellState.Agent && cell.State != CellState.Goal)
                    {
                        Console.Write("+");
                    }
                    else if (cell.State == CellState.Agent)
                    {
                        Console.Write(">");
                    }
                    else if (cell.State == CellState.Goal)
                    {
                        Console.Write("*");
                    }
                    else if (cell.State == CellState.Wall)
                    {
                        Console.Write("#");
                    }
                    else
                    {
                        Console.Write("-");
                    }
                }

                Console.WriteLine();
            }

            Cell lastCell = solution[0];

            for (int i = 1; i < solution.Count; i++)
            {
                Cell cell = solution[i];

                if (cell.Y < lastCell.Y)
                {
                    Console.Write("Up");
                }
                else if (cell.X < lastCell.X)
                {
                    Console.Write("Left");
                }
                else if (cell.Y > lastCell.Y)
                {
                    Console.Write("Down");
                }
                else if (cell.X > lastCell.X)
                {
                    Console.Write("Right");
                }

                if (i == solution.Count - 1)
                {
                    Console.WriteLine();
                }
                else
                {
                    Console.Write(", ");
                }

                lastCell = cell;
            }
        }