{ "cells": [ { "cell_type": "markdown", "id": "bc007b0b3800724f", "metadata": {}, "source": [] }, { "cell_type": "markdown", "id": "bc2f2d3d6aa8f352", "metadata": {}, "source": [ "## Numpy Example\n", "\n", "In this example, we will use numpy to perform matrix rotation using simulated annealing. We will define a fitness function that evaluates the rotation of a candidate matrix, and use simulated annealing to find the optimal rotation matrix.\n" ] }, { "cell_type": "code", "id": "657005d25728d98a", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "ExecuteTime": { "end_time": "2025-12-20T19:34:55.230907Z", "start_time": "2025-12-20T19:34:55.129615Z" } }, "source": [ "from pathlib import Path\n", "from numpy import array, zeros, matvec, ndarray, flip\n", "from numpy.random import default_rng\n", "from numpy.linalg import norm\n", "from altbacken.external.neighbourhood.numeric import ArrayNeighbourhood\n", "from altbacken.external.annealing import SimpleSimulatedAnnealing\n", "from altbacken.internal.analysis.convergence import ConvergenceAnalyzer\n", "from altbacken.internal.analysis.acceptance import AcceptanceAnalyzer\n", "from altbacken.internal.report.file import JSONLineReport" ], "outputs": [], "execution_count": 1 }, { "cell_type": "markdown", "id": "33d94a2078ec4925", "metadata": {}, "source": [ "### Fitness function\n", "\n", "We will use a simple fitness function to evaluate the rotation of a candidate matrix.\n", "The fitness function is the sum of the absolute difference between the rotated candidate and the optimal solution. For testings, we will use 100 random vectors.\n" ] }, { "cell_type": "code", "id": "d0af4d170633b8ac", "metadata": { "ExecuteTime": { "end_time": "2025-12-20T19:34:55.253074Z", "start_time": "2025-12-20T19:34:55.243145Z" } }, "source": [ "rng = default_rng(None)\n", "\n", "CANDIDATES: list[ndarray] = [\n", " (v := rng.normal(size=2)) / norm(v)\n", " for _ in range(100)\n", "]" ], "outputs": [], "execution_count": 2 }, { "cell_type": "code", "id": "d845bea2e89fff33", "metadata": { "ExecuteTime": { "end_time": "2025-12-20T19:34:55.293267Z", "start_time": "2025-12-20T19:34:55.291029Z" } }, "source": [ "def apply_matrix(matrix: ndarray) -> float:\n", " return sum(norm(flip(candidate) - matvec(matrix, candidate))**2 for candidate in CANDIDATES)\n" ], "outputs": [], "execution_count": 3 }, { "cell_type": "markdown", "id": "ffe6f68af61a46e2", "metadata": {}, "source": [ "### Annealing strategy\n", "\n", "We choose a simple simulated annealing strategy with a temperature of 10000.0 and a neighbourhood size of 0.1." ] }, { "cell_type": "code", "id": "b7905bea98c8539f", "metadata": { "ExecuteTime": { "end_time": "2025-12-20T19:34:55.342888Z", "start_time": "2025-12-20T19:34:55.338449Z" } }, "source": [ "annealing: SimpleSimulatedAnnealing = SimpleSimulatedAnnealing(\n", " apply_matrix,\n", " ArrayNeighbourhood(0.1),\n", " 10000.0\n", ")\n", "\n", "annealing.tracer = JSONLineReport(\n", " Path(\"rotation_log.json_line\"),\n", " [ConvergenceAnalyzer(), AcceptanceAnalyzer(annealing.energy)],\n", ")" ], "outputs": [], "execution_count": 4 }, { "cell_type": "markdown", "id": "1a7be8457121c4ed", "metadata": {}, "source": [ "### Evaluation\n", "\n", "The best values are printed, together with the norm of the difference between the matrix obtained by annealing and the rotation matrix." ] }, { "cell_type": "code", "id": "eba98aa75147f234", "metadata": { "ExecuteTime": { "end_time": "2025-12-20T19:34:55.834217Z", "start_time": "2025-12-20T19:34:55.385141Z" } }, "source": [ "best_match, best_value = annealing.simulate(zeros((2,2)))\n", "annealing.tracer.close()" ], "outputs": [], "execution_count": 5 }, { "cell_type": "code", "id": "386465803229bc69", "metadata": { "ExecuteTime": { "end_time": "2025-12-20T19:34:55.845763Z", "start_time": "2025-12-20T19:34:55.842118Z" } }, "source": [ "print(\"Best match\", best_match)\n", "print(\"Best value\", best_value)\n", "print(\"Distance from optimum\", norm(best_match - array([[0,1], [1, 0]])))" ], "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Best match [[0.01098449 0.98603174]\n", " [0.99981828 0.00534302]]\n", "Best value 0.016596983069443485\n", "Distance from optimum 0.01855673291866348\n" ] } ], "execution_count": 6 } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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 }