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

  DECLARATION SECTION 

    PARAMETER:
       identifier   :  n
       initial data :  1000 ;

    SET:
       identifier   :  S
       subset of    :  Integers
       index        :  i
       definition   :  {1..n} ;

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

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

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

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

    PARAMETER:
       identifier   :  solution_storage_limit
       initial data :  1
       comment      :  "Number of solutions to store." ;

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

  ENDSECTION  ;

  PROCEDURE
    identifier :  MainInitialization

  ENDPROCEDURE  ;

  PROCEDURE
    identifier :  MainExecution
    body       :  
      ShowProgressWindow;
      
      /*
      ! Limit the number of stored solutions for n > 10
      if (n <= 10) then
        OptionSetValue( "CP Optimizer 12.4::solution_storage_limit", 10000 );
      else
        OptionSetValue( "CP Optimizer 12.4::solution_storage_limit", 1 );
      endif;
      */
      if (solution_storage_limit > 1) then
         OptionSetValue( "CP Optimizer 12.4::solution_storage_limit", solution_storage_limit);
      else
         OptionSetValue( "CP Optimizer 12.4::solution_storage_limit", 1);
      endif;
      
      solve NQueens2Plan;
      
      if (NQueens2Plan.ProgramStatus <> 'Optimal') then
        empty x
      endif;
      
      /*
      if (n <= 10) then
        DialogMessage("It was " + GMP::Solution::Count('NQueens2Plan') + " solution(s).");
      else
       DialogMessage("It was " + GMP::Solution::Count('NQueens2Plan') + " solution.");
      endif
      */
        DialogMessage("It was " + GMP::Solution::Count('NQueens2Plan') + " solution(s).");

  ENDPROCEDURE  ;

  PROCEDURE
    identifier :  MainTermination
    body       :  
      return DataManagementExit();

  ENDPROCEDURE  ;

ENDMODEL Main_NQueens2 ;