//Returns if the state is a goal state bool TSP::is_goal( const search_node& s ) const { //If we aren't in the initial city, there's no way we're done if ( s.get_state().location() != s.m_path[0] ) return false; //Check each city for visited status for ( unsigned long int i = 0; i < m_num_cities; i++ ) if ( s.get_state().get_visited()[i] == false ) return false; return true; }
void TSP::successor( const search_node& iNode, vector<search_node>& avail ) { //For each city in the current state for ( unsigned long int i = 0; i < m_num_cities; i++ ) { //If that city has not been visited if ( !iNode.get_state().get_visited()[i] ) { //Then push it into the list of available states to move to avail.push_back( iNode ); //And update it to reflect what would happen after we move there avail.back().get_state().set_visited(i); avail.back().get_state().set_location(i); avail.back().m_path.push_back(i); avail.back().set_cost( iNode.cost() + m_graph[iNode.get_state().location()][avail.back().get_state().location()] ); } } return; }
bool TSP::is_BiASGS_goal( const search_node& s, const multiset<search_node, less<search_node> >& frontier ) { multiset<search_node>::iterator it; for ( it = frontier.begin(); it != frontier.end(); it++ ) { if ( s.get_state() == (*it).get_state() ) { return true; } } return false; }
void TSP::predecessor( const search_node& iNode, deque<search_node>& avail ) { //Make a copy of the current node search_node updated_node = iNode; //Set that node's location to unvisited (note: unreachable state!) updated_node.get_state().set_unvisited( updated_node.get_state().location() ); //For each city in the node for ( unsigned int i = 0; i < m_num_cities; i++ ) { //If that city is visited already if ( updated_node.get_state().get_visited()[i] ) { avail.push_back( updated_node ); avail.back().get_state().set_location(i); avail.back().m_path.push_back(i); avail.back().set_cost( iNode.cost() + m_graph[iNode.get_state().location()][avail.back().get_state().location()] + h_min_graph() ); } } return; }
double TSP::h_straight_line( const search_node& s ) { //straight line distance from initial location to current location return m_graph[s.m_path[0]][s.get_state().location()]; }