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 baa0d86a0eda747dfdd0e65a50c87bbdb1b9e7fe..bbcf40a28a1f9b9e727535f885cd8ab99eea9d2e 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 af32c8d4ba8ecd00c9acd6e7d29298996235384c..eaee666b66ec1b3f6bd31a1cd3859f055e67928c 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 e269511909d3227c0f3cc279455b60c90a396a1e..e62246d1696aebaa0547ac0460864c0e0ab5a983 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 835175cf8ed48e255484fdfd33d9e092f15d1fda..2ded1c6ec8a6745d7aa3690acd885c1f1ae16280 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 cef45ee863d649f25d561e22978bc73c10deb43c..24200dbe9ea9c30670f5a685fa7f9acbefb5de76 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 cf79b4d1b9339da45e242019a9d00a5cdb825bcd..312e63150edaa8686a6d8fc6f45b3bae08860006 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 337a9adc688040bbed4b7d2b785f9ae6d77f1eaa..0db7299410def2ee72df2a789355a38ee708fa96 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 2f434b5f149ff88d633f22a61b9f34b0281814a8..a3b7b3279a0eac6383d731b6d42cc58b140c42fa 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 7a0e531a1a74ea43e16c8436d2ea7202b8771724..d47cc5cdb62e2cf8a887c31d57794623da7dbfc6 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 9a94e6801527232451f0e76793fbcf91d79c9885..76cb8b7435b612b7b19ed610b4120173178507b1 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 f7fdd7857da7639be00b9300c5da6a46b1d3b89f..c3afdcca84b744a87396e6eb83a72e8f97940dea 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 8f283c04dc21c874a00083b4331e210176b0ba75..f201766b24e2f65fc8e32af9a9e36b0c3a12c080 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 acd981582430fdc82e9b268423d2c75ab688db63..86cc55862f3a7020da29208966a2cc8a582f0b8b 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 814bcb97c115f77f295bb9f523a788993ce69725..d62595f5f4e2cdb66f3e2cf0599c8482ffd42de7 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 e41ca9b9afcdc8cc4b40edf0028c547c1bbbe7c4..432bc9040f1a8aa05f86cd36f18bc1f6ed495d35 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 82e2efb81d1d86f6e7ee3eba22fd619b402f616d..d5b73613c25763e88d4df044969f1062fcf1ed6a 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 6928a966e6176ed50adb6da9fb255c6ba350406f..59a03eaf89c261b87575232fd56ef0568f8c108d 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 75bc78498d91d694d85c8567b554edfb37dd21f4..2aff26177bb4a9b2e90e3cba88211ce366fd234c 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])),