{ "cells": [ { "metadata": {}, "cell_type": "markdown", "source": [ "# Terrible Search Engine\n", "\n", "Using simulated annealing on websites with links pointing to neighbours is not a good idea, but a good example for async annealing." ], "id": "88cb648dc111d148" }, { "metadata": { "collapsed": true, "ExecuteTime": { "end_time": "2026-07-01T21:32:47.864907206Z", "start_time": "2026-07-01T21:32:47.852781369Z" } }, "cell_type": "code", "outputs": [], "execution_count": 45, "source": [ "from urllib.parse import urljoin\n", "from random import choice\n", "\n", "from anyio import sleep\n", "from httpx import AsyncClient\n", "from bs4 import BeautifulSoup\n", "from altbacken.core.async_annealing import AnnealingState\n", "from altbacken.external.async_annealing import SimpleAsyncSimulatedAnnealing" ], "id": "2233d4dcc94800b5" }, { "metadata": { "ExecuteTime": { "end_time": "2026-07-01T21:08:49.862087569Z", "start_time": "2026-07-01T21:08:49.843450567Z" } }, "cell_type": "code", "source": [ "type URL = str | None\n", "type Weight = int" ], "id": "1e97599a39a5978b", "outputs": [], "execution_count": 11 }, { "metadata": { "ExecuteTime": { "end_time": "2026-07-01T21:11:12.531277355Z", "start_time": "2026-07-01T21:11:12.513488364Z" } }, "cell_type": "code", "source": [ "SEARCH_KEYS: dict[str, Weight] = {\n", " \"Python\": -1,\n", " \"Moon\": -5,\n", " \"Bass\": -8,\n", " \"Cheesecake\": -10\n", "}" ], "id": "13ad3f6428ff6688", "outputs": [], "execution_count": 15 }, { "metadata": { "ExecuteTime": { "end_time": "2026-07-01T21:38:30.255073134Z", "start_time": "2026-07-01T21:38:30.206041569Z" } }, "cell_type": "code", "source": [ "class LinkNeighbourhood:\n", " async def __call__(self, state: AnnealingState[URL]) -> URL:\n", " if current_url := state.current_solution:\n", " try:\n", " async with AsyncClient(follow_redirects=True) as client:\n", " response = await client.get(current_url)\n", " if not response.is_success:\n", " return current_url\n", " await sleep(0.5)\n", " soup = BeautifulSoup(response.text, \"html.parser\")\n", " links: list[str] = [\n", " urljoin(current_url, link[\"href\"])\n", " for link in soup.find_all(\"a\", {\"href\": True})\n", " ]\n", " return choice(links) if links else None\n", " except Exception:\n", " return current_url\n", " else:\n", " return None\n" ], "id": "b29ed2546adef332", "outputs": [], "execution_count": 53 }, { "metadata": { "ExecuteTime": { "end_time": "2026-07-01T21:38:31.230987970Z", "start_time": "2026-07-01T21:38:31.221101945Z" } }, "cell_type": "code", "source": [ "async def fitness(url: URL) -> float:\n", " if url is None:\n", " return 100.0\n", " async with AsyncClient(follow_redirects=True) as client:\n", " response = await client.get(url)\n", " await sleep(0.5)\n", " if not response.is_success:\n", " return 50.0\n", " text: str = response.text\n", " return sum(\n", " text.count(key) * weight\n", " for key, weight in SEARCH_KEYS.items()\n", " )\n" ], "id": "9161e7c27436d5a9", "outputs": [], "execution_count": 54 }, { "metadata": { "ExecuteTime": { "end_time": "2026-07-01T21:38:32.257157919Z", "start_time": "2026-07-01T21:38:32.248315960Z" } }, "cell_type": "code", "source": [ "annealing: SimpleAsyncSimulatedAnnealing[URL] = SimpleAsyncSimulatedAnnealing(\n", " fitness,\n", " LinkNeighbourhood()\n", ")\n", "\n", "visited_sites: set[URL] = set()\n", "\n", "async def tracer(state: AnnealingState[URL]) -> None:\n", " visited_sites.add(state.current_solution)\n", "\n", "annealing.tracer = tracer" ], "id": "2d13b6caf8515629", "outputs": [], "execution_count": 55 }, { "metadata": { "ExecuteTime": { "end_time": "2026-07-01T21:38:49.594223134Z", "start_time": "2026-07-01T21:38:34.002282Z" } }, "cell_type": "code", "source": "best_url, value = await annealing.simulate(\"https://stackoverflow.com/questions\")", "id": "d49eca2668d5a1c", "outputs": [], "execution_count": 56 }, { "metadata": { "ExecuteTime": { "end_time": "2026-07-01T21:38:49.665670293Z", "start_time": "2026-07-01T21:38:49.602114497Z" } }, "cell_type": "code", "source": "best_url, value", "id": "782f72c3b91d1b33", "outputs": [ { "data": { "text/plain": [ "('https://stackoverflow.com/questions/79972182/scipy-import-from-module-fails-sometimes-path-dependent',\n", " -6)" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "execution_count": 57 }, { "metadata": { "ExecuteTime": { "end_time": "2026-07-01T21:38:49.731517017Z", "start_time": "2026-07-01T21:38:49.675371214Z" } }, "cell_type": "code", "source": "visited_sites", "id": "785b4f7b7c27ada5", "outputs": [ { "data": { "text/plain": [ "{None,\n", " 'https://biology.stackexchange.com/users/129209/keiv',\n", " 'https://stackexchange.com',\n", " 'https://stackoverflow.com/questions/79972182/scipy-import-from-module-fails-sometimes-path-dependent',\n", " 'https://stackoverflowteams.com',\n", " 'https://stackoverflowteams.com/teams/create/free/?utm_medium=referral&utm_source=biology-community&utm_campaign=side-bar&utm_content=explore-teams',\n", " 'https://stackoverflowteams.com/teams/create/free?isLogin=false',\n", " 'https://stackoverflowteams.com/teams/create/free?isLogin=true',\n", " 'https://www.instagram.com/thestackoverflow'}" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "execution_count": 58 } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }