{
"cells": [
{
"cell_type": "markdown",
"id": "baa0bae5-bb69-46b8-ae63-d4304f9859d9",
"metadata": {},
"source": [
"## Gurobi example"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "23c1c992-a563-4f2d-bbc8-cf0884e5b275",
"metadata": {},
"outputs": [],
"source": [
"from optiwindnet.importer import load_repository\n",
"from optiwindnet.svg import svgplot\n",
"from optiwindnet.mesh import make_planar_embedding\n",
"from optiwindnet.interarraylib import G_from_S\n",
"from optiwindnet.heuristics import constructor\n",
"from optiwindnet.MILP import solver_factory, ModelOptions"
]
},
{
"cell_type": "markdown",
"id": "d4a80ecc-64a9-4007-b6d6-ade397e250a3",
"metadata": {},
"source": [
"### Initialize Gwynt y Mor"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "29a6b5d1-a05f-4112-bf66-462b4d36b516",
"metadata": {},
"outputs": [],
"source": [
"locations = load_repository()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "666ca26c-b3cf-4000-a44e-cab38866018d",
"metadata": {},
"outputs": [],
"source": [
"L = locations.gwynt\n",
"capacity = 6"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a23b577c-d334-405e-b70a-9b3717eefa2b",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"svgplot(L)"
]
},
{
"cell_type": "markdown",
"id": "958b4117-b54a-4bba-9201-615d4c922cb2",
"metadata": {},
"source": [
"### Optimize Gwynt y Mor"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "df05184d-7caa-4b34-810e-37c13a7a8092",
"metadata": {},
"outputs": [],
"source": [
"P, A = make_planar_embedding(L)"
]
},
{
"cell_type": "markdown",
"id": "4f9b49b1-a597-4053-a3d1-f3de7bb8d164",
"metadata": {},
"source": [
"Initial heuristic solution to warm-start the solver:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "19eb273d-f662-4851-bf09-e0ec532d7b8b",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Sʹ = constructor(A, capacity=capacity)\n",
"Gʹ = G_from_S(Sʹ, A)\n",
"svgplot(Gʹ)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "d9ee42db-f79d-480f-bac9-8267741a7aed",
"metadata": {},
"outputs": [],
"source": [
"solver = solver_factory('gurobi')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "c3652c3b-3764-4dd7-bb4e-c3d51a178896",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Set parameter WLSAccessID\n",
"Set parameter WLSSecret\n",
"Set parameter LicenseID to value 937681\n",
"Set parameter MIPFocus to value 1\n",
"Academic license 937681 - for non-commercial use only - registered to ma___@dtu.dk\n"
]
}
],
"source": [
"solver.set_problem(\n",
" P, A,\n",
" capacity=Sʹ.graph['capacity'],\n",
" model_options=ModelOptions(\n",
" topology=\"branched\",\n",
" feeder_route=\"segmented\",\n",
" feeder_limit=\"unlimited\",\n",
" ),\n",
" warmstart=Sʹ,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "a5fa487b-6475-4be6-8259-4b4d090d384a",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Set parameter TimeLimit to value 90\n",
"Set parameter MIPGap to value 0.005\n",
"Gurobi Optimizer version 13.0.2 build v13.0.2rc1 (linux64 - \"Debian GNU/Linux forky/sid\")\n",
"\n",
"CPU model: 11th Gen Intel(R) Core(TM) i7-11850H @ 2.50GHz, instruction set [SSE2|AVX|AVX2|AVX512]\n",
"Thread count: 8 physical cores, 16 logical processors, using up to 16 threads\n",
"\n",
"Non-default parameters:\n",
"TimeLimit 90\n",
"MIPGap 0.005\n",
"MIPFocus 1\n",
"\n",
"Academic license 937681 - for non-commercial use only - registered to ma___@dtu.dk\n",
"Optimize a model with 5938 rows, 3976 columns and 22544 nonzeros (Min)\n",
"Model fingerprint: 0xf57374e8\n",
"Model has 1988 linear objective coefficients\n",
"Variable types: 0 continuous, 3976 integer (1988 binary)\n",
"Coefficient statistics:\n",
" Matrix range [1e+00, 6e+00]\n",
" Objective range [4e+02, 1e+04]\n",
" Bounds range [1e+00, 6e+00]\n",
" RHS range [1e+00, 2e+02]\n",
"\n",
"Loaded user MIP start with objective 136103\n",
"\n",
"Presolve removed 757 rows and 0 columns\n",
"Presolve time: 0.03s\n",
"Presolved: 5181 rows, 3976 columns, 19044 nonzeros\n",
"Variable types: 0 continuous, 3976 integer (1988 binary)\n",
"\n",
"Root relaxation: objective 1.252287e+05, 4730 iterations, 0.08 seconds (0.10 work units)\n",
"\n",
" Nodes | Current Node | Objective Bounds | Work\n",
" Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time\n",
"\n",
" 0 0 125228.749 0 310 136103.285 125228.749 7.99% - 0s\n",
"H 0 0 136065.07661 125228.749 7.96% - 0s\n",
"H 0 0 135994.94515 125228.749 7.92% - 0s\n",
"H 0 0 135395.43711 125228.749 7.51% - 0s\n",
"H 0 0 135009.41580 125228.749 7.24% - 0s\n",
"H 0 0 133571.28698 125228.749 6.25% - 0s\n",
" 0 0 125519.360 0 492 133571.287 125519.360 6.03% - 0s\n",
"H 0 0 133569.54874 125519.360 6.03% - 0s\n",
"H 0 0 133338.08445 125519.360 5.86% - 0s\n",
"H 0 0 133107.51091 125519.360 5.70% - 0s\n",
"H 0 0 133106.63501 125519.360 5.70% - 0s\n",
"H 0 0 133105.73725 125519.360 5.70% - 0s\n",
"H 0 0 133094.66555 125519.360 5.69% - 0s\n",
" 0 0 125641.091 0 514 133094.666 125641.091 5.60% - 0s\n",
" 0 0 125648.829 0 524 133094.666 125648.829 5.59% - 0s\n",
" 0 0 125649.955 0 539 133094.666 125649.955 5.59% - 0s\n",
" 0 0 125650.150 0 544 133094.666 125650.150 5.59% - 0s\n",
"H 0 0 133094.50473 125650.150 5.59% - 0s\n",
" 0 0 125994.529 0 557 133094.505 125994.529 5.33% - 1s\n",
" 0 0 126000.172 0 534 133094.505 126000.172 5.33% - 1s\n",
" 0 0 126021.660 0 541 133094.505 126021.660 5.31% - 1s\n",
" 0 0 126027.963 0 530 133094.505 126027.963 5.31% - 1s\n",
" 0 0 126029.213 0 538 133094.505 126029.213 5.31% - 1s\n",
" 0 0 126029.240 0 537 133094.505 126029.240 5.31% - 1s\n",
"H 0 0 133093.72744 126164.177 5.21% - 1s\n",
"H 0 0 133093.37507 126164.177 5.21% - 1s\n",
"H 0 0 133093.27941 126164.177 5.21% - 1s\n",
" 0 0 126164.177 0 578 133093.279 126164.177 5.21% - 1s\n",
"H 0 0 133093.11131 126164.177 5.21% - 1s\n",
"H 0 0 133093.10756 126164.177 5.21% - 1s\n",
"H 0 0 133092.96500 126164.177 5.21% - 1s\n",
"H 0 0 133092.52885 126164.177 5.21% - 1s\n",
"H 0 0 132600.01602 126209.885 4.82% - 2s\n",
"H 0 0 132521.18150 126209.885 4.76% - 2s\n",
" 0 0 126269.057 0 539 132521.181 126269.057 4.72% - 2s\n",
" 0 0 126279.293 0 568 132521.181 126279.293 4.71% - 2s\n",
" 0 0 126279.356 0 558 132521.181 126279.356 4.71% - 2s\n",
" 0 0 126279.356 0 566 132521.181 126279.356 4.71% - 2s\n",
" 0 0 126279.356 0 570 132521.181 126279.356 4.71% - 2s\n",
" 0 0 126279.356 0 574 132521.181 126279.356 4.71% - 2s\n",
"H 0 0 132520.86101 126279.356 4.71% - 2s\n",
" 0 0 126374.883 0 574 132520.861 126374.883 4.64% - 2s\n",
" 0 0 126406.943 0 600 132520.861 126406.943 4.61% - 2s\n",
"H 0 0 132519.77567 126415.813 4.61% - 3s\n",
" 0 0 126415.813 0 559 132519.776 126415.813 4.61% - 3s\n",
" 0 0 126418.298 0 578 132519.776 126418.298 4.60% - 3s\n",
" 0 0 126419.292 0 582 132519.776 126419.292 4.60% - 3s\n",
" 0 0 126419.465 0 586 132519.776 126419.465 4.60% - 3s\n",
" 0 0 126419.528 0 593 132519.776 126419.528 4.60% - 3s\n",
" 0 0 126448.915 0 565 132519.776 126448.915 4.58% - 3s\n",
" 0 0 126448.915 0 565 132519.776 126448.915 4.58% - 3s\n",
" 0 0 126448.915 0 565 132519.776 126448.915 4.58% - 3s\n",
"H 0 0 132519.75379 126448.920 4.58% - 4s\n",
"H 0 2 132516.53326 126448.920 4.58% - 7s\n",
" 0 2 126448.920 0 565 132516.533 126448.920 4.58% - 7s\n",
"H 5 8 132367.98692 126456.755 4.47% 194 7s\n",
"H 39 49 132101.60251 126473.190 4.26% 353 8s\n",
"H 70 73 132100.74907 126473.190 4.26% 387 9s\n",
"H 74 91 132099.47785 126473.190 4.26% 389 9s\n",
"H 78 91 132098.71285 126473.190 4.26% 397 9s\n",
"H 100 103 132097.98574 126473.190 4.26% 393 9s\n",
" 102 111 126797.575 12 548 132097.986 126473.190 4.26% 397 10s\n",
"H 107 111 132097.53363 126473.190 4.26% 397 10s\n",
"H 108 111 132066.71259 126473.190 4.24% 396 10s\n",
"H 113 122 131722.50062 126473.190 3.99% 394 10s\n",
"H 135 148 131533.92609 126473.190 3.85% 389 10s\n",
"H 147 156 131532.80521 126473.190 3.85% 371 10s\n",
"H 192 201 131532.28920 126473.190 3.85% 350 11s\n",
"H 239 242 131526.97267 126473.190 3.84% 328 12s\n",
"H 289 295 131526.91727 126473.190 3.84% 312 14s\n",
" 337 367 127924.573 32 452 131526.917 126473.190 3.84% 289 15s\n",
"H 353 367 131492.15273 126473.190 3.82% 281 15s\n",
"H 396 404 131489.68530 126473.190 3.82% 267 15s\n",
"H 486 495 130987.99225 126473.190 3.45% 244 16s\n",
"H 517 522 130421.69673 126473.190 3.03% 240 16s\n",
"H 522 535 130421.40259 126473.190 3.03% 239 17s\n",
"H 528 535 130421.07013 126473.190 3.03% 237 17s\n",
"H 529 535 130420.63186 126473.190 3.03% 238 17s\n",
"H 552 590 130183.15438 126473.190 2.85% 237 17s\n",
"H 633 620 130183.13248 126473.190 2.85% 224 18s\n",
"H 647 656 130182.59835 126473.190 2.85% 223 19s\n",
"H 689 652 129836.98743 126473.190 2.59% 215 19s\n",
"H 727 681 129835.86569 126473.190 2.59% 213 19s\n",
" 742 688 128394.541 67 385 129835.866 126473.190 2.59% 212 20s\n",
"H 744 688 129824.12067 126473.190 2.58% 211 20s\n",
"H 800 775 129823.58653 126473.190 2.58% 212 21s\n",
"H 1068 982 129822.97843 126473.190 2.58% 200 23s\n",
"H 1101 1067 129822.43901 126476.407 2.58% 199 23s\n",
"H 1107 1067 129822.22532 126476.407 2.58% 199 23s\n",
"H 1183 1075 129812.75274 126476.407 2.57% 197 24s\n",
" 1191 1141 126761.012 10 555 129812.753 126476.407 2.57% 196 25s\n",
"H 1194 1141 129809.57955 126476.407 2.57% 196 25s\n",
"H 1212 1141 129809.41873 126476.407 2.57% 196 25s\n",
"H 1243 1133 129785.07555 126476.407 2.55% 195 25s\n",
"H 1261 1279 129783.61579 126476.407 2.55% 194 25s\n",
"H 1427 1429 129783.19459 126476.407 2.55% 189 25s\n",
"H 1444 1429 129781.71314 126476.407 2.55% 188 25s\n",
"H 1509 1429 129780.91265 126476.407 2.55% 183 25s\n",
"H 1509 1429 129780.20513 126476.407 2.55% 183 25s\n",
"H 1619 1429 129779.86058 126496.487 2.53% 178 25s\n",
"H 1619 1428 129768.11557 126496.487 2.52% 178 26s\n",
"H 1625 1360 129536.13568 126496.487 2.35% 178 27s\n",
"H 1636 1299 129535.84482 126514.366 2.33% 176 28s\n",
" 1650 1308 128970.574 38 639 129535.845 126571.633 2.29% 175 30s\n",
" 1652 1309 129313.722 47 587 129535.845 126575.279 2.29% 175 35s\n",
"H 1873 1413 129535.13967 126617.766 2.25% 222 39s\n",
" 1947 1457 127106.865 26 570 129535.140 126617.766 2.25% 231 40s\n",
" 2182 1650 127288.124 32 458 129535.140 126617.766 2.25% 250 45s\n",
" 2449 1775 128058.955 39 429 129535.140 126624.389 2.25% 261 50s\n",
" 3034 2020 128792.887 58 378 129535.140 126642.660 2.23% 258 55s\n",
" 4124 2528 127170.627 46 497 129535.140 126644.291 2.23% 244 60s\n",
" 4970 2985 128283.078 126 412 129535.140 126644.291 2.23% 243 65s\n",
" 5206 3176 128311.710 131 425 129535.140 126644.291 2.23% 241 70s\n",
" 6810 4813 127692.463 58 467 129535.140 126663.451 2.22% 229 75s\n",
"H 7758 5306 129476.29287 126663.451 2.17% 227 77s\n",
" 8682 6097 126776.184 27 529 129476.293 126663.451 2.17% 229 81s\n",
" 9398 6421 129032.023 49 388 129476.293 126663.451 2.17% 229 85s\n",
" 10079 6940 127186.904 41 517 129476.293 126663.451 2.17% 224 90s\n",
"\n",
"Cutting planes:\n",
" Gomory: 65\n",
" Lift-and-project: 18\n",
" Cover: 5\n",
" MIR: 487\n",
" StrongCG: 15\n",
" Flow cover: 372\n",
" Flow path: 7\n",
" GUB cover: 4\n",
" Zero half: 2\n",
" Mod-K: 1\n",
" Network: 28\n",
" RLT: 4\n",
" Relax-and-lift: 12\n",
"\n",
"Explored 10213 nodes (2309181 simplex iterations) in 90.02 seconds (99.60 work units)\n",
"Thread count was 16 (of 16 available processors)\n",
"\n",
"Solution count 10: 129476 129535 129536 ... 129783\n",
"\n",
"Time limit reached\n",
"Best objective 1.294762928720e+05, best bound 1.266634506565e+05, gap 2.1725%\n"
]
},
{
"data": {
"text/plain": [
"SolutionInfo(runtime=90.02284598350525, bound=126663.45065652042, objective=129476.29287204682, relgap=0.02172476638874854, termination='maxTimeLimit')"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solver.solve(\n",
" mip_gap=0.005,\n",
" time_limit=90,\n",
" verbose=True,\n",
")"
]
},
{
"cell_type": "markdown",
"id": "1dbc6982-18fe-447e-b6d0-935f491af2dc",
"metadata": {},
"source": [
"OptiWindNet uses pyomo's `gurobi_persistent` interface with `manage_env=True`, so:\n",
"- the Gurobi license is held only between `set_problem()` and `get_solution()`, then released\n",
"- options that must be set at Gurobi environment creation (such as `threadlimit`) can be passed by assigning them to `solver.options` *before* calling `set_problem()`\n",
"\n",
"Example:\n",
"```python\n",
"solver.options.update(threadlimit=8)\n",
"solver.set_problem(...)\n",
"solver.solve(...)\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "d43bf79b-2403-4c1d-a4fe-39b1a1fed38f",
"metadata": {},
"outputs": [],
"source": [
"S, G = solver.get_solution()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "662ca8b3-d5ea-4231-9942-80b5f8eddc4f",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"svgplot(G)"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}