Download
# Licenced under CC-BY-4.0 : http://creativecommons.org/licenses/by/4.0/
MAIN MODEL Main_NQueens

  DECLARATION SECTION 

    PARAMETER:
       identifier   :  N
       initial data :  8 ;

    SET:
       identifier   :  ii
       subset of    :  Integers
       indices      :  i, j
       definition   :  ElementRange(1, N) ;

    VARIABLE:
       identifier   :  x
       index domain :  (i)
       range        :  {1..N} ;

    CONSTRAINT:
       identifier   :  C1
       index domain :  (i,j)
       definition   :  (i < j) and x(i) <> x(j)   ;

    CONSTRAINT:
       identifier   :  C2
       index domain :  (i,j)
       definition   :  (i < j) and x(i) + i <> x(j) + j ;

    CONSTRAINT:
       identifier   :  C3
       index domain :  (i,j)
       definition   :  (i < j) and x(i) - i <> x(j) - j ;

    CONSTRAINT:
       identifier   :  AllDiff
       definition   :  cp::AllDifferent(i, x(i)) ;

    MATHEMATICAL PROGRAM:
       identifier   :  NQueensPlan
       direction    :  minimize
       constraints  :  AllConstraints
       variables    :  AllVariables
       type         :  CSP ;

  ENDSECTION  ;

  PROCEDURE
    identifier :  MainInitialization

  ENDPROCEDURE  ;

  PROCEDURE
    identifier :  MainExecution
    body       :  
      ShowProgressWindow ;
      solve NQueensPlan;
      
      if (NQueensPlan.ProgramStatus <> 'Optimal') then
          empty x;
      endif;
      DialogMessage(GMP::Solution::Count('NQueensPlan'));

  ENDPROCEDURE  ;

  PROCEDURE
    identifier :  MainTermination
    body       :  
      if ( CaseSaveAll( confirm:2 ) = 1 ) then
          return 1;
      else
          return 0;
      endif ;

  ENDPROCEDURE  ;

ENDMODEL Main_NQueens ;