This repository contains a Monte-Carlo Markov Chain (MCMC) implementation for solving the
The
-
Create a python environment and install required packages.
conda create -n queens python=3.10 && conda activate queens pip install -r requirements.txt -
There are two different ways to launch the algorithm.
-
You can run the code as a Python script. In this case, again there are two different ways to proceed.
-
Run the code for a single configuration (board size, number of iterations, beta function, acceptance function). Add
--csv_outputto save the final queen positions to a CSV file.python src/mcmc.py --num_iterations <num-iterations> --board_size <board-size> --beta_func <beta-func> --acceptance_func <acceptance-func>
-
Get general results of our work comparing several configurations for given board sizes and number of iterations. (Use
--helpto see all available options).python src/results.py --board_sizes <space-separated-board-sizes> --num_iterations <num-iterations>
-
-
You can launch the web-based visualization, as a Flask application. This displays queen moving positions in a 3D cube, with the energy evolution as the algorithm run. After running the command below, you should open your browser to
http://127.0.0.1:5000and click "Start MCMC Simulation" to configure and run the algorithm.cd webapp python app.py
-
This project uses MCMC with the following components:
-
Metropolis-Hastings Algorithm, which is a probabilistic acceptance criterion for proposed moves which accept moves that lower so-called energy, which is our loss function. It accepts worse moves probabilistically based on temperature, and acceptance probability is given by
$P(\text{accept}) = \min(1, e^{-\beta \Delta E})$ . -
Simulated Annealing, which uses an exponential cooling schedule to control temperature (
$\beta$ parameter) and helps escape local minima early in the search.
We represent each state of our Markov Chain as a list of
Two queens at positions
Two coordinates match.
- Same
$i-j$ plane:$i_1 = i_2 \land j_1 = j_2 \land k_1 \neq k_2$ - Same
$i-k$ plane:$i_1 = i_2 \land k_1 = k_2 \land j_1 \neq j_2$ - Same
$j-k$ plane:$j_1 = j_2 \land k_1 = k_2 \land i_1 \neq i_2$
Two coordinates differ by the same amount, and the third coordinate matches.
- In
$i-j$ plane:$|i_1 - i_2| = |j_1 - j_2| \land k_1 = k_2 \land |i_1 - i_2| \neq 0$ - In
$i-k$ plane:$|i_1 - i_2| = |k_1 - k_2| \land j_1 = j_2 \land |i_1 - i_2| \neq 0$ - In
$j-k$ plane:$|j_1 - j_2| = |k_1 - k_2| \land i_1 = i_2 \land |j_1 - j_2| \neq 0$
Three coordinates differ by the same amount.
$|i_1 - i_2| = |j_1 - j_2| = |k_1 - k_2| \land |i_1 - i_2| \neq 0$
The energy function counts the total number of attacking queen pairs.
This function directly measures constraint violations. The energy landscape is smooth, meaning incremental moves tend to produce gradual energy changes rather than sudden jumps, which aids the MCMC algorithm in finding minima.
For MCMC efficiency, energy computation is optimized by using several hash maps to store the number of queens attacking each plane, face diagonal and space diagonal. This allows computation of initial energy in
Define the probability distribution over configurations:
where
-
Propose Sample candidate
$s'$ from proposal distribution$q(s' | s_t)$ . In our case,$q$ selects a random queen uniformly and proposes a uniform random position. -
Accept/Reject Compute the acceptance probability:
Since our proposal is symmetric ($q(s_t | s') = q(s' | s_t)$), this simplifies to:
Draw
As
- Small
$\beta$ : High mixing, rapid convergence to high-temperature distribution (broad exploration) - Large
$\beta$ : Slow mixing, concentrated probability on low-energy states (focused search)
For finite computation, simulated annealing increases
where