]> git.scottworley.com Git - slidingtile/blobdiff - sliding_tile_lib.cc
Bound the frontier. I.e., do beam search.
[slidingtile] / sliding_tile_lib.cc
index 51aeded95e8ded018c2bff508c9d62daa3ac0fad..5a4114bd89c6ab621ddec79db2476ef8e0665494 100644 (file)
@@ -2,6 +2,7 @@
 
 #include <cstdlib>
 #include <istream>
+#include <map>
 #include <memory>
 #include <ostream>
 #include <queue>
@@ -133,8 +134,10 @@ int Board::distance(const Board& o) const {
 int Board::distance(const InvertedBoard& invo) const {
   int dist = 0;
   for (int i = 0; i < BOARD_SIZE; i++) {
-    dist += std::abs(i % BOARD_DIM - invo.pos[board[i]] % BOARD_DIM) +
-            std::abs(i / BOARD_DIM - invo.pos[board[i]] / BOARD_DIM);
+    if (board[i] != 0) {
+      dist += std::abs(i % BOARD_DIM - invo.pos[board[i]] % BOARD_DIM) +
+              std::abs(i / BOARD_DIM - invo.pos[board[i]] / BOARD_DIM);
+    }
   }
   return dist;
 }
@@ -186,7 +189,7 @@ std::ostream& operator<<(std::ostream& os, const Step& step) {
   return os;
 }
 
-std::shared_ptr<Step> find_path(const std::string& start, const std::string& goal) {
+std::shared_ptr<Step> find_path(const std::string& start, const std::string& goal, unsigned max_frontier) {
   std::istringstream iss_start{start}, iss_goal{goal};
   Board board_start, board_goal;
   iss_start >> board_start;
@@ -197,39 +200,39 @@ std::shared_ptr<Step> find_path(const std::string& start, const std::string& goa
   if (iss_goal.fail() || !iss_goal.eof()) {
     throw std::runtime_error("Could not parse the goal board: " + goal);
   }
-  return find_path(board_start, board_goal);
+  return find_path(board_start, board_goal, max_frontier);
 }
 
-std::shared_ptr<Step> find_path(const Board& start, const Board& goal) {
+std::shared_ptr<Step> find_path(const Board& start, const Board& goal, unsigned max_frontier) {
   InvertedBoard invgoal = goal.invert();
-  auto heap_greater = [invgoal](const std::shared_ptr<Step>& a, const std::shared_ptr<Step>& b) {
-    return a->cost(invgoal) > b->cost(invgoal);
-  };
-  std::priority_queue<std::shared_ptr<Step>,
-                      std::vector<std::shared_ptr<Step>>,
-                      decltype(heap_greater)> todo(heap_greater);
+  std::multimap<int, std::shared_ptr<Step>> todo;
   std::set<Board> seen;
 
-  seen.emplace(start);
-  todo.push(std::make_shared<Step>(start, nullptr));
+  seen.insert(start);
+  auto start_step = std::make_shared<Step>(start, nullptr);
+  todo.emplace(start_step->cost(invgoal), start_step);
   while (!todo.empty()) {
-    if (todo.top()->board == goal) {
-      return todo.top();
+    auto cur = todo.begin()->second;
+    todo.erase(todo.begin());
+    if (cur->board == goal) {
+      return cur;
     }
-    std::vector<std::shared_ptr<Step>> successors = todo.top()->successors(todo.top());
+    std::vector<std::shared_ptr<Step>> successors = cur->successors(cur);
     for (const std::shared_ptr<Step>& s : successors) {
       if (seen.find(s->board) == seen.end()) {
-        seen.emplace(s->board);
-        todo.push(s);
+        seen.insert(s->board);
+        todo.emplace(s->cost(invgoal), s);
         if (seen.size() % 10000 == 0) {
           std::cerr << "Examined " << seen.size() << " boards.  Tracking "
                     << Step::count << " steps.  " << todo.size()
                     << " waiting.  Considering paths of length "
-                    << todo.top()->cost(invgoal) << std::endl;
+                    << cur->cost(invgoal) << std::endl;
         }
       }
     }
-    todo.pop();
+    while (todo.size() > max_frontier) {
+      todo.erase(--todo.end());
+    }
   }
   throw std::runtime_error("No path from start to goal");
 }