PBeFS
Parallel Best-First Search
Loading...
Searching...
No Matches
SequenceScore.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File SequenceScore.h --------------------------*/
3/*--------------------------------------------------------------------------*/
15/*--------------------------------------------------------------------------*/
16/*------------------------------ DEFINITIONS -------------------------------*/
17/*--------------------------------------------------------------------------*/
18
19#ifndef __PBeFS_SequenceScore
20 #define __PBeFS_SequenceScore
21 /* self-identification: #endif at the end of the file */
22
23/*--------------------------------------------------------------------------*/
24/*-------------------------------- INCLUDES --------------------------------*/
25/*--------------------------------------------------------------------------*/
26
27#include <cstdlib>
28
30
31/*--------------------------------------------------------------------------*/
32/*------------------------------- NAMESPACE --------------------------------*/
33/*--------------------------------------------------------------------------*/
34
36namespace PBeFS_di_unipi_it
37{
38
39/*--------------------------------------------------------------------------*/
40/*-------------------------- CLASS SequenceScore ---------------------------*/
41/*--------------------------------------------------------------------------*/
43
52
53template< int W , int H >
55
56/*------------------------ PUBLIC PART OF THE CLASS ------------------------*/
57
58 public:
59
61 using state_type = typename Base::state_type;
62 using cost_type = typename Base::cost_type;
63 using board_type = typename Base::board_type;
64
65 static constexpr int cells = Base::cells;
66
68
69 explicit SequenceScore( const board_type & start ,
70 const board_type & goal = Base::korf_goal() )
71 : f_base( start , goal ) , v_goal( goal ) {
72 for( int c = 0 ; c < cells ; ++c )
73 v_goal_cell[ goal[ c ] ] = c;
74 if constexpr( ( W == 3 ) && ( H == 3 ) ) {
75 const int ring[ 8 ] = { 0 , 1 , 2 , 5 , 8 , 7 , 6 , 3 };
76 for( int k = 0 ; k < 8 ; ++k )
77 v_next[ ring[ k ] ] = ring[ ( k + 1 ) % 8 ];
78 v_next[ 4 ] = -1;
79 }
80 else {
81 for( int c = 0 ; c < cells - 1 ; ++c )
82 v_next[ c ] = c + 1;
83 v_next[ cells - 1 ] = -1;
84 }
85 // the tile that follows t: the next of its goal cell, wrapping around to
86 // the first tile of the sequence after the last one
87 for( int t = 1 ; t < cells ; ++t ) {
88 const int c = v_goal_cell[ t ];
89 int n = v_next[ c ];
90 if( n < 0 )
91 n = ( W == 3 ) && ( H == 3 ) ? 0 : 1;
92 v_follower[ t ] = goal[ n ];
93 }
94 }
95
96 [[nodiscard]] state_type initial_state( void ) const noexcept {
97 return( f_base.initial_state() );
98 }
99
100 [[nodiscard]] bool is_goal( state_type s ) const noexcept {
101 return( f_base.is_goal( s ) );
102 }
103
104 [[nodiscard]] std::uint64_t hash( state_type s ) const noexcept {
105 return( f_base.hash( s ) );
106 }
107
109
110 [[nodiscard]] cost_type heuristic( state_type s ) const noexcept {
111 const int centre = ( W == 3 ) && ( H == 3 ) ? 4 : 0;
112 int m = 0 , sc = Base::tile( s , centre ) != v_goal[ centre ] ? 1 : 0;
113 for( int c = 0 ; c < cells ; ++c ) {
114 const int t = Base::tile( s , c );
115 if( t == 0 )
116 continue;
117 const int g = v_goal_cell[ t ];
118 m += std::abs( g % W - c % W ) + std::abs( g / W - c / W );
119 if( ( v_next[ c ] >= 0 ) &&
120 ( Base::tile( s , v_next[ c ] ) != v_follower[ t ] ) )
121 sc += 2;
122 }
123 return( m + W * sc );
124 }
125
126 template< class F >
127 void for_each_successor( state_type s , cost_type , F && f ) const {
128 f_base.for_each_successor( s , 0 , [ & ]( state_type t , cost_type c ,
129 cost_type ) {
130 f( t , c , heuristic( t ) );
131 } );
132 }
133
135 [[nodiscard]] const Base & base( void ) const noexcept { return( f_base ); }
136
137/*----------------------- PRIVATE PART OF THE CLASS ------------------------*/
138
139 private:
140
141 Base f_base;
142 board_type v_goal;
143 int v_goal_cell[ cells ] = {};
144 int v_next[ cells ] = {};
145 int v_follower[ cells ] = {};
146
147 }; // end( class( SequenceScore ) )
148
149/*--------------------------------------------------------------------------*/
150
151} // end( namespace PBeFS_di_unipi_it )
152
153/*--------------------------------------------------------------------------*/
154
155#endif /* __PBeFS_SequenceScore */
156
157/*--------------------------------------------------------------------------*/
158/*------------------------ End File SequenceScore.h ------------------------*/
159/*--------------------------------------------------------------------------*/
Header file for the class template SlidingTile, which implements the Domain concept [see Domain....
state_type initial_state(void) const noexcept
Definition SequenceScore.h:96
std::uint64_t hash(state_type s) const noexcept
Definition SequenceScore.h:104
static constexpr int cells
the cells
Definition SequenceScore.h:65
cost_type heuristic(state_type s) const noexcept
M + W S.
Definition SequenceScore.h:110
bool is_goal(state_type s) const noexcept
Definition SequenceScore.h:100
typename Base::cost_type cost_type
the cost type
Definition SequenceScore.h:62
const Base & base(void) const noexcept
the underlying puzzle
Definition SequenceScore.h:135
SequenceScore(const board_type &start, const board_type &goal=Base::korf_goal())
constructor, taking the start and the goal boards
Definition SequenceScore.h:69
void for_each_successor(state_type s, cost_type, F &&f) const
Definition SequenceScore.h:127
typename Base::board_type board_type
a board
Definition SequenceScore.h:63
typename Base::state_type state_type
a packed board
Definition SequenceScore.h:61
SlidingTile< W, H > Base
the puzzle
Definition SequenceScore.h:60
the sliding-tile puzzle on a board of at most 25 cells
Definition SlidingTile.h:170
std::conditional_t< cells<=16, std::uint64_t, Word128 > state_type
a packed board
Definition SlidingTile.h:187
static constexpr int cells
the number of cells
Definition SlidingTile.h:180
static board_type korf_goal(void)
the goal of Korf (1985): the blank in the first cell, then 1 , 2 , ...
Definition SlidingTile.h:196
std::array< std::uint8_t, cells > board_type
a board
Definition SlidingTile.h:190
int cost_type
the cost type
Definition SlidingTile.h:189
static int tile(state_type s, int c) noexcept
the tile in the cell c of s
Definition SlidingTile.h:456
namespace for the Parallel Best-First Search library
Definition AStar.h:83