{"id":23074394,"url":"https://github.com/farzanmrz/heuristic-pegboard-solver","last_synced_at":"2025-10-11T21:08:38.305Z","repository":{"id":266379412,"uuid":"898125305","full_name":"farzanmrz/heuristic-pegboard-solver","owner":"farzanmrz","description":"Solver for the classic pegboard puzzle, leveraging heuristic search algorithms like A* and DFS for optimized problem-solving. Features state modeling, action representation, and heuristic-driven exploration to demonstrate advanced search strategies","archived":false,"fork":false,"pushed_at":"2024-12-04T00:37:44.000Z","size":62,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-11T21:08:37.779Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/farzanmrz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-03T20:44:27.000Z","updated_at":"2024-12-04T00:40:07.000Z","dependencies_parsed_at":"2024-12-04T01:35:45.062Z","dependency_job_id":null,"html_url":"https://github.com/farzanmrz/heuristic-pegboard-solver","commit_stats":null,"previous_names":["farzanmrz/heuristic-pegboard-solver"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/farzanmrz/heuristic-pegboard-solver","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farzanmrz%2Fheuristic-pegboard-solver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farzanmrz%2Fheuristic-pegboard-solver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farzanmrz%2Fheuristic-pegboard-solver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farzanmrz%2Fheuristic-pegboard-solver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/farzanmrz","download_url":"https://codeload.github.com/farzanmrz/heuristic-pegboard-solver/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farzanmrz%2Fheuristic-pegboard-solver/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279008721,"owners_count":26084494,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-11T02:00:06.511Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-16T08:28:18.834Z","updated_at":"2025-10-11T21:08:38.276Z","avatar_url":"https://github.com/farzanmrz.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Heuristic Pegboard Solver\nThe pegboard puzzle is a classic game where pegs are arranged on a board with one empty slot, and the goal is to remove all but one peg by jumping one peg over another into an empty slot. This project implements a solver for the pegboard problem, leveraging heuristic-based search methods and random exploration strategies.\n\nThe initial board state is represented as an integer (e.g., `65023`), which is converted into a binary representation. Each bit corresponds to a slot on the board: `1` for a filled slot (peg) and `0` for an empty slot. This binary representation is then mapped to an `n x n` square grid. \n\n![Pegboard Example](pegboard.png)\n\n\n## Methodology\n\nThis project aims to provide a comprehensive solution to the pegboard puzzle by combining brute-force exploration with heuristic-guided search algorithms. The methodology leverages systematic state and action representations to enable efficient problem-solving techniques.\n\n- **State Representation**: Models the pegboard state as a binary grid to represent pegs (`1`) and empty slots (`0`) in an `n x n` layout.\n  \u003cbr\u003e\u003cbr\u003e\n- **Action Representation**: Defines the possible moves (peg jumps) that transition the pegboard from one state to another. Each action specifies the starting peg, the jumped peg, and the destination slot.\n  \u003cbr\u003e\u003cbr\u003e\n- **Flail Wildly**: A brute-force exploration method where random valid actions are iteratively applied until either a solution is found or no further actions are possible. This method is inefficient but serves as a baseline for comparison.\n  \u003cbr\u003e\u003cbr\u003e\n- **Search Algorithms**: Implements depth-first search (DFS) and A* search with various heuristics:\n  \u003cbr\u003e\u003cbr\u003e\n  - **DFS**: Explores all paths exhaustively, diving deep into one branch before backtracking. It guarantees finding a solution if one exists but is not optimal.\n    \u003cbr\u003e\u003cbr\u003e\n  - **Heuristic 1**: Prioritizes states with more valid actions remaining, assuming greater flexibility may lead to a solution.\n    \u003cbr\u003e\u003cbr\u003e\n  - **Heuristic 2**: Minimizes the sum of Manhattan distances from all pegs to the initial empty slot, favoring spatial convergence.\n    \u003cbr\u003e\u003cbr\u003e\n  - **Heuristic 3**: Combines peg reduction and Manhattan distances, assigning higher weight to reducing the number of pegs.\n    \u003cbr\u003e\u003cbr\u003e\n  - **A\\* with Heuristic 1**: Balances the cost of reaching the current state (`g(n)`) with Heuristic 1 to guide the search.\n    \u003cbr\u003e\u003cbr\u003e\n  - **A\\* with Heuristic 2**: Uses Heuristic 2 to prioritize states closer to the goal spatially.\n    \u003cbr\u003e\u003cbr\u003e\n  - **A\\* with Heuristic 3**: Uses Heuristic 3 for an efficient balance of peg reduction and spatial optimization.\n    \u003cbr\u003e\u003cbr\u003e\n\n\n## Classes\n\n- **State**: Converts a given integer into an `n x n` binary grid representation to model the current state of the pegboard.\n  \u003cbr\u003e\u003cbr\u003e\n  - `applicableActions`: Determines all valid actions (peg jumps) for the current state.\n  \u003cbr\u003e\u003cbr\u003e\n  - `goal_remaining`: Checks if only one peg remains.\n  \u003cbr\u003e\u003cbr\u003e\n  - `goal`: Checks if one peg remains at the initial empty position.\n  \u003cbr\u003e\u003cbr\u003e\n\n- **Action**: Defines the jumper (starting peg), goner (jumped peg), and newpos (destination slot) to model actions available on the pegboard.\n  \u003cbr\u003e\u003cbr\u003e\n  - `precondition`: Checks if the action is valid in the current state.\n  \u003cbr\u003e\u003cbr\u003e\n  - `applyState`: Applies the action to generate a new state.\n  \u003cbr\u003e\u003cbr\u003e\n\n\n\n## Running the Project\n\n1. **Clone the Repository**:\n   ```bash\n   git clone https://github.com/Farzanmrz/heuristic-pegboard-solver.git\n   ```\n   \n2. **Run Flail Wildly**: Ensure state is an integer \n   ```bash\n   python main.py flailWildly \u003cstate\u003e\n   ```\n\n3. **Run a Search Algorithm**: Specify a search algorithm (`dfs`, `heuristic1`, `heuristic2`, `heuristic3`,`astar1`, `astar2`, or `astar3`) and the initial state as an integer\n    ```bash\n      python main.py dfs \u003cstate\u003e\n      python main.py heuristic1 \u003cstate\u003e\n      python main.py heuristic2 \u003cstate\u003e\n      python main.py heuristic3 \u003cstate\u003e\n      python main.py astar1 \u003cstate\u003e\n      python main.py astar2 \u003cstate\u003e\n      python main.py astar3 \u003cstate\u003e\n     ```\n## Future Work\n- Extend the implementation to support non-square pegboard configurations.\n- Introduce additional heuristics to improve the efficiency of the search algorithms.\n- Develop a graphical interface to visualize the pegboard states and action sequences.\n\n## License\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n## Contact\n* Farzan Mirza: [farzan.mirza@drexel.edu](mailto:farzan.mirza@drexel.edu) | [LinkedIn](https://www.linkedin.com/in/farzan-mirza13/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffarzanmrz%2Fheuristic-pegboard-solver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffarzanmrz%2Fheuristic-pegboard-solver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffarzanmrz%2Fheuristic-pegboard-solver/lists"}