{ "cells": [ { "cell_type": "markdown", "id": "fbed597e", "metadata": {}, "source": [ "# HGSRouter example" ] }, { "cell_type": "markdown", "id": "5767fd33", "metadata": {}, "source": [ "This notebook uses `OptiWindNet` to route the collector system cables via `HGSRouter`." ] }, { "cell_type": "markdown", "id": "9fe197e1", "metadata": {}, "source": [ "## 🧬 HGS (Hybrid Genetic Search)" ] }, { "cell_type": "markdown", "id": "6b901e0f", "metadata": {}, "source": [ "`HGSRouter` is a *metaheuristic based router* that applies *Hybrid Genetic Search (HGS)* to optimize electrical network of a defined turbine-layout as a *Capacitated Vehicle Routing Problem (CVRP)*. It can handle both single and multiple substations and is well-suited for *fast and high-quality networks*.\n", "\n", "This router is slower than `EWRouter` but typically yields *better-quality solutions*, especially for medium and large windfarms with one substation." ] }, { "cell_type": "markdown", "id": "1435a3f2", "metadata": {}, "source": [ "### 🔧 Constructor: `HGSRouter(...)`" ] }, { "cell_type": "markdown", "id": "1724f5a7-e294-4113-a649-1f10fc5d43bb", "metadata": {}, "source": [ "**Required arguments:**\n", "\n", "| Argument | Type | Description |\n", "| ------------ | ----- | ----------------------------------------------------------------------------------------------------------------------- |\n", "| `time_limit` | `int` | Time limit (in seconds) which controls how long the genetic algorithm can search for better solutions. |\n", "\n", "**Optional arguments:**\n", "\n", "| Argument | Type | Description |\n", "| --------------- | ------------- | --------------------------------------------------------------------------------------- |\n", "| `feeder_limit` | `int or None` | Maximum number of feeders. |\n", "| `max_retries` | `int` | Maximum number of retries if a feasible solution is not found (default: `10`). Total runtime may reach up to `(max_retries + 1) * time_limit` in the worst case. |\n", "| `balanced` | `bool` | If `True`, balances the number of turbines per feeder. |\n", "| `seed` | `int` | Set random seed (default: None, meaning new seed). |\n", "| `verbose` | `bool` | If `True`, prints routing details and warnings. |" ] }, { "cell_type": "markdown", "id": "917f3e68", "metadata": {}, "source": [ "**✅ Example**\n", "\n", "```python\n", "wf = WindFarmNetwork(...)\n", "wf.optimize(\n", " router=HGSRouter(\n", " time_limit=60,\n", " feeder_limit=10,\n", " balanced=True,\n", " )\n", ")\n", "```\n", "* HGSRouter trades speed for *higher-quality networks* and is particularly useful when:\n", " * A better solution is worth longer runtime (compared to `EWRouter`)\n", " * The number of turbines connected to each feeder needs to be balanced\n", "\n", "* In *multi-substation networks*, the solver can only enforce a `feeder_limit` that is the minimum feasible number. The parameter may also be omitted (meaning unlimited feeders)." ] }, { "cell_type": "markdown", "id": "b9ee6aad", "metadata": {}, "source": [ ">This router uses [vidalt/HGS-CVRP: Modern implementation of the hybrid genetic search (HGS) algorithm specialized to the capacitated vehicle routing problem (CVRP)](https://github.com/vidalt/HGS-CVRP).\n", ">\n", ">HGSRouter can only produce *radial* topologies. Since a *radial* topology is a special case of the *branched* topology, solutions produced by this method can be used to warm-start both *branched*- and *radial*-topology models." ] }, { "cell_type": "markdown", "id": "ce9294e2", "metadata": {}, "source": [ "## Load data" ] }, { "cell_type": "markdown", "id": "89e1961f", "metadata": {}, "source": [ "import required modules" ] }, { "cell_type": "code", "execution_count": 1, "id": "c42a3703-113b-4aee-8dfb-a0b3b54663e6", "metadata": {}, "outputs": [], "source": [ "from optiwindnet.api import WindFarmNetwork, HGSRouter" ] }, { "cell_type": "markdown", "id": "bde9df37", "metadata": {}, "source": [ "Create an instance of `wfn` using `.from_pbf()`\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "b4c9f79f-6f57-4a77-b233-17c4e44f052e", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wfn = WindFarmNetwork.from_pbf(filepath='data/DTU_letters.osm.pbf', cables= [(7, 2000.0)])\n", "wfn" ] }, { "cell_type": "markdown", "id": "5d38a18d-ab5e-4b03-ae74-9ccb4dedbe07", "metadata": {}, "source": [ "## Optimize with HGSRouter" ] }, { "cell_type": "code", "execution_count": 3, "id": "809d91ad-1df9-4340-8107-fb8bbd8ad92a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1596.1580407347585" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "res = wfn.optimize(router=HGSRouter(time_limit=3))\n", "wfn.length()" ] }, { "cell_type": "markdown", "id": "b4165029", "metadata": {}, "source": [ "### Get solution_time" ] }, { "cell_type": "markdown", "id": "16ffe463-babd-4745-b712-1cbfedbe65fd", "metadata": {}, "source": [ "We can see the solution time for HGS using:" ] }, { "cell_type": "code", "execution_count": 4, "id": "5aaefea4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.02" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wfn.S.graph['solution_time']" ] }, { "cell_type": "markdown", "id": "3d18f6e4", "metadata": {}, "source": [ "It prints the computation time for each HGS algorithm call (which run in parallel in multi-core systems); \n", "each call handling one substation and its turbine cluster.\n", "The router partitions the turbines automatically in clusters, with the constraint of keeping the total number of feeders at the minimum." ] }, { "cell_type": "markdown", "id": "2bb4e742", "metadata": {}, "source": [ "### Choosing `time_limit` wisely" ] }, { "cell_type": "markdown", "id": "ed68ae5c", "metadata": {}, "source": [ "Based on the solution times observed in the previous step, we can make an informed decision about the time limit for the HGS router. If increasing the time limit does not significantly improve the solution (e.g., does not result in a noticeably shorter cable length), we can consider reducing it to save computational resources. In this case, a time limit of 0.1 second appears to be sufficient, as it is well above all the solution times." ] }, { "cell_type": "code", "execution_count": 5, "id": "45d586b9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.02" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wfn.optimize(router=HGSRouter(time_limit=0.1))\n", "wfn.S.graph['solution_time']" ] }, { "cell_type": "markdown", "id": "c61410af", "metadata": {}, "source": [ "#### Check the length\n", "\n", "We can see that, as expected, reducing the time limit from 10 seconds to 1 seconds has not affected the resultant length." ] }, { "cell_type": "code", "execution_count": 6, "id": "045e232b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1596.1580407347585" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wfn.length()" ] }, { "cell_type": "markdown", "id": "8d7ad952", "metadata": {}, "source": [ "### Plot the Optimized Network Graph" ] }, { "cell_type": "code", "execution_count": 7, "id": "696b6ca2", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "3 192 316 €Σλ = 1 596.2 m(+1) [-1]: 7κ = 7, T = 40" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wfn" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }