Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
machine_learning::aystar_search::AyStarSearch< Puzzle > Class Template Reference

A class defining A* search algorithm. for some initial state and final state. More...

Collaboration diagram for machine_learning::aystar_search::AyStarSearch< Puzzle >:
[legend]

Classes

struct  comparison_operator
 Custom comparator for open_list. More...
 
struct  Info
 Struct that handles all the information related to the current state. More...
 

Public Types

using MapOfPuzzleInfoWithPuzzleInfo
 
using MapOfPuzzleInfoWithInteger
 
using SetOfPuzzleInfo
 

Public Member Functions

 AyStarSearch (const Puzzle &initial, const Puzzle &final)
 Parameterized constructor for AyStarSearch.
 
std::vector< Puzzle > Solution (std::shared_ptr< Info > FinalState, const MapOfPuzzleInfoWithPuzzleInfo &parent_of)
 A helper solution: launches when a solution for AyStarSearch is found.
 
std::vector< Puzzle > a_star_search (const std::function< uint32_t(const Puzzle &, const Puzzle &)> &dist, const uint32_t permissible_depth=30)
 

Private Types

typedef struct machine_learning::aystar_search::AyStarSearch::Info Info
 Struct that handles all the information related to the current state.
 

Private Attributes

std::shared_ptr< InfoInitial
 
std::shared_ptr< InfoFinal
 

Detailed Description

template<typename Puzzle>
class machine_learning::aystar_search::AyStarSearch< Puzzle >

A class defining A* search algorithm. for some initial state and final state.

AyStarSearch class is defined as the informed search algorithm that is formulated in terms of weighted graphs: starting from a specific starting node of a graph (initial state), it aims to find a path to the given goal node having the smallest cost (least distance travelled, shortest time, etc.) The weighted edges (or cost) is evaluated on two factors, G score (cost required from starting node or initial state to current state) and H score (cost required from current state to final state). The F(state), then is evaluated as: F(state) = G(state) + H(state). The best search would be the final state having minimum F(state) value

Template Parameters
Puzzledenotes the puzzle or problem involving initial state and final state to be solved by A* search.
Note
1. The algorithm is referred from pesudocode from Wikipedia page as is.
  1. For AyStarSearch to work, the definitions for template Puzzle is compulsory. a. Comparison operator for template Puzzle (<, ==, and <=) b. generate_possible_moves()

Member Typedef Documentation

◆ MapOfPuzzleInfoWithInteger

template<typename Puzzle >
using machine_learning::aystar_search::AyStarSearch< Puzzle >::MapOfPuzzleInfoWithInteger
Initial value:
std::map<std::shared_ptr<Info>, uint32_t, comparison_operator>

◆ MapOfPuzzleInfoWithPuzzleInfo

template<typename Puzzle >
using machine_learning::aystar_search::AyStarSearch< Puzzle >::MapOfPuzzleInfoWithPuzzleInfo

◆ SetOfPuzzleInfo

template<typename Puzzle >
using machine_learning::aystar_search::AyStarSearch< Puzzle >::SetOfPuzzleInfo
Initial value:

Constructor & Destructor Documentation

◆ AyStarSearch()

template<typename Puzzle >
machine_learning::aystar_search::AyStarSearch< Puzzle >::AyStarSearch ( const Puzzle & initial,
const Puzzle & final )
inline

Parameterized constructor for AyStarSearch.

Parameters
initialdenoting initial state of the puzzle
finaldenoting final state of the puzzle
392 {
393 Initial = std::make_shared<Info>(initial);
394 Final = std::make_shared<Info>(final);
395 }

Member Function Documentation

◆ a_star_search()

template<typename Puzzle >
std::vector< Puzzle > machine_learning::aystar_search::AyStarSearch< Puzzle >::a_star_search ( const std::function< uint32_t(const Puzzle &, const Puzzle &)> & dist,
const uint32_t permissible_depth = 30 )
inline

Main algorithm for finding FinalState, given the InitialState

Parameters
distthe heuristic finction, defined by the user
permissible_depththe depth at which the A* search discards searching for solution
Returns
List of moves from Final state to initial state, if evaluated, else returns an empty array

Stores the parent of the states

Stores the g_score

Stores the list to explore

Stores the list that are explored

431 {
432 MapOfPuzzleInfoWithPuzzleInfo
433 parent_of; /// Stores the parent of the states
434 MapOfPuzzleInfoWithInteger g_score; /// Stores the g_score
435 SetOfPuzzleInfo open_list; /// Stores the list to explore
436 SetOfPuzzleInfo closed_list; /// Stores the list that are explored
437
438 // Before starting the AyStartSearch, initialize the set and maps
439 open_list.emplace(Initial);
440 parent_of[Initial] = nullptr;
441 g_score[Initial] = 0;
442
443 while (!open_list.empty()) {
444 // Iterator for state having having lowest f_score.
445 typename SetOfPuzzleInfo::iterator it_low_f_score;
446 uint32_t min_f_score = 1e9;
447 for (auto iter = open_list.begin(); iter != open_list.end();
448 ++iter) {
449 // f score here is evaluated by g score (depth) and h score
450 // (distance between current state and final state)
451 uint32_t f_score = (*iter)->heuristic_value + (*iter)->depth;
452 if (f_score < min_f_score) {
453 min_f_score = f_score;
454 it_low_f_score = iter;
455 }
456 }
457
458 // current_state, stores lowest f score so far for this state.
459 std::shared_ptr<Info> current_state = *it_low_f_score;
460
461 // if this current state is equal to final, return
462 if (*(current_state->state) == *(Final->state)) {
463 return Solution(current_state, parent_of);
464 }
465 // else remove from open list as visited.
466 open_list.erase(it_low_f_score);
467 // if current_state has exceeded the allowed depth, skip
468 // neighbor checking
469 if (current_state->depth >= permissible_depth) {
470 continue;
471 }
472 // Generate all possible moves (neighbors) given the current
473 // state
474 std::vector<Puzzle> total_possible_moves =
475 current_state->state->generate_possible_moves();
476
477 for (Puzzle &neighbor : total_possible_moves) {
478 // calculate score of neighbors with respect to
479 // current_state
480 std::shared_ptr<Info> Neighbor = std::make_shared<Info>(
481 neighbor, dist(neighbor, *(Final->state)),
482 current_state->depth + 1U);
483 uint32_t temp_g_score = Neighbor->depth;
484
485 // Check whether this state is explored.
486 // If this state is discovered at greater depth, then discard,
487 // else remove from closed list and explore the node
488 auto closed_list_iter = closed_list.find(Neighbor);
489 if (closed_list_iter != closed_list.end()) {
490 // 1. If state in closed list has higher depth, then remove
491 // from list since we have found better option,
492 // 2. Else don't explore this state.
493 if (Neighbor->depth < (*closed_list_iter)->depth) {
494 closed_list.erase(closed_list_iter);
495 } else {
496 continue;
497 }
498 }
499 auto neighbor_g_score_iter = g_score.find(Neighbor);
500 // if the neighbor is already created and has minimum
501 // g_score, then update g_score and f_score else insert new
502 if (neighbor_g_score_iter != g_score.end()) {
503 if (neighbor_g_score_iter->second > temp_g_score) {
504 neighbor_g_score_iter->second = temp_g_score;
505 parent_of[Neighbor] = current_state;
506 }
507 } else {
508 g_score[Neighbor] = temp_g_score;
509 parent_of[Neighbor] = current_state;
510 }
511 // If this is a new state, insert into open_list
512 // else update if the this state has better g score than
513 // existing one.
514 auto iter = open_list.find(Neighbor);
515 if (iter == open_list.end()) {
516 open_list.emplace(Neighbor);
517 } else if ((*iter)->depth > Neighbor->depth) {
518 (*iter)->depth = Neighbor->depth;
519 }
520 }
521 closed_list.emplace(current_state);
522 }
523 // Cannot find the solution, return empty vector
524 return std::vector<Puzzle>(0);
525 }
std::vector< Puzzle > Solution(std::shared_ptr< Info > FinalState, const MapOfPuzzleInfoWithPuzzleInfo &parent_of)
A helper solution: launches when a solution for AyStarSearch is found.
Definition a_star_search.cpp:405
Here is the call graph for this function:

◆ Solution()

template<typename Puzzle >
std::vector< Puzzle > machine_learning::aystar_search::AyStarSearch< Puzzle >::Solution ( std::shared_ptr< Info > FinalState,
const MapOfPuzzleInfoWithPuzzleInfo & parent_of )
inline

A helper solution: launches when a solution for AyStarSearch is found.

Parameters
FinalStatethe pointer to the obtained final state
parent_ofthe list of all parents of nodes stored during A* search
Returns
the list of moves denoting moves from final state to initial state (in reverse)
407 {
408 // Useful for traversing from final state to current state.
409 auto current_state = FinalState;
410 /*
411 * For storing the solution tree starting from initial state to
412 * final state
413 */
414 std::vector<Puzzle> answer;
415 while (current_state != nullptr) {
416 answer.emplace_back(*current_state->state);
417 current_state = parent_of.find(current_state)->second;
418 }
419 return answer;
420 }
T emplace_back(T... args)
Here is the call graph for this function:

The documentation for this class was generated from the following file: