sohamsadhu/hellogit
Folders and files
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Repository files navigation
Requirements: Python3.2 on system, the shell file tscript.sh all the five puzzle files named in convention puzzle#.txt and program rblock.py in the same folder. There is just one program file rblock.py and all functions are built into it rather than putting them in separate files. Execution: To execute the program and get the results you only need to type following on bash prompt. ./tscript.sh This script will dump all the results of the program for each of the puzzle file into a text file called testresults.txt in the same folder, and if such a file does not exist, the script will create. And if such a file for results exist then that will be cleared completly in each run of the script file and then entered with results from new run. Since the script file runs all the individual puzzle files, you do not have to run the program individually. However if you want to run the individual files you have to use it in following way, type the following on a bash terminal python3.2 rblock.py puzzle<number>.txt Results: The file testresults.txt will have all the results consolidated for you. For each of the puzzle file for each heuristic the cost to reach from start to goal, the final orientation of the die and the rolls that the die took are shown. The orientation of the die has been shown as a list of characters, where the characters represent the side facing and the index stands for the face of die. For example if index 1 has character 'u', it means the side 1 is facing up. The terminology used for the orientation are u = up, d = down, n = north, s = south, e = east and w = west. With this convention the start state die is represented as [ u, n, e, w, s, d ]. The rolls for the die are given in order they are taken. The side to which it rolls once is given by the following letters N = North or rolls upside or positive Y axis, S = South or rolls downside or negative Y axis, E = East or rolls right towards positive X axis and W = West or rolls towards negative X axis. There is also result that says the number of nodes generated which we get by the number of times a queue is poped. At the end when we find the goal, we add the length of the existing frontier queue with the number of times the queue had been poped to get the final number of states generated. Note: The shell file may not work if there is no python3.2, or the files are not named in convention puzzle#.txt and all five of them, or if the files are in different folders. Also if you want to test another puzzle file, you will have to type in the command "python3.2 rblock.py puzzle_file_name" Structure of the program: So the program starts at the bottom of file rblock.py with the invocation of main() function. From line 325 to 334 the program calls the function readMazeFile() which reads the puzzle file provided on command line. And sends back if it was successfull and a list representation of the maze. The function mazeFile() at line 7 looks if characters for all lines are equal that is maze of equal width and that it does not contain any other character than accepted. Next are lines 335 - 338 that get a heuristic function. These functions are defined around line 87. The goal cordinate is hard coded to the function. So it only gets the current location distances for evaluation with respect to goal. eDistance is for Euclidean straight line distance calculated by Pythagorean hypotenuese formula. In case the die cordinates and goal cordinates are in same row then too we get the straight line distance. Another one is Manhattan distance. The last heuristic is a slight variant of Manhattan distance with the obstacle thrown in, where the most of the work is done from line 99 in function findNumObstacle(). Next is call to the main A* evaluation function to give the results. The function a_star can be found around line 296. The function astar is the verbatim copy of the best first search algorithm python code implementation from the course text book source code implementation. In the course text book the implementation is with class and objects for so had to change it bit for my list implementation. The start die a function at around line 33 is the problem node state implementation. The die will have the representation of the problem; it is a list of lists. The first list has the present cordinates of the die, the next list has present orientation of the die, the third list will have the xrolls taken by the die till that time, and fourth list is the list of roll action like N, E, W, S that have been taken by the die till then. The explored (which is ideally a set) and frontier (ideally a priority queue) are both implemented as lists. The function isPresent() on line 115 which checks presence of node in list, and thus used as for implementing set for explored. You just have to check for the cordinates and orientation of die if they are same then it the same state. For frontier the nodes are appended as list but are poped off by function popElement() around line 242 from a list by the min priority based on the heuristic which gives it a priority queue implementation. The getChildren() function at around line 254 is one that generates different problem children states. It does this by following. 1. It has got the present die state which is representation of the problem. From the position of the current die via cordinates, it checks for the four possible moves it can make to N, E, W, S. It then checks on making those moves it does not land in a cell which is obstruction '*'. 2. If the cell is not a obstruction then you move to that cell. You change the orientation by calling rollDice. And then you increment the cost, update the cordinates and append the action at the end of action list. 3. Lastly you append this list made to a the list that will be sent back with other actions taken back to A* main algorithm for processing.