Example #1
0
 public override bool Equals(object o)
 {
     if (this == o)
     {
         return(true);
     }
     if (o != null && GetType() == o.GetType())
     {
         NQueensBoard aBoard = (NQueensBoard)o;
         if (aBoard.getQueenPositions().Size() != getQueenPositions().Size())
         {
             return(false);
         }
         for (int i = 0; i < getSize(); ++i)
         {
             for (int j = 0; j < getSize(); j++)
             {
                 if (queenExistsAt(i, j) != aBoard.queenExistsAt(i, j))
                 {
                     return(false);
                 }
             }
         }
         return(true);
     }
     return(false);
 }
Example #2
0
        public static NQueensBoard getBoardForIndividual(Individual <int> individual)
        {
            int          boardSize = individual.length();
            NQueensBoard board     = new NQueensBoard(boardSize);

            for (int i = 0; i < boardSize; ++i)
            {
                int pos = individual.getRepresentation().Get(i);
                board.addQueenAt(new XYLocation(i, pos));
            }
            return(board);
        }
Example #3
0
            public double apply(Individual <int> 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).
                ICollection <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);
            }
Example #4
0
 /**
  * Implements a GOAL-TEST for the n-queens problem.
  */
 public static bool testGoal(NQueensBoard state)
 {
     return(state.getNumberOfQueensOnBoard() == state.getSize() && state.getNumberOfAttackingPairs() == 0);
 }