PBeFS
Parallel Best-First Search
Loading...
Searching...
No Matches
NQueens.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*----------------------------- File NQueens.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
16/*--------------------------------------------------------------------------*/
17/*------------------------------ DEFINITIONS -------------------------------*/
18/*--------------------------------------------------------------------------*/
19
20#ifndef __PBeFS_NQueens
21 #define __PBeFS_NQueens
22 /* self-identification: #endif at the end of the file */
23
24/*--------------------------------------------------------------------------*/
25/*-------------------------------- INCLUDES --------------------------------*/
26/*--------------------------------------------------------------------------*/
27
28#include <array>
29
30#include <cstddef>
31
32#include <cstdint>
33
34#include <cstdlib>
35
36#include <cstring>
37
38#include <stdexcept>
39
40#include "PBeFS/astar/Domain.h"
41
42/*--------------------------------------------------------------------------*/
43/*------------------------------- NAMESPACE --------------------------------*/
44/*--------------------------------------------------------------------------*/
45
47namespace PBeFS_di_unipi_it
48{
49
50/*--------------------------------------------------------------------------*/
51/*----------------------------- CLASS NQueens ------------------------------*/
52/*--------------------------------------------------------------------------*/
54
66
67class NQueens {
68
69/*------------------------ PUBLIC PART OF THE CLASS ------------------------*/
70
71 public:
72
73/*------------------------------ PUBLIC TYPES ------------------------------*/
74
76 struct Board {
77 std::array< std::uint8_t , 32 > row;
78 std::uint8_t k;
79
80 bool operator==( const Board & ) const = default;
81 };
82
83 using state_type = Board;
84 using cost_type = int;
85
86/*------------------------------ CONSTRUCTOR -------------------------------*/
87
90
91 explicit NQueens( int n ) : f_n( n ) {
92 if( ( n < 1 ) || ( n > 32 ) )
93 throw( std::invalid_argument( "NQueens::NQueens: n is 1 to 32" ) );
94 }
95
96/*------------------------- METHODS OF THE DOMAIN --------------------------*/
97
98 [[nodiscard]] Board initial_state( void ) const noexcept {
99 return( Board{ {} , 0 } );
100 }
101
102 [[nodiscard]] bool is_goal( const Board & s ) const noexcept {
103 return( s.k == f_n );
104 }
105
106 [[nodiscard]] std::uint64_t hash( const Board & s ) const noexcept {
107 std::uint64_t w[ 4 ];
108 std::memcpy( w , s.row.data() , sizeof( w ) );
109 std::uint64_t z = ADS::splitmix64( s.k );
110 for( std::uint64_t x : w )
111 z = ADS::splitmix64( z ^ x );
112 return( z );
113 }
114
115 [[nodiscard]] int heuristic( const Board & s ) const noexcept {
116 return( f_n - s.k );
117 }
118
119/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
122
123 template< class F >
124 void for_each_successor( const Board & s , int h , F && f ) const {
125 const int k = s.k;
126 for( int r = 0 ; r < f_n ; ++r ) {
127 bool free = true;
128 for( int c = 0 ; free && ( c < k ) ; ++c ) {
129 const int rc = s.row[ c ];
130 free = ( rc != r ) && ( std::abs( rc - r ) != k - c );
131 }
132 if( free ) {
133 Board t = s;
134 t.row[ k ] = std::uint8_t( r );
135 t.k = std::uint8_t( k + 1 );
136 f( t , 1 , h - 1 );
137 }
138 }
139 }
140
141/*---------------------- FEATURES FOR ZOBRIST HASHING ----------------------*/
143
144 [[nodiscard]] std::size_t num_features( void ) const noexcept {
145 return( std::size_t( f_n ) * f_n );
146 }
147
150
151 template< class F >
152 void for_each_feature( const Board & s , F && f ) const {
153 for( int c = 0 ; c < s.k ; ++c )
154 f( std::size_t( c ) * f_n + s.row[ c ] );
155 }
156
157/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
159
160 [[nodiscard]] int size( void ) const noexcept { return( f_n ); }
161
162/*----------------------- PRIVATE PART OF THE CLASS ------------------------*/
163
164 private:
165
166 int f_n;
167
168 }; // end( class( NQueens ) )
169
170/*--------------------------------------------------------------------------*/
171
172} // end( namespace PBeFS_di_unipi_it )
173
174/*--------------------------------------------------------------------------*/
175
176#endif /* __PBeFS_NQueens */
177
178/*--------------------------------------------------------------------------*/
179/*--------------------------- End File NQueens.h ---------------------------*/
180/*--------------------------------------------------------------------------*/
Header file for the concepts a search problem has to satisfy in order to be searched by the algorithm...
void for_each_successor(const Board &s, int h, F &&f) const
calls f( t , 1 , h - 1 ) for each row of the column free of attacks
Definition NQueens.h:124
bool is_goal(const Board &s) const noexcept
Definition NQueens.h:102
std::uint64_t hash(const Board &s) const noexcept
Definition NQueens.h:106
int cost_type
the cost type
Definition NQueens.h:84
NQueens(int n)
constructor of the problem with queens,
Definition NQueens.h:91
std::size_t num_features(void) const noexcept
the number of features: one per pair ( column , row )
Definition NQueens.h:144
int heuristic(const Board &s) const noexcept
Definition NQueens.h:115
int size(void) const noexcept
the size of the board
Definition NQueens.h:160
Board state_type
a board
Definition NQueens.h:83
void for_each_feature(const Board &s, F &&f) const
calls f( c n + r ) for the queen of each column , in the row
Definition NQueens.h:152
Board initial_state(void) const noexcept
Definition NQueens.h:98
namespace for the Parallel Best-First Search library
Definition AStar.h:83
a board: the queens placed and the row of each of them
Definition NQueens.h:76
std::uint8_t k
the queens on the board
Definition NQueens.h:78
bool operator==(const Board &) const =default
std::array< std::uint8_t, 32 > row
the row of each column
Definition NQueens.h:77