コード例 #1
0
ファイル: NQueensGoalTest.cs プロジェクト: claudiu04/AIMA.Net
        public bool isGoalState(Object state)
        {
            NQueensBoard board = (NQueensBoard)state;

            return(board.getNumberOfQueensOnBoard() == board.getSize() &&
                   board.getNumberOfAttackingPairs() == 0);
        }
コード例 #2
0
 public Object result(Object s, Action a)
 {
     if (a is QueenAction)
     {
         QueenAction  qa       = (QueenAction)a;
         NQueensBoard board    = (NQueensBoard)s;
         NQueensBoard newBoard = new NQueensBoard(board.getSize());
         newBoard.setBoard(board.getQueenPositions());
         if (qa.getName() == QueenAction.PLACE_QUEEN)
         {
             newBoard.AddQueenAt(qa.getLocation());
         }
         else if (qa.getName() == QueenAction.REMOVE_QUEEN)
         {
             newBoard.removeQueenFrom(qa.getLocation());
         }
         else if (qa.getName() == QueenAction.MOVE_QUEEN)
         {
             newBoard.moveQueenTo(qa.getLocation());
         }
         s = newBoard;
     }
     // if action is not understood or is a NoOp
     // the result will be the current state.
     return(s);
 }
コード例 #3
0
        //
        // START - Interface FitnessFunction
        public double getValue(String individual)
        {
            double fitness = 0;

            NQueensBoard board     = getBoardForIndividual(individual);
            int          boardSize = board.getSize();

            // Calculate the number of non-attacking pairs of queens (refer to AIMA
            // page 117).
            List <XYLocation> qPositions = board.getQueenPositions();

            for (int fromX = 0; fromX < (boardSize - 1); fromX++)
            {
                for (int toX = fromX + 1; toX < boardSize; toX++)
                {
                    int  fromY            = qPositions.get(fromX).getYCoOrdinate();
                    bool nonAttackingPair = true;
                    // Check right beside
                    int toY = fromY;
                    if (board.queenExistsAt(new XYLocation(toX, toY)))
                    {
                        nonAttackingPair = false;
                    }
                    // Check right and above
                    toY = fromY - (toX - fromX);
                    if (toY >= 0)
                    {
                        if (board.queenExistsAt(new XYLocation(toX, toY)))
                        {
                            nonAttackingPair = false;
                        }
                    }
                    // Check right and below
                    toY = fromY + (toX - fromX);
                    if (toY < boardSize)
                    {
                        if (board.queenExistsAt(new XYLocation(toX, toY)))
                        {
                            nonAttackingPair = false;
                        }
                    }

                    if (nonAttackingPair)
                    {
                        fitness += 1.0;
                    }
                }
            }

            return(fitness);
        }
コード例 #4
0
        // END - Interface GoalTest
        //

        public NQueensBoard getBoardForIndividual(String individual)
        {
            int          boardSize = individual.length();
            NQueensBoard board     = new NQueensBoard(boardSize);

            for (int i = 0; i < boardSize; i++)
            {
                int pos = Character
                          .digit(individual.charAt(i), individual.length());
                board.AddQueenAt(new XYLocation(i, pos));
            }

            return(board);
        }
コード例 #5
0
            public HashSet <Action> actions(Object state)
            {
                HashSet <Action> actions = new LinkedHashSet <Action>();
                NQueensBoard     board   = (NQueensBoard)state;

                for (int i = 0; i < board.getSize(); i++)
                {
                    for (int j = 0; j < board.getSize(); j++)
                    {
                        XYLocation loc = new XYLocation(i, j);
                        if (!board.queenExistsAt(loc))
                        {
                            actions
                            .Add(new QueenAction(QueenAction.MOVE_QUEEN,
                                                 loc));
                        }
                    }
                }
                return(actions);
            }
コード例 #6
0
            public HashSet <Action> actions(Object state)
            {
                NQueensBoard board = (NQueensBoard)state;

                HashSet <Action> actions = new LinkedHashSet <Action>();

                int numQueens = board.getNumberOfQueensOnBoard();
                int boardSize = board.getSize();

                for (int i = 0; i < boardSize; i++)
                {
                    XYLocation newLocation = new XYLocation(numQueens, i);
                    if (!(board.isSquareUnderAttack(newLocation)))
                    {
                        actions.Add(new QueenAction(QueenAction.PLACE_QUEEN,
                                                    newLocation));
                    }
                }

                return(actions);
            }
コード例 #7
0
ファイル: NQueensBoard.cs プロジェクト: claudiu04/AIMA.Net
        public override bool Equals(Object o)
        {
            if (this == o)
            {
                return(true);
            }
            if ((o == null) || (this.getClass() != o.getClass()))
            {
                return(false);
            }
            NQueensBoard      aBoard = (NQueensBoard)o;
            bool              retVal = true;
            List <XYLocation> locs   = getQueenPositions();

            foreach (XYLocation loc in locs)
            {
                if (!(aBoard.queenExistsAt(loc)))
                {
                    retVal = false;
                }
            }
            return(retVal);
        }
コード例 #8
0
        // END - Interface GoalTest
        //

        public NQueensBoard getBoardForIndividual(String individual)
        {
            int boardSize = individual.length();
            NQueensBoard board = new NQueensBoard(boardSize);
            for (int i = 0; i < boardSize; i++)
            {
                int pos = Character
                        .digit(individual.charAt(i), individual.length());
                board.AddQueenAt(new XYLocation(i, pos));
            }

            return board;
        }
コード例 #9
0
        public double h(Object state)
        {
            NQueensBoard board = (NQueensBoard)state;

            return(board.getNumberOfAttackingPairs());
        }
コード例 #10
0
 public Object result(Object s, Action a)
 {
     if (a is QueenAction)
     {
         QueenAction qa = (QueenAction)a;
         NQueensBoard board = (NQueensBoard)s;
         NQueensBoard newBoard = new NQueensBoard(board.getSize());
         newBoard.setBoard(board.getQueenPositions());
         if (qa.getName() == QueenAction.PLACE_QUEEN)
             newBoard.AddQueenAt(qa.getLocation());
         else if (qa.getName() == QueenAction.REMOVE_QUEEN)
             newBoard.removeQueenFrom(qa.getLocation());
         else if (qa.getName() == QueenAction.MOVE_QUEEN)
             newBoard.moveQueenTo(qa.getLocation());
         s = newBoard;
     }
     // if action is not understood or is a NoOp
     // the result will be the current state.
     return s;
 }