{ "cells": [ { "cell_type": "markdown", "id": "5956c285-030d-4377-8f7b-936b05e62e33", "metadata": {}, "source": [ "## CPLEX 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": "eb29279f-9923-4047-889c-579ea970e51f", "metadata": {}, "source": [ "### Initialize Race Bank 2" ] }, { "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.race\n", "capacity = 5" ] }, { "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": "967172f9-04cb-4efc-8ac2-ebb3433d5a69", "metadata": {}, "source": [ "### Optimize Race Bank 2" ] }, { "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": [ "Σλ = 93 901 m(+3) South: 13, North: 9κ = 5, T = 91" ], "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": "5b56d961-a8e3-4d00-bd77-3065c875f39f", "metadata": {}, "outputs": [], "source": [ "solver = solver_factory('cplex')" ] }, { "cell_type": "code", "execution_count": 8, "id": "f3fbd3d9-b7c0-45cb-9219-16efc3691fc5", "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": "7b0ee0ba-361d-4528-bb32-5da5fee263b9", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Version identifier: 22.1.2.0 | 2024-11-26 | 0edbb82fd\n", "CPXPARAM_Read_DataCheck 1\n", "CPXPARAM_Parallel -1\n", "CPXPARAM_Emphasis_MIP 4\n", "CPXPARAM_TimeLimit 20\n", "CPXPARAM_MIP_Tolerances_MIPGap 0.002\n", "1 of 1 MIP starts provided solutions.\n", "MIP start 'm1' defined initial solution with objective 93901.0670.\n", "Tried aggregator 1 time.\n", "MIP Presolve eliminated 366 rows and 0 columns.\n", "MIP Presolve modified 1038 coefficients.\n", "Reduced MIP has 2736 rows, 2076 columns, and 9870 nonzeros.\n", "Reduced MIP has 1038 binaries, 1038 generals, 0 SOSs, and 0 indicators.\n", "Presolve time = 0.01 sec. (7.85 ticks)\n", "Probing time = 0.00 sec. (2.89 ticks)\n", "Tried aggregator 1 time.\n", "Detecting symmetries...\n", "Reduced MIP has 2736 rows, 2076 columns, and 9870 nonzeros.\n", "Reduced MIP has 1038 binaries, 1038 generals, 0 SOSs, and 0 indicators.\n", "Presolve time = 0.01 sec. (6.20 ticks)\n", "Probing time = 0.00 sec. (2.83 ticks)\n", "Clique table members: 476.\n", "MIP emphasis: hidden feasible solutions.\n", "MIP search method: dynamic search.\n", "Parallel mode: opportunistic, using up to 16 threads.\n", "Root relaxation solution time = 0.03 sec. (32.69 ticks)\n", "\n", " Nodes Cuts/\n", " Node Left Objective IInf Best Integer Best Bound ItCnt Gap\n", "\n", "* 0+ 0 93901.0670 0.0000 100.00%\n", " 0 0 85779.2773 106 93901.0670 85779.2773 1375 8.65%\n", "* 0+ 0 91566.2815 85779.2773 6.32%\n", " 0 0 86250.4617 268 91566.2815 Cuts: 119 1577 5.81%\n", " 0 0 86422.4029 276 91566.2815 Cuts: 102 1724 5.62%\n", "* 0+ 0 90436.7369 86422.4029 4.44%\n", " 0 0 86503.3039 278 90436.7369 Cuts: 47 1819 4.35%\n", " 0 0 86544.1957 271 90436.7369 Cuts: 39 1881 4.30%\n", " 0 0 86579.1952 288 90436.7369 Cuts: 27 1951 4.27%\n", "Detecting symmetries...\n", " 0 0 86592.7377 278 90436.7369 Cuts: 21 1998 4.25%\n", " 0 0 86619.5861 301 90436.7369 Cuts: 18 2046 4.22%\n", " 0 0 86633.8326 254 90436.7369 Cuts: 20 2088 4.21%\n", " 0 0 86639.0729 247 90436.7369 Cuts: 14 2130 4.20%\n", " 0 0 86643.9394 238 90436.7369 Cuts: 17 2146 4.19%\n", "* 0+ 0 90184.6613 86643.9394 3.93%\n", "* 0+ 0 89689.8969 86643.9394 3.40%\n", "* 0+ 0 89335.9763 86643.9394 3.01%\n", " 0 0 -1.00000e+75 0 89335.9763 86643.9394 2146 3.01%\n", "Detecting symmetries...\n", " 0 2 86643.9394 238 89335.9763 86643.9394 2146 3.01%\n", "Elapsed time = 1.02 sec. (685.32 ticks, tree = 0.02 MB, solutions = 6)\n", "* 79+ 13 89249.0347 86647.6587 2.91%\n", " 536 388 86734.1351 293 89249.0347 86698.2007 28641 2.86%\n", " 1399 856 88464.1348 128 88763.3852 86760.4828 68500 2.26%\n", "* 1413+ 951 88618.2794 86760.4828 2.10%\n", " 2242 949 87417.8676 269 88261.6166 86783.2962 92793 1.67%\n", " 3204 1663 87263.8740 208 88261.6166 86878.9561 129116 1.57%\n", " 4187 2606 cutoff 88261.6166 86884.4290 174750 1.56%\n", "* 4553+ 2617 88157.0288 86884.4290 1.44%\n", " 4606 2718 87140.7204 224 88157.0288 86884.4290 179231 1.44%\n", "\n", "Performing restart 1\n", "\n", "Repeating presolve.\n", "Tried aggregator 2 times.\n", "MIP Presolve eliminated 229 rows and 221 columns.\n", "MIP Presolve modified 25 coefficients.\n", "Aggregator did 1 substitutions.\n", "Reduced MIP has 2506 rows, 1854 columns, and 8944 nonzeros.\n", "Reduced MIP has 928 binaries, 926 generals, 0 SOSs, and 0 indicators.\n", "Presolve time = 0.01 sec. (6.08 ticks)\n", "Tried aggregator 1 time.\n", "Reduced MIP has 2506 rows, 1854 columns, and 8944 nonzeros.\n", "Reduced MIP has 928 binaries, 926 generals, 0 SOSs, and 0 indicators.\n", "Presolve time = 0.01 sec. (5.94 ticks)\n", "Represolve time = 0.03 sec. (28.09 ticks)\n", "* 4609+ 0 87938.3833 86913.5818 1.17%\n", " 4609 0 86769.4075 318 87938.3833 Cuts: 107 201604 1.17%\n", " 4609 0 86825.0546 362 87938.3833 Cuts: 104 201776 1.17%\n", " 4609 0 86846.1193 341 87938.3833 Cuts: 87 201889 1.17%\n", " 4609 0 86873.0659 342 87938.3833 Cuts: 79 202026 1.17%\n", " 4609 0 86956.4591 348 87938.3833 Cuts: 109 202243 0.90%\n", " 4609 0 86992.1332 372 87938.3833 Cuts: 114 202416 0.90%\n", " 4609 0 87007.2847 373 87938.3833 Cuts: 113 202578 0.78%\n", " 4609 0 87025.0635 392 87938.3833 Cuts: 100 202668 0.78%\n", " 4609 0 87049.3410 373 87938.3833 Cuts: 67 202790 0.69%\n", " 4609 0 87054.9770 373 87938.3833 Cuts: 64 202883 0.69%\n", " 4609 0 87060.2946 376 87938.3833 Cuts: 42 202983 0.69%\n", "* 4609+ 0 87835.9279 87398.6964 0.50%\n", "* 4609+ 0 87819.1346 87398.6964 0.48%\n", " 4609 0 -1.00000e+75 0 87819.1346 87398.6964 202983 0.48%\n", " 4609 2 87060.2946 376 87819.1346 87464.1843 202983 0.40%\n", " 4721 22 cutoff 87802.9883 87464.1843 208580 0.39%\n", " 4924 281 87544.7773 300 87802.9883 87464.1843 274674 0.39%\n", " 5467 278 cutoff 87802.9883 87464.1843 274797 0.39%\n", "Elapsed time = 10.03 sec. (7112.22 ticks, tree = 0.68 MB, solutions = 21)\n", "\n", "GUB cover cuts applied: 3\n", "Cover cuts applied: 1\n", "Flow cuts applied: 40\n", "Mixed integer rounding cuts applied: 141\n", "Zero-half cuts applied: 42\n", "Lift and project cuts applied: 44\n", "Gomory fractional cuts applied: 8\n", "\n", "Root node processing (before b&c):\n", " Real time = 0.97 sec. (684.15 ticks)\n", "Parallel b&c, 16 threads:\n", " Real time = 9.07 sec. (6523.03 ticks)\n", " Sync time (average) = 1.01 sec.\n", " Wait time (average) = 0.01 sec.\n", " ------------\n", "Total (root+branch&cut) = 10.03 sec. (7207.18 ticks)\n" ] }, { "data": { "text/plain": [ "SolutionInfo(runtime=10.041604042053223, bound=87627.39352862877, objective=87802.98827587762, relgap=0.0019998721079643733, termination='optimal')" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "solver.solve(\n", " mip_gap=0.002,\n", " time_limit=20,\n", " verbose=True,\n", ")" ] }, { "cell_type": "code", "execution_count": 10, "id": "e036545f-9954-40d0-aec3-be4cff555471", "metadata": {}, "outputs": [], "source": [ "S, G = solver.get_solution()" ] }, { "cell_type": "code", "execution_count": 11, "id": "9bd07f6e-e819-46ac-9def-33a9082ba339", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "Σλ = 87 803 m(+0) South: 10, North: 9κ = 5, T = 91" ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "svgplot(G)" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }