Beispiel #1
0
        static void Main(string[] args)
        {
            Log log = new Log();

            State InitialState = new State("initial");

            log.WriteLine("Initial State created...\n");

            State GoalState = new State("goal");

            log.WriteLine("Goal State created...\n\n");


            Frontier  frontier  = new Frontier();
            ClosedSet closedSet = new ClosedSet();

            State currentState = null;

            frontier.Add(InitialState);
            int step = 1;

            while (!frontier.isEmpty())
            {
                log.Write("Step " + step + ": ");
                step++;

                // Log frontier and closed set
                log.Write(frontier.printFrontier() + " | " + closedSet.printClosedSet() + " | ");

                currentState = frontier.Remove();

                // Log current state
                log.Write(currentState.getID().ToString());

                if (closedSet.contains(currentState))
                {
                    // Log '-' for the children list if the closed set contains current state
                    log.WriteLine(" | - ");
                    continue;
                }
                else if (currentState.equals(GoalState))
                {
                    log.Write(" | GOAL STATE");
                    log.WriteLine("\nSolution found!");
                    break;
                }
                string printChildren = " | ";
                foreach (State expandingState in currentState.expandState(closedSet))
                {
                    printChildren += expandingState.getID() + ", ";
                    frontier.Add(expandingState);
                }
                log.WriteLine(printChildren.TrimEnd().TrimEnd(','));
                closedSet.add(currentState);
            }

            Console.WriteLine("Program executed successfully. Check log for results...");
            Console.Read();
        }
Beispiel #2
0
        public List <State> expandState(ClosedSet closedSet)
        {
            List <State> returnList = new List <State>();
            State        tempState;

            if (boatIs == LEFT)
            {
                // First action
                tempState = new State(riverBankLeft.getCannibals() - 2, riverBankLeft.getMissionaries() - 0, riverBankRight.getCannibals() + 2, riverBankRight.getMissionaries() + 0, boatIs);
                if (tempState.isValidState())
                {
                    if (closedSet.contains_get_id(tempState) != -1)
                    {
                        tempState.id = closedSet.contains_get_id(tempState);
                        State.idCount--;
                    }
                    returnList.Add(tempState);
                }
                else
                {
                    State.idCount--;
                }
                // Second action
                tempState = new State(riverBankLeft.getCannibals() - 0, riverBankLeft.getMissionaries() - 2, riverBankRight.getCannibals() + 0, riverBankRight.getMissionaries() + 2, boatIs);
                if (tempState.isValidState())
                {
                    if (closedSet.contains_get_id(tempState) != -1)
                    {
                        tempState.id = closedSet.contains_get_id(tempState);
                        State.idCount--;
                    }
                    returnList.Add(tempState);
                }
                else
                {
                    State.idCount--;
                }
                // Third action
                tempState = new State(riverBankLeft.getCannibals() - 1, riverBankLeft.getMissionaries() - 1, riverBankRight.getCannibals() + 1, riverBankRight.getMissionaries() + 1, boatIs);
                if (tempState.isValidState())
                {
                    if (closedSet.contains_get_id(tempState) != -1)
                    {
                        tempState.id = closedSet.contains_get_id(tempState);
                        State.idCount--;
                    }
                    returnList.Add(tempState);
                }
                else
                {
                    State.idCount--;
                }
                // Fourth action
                tempState = new State(riverBankLeft.getCannibals() - 1, riverBankLeft.getMissionaries() - 0, riverBankRight.getCannibals() + 1, riverBankRight.getMissionaries() + 0, boatIs);
                if (tempState.isValidState())
                {
                    if (closedSet.contains_get_id(tempState) != -1)
                    {
                        tempState.id = closedSet.contains_get_id(tempState);
                        State.idCount--;
                    }
                    returnList.Add(tempState);
                }
                else
                {
                    State.idCount--;
                }
                // Fifth action
                tempState = new State(riverBankLeft.getCannibals() - 0, riverBankLeft.getMissionaries() - 1, riverBankRight.getCannibals() + 0, riverBankRight.getMissionaries() + 1, boatIs);
                if (tempState.isValidState())
                {
                    if (closedSet.contains_get_id(tempState) != -1)
                    {
                        tempState.id = closedSet.contains_get_id(tempState);
                        State.idCount--;
                    }
                    returnList.Add(tempState);
                }
                else
                {
                    State.idCount--;
                }
            }
            else if (boatIs == RIGHT)
            {
                // First action
                tempState = new State(riverBankLeft.getCannibals() + 2, riverBankLeft.getMissionaries() + 0, riverBankRight.getCannibals() - 2, riverBankRight.getMissionaries() - 0, boatIs);
                if (tempState.isValidState())
                {
                    if (closedSet.contains_get_id(tempState) != -1)
                    {
                        tempState.id = closedSet.contains_get_id(tempState);
                        State.idCount--;
                    }
                    returnList.Add(tempState);
                }
                else
                {
                    State.idCount--;
                }
                // Second action
                tempState = new State(riverBankLeft.getCannibals() + 0, riverBankLeft.getMissionaries() + 2, riverBankRight.getCannibals() - 0, riverBankRight.getMissionaries() - 2, boatIs);
                if (tempState.isValidState())
                {
                    if (closedSet.contains_get_id(tempState) != -1)
                    {
                        tempState.id = closedSet.contains_get_id(tempState);
                        State.idCount--;
                    }
                    returnList.Add(tempState);
                }
                else
                {
                    State.idCount--;
                }
                // Third action
                tempState = new State(riverBankLeft.getCannibals() + 1, riverBankLeft.getMissionaries() + 1, riverBankRight.getCannibals() - 1, riverBankRight.getMissionaries() - 1, boatIs);
                if (tempState.isValidState())
                {
                    if (closedSet.contains_get_id(tempState) != -1)
                    {
                        tempState.id = closedSet.contains_get_id(tempState);
                        State.idCount--;
                    }
                    returnList.Add(tempState);
                }
                else
                {
                    State.idCount--;
                }
                // Fourth action
                tempState = new State(riverBankLeft.getCannibals() + 1, riverBankLeft.getMissionaries() + 0, riverBankRight.getCannibals() - 1, riverBankRight.getMissionaries() - 0, boatIs);
                if (tempState.isValidState())
                {
                    if (closedSet.contains_get_id(tempState) != -1)
                    {
                        tempState.id = closedSet.contains_get_id(tempState);
                        State.idCount--;
                    }
                    returnList.Add(tempState);
                }
                else
                {
                    State.idCount--;
                }
                // Fifth action
                tempState = new State(riverBankLeft.getCannibals() + 0, riverBankLeft.getMissionaries() + 1, riverBankRight.getCannibals() - 0, riverBankRight.getMissionaries() - 1, boatIs);
                if (tempState.isValidState())
                {
                    if (closedSet.contains_get_id(tempState) != -1)
                    {
                        tempState.id = closedSet.contains_get_id(tempState);
                        State.idCount--;
                    }
                    returnList.Add(tempState);
                }
                else
                {
                    State.idCount--;
                }
            }

            return(returnList);
        }