From 9667f07b622cf7b2b7f56c1b1bf0b34f5ad66a98 Mon Sep 17 00:00:00 2001 From: GitLab CI <gitlab-ci@example.com> Date: Wed, 11 Dec 2024 11:48:30 +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/Analysis_GA_solution.ipynb | 210 ++++-------------- synced_files/GA_2_5/Analysis_GA_solution.md | 184 +++------------ synced_files/GA_2_5/Analysis_GA_solution.py | 180 +++------------ .../GA_2_5/Analysis_LP_solution.ipynb | 137 ++---------- synced_files/GA_2_5/Analysis_LP_solution.md | 128 +---------- synced_files/GA_2_5/Analysis_LP_solution.py | 131 +---------- .../Week_2_2/old/PA_2_1_solution_sympy.ipynb | 18 +- .../old/old_PA10_solution_sympy.ipynb | 18 +- synced_files/Week_2_5/WS_2_5_solution.ipynb | 5 +- synced_files/Week_2_5/WS_2_5_solution.md | 3 +- synced_files/Week_2_5/WS_2_5_solution.py | 3 +- 18 files changed, 205 insertions(+), 882 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 baa0d86a..bbcf40a2 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": "74eb5163", + "id": "11ddc8c7", "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 af32c8d4..eaee666b 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": "c56c5ed9", + "id": "4d6a4ac5", "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": "1ddab905", + "id": "a360a68e", "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": "89a61090", + "id": "78e8bfb0", "metadata": {}, "outputs": [], "source": [ @@ -437,7 +437,7 @@ }, { "cell_type": "markdown", - "id": "f66dfd6b", + "id": "f9e6e4b7", "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": "ae7c2ca1", + "id": "b2b33edf", "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": "1da90060", + "id": "6dc245f9", "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": "6bcfbec9", + "id": "247c8913", "metadata": {}, "outputs": [], "source": [ @@ -589,7 +589,7 @@ { "cell_type": "code", "execution_count": null, - "id": "29dc80dc", + "id": "36f2bad5", "metadata": {}, "outputs": [], "source": [ @@ -602,7 +602,7 @@ }, { "cell_type": "markdown", - "id": "59a4df28", + "id": "fd15799d", "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 e2695119..e62246d1 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": "082cfeb6", + "id": "f36e02f9", "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": "82f960e1", + "id": "f1685ef7", "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": "65d5ce42", + "id": "ee4fa46e", "metadata": {}, "outputs": [], "source": [ @@ -445,7 +445,7 @@ }, { "cell_type": "markdown", - "id": "c6560991", + "id": "1954e862", "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": "7144e349", + "id": "6fbb2622", "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": "c598eb3d", + "id": "a24abfa0", "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": "5a20bb53", + "id": "fdf07f46", "metadata": {}, "outputs": [], "source": [ @@ -598,7 +598,7 @@ { "cell_type": "code", "execution_count": null, - "id": "25ff1cf6", + "id": "52162335", "metadata": {}, "outputs": [], "source": [ @@ -611,7 +611,7 @@ }, { "cell_type": "markdown", - "id": "9f9d6fcf", + "id": "135af4ab", "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 835175cf..2ded1c6e 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": "e0551500", + "id": "6af7f7aa", "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": "b25afd92", + "id": "2775d21f", "metadata": {}, "outputs": [], "source": [ @@ -421,7 +421,7 @@ { "cell_type": "code", "execution_count": null, - "id": "331b9241", + "id": "dc689f4a", "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 cef45ee8..24200dbe 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": "6699cfe5", + "id": "bb49a9ae", "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": "58ed0063", + "id": "06e94020", "metadata": {}, "outputs": [], "source": [ @@ -415,7 +415,7 @@ { "cell_type": "code", "execution_count": null, - "id": "69b87a61", + "id": "20b68004", "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 cf79b4d1..312e6315 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": "497b0342", + "id": "4708e1c2", "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": "4b34b1de", + "id": "4687da18", "metadata": {}, "outputs": [], "source": [ @@ -421,7 +421,7 @@ { "cell_type": "code", "execution_count": null, - "id": "ca03da14", + "id": "cc6cdabe", "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 337a9adc..0db72994 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": "ae008411", + "id": "7a98f23d", "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": "b6835630", + "id": "072da885", "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": "ee4beea0", + "id": "fce8f81c", "metadata": {}, "outputs": [], "source": [ @@ -434,7 +434,7 @@ }, { "cell_type": "markdown", - "id": "259a9790", + "id": "b5a9cb50", "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": "76015db4", + "id": "59dc7afc", "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": "656b2821", + "id": "8542b9a9", "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": "69569954", + "id": "6061a45e", "metadata": {}, "outputs": [], "source": [ diff --git a/synced_files/GA_2_5/Analysis_GA_solution.ipynb b/synced_files/GA_2_5/Analysis_GA_solution.ipynb index 2f434b5f..a3b7b327 100644 --- a/synced_files/GA_2_5/Analysis_GA_solution.ipynb +++ b/synced_files/GA_2_5/Analysis_GA_solution.ipynb @@ -54,43 +54,6 @@ "<div style=\"background-color:#facb8e; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%\"> <p> <b>Note:</b> You will need to select mude-week-2-5 as your kernel as it includes the required packages.</p></div>" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "pycharm": { - "name": "#%%\n" - } - }, - "outputs": [], - "source": [ - "import pandas as pd\n", - "import numpy as np\n", - "import gurobipy as gp\n", - "import matplotlib.pyplot as plt\n", - "\n", - "# Genetic algorithm dependencies. We are importing the pymoo functions that are imporant for applying GA (the package can also apply other methods)\n", - "from pymoo.algorithms.soo.nonconvex.ga import GA\n", - "from pymoo.core.problem import ElementwiseProblem\n", - "from pymoo.optimize import minimize\n", - "from pymoo.operators.sampling.rnd import BinaryRandomSampling\n", - "from pymoo.operators.crossover.hux import HalfUniformCrossover #\n", - "from pymoo.operators.mutation.bitflip import BitflipMutation\n", - "from pymoo.operators.crossover.pntx import PointCrossover # " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# For visualization\n", - "from utils.network_visualization import network_visualization\n", - "from utils.network_visualization_highlight_link import network_visualization_highlight_links\n", - "from utils.network_visualization_upgraded import network_visualization_upgraded" - ] - }, { "cell_type": "markdown", "metadata": { @@ -251,9 +214,31 @@ } }, "source": [ - "## Data preprocessing\n", + "## Part 1: Data preprocessing\n", "\n", - "Our data preprocessing steps are similar to the previous notebook. We use some networks from the well-known transportation networks for benchmarking repository as well as a small toy network for case studies of NDPs. the following functions read data from this repository and perform data preprocessing to have the input and the parameters required for our case studies." + "The demand of the network is given by an **OD matrix**, which will be constructed below. The OD matrix is as table that tells you how many cars go from node i to node j in an a given timeframe. The functions for this can be found in the helper function in utils/read.py. You do not need to edit anything in this codeblock." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "import gurobipy as gp\n", + "import matplotlib.pyplot as plt\n", + "\n", + "# Genetic algorithm dependencies. We are importing the pymoo functions that are imporant for applying GA (the package can also apply other methods)\n", + "from pymoo.algorithms.soo.nonconvex.ga import GA\n", + "from pymoo.core.problem import ElementwiseProblem\n", + "from pymoo.optimize import minimize\n", + "from pymoo.operators.sampling.rnd import BinaryRandomSampling\n", + "from pymoo.operators.crossover.hux import HalfUniformCrossover \n", + "from pymoo.operators.mutation.bitflip import BitflipMutation\n", + "from pymoo.operators.crossover.pntx import PointCrossover \n", + "\n" ] }, { @@ -270,128 +255,19 @@ "import os\n", "import time\n", "\n", - "# read network file\n", - "def read_net(net_file):\n", - " \"\"\"\n", - " read network file\n", - " \"\"\"\n", - "\n", - " net_data = pd.read_csv(net_file, skiprows=8, sep='\\t')\n", - " # make sure all headers are lower case and without trailing spaces\n", - " trimmed = [s.strip().lower() for s in net_data.columns]\n", - " net_data.columns = trimmed\n", - " # And drop the silly first and last columns\n", - " net_data.drop(['~', ';'], axis=1, inplace=True)\n", - " # using dictionary to convert type of specific columns so taht we can assign very small (close to zero) possitive number to it.\n", - " convert_dict = {'free_flow_time': float,\n", - " 'capacity': float,\n", - " 'length': float,\n", - " 'power': float\n", - " }\n", - " \n", - " net_data = net_data.astype(convert_dict)\n", - "\n", - " # make sure everything makes sense (otherwise some solvers throw errors)\n", - " net_data.loc[net_data['free_flow_time'] <= 0, 'free_flow_time'] = 1e-6\n", - " net_data.loc[net_data['capacity'] <= 0, 'capacity'] = 1e-6\n", - " net_data.loc[net_data['length'] <= 0, 'length'] = 1e-6\n", - " net_data.loc[net_data['power'] <= 1, 'power'] = int(4)\n", - " net_data['init_node'] = net_data['init_node'].astype(int)\n", - " net_data['term_node'] = net_data['term_node'].astype(int)\n", - " net_data['b'] = net_data['b'].astype(float)\n", - "\n", - " # extract features in dict format\n", - " links = list(zip(net_data['init_node'], net_data['term_node']))\n", - " caps = dict(zip(links, net_data['capacity']))\n", - " fftt = dict(zip(links, net_data['free_flow_time']))\n", - " lent = dict(zip(links, net_data['length']))\n", - " alpha = dict(zip(links, net_data['b']))\n", - " beta = dict(zip(links, net_data['power']))\n", - "\n", - " net = {'capacity': caps, 'free_flow': fftt, 'length': lent, 'alpha': alpha, 'beta': beta}\n", - "\n", - " return net\n", - "\n", - "\n", - "# read OD matrix (demand)\n", - "def read_od(od_file):\n", - " \"\"\"\n", - " read OD matrix\n", - " \"\"\"\n", - "\n", - " f = open(od_file, 'r')\n", - " all_rows = f.read()\n", - " blocks = all_rows.split('Origin')[1:]\n", - " matrix = {}\n", - " for k in range(len(blocks)):\n", - " orig = blocks[k].split('\\n')\n", - " dests = orig[1:]\n", - " origs = int(orig[0])\n", - "\n", - " d = [eval('{' + a.replace(';', ',').replace(' ', '') + '}') for a in dests]\n", - " destinations = {}\n", - " for i in d:\n", - " destinations = {**destinations, **i}\n", - " matrix[origs] = destinations\n", - " zones = max(matrix.keys())\n", - " od_dict = {}\n", - " for i in range(zones):\n", - " for j in range(zones):\n", - " demand = matrix.get(i + 1, {}).get(j + 1, 0)\n", - " if demand:\n", - " od_dict[(i + 1, j + 1)] = demand\n", - " else:\n", - " od_dict[(i + 1, j + 1)] = 0\n", - "\n", - " return od_dict\n", - "\n", - "\n", - "# read case study data\n", - "def read_cases(networks, input_dir):\n", - " \"\"\"\n", - " read case study data\n", - " \"\"\"\n", - "\n", - " # dictionaries for network and OD files\n", - " net_dict = {}\n", - " ods_dict = {}\n", - "\n", - " # selected case studies\n", - " if networks:\n", - " cases = [case for case in networks]\n", - " else:\n", - " # all folders available (each one for one specific case)\n", - " cases = [x for x in os.listdir(input_dir) if os.path.isdir(os.path.join(input_dir, x))]\n", - "\n", - " # iterate through cases and read network and OD\n", - " for case in cases:\n", - " mod = os.path.join(input_dir, case)\n", - " mod_files = os.listdir(mod)\n", - " for i in mod_files:\n", - " # read network\n", - " if i.lower()[-8:] == 'net.tntp':\n", - " net_file = os.path.join(mod, i)\n", - " net_dict[case] = read_net(net_file)\n", - " # read OD matrix\n", - " if 'TRIPS' in i.upper() and i.lower()[-5:] == '.tntp':\n", - " ods_file = os.path.join(mod, i)\n", - " ods_dict[case] = read_od(ods_file)\n", - "\n", - " return net_dict, ods_dict\n", - "\n", - "\n", - "# create node-destination demand matrix\n", - "def create_nd_matrix(ods_data, origins, destinations, nodes):\n", - " # create node-destination demand matrix (not a regular OD!)\n", - " demand = {(n, d): 0 for n in nodes for d in destinations}\n", - " for r in origins:\n", - " for s in destinations:\n", - " if (r, s) in ods_data:\n", - " demand[r, s] = ods_data[r, s]\n", - " for s in destinations:\n", - " demand[s, s] = - sum(demand[j, s] for j in origins)\n", - "\n", - " return demand\n" + "# import required functions\n", + "from utils.read import read_cases\n", + "from utils.read import read_net\n", + "from utils.read import read_od\n", + "from utils.read import create_nd_matrix\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Network Display\n", + "We will use the same function we used in the previous notebook to visualize the network. " ] }, { @@ -441,9 +317,17 @@ { "cell_type": "markdown", "metadata": {}, + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ - "## Network Display\n", - "We will use the same function we used in the previous notebook to visualize the network. " + "from utils.network_visualization import network_visualization\n", + "from utils.network_visualization_highlight_link import network_visualization_highlight_links\n", + "from utils.network_visualization_upgraded import network_visualization_upgraded" ] }, { diff --git a/synced_files/GA_2_5/Analysis_GA_solution.md b/synced_files/GA_2_5/Analysis_GA_solution.md index 7a0e531a..d47cc5cd 100644 --- a/synced_files/GA_2_5/Analysis_GA_solution.md +++ b/synced_files/GA_2_5/Analysis_GA_solution.md @@ -47,29 +47,6 @@ _Note: part of the background material for this project was already available in <div style="background-color:#facb8e; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"> <p> <b>Note:</b> You will need to select mude-week-2-5 as your kernel as it includes the required packages.</p></div> <!-- #endregion --> -```python pycharm={"name": "#%%\n"} -import pandas as pd -import numpy as np -import gurobipy as gp -import matplotlib.pyplot as plt - -# Genetic algorithm dependencies. We are importing the pymoo functions that are imporant for applying GA (the package can also apply other methods) -from pymoo.algorithms.soo.nonconvex.ga import GA -from pymoo.core.problem import ElementwiseProblem -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 -from pymoo.operators.crossover.pntx import PointCrossover # -``` - -```python -# For visualization -from utils.network_visualization import network_visualization -from utils.network_visualization_highlight_link import network_visualization_highlight_links -from utils.network_visualization_upgraded import network_visualization_upgraded -``` - <!-- #region pycharm={"name": "#%% md\n"} --> ## Genetic algorithm for NDP @@ -174,141 +151,45 @@ The following is a diagram that shows what you are finally doing to solve the sa  <!-- #region pycharm={"name": "#%% md\n"} --> -## Data preprocessing +## Part 1: Data preprocessing -Our data preprocessing steps are similar to the previous notebook. We use some networks from the well-known transportation networks for benchmarking repository as well as a small toy network for case studies of NDPs. the following functions read data from this repository and perform data preprocessing to have the input and the parameters required for our case studies. +The demand of the network is given by an **OD matrix**, which will be constructed below. The OD matrix is as table that tells you how many cars go from node i to node j in an a given timeframe. The functions for this can be found in the helper function in utils/read.py. You do not need to edit anything in this codeblock. <!-- #endregion --> +```python +import pandas as pd +import numpy as np +import gurobipy as gp +import matplotlib.pyplot as plt + +# Genetic algorithm dependencies. We are importing the pymoo functions that are imporant for applying GA (the package can also apply other methods) +from pymoo.algorithms.soo.nonconvex.ga import GA +from pymoo.core.problem import ElementwiseProblem +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 +from pymoo.operators.crossover.pntx import PointCrossover + + +``` + ```python pycharm={"name": "#%%\n"} # import required packages import os import time -# read network file -def read_net(net_file): - """ - read network file - """ - - net_data = pd.read_csv(net_file, skiprows=8, sep='\t') - # make sure all headers are lower case and without trailing spaces - trimmed = [s.strip().lower() for s in net_data.columns] - net_data.columns = trimmed - # And drop the silly first and last columns - net_data.drop(['~', ';'], axis=1, inplace=True) - # using dictionary to convert type of specific columns so taht we can assign very small (close to zero) possitive number to it. - convert_dict = {'free_flow_time': float, - 'capacity': float, - 'length': float, - 'power': float - } - - net_data = net_data.astype(convert_dict) - - # make sure everything makes sense (otherwise some solvers throw errors) - net_data.loc[net_data['free_flow_time'] <= 0, 'free_flow_time'] = 1e-6 - net_data.loc[net_data['capacity'] <= 0, 'capacity'] = 1e-6 - net_data.loc[net_data['length'] <= 0, 'length'] = 1e-6 - net_data.loc[net_data['power'] <= 1, 'power'] = int(4) - net_data['init_node'] = net_data['init_node'].astype(int) - net_data['term_node'] = net_data['term_node'].astype(int) - net_data['b'] = net_data['b'].astype(float) - - # extract features in dict format - links = list(zip(net_data['init_node'], net_data['term_node'])) - caps = dict(zip(links, net_data['capacity'])) - fftt = dict(zip(links, net_data['free_flow_time'])) - lent = dict(zip(links, net_data['length'])) - alpha = dict(zip(links, net_data['b'])) - beta = dict(zip(links, net_data['power'])) - - net = {'capacity': caps, 'free_flow': fftt, 'length': lent, 'alpha': alpha, 'beta': beta} - - return net - - -# read OD matrix (demand) -def read_od(od_file): - """ - read OD matrix - """ - - f = open(od_file, 'r') - all_rows = f.read() - blocks = all_rows.split('Origin')[1:] - matrix = {} - for k in range(len(blocks)): - orig = blocks[k].split('\n') - dests = orig[1:] - origs = int(orig[0]) - - d = [eval('{' + a.replace(';', ',').replace(' ', '') + '}') for a in dests] - destinations = {} - for i in d: - destinations = {**destinations, **i} - matrix[origs] = destinations - zones = max(matrix.keys()) - od_dict = {} - for i in range(zones): - for j in range(zones): - demand = matrix.get(i + 1, {}).get(j + 1, 0) - if demand: - od_dict[(i + 1, j + 1)] = demand - else: - od_dict[(i + 1, j + 1)] = 0 - - return od_dict - - -# read case study data -def read_cases(networks, input_dir): - """ - read case study data - """ - - # dictionaries for network and OD files - net_dict = {} - ods_dict = {} - - # selected case studies - if networks: - cases = [case for case in networks] - else: - # all folders available (each one for one specific case) - cases = [x for x in os.listdir(input_dir) if os.path.isdir(os.path.join(input_dir, x))] - - # iterate through cases and read network and OD - for case in cases: - mod = os.path.join(input_dir, case) - mod_files = os.listdir(mod) - for i in mod_files: - # read network - if i.lower()[-8:] == 'net.tntp': - net_file = os.path.join(mod, i) - net_dict[case] = read_net(net_file) - # read OD matrix - if 'TRIPS' in i.upper() and i.lower()[-5:] == '.tntp': - ods_file = os.path.join(mod, i) - ods_dict[case] = read_od(ods_file) - - return net_dict, ods_dict - - -# create node-destination demand matrix -def create_nd_matrix(ods_data, origins, destinations, nodes): - # create node-destination demand matrix (not a regular OD!) - demand = {(n, d): 0 for n in nodes for d in destinations} - for r in origins: - for s in destinations: - if (r, s) in ods_data: - demand[r, s] = ods_data[r, s] - for s in destinations: - demand[s, s] = - sum(demand[j, s] for j in origins) - - return demand +# import required functions +from utils.read import read_cases +from utils.read import read_net +from utils.read import read_od +from utils.read import create_nd_matrix ``` +### Network Display +We will use the same function we used in the previous notebook to visualize the network. + <!-- #region 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. <!-- #endregion --> @@ -337,8 +218,13 @@ nodes = np.unique([list(edge) for edge in links]) fftts = net_data['free_flow'] ``` -## Network Display -We will use the same function we used in the previous notebook to visualize the network. + + +```python +from utils.network_visualization import network_visualization +from utils.network_visualization_highlight_link import network_visualization_highlight_links +from utils.network_visualization_upgraded import network_visualization_upgraded +``` ```python coordinates_path = 'input/TransportationNetworks/SiouxFalls/SiouxFallsCoordinates.geojson' diff --git a/synced_files/GA_2_5/Analysis_GA_solution.py b/synced_files/GA_2_5/Analysis_GA_solution.py index 9a94e680..76cb8b74 100644 --- a/synced_files/GA_2_5/Analysis_GA_solution.py +++ b/synced_files/GA_2_5/Analysis_GA_solution.py @@ -44,27 +44,6 @@ # %% [markdown] pycharm={"name": "#%% md\n"} # <div style="background-color:#facb8e; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"> <p> <b>Note:</b> You will need to select mude-week-2-5 as your kernel as it includes the required packages.</p></div> -# %% pycharm={"name": "#%%\n"} -import pandas as pd -import numpy as np -import gurobipy as gp -import matplotlib.pyplot as plt - -# Genetic algorithm dependencies. We are importing the pymoo functions that are imporant for applying GA (the package can also apply other methods) -from pymoo.algorithms.soo.nonconvex.ga import GA -from pymoo.core.problem import ElementwiseProblem -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 -from pymoo.operators.crossover.pntx import PointCrossover # - -# %% -# For visualization -from utils.network_visualization import network_visualization -from utils.network_visualization_highlight_link import network_visualization_highlight_links -from utils.network_visualization_upgraded import network_visualization_upgraded - # %% [markdown] pycharm={"name": "#%% md\n"} # ## Genetic algorithm for NDP # @@ -164,139 +143,42 @@ from utils.network_visualization_upgraded import network_visualization_upgraded #  # %% [markdown] pycharm={"name": "#%% md\n"} -# ## Data preprocessing +# ## Part 1: Data preprocessing # -# Our data preprocessing steps are similar to the previous notebook. We use some networks from the well-known transportation networks for benchmarking repository as well as a small toy network for case studies of NDPs. the following functions read data from this repository and perform data preprocessing to have the input and the parameters required for our case studies. +# The demand of the network is given by an **OD matrix**, which will be constructed below. The OD matrix is as table that tells you how many cars go from node i to node j in an a given timeframe. The functions for this can be found in the helper function in utils/read.py. You do not need to edit anything in this codeblock. + +# %% +import pandas as pd +import numpy as np +import gurobipy as gp +import matplotlib.pyplot as plt + +# Genetic algorithm dependencies. We are importing the pymoo functions that are imporant for applying GA (the package can also apply other methods) +from pymoo.algorithms.soo.nonconvex.ga import GA +from pymoo.core.problem import ElementwiseProblem +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 +from pymoo.operators.crossover.pntx import PointCrossover + + # %% pycharm={"name": "#%%\n"} # import required packages import os import time -# read network file -def read_net(net_file): - """ - read network file - """ - - net_data = pd.read_csv(net_file, skiprows=8, sep='\t') - # make sure all headers are lower case and without trailing spaces - trimmed = [s.strip().lower() for s in net_data.columns] - net_data.columns = trimmed - # And drop the silly first and last columns - net_data.drop(['~', ';'], axis=1, inplace=True) - # using dictionary to convert type of specific columns so taht we can assign very small (close to zero) possitive number to it. - convert_dict = {'free_flow_time': float, - 'capacity': float, - 'length': float, - 'power': float - } - - net_data = net_data.astype(convert_dict) - - # make sure everything makes sense (otherwise some solvers throw errors) - net_data.loc[net_data['free_flow_time'] <= 0, 'free_flow_time'] = 1e-6 - net_data.loc[net_data['capacity'] <= 0, 'capacity'] = 1e-6 - net_data.loc[net_data['length'] <= 0, 'length'] = 1e-6 - net_data.loc[net_data['power'] <= 1, 'power'] = int(4) - net_data['init_node'] = net_data['init_node'].astype(int) - net_data['term_node'] = net_data['term_node'].astype(int) - net_data['b'] = net_data['b'].astype(float) - - # extract features in dict format - links = list(zip(net_data['init_node'], net_data['term_node'])) - caps = dict(zip(links, net_data['capacity'])) - fftt = dict(zip(links, net_data['free_flow_time'])) - lent = dict(zip(links, net_data['length'])) - alpha = dict(zip(links, net_data['b'])) - beta = dict(zip(links, net_data['power'])) - - net = {'capacity': caps, 'free_flow': fftt, 'length': lent, 'alpha': alpha, 'beta': beta} - - return net - - -# read OD matrix (demand) -def read_od(od_file): - """ - read OD matrix - """ - - f = open(od_file, 'r') - all_rows = f.read() - blocks = all_rows.split('Origin')[1:] - matrix = {} - for k in range(len(blocks)): - orig = blocks[k].split('\n') - dests = orig[1:] - origs = int(orig[0]) - - d = [eval('{' + a.replace(';', ',').replace(' ', '') + '}') for a in dests] - destinations = {} - for i in d: - destinations = {**destinations, **i} - matrix[origs] = destinations - zones = max(matrix.keys()) - od_dict = {} - for i in range(zones): - for j in range(zones): - demand = matrix.get(i + 1, {}).get(j + 1, 0) - if demand: - od_dict[(i + 1, j + 1)] = demand - else: - od_dict[(i + 1, j + 1)] = 0 - - return od_dict - - -# read case study data -def read_cases(networks, input_dir): - """ - read case study data - """ - - # dictionaries for network and OD files - net_dict = {} - ods_dict = {} - - # selected case studies - if networks: - cases = [case for case in networks] - else: - # all folders available (each one for one specific case) - cases = [x for x in os.listdir(input_dir) if os.path.isdir(os.path.join(input_dir, x))] - - # iterate through cases and read network and OD - for case in cases: - mod = os.path.join(input_dir, case) - mod_files = os.listdir(mod) - for i in mod_files: - # read network - if i.lower()[-8:] == 'net.tntp': - net_file = os.path.join(mod, i) - net_dict[case] = read_net(net_file) - # read OD matrix - if 'TRIPS' in i.upper() and i.lower()[-5:] == '.tntp': - ods_file = os.path.join(mod, i) - ods_dict[case] = read_od(ods_file) - - return net_dict, ods_dict - - -# create node-destination demand matrix -def create_nd_matrix(ods_data, origins, destinations, nodes): - # create node-destination demand matrix (not a regular OD!) - demand = {(n, d): 0 for n in nodes for d in destinations} - for r in origins: - for s in destinations: - if (r, s) in ods_data: - demand[r, s] = ods_data[r, s] - for s in destinations: - demand[s, s] = - sum(demand[j, s] for j in origins) - - return demand +# import required functions +from utils.read import read_cases +from utils.read import read_net +from utils.read import read_od +from utils.read import create_nd_matrix +# %% [markdown] +# ### Network Display +# We will use the same function we used in the previous notebook to visualize the network. # %% [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. @@ -325,8 +207,12 @@ nodes = np.unique([list(edge) for edge in links]) fftts = net_data['free_flow'] # %% [markdown] -# ## Network Display -# We will use the same function we used in the previous notebook to visualize the network. +# + +# %% +from utils.network_visualization import network_visualization +from utils.network_visualization_highlight_link import network_visualization_highlight_links +from utils.network_visualization_upgraded import network_visualization_upgraded # %% coordinates_path = 'input/TransportationNetworks/SiouxFalls/SiouxFallsCoordinates.geojson' diff --git a/synced_files/GA_2_5/Analysis_LP_solution.ipynb b/synced_files/GA_2_5/Analysis_LP_solution.ipynb index f7fdd785..c3afdcca 100644 --- a/synced_files/GA_2_5/Analysis_LP_solution.ipynb +++ b/synced_files/GA_2_5/Analysis_LP_solution.ipynb @@ -68,6 +68,13 @@ "Using the simplifcations and assumptions referred to above we can formulate an NDP and solve it using the branch and bound method (that you have studied before).\n" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<div style=\"background-color:#facb8e; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%\"> <p> <b>Note:</b> You will need to select mude-week-2-5 as your kernel as it includes the required packages.</p></div>" + ] + }, { "cell_type": "markdown", "metadata": { @@ -79,16 +86,7 @@ "\n", "## Part 1: Data preprocessing\n", "\n", - "The following functions reads the data from from the input folder and performs data preprocessing.\n", - "\n", - "The demand of the network is given by an **OD matrix**, which will be constructed below. The OD matrix is as table that tells you how many cars go from node i to node j in an a given timeframe. The paths between the nodes are chosen by solving the optimization model." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "<div style=\"background-color:#facb8e; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%\"> <p> <b>Note:</b> You will need to select mude-week-2-5 as your kernel as it includes the required packages.</p></div>" + "The demand of the network is given by an **OD matrix**, which will be constructed below. The OD matrix is as table that tells you how many cars go from node i to node j in an a given timeframe. The functions for this can be found in the helper function in utils/read.py. You do not need to edit anything in this codeblock." ] }, { @@ -117,118 +115,11 @@ "import os\n", "import time\n", "\n", - "# read network file\n", - "def read_net(net_file):\n", - " \"\"\"\n", - " read network file\n", - " \"\"\"\n", - " net_data = pd.read_csv(net_file, skiprows=8, sep='\\t')\n", - " trimmed = [s.strip().lower() for s in net_data.columns]\n", - " net_data.columns = trimmed\n", - " net_data.drop(['~', ';'], axis=1, inplace=True)\n", - " convert_dict = {'free_flow_time': float,\n", - " 'capacity': float,\n", - " 'length': float,\n", - " 'power': float\n", - " }\n", - " \n", - " net_data = net_data.astype(convert_dict)\n", - "\n", - " net_data.loc[net_data['free_flow_time'] <= 0, 'free_flow_time'] = 1e-6\n", - " net_data.loc[net_data['capacity'] <= 0, 'capacity'] = 1e-6\n", - " net_data.loc[net_data['length'] <= 0, 'length'] = 1e-6\n", - " net_data.loc[net_data['power'] <= 1, 'power'] = int(4)\n", - " net_data['init_node'] = net_data['init_node'].astype(int)\n", - " net_data['term_node'] = net_data['term_node'].astype(int)\n", - " net_data['b'] = net_data['b'].astype(float)\n", - "\n", - " links = list(zip(net_data['init_node'], net_data['term_node']))\n", - " caps = dict(zip(links, net_data['capacity']))\n", - " fftt = dict(zip(links, net_data['free_flow_time']))\n", - " lent = dict(zip(links, net_data['length']))\n", - " alpha = dict(zip(links, net_data['b']))\n", - " beta = dict(zip(links, net_data['power']))\n", - "\n", - " net = {'capacity': caps, 'free_flow': fftt, 'length': lent, 'alpha': alpha, 'beta': beta}\n", - "\n", - " return net\n", - "\n", - "\n", - "# read OD matrix\n", - "def read_od(od_file):\n", - " \"\"\"\n", - " read OD matrix\n", - " \"\"\"\n", - " f = open(od_file, 'r')\n", - " all_rows = f.read()\n", - " blocks = all_rows.split('Origin')[1:]\n", - " matrix = {}\n", - "\n", - " for k in range(len(blocks)):\n", - " orig = blocks[k].split('\\n')\n", - " dests = orig[1:]\n", - " origs = int(orig[0])\n", - "\n", - " d = [eval('{' + a.replace(';', ',').replace(' ', '') + '}') for a in dests]\n", - " destinations = {}\n", - "\n", - " for i in d:\n", - " destinations = {**destinations, **i}\n", - " matrix[origs] = destinations\n", - "\n", - " zones = max(matrix.keys())\n", - " od_dict = {}\n", - "\n", - " for i in range(zones):\n", - " for j in range(zones):\n", - " demand = matrix.get(i + 1, {}).get(j + 1, 0)\n", - " if demand:\n", - " od_dict[(i + 1, j + 1)] = demand\n", - " else:\n", - " od_dict[(i + 1, j + 1)] = 0\n", - "\n", - " return od_dict\n", - "\n", - "\n", - "# read case study data, different case studies that have different demand and road network \n", - "def read_cases(networks, input_dir):\n", - " \"\"\"\n", - " read case study data\n", - " \"\"\"\n", - " net_dict = {}\n", - " ods_dict = {}\n", - "\n", - " if networks:\n", - " cases = [case for case in networks]\n", - " else:\n", - " cases = [x for x in os.listdir(input_dir) if os.path.isdir(os.path.join(input_dir, x))]\n", - "\n", - " for case in cases:\n", - " mod = os.path.join(input_dir, case)\n", - " mod_files = os.listdir(mod)\n", - "\n", - " for i in mod_files:\n", - " if i.lower()[-8:] == 'net.tntp':\n", - " net_file = os.path.join(mod, i)\n", - " net_dict[case] = read_net(net_file)\n", - " if 'TRIPS' in i.upper() and i.lower()[-5:] == '.tntp':\n", - " ods_file = os.path.join(mod, i)\n", - " ods_dict[case] = read_od(ods_file)\n", - "\n", - " return net_dict, ods_dict\n", - "\n", - "\n", - "# create node-destination demand matrix (not a regular OD!)\n", - "def create_nd_matrix(ods_data, origins, destinations, nodes):\n", - " demand = {(n, d): 0 for n in nodes for d in destinations}\n", - " for r in origins:\n", - " for s in destinations:\n", - " if (r, s) in ods_data:\n", - " demand[r, s] = ods_data[r, s]\n", - " for s in destinations:\n", - " demand[s, s] = - sum(demand[j, s] for j in origins)\n", - "\n", - " return demand\n" + "# import required functions\n", + "from utils.read import read_cases\n", + "from utils.read import read_net\n", + "from utils.read import read_od\n", + "from utils.read import create_nd_matrix" ] }, { @@ -376,7 +267,7 @@ "outputs": [], "source": [ "# define parameters\n", - "extension_factor = 2 # capacity after extension\n", + "extension_factor = 1.5 # capacity after extension\n", "extension_max_no = 40 # simplified budget limit\n", "timelimit = 300 # seconds\n", "beta = 2 # explained later\n", diff --git a/synced_files/GA_2_5/Analysis_LP_solution.md b/synced_files/GA_2_5/Analysis_LP_solution.md index 8f283c04..f201766b 100644 --- a/synced_files/GA_2_5/Analysis_LP_solution.md +++ b/synced_files/GA_2_5/Analysis_LP_solution.md @@ -67,17 +67,15 @@ Using the simplifcations and assumptions referred to above we can formulate an N <!-- #endregion --> +<div style="background-color:#facb8e; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"> <p> <b>Note:</b> You will need to select mude-week-2-5 as your kernel as it includes the required packages.</p></div> + <!-- #region pycharm={"name": "#%% md\n"} --> ## Part 1: Data preprocessing -The following functions reads the data from from the input folder and performs data preprocessing. - -The demand of the network is given by an **OD matrix**, which will be constructed below. The OD matrix is as table that tells you how many cars go from node i to node j in an a given timeframe. The paths between the nodes are chosen by solving the optimization model. +The demand of the network is given by an **OD matrix**, which will be constructed below. The OD matrix is as table that tells you how many cars go from node i to node j in an a given timeframe. The functions for this can be found in the helper function in utils/read.py. You do not need to edit anything in this codeblock. <!-- #endregion --> -<div style="background-color:#facb8e; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"> <p> <b>Note:</b> You will need to select mude-week-2-5 as your kernel as it includes the required packages.</p></div> - ```python import gurobipy as gp import pandas as pd @@ -90,119 +88,11 @@ from matplotlib import pyplot as plt import os import time -# read network file -def read_net(net_file): - """ - read network file - """ - net_data = pd.read_csv(net_file, skiprows=8, sep='\t') - trimmed = [s.strip().lower() for s in net_data.columns] - net_data.columns = trimmed - net_data.drop(['~', ';'], axis=1, inplace=True) - convert_dict = {'free_flow_time': float, - 'capacity': float, - 'length': float, - 'power': float - } - - net_data = net_data.astype(convert_dict) - - net_data.loc[net_data['free_flow_time'] <= 0, 'free_flow_time'] = 1e-6 - net_data.loc[net_data['capacity'] <= 0, 'capacity'] = 1e-6 - net_data.loc[net_data['length'] <= 0, 'length'] = 1e-6 - net_data.loc[net_data['power'] <= 1, 'power'] = int(4) - net_data['init_node'] = net_data['init_node'].astype(int) - net_data['term_node'] = net_data['term_node'].astype(int) - net_data['b'] = net_data['b'].astype(float) - - links = list(zip(net_data['init_node'], net_data['term_node'])) - caps = dict(zip(links, net_data['capacity'])) - fftt = dict(zip(links, net_data['free_flow_time'])) - lent = dict(zip(links, net_data['length'])) - alpha = dict(zip(links, net_data['b'])) - beta = dict(zip(links, net_data['power'])) - - net = {'capacity': caps, 'free_flow': fftt, 'length': lent, 'alpha': alpha, 'beta': beta} - - return net - - -# read OD matrix -def read_od(od_file): - """ - read OD matrix - """ - f = open(od_file, 'r') - all_rows = f.read() - blocks = all_rows.split('Origin')[1:] - matrix = {} - - for k in range(len(blocks)): - orig = blocks[k].split('\n') - dests = orig[1:] - origs = int(orig[0]) - - d = [eval('{' + a.replace(';', ',').replace(' ', '') + '}') for a in dests] - destinations = {} - - for i in d: - destinations = {**destinations, **i} - matrix[origs] = destinations - - zones = max(matrix.keys()) - od_dict = {} - - for i in range(zones): - for j in range(zones): - demand = matrix.get(i + 1, {}).get(j + 1, 0) - if demand: - od_dict[(i + 1, j + 1)] = demand - else: - od_dict[(i + 1, j + 1)] = 0 - - return od_dict - - -# read case study data, different case studies that have different demand and road network -def read_cases(networks, input_dir): - """ - read case study data - """ - net_dict = {} - ods_dict = {} - - if networks: - cases = [case for case in networks] - else: - cases = [x for x in os.listdir(input_dir) if os.path.isdir(os.path.join(input_dir, x))] - - for case in cases: - mod = os.path.join(input_dir, case) - mod_files = os.listdir(mod) - - for i in mod_files: - if i.lower()[-8:] == 'net.tntp': - net_file = os.path.join(mod, i) - net_dict[case] = read_net(net_file) - if 'TRIPS' in i.upper() and i.lower()[-5:] == '.tntp': - ods_file = os.path.join(mod, i) - ods_dict[case] = read_od(ods_file) - - return net_dict, ods_dict - - -# create node-destination demand matrix (not a regular OD!) -def create_nd_matrix(ods_data, origins, destinations, nodes): - demand = {(n, d): 0 for n in nodes for d in destinations} - for r in origins: - for s in destinations: - if (r, s) in ods_data: - demand[r, s] = ods_data[r, s] - for s in destinations: - demand[s, s] = - sum(demand[j, s] for j in origins) - - return demand - +# import required functions +from utils.read import read_cases +from utils.read import read_net +from utils.read import read_od +from utils.read import create_nd_matrix ``` ### Visualisation @@ -300,7 +190,7 @@ Now that we have the required functions for reading and processing the data, let ```python # define parameters -extension_factor = 2 # capacity after extension +extension_factor = 1.5 # capacity after extension extension_max_no = 40 # simplified budget limit timelimit = 300 # seconds beta = 2 # explained later diff --git a/synced_files/GA_2_5/Analysis_LP_solution.py b/synced_files/GA_2_5/Analysis_LP_solution.py index acd98158..86cc5586 100644 --- a/synced_files/GA_2_5/Analysis_LP_solution.py +++ b/synced_files/GA_2_5/Analysis_LP_solution.py @@ -65,16 +65,14 @@ # Using the simplifcations and assumptions referred to above we can formulate an NDP and solve it using the branch and bound method (that you have studied before). # +# %% [markdown] +# <div style="background-color:#facb8e; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"> <p> <b>Note:</b> You will need to select mude-week-2-5 as your kernel as it includes the required packages.</p></div> + # %% [markdown] pycharm={"name": "#%% md\n"} # # ## Part 1: Data preprocessing # -# The following functions reads the data from from the input folder and performs data preprocessing. -# -# The demand of the network is given by an **OD matrix**, which will be constructed below. The OD matrix is as table that tells you how many cars go from node i to node j in an a given timeframe. The paths between the nodes are chosen by solving the optimization model. - -# %% [markdown] -# <div style="background-color:#facb8e; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"> <p> <b>Note:</b> You will need to select mude-week-2-5 as your kernel as it includes the required packages.</p></div> +# The demand of the network is given by an **OD matrix**, which will be constructed below. The OD matrix is as table that tells you how many cars go from node i to node j in an a given timeframe. The functions for this can be found in the helper function in utils/read.py. You do not need to edit anything in this codeblock. # %% import gurobipy as gp @@ -87,120 +85,11 @@ from matplotlib import pyplot as plt import os import time -# read network file -def read_net(net_file): - """ - read network file - """ - net_data = pd.read_csv(net_file, skiprows=8, sep='\t') - trimmed = [s.strip().lower() for s in net_data.columns] - net_data.columns = trimmed - net_data.drop(['~', ';'], axis=1, inplace=True) - convert_dict = {'free_flow_time': float, - 'capacity': float, - 'length': float, - 'power': float - } - - net_data = net_data.astype(convert_dict) - - net_data.loc[net_data['free_flow_time'] <= 0, 'free_flow_time'] = 1e-6 - net_data.loc[net_data['capacity'] <= 0, 'capacity'] = 1e-6 - net_data.loc[net_data['length'] <= 0, 'length'] = 1e-6 - net_data.loc[net_data['power'] <= 1, 'power'] = int(4) - net_data['init_node'] = net_data['init_node'].astype(int) - net_data['term_node'] = net_data['term_node'].astype(int) - net_data['b'] = net_data['b'].astype(float) - - links = list(zip(net_data['init_node'], net_data['term_node'])) - caps = dict(zip(links, net_data['capacity'])) - fftt = dict(zip(links, net_data['free_flow_time'])) - lent = dict(zip(links, net_data['length'])) - alpha = dict(zip(links, net_data['b'])) - beta = dict(zip(links, net_data['power'])) - - net = {'capacity': caps, 'free_flow': fftt, 'length': lent, 'alpha': alpha, 'beta': beta} - - return net - - -# read OD matrix -def read_od(od_file): - """ - read OD matrix - """ - f = open(od_file, 'r') - all_rows = f.read() - blocks = all_rows.split('Origin')[1:] - matrix = {} - - for k in range(len(blocks)): - orig = blocks[k].split('\n') - dests = orig[1:] - origs = int(orig[0]) - - d = [eval('{' + a.replace(';', ',').replace(' ', '') + '}') for a in dests] - destinations = {} - - for i in d: - destinations = {**destinations, **i} - matrix[origs] = destinations - - zones = max(matrix.keys()) - od_dict = {} - - for i in range(zones): - for j in range(zones): - demand = matrix.get(i + 1, {}).get(j + 1, 0) - if demand: - od_dict[(i + 1, j + 1)] = demand - else: - od_dict[(i + 1, j + 1)] = 0 - - return od_dict - - -# read case study data, different case studies that have different demand and road network -def read_cases(networks, input_dir): - """ - read case study data - """ - net_dict = {} - ods_dict = {} - - if networks: - cases = [case for case in networks] - else: - cases = [x for x in os.listdir(input_dir) if os.path.isdir(os.path.join(input_dir, x))] - - for case in cases: - mod = os.path.join(input_dir, case) - mod_files = os.listdir(mod) - - for i in mod_files: - if i.lower()[-8:] == 'net.tntp': - net_file = os.path.join(mod, i) - net_dict[case] = read_net(net_file) - if 'TRIPS' in i.upper() and i.lower()[-5:] == '.tntp': - ods_file = os.path.join(mod, i) - ods_dict[case] = read_od(ods_file) - - return net_dict, ods_dict - - -# create node-destination demand matrix (not a regular OD!) -def create_nd_matrix(ods_data, origins, destinations, nodes): - demand = {(n, d): 0 for n in nodes for d in destinations} - for r in origins: - for s in destinations: - if (r, s) in ods_data: - demand[r, s] = ods_data[r, s] - for s in destinations: - demand[s, s] = - sum(demand[j, s] for j in origins) - - return demand - - +# import required functions +from utils.read import read_cases +from utils.read import read_net +from utils.read import read_od +from utils.read import create_nd_matrix # %% [markdown] # ### Visualisation @@ -294,7 +183,7 @@ plt.show() # %% # define parameters -extension_factor = 2 # capacity after extension +extension_factor = 1.5 # capacity after extension extension_max_no = 40 # simplified budget limit timelimit = 300 # seconds beta = 2 # explained later 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 814bcb97..d62595f5 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": "ad0832a4", + "id": "2d17caa6", "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": "70264da2", + "id": "19e5dbc1", "metadata": {}, "outputs": [], "source": [ @@ -423,7 +423,7 @@ { "cell_type": "code", "execution_count": null, - "id": "e203d666", + "id": "71ef0f93", "metadata": {}, "outputs": [], "source": [ @@ -433,7 +433,7 @@ { "cell_type": "code", "execution_count": null, - "id": "1cccfdb0", + "id": "1ad5a2ea", "metadata": {}, "outputs": [], "source": [ @@ -445,7 +445,7 @@ { "cell_type": "code", "execution_count": null, - "id": "18cde03f", + "id": "076e8651", "metadata": {}, "outputs": [], "source": [ @@ -456,7 +456,7 @@ { "cell_type": "code", "execution_count": null, - "id": "8f742256", + "id": "24a08ec0", "metadata": {}, "outputs": [], "source": [ @@ -465,7 +465,7 @@ }, { "cell_type": "markdown", - "id": "078cc85d", + "id": "70a7e741", "metadata": {}, "source": [ "What is the result for the 501th value using float values?" @@ -474,7 +474,7 @@ { "cell_type": "code", "execution_count": null, - "id": "27e5f0d0", + "id": "9f8c31cb", "metadata": {}, "outputs": [], "source": [ @@ -514,7 +514,7 @@ }, { "cell_type": "markdown", - "id": "5832dcfa", + "id": "206167c6", "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 e41ca9b9..432bc904 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": "7733d77b", + "id": "cfacddfb", "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": "4bad3c92", + "id": "d77507ca", "metadata": {}, "outputs": [], "source": [ @@ -423,7 +423,7 @@ { "cell_type": "code", "execution_count": null, - "id": "818dc5a9", + "id": "830df2fa", "metadata": {}, "outputs": [], "source": [ @@ -433,7 +433,7 @@ { "cell_type": "code", "execution_count": null, - "id": "07450255", + "id": "2a3aa5b0", "metadata": {}, "outputs": [], "source": [ @@ -445,7 +445,7 @@ { "cell_type": "code", "execution_count": null, - "id": "901446e2", + "id": "dbaffbad", "metadata": {}, "outputs": [], "source": [ @@ -456,7 +456,7 @@ { "cell_type": "code", "execution_count": null, - "id": "0369672d", + "id": "cfcc9078", "metadata": {}, "outputs": [], "source": [ @@ -465,7 +465,7 @@ }, { "cell_type": "markdown", - "id": "fea50841", + "id": "c32e6396", "metadata": {}, "source": [ "What is the result for the 501th value using float values?" @@ -474,7 +474,7 @@ { "cell_type": "code", "execution_count": null, - "id": "c4ee58f2", + "id": "1ff38cbe", "metadata": {}, "outputs": [], "source": [ @@ -514,7 +514,7 @@ }, { "cell_type": "markdown", - "id": "d8207380", + "id": "acaaf4cd", "metadata": {}, "source": [] } diff --git a/synced_files/Week_2_5/WS_2_5_solution.ipynb b/synced_files/Week_2_5/WS_2_5_solution.ipynb index 82e2efb8..d5b73613 100644 --- a/synced_files/Week_2_5/WS_2_5_solution.ipynb +++ b/synced_files/Week_2_5/WS_2_5_solution.ipynb @@ -40,9 +40,7 @@ "\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$)\n", - "\n", - "<b>You are not allowed to use ChatGPT for this task otherwise you won’t learn ;)</b>" + "- the profit of all projects together must be greater or equal than $250$ ($\\beta$)" ] }, { @@ -307,6 +305,7 @@ " \"Type_Constraint\")\n", "model2.addConstr(sum(P[i] * x[i] for i in range(num_projects)) >= beta,\n", " \"Minimum_Profit\")\n", + "# added constraint\n", "gamma = 130\n", "model2.addConstr((sum(I[i] * x[i] for i in range(num_projects))\n", " <= gamma * x[0]+ M * (1 - x[0])),\n", diff --git a/synced_files/Week_2_5/WS_2_5_solution.md b/synced_files/Week_2_5/WS_2_5_solution.md index 6928a966..59a03eaf 100644 --- a/synced_files/Week_2_5/WS_2_5_solution.md +++ b/synced_files/Week_2_5/WS_2_5_solution.md @@ -44,8 +44,6 @@ The company is operating with the following constraints, please formulate the ma - the projects of type 2 must be at least as many as the ones of type 1 - the profit of all projects together must be greater or equal than $250$ ($\beta$) -<b>You are not allowed to use ChatGPT for this task otherwise you won’t learn ;)</b> - <div style="background-color:#AABAB2; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"> <p> @@ -230,6 +228,7 @@ model2.addConstr((sum(x[i] for i in range(num_type2_projects, num_projects)) "Type_Constraint") model2.addConstr(sum(P[i] * x[i] for i in range(num_projects)) >= beta, "Minimum_Profit") +# added constraint gamma = 130 model2.addConstr((sum(I[i] * x[i] for i in range(num_projects)) <= gamma * x[0]+ M * (1 - x[0])), diff --git a/synced_files/Week_2_5/WS_2_5_solution.py b/synced_files/Week_2_5/WS_2_5_solution.py index 75bc7849..2aff2617 100644 --- a/synced_files/Week_2_5/WS_2_5_solution.py +++ b/synced_files/Week_2_5/WS_2_5_solution.py @@ -44,8 +44,6 @@ # - The company wants to do 3 out of the 6 projects # - the projects of type 2 must be at least as many as the ones of type 1 # - the profit of all projects together must be greater or equal than $250$ ($\beta$) -# -# <b>You are not allowed to use ChatGPT for this task otherwise you won’t learn ;)</b> # %% [markdown] # <div style="background-color:#AABAB2; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"> @@ -231,6 +229,7 @@ model2.addConstr((sum(x[i] for i in range(num_type2_projects, num_projects)) "Type_Constraint") model2.addConstr(sum(P[i] * x[i] for i in range(num_projects)) >= beta, "Minimum_Profit") +# added constraint gamma = 130 model2.addConstr((sum(I[i] * x[i] for i in range(num_projects)) <= gamma * x[0]+ M * (1 - x[0])), -- GitLab