{
"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 EW_presolver\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ʹ = EW_presolver(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": [],
"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 OutputFlag to value 1\n",
"Gurobi Optimizer version 12.0.2 build v12.0.2rc0 (win64 - Windows 11.0 (26100.2))\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\n",
"Model fingerprint: 0x53addd12\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 138349\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, 4568 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 138349.264 125228.749 9.48% - 0s\n",
"H 0 0 138342.10021 125228.749 9.48% - 0s\n",
"H 0 0 138330.82125 125228.749 9.47% - 0s\n",
"H 0 0 138327.82020 125228.749 9.47% - 0s\n",
"H 0 0 136182.69132 125228.749 8.04% - 0s\n",
"H 0 0 136181.59500 125228.749 8.04% - 0s\n",
"H 0 0 136181.41684 125228.749 8.04% - 0s\n",
"H 0 0 136179.51101 125228.749 8.04% - 0s\n",
"H 0 0 136178.70664 125228.749 8.04% - 0s\n",
"H 0 0 136178.41578 125228.749 8.04% - 0s\n",
"H 0 0 135970.93850 125228.749 7.90% - 0s\n",
"H 0 0 135958.34540 125228.749 7.89% - 0s\n",
"H 0 0 135110.24716 125228.749 7.31% - 0s\n",
"H 0 0 135109.44597 125228.749 7.31% - 0s\n",
"H 0 0 135108.83583 125653.007 7.00% - 0s\n",
" 0 0 125653.007 0 472 135108.836 125653.007 7.00% - 0s\n",
"H 0 0 135108.27410 125653.007 7.00% - 0s\n",
"H 0 0 134876.80981 125653.007 6.84% - 0s\n",
"H 0 0 134863.96445 125653.007 6.83% - 0s\n",
"H 0 0 134340.42058 125653.007 6.47% - 0s\n",
"H 0 0 134339.61798 125653.007 6.47% - 0s\n",
"H 0 0 134339.59465 125653.007 6.47% - 0s\n",
"H 0 0 134339.46458 125653.007 6.47% - 0s\n",
"H 0 0 134339.33059 125653.007 6.47% - 0s\n",
" 0 0 125717.537 0 505 134339.331 125717.537 6.42% - 0s\n",
" 0 0 125720.502 0 519 134339.331 125720.502 6.42% - 0s\n",
" 0 0 125720.732 0 514 134339.331 125720.732 6.42% - 0s\n",
" 0 0 125720.733 0 514 134339.331 125720.733 6.42% - 0s\n",
" 0 0 126097.783 0 537 134339.331 126097.783 6.13% - 1s\n",
" 0 0 126110.609 0 571 134339.331 126110.609 6.13% - 1s\n",
" 0 0 126117.238 0 543 134339.331 126117.238 6.12% - 1s\n",
"H 0 0 134165.85548 126118.406 6.00% - 1s\n",
"H 0 0 134165.36007 126118.406 6.00% - 1s\n",
"H 0 0 134165.07878 126118.406 6.00% - 1s\n",
"H 0 0 134164.27580 126118.406 6.00% - 1s\n",
" 0 0 126118.406 0 563 134164.276 126118.406 6.00% - 1s\n",
" 0 0 126118.609 0 575 134164.276 126118.609 6.00% - 1s\n",
" 0 0 126118.657 0 589 134164.276 126118.657 6.00% - 1s\n",
"H 0 0 134164.25391 126118.657 6.00% - 1s\n",
"H 0 0 134163.68288 126118.657 6.00% - 1s\n",
" 0 0 126205.700 0 575 134163.683 126205.700 5.93% - 1s\n",
"H 0 0 134163.12114 126205.700 5.93% - 1s\n",
"H 0 0 134148.29846 126205.700 5.92% - 1s\n",
" 0 0 126248.160 0 508 134148.298 126248.160 5.89% - 1s\n",
" 0 0 126261.133 0 557 134148.298 126261.133 5.88% - 1s\n",
"H 0 0 133589.97784 126271.379 5.48% - 2s\n",
" 0 0 126271.379 0 564 133589.978 126271.379 5.48% - 2s\n",
"H 0 0 133233.28481 126272.166 5.22% - 2s\n",
"H 0 0 133233.27026 126272.166 5.22% - 2s\n",
" 0 0 126272.166 0 581 133233.270 126272.166 5.22% - 2s\n",
" 0 0 126273.676 0 578 133233.270 126273.676 5.22% - 2s\n",
" 0 0 126273.697 0 589 133233.270 126273.697 5.22% - 2s\n",
"H 0 0 133233.20449 126273.697 5.22% - 2s\n",
"H 0 0 133152.11368 126273.697 5.17% - 2s\n",
"H 0 0 132980.56962 126273.697 5.04% - 2s\n",
"H 0 0 132979.76664 126273.697 5.04% - 2s\n",
"H 0 0 132978.53079 126313.751 5.01% - 2s\n",
"H 0 0 132978.30401 126313.751 5.01% - 2s\n",
" 0 0 126313.751 0 625 132978.304 126313.751 5.01% - 2s\n",
" 0 0 126338.310 0 614 132978.304 126338.310 4.99% - 3s\n",
" 0 0 126342.390 0 591 132978.304 126342.390 4.99% - 3s\n",
" 0 0 126344.028 0 620 132978.304 126344.028 4.99% - 3s\n",
" 0 0 126344.711 0 631 132978.304 126344.711 4.99% - 3s\n",
" 0 0 126344.726 0 632 132978.304 126344.726 4.99% - 3s\n",
" 0 0 126369.503 0 587 132978.304 126369.503 4.97% - 3s\n",
"H 0 0 132977.65628 126369.503 4.97% - 3s\n",
"H 0 0 132977.11832 126369.503 4.97% - 3s\n",
"H 0 0 132967.88839 126369.516 4.96% - 4s\n",
"H 0 0 132965.49301 126369.516 4.96% - 4s\n",
"H 0 0 132623.38745 126369.516 4.72% - 4s\n",
"H 0 0 132479.10566 126369.516 4.61% - 5s\n",
"H 0 0 132297.13443 126369.516 4.48% - 5s\n",
"H 0 2 131714.07279 126369.516 4.06% - 8s\n",
" 0 2 126369.516 0 587 131714.073 126369.516 4.06% - 8s\n",
"H 6 8 131482.60974 126373.933 3.89% 247 8s\n",
"H 15 24 131278.73387 126408.816 3.71% 452 9s\n",
"H 39 48 131276.18669 126418.723 3.70% 409 9s\n",
"H 44 48 131234.07918 126418.723 3.67% 425 9s\n",
" 47 64 126456.910 8 591 131234.079 126418.723 3.67% 425 10s\n",
"H 73 83 131233.00075 126418.723 3.67% 412 10s\n",
"H 128 139 131232.65678 126418.723 3.67% 369 11s\n",
"H 159 176 131038.66079 126418.723 3.53% 354 11s\n",
"H 170 176 130995.66800 126418.723 3.49% 347 11s\n",
"H 173 176 130993.85253 126418.723 3.49% 346 11s\n",
"H 246 251 130993.78530 126418.723 3.49% 319 12s\n",
"H 293 302 130964.60045 126418.723 3.47% 303 13s\n",
"H 301 340 130964.55511 126418.723 3.47% 303 13s\n",
"H 344 348 130963.14084 126418.723 3.47% 291 13s\n",
"H 409 415 130732.39791 126418.723 3.30% 275 13s\n",
" 636 654 129251.361 52 362 130732.398 126418.723 3.30% 240 15s\n",
"H 1160 1106 130731.76770 126422.283 3.30% 218 17s\n",
"H 1462 1283 130144.39448 126422.283 2.86% 211 18s\n",
"H 1544 1364 130144.09499 126422.283 2.86% 208 19s\n",
"H 1544 1364 130143.90418 126422.283 2.86% 208 19s\n",
" 1608 1365 129454.379 56 587 130143.904 126430.386 2.85% 205 20s\n",
" 1635 1383 127372.764 18 666 130143.904 126515.770 2.79% 201 25s\n",
" 1636 1384 127954.986 32 666 130143.904 126515.772 2.79% 201 31s\n",
" 1790 1519 126868.993 19 680 130143.904 126555.396 2.76% 233 35s\n",
" 2258 1815 127063.656 31 568 130143.904 126555.396 2.76% 249 40s\n",
" 2783 2142 128768.796 51 432 130143.904 126563.163 2.75% 241 45s\n",
" 3478 2541 127594.686 54 515 130143.904 126566.262 2.75% 236 50s\n",
" 4531 2924 126789.247 27 606 130143.904 126580.201 2.74% 222 55s\n",
"H 4612 2847 130059.15017 126583.968 2.67% 222 57s\n",
" 5213 3287 128033.830 64 504 130059.150 126591.332 2.67% 217 60s\n",
"H 5722 3132 129520.77078 126592.088 2.26% 209 62s\n",
" 6355 3897 128881.490 76 462 129520.771 126592.088 2.26% 204 65s\n",
" 7642 4815 128553.046 48 439 129520.771 126592.088 2.26% 200 70s\n",
" 8314 5267 127150.667 27 479 129520.771 126592.323 2.26% 197 76s\n",
"H 8325 4957 129226.51179 126592.323 2.04% 197 76s\n",
"H 8339 4932 129200.19218 126592.323 2.02% 197 77s\n",
" 8374 4961 127312.408 33 427 129200.192 126592.323 2.02% 197 80s\n",
"H 8457 4623 128899.33531 126592.323 1.79% 196 84s\n",
" 8458 5001 127341.027 35 434 128899.335 126592.323 1.79% 196 86s\n",
"H 8556 4869 128807.18580 126592.323 1.72% 195 86s\n",
"H 8982 5087 128806.91198 126599.119 1.71% 194 90s\n",
"\n",
"Cutting planes:\n",
" Gomory: 45\n",
" Lift-and-project: 3\n",
" Cover: 3\n",
" MIR: 451\n",
" StrongCG: 17\n",
" Flow cover: 354\n",
" Flow path: 11\n",
" GUB cover: 2\n",
" Mod-K: 3\n",
" Network: 32\n",
" Relax-and-lift: 11\n",
"\n",
"Explored 9242 nodes (1807072 simplex iterations) in 90.14 seconds (88.39 work units)\n",
"Thread count was 16 (of 16 available processors)\n",
"\n",
"Solution count 10: 128807 128807 128899 ... 130144\n",
"\n",
"Time limit reached\n",
"Best objective 1.288069119839e+05, best bound 1.265991192522e+05, gap 1.7140%\n",
"WARNING: Loading a SolverResults object with an 'aborted' status, but\n",
"containing a solution\n"
]
},
{
"data": {
"text/plain": [
"SolutionInfo(runtime=90.14299988746643, bound=126599.11925224712, objective=128806.91198386771, relgap=0.01714032808966881, 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": [
"The pyomo GurobiDirect class can work as context manager by passing the `manage_env=True` parameter to SolverFactory. Advantages of operating this way:\n",
"- the Gurobi license is released after the context's scope ends\n",
"- one can pass `options` that can only be set at the Gurobi environment creation (such as `threadlimit`)"
]
},
{
"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
}