This commit is contained in:
counterweight 2023-12-09 18:05:57 +01:00
parent a67287eeb7
commit ee8dd41f85
129 changed files with 91712 additions and 0 deletions

BIN
aot/cases/case_1/case_1.zip Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,405 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"toc_visible": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"source": [
"# Introduction to PuLP\n",
"\n",
"For case 1, you will need to define and solve optimization problems. In this notebook, I'll help you understand how to use `pulp`, a Python package for modeling optimization problems. You might want to check the following links:\n",
"\n",
"- Documentation: https://coin-or.github.io/pulp/\n",
"- Homepage: https://github.com/coin-or/pulp\n",
"\n"
],
"metadata": {
"id": "eLvjUuJdzS7z"
}
},
{
"cell_type": "markdown",
"source": [
"# Installing and checking all is in place"
],
"metadata": {
"id": "HFavOEVS0dbY"
}
},
{
"cell_type": "markdown",
"source": [
"The first thing you need to do is to install `pulp`. `pulp` is not in the standard available packages in Colab, so you need to run the following cell once. "
],
"metadata": {
"id": "HgZwpjUG0PsK"
}
},
{
"cell_type": "code",
"source": [
"!pip install pulp"
],
"metadata": {
"id": "ni6Q_YiO0nIm"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"After doing that, you can import the library."
],
"metadata": {
"id": "k9YI0Kzw0qLT"
}
},
{
"cell_type": "code",
"source": [
"import pulp"
],
"metadata": {
"id": "hw6keX7x0tZ1"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"If all is good, running the following command will print a large log testing `pulp`. The last line should read \"OK\"."
],
"metadata": {
"id": "vD_rXehL1KXX"
}
},
{
"cell_type": "code",
"source": [
"pulp.pulpTestAll()"
],
"metadata": {
"id": "Ney2a8mu1JqQ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Defining and solving problems\n",
"\n",
"The following cells show you the absolute minimum to model and solve a problem with `pulp`. The steps are:\n",
"\n",
"1. Define decision variables\n",
"2. Define the target function\n",
"3. Define the constraints\n",
"4. Assemble the problem\n",
"5. Solve it\n",
"6. Examine results\n",
"\n",
"For more flexibility, options and interesting stuff, please check up the PuLP documentation."
],
"metadata": {
"id": "oiXz40NR1whf"
}
},
{
"cell_type": "markdown",
"source": [
"## Define decision variables"
],
"metadata": {
"id": "nq5bcQs03g0j"
}
},
{
"cell_type": "code",
"source": [
"x = pulp.LpVariable(\n",
" name=\"x\",\n",
" cat=pulp.LpContinuous \n",
" )\n",
"\n",
"y = pulp.LpVariable(\n",
" name=\"y\",\n",
" cat=pulp.LpInteger # This will make the variable integer only\n",
" )\n",
"\n",
"z = pulp.LpVariable(\n",
" name=\"z\",\n",
" cat=pulp.LpBinary # This will make the variable binary (only 0 or 1)\n",
")"
],
"metadata": {
"id": "0SPhww4L3buh"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Define the target function"
],
"metadata": {
"id": "uhlbq2oO35kp"
}
},
{
"cell_type": "code",
"source": [
"target_function = 10 * x - 5 * y + z"
],
"metadata": {
"id": "pu3Im9DH39CN"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Define constraints"
],
"metadata": {
"id": "lqD0dD474Izw"
}
},
{
"cell_type": "code",
"source": [
"constraint_1 = x >= 0\n",
"constraint_2 = y >= 0\n",
"constraint_3 = x >= 10\n",
"constraint_4 = y <= 50"
],
"metadata": {
"id": "5Cu51lYj4OUC"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Assemble the problem\n",
"\n",
"To put all the parts together, you need to declare a problem and specify if you want to minimize or maximize the target function.\n",
"\n",
"Once you have that:\n",
"- First, you \"add\" the target function.\n",
"- After, you \"add\" all the constraints you want to include."
],
"metadata": {
"id": "d5nq94IM4kSU"
}
},
{
"cell_type": "code",
"source": [
"problem = pulp.LpProblem(\"my_silly_problem\", pulp.LpMinimize)\n",
"\n",
"problem += target_function\n",
"\n",
"for constraint in (\n",
" constraint_1,\n",
" constraint_2,\n",
" constraint_3,\n",
" constraint_4\n",
" ):\n",
" problem += constraint"
],
"metadata": {
"id": "yI-Oiwh64mRc"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Solve it\n",
"\n",
"The problem object is now unsolved. You can call the `solve` method on it to find a solution."
],
"metadata": {
"id": "RJTWfR8-5fBd"
}
},
{
"cell_type": "code",
"source": [
"f\"Status: {pulp.LpStatus[problem.status]}\"\n",
"problem.solve()"
],
"metadata": {
"id": "4Fbltpbp5mRi"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Examine results\n",
"\n",
"After calling `solve` on a problem, you can access:\n",
"- The status of the problem. It can be solved, but also it might show to be not feasible.\n",
"- The values assigned to each decision variable.\n",
"- The final value for the target function.\n",
"\n"
],
"metadata": {
"id": "0pc9RmrO7FKo"
}
},
{
"cell_type": "code",
"source": [
"print(f\"Status: {pulp.LpStatus[problem.status]}\")\n",
"for v in problem.variables():\n",
" print(v.name, \"=\", v.varValue)\n",
" \n",
"print(pulp.value(problem.objective))"
],
"metadata": {
"id": "8U4xVvUg9W07"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Peanut Butter Example\n",
"\n",
"As an additional example, you can find below the model and solver for the Peanut Butter Sandwich example we discussed on our lectures."
],
"metadata": {
"id": "I2lNaFm2XVK1"
}
},
{
"cell_type": "code",
"source": [
"pb = pulp.LpVariable(\n",
" name=\"Peanut Butter grams\",\n",
" cat=pulp.LpContinuous \n",
" )\n",
"\n",
"b = pulp.LpVariable(\n",
" name=\"Bread grams\",\n",
" cat=pulp.LpContinuous \n",
" )"
],
"metadata": {
"id": "HI4E2dNoXVK4"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"target_function = 5.88 * pb + 2.87 * b"
],
"metadata": {
"id": "PfTxq8R0XVLB"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"no_negative_pb = pb >= 0\n",
"no_negative_b = b >= 0\n",
"max_pb_we_have = pb <= 200\n",
"max_b_we_have = b <= 300\n",
"doctors_dietary_restriction = pb <= 0.13 * b"
],
"metadata": {
"id": "2X1AzQM8XVLD"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"problem = pulp.LpProblem(\"sandwich_problem\", pulp.LpMaximize)\n",
"\n",
"problem += target_function\n",
"\n",
"for constraint in (\n",
" no_negative_pb,\n",
" no_negative_b,\n",
" max_pb_we_have,\n",
" max_b_we_have,\n",
" doctors_dietary_restriction\n",
" ):\n",
" problem += constraint"
],
"metadata": {
"id": "3oEoQXebXVLE"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"f\"Status: {pulp.LpStatus[problem.status]}\"\n",
"problem.solve()\n",
"print(f\"Status: {pulp.LpStatus[problem.status]}\")\n",
"for v in problem.variables():\n",
" print(v.name, \"=\", v.varValue)\n",
" \n",
"print(f\"Final calories: {pulp.value(problem.objective)}\")"
],
"metadata": {
"id": "u1vI73kiXVLF"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Case 2\n",
"\n",
"You can use the rest of the notebook to work on the different parts of case 1."
],
"metadata": {
"id": "6kWgbTjU-LaN"
}
},
{
"cell_type": "code",
"source": [
"# Good luck!"
],
"metadata": {
"id": "aYzseTWh-Sal"
},
"execution_count": null,
"outputs": []
}
]
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,9 @@
dock,40_ft_container_price_eur,max_capacity
Rotterdam,470,33000
Antwerp,470,25000
Hamburg,480,44000
Amsterdam,610,11000
Marseille,380,9000
Algeciras,280,20000
Valencia,310,11000
Genoa,340,7500
1 dock 40_ft_container_price_eur max_capacity
2 Rotterdam 470 33000
3 Antwerp 470 25000
4 Hamburg 480 44000
5 Amsterdam 610 11000
6 Marseille 380 9000
7 Algeciras 280 20000
8 Valencia 310 11000
9 Genoa 340 7500