From b37344f521ab3b1cd671615f412064ab2976acb6 Mon Sep 17 00:00:00 2001 From: GitLab CI <gitlab-ci@example.com> Date: Tue, 10 Dec 2024 15:30:45 +0000 Subject: [PATCH] Update synced files [skip ci] --- .../Analysis_discharge_solution.ipynb | 2 +- .../Analysis_emissions_solution.ipynb | 18 ++-- .../Force/Analysis_force_solution.ipynb | 18 ++-- .../Discharge/Analysis_discharge.ipynb | 6 +- .../Emissions/Analysis_emissions.ipynb | 6 +- .../GA_1_7/Student/Force/Analysis_force.ipynb | 6 +- .../Unused/Temp/Distribution_Fitting_T.ipynb | 14 +-- .../GA_2_5/old_Analysis_GA_solution.ipynb | 92 +++---------------- .../GA_2_5/old_Analysis_GA_solution.md | 60 ++---------- .../GA_2_5/old_Analysis_GA_solution.py | 56 ++--------- .../Week_2_2/old/PA_2_1_solution_sympy.ipynb | 18 ++-- .../old/old_PA10_solution_sympy.ipynb | 18 ++-- 12 files changed, 87 insertions(+), 227 deletions(-) diff --git a/synced_files/GA_1_7/Solution/Discharge/Analysis_discharge_solution.ipynb b/synced_files/GA_1_7/Solution/Discharge/Analysis_discharge_solution.ipynb index 75e09718..e36ef348 100644 --- a/synced_files/GA_1_7/Solution/Discharge/Analysis_discharge_solution.ipynb +++ b/synced_files/GA_1_7/Solution/Discharge/Analysis_discharge_solution.ipynb @@ -606,7 +606,7 @@ }, { "cell_type": "markdown", - "id": "8f758bdc", + "id": "896efc98", "metadata": {}, "source": [ "<div style=\"background-color:#FAE99E; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px\">\n", diff --git a/synced_files/GA_1_7/Solution/Emissions/Analysis_emissions_solution.ipynb b/synced_files/GA_1_7/Solution/Emissions/Analysis_emissions_solution.ipynb index 6567f282..3c7d67fb 100644 --- a/synced_files/GA_1_7/Solution/Emissions/Analysis_emissions_solution.ipynb +++ b/synced_files/GA_1_7/Solution/Emissions/Analysis_emissions_solution.ipynb @@ -159,7 +159,7 @@ }, { "cell_type": "markdown", - "id": "60c2772b", + "id": "5fe35d99", "metadata": {}, "source": [ "<div style=\"background-color:#FAE99E; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px\">\n", @@ -262,7 +262,7 @@ }, { "cell_type": "markdown", - "id": "47336511", + "id": "595ed5e1", "metadata": {}, "source": [ "<div style=\"background-color:#FAE99E; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px\">\n", @@ -374,7 +374,7 @@ { "cell_type": "code", "execution_count": null, - "id": "8b7bc362", + "id": "789f8ffe", "metadata": {}, "outputs": [], "source": [ @@ -437,7 +437,7 @@ }, { "cell_type": "markdown", - "id": "828202de", + "id": "2609f730", "metadata": {}, "source": [ "<div style=\"background-color:#FAE99E; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px\">\n", @@ -538,7 +538,7 @@ }, { "cell_type": "markdown", - "id": "0abd6107", + "id": "addd7d94", "metadata": {}, "source": [ "<div style=\"background-color:#FAE99E; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px\">\n", @@ -552,7 +552,7 @@ }, { "cell_type": "markdown", - "id": "1d6cd58b", + "id": "97ec1d42", "metadata": {}, "source": [ "If you run the code in the cell below, you will obtain a scatter plot of both variables. Explore the relationship between both variables and answer the following questions:\n", @@ -573,7 +573,7 @@ { "cell_type": "code", "execution_count": null, - "id": "9f6048c7", + "id": "26a7f2b5", "metadata": {}, "outputs": [], "source": [ @@ -589,7 +589,7 @@ { "cell_type": "code", "execution_count": null, - "id": "730053de", + "id": "671ce3c4", "metadata": {}, "outputs": [], "source": [ @@ -602,7 +602,7 @@ }, { "cell_type": "markdown", - "id": "c36c1228", + "id": "78effa24", "metadata": {}, "source": [ "<div style=\"background-color:#FAE99E; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px\">\n", diff --git a/synced_files/GA_1_7/Solution/Force/Analysis_force_solution.ipynb b/synced_files/GA_1_7/Solution/Force/Analysis_force_solution.ipynb index f70dddea..875a3526 100644 --- a/synced_files/GA_1_7/Solution/Force/Analysis_force_solution.ipynb +++ b/synced_files/GA_1_7/Solution/Force/Analysis_force_solution.ipynb @@ -164,7 +164,7 @@ }, { "cell_type": "markdown", - "id": "4813e021", + "id": "47600754", "metadata": {}, "source": [ "<div style=\"background-color:#FAE99E; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px\">\n", @@ -269,7 +269,7 @@ }, { "cell_type": "markdown", - "id": "72632d52", + "id": "ddb49ba6", "metadata": {}, "source": [ "<div style=\"background-color:#FAE99E; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px\">\n", @@ -382,7 +382,7 @@ { "cell_type": "code", "execution_count": null, - "id": "b678de66", + "id": "6de8ff43", "metadata": {}, "outputs": [], "source": [ @@ -445,7 +445,7 @@ }, { "cell_type": "markdown", - "id": "06341330", + "id": "4bed4432", "metadata": {}, "source": [ "<div style=\"background-color:#FAE99E; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px\">\n", @@ -546,7 +546,7 @@ }, { "cell_type": "markdown", - "id": "f0cd2ba2", + "id": "643950ec", "metadata": {}, "source": [ "<div style=\"background-color:#FAE99E; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px\">\n", @@ -561,7 +561,7 @@ }, { "cell_type": "markdown", - "id": "baa64738", + "id": "d56dcbe7", "metadata": {}, "source": [ "If you run the code in the cell below, you will obtain a scatter plot of both variables. Explore the relationship between both variables and answer the following questions:\n", @@ -582,7 +582,7 @@ { "cell_type": "code", "execution_count": null, - "id": "4b832077", + "id": "60905bdb", "metadata": {}, "outputs": [], "source": [ @@ -598,7 +598,7 @@ { "cell_type": "code", "execution_count": null, - "id": "7a12f07f", + "id": "8febae98", "metadata": {}, "outputs": [], "source": [ @@ -611,7 +611,7 @@ }, { "cell_type": "markdown", - "id": "4ee89c3b", + "id": "075b32b4", "metadata": {}, "source": [ "<div style=\"background-color:#FAE99E; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px\">\n", diff --git a/synced_files/GA_1_7/Student/Discharge/Analysis_discharge.ipynb b/synced_files/GA_1_7/Student/Discharge/Analysis_discharge.ipynb index 819bb4ac..017df977 100644 --- a/synced_files/GA_1_7/Student/Discharge/Analysis_discharge.ipynb +++ b/synced_files/GA_1_7/Student/Discharge/Analysis_discharge.ipynb @@ -384,7 +384,7 @@ }, { "cell_type": "markdown", - "id": "5a36d8e1", + "id": "b84744a4", "metadata": {}, "source": [ "If you run the code in the cell below, you will obtain a scatter plot of both variables. Explore the relationship between both variables and answer the following questions:\n", @@ -405,7 +405,7 @@ { "cell_type": "code", "execution_count": null, - "id": "e43d8b5d", + "id": "4de11b60", "metadata": {}, "outputs": [], "source": [ @@ -421,7 +421,7 @@ { "cell_type": "code", "execution_count": null, - "id": "63e7bdd0", + "id": "5abb8939", "metadata": {}, "outputs": [], "source": [ diff --git a/synced_files/GA_1_7/Student/Emissions/Analysis_emissions.ipynb b/synced_files/GA_1_7/Student/Emissions/Analysis_emissions.ipynb index 578d2a1f..da27448e 100644 --- a/synced_files/GA_1_7/Student/Emissions/Analysis_emissions.ipynb +++ b/synced_files/GA_1_7/Student/Emissions/Analysis_emissions.ipynb @@ -378,7 +378,7 @@ }, { "cell_type": "markdown", - "id": "7da4cee5", + "id": "d42e2d08", "metadata": {}, "source": [ "If you run the code in the cell below, you will obtain a scatter plot of both variables. Explore the relationship between both variables and answer the following questions:\n", @@ -399,7 +399,7 @@ { "cell_type": "code", "execution_count": null, - "id": "bdfcfda3", + "id": "a8947efd", "metadata": {}, "outputs": [], "source": [ @@ -415,7 +415,7 @@ { "cell_type": "code", "execution_count": null, - "id": "c382e811", + "id": "eccf40b6", "metadata": {}, "outputs": [], "source": [ diff --git a/synced_files/GA_1_7/Student/Force/Analysis_force.ipynb b/synced_files/GA_1_7/Student/Force/Analysis_force.ipynb index 208e1d35..191318c7 100644 --- a/synced_files/GA_1_7/Student/Force/Analysis_force.ipynb +++ b/synced_files/GA_1_7/Student/Force/Analysis_force.ipynb @@ -384,7 +384,7 @@ }, { "cell_type": "markdown", - "id": "3f307882", + "id": "0c674860", "metadata": {}, "source": [ "If you run the code in the cell below, you will obtain a scatter plot of both variables. Explore the relationship between both variables and answer the following questions:\n", @@ -405,7 +405,7 @@ { "cell_type": "code", "execution_count": null, - "id": "3069b81b", + "id": "3ac217e3", "metadata": {}, "outputs": [], "source": [ @@ -421,7 +421,7 @@ { "cell_type": "code", "execution_count": null, - "id": "0347865f", + "id": "071694a1", "metadata": {}, "outputs": [], "source": [ diff --git a/synced_files/GA_1_7/Unused/Temp/Distribution_Fitting_T.ipynb b/synced_files/GA_1_7/Unused/Temp/Distribution_Fitting_T.ipynb index ed961c01..3e437d59 100644 --- a/synced_files/GA_1_7/Unused/Temp/Distribution_Fitting_T.ipynb +++ b/synced_files/GA_1_7/Unused/Temp/Distribution_Fitting_T.ipynb @@ -163,7 +163,7 @@ }, { "cell_type": "markdown", - "id": "2894cf33", + "id": "256d920e", "metadata": {}, "source": [ "<div style=\"background-color:#FAE99E; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px\">\n", @@ -263,7 +263,7 @@ }, { "cell_type": "markdown", - "id": "cedffbee", + "id": "973ce758", "metadata": {}, "source": [ "<div style=\"background-color:#FAE99E; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px\">\n", @@ -372,7 +372,7 @@ { "cell_type": "code", "execution_count": null, - "id": "1e5f9dea", + "id": "c11b680c", "metadata": {}, "outputs": [], "source": [ @@ -434,7 +434,7 @@ }, { "cell_type": "markdown", - "id": "00c255ef", + "id": "84699219", "metadata": {}, "source": [ "<div style=\"background-color:#FAE99E; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px\">\n", @@ -533,7 +533,7 @@ }, { "cell_type": "markdown", - "id": "7c9d5887", + "id": "9706c82a", "metadata": {}, "source": [ "<div style=\"background-color:#FAE99E; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px\">\n", @@ -546,7 +546,7 @@ }, { "cell_type": "markdown", - "id": "22003ddf", + "id": "3ab96f3c", "metadata": {}, "source": [ "If you run the code in the cell below, you will obtain a scatter plot of both variables. Explore the relationship between both variables and answer the following questions:\n", @@ -567,7 +567,7 @@ { "cell_type": "code", "execution_count": null, - "id": "233d3478", + "id": "f64e0385", "metadata": {}, "outputs": [], "source": [ diff --git a/synced_files/GA_2_5/old_Analysis_GA_solution.ipynb b/synced_files/GA_2_5/old_Analysis_GA_solution.ipynb index d65dc0f6..47914811 100644 --- a/synced_files/GA_2_5/old_Analysis_GA_solution.ipynb +++ b/synced_files/GA_2_5/old_Analysis_GA_solution.ipynb @@ -25,15 +25,6 @@ "*[CEGM1000 MUDE](http://mude.citg.tudelft.nl/): Week 2.5. For: 15 December, 2023.*\n" ] }, - { - "cell_type": "markdown", - "metadata": { - "id": "0491cc69" - }, - "source": [ - "<div style=\"background-color:#ffa6a6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%\"><p><b>Note:</b> during the in-class session some of the confusion was caused by code issues. Comments relevant to the code and notebooks as-used in the Friday in-class session are provided in boxes like this.</p></div>" - ] - }, { "cell_type": "markdown", "metadata": { @@ -96,25 +87,7 @@ "from pymoo.operators.sampling.rnd import BinaryRandomSampling\n", "from pymoo.operators.crossover.hux import HalfUniformCrossover #\n", "from pymoo.operators.mutation.bitflip import BitflipMutation\n", - "\n", - "# not used here but generally useful dependencies\n", - "#from pymoo.core.problem import Problem\n", - "#from pymoo.operators.mutation.pm import PolynomialMutation\n", - "#from pymoo.operators.crossover.pntx import PointCrossover #\n", - "#from pymoo.operators.crossover.sbx import SBX\n", - "#from pymoo.operators.crossover.sbx import SimulatedBinaryCrossover\n", - "#from pymoo.operators.crossover.pntx import Crossover\n", - "#from pymoo.operators.repair.bounds_repair import BoundsRepair\n", - "#from pymoo.core.repair import Repair" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "0491cc69" - }, - "source": [ - "<div style=\"background-color:#ffa6a6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%\"><p><b>Note:</b> the intention with the commented lines above was to illustrate possible crossover methods you could use, but unfortunately the pymoo documentation was not clear enough to indicate how they could have been used. The methods <code>SinglePointCrossover</code> and <code>TwoPointCrossover</code> (n<em>not</em> listed above!) could have been easily used, as these methods require no additional keyword arguments and would have worked “out of the box;†they were also illustrated with examples in the online textbook. We apologize for this oversight on our part.</p></div>" + "from pymoo.operators.crossover.pntx import PointCrossover # " ] }, { @@ -443,15 +416,6 @@ "Now that we have the required functions for reading and processing the data, let's define some problem parameters and prepare the input." ] }, - { - "cell_type": "markdown", - "metadata": { - "id": "0491cc69" - }, - "source": [ - "<div style=\"background-color:#ffa6a6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%\"><p><b>Note:</b> the variables <code>extension_max_no</code> and <code>timelimit</code> were both defined here, but never used. <code>extension_max_no</code> is similar to the variable in notebook A with the same name; here it is superceded below by <code>Budget</code>.</p></div>" - ] - }, { "cell_type": "code", "execution_count": null, @@ -464,11 +428,6 @@ "source": [ "# define parameters, case study (network) list and the directory where their files are\n", "extension_factor = 1.5 # capacity after extension (1.5 means add 50%)\n", - "extension_max_no = 20 # the number of links to add capacity to (simplified way of reprsenting a budget for investing)\n", - "#it's the same to say that it's exactly this number of that this number is the max, that's because every investment brings travel time benefits \n", - "#even if just one car circulates.\n", - "timelimit = 300 # seconds\n", - "beta = 2 # parameter to use in link travel time function (explained later)\n", "\n", "networks = ['SiouxFalls']\n", "networks_dir = os.getcwd() +'/input/TransportationNetworks'\n", @@ -487,18 +446,7 @@ "# prep links, nodes, and free flow travel times\n", "links = list(net_data['capacity'].keys())\n", "nodes = np.unique([list(edge) for edge in links])\n", - "fftts = net_data['free_flow']\n", - "\n", - "# auxiliary parameters (dict format) to keep the problem linear (capacities as parameters rather than variables)\n", - "cap_normal = {(i, j): cap for (i, j), cap in net_data['capacity'].items()}\n", - "cap_extend = {(i, j): cap * extension_factor for (i, j), cap in net_data['capacity'].items()}\n", - "\n", - "# origins and destinations\n", - "dests = np.unique([dest for (orig, dest) in list(ods_data.keys())])\n", - "origs = np.unique([orig for (orig, dest) in list(ods_data.keys())])\n", - "\n", - "# demand in node-destination form\n", - "demand = create_nd_matrix(ods_data, origs, dests, nodes)" + "fftts = net_data['free_flow']" ] }, { @@ -707,25 +655,7 @@ "id": "0491cc69" }, "source": [ - "<div style=\"background-color:#ffa6a6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%\"><p><b>Note:</b> population size <code>pop_size</code> was 10 originally. If you change this, you will see different results. This is problem-dependent!</p></div>" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "0491cc69" - }, - "source": [ - "<div style=\"background-color:#ffa6a6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%\"><p><b>This:</b> <code>Budget</code> is the way the number of links was selected (unlike notebook A, which used <code>extension_max_no</code>). </p></div>" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "0491cc69" - }, - "source": [ - "<div style=\"background-color:#ffa6a6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%\"><p><b>This:</b> <code>Budget</code> is the way the number of links was selected (unlike notebook A, which used <code>extension_max_no</code>). The initial value of 76 was trivial, and the solution converged quickly. Numbers between 10 and 40 would have produced <em>much</em> more interesting results (see solution explanation).</p></div>" + "<div style=\"background-color:#ffa500; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%\"><p><b>Note:</b> population size <code>pop_size</code> is 10 originally. If you change this, you will see different results. This is problem-dependent!</p></div>" ] }, { @@ -738,17 +668,18 @@ }, "outputs": [], "source": [ - "Budget = 76\n", + "extension_max_no = 40\n", "pop_size = 10\n", "\n", "# initiate an instance of the problem with max number of selected links as budget constraint\n", - "problem = NDP(budget=Budget)\n", + "problem = NDP(budget=extension_max_no)\n", "\n", "# initiate the GA with parameters appropriate for binary variables\n", "method = GA(pop_size=pop_size,\n", " sampling=BinaryRandomSampling(),\n", " mutation=BitflipMutation(),\n", " crossover=HalfUniformCrossover()\n", + " # replace HalfUniformCrossover() by PointCrossover(2) for two-point crossover\n", " )" ] }, @@ -763,13 +694,20 @@ "Now we are ready to minimize the NDP problem using the GA method we defined." ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<div style=\"background-color:#a6ffa6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%\"><p><b>Instructions:</b> Run the GA for 3 minutes initially to observe how the results converge and to understand the process of obtaining the final solution. Once you have familiarized yourself with the mechanism and the behavior of the algorithm, extend the maximum computation time to 10 minutes. Use the results from this extended run as the foundation for addressing the questions outlined in the report. </p></div>" + ] + }, { "cell_type": "markdown", "metadata": { "id": "0491cc69" }, "source": [ - "<div style=\"background-color:#ffa6a6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%\"><p><b>Note:</b> termination is set here as a keyword argument (see note above).</p></div>" + "<div style=\"background-color:#ffa500; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%\"><p><b>Note:</b> Maximum computation time (termination criteria) is set here as a keyword argument.</p></div>" ] }, { @@ -785,7 +723,7 @@ "\n", "opt_results = minimize(problem,\n", " method,\n", - " termination=(\"time\", \"00:05:00\"), #5 minute maximum computation time\n", + " termination=(\"time\", \"00:03:00\"), #this is maximum computation time\n", " seed=7,\n", " save_history=True,\n", " verbose=True,\n", diff --git a/synced_files/GA_2_5/old_Analysis_GA_solution.md b/synced_files/GA_2_5/old_Analysis_GA_solution.md index 69eaf9f3..c6f84b9a 100644 --- a/synced_files/GA_2_5/old_Analysis_GA_solution.md +++ b/synced_files/GA_2_5/old_Analysis_GA_solution.md @@ -31,10 +31,6 @@ jupyter: <!-- #endregion --> -<!-- #region id="0491cc69" --> -<div style="background-color:#ffa6a6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"><p><b>Note:</b> during the in-class session some of the confusion was caused by code issues. Comments relevant to the code and notebooks as-used in the Friday in-class session are provided in boxes like this.</p></div> -<!-- #endregion --> - <!-- #region pycharm={"name": "#%% md\n"} --> ## Introduction @@ -75,22 +71,9 @@ from pymoo.optimize import minimize from pymoo.operators.sampling.rnd import BinaryRandomSampling from pymoo.operators.crossover.hux import HalfUniformCrossover # from pymoo.operators.mutation.bitflip import BitflipMutation - -# not used here but generally useful dependencies -#from pymoo.core.problem import Problem -#from pymoo.operators.mutation.pm import PolynomialMutation -#from pymoo.operators.crossover.pntx import PointCrossover # -#from pymoo.operators.crossover.sbx import SBX -#from pymoo.operators.crossover.sbx import SimulatedBinaryCrossover -#from pymoo.operators.crossover.pntx import Crossover -#from pymoo.operators.repair.bounds_repair import BoundsRepair -#from pymoo.core.repair import Repair +from pymoo.operators.crossover.pntx import PointCrossover # ``` -<!-- #region id="0491cc69" --> -<div style="background-color:#ffa6a6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"><p><b>Note:</b> the intention with the commented lines above was to illustrate possible crossover methods you could use, but unfortunately the pymoo documentation was not clear enough to indicate how they could have been used. The methods <code>SinglePointCrossover</code> and <code>TwoPointCrossover</code> (n<em>not</em> listed above!) could have been easily used, as these methods require no additional keyword arguments and would have worked “out of the box;†they were also illustrated with examples in the online textbook. We apologize for this oversight on our part.</p></div> -<!-- #endregion --> - ```python # For visualization from utils.network_visualization import network_visualization @@ -341,18 +324,9 @@ def create_nd_matrix(ods_data, origins, destinations, nodes): Now that we have the required functions for reading and processing the data, let's define some problem parameters and prepare the input. <!-- #endregion --> -<!-- #region id="0491cc69" --> -<div style="background-color:#ffa6a6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"><p><b>Note:</b> the variables <code>extension_max_no</code> and <code>timelimit</code> were both defined here, but never used. <code>extension_max_no</code> is similar to the variable in notebook A with the same name; here it is superceded below by <code>Budget</code>.</p></div> -<!-- #endregion --> - ```python pycharm={"name": "#%%\n"} # define parameters, case study (network) list and the directory where their files are extension_factor = 1.5 # capacity after extension (1.5 means add 50%) -extension_max_no = 20 # the number of links to add capacity to (simplified way of reprsenting a budget for investing) -#it's the same to say that it's exactly this number of that this number is the max, that's because every investment brings travel time benefits -#even if just one car circulates. -timelimit = 300 # seconds -beta = 2 # parameter to use in link travel time function (explained later) networks = ['SiouxFalls'] networks_dir = os.getcwd() +'/input/TransportationNetworks' @@ -372,17 +346,6 @@ net_data, ods_data = net_dict[networks[0]], ods_dict[networks[0]] links = list(net_data['capacity'].keys()) nodes = np.unique([list(edge) for edge in links]) fftts = net_data['free_flow'] - -# auxiliary parameters (dict format) to keep the problem linear (capacities as parameters rather than variables) -cap_normal = {(i, j): cap for (i, j), cap in net_data['capacity'].items()} -cap_extend = {(i, j): cap * extension_factor for (i, j), cap in net_data['capacity'].items()} - -# origins and destinations -dests = np.unique([dest for (orig, dest) in list(ods_data.keys())]) -origs = np.unique([orig for (orig, dest) in list(ods_data.keys())]) - -# demand in node-destination form -demand = create_nd_matrix(ods_data, origs, dests, nodes) ``` ## Network Display @@ -518,29 +481,22 @@ Now, let's initiate an instance of the problem based on the problem class we def <!-- #endregion --> <!-- #region id="0491cc69" --> -<div style="background-color:#ffa6a6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"><p><b>Note:</b> population size <code>pop_size</code> was 10 originally. If you change this, you will see different results. This is problem-dependent!</p></div> -<!-- #endregion --> - -<!-- #region id="0491cc69" --> -<div style="background-color:#ffa6a6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"><p><b>This:</b> <code>Budget</code> is the way the number of links was selected (unlike notebook A, which used <code>extension_max_no</code>). </p></div> -<!-- #endregion --> - -<!-- #region id="0491cc69" --> -<div style="background-color:#ffa6a6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"><p><b>This:</b> <code>Budget</code> is the way the number of links was selected (unlike notebook A, which used <code>extension_max_no</code>). The initial value of 76 was trivial, and the solution converged quickly. Numbers between 10 and 40 would have produced <em>much</em> more interesting results (see solution explanation).</p></div> +<div style="background-color:#ffa500; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"><p><b>Note:</b> population size <code>pop_size</code> is 10 originally. If you change this, you will see different results. This is problem-dependent!</p></div> <!-- #endregion --> ```python pycharm={"name": "#%%\n"} -Budget = 76 +extension_max_no = 40 pop_size = 10 # initiate an instance of the problem with max number of selected links as budget constraint -problem = NDP(budget=Budget) +problem = NDP(budget=extension_max_no) # initiate the GA with parameters appropriate for binary variables method = GA(pop_size=pop_size, sampling=BinaryRandomSampling(), mutation=BitflipMutation(), crossover=HalfUniformCrossover() + # replace HalfUniformCrossover() by PointCrossover(2) for two-point crossover ) ``` @@ -548,15 +504,17 @@ method = GA(pop_size=pop_size, Now we are ready to minimize the NDP problem using the GA method we defined. <!-- #endregion --> +<div style="background-color:#a6ffa6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"><p><b>Instructions:</b> Run the GA for 3 minutes initially to observe how the results converge and to understand the process of obtaining the final solution. Once you have familiarized yourself with the mechanism and the behavior of the algorithm, extend the maximum computation time to 10 minutes. Use the results from this extended run as the foundation for addressing the questions outlined in the report. </p></div> + <!-- #region id="0491cc69" --> -<div style="background-color:#ffa6a6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"><p><b>Note:</b> termination is set here as a keyword argument (see note above).</p></div> +<div style="background-color:#ffa500; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"><p><b>Note:</b> Maximum computation time (termination criteria) is set here as a keyword argument.</p></div> <!-- #endregion --> ```python pycharm={"name": "#%%\n"} opt_results = minimize(problem, method, - termination=("time", "00:05:00"), #5 minute maximum computation time + termination=("time", "00:03:00"), #this is maximum computation time seed=7, save_history=True, verbose=True, diff --git a/synced_files/GA_2_5/old_Analysis_GA_solution.py b/synced_files/GA_2_5/old_Analysis_GA_solution.py index b93e4757..e64e370d 100644 --- a/synced_files/GA_2_5/old_Analysis_GA_solution.py +++ b/synced_files/GA_2_5/old_Analysis_GA_solution.py @@ -30,9 +30,6 @@ # *[CEGM1000 MUDE](http://mude.citg.tudelft.nl/): Week 2.5. For: 15 December, 2023.* # -# %% [markdown] id="0491cc69" -# <div style="background-color:#ffa6a6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"><p><b>Note:</b> during the in-class session some of the confusion was caused by code issues. Comments relevant to the code and notebooks as-used in the Friday in-class session are provided in boxes like this.</p></div> - # %% [markdown] pycharm={"name": "#%% md\n"} # ## Introduction # @@ -71,19 +68,7 @@ from pymoo.optimize import minimize from pymoo.operators.sampling.rnd import BinaryRandomSampling from pymoo.operators.crossover.hux import HalfUniformCrossover # from pymoo.operators.mutation.bitflip import BitflipMutation - -# not used here but generally useful dependencies -#from pymoo.core.problem import Problem -#from pymoo.operators.mutation.pm import PolynomialMutation -#from pymoo.operators.crossover.pntx import PointCrossover # -#from pymoo.operators.crossover.sbx import SBX -#from pymoo.operators.crossover.sbx import SimulatedBinaryCrossover -#from pymoo.operators.crossover.pntx import Crossover -#from pymoo.operators.repair.bounds_repair import BoundsRepair -#from pymoo.core.repair import Repair - -# %% [markdown] id="0491cc69" -# <div style="background-color:#ffa6a6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"><p><b>Note:</b> the intention with the commented lines above was to illustrate possible crossover methods you could use, but unfortunately the pymoo documentation was not clear enough to indicate how they could have been used. The methods <code>SinglePointCrossover</code> and <code>TwoPointCrossover</code> (n<em>not</em> listed above!) could have been easily used, as these methods require no additional keyword arguments and would have worked “out of the box;†they were also illustrated with examples in the online textbook. We apologize for this oversight on our part.</p></div> +from pymoo.operators.crossover.pntx import PointCrossover # # %% # For visualization @@ -327,17 +312,9 @@ def create_nd_matrix(ods_data, origins, destinations, nodes): # %% [markdown] pycharm={"name": "#%% md\n"} # Now that we have the required functions for reading and processing the data, let's define some problem parameters and prepare the input. -# %% [markdown] id="0491cc69" -# <div style="background-color:#ffa6a6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"><p><b>Note:</b> the variables <code>extension_max_no</code> and <code>timelimit</code> were both defined here, but never used. <code>extension_max_no</code> is similar to the variable in notebook A with the same name; here it is superceded below by <code>Budget</code>.</p></div> - # %% pycharm={"name": "#%%\n"} # define parameters, case study (network) list and the directory where their files are extension_factor = 1.5 # capacity after extension (1.5 means add 50%) -extension_max_no = 20 # the number of links to add capacity to (simplified way of reprsenting a budget for investing) -#it's the same to say that it's exactly this number of that this number is the max, that's because every investment brings travel time benefits -#even if just one car circulates. -timelimit = 300 # seconds -beta = 2 # parameter to use in link travel time function (explained later) networks = ['SiouxFalls'] networks_dir = os.getcwd() +'/input/TransportationNetworks' @@ -358,17 +335,6 @@ links = list(net_data['capacity'].keys()) nodes = np.unique([list(edge) for edge in links]) fftts = net_data['free_flow'] -# auxiliary parameters (dict format) to keep the problem linear (capacities as parameters rather than variables) -cap_normal = {(i, j): cap for (i, j), cap in net_data['capacity'].items()} -cap_extend = {(i, j): cap * extension_factor for (i, j), cap in net_data['capacity'].items()} - -# origins and destinations -dests = np.unique([dest for (orig, dest) in list(ods_data.keys())]) -origs = np.unique([orig for (orig, dest) in list(ods_data.keys())]) - -# demand in node-destination form -demand = create_nd_matrix(ods_data, origs, dests, nodes) - # %% [markdown] # ## Network Display # We will use the same function we used in the previous notebook to visualize the network. @@ -497,39 +463,37 @@ class NDP(ElementwiseProblem): # Now, let's initiate an instance of the problem based on the problem class we defined, and initiate the GA with its parameters. Note that depending on the problem size and the number of feasible links, you might need larger values for population and generation size to achieve good results or even feasible results. Of course this increases the computation times. # %% [markdown] id="0491cc69" -# <div style="background-color:#ffa6a6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"><p><b>Note:</b> population size <code>pop_size</code> was 10 originally. If you change this, you will see different results. This is problem-dependent!</p></div> - -# %% [markdown] id="0491cc69" -# <div style="background-color:#ffa6a6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"><p><b>This:</b> <code>Budget</code> is the way the number of links was selected (unlike notebook A, which used <code>extension_max_no</code>). </p></div> - -# %% [markdown] id="0491cc69" -# <div style="background-color:#ffa6a6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"><p><b>This:</b> <code>Budget</code> is the way the number of links was selected (unlike notebook A, which used <code>extension_max_no</code>). The initial value of 76 was trivial, and the solution converged quickly. Numbers between 10 and 40 would have produced <em>much</em> more interesting results (see solution explanation).</p></div> +# <div style="background-color:#ffa500; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"><p><b>Note:</b> population size <code>pop_size</code> is 10 originally. If you change this, you will see different results. This is problem-dependent!</p></div> # %% pycharm={"name": "#%%\n"} -Budget = 76 +extension_max_no = 40 pop_size = 10 # initiate an instance of the problem with max number of selected links as budget constraint -problem = NDP(budget=Budget) +problem = NDP(budget=extension_max_no) # initiate the GA with parameters appropriate for binary variables method = GA(pop_size=pop_size, sampling=BinaryRandomSampling(), mutation=BitflipMutation(), crossover=HalfUniformCrossover() + # replace HalfUniformCrossover() by PointCrossover(2) for two-point crossover ) # %% [markdown] pycharm={"name": "#%% md\n"} # Now we are ready to minimize the NDP problem using the GA method we defined. +# %% [markdown] +# <div style="background-color:#a6ffa6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"><p><b>Instructions:</b> Run the GA for 3 minutes initially to observe how the results converge and to understand the process of obtaining the final solution. Once you have familiarized yourself with the mechanism and the behavior of the algorithm, extend the maximum computation time to 10 minutes. Use the results from this extended run as the foundation for addressing the questions outlined in the report. </p></div> + # %% [markdown] id="0491cc69" -# <div style="background-color:#ffa6a6; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"><p><b>Note:</b> termination is set here as a keyword argument (see note above).</p></div> +# <div style="background-color:#ffa500; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"><p><b>Note:</b> Maximum computation time (termination criteria) is set here as a keyword argument.</p></div> # %% pycharm={"name": "#%%\n"} opt_results = minimize(problem, method, - termination=("time", "00:05:00"), #5 minute maximum computation time + termination=("time", "00:03:00"), #this is maximum computation time seed=7, save_history=True, verbose=True, diff --git a/synced_files/Week_2_2/old/PA_2_1_solution_sympy.ipynb b/synced_files/Week_2_2/old/PA_2_1_solution_sympy.ipynb index 7231cc37..104c3a06 100644 --- a/synced_files/Week_2_2/old/PA_2_1_solution_sympy.ipynb +++ b/synced_files/Week_2_2/old/PA_2_1_solution_sympy.ipynb @@ -404,7 +404,7 @@ }, { "cell_type": "markdown", - "id": "fb421a99", + "id": "0e6034c8", "metadata": {}, "source": [ "You could also solve this problem using sympy! What would be the benefit of doing this? Check below how long it will take! You won't need timeit for this one..." @@ -413,7 +413,7 @@ { "cell_type": "code", "execution_count": null, - "id": "9d46ce25", + "id": "10b0eed5", "metadata": {}, "outputs": [], "source": [ @@ -423,7 +423,7 @@ { "cell_type": "code", "execution_count": null, - "id": "77d02ecd", + "id": "33730592", "metadata": {}, "outputs": [], "source": [ @@ -433,7 +433,7 @@ { "cell_type": "code", "execution_count": null, - "id": "4a7fd681", + "id": "2932c802", "metadata": {}, "outputs": [], "source": [ @@ -445,7 +445,7 @@ { "cell_type": "code", "execution_count": null, - "id": "dbd13d4f", + "id": "71e0ad8b", "metadata": {}, "outputs": [], "source": [ @@ -456,7 +456,7 @@ { "cell_type": "code", "execution_count": null, - "id": "b1f1a425", + "id": "662424a4", "metadata": {}, "outputs": [], "source": [ @@ -465,7 +465,7 @@ }, { "cell_type": "markdown", - "id": "695a0650", + "id": "fb9cb604", "metadata": {}, "source": [ "What is the result for the 501th value using float values?" @@ -474,7 +474,7 @@ { "cell_type": "code", "execution_count": null, - "id": "e76d2404", + "id": "577f7b77", "metadata": {}, "outputs": [], "source": [ @@ -514,7 +514,7 @@ }, { "cell_type": "markdown", - "id": "8ee71e48", + "id": "9e2c54ce", "metadata": {}, "source": [] } diff --git a/synced_files/Week_2_2/old/old_PA10_solution_sympy.ipynb b/synced_files/Week_2_2/old/old_PA10_solution_sympy.ipynb index 98b67314..2f7528c3 100644 --- a/synced_files/Week_2_2/old/old_PA10_solution_sympy.ipynb +++ b/synced_files/Week_2_2/old/old_PA10_solution_sympy.ipynb @@ -404,7 +404,7 @@ }, { "cell_type": "markdown", - "id": "f578e197", + "id": "62b145a8", "metadata": {}, "source": [ "You could also solve this problem using sympy! What would be the benefit of doing this? Check below how long it will take! You won't need timeit for this one..." @@ -413,7 +413,7 @@ { "cell_type": "code", "execution_count": null, - "id": "48a54a23", + "id": "7fdb91f6", "metadata": {}, "outputs": [], "source": [ @@ -423,7 +423,7 @@ { "cell_type": "code", "execution_count": null, - "id": "d8c6c3e1", + "id": "c2e7d800", "metadata": {}, "outputs": [], "source": [ @@ -433,7 +433,7 @@ { "cell_type": "code", "execution_count": null, - "id": "66de3bb0", + "id": "9f9c00c6", "metadata": {}, "outputs": [], "source": [ @@ -445,7 +445,7 @@ { "cell_type": "code", "execution_count": null, - "id": "84949595", + "id": "2173c512", "metadata": {}, "outputs": [], "source": [ @@ -456,7 +456,7 @@ { "cell_type": "code", "execution_count": null, - "id": "8f366c94", + "id": "0c87007b", "metadata": {}, "outputs": [], "source": [ @@ -465,7 +465,7 @@ }, { "cell_type": "markdown", - "id": "7d0e9b1a", + "id": "51301705", "metadata": {}, "source": [ "What is the result for the 501th value using float values?" @@ -474,7 +474,7 @@ { "cell_type": "code", "execution_count": null, - "id": "4c2decbe", + "id": "b96f5b71", "metadata": {}, "outputs": [], "source": [ @@ -514,7 +514,7 @@ }, { "cell_type": "markdown", - "id": "f8a738aa", + "id": "0d5c33b6", "metadata": {}, "source": [] } -- GitLab