Skip to content

Commit

Permalink
solve error
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragneel7 committed Jan 12, 2018
1 parent 5ba93f5 commit ae89679
Showing 1 changed file with 87 additions and 15 deletions.
102 changes: 87 additions & 15 deletions search.ipynb
Expand Up @@ -20,7 +20,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 1,
"metadata": {
"scrolled": true
},
Expand Down Expand Up @@ -1256,12 +1256,46 @@
"display_visual(user_input = True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A* Heuristics\n",
"\n",
"Different heuristics provide different have different efficiency in solving A* problems which are generally defined by the number of explored nodes as well as the branching factor. With the classic 8 puzzle we can show the efficiency of different heuristics through the number of explored nodes.\n",
"\n",
"### 8 Puzzle Problem\n",
"\n",
"The *8 Puzzle Problem* consists of a 3x3 tray in which the goal is to get the initial configuration to the goal state by shifting the numbered tiles into the blank space.\n",
"\n",
"example:- \n",
"\n",
" Initial State Goal State\n",
" | 7 | 2 | 4 | | 0 | 1 | 2 |\n",
" | 5 | 0 | 6 | | 3 | 4 | 5 |\n",
" | 8 | 3 | 1 | | 6 | 7 | 8 |\n",
" \n",
"We have a total of 9 blank tiles giving us a total of 9! initial configuration but not all of these are solvable, the solvability of a configuration can be checked by calculating the Inversion Permutation.\n",
"\n",
"#### Heuristics :-\n",
"\n",
"1.) Manhattan Distance:- For the 8 puzzle problem Manhattan distance is defined as the distance of a tile from its goal state( for the tile numbered '1' in the initial configuration Manhattan distance is 4 \"2 for left and 2 for upward displacement\").\n",
"\n",
"2.) No. of Misplaced Tiles:- The heuristic calculates the number of misplaced tiles between the current state and goal state.\n",
"\n",
"3.) Sqrt of Manhattan Distance:- It calculates the square root of Manhattan distance.\n",
"\n",
"4.) Max Heuristic:- It assign the score as max of Manhattan Distance and No. of misplaced tiles. "
]
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# heuristics for 8 Puzzle Problem\n",
"\n",
"def linear(state,goal):\n",
" return sum([1 if state[i] != goal[i] else 0 for i in range(8)])\n",
"\n",
Expand Down Expand Up @@ -1301,27 +1335,65 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'xrange' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-9-e8d98ff0af98>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mpuzzle\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mEightPuzzle\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mpuzzle\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcheckSolvability\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m6\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m7\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m8\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mpuzzle\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msolve\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m6\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m7\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m8\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m6\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m7\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m8\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mmax_heuristic\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mpuzzle\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msolve\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m6\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m7\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m8\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m6\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m7\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m8\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mlinear\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/Desktop/aima/aima-python/search.py\u001b[0m in \u001b[0;36mcheckSolvability\u001b[0;34m(self, state)\u001b[0m\n\u001b[1;32m 411\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcheckSolvability\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstate\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 412\u001b[0m \u001b[0minversion\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 413\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstate\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 414\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mj\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstate\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 415\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mstate\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0mstate\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mj\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mstate\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mj\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m!=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'xrange' is not defined"
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"Number of explored nodes by the following heuristic are: 126\n",
"[2, 4, 3, 1, 5, 6, 7, 8, 0]\n",
"[2, 4, 3, 1, 5, 0, 7, 8, 6]\n",
"[2, 4, 3, 1, 0, 5, 7, 8, 6]\n",
"[2, 0, 3, 1, 4, 5, 7, 8, 6]\n",
"[0, 2, 3, 1, 4, 5, 7, 8, 6]\n",
"[1, 2, 3, 0, 4, 5, 7, 8, 6]\n",
"[1, 2, 3, 4, 0, 5, 7, 8, 6]\n",
"[1, 2, 3, 4, 5, 0, 7, 8, 6]\n",
"[1, 2, 3, 4, 5, 6, 7, 8, 0]\n",
"Number of explored nodes by the following heuristic are: 129\n",
"[2, 4, 3, 1, 5, 6, 7, 8, 0]\n",
"[2, 4, 3, 1, 5, 0, 7, 8, 6]\n",
"[2, 4, 3, 1, 0, 5, 7, 8, 6]\n",
"[2, 0, 3, 1, 4, 5, 7, 8, 6]\n",
"[0, 2, 3, 1, 4, 5, 7, 8, 6]\n",
"[1, 2, 3, 0, 4, 5, 7, 8, 6]\n",
"[1, 2, 3, 4, 0, 5, 7, 8, 6]\n",
"[1, 2, 3, 4, 5, 0, 7, 8, 6]\n",
"[1, 2, 3, 4, 5, 6, 7, 8, 0]\n",
"Number of explored nodes by the following heuristic are: 126\n",
"[2, 4, 3, 1, 5, 6, 7, 8, 0]\n",
"[2, 4, 3, 1, 5, 0, 7, 8, 6]\n",
"[2, 4, 3, 1, 0, 5, 7, 8, 6]\n",
"[2, 0, 3, 1, 4, 5, 7, 8, 6]\n",
"[0, 2, 3, 1, 4, 5, 7, 8, 6]\n",
"[1, 2, 3, 0, 4, 5, 7, 8, 6]\n",
"[1, 2, 3, 4, 0, 5, 7, 8, 6]\n",
"[1, 2, 3, 4, 5, 0, 7, 8, 6]\n",
"[1, 2, 3, 4, 5, 6, 7, 8, 0]\n",
"Number of explored nodes by the following heuristic are: 139\n",
"[2, 4, 3, 1, 5, 6, 7, 8, 0]\n",
"[2, 4, 3, 1, 5, 0, 7, 8, 6]\n",
"[2, 4, 3, 1, 0, 5, 7, 8, 6]\n",
"[2, 0, 3, 1, 4, 5, 7, 8, 6]\n",
"[0, 2, 3, 1, 4, 5, 7, 8, 6]\n",
"[1, 2, 3, 0, 4, 5, 7, 8, 6]\n",
"[1, 2, 3, 4, 0, 5, 7, 8, 6]\n",
"[1, 2, 3, 4, 5, 0, 7, 8, 6]\n",
"[1, 2, 3, 4, 5, 6, 7, 8, 0]\n"
]
}
],
"source": [
"# Solving the puzzle \n",
"puzzle = EightPuzzle()\n",
"puzzle.checkSolvability([2,4,3,1,5,6,7,8,0])\n",
"puzzle.solve([2,4,3,1,5,6,7,8,0],[1,2,3,4,5,6,7,8,0],max_heuristic)\n",
"puzzle.solve([2,4,3,1,5,6,7,8,0],[1,2,3,4,5,6,7,8,0],linear)"
"puzzle.checkSolvability([2,4,3,1,5,6,7,8,0]) # checks whether the initialized configuration is solvable or not\n",
"puzzle.solve([2,4,3,1,5,6,7,8,0],[1,2,3,4,5,6,7,8,0],max_heuristic) # Max_heuristic\n",
"puzzle.solve([2,4,3,1,5,6,7,8,0],[1,2,3,4,5,6,7,8,0],linear) # Linear\n",
"puzzle.solve([2,4,3,1,5,6,7,8,0],[1,2,3,4,5,6,7,8,0],manhanttan) # Manhattan\n",
"puzzle.solve([2,4,3,1,5,6,7,8,0],[1,2,3,4,5,6,7,8,0],sqrt_manhanttan) # Sqrt_manhattan"
]
},
{
Expand Down

0 comments on commit ae89679

Please sign in to comment.