Create Your First Project
Start adding your projects to your portfolio. Click on "Manage Projects" to get started
Expense 8 Puzzle Problem
Date
October 2023
Location
Arlington Texas
This code implements various search algorithms (Depth-First Search, Breadth-First Search, Uniform Cost Search, Greedy Best-First Search, A* Search, and Iterative Deepening Search) for solving the sliding puzzle problem. The sliding puzzle is represented as a grid with tiles numbered from 0 to 8, where 0 represents the empty tile. The goal is to reach a target configuration from a given initial configuration through a sequence of tile moves.
Let's break down the code structure and components:
StateGenerator Class:
Initializes with the start state, goal state, and search method.
find_zero_position: Locates the position of the empty tile (0) in the current state.
add_next_state: Generates possible next states by moving the empty tile in different directions.
estimation_function: Heuristic function estimating the distance from the current state to the goal state.
find_child: Finds child states based on the current state and search method.
SearchState Class:
Stores various statistics and information about the search process.
Search Algorithms:
Depth-First Search (dfs_cal and dfs_print):
Implements a depth-first search algorithm.
It maintains a stack (cus_fg) for exploring states.
Breadth-First Search (bfs and bfs_print):
Implements a breadth-first search algorithm.
It maintains a queue (cus_fg) for exploring states.
Uniform Cost Search (ucs and ucs_print):
Implements a uniform cost search algorithm.
It maintains a priority queue (cus_fg) based on the cost of reaching the state.
Greedy Best-First Search (greedy and greedy_print):
Implements a greedy best-first search algorithm.
It uses a heuristic function (hue_num) for prioritizing states in the queue.
A Search (astar and astar_print):*
Implements the A* search algorithm.
It combines the cost to reach the state (cst_exp) and the heuristic value (hue_num) for prioritizing states in the queue.
Iterative Deepening Search (dls and ids_print):
Implements iterative deepening search.
It gradually increases the depth limit until the goal is reached or a specified depth limit is reached.
Other Functions:
generate_search_trace: Generates a search trace file if dump_flag is True.
steps: Recursive function to print the steps taken in the solution.
Main Function (main):
Reads start and goal states from input files.
Selects the search method based on command-line arguments or defaults to A*.
Invokes the corresponding search function (astar, dfs, bfs, ucs, greedy, ids, or dls).
Prints relevant statistics and the sequence of steps taken to reach the goal.
Usage:
The code is intended to be run from the command line, and the user can specify the search method and other parameters as command-line arguments.
Note:
It seems there's an inconsistency with the function names in the code. For example, search_trace is generated for "bfs" even when the method is "astar". Also, the steps function is called with approach_desired[i + 1], but approach_desired[i + 1] can be None causing an issue. These might need to be reviewed and adjusted accordingly.




