Gem Hunter is an Artificial Intelligence problem that involves solving the task of finding gems on a grid map. The grid consists of various types of cells: traps (T), numbers indicating the count of traps around the cell, empty cells, and gems (G). The challenge is to determine the locations of the gems by translating the map into Conjunctive Normal Form (CNF) and solving it using different SAT-solving algorithms.
- PySAT: Solves the problem using SAT solvers from the
pysatlibrary. - Brute-force: Exhaustively checks all possible configurations.
- Backtracking: Attempts to find a solution by trying possibilities and backtracking when a solution fails.
- DPLL: A more efficient SAT-solving algorithm based on the Davis-Putnam-Logemann-Loveland procedure.
The project is organized as follows:
Gem Hunter/
│
├── algorithm
│ ├── backtracking.py # Backtracking algorithm
│ ├── bruteforce.py # Brute-force algorithm
│ ├── CNF.py # Converts map to CNF form
│ ├── pysat.py # Solves using the PySAT library
│ └── solvers.py # Contains generic solver functions
├── main.py # Main program to run the algorithms (DPLL is called here)
├── table.py # Manages the grid map
└── utils
├── filehandle.py # File input/output handling
└── helper.py # Helper functions for auxiliary tasks
To run the project, you can set up the required environment using Conda or Pip. Follow the instructions below for each method.
If you're using Conda, you can create a virtual environment and install the required libraries as follows:
-
Create a new Conda environment:
conda create -n gem_hunter python=3.11
-
Activate the environment:
conda activate gem_hunter
-
Install required libraries:
-
Install
pysatfrom Conda:conda install -c conda-forge pysat
-
If you need additional libraries like
itertools, you can install them usingpip:pip install itertools
-
-
After installing the libraries, you can run the scripts in the project.
If you prefer Pip, you can install the libraries as follows:
-
Create and activate a virtual environment (optional, but recommended):
python -m venv venv source venv/bin/activate # On macOS/Linux venv\Scripts\activate # On Windows
-
Install the required libraries:
pip install pysat pip install itertools
Alternatively, you can use the provided
requirements.txtfile to install all dependencies at once:pip install -r requirements.txt
requirements.txt:
pysat python-sat itertools
For Conda users, some libraries like pysat may not be available by default in Conda repositories. You can install them using pip after setting up the Conda environment.
Once the environment is set up and the required libraries are installed, you can run the program using:
python main.pyRun the program with the following command:
python main.py <algorithm> <test_case>-
<algorithm>: Algorithm to solve the problem. Supported values:pysatbacktrackingbruteforce
-
<test_case>: Test case file (without extension) from thetestcases/folder. Supported values:5x511x1120x20
python main.py pysat 5x5
python main.py backtracking 11x11
python main.py bruteforce 20x20- Input: The program will read the map from the files located in the
testcases/directory (e.g.,input_1.txt,input_2.txt, etc.). - Output: The results will be written to
output.txt, which will indicate the discovered locations of the gems.
The PySAT algorithm uses the pysat library to solve the problem by encoding it as a SAT problem. It utilizes advanced SAT solvers to efficiently find solutions to the gem-hunting problem.
The brute-force algorithm explores all possible configurations, checking each one to see if it satisfies the constraints of the problem. While it guarantees a solution, it is computationally expensive for larger grids.
Backtracking is a more intelligent approach, where the algorithm searches for a solution by incrementally building up possibilities and backtracks when an invalid solution path is encountered.
The DPLL (Davis-Putnam-Logemann-Loveland) algorithm is an optimization of the SAT-solving process. It uses efficient techniques such as unit propagation and variable elimination to improve performance compared to brute-force methods.
main.py: The main program that orchestrates the execution of different algorithms. The DPLL algorithm is implemented and invoked here.algorithm/: This directory contains various algorithms, such as backtracking, brute-force, pysat, and CNF generation.Utils/: A folder for utility functions, such as file I/O handling and other helper functions.
If you'd like to add new algorithms, simply create a new Python file in the algorithm/ directory. Then, you can integrate it into main.py by importing the appropriate functions.
Currently, the program runs in the command line. If desired, you could create a graphical user interface (GUI) to enhance user interaction. Consider using frameworks like Tkinter or PyQt for a GUI.