The first step is to implement a simulator for the domain, in our case the Tower of Hanoi puzzle. We start by using the simulator_template.cpp and simulator_template.hpp files under the examples/templates subdir, and copying them as simulator.cpp and simulator.hpp to the examples/hanoi subdir.
All template files contains the string $domain$ to represent the domain namespace. We choose hanoi as the namespace for our domain. So we change the newly created simulator.hpp and simulator.cpp files to replace all occurrences of $domain$ to hanoi.
In Plansim, the simulator concerns are to represent the problem state and to define all applicable actions for this state. We choose to represent each tower by a STL list, with the lists being stored in an STL array. The simulator constructor receives the total number of towers and the total number of disks, which are always placed in the leftmost tower, represented by the lower index in the array. The final simulator constructor looks like:
simulator::simulator( unsigned int number_of_towers, unsigned int number_of_disks ) : total_number_of_disks_( number_of_disks ) { // Reserves the total number of towers towers_.resize( number_of_towers ); // Fills the initial tower, disks are represented by numbers, // with 0 being the larger disk for( unsigned int i = 0; i < number_of_disks; i++ ) { towers_[ 0 ].push_back( i ); } }
The tower types are defined in simulator.hpp:
// Towers are represented by unsigned int lists typedef std::list<unsigned int> tower_type; // Vector with all the towers std::vector<tower_type> towers_;
The towers_ attribute defines the simulator state. We now must provide a way to serialize this state, which is accomplished with the aid of Boost's serialization library. The serialization must be implemented by the serialize method in simulator.hpp, and the final code looks like:
/// Template to generate serialization, for input and output template<class Archive> void serialize( Archive& ar, const unsigned int version ) { // Serialize state here ar & towers_; }
This is quite direct since the serialization library includes support for STL containers. In order to use them, simply include the appropriate headers in simulator.hpp:
// Uncomment this include if the simulator object contains STL // lists that are serialized. #include <boost/serialization/vector.hpp> #include <boost/serialization/list.hpp>
We are not done with the simulator yet, we will return to this class later when the actions are defined.