Skip to main content

A Tents and Trees puzzle solver using Mixed Integer Programming (MIP)

Project description

Tents and Trees MIP Solver

CI Code Coverage PyPI version Python License: MIT

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:
    1. Tent-tree balance (equal counts)
    2. Tree adjacency (each tree has ≥1 adjacent tent)
    3. Tent separation (no adjacent tents)
    4. Row sum constraints
    5. Column sum constraints
    6. Tree group balance constraints
    7. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tents_and_trees_mip_solver-0.1.2.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tents_and_trees_mip_solver-0.1.2-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file tents_and_trees_mip_solver-0.1.2.tar.gz.

File metadata

File hashes

Hashes for tents_and_trees_mip_solver-0.1.2.tar.gz
Algorithm Hash digest
SHA256 a7048980c774844e35a1f613db3c74d87d33971686d53e4c91a7f6216e5c69b3
MD5 8c23d55cd158a23708e1836649df54bf
BLAKE2b-256 9cb7d7bbd4454658f53c44d4671d74ae1afcff4eba20475d757b62bd2a176bc4

See more details on using hashes here.

File details

Details for the file tents_and_trees_mip_solver-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for tents_and_trees_mip_solver-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8b24de38ff118b4da03570dd94f4ba089ae3c4c634fa0563707ed6d94da2ecc9
MD5 ef6ddcfe486c54bdb40d66dea5f4c872
BLAKE2b-256 8c7af4b59d0bf48de7afcdbd522a70dece53a56cfcc1535111bef8abf17bb17f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page