title: A* Algorithm with Manhattan Distance Heuristic
body: <p>I've been working on a 15-puzzle solver in C. And I had some issues with the huge amounts of memory that my code uses.</p>
  
  <p>I won't be posting my code because it's too long... I've implemented most of the libraries I'm using and it will probably just bring confusion to you. Let's start on the basics.</p>
  
  <p>The things that I'm using right now are: (All of them implemented in C)</p>
  
  <p><strong>- Fibonacci Heap:</strong></p>
  
  <pre><code>/* Struct for the Fibonacci Heap */
  typedef struct _fiboheap {
      int  size;           // Number of nodes in the heap
      node min;            // Pointer to the minimun element in the Heap
      funCompare compare;  // Function to compare within nodes in the heap
      freeFunction freeFun;// Function for freeing nodes in the heap
  } *fiboheap;
  
  
  /* Struct of a Fibonacci Heap Node */
  typedef struct _node {
      node parent;    // Pointer to the node parent
      node child;     // Pointer to the child node
      node left;      // Pointer to the right sibling
      node right;     // Pointer to the left sibling
      int  degree;    // Degree of the node
      int  mark;      // Mark
      void *key;      // Value of the node (Here is the element)
  } *node;
  </code></pre>
  
  <p><strong>- Hash Table</strong> </p>
  
  <p>The only thing I haven't implemented myself, I'm using <strong>uthash</strong>.</p>
  
  <p><strong>- 15-Puzzle States representation</strong></p>
  
  <p><strong>This is an interesting topic.</strong> Before explaining the situation here, let's just think a little bit about the 15-puzzle problem... As we know, 15-Puzzle has 16 tiles (We're counting the blank tile as the tile with the number 0). So, how many possible states has the 15-puzzle problem? Well, it's factorial(16) states. So, that's a lot of states. That means that we want our states to be as small as possible... If our initial state is too far away from the solution, we may explore so much states that our program memory will just explode.</p>
  
  <p>So... my 15-puzzle representation consist of using bits and logical operators:</p>
  
  <pre><code>/* Struct for the 15-puzzle States Representation */
  typedef struct _state {
      unsigned int quad_1; /* This int represent the first 8 numbers */
      unsigned int quad_2; /* This int represent the last 8 numbers */
      unsigned short zero; /* This is the position of the zero */
  } *state;
  </code></pre>
  
  <p>So what I'm doing is using logical operators to move and change the numbers, using the minimum space.</p>
  
  <p><strong>Note</strong> that the size of this struct is <strong>12 bytes</strong> (Because it has three integers)</p>
  
  <p><strong>- Manhattan Distance</strong></p>
  
  <p>This is just the well known Manhattan Distance Heuristic. Where you basically calculate the sum of the distances of each number current position to the number position in the goal state.</p>
  
  <p><strong>- A* Implementation and Node structure to be used with A*</strong></p>
  
  <p>Let's start with the Nodes.</p>
  
  <pre><code>typedef struct nodo_struct {
      nodo parent;        // Pointer to the parent of the node
      state estado;       // State associated with the node
      unsigned int g : 8; // Cost of the node
      action a;           // Action by which we arrived to this node
                          // If null, it means that this is the initial node
  } *nodo;
  </code></pre>
  
  <p><strong>Note</strong> that these nodes had nothing to do with the nodes in fibonacci heap.</p>
  
  <p>And now the main reason of this topic... The pseudocode of A* I'm currently using.</p>
  
  <pre><code>a_star(state initial_state) {
      q = new_fibo_heap; // Sorted by (Cost g) + (Manhattan Distance)
                         // It will have nodes which contain a pointer to the state
      q.push(make_root_node(initial_state));
      closed = new_hash_table();
      while (!q.empty()) {
          n = q.pop();
          if ((n-&gt;state ∉ closed) || (n-&gt;g &lt; dist(n-&gt;state))) { 
          /* The dist used above is stored in the hash table as well */
              closed.insert(n-&gt;state);
              dist(n-&gt;state) = n-&gt;g;   // Update the hash table
              if (is_goal(n-&gt;state)) {
                  return extract_solution(n); // Go through parents, return the path
              }
              for &lt;a,s&gt; ∈ successors(n-&gt;state) {
              // 'a' is the action, It can be 'l', 'r', 'u', 'd'
              // 's' is the state that is a successor of n
                  if (manhattan(s) &lt; Infinity) {
                      q.push(make_node(n,a,s));
                      // Assuming that manhattan is safe
                  }
              }
          }
      }
      return NULL;
  }
  </code></pre>
  
  <p>So the questions I haven't been able to answer yet are... </p>
  
  <p><strong>How do you efficiently manage the memory? Can you re-use states or nodes? What consequences does that bring?</strong> </p>
  
  <p>I mean, If you take a look at the pseudocode. It's not considering re-using states or nodes.  It just keeps allocating over and over nodes and states, even though they've been calculated before. </p>
  
  <p>I've been thinking a lot about this... Each time you run the solver, it can expand millions of nodes real quick. And as we know, A* may re-explore nodes when you find another path with a cost less than the previous one. That means... If we explore 1 million nodes, it will be 24 millions of bytes (Wow)... And considering that each node has a state, that would be 14 bytes per node, those are 14 millions of bytes...</p>
  
  <p><strong><em>In the end, What I need is a way of reusing/freeing space so my computer won't crash when I execute this solver.</em></strong></p>
  
  <p>(PD: Sorry for the long post)</p>