Skip to content

Instantly share code, notes, and snippets.

@leobarone
Last active March 23, 2022 14:45
Show Gist options
  • Save leobarone/30d0a2c0c34fa5f486a37a6f5185b624 to your computer and use it in GitHub Desktop.
Save leobarone/30d0a2c0c34fa5f486a37a6f5185b624 to your computer and use it in GitHub Desktop.
linear_programming_ortools.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/leobarone/30d0a2c0c34fa5f486a37a6f5185b624/linear_programming_ortools.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"id": "f718ccbb-d2ce-4b82-8123-afddce958174",
"metadata": {
"id": "f718ccbb-d2ce-4b82-8123-afddce958174"
},
"source": [
"Import linear solver \"pywraplp\" from ortools."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b5d02ab6-c0ee-421c-92bc-aeccb0ad9cc1",
"metadata": {
"id": "b5d02ab6-c0ee-421c-92bc-aeccb0ad9cc1"
},
"outputs": [],
"source": [
"from ortools.linear_solver import pywraplp"
]
},
{
"cell_type": "markdown",
"id": "7ec059fa-e0ea-4c84-981e-4255e1803798",
"metadata": {
"id": "7ec059fa-e0ea-4c84-981e-4255e1803798"
},
"source": [
"Create a linear solver object."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "11f14f09-e5fc-4ef2-b5af-b1b9a0af6010",
"metadata": {
"id": "11f14f09-e5fc-4ef2-b5af-b1b9a0af6010"
},
"outputs": [],
"source": [
"# Create the linear solver with the SCIP backend.\n",
"solver = pywraplp.Solver.CreateSolver('SCIP')"
]
},
{
"cell_type": "markdown",
"id": "8d4b0087-dba4-42e0-8c89-4c6ca36d1bc3",
"metadata": {
"id": "8d4b0087-dba4-42e0-8c89-4c6ca36d1bc3"
},
"source": [
"Define a variable for infinity."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ecfc8ed2-52d4-403a-9cd6-ce14c6b09543",
"metadata": {
"tags": [],
"id": "ecfc8ed2-52d4-403a-9cd6-ce14c6b09543"
},
"outputs": [],
"source": [
"inf = solver.infinity()"
]
},
{
"cell_type": "markdown",
"id": "470092dc-acd9-43c3-a27a-746fae098457",
"metadata": {
"id": "470092dc-acd9-43c3-a27a-746fae098457"
},
"source": [
"Define the 3 variables of the problem, $x_1$, $x_2$ and $x_3$ as non-negative real numbers."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c34a0cd-5a0c-4d18-b06e-fca8b5a44219",
"metadata": {
"id": "1c34a0cd-5a0c-4d18-b06e-fca8b5a44219"
},
"outputs": [],
"source": [
"# Create the variables x1, x2 and x3.\n",
"x1 = solver.NumVar(0., inf, 'x1')\n",
"x2 = solver.NumVar(0., inf, 'x2')\n",
"x3 = solver.NumVar(0., inf, 'x3')"
]
},
{
"cell_type": "markdown",
"id": "7317fa70-1470-4a3a-8308-3771ceac75a4",
"metadata": {
"id": "7317fa70-1470-4a3a-8308-3771ceac75a4"
},
"source": [
"Write the four linear constraints of the problem\n",
"\n",
"$0.7 * x_1 + 0.8 * x_2 + 0 * x_3 >= 10$\n",
"\n",
"$0.9 * x_1 + 0.8 * x_2 + 0.8 * x_3 >= 12$\n",
"\n",
"$0.8 * x_1 + 1.5 * x_2 + 0.9 * x_3 >= 15$\n",
"\n",
"$0.5 * x_1 + 0.6 * x_2 + 0.4 * x_3 <= 7.5$"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1f00bfab-9f42-4c99-9df4-bb44dbf97c21",
"metadata": {
"id": "1f00bfab-9f42-4c99-9df4-bb44dbf97c21",
"outputId": "a2dc0048-b3b9-4787-827b-4e4240f1ecd3"
},
"outputs": [
{
"data": {
"text/plain": [
"<ortools.linear_solver.pywraplp.Constraint; proxy of <Swig Object of type 'operations_research::MPConstraint *' at 0x7f7758064720> >"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solver.Add(0.7 * x1 + 0.8 * x2 + 0 * x3 >= 10)\n",
"solver.Add(0.9 * x1 + 0.8 * x2 + 0.8 * x3 >= 12)\n",
"solver.Add(0.8 * x1 + 1.5 * x2 + 0.9 * x3 >= 15)\n",
"solver.Add(0.5 * x1 + 0.6 * x2 + 0.4 * x3 <= 7.5)"
]
},
{
"cell_type": "markdown",
"id": "8d9d8ef1-aef0-4fe1-ac60-e4fefe518975",
"metadata": {
"id": "8d9d8ef1-aef0-4fe1-ac60-e4fefe518975"
},
"source": [
"Write the objective function of the problem to be minimized:\n",
"\n",
"$0.25 * x_1 + 0.10 * x_2 + 0.08 * x_3$"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "006bd0f4-a8e2-4359-99a4-e3c2a73d26f5",
"metadata": {
"id": "006bd0f4-a8e2-4359-99a4-e3c2a73d26f5"
},
"outputs": [],
"source": [
"solver.Minimize(0.25 * x1 + 0.10 * x2 + 0.08 * x3)"
]
},
{
"cell_type": "markdown",
"id": "ae05e61f-17dd-4935-adf2-e395d0f10563",
"metadata": {
"id": "ae05e61f-17dd-4935-adf2-e395d0f10563"
},
"source": [
"And, finally, solve it:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f3aee2b4-fc76-4609-ab20-ab54565936d8",
"metadata": {
"id": "f3aee2b4-fc76-4609-ab20-ab54565936d8",
"outputId": "747c216e-199e-4085-8ae7-65f3d48d6c6d"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Solution:\n",
"Objective value = 2.590000000000001\n",
"x1 = 8.000000000000007\n",
"x2 = 5.499999999999995\n",
"x3 = 0.49999999999999756\n"
]
}
],
"source": [
"solver.Solve()\n",
"print('Solution:')\n",
"print('Objective value =', solver.Objective().Value())\n",
"print('x1 =', x1.solution_value())\n",
"print('x2 =', x2.solution_value())\n",
"print('x3 =', x3.solution_value())"
]
}
],
"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.8.10"
},
"colab": {
"name": "linear_programming_ortools.ipynb",
"provenance": [],
"include_colab_link": true
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment