{ "cells": [ { "cell_type": "markdown", "id": "11e7f6a4ec790751", "metadata": {}, "source": [ "## Sorting example\n", "\n", "This example is meant as a demonstration of the capabilities of simulated annealing. There are of course better algorithms than metaheuristic approaches." ] }, { "cell_type": "code", "id": "dc3193b2e8744190", "metadata": { "collapsed": true, "ExecuteTime": { "end_time": "2025-12-20T19:34:48.257739Z", "start_time": "2025-12-20T19:34:48.250482Z" } }, "source": [ "from collections.abc import Sequence\n", "\n", "from altbacken.external.neighbourhood.permutation import PermutationNeighbourhood\n", "from altbacken.external.annealing import SimpleSimulatedAnnealing\n" ], "outputs": [], "execution_count": 1 }, { "cell_type": "code", "id": "c86c51aa1b86a25f", "metadata": { "ExecuteTime": { "end_time": "2025-12-20T19:34:48.312023Z", "start_time": "2025-12-20T19:34:48.309843Z" } }, "source": [ "unsorted = [4, 2, 9, 7, 1, 6, 0, 8, 5, 3]\n" ], "outputs": [], "execution_count": 2 }, { "cell_type": "markdown", "id": "e44c1852bad35cd", "metadata": { "ExecuteTime": { "end_time": "2025-12-13T21:07:25.915061Z", "start_time": "2025-12-13T21:07:25.912776Z" } }, "source": [ "### Fitness function\n", "\n", "We will use inversion count as it provides a simple measure of the quality of a permutation and provides some local optima.\n" ] }, { "cell_type": "code", "id": "f69f4e6405ecfb3d", "metadata": { "ExecuteTime": { "end_time": "2025-12-20T19:34:48.360847Z", "start_time": "2025-12-20T19:34:48.358031Z" } }, "source": [ "def inversion_count(seq: Sequence[int]) -> int:\n", " return sum(\n", " 1 for i in range(len(seq))\n", " for j in range(i+1, len(seq))\n", " if seq[i] > seq[j]\n", " )" ], "outputs": [], "execution_count": 3 }, { "cell_type": "markdown", "id": "47e08ea8246b2fbe", "metadata": {}, "source": [ "### Annealing strategy\n", "\n", "We choose a simple simulated annealing strategy with a temperature of 10000.0." ] }, { "cell_type": "code", "id": "e78f5bfc585109ef", "metadata": { "ExecuteTime": { "end_time": "2025-12-20T19:34:48.408918Z", "start_time": "2025-12-20T19:34:48.406638Z" } }, "source": [ "annealing: SimpleSimulatedAnnealing = SimpleSimulatedAnnealing(\n", " inversion_count,\n", " PermutationNeighbourhood(),\n", " 10000.0\n", ")" ], "outputs": [], "execution_count": 4 }, { "cell_type": "code", "id": "a2924cc298cc34ea", "metadata": { "ExecuteTime": { "end_time": "2025-12-20T19:34:48.464484Z", "start_time": "2025-12-20T19:34:48.455995Z" } }, "source": [ "best_match, best_value = annealing.simulate(unsorted)\n", "\n", "print(\"Best match:\", best_match)\n", "print(\"Best value:\", best_value)" ], "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Best match: (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)\n", "Best value: 0\n" ] } ], "execution_count": 5 } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "python", "version": "3.14" } }, "nbformat": 4, "nbformat_minor": 5 }