{
"cells": [
{
"cell_type": "markdown",
"id": "5f6be665-2ab0-404e-b4f1-68e882887775",
"metadata": {
"deletable": true,
"editable": true,
"frozen": false
},
"source": [
"## SCIP example"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "122bc4f2-e6a0-4116-ba3b-6bea5bb92e07",
"metadata": {
"deletable": true,
"editable": true,
"frozen": false
},
"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": "dd5d2195-9b34-4aed-bd09-8f52116d098a",
"metadata": {
"deletable": true,
"editable": true,
"frozen": false
},
"source": [
"### Initialize Sheringham Shoal"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "29a6b5d1-a05f-4112-bf66-462b4d36b516",
"metadata": {
"deletable": true,
"editable": true,
"frozen": false
},
"outputs": [],
"source": [
"locations = load_repository()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6493ef36-765a-4cd3-9dc8-f2d952a23573",
"metadata": {
"deletable": true,
"editable": true,
"frozen": false
},
"outputs": [],
"source": [
"L = locations.sheringham\n",
"capacity = 6"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "2a81cdd4-7be8-4da3-9b94-9799c6f1760b",
"metadata": {
"deletable": true,
"editable": true,
"frozen": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"svgplot(L)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "9db5a5fb-bc38-495a-893a-93567216e060",
"metadata": {
"deletable": true,
"editable": true,
"frozen": false
},
"outputs": [],
"source": [
"solver = solver_factory('scip')"
]
},
{
"cell_type": "markdown",
"id": "5996b343-8973-49cb-8e34-d4b1161d625e",
"metadata": {
"deletable": true,
"editable": true,
"frozen": false
},
"source": [
"### Optimize Sheringham Shoal"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "df05184d-7caa-4b34-810e-37c13a7a8092",
"metadata": {
"deletable": true,
"editable": true,
"frozen": false
},
"outputs": [],
"source": [
"P, A = make_planar_embedding(L)"
]
},
{
"cell_type": "markdown",
"id": "4f9b49b1-a597-4053-a3d1-f3de7bb8d164",
"metadata": {
"deletable": true,
"editable": true,
"frozen": false
},
"source": [
"Initial heuristic solution to warm-start the solver:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "19eb273d-f662-4851-bf09-e0ec532d7b8b",
"metadata": {
"deletable": true,
"editable": true,
"frozen": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"execution_count": 7,
"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": 8,
"id": "ac1e54f3-d627-4b89-97d7-fc29fd40dcd5",
"metadata": {
"deletable": true,
"editable": true,
"frozen": false
},
"outputs": [],
"source": [
"solver = solver_factory('scip')"
]
},
{
"cell_type": "markdown",
"id": "8c21a9a5-25de-4e2a-b1a8-171ec6041451",
"metadata": {
"deletable": true,
"editable": true,
"frozen": false
},
"source": [
"If SCIP was compiled with multi-threading capability, multiple concurrent solver instances are started. OptiWindNet's default options prioritize launching feasibility-focused instances, but the choice of emphasis can be tuned by setting different weights.\n",
"\n",
"OptiWindNet sets the maximum number of concurrent threads (`'parallel/maxnthreads'`) to the number of physical cores by default; assign a different value to `solver.options['parallel/maxnthreads']` before `set_problem()` to override. SCIP may still use a lower number than requested to limit RAM usage."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "9c697d44-2a8e-444d-8a2b-241ff8a6a485",
"metadata": {
"deletable": true,
"editable": true,
"frozen": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'parallel/maxnthreads': 8,\n",
" 'concurrent/scip-feas/prefprio': 0.6,\n",
" 'concurrent/scip/prefprio': 0.3,\n",
" 'concurrent/scip-cpsolver/prefprio': 0,\n",
" 'concurrent/scip-easycip/prefprio': 0,\n",
" 'concurrent/scip-opti/prefprio': 0}"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solver.options"
]
},
{
"cell_type": "markdown",
"id": "0f8e27e8-c184-405a-bc7a-15e35a3213cb",
"metadata": {
"deletable": true,
"editable": true,
"frozen": false
},
"source": [
"The latest [SCIP Optimization Suite precompiled packages](https://www.scipopt.org/index.php#download) include multi-threading, but the PySCIPOpt wheel must be built on top of the library distributed in that package ([see instructions](https://pyscipopt.readthedocs.io/en/latest/build.html)). Latest versions at the time of writing: SCIP 10.0.0, PySCIPOpt 6.0. The conda-forge `pyscipopt` was at version 5.6.0 and was not concurrency-enabled for all platforms at the time ([check latest version](https://anaconda.org/channels/conda-forge/packages/pyscipopt/overview))."
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "47da8738-3ef7-4163-8f0d-52e17c91b9e2",
"metadata": {
"deletable": true,
"editable": true,
"frozen": false
},
"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": 11,
"id": "32780153-9756-4599-a1ee-1ce7aae07849",
"metadata": {
"deletable": true,
"editable": true,
"frozen": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"initializing seeds to 1963210296 in concurrent solver 'scip-2'\n",
"initializing seeds to 1332858414 in concurrent solver 'scip-feas-1'\n",
"initializing seeds to 1541326760 in concurrent solver 'scip-feas-2'\n",
"initializing seeds to 247360965 in concurrent solver 'scip-feas-3'\n",
"initializing seeds to 387742462 in concurrent solver 'scip-feas-4'\n",
"initializing seeds to 520723434 in concurrent solver 'scip-feas-5'\n",
"initializing seeds to 1176648445 in concurrent solver 'scip-3'\n",
"starting solve in concurrent solver 'scip-1'\n",
"starting solve in concurrent solver 'scip-2'\n",
"starting solve in concurrent solver 'scip-feas-1'\n",
"starting solve in concurrent solver 'scip-feas-2'\n",
"starting solve in concurrent solver 'scip-feas-3'\n",
"starting solve in concurrent solver 'scip-feas-4'\n",
"starting solve in concurrent solver 'scip-feas-5'\n",
"starting solve in concurrent solver 'scip-3'\n",
" 60.0s: concurrent solver 'scip-2' stopped with status time limit reached\n",
" 60.0s: concurrent solver 'scip-3' stopped with status time limit reached\n",
" 60.0s: concurrent solver 'scip-feas-4' stopped with status time limit reached\n",
" 60.0s: concurrent solver 'scip-feas-1' stopped with status time limit reached\n",
" 60.0s: concurrent solver 'scip-feas-3' stopped with status time limit reached\n",
" 60.0s: concurrent solver 'scip-1' stopped with status time limit reached\n",
" 60.0s: concurrent solver 'scip-feas-2' stopped with status time limit reached\n",
" 60.0s: concurrent solver 'scip-feas-5' stopped with status time limit reached\n"
]
},
{
"data": {
"text/plain": [
"SolutionInfo(runtime=60.487473, bound=60497.94224317833, objective=62598.45029400818, relgap=0.033555272390359936, termination='timelimit')"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solver.solve(\n",
" mip_gap=0.005,\n",
" time_limit=60,\n",
")"
]
},
{
"cell_type": "markdown",
"id": "6352be11-094e-4b4b-a949-683a6e2eeaaa",
"metadata": {
"deletable": true,
"editable": true,
"frozen": false
},
"source": [
"> Note: If SCIP lacks multi-threading capability, a warning will be displayed and it will fall back to the single-threaded solver. The warning about the lack of concurrent capability looks like this:\n",
"```\n",
".../optiwindnet/MILP/scip.py:96: UserWarning: SCIP was compiled without task processing interface. Parallel solve not possible - using optimize() instead of solveConcurrent()\n",
" model.solveConcurrent()\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "edec0060-53b1-4faf-8682-c23916470040",
"metadata": {
"deletable": true,
"editable": true,
"frozen": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"S, G = solver.get_solution()\n",
"svgplot(G)"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}