Add concrete implementation of a problem
},
{
"cell_type": "code",
- "execution_count": 32,
+ "execution_count": 50,
"metadata": {},
"outputs": [
{
"aima.notebooks.classicalsearch.EightPuzzleProblem"
]
},
- "execution_count": 32,
+ "execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
"\n",
"public class EightPuzzleProblem implements Problem<int[], EightPuzzleProblem.Action> {\n",
"\n",
- " int[] initialState;\n",
+ " int[] initialState = new int[9];\n",
+ " \n",
+ " public EightPuzzleProblem(int[] initialState){\n",
+ " this.initialState = initialState;\n",
+ " }\n",
"\n",
" @Override\n",
" public int[] getInitialState() {\n",
" int y = gapPos % 3;\n",
" if (!(x == 2)) {\n",
" int valueOnBottom = result[(x + 1) * 3 + y];\n",
+ " System.out.println(valueOnBottom);\n",
" setValue(result, x, y, valueOnBottom);\n",
" setValue(result, x + 1, y, 0);\n",
" }\n",
" }\n",
"\n",
" private void setValue(int[] result, int x, int y, int valueOnTop) {\n",
- " int absPos = x * y + 3;\n",
+ " int absPos = x *3 + y;\n",
" result[absPos] = valueOnTop;\n",
" }\n",
"\n",
- " enum Action {\n",
+ " public enum Action {\n",
" LEFT, RIGHT, UP, DOWN\n",
" }\n",
"}\n",
"So, in this way we can implement a Problem. Now let us see our problem class in action"
]
},
+ {
+ "cell_type": "code",
+ "execution_count": 51,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Initial State = [5, 4, 0, 6, 1, 8, 7, 3, 2]\n",
+ "Available Actions = [DOWN, LEFT]\n",
+ "8\n",
+ "Resulting State = [5, 4, 8, 6, 1, 0, 7, 3, 2]\n",
+ "isGoal\tfalse\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "null"
+ ]
+ },
+ "execution_count": 51,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "import aima.notebooks.classicalsearch.EightPuzzleProblem;\n",
+ "import java.util.*;\n",
+ "\n",
+ "int [] initialState = new int[] { 5, 4, 0, 6, 1, 8, 7, 3, 2 };\n",
+ "\n",
+ "EightPuzzleProblem problem = new EightPuzzleProblem(initialState);\n",
+ "\n",
+ "System.out.println(\"Initial State = \" + Arrays.toString(initialState));\n",
+ "System.out.println(\"Available Actions = \" + problem.getActions(initialState).toString());\n",
+ "System.out.println(\"Resulting State = \" + Arrays.toString(problem.getResult(initialState,problem.getActions(initialState).get(0))));\n",
+ "System.out.println(\"isGoal\\t\"+ problem.testGoal(initialState));"
+ ]
+ },
{
"cell_type": "code",
"execution_count": null,
