A Tents and Trees puzzle solver using Mixed Integer Programming (MIP)
Project description
Tents and Trees MIP Solver
A Tents and Trees puzzle solver using mathematical programming.
Overview
Tents and Trees is a logic puzzle where you must place tents on a grid according to specific rules:
- Each tree must have exactly one adjacent tent (horizontally or vertically)
- Each tent must be adjacent to exactly one tree
- Tents cannot touch each other (even diagonally)
- Row and column constraints specify how many tents must be in each row/column
This solver models the puzzle as a Mixed Integer Programming (MIP) problem to find solutions.
Try It Online
🚀 Interactive Dashboard - Try the solver in your browser with a user-friendly interface built with Streamlit!
Installation
pip install tents-and-trees-mip-solver
Requirements
- Python 3.9+
- Google OR-Tools
- pytest (for testing)
Usage
from tents_and_trees_mip_solver import TentsAndTreesPuzzle, TentsAndTreesSolver
# Define a puzzle
puzzle = TentsAndTreesPuzzle(
row_sums=[1, 1, 0, 2, 1], # Tents per row
col_sums=[2, 0, 1, 1, 1], # Tents per column
tree_positions={(1,1), (1,3), (3,0), (3,1), (4,4)} # Tree locations
)
# Display the puzzle
print("Puzzle:")
print(puzzle.display_board())
# Solve the puzzle
solver = TentsAndTreesSolver(puzzle)
solution = solver.solve()
if solution:
print(f"\nSolution found! Tent positions: {solution}")
# Validate the solution
is_valid, errors = puzzle.validate_solution(solution)
print(f"Solution is valid: {is_valid}")
# Display the solved board
print("\nSolved puzzle:")
print(puzzle.display_board(tent_positions=solution))
# Get solver information
info = solver.get_solver_info()
print(f"\nModel consists of {info['variables']} variables and {info['constraints']} constraints")
else:
print("No solution exists")
Output
Puzzle:
2 0 1 1 1
1 _ _ _ _ _
1 _ T _ T _
0 _ _ _ _ _
2 T T _ _ _
1 _ _ _ _ T
Solution found! Tent positions: {(4, 0), (3, 4), (0, 3), (1, 0), (3, 2)}
Solution is valid: True
Solved puzzle:
2 0 1 1 1
1 _ _ _ @ _
1 @ T _ T _
0 _ _ _ _ _
2 T T @ _ @
1 @ _ _ _ T
Model consists of 13 variables and 36 constraints
Legend: T=Tree, @=Tent, _=Empty
Testing
The project uses pytest for testing:
pytest # Run all tests
pytest --cov=puzzle --cov=solver # Run with coverage
Algorithm Details
The solver uses Mixed Integer Programming (MIP) to model the puzzle with:
- Binary decision variables for each potential tent position
- Seven constraint types ensuring all puzzle rules are satisfied:
- Tent-tree balance (equal counts)
- Tree adjacency (each tree has ≥1 adjacent tent)
- Tent separation (no adjacent tents)
- Row sum constraints
- Column sum constraints
- Tree group balance constraints
- Unshared tile constraints
Solver Backend: Uses Google OR-Tools with SCIP optimizer by default.
Mathematical Model: See the complete formulation in Mathematical Model Documentation.
License
This project is open source and available under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tents_and_trees_mip_solver-0.1.2.tar.gz.
File metadata
- Download URL: tents_and_trees_mip_solver-0.1.2.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a7048980c774844e35a1f613db3c74d87d33971686d53e4c91a7f6216e5c69b3
|
|
| MD5 |
8c23d55cd158a23708e1836649df54bf
|
|
| BLAKE2b-256 |
9cb7d7bbd4454658f53c44d4671d74ae1afcff4eba20475d757b62bd2a176bc4
|
File details
Details for the file tents_and_trees_mip_solver-0.1.2-py3-none-any.whl.
File metadata
- Download URL: tents_and_trees_mip_solver-0.1.2-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8b24de38ff118b4da03570dd94f4ba089ae3c4c634fa0563707ed6d94da2ecc9
|
|
| MD5 |
ef6ddcfe486c54bdb40d66dea5f4c872
|
|
| BLAKE2b-256 |
8c7af4b59d0bf48de7afcdbd522a70dece53a56cfcc1535111bef8abf17bb17f
|