TheAlgorithms/C++ 1.0.0
All the 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()

Definition at line 290 of file a_star_search.cpp.

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>

Definition at line 384 of file a_star_search.cpp.

◆ MapOfPuzzleInfoWithPuzzleInfo

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

Definition at line 380 of file a_star_search.cpp.

◆ SetOfPuzzleInfo

template<typename Puzzle>
using machine_learning::aystar_search::AyStarSearch< Puzzle >::SetOfPuzzleInfo
Initial value:
std::set<std::shared_ptr<Info>, comparison_operator>

Definition at line 387 of file a_star_search.cpp.

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

Definition at line 394 of file a_star_search.cpp.

394 {
396 Final = std::make_shared<Info>(final);
397 }
A class defining A* search algorithm. for some initial state and final state.

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

Definition at line 431 of file a_star_search.cpp.

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

◆ 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)

Definition at line 407 of file a_star_search.cpp.

409 {
410 // Useful for traversing from final state to current state.
412 /*
413 * For storing the solution tree starting from initial state to
414 * final state
415 */
417 while (current_state != nullptr) {
418 answer.emplace_back(*current_state->state);
419 current_state = parent_of.find(current_state)->second;
420 }
421 return answer;
422 }

Member Data Documentation

◆ Final

template<typename Puzzle>
std::shared_ptr<Info> machine_learning::aystar_search::AyStarSearch< Puzzle >::Final
private

Definition at line 368 of file a_star_search.cpp.

◆ Initial

template<typename Puzzle>
std::shared_ptr<Info> machine_learning::aystar_search::AyStarSearch< Puzzle >::Initial
private

Definition at line 367 of file a_star_search.cpp.


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