Skip to content

mjpieters/rummikub-solver

Repository files navigation

PyPI version License Python versions supported Built with uv Checked with Ruff Checked with Pyright Built with Material for MkDocs Python checks Coverage

Rummikub Solver library

Fast and flexible Rummikub solver library, written in Python, to find the best options for placing tiles from a player's rack on to the table.

The algorithm used builds on the approach described by D. Den Hertog, P. B. Hulshof (2006), "Solving Rummikub Problems by Integer Linear Programming", The Computer Journal, 49(6), 665-669 (DOI 10.1093/comjnl/bxl033).

Features

  • Can work with different Rummikub variations, letting you adjust the number of colours, tiles, and other aspects
  • You can freely adjust what tiles are on the rack or on the table, within the limits of what tiles are available according to the current rules
  • Can be used with any of the Mixed-Integer Linear Programming (MILP) solvers supported by cvxpy.

Solver improvements

The original models described by Den Hertog and Hulshof assume that all possible sets that meet the minimum length requirements and can't be split up are desirable outcomes.

However, any group set (tiles with the same number but with different colours) containing at least one joker, but which is longer than the minimal run, in effect contains a redundant joker, something you wouldn't want to leave on the table for the next player to use. The same applies to run sets (tiles of the same colour but with consecutive numbers), that are longer than the minimal set length but start or end with a joker. In this implementation, such sets are omitted from the possible options.

The implementation also includes a solver for the initial move, where you can only use tiles from your own rack and must place a minimum amount of points before you can use tiles already on the table. This solver is a variant of the original solver that maximizes tiles placed, but is constrained by the minimal point amount and disregards jokers (which means jokers are only used for the opening meld if that is the only option available).

Documentation

See the project documentation for details.

Install

You can install this project the usual way. e.g. with pip:

$ pip install rummikub-solver

or with uv:

$ uv add rummikub-solver

Picking an alternative solver backend

This library builds on cvxpy to define the Rummikub models, which can then be solved using any of the supported MILP solver backends. By default, the SCIPY backend is used, which in turn uses a version of the HiGHS optimizer that comes bundled with SciPy.

You can also install an alternative Open Source solver backends via extras:

Extra Backend License
cbc COIN-OR Branch-and-Cut solver EPL-2.0
glpk_mi GNU Linear Programming Kit GPL-3.0-only Installs the cvxopt project
highs HiGHS MIT Arguably the best OSS MILP solver available. This installs a newer version of HiGHS than what is bundled with SciPy.
scip SCIP Apache-2.0

You can also pick from a number of commercial solvers; no extras are provided for these:

Refer to their respective documentations for installation instructions.

When HiGHS is installed, it is automatically used as the default solver.

Development

The source code for this project can be found on GitHub.

When running locally, install uv, then run:

$ uv run rsconsole

to run the console solver. A Taskfile is provided that defines specific tasks such as linting, formatting or previewing the documentation:

$ task --list
task: Available tasks for this project:
* default:                     Default task, runs linters and tests
* dev:format:                  Runs formatters      (aliases: format)
* dev:install-precommit:       Install pre-commit into local git checkout
* dev:lint:                    Runs linters      (aliases: lint)
* dev:lint:code:               Lint the source code
* dev:lint:renovate:           Lint the Renovate configuration file
* dev:test:                    Run tests                                          (aliases: test)
* dev:uv-lock:                 Updates the uv lock file                           (aliases: lock)
* dist:build:                  Build the distribution packages                    (aliases: dist)
* dist:clean:                  Remove built distribution packages                 (aliases: clean)
* dist:publish:                Publish package to PyPI                            (aliases: publish)
* docs:build:                  Build project documentation                        (aliases: docs)
* docs:serve:                  Live preview server for project documentation      (aliases: preview)

Credits

The initial version of the solver was written by Ollie Hooper.

This version is a complete rewrite by Martijn Pieters, to improve performance and address shortcomings in the original paper.

About

Fast and flexible Rummikub solver library

Topics

Resources

License

Stars

Watchers

Forks

Contributors 2

  •  
  •  

Languages