Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hsteinshiromoto/20e5d8fa03362a6e12dd5d8cdc4165df to your computer and use it in GitHub Desktop.
Save hsteinshiromoto/20e5d8fa03362a6e12dd5d8cdc4165df to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "64aa77c1-1fa5-4d98-bf1e-9137d3326572",
"metadata": {},
"outputs": [],
"source": [
"# Import Solver\n",
"import gurobipy as gp\n",
"from gurobipy import GRB"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "6a5dde57-516c-4396-98c2-85b4c3d77e49",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Restricted license - for non-production use only - expires 2023-10-25\n"
]
}
],
"source": [
"# Instantiate the model\n",
"mip = gp.Model('example')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "198757c2-57d7-4386-b909-c10ae1edc2b8",
"metadata": {},
"outputs": [],
"source": [
"# Define the decision variables\n",
"x1 = mip.addVar(vtype=GRB.CONTINUOUS, name=\"x1\")\n",
"x2 = mip.addVar(vtype=GRB.CONTINUOUS, name=\"x2\")\n",
"x3 = mip.addVar(vtype=GRB.INTEGER, name=\"x3\")\n",
"x4 = mip.addVar(vtype=GRB.INTEGER, name=\"x4\")"
]
},
{
"cell_type": "markdown",
"id": "0114c207-0421-44f8-9a8a-9862e4b38de3",
"metadata": {},
"source": [
"The constraints are\n",
"\n",
"$$\\begin{array}{r}\n",
"x_1 + 2x_3 \\leq 700 \\\\\n",
"2x_2−8x_3 \\leq 0 \\\\\n",
"x_2−2x_3+x_4 \\geq 1 \\\\ \n",
"x_1 + x_2 + x_3 + x_4 = 10 \\\\\n",
"0 \\leq x_1 \\leq 10 \\\\\n",
"0 \\leq x_2 \\leq 10 \\\\\n",
"0 \\leq x_3 \\leq 10 \\\\\n",
"0 \\leq x_4 \\leq 10 \\\\\n",
"x_1, x_2\\in\\mathbb{R}, x_3,x_4\\in\\mathbb{Z}\n",
"\\end{array}$$"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "cfb68c1c-9978-4dc2-9a34-824c4cdad20e",
"metadata": {},
"outputs": [],
"source": [
"# Define the constraints\n",
"c1 = mip.addConstr(x1+2*x3 <= 700, name='c1')\n",
"c2 = mip.addConstr(2*x2-8*x3 <= 0, name='c2')\n",
"c3 = mip.addConstr(x2-2*x3+x4 >= 1, name='c3')\n",
"c4 = mip.addConstr(x1+x2+x3+x4 == 10, name='c4')\n",
"c5 = mip.addConstr(0 <= x1, name='c5')\n",
"c6 = mip.addConstr(0 <= x2, name='c6')\n",
"c7 = mip.addConstr(0 <= x3, name='c7')\n",
"c8 = mip.addConstr(0 <= x4, name='c8')\n",
"c9 = mip.addConstr(x1 <= 10, name='c9')\n",
"c10 = mip.addConstr(x2 <= 10, name='c10')\n",
"c11 = mip.addConstr(x3 <= 10, name='c11')\n",
"c12 = mip.addConstr(x4 <= 10, name='c12')"
]
},
{
"cell_type": "markdown",
"id": "0fb0edf7-0640-46bc-9bbf-013ddbf2f9b8",
"metadata": {},
"source": [
"The objective function is\n",
"\n",
"$$\\max x_1+x_2+2x_3-2x_4$$"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "2bb0271c-075d-49ae-9f25-e0f7fc730ff4",
"metadata": {},
"outputs": [],
"source": [
"# Define objective function\n",
"mip.setObjective(x1+x2+2*x3-2*x4, GRB.MAXIMIZE)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "e69d527f-f48d-4c16-a72f-d80fd51f27b2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Gurobi Optimizer version 9.5.0 build v9.5.0rc5 (linux64)\n",
"Thread count: 8 physical cores, 8 logical processors, using up to 8 threads\n",
"Optimize a model with 12 rows, 4 columns and 19 nonzeros\n",
"Model fingerprint: 0x46c3d51d\n",
"Variable types: 2 continuous, 2 integer (0 binary)\n",
"Coefficient statistics:\n",
" Matrix range [1e+00, 8e+00]\n",
" Objective range [1e+00, 2e+00]\n",
" Bounds range [0e+00, 0e+00]\n",
" RHS range [1e+00, 7e+02]\n",
"Presolve removed 9 rows and 1 columns\n",
"Presolve time: 0.00s\n",
"Presolved: 3 rows, 3 columns, 8 nonzeros\n",
"Variable types: 0 continuous, 3 integer (0 binary)\n",
"Found heuristic solution: objective 11.0000000\n",
"Found heuristic solution: objective 13.0000000\n",
"\n",
"Explored 0 nodes (0 simplex iterations) in 0.25 seconds (0.00 work units)\n",
"Thread count was 8 (of 8 available processors)\n",
"\n",
"Solution count 2: 13 11 \n",
"\n",
"Optimal solution found (tolerance 1.00e-04)\n",
"Best objective 1.300000000000e+01, best bound 1.300000000000e+01, gap 0.0000%\n"
]
}
],
"source": [
"# Solve the optimization problem\n",
"mip.optimize()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "754bf163-ace0-4452-9cdf-083e1f0b23a8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x1=0.0\n",
"x2=7.0\n",
"x3=3.0\n",
"x4=-0.0\n"
]
}
],
"source": [
"# Optimal values\n",
"print(f\"x1={x1.x}\")\n",
"print(f\"x2={x2.x}\")\n",
"print(f\"x3={x3.x}\")\n",
"print(f\"x4={x4.x}\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "6fde6bd0-516f-4df9-89e0-a652d512eca1",
"metadata": {},
"outputs": [],
"source": [
"# Save the optimization model\n",
"mip.write('example.lp')"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "fa4087be-7677-454f-a720-9209dc51d7f2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Last updated: Thu Dec 09 2021\n",
"\n",
"Python implementation: CPython\n",
"Python version : 3.9.9\n",
"IPython version : 7.27.0\n",
"\n",
"watermark: 2.2.0\n",
"gurobipy : 9.5.0\n",
"\n",
"Watermark: 2.2.0\n",
"\n"
]
}
],
"source": [
"%load_ext watermark\n",
"%watermark -n -u -v -iv -w"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment