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 index 4f78ce11bf161fc669c36f3f306e136051b15a84..7d05bc65bedc3715fb300bed4d4733891e07ca01 100644 --- a/content/Week_2_5/WS_2_5_Profit_vs_Planet.ipynb +++ b/content/Week_2_5/WS_2_5_Profit_vs_Planet.ipynb @@ -203,7 +203,7 @@ "\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", + "Paste your model previous model, and call it <code>model2</code> to keep the results separated and add the new constraint. Then run your second model. \n", "\n", "</p>\n", "</div>" @@ -219,16 +219,6 @@ "YOUR_CODE_HERE" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "19040bd6", - "metadata": {}, - "outputs": [], - "source": [ - "YOUR_CODE_HERE" - ] - }, { "cell_type": "markdown", "id": "603aea77", diff --git a/content/Week_2_5/WS_2_5_solution.ipynb b/content/Week_2_5/WS_2_5_solution.ipynb index 41c670cafc58ce05882d2a590d5b7d5f42b35dd8..5020acc6dab9936e442d40781fc5b4c577fc3966 100644 --- a/content/Week_2_5/WS_2_5_solution.ipynb +++ b/content/Week_2_5/WS_2_5_solution.ipynb @@ -127,7 +127,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 8, "id": "d5cba9fe", "metadata": {}, "outputs": [], @@ -209,7 +209,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 9, "id": "8d008ba9", "metadata": {}, "outputs": [ @@ -217,9 +217,6 @@ "name": "stdout", "output_type": "stream", "text": [ - "Set parameter Username\n", - "Set parameter LicenseID to value 2588551\n", - "Academic license - for non-commercial use only - expires 2025-11-21\n", "Gurobi Optimizer version 12.0.0 build v12.0.0rc1 (win64 - Windows 11.0 (22631.2))\n", "\n", "CPU model: 13th Gen Intel(R) Core(TM) i7-1365U, instruction set [SSE2|AVX|AVX2]\n", @@ -233,7 +230,7 @@ " Objective range [5e+01, 1e+02]\n", " Bounds range [1e+00, 1e+00]\n", " RHS range [3e+00, 3e+02]\n", - "Presolve time: 0.01s\n", + "Presolve time: 0.00s\n", "Presolved: 3 rows, 6 columns, 17 nonzeros\n", "Variable types: 0 continuous, 6 integer (6 binary)\n", "Found heuristic solution: objective 228.0000000\n", @@ -246,7 +243,7 @@ "\n", " 0 0 cutoff 0 198.00000 198.00000 0.00% - 0s\n", "\n", - "Explored 1 nodes (3 simplex iterations) in 0.04 seconds (0.00 work units)\n", + "Explored 1 nodes (3 simplex iterations) in 0.03 seconds (0.00 work units)\n", "Thread count was 12 (of 12 available processors)\n", "\n", "Solution count 2: 198 228 \n", @@ -289,7 +286,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 10, "id": "83ef8b18", "metadata": {}, "outputs": [ @@ -318,7 +315,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "C:\\Users\\jdding\\AppData\\Local\\Temp\\ipykernel_6080\\2324086517.py:6: DeprecationWarning: Model.display() is deprecated\n", + "C:\\Users\\jdding\\AppData\\Local\\Temp\\ipykernel_17648\\2324086517.py:6: DeprecationWarning: Model.display() is deprecated\n", " model.display()\n" ] } @@ -345,7 +342,7 @@ }, { "cell_type": "markdown", - "id": "98cb6023", + "id": "e93fc43f", "metadata": {}, "source": [ "<div style=\"background-color:#AABAB2; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%\">\n", @@ -354,7 +351,7 @@ "\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", + "Paste your model previous model, and call it <code>model2</code> to keep the results separated and add the new constraint. Then run your second model. \n", "\n", "</p>\n", "</div>" @@ -362,7 +359,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 11, "id": "9f2f9b6d", "metadata": {}, "outputs": [ @@ -372,7 +369,7 @@ "<gurobi.Constr *Awaiting Model Update*>" ] }, - "execution_count": 5, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -381,15 +378,26 @@ "# YOUR_CODE_HERE\n", "\n", "# SOLUTION\n", + "model2 = gp.Model(\"Project_Selection\")\n", + "x = model2.addVars(num_projects, vtype=gp.GRB.BINARY, name=\"x\")\n", + "model2.setObjective(sum(I[i] * x[i] for i in range(num_projects)),\n", + " gp.GRB.MINIMIZE)\n", + "model2.addConstr(x.sum() == 3, \"Select_Projects\")\n", + "model2.addConstr((sum(x[i] for i in range(num_type2_projects, num_projects))\n", + " - sum(x[i] for i in range(num_type1_projects)) >= 0),\n", + " \"Type_Constraint\")\n", + "model2.addConstr(sum(P[i] * x[i] for i in range(num_projects)) >= beta,\n", + " \"Minimum_Profit\")\n", "gamma = 130\n", - "model.addConstr((sum(I[i] * x[i] for i in range(num_projects))\n", + "model2.addConstr((sum(I[i] * x[i] for i in range(num_projects))\n", " <= gamma * x[0]+ M * (1 - x[0])),\n", - " \"Impact_Constraint\") " + " \"Impact_Constraint\") \n", + "\n" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 12, "id": "19040bd6", "metadata": {}, "outputs": [ @@ -410,10 +418,6 @@ " Objective range [5e+01, 1e+02]\n", " Bounds range [1e+00, 1e+00]\n", " RHS range [3e+00, 1e+05]\n", - "\n", - "MIP start from previous solve did not produce a new incumbent solution\n", - "MIP start from previous solve violates constraint Impact_Constraint by 68.000000000\n", - "\n", "Presolve removed 4 rows and 6 columns\n", "Presolve time: 0.00s\n", "Presolve: All rows and columns removed\n", @@ -448,8 +452,8 @@ "name": "stderr", "output_type": "stream", "text": [ - "C:\\Users\\jdding\\AppData\\Local\\Temp\\ipykernel_6080\\1357951651.py:8: DeprecationWarning: Model.display() is deprecated\n", - " model.display()\n" + "C:\\Users\\jdding\\AppData\\Local\\Temp\\ipykernel_17648\\2231829295.py:8: DeprecationWarning: Model.display() is deprecated\n", + " model2.display()\n" ] } ], @@ -457,14 +461,14 @@ "# YOUR_CODE_HERE\n", "\n", "# SOLUTION\n", - "model.optimize()\n", + "model2.optimize()\n", "\n", "print(\"Model structure:\") \n", "# see the model that you have built in a nice why to interpret\n", - "model.display() \n", + "model2.display() \n", "\n", "# Display the solution\n", - "if model.status == gp.GRB.OPTIMAL:\n", + "if model2.status == gp.GRB.OPTIMAL:\n", " print(\"Optimal Solution:\")\n", " for i in range(num_projects):\n", " if x[i].x > 0.9:\n", @@ -473,7 +477,7 @@ " print(\"No optimal solution found.\")\n", "\n", " \n", - "print(\"Optimal Objective function Value\", model.objVal) " + "print(\"Optimal Objective function Value\", model2.objVal) " ] }, {