{
"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 EW_presolver\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": [
""
],
"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": "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-25 | 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 93274.5215.\n",
"Tried aggregator 1 time.\n",
"MIP Presolve eliminated 368 rows and 0 columns.\n",
"MIP Presolve modified 1040 coefficients.\n",
"Reduced MIP has 2740 rows, 2080 columns, and 9890 nonzeros.\n",
"Reduced MIP has 1040 binaries, 1040 generals, 0 SOSs, and 0 indicators.\n",
"Presolve time = 0.00 sec. (7.87 ticks)\n",
"Probing time = 0.01 sec. (2.90 ticks)\n",
"Tried aggregator 1 time.\n",
"Detecting symmetries...\n",
"Reduced MIP has 2740 rows, 2080 columns, and 9890 nonzeros.\n",
"Reduced MIP has 1040 binaries, 1040 generals, 0 SOSs, and 0 indicators.\n",
"Presolve time = 0.00 sec. (6.22 ticks)\n",
"Probing time = 0.02 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. (36.66 ticks)\n",
"\n",
" Nodes Cuts/\n",
" Node Left Objective IInf Best Integer Best Bound ItCnt Gap\n",
"\n",
"* 0+ 0 93274.5215 0.0000 100.00%\n",
" 0 0 85779.2773 106 93274.5215 85779.2773 1392 8.04%\n",
"* 0+ 0 91529.1336 85779.2773 6.28%\n",
" 0 0 86296.5926 291 91529.1336 Cuts: 112 1615 5.72%\n",
" 0 0 86449.7008 233 91529.1336 Cuts: 113 1773 5.55%\n",
"* 0+ 0 90212.0836 86449.7008 4.17%\n",
" 0 0 86530.8140 310 90212.0836 Cuts: 44 1854 4.08%\n",
" 0 0 86591.5711 260 90212.0836 Cuts: 45 1935 4.01%\n",
" 0 0 86613.4710 293 90212.0836 Cuts: 25 2000 3.99%\n",
"Detecting symmetries...\n",
" 0 0 86630.5237 312 90212.0836 Cuts: 27 2054 3.97%\n",
"* 0+ 0 89811.3539 86630.5237 3.54%\n",
" 0 0 86656.5904 314 89811.3539 Cuts: 26 2131 3.51%\n",
" 0 0 86668.0809 308 89811.3539 Cuts: 23 2187 3.50%\n",
"* 0+ 0 88489.5866 86668.0809 2.06%\n",
" 0 0 -1.00000e+75 0 88489.5866 86668.0809 2187 2.06%\n",
" 0 0 86688.9715 323 88489.5866 Cuts: 29 2250 2.03%\n",
" 0 0 86728.0362 292 88489.5866 Cuts: 13 2310 1.99%\n",
" 0 0 86743.6061 316 88489.5866 Cuts: 25 2380 1.97%\n",
" 0 0 86755.7227 324 88489.5866 Cuts: 31 2446 1.96%\n",
" 0 0 86758.6886 318 88489.5866 Cuts: 21 2468 1.96%\n",
" 0 0 86761.2511 312 88489.5866 Cuts: 19 2495 1.95%\n",
"* 0+ 0 88420.3525 86761.2511 1.88%\n",
"* 0+ 0 88331.6140 86761.2511 1.78%\n",
" 0 0 -1.00000e+75 0 88331.6140 86761.2511 2495 1.78%\n",
"Detecting symmetries...\n",
" 0 2 86761.2511 312 88331.6140 86761.2511 2495 1.78%\n",
"Elapsed time = 2.92 sec. (1626.09 ticks, tree = 0.02 MB, solutions = 7)\n",
" 219 169 86992.6114 291 88331.6140 86797.0783 28596 1.74%\n",
" 552 393 87304.9614 263 88331.6140 86797.0783 62224 1.74%\n",
" 952 567 87938.1457 193 88331.6140 86895.1664 83205 1.63%\n",
"* 1398+ 807 88167.0731 86946.6821 1.38%\n",
" 1453 935 87450.9176 219 88167.0731 86946.6821 123491 1.38%\n",
"* 1792+ 1113 87993.7803 86958.9363 1.18%\n",
" 1853 1135 cutoff 87993.7803 86958.9363 155644 1.18%\n",
" 2219 1129 87094.4152 312 87993.7803 87006.4361 182773 1.12%\n",
" 2709 1512 87584.8536 287 87993.7803 87067.9193 238180 1.05%\n",
" 3129 1670 87323.2703 249 87993.7803 87082.0990 255155 1.04%\n",
" 3508 1970 87973.1209 233 87993.7803 87082.0990 288727 1.04%\n",
"* 4582+ 2467 87938.3833 87144.1873 0.90%\n",
"\n",
"Performing restart 1\n",
"\n",
"Repeating presolve.\n",
"Tried aggregator 2 times.\n",
"MIP Presolve eliminated 381 rows and 350 columns.\n",
"MIP Presolve modified 135 coefficients.\n",
"Aggregator did 2 substitutions.\n",
"Reduced MIP has 2357 rows, 1728 columns, and 8268 nonzeros.\n",
"Reduced MIP has 868 binaries, 860 generals, 0 SOSs, and 0 indicators.\n",
"Presolve time = 0.01 sec. (6.21 ticks)\n",
"Tried aggregator 1 time.\n",
"Reduced MIP has 2357 rows, 1728 columns, and 8268 nonzeros.\n",
"Reduced MIP has 868 binaries, 860 generals, 0 SOSs, and 0 indicators.\n",
"Presolve time = 0.00 sec. (5.54 ticks)\n",
"Represolve time = 0.05 sec. (28.48 ticks)\n",
" 4589 0 86847.0677 351 87938.3833 Cuts: 116 394059 0.88%\n",
" 4589 0 86872.8665 342 87938.3833 Cuts: 65 394171 0.88%\n",
"* 4589+ 0 87802.9883 87162.8768 0.73%\n",
" 4589 0 86892.1240 363 87802.9883 Cuts: 100 394292 0.73%\n",
" 4589 0 86908.8440 365 87802.9883 Cuts: 81 394425 0.73%\n",
" 4589 0 86925.2403 374 87802.9883 Cuts: 105 394559 0.73%\n",
" 4589 0 86935.4419 375 87802.9883 Cuts: 68 394680 0.73%\n",
" 4589 16 cutoff 87802.9883 87222.3496 399510 0.66%\n",
"Elapsed time = 11.91 sec. (6659.73 ticks, tree = 0.02 MB, solutions = 12)\n",
" 4940 13 87517.9866 370 87802.9883 87233.7246 397451 0.65%\n",
" 5074 28 cutoff 87802.9883 87233.7246 402592 0.65%\n",
"\n",
"GUB cover cuts applied: 6\n",
"Cover cuts applied: 1\n",
"Flow cuts applied: 44\n",
"Mixed integer rounding cuts applied: 105\n",
"Zero-half cuts applied: 47\n",
"Lift and project cuts applied: 62\n",
"Gomory fractional cuts applied: 5\n",
"\n",
"Root node processing (before b&c):\n",
" Real time = 2.80 sec. (1624.64 ticks)\n",
"Parallel b&c, 16 threads:\n",
" Real time = 10.25 sec. (7660.06 ticks)\n",
" Sync time (average) = 1.63 sec.\n",
" Wait time (average) = 0.01 sec.\n",
" ------------\n",
"Total (root+branch&cut) = 13.05 sec. (9284.70 ticks)\n"
]
},
{
"data": {
"text/plain": [
"SolutionInfo(runtime=13.05971646308899, bound=87628.45738695332, objective=87802.98827587758, relgap=0.001987755682937409, 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": [
""
],
"text/plain": [
""
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"svgplot(G)"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}