From d9c2c9e8530ff07035f9f7e227d1778ba4fcdead Mon Sep 17 00:00:00 2001 From: Jialei Ding <j.d.ding@tudelft.nl> Date: Wed, 4 Dec 2024 14:08:36 +0100 Subject: [PATCH] W.2.5 ready --- .../Week_2_5/WS_2_5_Profit_vs_Planet.ipynb | 280 ++++++++++++++++++ content/Week_2_5/WS_2_5_solution.ipynb | 16 +- 2 files changed, 288 insertions(+), 8 deletions(-) create mode 100644 content/Week_2_5/WS_2_5_Profit_vs_Planet.ipynb diff --git a/content/Week_2_5/WS_2_5_Profit_vs_Planet.ipynb b/content/Week_2_5/WS_2_5_Profit_vs_Planet.ipynb new file mode 100644 index 00000000..f0a732c3 --- /dev/null +++ b/content/Week_2_5/WS_2_5_Profit_vs_Planet.ipynb @@ -0,0 +1,280 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "b3210984", + "metadata": {}, + "source": [ + "# WS 2.5: Profit vs Planet\n", + "\n", + "<h1 style=\"position: absolute; display: flex; flex-grow: 0; flex-shrink: 0; flex-direction: row-reverse; top: 60px;right: 30px; margin: 0; border: 0\">\n", + " <style>\n", + " .markdown {width:100%; position: relative}\n", + " article { position: relative }\n", + " </style>\n", + " <img src=\"https://gitlab.tudelft.nl/mude/public/-/raw/main/tu-logo/TU_P1_full-color.png\" style=\"width:100px\" />\n", + " <img src=\"https://gitlab.tudelft.nl/mude/public/-/raw/main/mude-logo/MUDE_Logo-small.png\" style=\"width:100px\" />\n", + "</h1>\n", + "<h2 style=\"height: 10px\">\n", + "</h2>\n", + "\n", + "*[CEGM1000 MUDE](http://mude.citg.tudelft.nl/): Week 2.5, Optimization. For: December 11, 2024*" + ] + }, + { + "cell_type": "markdown", + "id": "1c4b1a7a", + "metadata": {}, + "source": [ + "## Part 1: Overview and Mathematical Formulation\n", + "\n", + "A civil engineering company wants to decide on the projects that they should do. Their objective is to minimize the environmental impact of their projects while making enough profit to keep the company running.\n", + "\n", + "They have a portfolio of 6 possible projects to invest in, where A, B , and C are new infrastructure projects (so-called type 1), and D, E, F are refurbishment projects (so-called type 2).\n", + "\n", + "The environmental impact of each project is given by $I_i$ where $i \\in [1,(...),6]$ is the index of the project. $I_i=[90,45,78,123,48,60]$\n", + "\n", + "The profit of each project is given by $P_i$ where $i\\in [1,(...),6]$ is the index of the project: $P_i=[120,65,99,110,33,99]$\n", + "\n", + "The company is operating with the following constraints, please formulate the mathematical program that allows solving the problem:\n", + "\n", + "- The company wants to do 3 out of the 6 projects\n", + "- the projects of type 2 must be at least as many as the ones of type 1 \n", + "- the profit of all projects together must be greater or equal than $250$ ($\\beta$)" + ] + }, + { + "cell_type": "markdown", + "id": "15d6b3f0", + "metadata": {}, + "source": [ + "<div style=\"background-color:#AABAB2; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%\">\n", + "<p>\n", + "<b>Task 1: Writing the mathematical formulation</b> \n", + "\n", + "Write down every formulation and constraint that is relevant to solve this optimization problem.\n", + "</p>\n", + "</div>" + ] + }, + { + "cell_type": "markdown", + "id": "7d0c2565", + "metadata": {}, + "source": [ + "_Your answer here._" + ] + }, + { + "cell_type": "markdown", + "id": "d1f9f576", + "metadata": {}, + "source": [ + "<div style=\"background-color:#AABAB2; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%\">\n", + "<p>\n", + "<b>Task 2: Setting up the software</b> \n", + "\n", + "We'll continue using Gurobi this week, which you've set up in last week's PA. We'll use some other special packages as well. **Therefore, be sure to use the special conda environment created for this week.**\n", + "\n", + "</p>\n", + "</div>" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ae96cbb5", + "metadata": {}, + "outputs": [], + "source": [ + "import gurobipy as gp" + ] + }, + { + "cell_type": "markdown", + "id": "a1b35e97", + "metadata": {}, + "source": [ + "<div style=\"background-color:#AABAB2; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%\">\n", + "<p>\n", + "<b>Task 3: Setting up the problem</b> \n", + "\n", + "Define any variables you might need to setup your model.\n", + "</p>\n", + "</div>" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d5cba9fe", + "metadata": {}, + "outputs": [], + "source": [ + "# Project data\n", + "YOUR_CODE_HERE\n", + "\n", + "# Minimum required profit\n", + "YOUR_CODE_HERE\n", + "\n", + "# Number of projects and types\n", + "YOUR_CODE_HERE" + ] + }, + { + "cell_type": "markdown", + "id": "88d658e6", + "metadata": {}, + "source": [ + "## Part 2: Create model with Gurobi\n", + "\n", + "**Remember that examples of using Gurobi to create and optimize a model are provided in the online textbook**, and generally consist of the following steps (the first instantiates a class and the rest are executed as methods of the class):\n", + "\n", + "1. Define the model (instantiate the class)\n", + "2. Define variables\n", + "3. Define objective function\n", + "4. Add constraints\n", + "5. Optimize the model\n", + "\n", + "Remember, you can always ask for help to understand a function of gurobi\n", + "```\n", + "help(gurobipy.model.addVars)\n", + "```\n" + ] + }, + { + "cell_type": "markdown", + "id": "789ed5ae", + "metadata": {}, + "source": [ + "<div style=\"background-color:#AABAB2; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%\">\n", + "<p>\n", + "<b>Task 4: Create the Gurobi model</b> \n", + "\n", + "Create the Gurobi model, set your decision variables, your function and your constrains. Take a look at the book for an example implementation in Python if you don't know where to start.\n", + "</p>\n", + "</div>" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8d008ba9", + "metadata": {}, + "outputs": [], + "source": [ + "YOUR_CODE_HERE" + ] + }, + { + "cell_type": "markdown", + "id": "1380916b", + "metadata": {}, + "source": [ + "<div style=\"background-color:#AABAB2; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%\">\n", + "<p>\n", + "<b>Task 5: Display your results</b> \n", + "\n", + "Display the model in a good way to interpret and print the solution of the optimization.\n", + "</p>\n", + "</div>" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "83ef8b18", + "metadata": {}, + "outputs": [], + "source": [ + "YOUR_CODE_HERE" + ] + }, + { + "cell_type": "markdown", + "id": "98cb6023", + "metadata": {}, + "source": [ + "<div style=\"background-color:#AABAB2; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%\">\n", + "<p>\n", + "<b>Task 6: Additional constraint</b> \n", + "\n", + "Solve the model with an additional constraint: if project 1 is done then the impact of all projects together should be lower than $\\gamma$ with $\\gamma=130$.\n", + "\n", + "In the first cell you should add the constraint, then in a second cell optimize the model.\n", + "\n", + "</p>\n", + "</div>" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9f2f9b6d", + "metadata": {}, + "outputs": [], + "source": [ + "YOUR_CODE_HERE" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "19040bd6", + "metadata": {}, + "outputs": [], + "source": [ + "YOUR_CODE_HERE" + ] + }, + { + "cell_type": "markdown", + "id": "603aea77", + "metadata": {}, + "source": [ + "**End of notebook.**\n", + "<h2 style=\"height: 60px\">\n", + "</h2>\n", + "<h3 style=\"position: absolute; display: flex; flex-grow: 0; flex-shrink: 0; flex-direction: row-reverse; bottom: 60px; right: 50px; margin: 0; border: 0\">\n", + " <style>\n", + " .markdown {width:100%; position: relative}\n", + " article { position: relative }\n", + " </style>\n", + " <a rel=\"license\" href=\"http://creativecommons.org/licenses/by/4.0/\">\n", + " <img alt=\"Creative Commons License\" style=\"border-width:; width:88px; height:auto; padding-top:10px\" src=\"https://i.creativecommons.org/l/by/4.0/88x31.png\" />\n", + " </a>\n", + " <a rel=\"TU Delft\" href=\"https://www.tudelft.nl/en/ceg\">\n", + " <img alt=\"TU Delft\" style=\"border-width:0; width:100px; height:auto; padding-bottom:0px\" src=\"https://gitlab.tudelft.nl/mude/public/-/raw/main/tu-logo/TU_P1_full-color.png\" />\n", + " </a>\n", + " <a rel=\"MUDE\" href=\"http://mude.citg.tudelft.nl/\">\n", + " <img alt=\"MUDE\" style=\"border-width:0; width:100px; height:auto; padding-bottom:0px\" src=\"https://gitlab.tudelft.nl/mude/public/-/raw/main/mude-logo/MUDE_Logo-small.png\" />\n", + " </a>\n", + " \n", + "</h3>\n", + "<span style=\"font-size: 75%\">\n", + "© Copyright 2024 <a rel=\"MUDE\" href=\"http://mude.citg.tudelft.nl/\">MUDE</a> TU Delft. This work is licensed under a <a rel=\"license\" href=\"http://creativecommons.org/licenses/by/4.0/\">CC BY 4.0 License</a>." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "mude-week-2-5", + "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.12.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/content/Week_2_5/WS_2_5_solution.ipynb b/content/Week_2_5/WS_2_5_solution.ipynb index 0c429c3e..41c670ca 100644 --- a/content/Week_2_5/WS_2_5_solution.ipynb +++ b/content/Week_2_5/WS_2_5_solution.ipynb @@ -54,7 +54,7 @@ "<p>\n", "<b>Task 1: Writing the mathematical formulation</b> \n", "\n", - "Write down every formulation and constrain that is relevant to solve this optimization problem.\n", + "Write down every formulation and constraint that is relevant to solve this optimization problem.\n", "</p>\n", "</div>" ] @@ -95,7 +95,7 @@ "<p>\n", "<b>Task 2: Setting up the software</b> \n", "\n", - "We'll continue using Gurobi this week, which you've set up in last week's PA. We'll use some other special packages as well. Therefore, be sure to use the special conda environment created for this week.\n", + "We'll continue using Gurobi this week, which you've set up in last week's PA. We'll use some other special packages as well. **Therefore, be sure to use the special conda environment created for this week.**\n", "\n", "</p>\n", "</div>" @@ -103,7 +103,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 7, "id": "ae96cbb5", "metadata": {}, "outputs": [], @@ -246,7 +246,7 @@ "\n", " 0 0 cutoff 0 198.00000 198.00000 0.00% - 0s\n", "\n", - "Explored 1 nodes (3 simplex iterations) in 0.05 seconds (0.00 work units)\n", + "Explored 1 nodes (3 simplex iterations) in 0.04 seconds (0.00 work units)\n", "Thread count was 12 (of 12 available processors)\n", "\n", "Solution count 2: 198 228 \n", @@ -289,7 +289,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "83ef8b18", "metadata": {}, "outputs": [ @@ -318,7 +318,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "C:\\Users\\jdding\\AppData\\Local\\Temp\\ipykernel_12768\\2324086517.py:6: DeprecationWarning: Model.display() is deprecated\n", + "C:\\Users\\jdding\\AppData\\Local\\Temp\\ipykernel_6080\\2324086517.py:6: DeprecationWarning: Model.display() is deprecated\n", " model.display()\n" ] } @@ -418,7 +418,7 @@ "Presolve time: 0.00s\n", "Presolve: All rows and columns removed\n", "\n", - "Explored 0 nodes (0 simplex iterations) in 0.01 seconds (0.00 work units)\n", + "Explored 0 nodes (0 simplex iterations) in 0.02 seconds (0.00 work units)\n", "Thread count was 1 (of 12 available processors)\n", "\n", "Solution count 1: 228 \n", @@ -448,7 +448,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "C:\\Users\\jdding\\AppData\\Local\\Temp\\ipykernel_12768\\1357951651.py:8: DeprecationWarning: Model.display() is deprecated\n", + "C:\\Users\\jdding\\AppData\\Local\\Temp\\ipykernel_6080\\1357951651.py:8: DeprecationWarning: Model.display() is deprecated\n", " model.display()\n" ] } -- GitLab