The solve_template.cpp file under the examples/templates directory serves as basis for the examples/hanoi/hanoi_test.cpp file. This file creates a Tower of Hanoi instance and solves it through breadth first search.
First we replace all occurrences of $domain$ by 'hanoi'. Then we define a helper function that creates Tower of Hanoi instances, receiving as arguments the total number of towers and disks:
// Function to create instances plansim::problem_instance::shared_ptr create_instance( unsigned int towers, unsigned int disks ) { // Creates a simulator simulator::shared_ptr s( new simulator( towers, disks ) ); // Creates the evaluation and goal list std::list<plansim::goal::const_ptr> goal_list; plansim::evaluation::const_ptr e( new disks_in_place() ); plansim::goal::const_ptr g1( new all_disks_in_rightmost_tower( s, e ) ); goal_list.push_back( g1 ); // Creates the search nodes manager plansim::search_nodes_manager::shared_ptr snm( new plansim::search_nodes_manager( 1024 ) ); // Creates the instance plansim::problem_instance::shared_ptr pi( new plansim::problem_instance( s, goal_list, snm ) ); return pi; }
We also must include the headers for the search strategies, goals and evaluations that will be used, in our case:
// Include here all the search algorithms that will be used #include <plansim/breadth_first_search.hpp> // Include the domain specific goals, for instance: #include "all_disks_in_rightmost_tower.hpp" // Include the domain specific evaluations, for instance: #include "disks_in_place.hpp"
The bfs_test function is called by the Boost' unit test framework to solve the puzzle with 3 towers and 2 disks, 3 tower and 4 disks and 3 towers and 6 disks:
void bfs_test( void ) { try { plansim::problem_instance::shared_ptr pi( create_instance( 3, 2 ) ); plansim::breadth_first_search bfs; plansim::plan p; plansim::search_algorithm::solve_return_code rc; rc = bfs.solve( pi, p, 10., numeric_limits<unsigned int>::max() ); BOOST_CHECK_EQUAL( rc, 0 ); BOOST_CHECK_EQUAL( p.get_cost(), 3 ); std::cerr << p << std::endl; pi = create_instance( 3, 4 ); rc = bfs.solve( pi, p, 16., numeric_limits<unsigned int>::max() ); BOOST_CHECK_EQUAL( rc, 0 ); BOOST_CHECK_EQUAL( p.get_cost(), 15 ); std::cerr << p << std::endl; pi = create_instance( 3, 6 ); rc = bfs.solve( pi, p, numeric_limits<unsigned int>::max(), numeric_limits<unsigned int>::max() ); BOOST_CHECK_EQUAL( rc, 0 ); BOOST_CHECK_EQUAL( p.get_cost(), 63 ); std::cerr << p << std::endl; } catch(...) { BOOST_ERROR( "Unexpected exception" ); throw; } }
The resulting plans are:
1: [ Move disk 1 from tower 0 to tower 1 ], C: 1 2: [ Move disk 0 from tower 0 to tower 2 ], C: 2 3: [ Move disk 1 from tower 1 to tower 2 ], C: 3 1: [ Move disk 3 from tower 0 to tower 1 ], C: 1 2: [ Move disk 2 from tower 0 to tower 2 ], C: 2 3: [ Move disk 3 from tower 1 to tower 2 ], C: 3 4: [ Move disk 1 from tower 0 to tower 1 ], C: 4 5: [ Move disk 3 from tower 2 to tower 0 ], C: 5 6: [ Move disk 2 from tower 2 to tower 1 ], C: 6 7: [ Move disk 3 from tower 0 to tower 1 ], C: 7 8: [ Move disk 0 from tower 0 to tower 2 ], C: 8 9: [ Move disk 3 from tower 1 to tower 2 ], C: 9 10: [ Move disk 2 from tower 1 to tower 0 ], C: 10 11: [ Move disk 3 from tower 2 to tower 0 ], C: 11 12: [ Move disk 1 from tower 1 to tower 2 ], C: 12 13: [ Move disk 3 from tower 0 to tower 1 ], C: 13 14: [ Move disk 2 from tower 0 to tower 2 ], C: 14 15: [ Move disk 3 from tower 1 to tower 2 ], C: 15 1: [ Move disk 5 from tower 0 to tower 1 ], C: 1 2: [ Move disk 4 from tower 0 to tower 2 ], C: 2 3: [ Move disk 5 from tower 1 to tower 2 ], C: 3 4: [ Move disk 3 from tower 0 to tower 1 ], C: 4 5: [ Move disk 5 from tower 2 to tower 0 ], C: 5 6: [ Move disk 4 from tower 2 to tower 1 ], C: 6 7: [ Move disk 5 from tower 0 to tower 1 ], C: 7 8: [ Move disk 2 from tower 0 to tower 2 ], C: 8 9: [ Move disk 5 from tower 1 to tower 2 ], C: 9 10: [ Move disk 4 from tower 1 to tower 0 ], C: 10 11: [ Move disk 5 from tower 2 to tower 0 ], C: 11 12: [ Move disk 3 from tower 1 to tower 2 ], C: 12 13: [ Move disk 5 from tower 0 to tower 1 ], C: 13 14: [ Move disk 4 from tower 0 to tower 2 ], C: 14 15: [ Move disk 5 from tower 1 to tower 2 ], C: 15 16: [ Move disk 1 from tower 0 to tower 1 ], C: 16 17: [ Move disk 5 from tower 2 to tower 0 ], C: 17 18: [ Move disk 4 from tower 2 to tower 1 ], C: 18 19: [ Move disk 5 from tower 0 to tower 1 ], C: 19 20: [ Move disk 3 from tower 2 to tower 0 ], C: 20 21: [ Move disk 5 from tower 1 to tower 2 ], C: 21 22: [ Move disk 4 from tower 1 to tower 0 ], C: 22 23: [ Move disk 5 from tower 2 to tower 0 ], C: 23 24: [ Move disk 2 from tower 2 to tower 1 ], C: 24 25: [ Move disk 5 from tower 0 to tower 1 ], C: 25 26: [ Move disk 4 from tower 0 to tower 2 ], C: 26 27: [ Move disk 5 from tower 1 to tower 2 ], C: 27 28: [ Move disk 3 from tower 0 to tower 1 ], C: 28 29: [ Move disk 5 from tower 2 to tower 0 ], C: 29 30: [ Move disk 4 from tower 2 to tower 1 ], C: 30 31: [ Move disk 5 from tower 0 to tower 1 ], C: 31 32: [ Move disk 0 from tower 0 to tower 2 ], C: 32 33: [ Move disk 5 from tower 1 to tower 2 ], C: 33 34: [ Move disk 4 from tower 1 to tower 0 ], C: 34 35: [ Move disk 5 from tower 2 to tower 0 ], C: 35 36: [ Move disk 3 from tower 1 to tower 2 ], C: 36 37: [ Move disk 5 from tower 0 to tower 1 ], C: 37 38: [ Move disk 4 from tower 0 to tower 2 ], C: 38 39: [ Move disk 5 from tower 1 to tower 2 ], C: 39 40: [ Move disk 2 from tower 1 to tower 0 ], C: 40 41: [ Move disk 5 from tower 2 to tower 0 ], C: 41 42: [ Move disk 4 from tower 2 to tower 1 ], C: 42 43: [ Move disk 5 from tower 0 to tower 1 ], C: 43 44: [ Move disk 3 from tower 2 to tower 0 ], C: 44 45: [ Move disk 5 from tower 1 to tower 2 ], C: 45 46: [ Move disk 4 from tower 1 to tower 0 ], C: 46 47: [ Move disk 5 from tower 2 to tower 0 ], C: 47 48: [ Move disk 1 from tower 1 to tower 2 ], C: 48 49: [ Move disk 5 from tower 0 to tower 1 ], C: 49 50: [ Move disk 4 from tower 0 to tower 2 ], C: 50 51: [ Move disk 5 from tower 1 to tower 2 ], C: 51 52: [ Move disk 3 from tower 0 to tower 1 ], C: 52 53: [ Move disk 5 from tower 2 to tower 0 ], C: 53 54: [ Move disk 4 from tower 2 to tower 1 ], C: 54 55: [ Move disk 5 from tower 0 to tower 1 ], C: 55 56: [ Move disk 2 from tower 0 to tower 2 ], C: 56 57: [ Move disk 5 from tower 1 to tower 2 ], C: 57 58: [ Move disk 4 from tower 1 to tower 0 ], C: 58 59: [ Move disk 5 from tower 2 to tower 0 ], C: 59 60: [ Move disk 3 from tower 1 to tower 2 ], C: 60 61: [ Move disk 5 from tower 0 to tower 1 ], C: 61 62: [ Move disk 4 from tower 0 to tower 2 ], C: 62 63: [ Move disk 5 from tower 1 to tower 2 ], C: 63