├── Dynamic Traffic Assignment ├── CTM_SC.m ├── DTA_MSA.m ├── DTA_ProjGrad.m ├── DTA_STOCH.m ├── DTA_inst.m ├── ILTM.m ├── ILTM_BASE.m ├── ILTM_cold.prj ├── ILTM_cold_mex.mexw64 ├── ILTM_warm.prj ├── ILTM_warm_mex.mexw64 ├── LTM_MC.m ├── LTM_MC_INST.m ├── LTM_SC.m ├── MSA_DET.m ├── MSA_STOCH_D.m ├── MSA_STOCH_S.m ├── NodeModel.m ├── NodeModel_demand_prop.m ├── NodeModel_no_redistribution.m ├── PairComparator.class ├── TF_init.m ├── allOrNothingTF.m ├── buildODmatrix.m ├── cvn2artt.m ├── cvn2dens.m ├── cvn2flows.m ├── cvn2tt.m ├── dataParser.m ├── dens2tt.m ├── dijkstra.m ├── projGradTF.m └── stochasticTF.m ├── Network Data ├── net1.mat ├── net1_old.mat ├── net2.mat ├── net2_old.mat ├── net3.mat ├── net3_old.mat ├── net4.mat ├── net4_old.mat ├── net5.mat ├── net5_old.mat ├── net6.mat ├── net6_old.mat ├── net7.mat ├── net7_old.mat └── result_net1.mat ├── README.md ├── README.txt ├── Visualization Tools ├── animateSimulation.m ├── plotLoadedLinks.m ├── plotLoadedNodes.m ├── plotNetwork.m ├── plotTT.m └── plotXT.m ├── license.txt ├── tutorial1.m ├── tutorial10.m ├── tutorial11.m ├── tutorial12.m ├── tutorial13.m ├── tutorial14.m ├── tutorial2.m ├── tutorial3.m ├── tutorial4.m ├── tutorial5.m ├── tutorial6.m ├── tutorial7.m ├── tutorial8.m └── tutorial9.m /Dynamic Traffic Assignment/CTM_SC.m: -------------------------------------------------------------------------------- 1 | function [flows_up,flows_down,dens_n] = CTM_SC(nodes,links,origins,destinations,ODmatrix,dt,totT,TF) 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 | %link transmission assignment procedure % 4 | % % 5 | %destination based storing of commodities % 6 | %splitting rates at nodes based on TF % 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | 9 | % This file is part of the matlab package for dynamic traffic assignments 10 | % developed by the KULeuven. 11 | % 12 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 13 | % 14 | % This program is free software: you can redistribute it and/or modify 15 | % it under the terms of the GNU General Public License as published by 16 | % the Free Software Foundation, either version 3 of the License, or 17 | % any later version. 18 | % 19 | % This program is distributed in the hope that it will be useful, 20 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 21 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 | % GNU General Public License for more details. 23 | % 24 | % You should have received a copy of the GNU General Public License 25 | % along with this program. If not, see . 26 | % 27 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 28 | % or contact: willem.himpe {@} kuleuven.be 29 | 30 | %size of the network 31 | totLinks=length(links.fromNode); 32 | 33 | %time slices for which a solution is build 34 | timeSlices = [0:totT]*dt; 35 | 36 | %cumulative vehicle numbers (cvn) are stored on both upstream and 37 | %dowsntream link end of each link for every time slice 38 | dens_n = zeros(totLinks,totT+1); 39 | flows_up = zeros(totLinks,totT); 40 | flows_down = zeros(totLinks,totT); 41 | 42 | %local rename link properties (for shorter code) 43 | fromNodes = links.fromNode; 44 | toNodes = links.toNode; 45 | freeSpeeds = links.freeSpeed; 46 | capacities = links.capacity; 47 | kJams = links.kJam; 48 | lengths = links.length; 49 | wSpeeds = capacities./(kJams-capacities./freeSpeeds); 50 | 51 | normalNodes = setdiff(nodes.id,[origins,destinations]); 52 | 53 | %forward explicit scheme 54 | %go sequentially over each time step (first time step => all zeros) 55 | for t=2:totT+1 56 | %ORIGIN NODES<-------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 57 | %this nested function goes over all origin nodes 58 | loadOriginNodes(t); 59 | 60 | %ACTUAL CTM <--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 61 | %go over all normal nodes in this time step 62 | for nIndex=1:length(normalNodes); 63 | %STANDARD NODES<-------------------------------------------------------------------------------------------------------------------------------------------------------------------- 64 | %most function calls will end up here 65 | n=normalNodes(nIndex); 66 | 67 | if isempty(TF{n,t-1}) || sum(sum((TF{n,t-1})))==0 68 | %no paths crossing this node 69 | else 70 | %active node 71 | 72 | %CALCULATE THE SENDING FLOW<-------------------------------------------------------------------------------------------------------------------------------------------------------- 73 | %this is the maximum number of vehicles comming from the 74 | %incoming links that want to travel over a node within this 75 | %time interval 76 | incomingLinks = find(toNodes==n); 77 | nbIn = length(incomingLinks); 78 | SF = zeros(nbIn,1); 79 | for l_index=1:nbIn 80 | l=incomingLinks(l_index); 81 | SF(l_index) = calculateSendingFlow(l,t); 82 | end 83 | 84 | %CALCULATE RECEIVING FLOW<----------------------------------------------------------------------------------------------------------------------------------------------------------- 85 | %this is the maximum number of vehicles that can flow into the 86 | %outgoing links within this time interval 87 | outgoingLinks = find(fromNodes==n); 88 | nbOut = length(outgoingLinks); 89 | RF = zeros(nbOut,1); 90 | for l_index=1:nbOut 91 | l=outgoingLinks(l_index); 92 | % RF(l_index) = calculateReceivingFlow_VQ(l,t); 93 | % RF(l_index) = calculateReceivingFlow_HQ(l,t); 94 | RF(l_index) = calculateReceivingFlow_FQ(l,t); 95 | end 96 | 97 | %compute transfer flows with the NODE MODEL 98 | TransferFlow = NodeModel(nbIn,nbOut,SF,TF{n,t-1},RF,capacities(incomingLinks)); 99 | % TransferFlow = NodeModel_no_redistribution(incomingLinks,outgoingLinks,SF,TF{n,t-1},RF,capacities,dt); 100 | 101 | %update number of vehicles in a link 102 | flows_down(incomingLinks,t)=sum(TransferFlow,2); 103 | flows_up(outgoingLinks,t)=sum(TransferFlow,1)'; 104 | end 105 | end 106 | 107 | %DESTINATION NODES<---------------------------------------------------------------------------------------------------------------------------- 108 | %this nested function goes over all destination nodes 109 | loadDestinationNodes(t); 110 | 111 | 112 | %CTM update 113 | dens_n(:,t) = dens_n(:,t-1)+1./lengths.*(flows_up(:,t)-flows_down(:,t))*dt; 114 | end 115 | 116 | %All nested function follow below: 117 | 118 | %Nested function for finding sending flows 119 | function SF = calculateSendingFlow(l,t) 120 | SF = capacities(l); 121 | ki = dens_n(l,t-1); 122 | SF = min(SF,freeSpeeds(l)*ki); 123 | end 124 | 125 | %Nested function for finding receiving flows for a vertical queue 126 | function RF = calculateReceivingFlow_VQ(l,t) 127 | RF = capacities(l); 128 | end 129 | 130 | %Nested function for finding receiving flows for a horizontal queue 131 | function RF = calculateReceivingFlow_HQ(l,t) 132 | RF = capacities(l); 133 | RF=min(RF,wSpeeds(l)*kJams(l)); 134 | end 135 | 136 | %Nested function for finding receiving flows for a physical queue 137 | function RF = calculateReceivingFlow_FQ(l,t) 138 | RF = capacities(l); 139 | ki = dens_n(l,t-1); 140 | RF = min(RF,wSpeeds(l)*(kJams(l)-ki)); 141 | end 142 | 143 | %Nested function that assigns the origin flow 144 | function loadOriginNodes(t) 145 | %update origin nodes 146 | for o_index=1:length(origins) 147 | o = origins(o_index); 148 | outgoingLinks = find(fromNodes==o); 149 | for l_index=1:length(outgoingLinks) 150 | l=outgoingLinks(l_index); 151 | %calculation sending flow 152 | SF = TF{o,t-1}.*sum(ODmatrix(o_index,:,t-1)); 153 | flows_up(l,t)=SF; 154 | end 155 | end 156 | end 157 | 158 | %Nested function that assigns the destination flow 159 | function loadDestinationNodes(t) 160 | %update origin nodes 161 | for d_index=1:length(destinations) 162 | d = destinations(d_index); 163 | incomingLinks = find(toNodes==d); 164 | for l_index=1:length(incomingLinks) 165 | l=incomingLinks(l_index); 166 | %calculation sending flow 167 | SF = capacities(l); 168 | ki = dens_n(l,t-1); 169 | flows_down(l,t) = min(SF,freeSpeeds(l)*ki); 170 | end 171 | end 172 | end 173 | end -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/DTA_MSA.m: -------------------------------------------------------------------------------- 1 | function [cvn_up,cvn_down,TF] = DTA_MSA(nodes,links,origins,destinations,ODmatrix,dt,totT,rc_dt,maxIt,rc_agg) 2 | 3 | %Method of successive averages for calculating deterministic user 4 | %equilibrium 5 | % 6 | %SYNTAX 7 | % [flows] = MSA_exercise(odmatrix,nodes,links) 8 | % 9 | %DESCRIPTION 10 | % returns the flow on each link in the deterministic user equilibrium as 11 | % calculated by the method of successive averages 12 | % 13 | %INPUTS 14 | % odmatrix: static origin/destination matrix 15 | % nodes: table with all the nodes in the network. 16 | % links: table with all the links in the network 17 | 18 | % This file is part of the matlab package for dynamic traffic assignments 19 | % developed by the KULeuven. 20 | % 21 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 22 | % 23 | % This program is free software: you can redistribute it and/or modify 24 | % it under the terms of the GNU General Public License as published by 25 | % the Free Software Foundation, either version 3 of the License, or 26 | % any later version. 27 | % 28 | % This program is distributed in the hope that it will be useful, 29 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 30 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 31 | % GNU General Public License for more details. 32 | % 33 | % You should have received a copy of the GNU General Public License 34 | % along with this program. If not, see . 35 | % 36 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 37 | % or contact: willem.himpe {@} kuleuven.be 38 | 39 | %setup the output figure 40 | h = figure; 41 | semilogy(0,NaN); 42 | start_time = cputime; 43 | 44 | %Maximum number of iterations 45 | if isempty(maxIt) 46 | maxIt = 20; 47 | end 48 | 49 | %initilization 50 | totNodes = size(nodes.id,1); 51 | totLinks = size(links.toNode,1); 52 | totDest = length(destinations); 53 | %cumulative vehicle numbers (cvn) are stored on both upstream and 54 | %dowsntream link end of each link for every time slice 55 | cvn_up = zeros(totLinks,totT+1,totDest); 56 | cvn_down = zeros(totLinks,totT+1,totDest); 57 | 58 | %Initialize the iteration numbering 59 | it = 0; 60 | 61 | %initialize the travel cost 62 | [simTT] = cvn2tt(sum(cvn_up,3),sum(cvn_down,3),dt,totT,links); 63 | 64 | %initialize the gap function 65 | gap_dt = inf; 66 | TF = []; 67 | 68 | %initialize the turning fractions (both first and last give the same result) 69 | TF_new = allOrNothingTF(nodes,links,destinations,simTT,cvn_up,dt,totT,rc_dt,rc_agg); 70 | 71 | %MAIN LOOP: iterate until convergence is reached or maximum number of 72 | %iterations is reached 73 | while it < maxIt && gap_dt > 10^-6 74 | it = it+1; 75 | 76 | %calculate the update step 77 | if isempty(TF) 78 | TF = TF_new; 79 | else 80 | for n = 1:totNodes 81 | for t = 1:totT 82 | for d= 1:totDest 83 | update = (TF_new{n,t,d} - TF{n,t,d}); 84 | TF{n,t,d} = TF{n,t,d} + 1/it*update; 85 | end 86 | end 87 | end 88 | end 89 | sp=[TF{2,:,1}]; 90 | figure(10);plot(dt*[0:totT-1],sp(1:2:end),'r',dt*[0:totT-1],sp(2:2:end),'b'); 91 | 92 | %calculate new flows 93 | [cvn_up,cvn_down] = LTM_MC(nodes,links,origins,destinations,ODmatrix,dt,totT,TF); 94 | 95 | %update costs 96 | [simTT] = cvn2tt(sum(cvn_up,3),sum(cvn_down,3),dt,totT,links); 97 | 98 | %Compute new turning fractions via all-or-nothing assignment 99 | %and compute convergence gap 100 | [TF_new,gap_dt,gap_rc] = allOrNothingTF(nodes,links,destinations,simTT,cvn_up,dt,totT,rc_dt,rc_agg); 101 | 102 | %plot convergence in function of computation time 103 | figure(h) 104 | hold on 105 | time=cputime-start_time; 106 | a=semilogy(time,gap_dt,'r.'); 107 | b=semilogy(time,gap_rc,'ob'); 108 | legend([a,b],'gap based on simulation interval','gap based on route choice interval'); 109 | pause(0.01); 110 | end 111 | 112 | %Check for number of iterations until convergence 113 | if it >= maxIt 114 | disp(['Maximum Iteration limit reached: ', num2str(maxIt), ' Gap: ', num2str(gap_dt)]); 115 | else 116 | disp(['Convergence reached in iteration ', num2str(it), ' Gap: ', num2str(gap_dt)]); 117 | end 118 | 119 | end 120 | 121 | 122 | -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/DTA_ProjGrad.m: -------------------------------------------------------------------------------- 1 | function [cvn_up,cvn_down,TF] = DTA_ProjGrad(nodes,links,origins,destinations,ODmatrix,dt,totT,rc_dt,maxIt,alpha) 2 | 3 | %Method of successive averages for calculating deterministic user 4 | %equilibrium 5 | % 6 | %SYNTAX 7 | % [flows] = MSA_exercise(odmatrix,nodes,links) 8 | % 9 | %DESCRIPTION 10 | % returns the flow on each link in the deterministic user equilibrium as 11 | % calculated by the method of successive averages 12 | % 13 | %INPUTS 14 | % odmatrix: static origin/destination matrix 15 | % nodes: table with all the nodes in the network. 16 | % links: table with all the links in the network 17 | 18 | % This file is part of the matlab package for dynamic traffic assignments 19 | % developed by the KULeuven. 20 | % 21 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 22 | % 23 | % This program is free software: you can redistribute it and/or modify 24 | % it under the terms of the GNU General Public License as published by 25 | % the Free Software Foundation, either version 3 of the License, or 26 | % any later version. 27 | % 28 | % This program is distributed in the hope that it will be useful, 29 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 30 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 31 | % GNU General Public License for more details. 32 | % 33 | % You should have received a copy of the GNU General Public License 34 | % along with this program. If not, see . 35 | % 36 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 37 | % or contact: willem.himpe {@} kuleuven.be 38 | 39 | %setup the output figure 40 | h = figure; 41 | semilogy(0,NaN); 42 | start_time = cputime; 43 | 44 | %Maximum number of iterations 45 | if isempty(maxIt) 46 | maxIt = 20; 47 | end 48 | 49 | %initilization 50 | totNodes = size(nodes.id,1); 51 | totLinks = size(links.toNode,1); 52 | totDest = length(destinations); 53 | %cumulative vehicle numbers (cvn) are stored on both upstream and 54 | %dowsntream link end of each link for every time slice 55 | cvn_up = zeros(totLinks,totT+1,totDest); 56 | cvn_down = zeros(totLinks,totT+1,totDest); 57 | 58 | %Initialize the iteration numbering 59 | it = 0; 60 | 61 | %Initialize the aggragation 62 | rc_agg = 'last'; %[first/middle/last] 63 | 64 | %initialize the travel cost 65 | [simTT] = cvn2tt(sum(cvn_up,3),sum(cvn_down,3),dt,totT,links); 66 | 67 | %initialize the gap function 68 | gap_dt = inf; 69 | TF = []; 70 | 71 | %initialize the turning fractions (all options [first/middle/last] give the same result) 72 | TF_new = allOrNothingTF(nodes,links,destinations,simTT,cvn_up,dt,totT,rc_dt,rc_agg); 73 | 74 | %MAIN LOOP: iterate until convergence is reached or maximum number of 75 | %iterations is reached 76 | while it < maxIt && gap_dt > 10^-60 77 | it = it+1; 78 | 79 | %calculate the update step 80 | if isempty(TF) 81 | TF = TF_new; 82 | else 83 | for n = 1:totNodes 84 | for t = 1:totT 85 | for d= 1:totDest 86 | TF{n,t,d} = TF{n,t,d} + updateTF{n,t,d}; 87 | end 88 | end 89 | end 90 | end 91 | 92 | %calculate new flows 93 | [cvn_up,cvn_down] = LTM_MC(nodes,links,origins,destinations,ODmatrix,dt,totT,TF); 94 | 95 | %update costs 96 | [simTT] = cvn2tt(sum(cvn_up,3),sum(cvn_down,3),dt,totT,links); 97 | 98 | %Compute new turning fractions via all-or-nothing assignment 99 | %and compute convergence gap 100 | [updateTF,gap_dt,gap_rc] = projGradTF(nodes,links,destinations,simTT,cvn_up,dt,totT,rc_dt,rc_agg,TF,alpha); 101 | 102 | %plot convergence in function of computation time 103 | figure(h) 104 | hold on 105 | time=cputime-start_time; 106 | a=semilogy(time,gap_dt,'r.'); 107 | b=semilogy(time,gap_rc,'ob'); 108 | legend([a,b],'gap based on simulation interval','gap based on route choice interval'); 109 | 110 | sp=[TF{2,:,1}]; 111 | figure(10);plot(dt*[0:totT-1],sp(1:2:end),'r',dt*[0:totT-1],sp(2:2:end),'b'); 112 | title(['iteration: ',num2str(it)]); 113 | pause(0.01); 114 | end 115 | %Check for number of iterations until convergence 116 | if it >= maxIt 117 | disp(['Maximum Iteration limit reached: ', num2str(maxIt), ' Gap: ', num2str(gap_dt)]); 118 | else 119 | disp(['Convergence reached in iteration ', num2str(it), ' Gap: ', num2str(gap_dt)]); 120 | end 121 | 122 | end 123 | 124 | 125 | -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/DTA_STOCH.m: -------------------------------------------------------------------------------- 1 | function [cvn_up,cvn_down,TF] = DTA(nodes,links,origins,destinations,ODmatrix,dt,totT,rc_dt,maxIt,alpha,theta) 2 | 3 | %Method of successive averages for calculating deterministic user 4 | %equilibrium 5 | % 6 | %SYNTAX 7 | % [flows] = MSA_exercise(odmatrix,nodes,links) 8 | % 9 | %DESCRIPTION 10 | % returns the flow on each link in the deterministic user equilibrium as 11 | % calculated by the method of successive averages 12 | % 13 | %INPUTS 14 | % odmatrix: static origin/destination matrix 15 | % nodes: table with all the nodes in the network. 16 | % links: table with all the links in the network 17 | 18 | % This file is part of the matlab package for dynamic traffic assignments 19 | % developed by the KULeuven. 20 | % 21 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 22 | % 23 | % This program is free software: you can redistribute it and/or modify 24 | % it under the terms of the GNU General Public License as published by 25 | % the Free Software Foundation, either version 3 of the License, or 26 | % any later version. 27 | % 28 | % This program is distributed in the hope that it will be useful, 29 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 30 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 31 | % GNU General Public License for more details. 32 | % 33 | % You should have received a copy of the GNU General Public License 34 | % along with this program. If not, see . 35 | % 36 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 37 | % or contact: willem.himpe {@} kuleuven.be 38 | 39 | %setup the output figure 40 | h = figure; 41 | semilogy(0,NaN); 42 | start_time = cputime; 43 | 44 | %Maximum number of iterations 45 | if isempty(maxIt) 46 | maxIt = 20; 47 | end 48 | 49 | %initilization 50 | totNodes = size(nodes.id,1); 51 | totLinks = size(links.toNode,1); 52 | totDest = length(destinations); 53 | %cumulative vehicle numbers (cvn) are stored on both upstream and 54 | %dowsntream link end of each link for every time slice 55 | cvn_up = zeros(totLinks,totT+1,totDest); 56 | cvn_down = zeros(totLinks,totT+1,totDest); 57 | 58 | %Initialize the iteration numbering 59 | it = 0; 60 | 61 | %Initialize the aggragation 62 | rc_agg = 'last'; %[first/middle/last] 63 | 64 | %initialize the travel cost 65 | [simTT] = cvn2tt(sum(cvn_up,3),sum(cvn_down,3),dt,totT,links); 66 | 67 | %initialize the gap function 68 | gap = inf; 69 | gap_TF = inf; 70 | TF = []; 71 | 72 | %initialize the turning fractions (both first and last give the same result) 73 | TF_new = stochasticTF(nodes,links,destinations,simTT,cvn_up,dt,totT,rc_dt,rc_agg,theta); 74 | 75 | [flows_up_prev] = cvn2flows(sum(cvn_up,3),dt); 76 | 77 | %MAIN LOOP: iterate until convergence is reached or maximum number of 78 | %iterations is reached 79 | while it < maxIt && gap > 10^-6 80 | it = it+1; 81 | gap_TF = 0; 82 | 83 | %calculate the update step 84 | if isempty(TF) 85 | TF = TF_new; 86 | else 87 | for n = 1:totNodes 88 | for t = 1:totT 89 | for d= 1:totDest 90 | update = (TF_new{n,t,d} - TF{n,t,d}); 91 | TF{n,t,d} = TF{n,t,d} + alpha*update; 92 | end 93 | end 94 | end 95 | end 96 | sp=[TF{2,:,1}]; 97 | figure(10);plot(dt*[0:totT-1],sp(1:2:end),'r',dt*[0:totT-1],sp(2:2:end),'b'); 98 | 99 | %calculate new flows 100 | [cvn_up,cvn_down] = LTM_MC(nodes,links,origins,destinations,ODmatrix,dt,totT,TF); 101 | 102 | %update costs 103 | [simTT] = cvn2tt(sum(cvn_up,3),sum(cvn_down,3),dt,totT,links); 104 | %update flows 105 | [flows_up] = cvn2flows(sum(cvn_up,3),dt); 106 | 107 | 108 | %Compute new turning fractions via all-or-nothing assignment 109 | %and compute convergence gap 110 | [TF_new,gap,gap_s] = stochasticTF(nodes,links,destinations,simTT,cvn_up,dt,totT,rc_dt,rc_agg,theta); 111 | 112 | gap_flow = sum(sum(abs(flows_up-flows_up_prev))); 113 | %plot convergence in function of computation time 114 | figure(h) 115 | hold on 116 | time=cputime-start_time; 117 | a=semilogy(time,gap,'r.'); 118 | b=semilogy(time,gap_s,'ob'); 119 | c=semilogy(time,gap_flow,'xg'); 120 | legend([a,b,c],'gap based on simulation interval','adjusted gap based on simulation interval','total flow difference'); 121 | flows_up_prev = flows_up; 122 | end 123 | 124 | %Check for number of iterations until convergence 125 | if it >= maxIt 126 | disp(['Maximum Iteration limit reached: ', num2str(maxIt), ' Gap: ', num2str(gap_flow)]); 127 | else 128 | disp(['Convergence reached in iteration ', num2str(it), ' Gap: ', num2str(gap_flow)]); 129 | end 130 | 131 | end 132 | 133 | 134 | -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/DTA_inst.m: -------------------------------------------------------------------------------- 1 | function [cvn_up,cvn_down,TF] = DTA_inst(nodes,links,origins,destinations,ODmatrix,dt,totT,rc_dt) 2 | 3 | %Method of successive averages for calculating deterministic user 4 | %equilibrium 5 | % 6 | %SYNTAX 7 | % [flows] = MSA_exercise(odmatrix,nodes,links) 8 | % 9 | %DESCRIPTION 10 | % returns the flow on each link in the deterministic user equilibrium as 11 | % calculated by the method of successive averages 12 | % 13 | %INPUTS 14 | % odmatrix: static origin/destination matrix 15 | % nodes: table with all the nodes in the network. 16 | % links: table with all the links in the network 17 | 18 | % This file is part of the matlab package for dynamic traffic assignments 19 | % developed by the KULeuven. 20 | % 21 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 22 | % 23 | % This program is free software: you can redistribute it and/or modify 24 | % it under the terms of the GNU General Public License as published by 25 | % the Free Software Foundation, either version 3 of the License, or 26 | % any later version. 27 | % 28 | % This program is distributed in the hope that it will be useful, 29 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 30 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 31 | % GNU General Public License for more details. 32 | % 33 | % You should have received a copy of the GNU General Public License 34 | % along with this program. If not, see . 35 | % 36 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 37 | % or contact: willem.himpe {@} kuleuven.be 38 | 39 | %initilization 40 | totNodes = size(nodes.id,1); 41 | totLinks = size(links.toNode,1); 42 | totDest = length(destinations); 43 | %cumulative vehicle numbers (cvn) are stored on both upstream and 44 | %dowsntream link end of each link for every time slice 45 | cvn_up = zeros(totLinks,totT+1,totDest); 46 | cvn_down = zeros(totLinks,totT+1,totDest); 47 | 48 | %calculate new flows 49 | [cvn_up,cvn_down,TF] = LTM_MC_INST(nodes,links,origins,destinations,ODmatrix,dt,totT,rc_dt); 50 | 51 | %update costs 52 | [simTT] = cvn2tt(sum(cvn_up,3),sum(cvn_down,3),dt,totT,links); 53 | 54 | %Compute the convergence gap 55 | %using the Null option no turning fractions are calculated 56 | [~,gap] = allOrNothingTF(nodes,links,destinations,simTT,cvn_up,dt,totT,rc_dt,'Null'); 57 | 58 | %Check for number of iterations until convergence 59 | disp(['Instantaneous route choice: No Iterations', ' Gap: ', num2str(gap)]); 60 | 61 | end 62 | 63 | 64 | -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/ILTM_cold_mex.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HimpeWillem/MatlabTrafficToolbox/7e58302d0f493cfd6745c5f0daf340fcf6fdb63a/Dynamic Traffic Assignment/ILTM_cold_mex.mexw64 -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/ILTM_warm_mex.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HimpeWillem/MatlabTrafficToolbox/7e58302d0f493cfd6745c5f0daf340fcf6fdb63a/Dynamic Traffic Assignment/ILTM_warm_mex.mexw64 -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/LTM_MC.m: -------------------------------------------------------------------------------- 1 | function [cvn_up,cvn_down] = LTM_MC(nodes,links,origins,destinations,ODmatrix,dt,totT,TF) 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 | %link transmission assignment procedure % 4 | % % 5 | %destination based storing of commodities % 6 | %splitting rates at nodes based on TF % 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | 9 | % This file is part of the matlab package for dynamic traffic assignments 10 | % developed by the KULeuven. 11 | % 12 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 13 | % 14 | % This program is free software: you can redistribute it and/or modify 15 | % it under the terms of the GNU General Public License as published by 16 | % the Free Software Foundation, either version 3 of the License, or 17 | % any later version. 18 | % 19 | % This program is distributed in the hope that it will be useful, 20 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 21 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 | % GNU General Public License for more details. 23 | % 24 | % You should have received a copy of the GNU General Public License 25 | % along with this program. If not, see . 26 | % 27 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 28 | % or contact: willem.himpe {@} kuleuven.be 29 | 30 | %size of the network 31 | totLinks = length(links.fromNode); 32 | totDest = length(destinations); 33 | 34 | %time slices for which a solution is build 35 | timeSlices = [0:totT]*dt; 36 | 37 | %cumulative vehicle numbers (cvn) are stored on both upstream and 38 | %dowsntream link end of each link for every time slice 39 | cvn_up = zeros(totLinks,totT+1,totDest); 40 | cvn_down = zeros(totLinks,totT+1,totDest); 41 | 42 | %local rename link properties (for shorter code) 43 | fromNodes = links.fromNode; 44 | toNodes = links.toNode; 45 | freeSpeeds = links.freeSpeed; 46 | capacities = links.capacity; 47 | kJams = links.kJam; 48 | lengths = links.length; 49 | wSpeeds = capacities./(kJams-capacities./freeSpeeds); 50 | 51 | normalNodes = setdiff(nodes.id,[origins,destinations]); 52 | 53 | %forward explicit scheme 54 | %go sequentially over each time step (first time step => all zeros) 55 | for t=2:totT+1 56 | %ORIGIN NODES<-------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 57 | %this nested function goes over all origin nodes 58 | loadOriginNodes(t); 59 | 60 | %ACTUAL LTM <--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 61 | %go over all normal nodes in this time step 62 | for nIndex=1:length(normalNodes); 63 | %STANDARD NODES<-------------------------------------------------------------------------------------------------------------------------------------------------------------------- 64 | %most function calls will end up here 65 | n=normalNodes(nIndex); 66 | 67 | %CALCULATE THE SENDING FLOW<-------------------------------------------------------------------------------------------------------------------------------------------------------- 68 | %this is the maximum number of vehicles comming from the 69 | %incoming links that want to travel over a node within this 70 | %time interval 71 | incomingLinks = find(toNodes==n); 72 | nbIn = length(incomingLinks); 73 | SF_d = zeros(nbIn,totDest); 74 | SF_tot = zeros(nbIn,1); 75 | SF = zeros(nbIn,1); 76 | for l_index=1:nbIn 77 | l=incomingLinks(l_index); 78 | SF_d(l_index,:) = calculateDestSendFlow(l,t); 79 | SF_tot(l_index) = sum(SF_d(l_index,:)); 80 | SF(l_index) = min(capacities(l)*dt,SF_tot(l_index)); 81 | end 82 | 83 | %CALCULATE RECEIVING FLOW<----------------------------------------------------------------------------------------------------------------------------------------------------------- 84 | %this is the maximum number of vehicles that can flow into the 85 | %outgoing links within this time interval 86 | outgoingLinks = find(fromNodes==n); 87 | nbOut = length(outgoingLinks); 88 | RF = zeros(nbOut,1); 89 | for l_index=1:nbOut 90 | l=outgoingLinks(l_index); 91 | % RF(l_index) = calculateReceivingFlow_VQ(l,t); 92 | % RF(l_index) = calculateReceivingFlow_HQ(l,t); 93 | RF(l_index) = calculateReceivingFlow_FQ(l,t); 94 | end 95 | 96 | %CALCULATE TURNING FRACTIONS<--------------------------------------------------------------------------------------------------------------------------- 97 | %this calculates the split rates of all flows that run over the node 98 | TF_n = calculateTurningFractions(n,t-1); 99 | 100 | %compute transfer flows with the NODE MODEL 101 | TransferFlow = NodeModel(nbIn,nbOut,SF,TF_n,RF,capacities(incomingLinks)*dt); 102 | 103 | %update CVN values 104 | red = sum(TransferFlow,2)./(eps+SF_tot); 105 | for d = 1:totDest 106 | cvn_down(incomingLinks,t,d)=cvn_down(incomingLinks,t-1,d)+red.*SF_d(:,d); 107 | cvn_up(outgoingLinks,t,d)=cvn_up(outgoingLinks,t-1,d)+((red.*SF_d(:,d))'*TF{n,t-1,d})'; 108 | end 109 | end 110 | 111 | %DESTINATION NODES<---------------------------------------------------------------------------------------------------------------------------- 112 | %this nested function goes over all destination nodes 113 | loadDestinationNodes(t); 114 | end 115 | 116 | %All nested function follow below: 117 | 118 | %Nested function for finding destination based sending flows 119 | function SF = calculateDestSendFlow(l,t) 120 | SFCAP = capacities(l)*dt; 121 | time = timeSlices(t)-lengths(l)/freeSpeeds(l); 122 | val = findCVN(cvn_up(l,:,:),time,timeSlices,dt); 123 | SF = val-cvn_down(l,t-1,:); 124 | if SF > SFCAP 125 | red = SFCAP/sum(SF); 126 | SF = red*SF; 127 | end 128 | SF = max(0,SF); 129 | end 130 | 131 | %Nested function for finding receiving flows for a vertical queue 132 | function RF = calculateReceivingFlow_VQ(l,t) 133 | RF = capacities(l)*dt; 134 | end 135 | 136 | %Nested function for finding receiving flows for a horizontal queue 137 | function RF = calculateReceivingFlow_HQ(l,t) 138 | RF = capacities(l)*dt; 139 | val = sum(cvn_down(l,t-1,:),3)+kJams(l)*lengths(l) 140 | RF=min(RF,val-sum(cvn_up(l,t-1,:))); 141 | end 142 | 143 | %Nested function for finding receiving flows for a physical queue 144 | function RF = calculateReceivingFlow_FQ(l,t) 145 | RF = capacities(l)*dt; 146 | time = timeSlices(t)-lengths(l)/wSpeeds(l); 147 | val = findCVN(sum(cvn_down(l,:,:),3),time,timeSlices,dt)+kJams(l)*lengths(l); 148 | RF = min(RF,val-sum(cvn_up(l,t-1,:),3)); 149 | RF = max(RF,0); 150 | end 151 | 152 | %Nested function for finding turning fractions 153 | function TF_n = calculateTurningFractions(n,t) 154 | %split rates of all flow that runs over the node 155 | if nbOut==1 156 | %Straight forward merge 157 | TF_n(1:nbIn,1) = 1; 158 | else 159 | %more complex nodes with multiple outgoing links 160 | TF_n=zeros(nbIn,nbOut); 161 | for d=1:totDest 162 | TF_n=TF_n+repmat(SF_d(:,d),1,nbOut).*TF{n,t,d}; 163 | end 164 | %derive turning fractions from the turningFlows 165 | TF_n = TF_n./repmat(eps+sum(TF_n,2),1,nbOut); 166 | end 167 | end 168 | 169 | %Nested function that assigns the origin flow 170 | function loadOriginNodes(t) 171 | %update origin nodes 172 | for o_index=1:length(origins) 173 | o = origins(o_index); 174 | outgoingLinks = find(fromNodes==o); 175 | for l_index = 1:length(outgoingLinks) 176 | l=outgoingLinks(l_index); 177 | for d_index = 1:totDest 178 | %calculation sending flow 179 | SF_d = TF{o,t-1,d_index}.*sum(ODmatrix(o_index,d_index,t-1))*dt; 180 | cvn_up(l,t,d_index)=cvn_up(l,t-1,d_index) + SF_d(l_index); 181 | end 182 | end 183 | end 184 | end 185 | 186 | %Nested function that assigns the destination flow 187 | function loadDestinationNodes(t) 188 | %update origin nodes 189 | for d_index=1:length(destinations) 190 | d = destinations(d_index); 191 | incomingLinks = find(toNodes==d); 192 | for l_index=1:length(incomingLinks) 193 | l=incomingLinks(l_index); 194 | for d_index = 1:totDest 195 | %calculation sending flow 196 | SF_d = findCVN(cvn_up(l,:,:),timeSlices(t)-lengths(l)/freeSpeeds(l),timeSlices,dt)-cvn_down(l,t-1,:); 197 | cvn_down(l,t,:)=cvn_down(l,t-1,:) + SF_d; 198 | end 199 | end 200 | end 201 | end 202 | 203 | %Nested function used for finding CVN values inbetween time slices 204 | function val = findCVN(cvn,time,timeSlices,dt) 205 | if time<=timeSlices(1) 206 | val=cvn(1,1,:); 207 | elseif time>=timeSlices(end) 208 | val=cvn(1,end,:); 209 | else 210 | t1=ceil(time/dt); 211 | t2=t1+1; 212 | val = cvn(1,t1,:)+(time/dt-t1+1)*(cvn(1,t2,:)-cvn(1,t1,:)); 213 | end 214 | end 215 | 216 | end 217 | -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/LTM_SC.m: -------------------------------------------------------------------------------- 1 | function [cvn_up,cvn_down] = LTM_SC(nodes,links,origins,destinations,ODmatrix,dt,totT,TF,varargin) 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 | %link transmission model for network loading % 4 | % % 5 | %destination based storing of commodities % 6 | %splitting rates at nodes based on TF % 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | 9 | % This file is part of the matlab package for dynamic traffic assignments 10 | % developed by the KULeuven. 11 | % 12 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 13 | % 14 | % This program is free software: you can redistribute it and/or modify 15 | % it under the terms of the GNU General Public License as published by 16 | % the Free Software Foundation, either version 3 of the License, or 17 | % any later version. 18 | % 19 | % This program is distributed in the hope that it will be useful, 20 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 21 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 | % GNU General Public License for more details. 23 | % 24 | % You should have received a copy of the GNU General Public License 25 | % along with this program. If not, see . 26 | % 27 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 28 | % or contact: willem.himpe {@} kuleuven.be 29 | 30 | %size of the network 31 | totLinks=length(links.fromNode); 32 | 33 | %time slices for which a solution is build 34 | timeSlices = [0:totT]*dt; 35 | 36 | %cumulative vehicle numbers (cvn) are stored on both upstream and 37 | %dowsntream link end of each link for every time slice 38 | cvn_up = zeros(totLinks,totT+1); 39 | cvn_down = zeros(totLinks,totT+1); 40 | 41 | %local rename link properties (for shorter code) 42 | fromNodes = links.fromNode; 43 | toNodes = links.toNode; 44 | freeSpeeds = links.freeSpeed; 45 | capacities = links.capacity; 46 | kJams = links.kJam; 47 | lengths = links.length; 48 | wSpeeds = capacities./(kJams-capacities./freeSpeeds); 49 | 50 | normalNodes = setdiff(nodes.id,[origins,destinations]); 51 | 52 | if nargin==8 53 | Lmod = 'PQ'; 54 | Nmod = 'OC'; 55 | elseif nargin == 9 56 | Lmod = varargin{1}; 57 | Nmod = 'OC'; 58 | else 59 | Lmod = varargin{1}; 60 | Nmod = varargin{2}; 61 | end 62 | 63 | %forward explicit scheme 64 | %go sequentially over each time step (first time step => all zeros) 65 | for t=2:totT+1 66 | %ORIGIN NODES<-------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 67 | %this nested function goes over all origin nodes 68 | loadOriginNodes(t); 69 | 70 | %ACTUAL LTM <--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 71 | %go over all normal nodes in this time step 72 | for nIndex=1:length(normalNodes); 73 | %STANDARD NODES<-------------------------------------------------------------------------------------------------------------------------------------------------------------------- 74 | %most function calls will end up here 75 | n=normalNodes(nIndex); 76 | 77 | if isempty(TF{n,t-1}) || sum(sum((TF{n,t-1})))==0 78 | %no paths crossing this node 79 | else 80 | %active node 81 | 82 | %CALCULATE THE SENDING FLOW<-------------------------------------------------------------------------------------------------------------------------------------------------------- 83 | %this is the maximum number of vehicles comming from the 84 | %incoming links that want to travel over a node within this 85 | %time interval 86 | incomingLinks = find(toNodes==n); 87 | nbIn = length(incomingLinks); 88 | SF = zeros(nbIn,1); 89 | for l_index=1:nbIn 90 | l=incomingLinks(l_index); 91 | SF(l_index) = calculateSendingFlow(l,t); 92 | end 93 | 94 | %CALCULATE RECEIVING FLOW<----------------------------------------------------------------------------------------------------------------------------------------------------------- 95 | %this is the maximum number of vehicles that can flow into the 96 | %outgoing links within this time interval 97 | outgoingLinks = find(fromNodes==n); 98 | nbOut = length(outgoingLinks); 99 | RF = zeros(nbOut,1); 100 | for l_index=1:nbOut 101 | l=outgoingLinks(l_index); 102 | switch Lmod 103 | case 'VQ' 104 | RF(l_index) = calculateReceivingFlow_VQ(l,t); 105 | case 'HQ' 106 | RF(l_index) = calculateReceivingFlow_HQ(l,t); 107 | case 'PQ' 108 | RF(l_index) = calculateReceivingFlow_FQ(l,t); 109 | end 110 | end 111 | 112 | %compute transfer flows with the NODE MODEL 113 | switch Nmod 114 | case 'OC' 115 | TransferFlow = NodeModel(nbIn,nbOut,SF,TF{n,t-1},RF,capacities(incomingLinks)*dt); 116 | case 'DP' 117 | TransferFlow = NodeModel_demand_prop(nbIn,nbOut,SF,TF{n,t-1},RF,capacities(incomingLinks)*dt); 118 | case 'NR' 119 | TransferFlow = NodeModel_no_redistribution(incomingLinks,outgoingLinks,SF,TF{n,t-1},RF,capacities,dt); 120 | end 121 | 122 | %update CVN values 123 | cvn_down(incomingLinks,t)=cvn_down(incomingLinks,t-1)+sum(TransferFlow,2); 124 | cvn_up(outgoingLinks,t)=cvn_up(outgoingLinks,t-1)+sum(TransferFlow,1)'; 125 | end 126 | end 127 | 128 | %DESTINATION NODES<---------------------------------------------------------------------------------------------------------------------------- 129 | %this nested function goes over all destination nodes 130 | loadDestinationNodes(t); 131 | end 132 | 133 | %All nested function follow below: 134 | 135 | %Nested function for finding sending flows 136 | function SF = calculateSendingFlow(l,t) 137 | SF = capacities(l)*dt; 138 | time = timeSlices(t)-lengths(l)/freeSpeeds(l); 139 | val = findCVN(cvn_up(l,:),time,timeSlices,dt); 140 | SF = min(SF,val-cvn_down(l,t-1)); 141 | end 142 | 143 | %Nested function for finding receiving flows for a vertical queue 144 | function RF = calculateReceivingFlow_VQ(l,t) 145 | RF = capacities(l)*dt; 146 | end 147 | 148 | %Nested function for finding receiving flows for a horizontal queue 149 | function RF = calculateReceivingFlow_HQ(l,t) 150 | RF = capacities(l)*dt; 151 | val = cvn_down(l,t-1)+kJams(l)*lengths(l); 152 | RF=min(RF,val-cvn_up(l,t-1)); 153 | end 154 | 155 | %Nested function for finding receiving flows for a physical queue 156 | function RF = calculateReceivingFlow_FQ(l,t) 157 | RF = capacities(l)*dt; 158 | time = timeSlices(t)-lengths(l)/wSpeeds(l); 159 | val = findCVN(cvn_down(l,:),time,timeSlices,dt)+kJams(l)*lengths(l); 160 | RF = min(RF,val-cvn_up(l,t-1)); 161 | end 162 | 163 | %Nested function used for finding CVN values inbetween time slices 164 | function val = findCVN(cvn,time,timeSlices,dt) 165 | if time<=timeSlices(1) 166 | val=0; 167 | return; 168 | elseif time>=timeSlices(end) 169 | val=cvn(end); 170 | return; 171 | else 172 | t1=ceil(time/dt); 173 | t2=t1+1; 174 | val = cvn(t1)+(time/dt-t1+1)*(cvn(t2)-cvn(t1)); 175 | end 176 | end 177 | 178 | %Nested function that assigns the origin flow 179 | function loadOriginNodes(t) 180 | %update origin nodes 181 | for o_index=1:length(origins) 182 | o = origins(o_index); 183 | outgoingLinks = find(fromNodes==o); 184 | for l_index=1:length(outgoingLinks) 185 | l=outgoingLinks(l_index); 186 | %calculation sending flow 187 | SF = TF{o,t-1}.*sum(ODmatrix(o_index,:,t-1))*dt; 188 | cvn_up(l,t)=cvn_up(l,t-1) + SF; 189 | end 190 | end 191 | end 192 | 193 | %Nested function that assigns the destination flow 194 | function loadDestinationNodes(t) 195 | %update origin nodes 196 | for d_index=1:length(destinations) 197 | d = destinations(d_index); 198 | incomingLinks = find(toNodes==d); 199 | for l_index=1:length(incomingLinks) 200 | l=incomingLinks(l_index); 201 | %calculation sending flow 202 | SF = capacities(l)*dt; 203 | SF = min(SF,findCVN(cvn_up(l,:),timeSlices(t)-lengths(l)/freeSpeeds(l),timeSlices,dt)-cvn_down(l,t-1)); 204 | cvn_down(l,t)=cvn_down(l,t-1) + SF; 205 | end 206 | end 207 | end 208 | end -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/MSA_DET.m: -------------------------------------------------------------------------------- 1 | function [flows] = MSA_DET(odmatrix,nodes,links) 2 | %Method of successive averages for calculating deterministic user 3 | %equilibrium 4 | % 5 | %SYNTAX 6 | % [flows] = MSA_exercise(odmatrix,nodes,links) 7 | % 8 | %DESCRIPTION 9 | % returns the flow on each link in the deterministic user equilibrium as 10 | % calculated by the method of successive averages 11 | % 12 | %INPUTS 13 | % odmatrix: static origin/destination matrix 14 | % nodes: table with all the nodes in the network. 15 | % links: table with all the links in the network 16 | 17 | % This file is part of the matlab package for dynamic traffic assignments 18 | % developed by the KULeuven. 19 | % 20 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 21 | % 22 | % This program is free software: you can redistribute it and/or modify 23 | % it under the terms of the GNU General Public License as published by 24 | % the Free Software Foundation, either version 3 of the License, or 25 | % any later version. 26 | % 27 | % This program is distributed in the hope that it will be useful, 28 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 29 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 30 | % GNU General Public License for more details. 31 | % 32 | % You should have received a copy of the GNU General Public License 33 | % along with this program. If not, see . 34 | % 35 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 36 | % or contact: willem.himpe {@} kuleuven.be 37 | 38 | %setup the output figure 39 | h = figure; 40 | semilogy(0,NaN); 41 | start_time = cputime; 42 | 43 | %Maximum number of iterations 44 | maxIt = 20; 45 | 46 | %initilization 47 | totLinks = size(links.toNode,1); 48 | originFlows=zeros(totLinks,size(odmatrix,1)); 49 | 50 | %Initialize the iteration numbering 51 | it = 0; 52 | 53 | %initialize the travel cost 54 | %note that the total flow on a link is the sum of the origin based flow 55 | %on that link 56 | alpha = 0.15; 57 | beta = 4; 58 | travelCosts = calculateCostBPR(alpha,beta,sum(originFlows,2),links.length,links.freeSpeed,links.capacity); 59 | 60 | %initialize the gap function 61 | gap = inf; 62 | 63 | %MAIN LOOP: iterate until convergence is reached or maximum number of 64 | %iterations is reached 65 | while it < maxIt && gap > 10^-6 66 | it = it+1; 67 | 68 | %Compute new flows via all-or-nothing assignment 69 | newOriginFlows = allOrNothing(odmatrix, nodes, links, travelCosts); 70 | 71 | %calculate the update step 72 | update = (newOriginFlows - originFlows); 73 | 74 | %calculate new flows 75 | originFlows = originFlows + 1/it*update; 76 | 77 | %update costs 78 | travelCosts = calculateCostBPR(alpha,beta,sum(originFlows,2),links.length,links.freeSpeed,links.capacity); 79 | 80 | %convergence gap 81 | gap = reducedCostGap(originFlows,travelCosts,links,nodes); 82 | 83 | %plot convergence in function of computation time 84 | figure(h) 85 | hold on 86 | semilogy(cputime-start_time,gap,'r.') 87 | end 88 | 89 | %Check for number of iterations until convergence 90 | if it >= maxIt 91 | disp(['Maximum Iteration limit reached: ', num2str(maxIt)]); 92 | else 93 | disp(['Convergence reached in iteration ', num2str(it)]); 94 | end 95 | 96 | %Return the total flow for every link (sum over all origines) 97 | flows = sum(originFlows,2); 98 | 99 | end -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/MSA_STOCH_D.m: -------------------------------------------------------------------------------- 1 | function [flows] = MSA_STOCH_D(odmatrix,nodes,links,theta) 2 | %Method of successive averages for calculating deterministic user 3 | %equilibrium 4 | % 5 | %SYNTAX 6 | % [flows] = MSA_exercise_D(ODmatrix,nodes,links) 7 | % 8 | %DESCRIPTION 9 | % returns the flow on each link in the stochastic user equilibrium as 10 | % calculated by the method of successive averages 11 | % 12 | %INPUTS 13 | % odmatrix: static origin/destination matrix 14 | % nodes: table with all the nodes in the network. 15 | % links: table with all the links in the network 16 | % theta: stochastic distribution parameter (related to the value of time) 17 | 18 | % This file is part of the matlab package for dynamic traffic assignments 19 | % developed by the KULeuven. 20 | % 21 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 22 | % 23 | % This program is free software: you can redistribute it and/or modify 24 | % it under the terms of the GNU General Public License as published by 25 | % the Free Software Foundation, either version 3 of the License, or 26 | % any later version. 27 | % 28 | % This program is distributed in the hope that it will be useful, 29 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 30 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 31 | % GNU General Public License for more details. 32 | % 33 | % You should have received a copy of the GNU General Public License 34 | % along with this program. If not, see . 35 | % 36 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 37 | % or contact: willem.himpe {@} kuleuven.be 38 | 39 | %setup the output figure 40 | h = figure; 41 | semilogy(0,NaN); 42 | start_time = cputime; 43 | 44 | %Maximum number of iterations 45 | maxIt = 20; 46 | 47 | %initilization 48 | totLinks = size(links.toNode,1); 49 | originFlows=zeros(totLinks,size(odmatrix,1)); 50 | 51 | %Initialize the iteration numbering 52 | it = 0; 53 | 54 | %initialize the travel cost 55 | %note that the total flow on a link is the sum of the origin based flow 56 | %on that link 57 | alpha = 0.15; 58 | beta = 4; 59 | travelCosts = calculateCostBPR(alpha,beta,sum(originFlows,2),links.length,links.freeSpeed,links.capacity); 60 | 61 | %initialize the gap function 62 | gap = inf; 63 | 64 | %MAIN LOOP: iterate until convergence is reached or maximum number of 65 | %iterations is reached 66 | while it < maxIt && gap > 10^-8 67 | it = it+1; 68 | 69 | %Compute new flows via the implicit routing scheme of Dail (1971) 70 | newOriginFlows = Dial(odmatrix,nodes,links,travelCosts,theta); 71 | 72 | %calculate the update step 73 | update = (newOriginFlows - originFlows); 74 | 75 | %calculate new flows 76 | originFlows = originFlows + 1/it^(2/3)*update; 77 | 78 | %update costs 79 | travelCosts = calculateCostBPR(alpha,beta,sum(originFlows,2),links.length,links.freeSpeed,links.capacity); 80 | 81 | %convergence gap 82 | gap = sum(sum(abs(update))); 83 | 84 | %plot convergence 85 | figure(h) 86 | hold on 87 | semilogy(cputime-start_time,gap,'r.') 88 | end 89 | 90 | %Check for number of iterations until convergence 91 | if it >= maxIt 92 | disp(['Maximum Iteration limit reached: ', num2str(maxIt)]); 93 | else 94 | disp(['Convergenced reache in iteration ', num2str(it)]); 95 | end 96 | 97 | %Return the total flow for every linnk (sum over all origines) 98 | flows = sum(originFlows,2); 99 | 100 | end -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/MSA_STOCH_S.m: -------------------------------------------------------------------------------- 1 | function [flows] = MSA_STOCH_S(odmatrix,nodes,links,theta) 2 | %Method of successive averages for calculating deterministic user 3 | %equilibrium 4 | % 5 | %SYNTAX 6 | % [flows] = MSA_exercise_S(ODmatrix,nodes,links) 7 | % 8 | %DESCRIPTION 9 | % returns the flow on each link in the stochastic user equilibrium as 10 | % calculated by the method of successive averages 11 | % 12 | %INPUTS 13 | % odmatrix: static origin/destination matrix 14 | % nodes: table with all the nodes in the network. 15 | % links: table with all the links in the network 16 | % theta: stochastic distribution parameter (related to the value of time) 17 | 18 | % This file is part of the matlab package for dynamic traffic assignments 19 | % developed by the KULeuven. 20 | % 21 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 22 | % 23 | % This program is free software: you can redistribute it and/or modify 24 | % it under the terms of the GNU General Public License as published by 25 | % the Free Software Foundation, either version 3 of the License, or 26 | % any later version. 27 | % 28 | % This program is distributed in the hope that it will be useful, 29 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 30 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 31 | % GNU General Public License for more details. 32 | % 33 | % You should have received a copy of the GNU General Public License 34 | % along with this program. If not, see . 35 | % 36 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 37 | % or contact: willem.himpe {@} kuleuven.be 38 | 39 | %setup the output figure 40 | h = figure; 41 | semilogy(0,NaN); 42 | start_time = cputime; 43 | 44 | %Maximum number of iterations 45 | maxIt = 20; 46 | 47 | %initilization 48 | totLinks = size(links.toNode,1); 49 | originFlows=zeros(totLinks,size(odmatrix,1)); 50 | 51 | %Initialize the iteration numbering 52 | it = 0; 53 | 54 | %initialize the travel cost 55 | %note that the total flow on a link is the sum of the origin based flow 56 | %on that link 57 | alpha = 0.15; 58 | beta = 4; 59 | travelCosts = calculateCostBPR(alpha,beta,sum(originFlows,2),links.length,links.freeSpeed,links.capacity); 60 | 61 | %initialize the different route alternatives 62 | [incidenceODtoR, incidenceLtoR] = enumerateRoutes(odmatrix,nodes,links,[],travelCosts,5); 63 | 64 | %initialize the gap function 65 | gap = inf; 66 | 67 | %MAIN LOOP: iterate until convergence is reached or maximum number of 68 | %iterations is reached 69 | while it < maxIt && gap > 10^-8 70 | it = it+1; 71 | 72 | %Compute new flows via logit distribution over all initialized routes 73 | newOriginFlows = stochastic(odmatrix, links, travelCosts,theta,incidenceODtoR,incidenceLtoR); 74 | 75 | %calculate the update step 76 | update = (newOriginFlows - originFlows); 77 | 78 | %calculate new flows 79 | originFlows = originFlows + 1/it*update; 80 | 81 | %update costs 82 | travelCosts = calculateCostBPR(alpha,beta,sum(originFlows,2),links.length,links.freeSpeed,links.capacity); 83 | 84 | %convergence gap 85 | gap = sum(sum(abs(update))); 86 | 87 | %plot convergence in function of computation time 88 | figure(h) 89 | hold on 90 | semilogy(cputime-start_time,gap,'r.') 91 | end 92 | 93 | %Check for number of iterations until convergence 94 | if it >= maxIt 95 | disp(['Maximum Iteration limit reached: ', num2str(maxIt)]); 96 | else 97 | disp(['Convergence reached in iteration ', num2str(it)]); 98 | end 99 | 100 | %Return the total flow for every link (sum over all origines) 101 | flows = sum(originFlows,2); 102 | 103 | end -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/NodeModel.m: -------------------------------------------------------------------------------- 1 | function [TurnFlows] = NodeModel(nbIncomingLinks,nbOutgoingLinks,sendingFlow,turningFractions,receivingFlow,incomingLinks_capacity) 2 | %#codegen 3 | %coder.inline('never'); 4 | 5 | %Node model 6 | % 7 | %SYNTAX 8 | % [flowIncomingLinks,flowOutgoingLinks] = NodeModel(incomingLinks,outgoingLinks,sendingFlow,turningFractions,receivingFlow,caps,timeInterval) 9 | % 10 | %DESCRIPTION 11 | % returns the transfer flow over a node 12 | % 13 | %INPUTS 14 | % incomingLinks: id of the incoming links 15 | % outgoingLinks: id of the outgoing links 16 | % sendingFlow: the sending flow 17 | % turningFractions: turning fractions at the node 18 | % receivingFlow: the receiving flow 19 | % links: list of all the links in the network 20 | % Each entry of the list represents one link. Each link is a structure that 21 | % has at least a link ID and an upstream and downstream node. 22 | % timeInterval: updating interval 23 | 24 | % This file is part of the matlab package for dynamic traffic assignments 25 | % developed by the KULeuven. 26 | % 27 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 28 | % 29 | % This program is free software: you can redistribute it and/or modify 30 | % it under the terms of the GNU General Public License as published by 31 | % the Free Software Foundation, either version 3 of the License, or 32 | % any later version. 33 | % 34 | % This program is distributed in the hope that it will be useful, 35 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 36 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 37 | % GNU General Public License for more details. 38 | % 39 | % You should have received a copy of the GNU General Public License 40 | % along with this program. If not, see . 41 | % 42 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 43 | % or contact: willem.himpe {@} kuleuven.be 44 | 45 | %STATIC INITIALIZATION WITH MAX 7 IN- AND OUTGOING LINKS 46 | 47 | %check also here dynamic initialization based on actual number of incoming 48 | %and outgoing links 49 | 50 | %initialize 51 | adjustedReceivingFlow = receivingFlow; % adjusted receiving flow 52 | competingLinks = repmat(sendingFlow>0,1,nbOutgoingLinks); 53 | competingLinks(turningFractions==0)=0; % competing links 54 | activeOutLinks = any(competingLinks,1);% active outgoing links 55 | 56 | distrFactors = turningFractions.*repmat(incomingLinks_capacity,1,nbOutgoingLinks); %oriented capacity proportional 57 | TurnFlows = zeros(nbIncomingLinks,nbOutgoingLinks); 58 | 59 | while any(activeOutLinks) 60 | alpha=adjustedReceivingFlow./sum(distrFactors.*competingLinks,1)'; 61 | alpha(~activeOutLinks)=inf; 62 | [alpha_min,j]=min(alpha); 63 | if any(sendingFlow(competingLinks(:,j))<=alpha_min*incomingLinks_capacity(competingLinks(:,j))) 64 | for a=1:nbIncomingLinks 65 | if competingLinks(a,j) 66 | if sendingFlow(a)<=alpha_min*incomingLinks_capacity(a) 67 | for b=1:nbOutgoingLinks 68 | if activeOutLinks(b) 69 | TurnFlows(a,b)=turningFractions(a,b)*sendingFlow(a); 70 | adjustedReceivingFlow(b)=adjustedReceivingFlow(b)-TurnFlows(a,b); 71 | competingLinks(a,b)=false; 72 | if all(~competingLinks(:,b)) 73 | activeOutLinks(b)=false; 74 | end 75 | end 76 | end 77 | end 78 | end 79 | end 80 | else 81 | for a=1:nbIncomingLinks 82 | if competingLinks(a,j) 83 | for b=1:nbOutgoingLinks 84 | if activeOutLinks(b) 85 | TurnFlows(a,b)=alpha_min*distrFactors(a,b); 86 | adjustedReceivingFlow(b)=adjustedReceivingFlow(b)-TurnFlows(a,b); 87 | competingLinks(a,b)=false; 88 | if all(~competingLinks(:,b)) 89 | activeOutLinks(b)=false; 90 | end 91 | end 92 | end 93 | end 94 | end 95 | activeOutLinks(j)=false; 96 | end 97 | end 98 | end -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/NodeModel_demand_prop.m: -------------------------------------------------------------------------------- 1 | function [TurnFlows] = NodeModel_demand_prop(nbIncomingLinks,nbOutgoingLinks,sendingFlow,turningFractions,receivingFlow,incomingLinks_capacity) 2 | %#codegen 3 | %coder.inline('never'); 4 | 5 | %Node model 6 | % 7 | %SYNTAX 8 | % [flowIncomingLinks,flowOutgoingLinks] = NodeModel(incomingLinks,outgoingLinks,sendingFlow,turningFractions,receivingFlow,caps,timeInterval) 9 | % 10 | %DESCRIPTION 11 | % returns the transfer flow over a node 12 | % 13 | %INPUTS 14 | % incomingLinks: id of the incoming links 15 | % outgoingLinks: id of the outgoing links 16 | % sendingFlow: the sending flow 17 | % turningFractions: turning fractions at the node 18 | % receivingFlow: the receiving flow 19 | % links: list of all the links in the network 20 | % Each entry of the list represents one link. Each link is a structure that 21 | % has at least a link ID and an upstream and downstream node. 22 | % timeInterval: updating interval 23 | 24 | % This file is part of the matlab package for dynamic traffic assignments 25 | % developed by the KULeuven. 26 | % 27 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 28 | % 29 | % This program is free software: you can redistribute it and/or modify 30 | % it under the terms of the GNU General Public License as published by 31 | % the Free Software Foundation, either version 3 of the License, or 32 | % any later version. 33 | % 34 | % This program is distributed in the hope that it will be useful, 35 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 36 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 37 | % GNU General Public License for more details. 38 | % 39 | % You should have received a copy of the GNU General Public License 40 | % along with this program. If not, see . 41 | % 42 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 43 | % or contact: willem.himpe {@} kuleuven.be 44 | 45 | %STATIC INITIALIZATION WITH MAX 7 IN- AND OUTGOING LINKS 46 | 47 | %check also here dynamic initialization based on actual number of incoming 48 | %and outgoing links 49 | 50 | %initialize 51 | adjustedReceivingFlow = receivingFlow; % adjusted receiving flow 52 | competingLinks = repmat(sendingFlow>0,1,nbOutgoingLinks); 53 | competingLinks(turningFractions==0)=0; % competing links 54 | activeOutLinks = any(competingLinks,1);% active outgoing links 55 | 56 | distrFactors = turningFractions.*repmat(sendingFlow,1,nbOutgoingLinks); %demand proportional 57 | TurnFlows = zeros(nbIncomingLinks,nbOutgoingLinks); 58 | 59 | while any(activeOutLinks) 60 | alpha=adjustedReceivingFlow./sum(distrFactors.*competingLinks,1)'; 61 | alpha(~activeOutLinks)=inf; 62 | [alpha_min,j]=min(alpha); 63 | if any(sendingFlow(competingLinks(:,j))<=alpha_min*sendingFlow(competingLinks(:,j))) 64 | for a=1:nbIncomingLinks 65 | if competingLinks(a,j) 66 | if sendingFlow(a)<=alpha_min*sendingFlow(a) 67 | for b=1:nbOutgoingLinks 68 | if activeOutLinks(b) 69 | TurnFlows(a,b)=turningFractions(a,b)*sendingFlow(a); 70 | adjustedReceivingFlow(b)=adjustedReceivingFlow(b)-TurnFlows(a,b); 71 | competingLinks(a,b)=false; 72 | if all(~competingLinks(:,b)) 73 | activeOutLinks(b)=false; 74 | end 75 | end 76 | end 77 | end 78 | end 79 | end 80 | else 81 | for a=1:nbIncomingLinks 82 | if competingLinks(a,j) 83 | for b=1:nbOutgoingLinks 84 | if activeOutLinks(b) 85 | TurnFlows(a,b)=alpha_min*distrFactors(a,b); 86 | adjustedReceivingFlow(b)=adjustedReceivingFlow(b)-TurnFlows(a,b); 87 | competingLinks(a,b)=false; 88 | if all(~competingLinks(:,b)) 89 | activeOutLinks(b)=false; 90 | end 91 | end 92 | end 93 | end 94 | end 95 | activeOutLinks(j)=false; 96 | end 97 | end 98 | end -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/NodeModel_no_redistribution.m: -------------------------------------------------------------------------------- 1 | function [turnFlow] = NodeModel_no_redistribution(incomingLinks,outgoingLinks,sendingFlow,turningFractions,receivingFlow,caps,timeInterval) 2 | %Node model 3 | % 4 | %SYNTAX 5 | % [flowIncomingLinks,flowOutgoingLinks] = NodeModel(incomingLinks,outgoingLinks,sendingFlow,turningFractions,receivingFlow,caps,timeInterval) 6 | % 7 | %DESCRIPTION 8 | % returns the transfer flow over a node 9 | % 10 | %INPUTS 11 | % incomingLinks: index of the incoming links 12 | % outgoingLinks: index of the outgoing links 13 | % sendingFlow: the sending flow 14 | % turningFractions: turning fractions at the node 15 | % receivingFlow: the receiving flow 16 | % caps: capacities 17 | % timeInterval: updating interval 18 | 19 | 20 | % This file is part of the matlab package for dynamic traffic assignments 21 | % developed by the KULeuven. 22 | % 23 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 24 | % 25 | % This program is free software: you can redistribute it and/or modify 26 | % it under the terms of the GNU General Public License as published by 27 | % the Free Software Foundation, either version 3 of the License, or 28 | % any later version. 29 | % 30 | % This program is distributed in the hope that it will be useful, 31 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 32 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 33 | % GNU General Public License for more details. 34 | % 35 | % You should have received a copy of the GNU General Public License 36 | % along with this program. If not, see . 37 | % 38 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 39 | % or contact: willem.himpe {@} kuleuven.be 40 | 41 | 42 | intLinksToDo = length(incomingLinks); 43 | dblReceivingFraction = zeros(size(outgoingLinks)); 44 | blnRecalculateSendingFlow = zeros(size(incomingLinks)); 45 | dblSendingCapFlows = zeros(length(incomingLinks),length(outgoingLinks)); 46 | dblCapFlows = zeros(size(outgoingLinks)); 47 | dblCapacities = zeros(length(incomingLinks),length(outgoingLinks)); 48 | blnLinkIsDone = zeros(size(incomingLinks)); 49 | dblTransferFlows = zeros(length(incomingLinks),length(outgoingLinks)); 50 | 51 | %determine overflow factor of outgoing constraints: 52 | for j=1:length(outgoingLinks) 53 | dblDummy1 = 0; 54 | for i=1:length(incomingLinks) 55 | dblDummy1 = dblDummy1 + turningFractions(i,j)*sendingFlow(i); %holds the total sending flow:receiving link j 56 | end 57 | if dblDummy1 > 0 58 | dblReceivingFraction(j) = receivingFlow(j) / dblDummy1; %holds the fraction of the receiving flow of link j over the total sending flow:link j 59 | else 60 | dblReceivingFraction(j) = inf; %if there%s no sending flow:receiving link j 61 | end 62 | end 63 | 64 | %check which incoming links may be supply constrained and which are 65 | %definitely demand constrained: 66 | for i = 1:length(incomingLinks) 67 | for j=1:length(outgoingLinks) 68 | if dblReceivingFraction(j) < 1 69 | if turningFractions(i,j)*sendingFlow(i) > 0 70 | blnRecalculateSendingFlow(i) = 1; %1 if the receiving flow is exceeded for any link j:which link i sends flow 71 | break 72 | end 73 | end 74 | end 75 | if blnRecalculateSendingFlow(i) == 1 76 | dblDummy1 = caps(incomingLinks(i))*timeInterval/sendingFlow(i); 77 | for j = 1:length(outgoingLinks) 78 | dblSendingCapFlows(i,j) = turningFractions(i,j)*sendingFlow(i)*dblDummy1; %capacity flow from link i:link j 79 | dblCapFlows(j) = dblCapFlows(j) + dblSendingCapFlows(i,j); %total flow at capacity:link j 80 | end 81 | else %if no receiving flow is exceeded, link i can send all its flow 82 | for j = 1:length(outgoingLinks) 83 | dblCapacities(i,j) = turningFractions(i,j)*sendingFlow(i); 84 | end 85 | blnLinkIsDone(i) = 1; 86 | intLinksToDo = intLinksToDo - 1; 87 | end 88 | end 89 | 90 | %if there is at least one active supply constraint: calculate flows 91 | MostRestrictiveOutgoingLink = 0; 92 | while intLinksToDo > 0 93 | if MostRestrictiveOutgoingLink ~= length(outgoingLinks) 94 | MostRestrictiveOutgoingLink = MostRestrictiveOutgoingLink + 1; %needed:change MostRestrictiveOutgoingLink in case all RF%s become > 1 95 | else 96 | MostRestrictiveOutgoingLink = 1; 97 | end 98 | MostRestrictiveConstraint = 1; 99 | for j = 1:length(outgoingLinks) 100 | if dblCapFlows(j) > 0.000000000001 %prevents rounding off errors 101 | if receivingFlow(j) >= 0 102 | dblReceivingFraction(j) = receivingFlow(j) / dblCapFlows(j); 103 | else 104 | dblReceivingFraction(j) = 1; 105 | end 106 | else 107 | dblReceivingFraction(j) = 1; 108 | end 109 | if dblReceivingFraction(j) < MostRestrictiveConstraint %the smallest RF is stored in dblConstraint 110 | MostRestrictiveOutgoingLink = j; 111 | MostRestrictiveConstraint = dblReceivingFraction(j); 112 | end 113 | end 114 | intLinksInFF = zeros(0,1); 115 | for i = 1:length(incomingLinks) 116 | if blnLinkIsDone(i) == 0 %if this SLi is not done yet 117 | if turningFractions(i,MostRestrictiveOutgoingLink) > 0 %...and sends flow:most restrictive RLj 118 | if sendingFlow(i) <= caps(incomingLinks(i))*timeInterval*MostRestrictiveConstraint %if flow is not constrained 119 | intLinksInFF(size(intLinksInFF,1)+1) = i; %all not constrained links are stored 120 | for j = 1:length(outgoingLinks) 121 | dblCapacities(i,j) = turningFractions(i,j) * sendingFlow(i); 122 | end 123 | blnLinkIsDone(i) = 1; 124 | intLinksToDo = intLinksToDo - 1; 125 | end 126 | end 127 | end 128 | end 129 | if length(intLinksInFF) > 0 %at least 1 SL was not constrained 130 | for j = 1:length(outgoingLinks) 131 | for k = 1:length(intLinksInFF) 132 | dblCapFlows(j) = dblCapFlows(j) - dblSendingCapFlows(intLinksInFF(k), j); %in the end run, we don%t consider flows from link i anymore 133 | receivingFlow(j) = receivingFlow(j) - turningFractions(intLinksInFF(k),j) * sendingFlow(intLinksInFF(k)); 134 | end 135 | end 136 | else %no links in FF, MostRestrictiveConstraint is applied on all SLi towards MostRestrictiveOutgoingLink 137 | for i = 1:length(incomingLinks) 138 | if blnLinkIsDone(i) == 0 139 | if turningFractions(i,MostRestrictiveOutgoingLink) > 0 140 | dblCapFlows(MostRestrictiveOutgoingLink) = 0; 141 | receivingFlow(MostRestrictiveOutgoingLink) = 0; 142 | for j = 1:length(outgoingLinks) 143 | dblCapacities(i, j) = dblSendingCapFlows(i, j) * MostRestrictiveConstraint; 144 | end 145 | blnLinkIsDone(i) = 1; 146 | intLinksToDo = intLinksToDo - 1; 147 | end 148 | end 149 | end 150 | end 151 | end 152 | 153 | %incoming flows: 154 | dblMaxConstraint = zeros(size(incomingLinks)); 155 | for i = 1:length(incomingLinks) 156 | dblMaxConstraint(i) = 1; 157 | for j = 1:length(outgoingLinks) 158 | if turningFractions(i,j) * sendingFlow(i) > 0 159 | dblMaxConstraint(i) = min(dblMaxConstraint(i), dblCapacities(i, j) / (turningFractions(i,j) * sendingFlow(i))); 160 | %for each sending link, the maximum constraint is determined 161 | end 162 | end 163 | flowIncomingLinks(i) = dblMaxConstraint(i) * sendingFlow(i); %the flow of incoming links is calculated by the max constraint 164 | end 165 | 166 | %outgoing flows: 167 | for i = 1:length(incomingLinks) 168 | for j = 1:length(outgoingLinks) 169 | dblTransferFlows(i, j) = turningFractions(i,j) * flowIncomingLinks(i); 170 | end 171 | end 172 | for j = 1:length(outgoingLinks) 173 | flowOutgoingLinks(j) = 0; 174 | for i = 1:length(incomingLinks) 175 | flowOutgoingLinks(j) = flowOutgoingLinks(j) + dblTransferFlows(i, j); 176 | end 177 | end 178 | 179 | turnFlow = repmat(flowIncomingLinks',1,length(outgoingLinks)).*turningFractions; -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/PairComparator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HimpeWillem/MatlabTrafficToolbox/7e58302d0f493cfd6745c5f0daf340fcf6fdb63a/Dynamic Traffic Assignment/PairComparator.class -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/TF_init.m: -------------------------------------------------------------------------------- 1 | function [TF_P,arr_map] = TF_init(node_prop,links,destinations,dt,totT) 2 | %#codegen 3 | 4 | % This file is part of the matlab package for dynamic traffic assignments 5 | % developed by the KULeuven. 6 | % 7 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 8 | % 9 | % This program is free software: you can redistribute it and/or modify 10 | % it under the terms of the GNU General Public License as published by 11 | % the Free Software Foundation, either version 3 of the License, or 12 | % any later version. 13 | % 14 | % This program is distributed in the hope that it will be useful, 15 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | % GNU General Public License for more details. 18 | % 19 | % You should have received a copy of the GNU General Public License 20 | % along with this program. If not, see . 21 | % 22 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 23 | % or contact: willem.himpe {@} kuleuven.be 24 | 25 | %based on free flow travel times 26 | 27 | tt_base=links.length./links.freeSpeed; 28 | totDest = length(destinations); 29 | totNod = length(node_prop.nbIncomingLinks); 30 | totTF = length(node_prop.outgoingLinksTF); 31 | 32 | TF_P = zeros(totTF,totDest,totT); 33 | distance_destination = zeros(totNod,1); 34 | temp_distance = zeros(totNod,1); 35 | parent = zeros(totNod,1); 36 | 37 | arr_map = zeros(totNod,totT+1,totDest); 38 | 39 | %simple proportion matrix 40 | %in free flow conditions (this is used in the first DTA iteration) 41 | for d_index=1:totDest 42 | tt=tt_base; 43 | for n_index=1:totDest 44 | if n_index~=d_index 45 | n = destinations(n_index); 46 | tt(node_prop.incomingLinksList(node_prop.positionFirstIn(n):(node_prop.positionFirstIn(n)+node_prop.nbIncomingLinks(n)-1)))=inf; 47 | end 48 | end 49 | 50 | d=destinations(d_index); 51 | %dijkstra tree 52 | distance_destination(:,1) = inf; % it stores the shortest distance between each node and the source node; 53 | temp_distance(:,1) = inf; 54 | TF_P(:,d_index,:)=0; 55 | parent(:,1) = 0; 56 | distance_destination(d) = 0; 57 | temp_distance(d) = 0; 58 | 59 | for i = 1:totNod 60 | [time, u] = min(temp_distance(:,1)); % it starts from node with the shortest distance to the source; 61 | temp_distance(u) = inf; % mark it as visited; 62 | 63 | incomingLinks = node_prop.incomingLinksList(node_prop.positionFirstIn(u):(node_prop.positionFirstIn(u)+node_prop.nbIncomingLinks(u)-1)); 64 | 65 | for l_index=1:node_prop.nbIncomingLinks(u) % for each neighbors of node u; 66 | l=incomingLinks(l_index); 67 | s=links.fromNode(l); 68 | if (tt(l) + time < distance_destination(s)) 69 | distance_destination(s) = time + tt(l); % update the shortest distance when a shorter path is found; 70 | temp_distance(s) = distance_destination(s); 71 | parent(s) = u; % update its parent; 72 | end; 73 | end; 74 | end 75 | 76 | lout=find(parent(links.fromNode)==links.toNode); 77 | for l=1:length(lout) 78 | TF_P(node_prop.outgoingLinksTF==lout(l),d_index,:)=1; 79 | end 80 | 81 | % out_link_P(parent(links.startNodes)==links.endNodes,d_index,:)=1; 82 | arr_map(:,:,d_index)=distance_destination*ones(1,totT+1)+dt*ones(totNod,1)*[0:1:totT]; 83 | end -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/allOrNothingTF.m: -------------------------------------------------------------------------------- 1 | function [TF,gap_dt,gap_rc] = allOrNothingTF(nodes,links,destinations,simTT,cvn_up,dt,totT,rc_dt,rc_agg) 2 | 3 | % This file is part of the matlab package for dynamic traffic assignments 4 | % developed by the KULeuven. 5 | % 6 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 7 | % 8 | % This program is free software: you can redistribute it and/or modify 9 | % it under the terms of the GNU General Public License as published by 10 | % the Free Software Foundation, either version 3 of the License, or 11 | % any later version. 12 | % 13 | % This program is distributed in the hope that it will be useful, 14 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | % GNU General Public License for more details. 17 | % 18 | % You should have received a copy of the GNU General Public License 19 | % along with this program. If not, see . 20 | % 21 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 22 | % or contact: willem.himpe {@} kuleuven.be 23 | 24 | totDest = length(destinations); 25 | totNodes = length(nodes.id); 26 | totLinks = length(links.id); 27 | strN = links.fromNode; 28 | endN = links.toNode; 29 | 30 | if isempty(cvn_up) 31 | cvn_up=zeros(totLinks,totT+1,totDest); 32 | end 33 | 34 | TF = num2cell(ones(size(nodes.id,1),totT,totDest)); 35 | timeSteps = dt*[0:1:totT]; 36 | timeRC = rc_dt*[0:1:totT]; 37 | timeRC(timeRC>timeSteps(end))=[]; 38 | 39 | gap = zeros(totLinks,totT+1); 40 | gap_dt = 0; 41 | gap_rc = 0; 42 | act_t = false(1,totT+1); 43 | gVeh = floor(rc_dt/dt); 44 | switch rc_agg 45 | case 'first' 46 | timeVeh = 0; 47 | case 'middle' 48 | timeVeh = rc_dt/2; 49 | case 'last' 50 | timeVeh = rc_dt; 51 | case 'Null' 52 | for d_index=1:totDest 53 | arr_map_d(d_index); 54 | end 55 | TF=[]; 56 | return; 57 | case 'inst' 58 | for d_index=1:totDest 59 | d=destinations(d_index); 60 | netCostMatrix=sparse(endN,strN,simTT(:,1),totNodes,totNodes); 61 | [parent, ~] = dijkstra(netCostMatrix, d); 62 | for n=1:totNodes 63 | incomingLinks = find(endN==n); 64 | outgoingLinks = find(strN==n); 65 | TF{n,1,d_index}=zeros(max(1,length(incomingLinks)),length(outgoingLinks)); 66 | TF{n,1,d_index}(:,endN(outgoingLinks)==parent(n))=1; 67 | end 68 | end 69 | gap = []; 70 | return; 71 | 72 | end 73 | tVeh = floor(timeVeh/dt); 74 | 75 | for d_index=1:totDest 76 | %find the arrival map and update the gap function 77 | [~,parent] = arr_map_d(d_index); 78 | 79 | %compute update of the turning fractions based on the projected 80 | %gradient 81 | for n=1:totNodes 82 | next_rc=1; 83 | incomingLinks = find(endN==n); 84 | outgoingLinks = find(strN==n); 85 | 86 | if length(outgoingLinks)<=1 87 | %no update is required for a non divergent node 88 | for t=1:totT 89 | TF{n,t,d_index}=ones(max(1,length(incomingLinks)),max(1,length(outgoingLinks))); 90 | end 91 | else 92 | for t=1:totT 93 | %update all turning fractions within the route choice 94 | %interval by same value 95 | if timeSteps(t)>=timeRC(min(length(timeRC),next_rc)) 96 | next_rc = next_rc+1; 97 | %find the parent value 98 | par = parent(n,min(totT+1,t+tVeh)); 99 | act_t(min(totT+1,t+tVeh))=true; 100 | end 101 | %updating of the turning fractions 102 | TF{n,t,d_index}=zeros(max(1,length(incomingLinks)),length(outgoingLinks)); 103 | TF{n,t,d_index}(:,endN(outgoingLinks)==par)=1; 104 | end 105 | end 106 | end 107 | 108 | gap_dt=gap_dt+sum(sum(gap(:,2:end).*diff(cvn_up(:,:,d_index),1,2))); 109 | gap_rc=gap_rc+sum(sum(gap(:,act_t).*diff(cvn_up(:,[1,find(act_t)+gVeh-tVeh],d_index),1,2))); 110 | end 111 | 112 | 113 | 114 | 115 | %Nested function used for finding the destination based arrival map 116 | function [arr_map,parent] = arr_map_d(d_index) 117 | d=destinations(d_index); 118 | netCostMatrix=sparse(endN,strN,simTT(:,end),totNodes,totNodes); 119 | [par, dist] = dijkstra(netCostMatrix, d); 120 | parent = zeros(totNodes,totT+1); 121 | parent(:,totT+1) = par; 122 | arr_map = zeros(totNodes,totT+1); 123 | arr_map(:,totT+1)=dist+dt*totT; 124 | for t=totT+1:-1:1 125 | for n=1:totNodes 126 | if any(n==destinations) 127 | if n~=d 128 | arr_map(n,t)=inf; 129 | else 130 | arr_map(n,t)=(t-1)*dt; 131 | end 132 | continue; 133 | end 134 | outgoingLinks = find(strN==n); 135 | arr = inf; 136 | for l=outgoingLinks' 137 | time=timeSteps(t)+simTT(l,t); 138 | if time>=timeSteps(end) 139 | val=time-dt*totT+arr_map(endN(l),end); 140 | else 141 | t1 = min(totT+1,max(t+1,1+floor(time/dt))); 142 | t2 = min(totT+1,t1+1); 143 | val = arr_map(endN(l),t1,:)+max(0,(1+time/dt-t1))*(arr_map(endN(l),t2,:)-arr_map(endN(l),t1,:)); 144 | end 145 | if cvn_up(l,t,d_index)>0 146 | gap(l,t) = gap(l,t) + val; 147 | end 148 | if val<=arr 149 | arr=val; 150 | parent(n,t) = endN(l); 151 | end 152 | end 153 | for l=outgoingLinks' 154 | if cvn_up(l,t,d_index)>0 155 | gap(l,t) = gap(l,t) - arr; 156 | end 157 | end 158 | arr_map(n,t)=arr; 159 | end 160 | end 161 | end 162 | end 163 | -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/buildODmatrix.m: -------------------------------------------------------------------------------- 1 | function [od_matrix,origins,destinations] = buildODmatrix(ODmatrices,timeSeries,dt,totT) 2 | 3 | % This file is part of the matlab package for dynamic traffic assignments 4 | % developed by the KULeuven. 5 | % 6 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 7 | % 8 | % This program is free software: you can redistribute it and/or modify 9 | % it under the terms of the GNU General Public License as published by 10 | % the Free Software Foundation, either version 3 of the License, or 11 | % any later version. 12 | % 13 | % This program is distributed in the hope that it will be useful, 14 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | % GNU General Public License for more details. 17 | % 18 | % You should have received a copy of the GNU General Public License 19 | % along with this program. If not, see . 20 | % 21 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 22 | % or contact: willem.himpe {@} kuleuven.be 23 | 24 | 25 | %find all non empty od cells 26 | sumOD=ODmatrices{1,1}; 27 | for t=2:length(ODmatrices) 28 | sumOD = sumOD+ODmatrices{1,t}; 29 | end 30 | 31 | %origins 32 | origins = find(sum(sumOD,2)>0)'; 33 | 34 | %destinations 35 | destinations = find(sum(sumOD,1)>0); 36 | 37 | %build odmatrix 38 | od_matrix = zeros(length(origins),length(destinations),totT); 39 | 40 | timeSteps = dt*[0:1:totT]; 41 | timeSeries = cell2mat(timeSeries); 42 | 43 | for t=1:totT 44 | tempSlices = unique([find(timeSeries<=timeSteps(t),1,'last'),find(timeSeries1 48 | tempSlices = tempSlices(1):tempSlices(end); 49 | tempFrac = (timeSeries(tempSlices(2))-timeSteps(t))/dt; 50 | od_matrix(:,:,t)=tempFrac*ODmatrices{tempSlices(1)}(origins,destinations); 51 | for i=2:length(tempSlices)-1 52 | tempFrac = (timeSeries(tempSlices(i+1))-timeSeries(tempSlices(i)))/dt; 53 | od_matrix(:,:,t)=od_matrix(:,:,t)+tempFrac*ODmatrices{tempSlices(end)}(origins,destinations); 54 | end 55 | tempFrac = (timeSteps(t+1)-timeSeries(tempSlices(end)))/dt; 56 | od_matrix(:,:,t)=od_matrix(:,:,t)+tempFrac*ODmatrices{tempSlices(end)}(origins,destinations); 57 | end 58 | end 59 | 60 | end -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/cvn2artt.m: -------------------------------------------------------------------------------- 1 | function simTT= cvn2artt(cvn_up,cvn_down,dt,totT,links) 2 | 3 | % This file is part of the matlab package for dynamic traffic assignments 4 | % developed by the KULeuven. 5 | % 6 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 7 | % 8 | % This program is free software: you can redistribute it and/or modify 9 | % it under the terms of the GNU General Public License as published by 10 | % the Free Software Foundation, either version 3 of the License, or 11 | % any later version. 12 | % 13 | % This program is distributed in the hope that it will be useful, 14 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | % GNU General Public License for more details. 17 | % 18 | % You should have received a copy of the GNU General Public License 19 | % along with this program. If not, see . 20 | % 21 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 22 | % or contact: willem.himpe {@} kuleuven.be 23 | 24 | simTT = zeros(length(links.id),totT+1); 25 | %compute the simulated travel times 26 | timeSteps = dt*[0:1:totT]; 27 | for l=1:length(links.length) 28 | [up,iun] = unique(cvn_up(l,:),'last'); 29 | if length(up)<=1 30 | simTT(l,:)=links.length(l)/links.freeSpeed(l); 31 | else 32 | simTT(l,:)=max(dt*[0:totT]-interp1(up,timeSteps(iun),cvn_down(l,:)),links.length(l)/links.freeSpeed(l)); 33 | simTT(l,cvn_up(l,:)-cvn_down(l,:)<10-3)=links.length(l)/links.freeSpeed(l); 34 | end 35 | end 36 | end -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/cvn2dens.m: -------------------------------------------------------------------------------- 1 | function simDens = cvn2dens(cvn_up,cvn_down,totT,links) 2 | 3 | % This file is part of the matlab package for dynamic traffic assignments 4 | % developed by the KULeuven. 5 | % 6 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 7 | % 8 | % This program is free software: you can redistribute it and/or modify 9 | % it under the terms of the GNU General Public License as published by 10 | % the Free Software Foundation, either version 3 of the License, or 11 | % any later version. 12 | % 13 | % This program is distributed in the hope that it will be useful, 14 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | % GNU General Public License for more details. 17 | % 18 | % You should have received a copy of the GNU General Public License 19 | % along with this program. If not, see . 20 | % 21 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 22 | % or contact: willem.himpe {@} kuleuven.be 23 | 24 | %compute the simulated densities 25 | simDens=(cvn_up-cvn_down)./repmat(links.length,1,totT+1); 26 | end 27 | 28 | 29 | -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/cvn2flows.m: -------------------------------------------------------------------------------- 1 | function simFlows = cvn2flows(cvn,dt) 2 | 3 | % This file is part of the matlab package for dynamic traffic assignments 4 | % developed by the KULeuven. 5 | % 6 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 7 | % 8 | % This program is free software: you can redistribute it and/or modify 9 | % it under the terms of the GNU General Public License as published by 10 | % the Free Software Foundation, either version 3 of the License, or 11 | % any later version. 12 | % 13 | % This program is distributed in the hope that it will be useful, 14 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | % GNU General Public License for more details. 17 | % 18 | % You should have received a copy of the GNU General Public License 19 | % along with this program. If not, see . 20 | % 21 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 22 | % or contact: willem.himpe {@} kuleuven.be 23 | 24 | %compute the simulated flows 25 | simFlows=diff(cvn,1,2)/dt; 26 | end 27 | -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/cvn2tt.m: -------------------------------------------------------------------------------- 1 | function simTT= cvn2tt(cvn_up,cvn_down,dt,totT,links) 2 | 3 | % This file is part of the matlab package for dynamic traffic assignments 4 | % developed by the KULeuven. 5 | % 6 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 7 | % 8 | % This program is free software: you can redistribute it and/or modify 9 | % it under the terms of the GNU General Public License as published by 10 | % the Free Software Foundation, either version 3 of the License, or 11 | % any later version. 12 | % 13 | % This program is distributed in the hope that it will be useful, 14 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | % GNU General Public License for more details. 17 | % 18 | % You should have received a copy of the GNU General Public License 19 | % along with this program. If not, see . 20 | % 21 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 22 | % or contact: willem.himpe {@} kuleuven.be 23 | 24 | simTT = zeros(length(links.id),totT+1); 25 | %compute the simulated travel times 26 | timeSteps = dt*[0:1:totT]; 27 | for l=1:length(links.length) 28 | [down,iun] = unique(cvn_down(l,:),'first'); 29 | if length(down)<=1 30 | simTT(l,:)=links.length(l)/links.freeSpeed(l); 31 | else 32 | simTT(l,:)=max(interp1(down,timeSteps(iun),cvn_up(l,:))-dt*[0:totT],links.length(l)/links.freeSpeed(l)); 33 | simTT(l,cvn_up(l,:)-cvn_down(l,:)<10-3)=links.length(l)/links.freeSpeed(l); 34 | for t=2:totT+1 35 | simTT(l,t)=max(simTT(l,t),simTT(l,t-1)-dt); 36 | end 37 | end 38 | end 39 | end 40 | 41 | 42 | -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/dataParser.m: -------------------------------------------------------------------------------- 1 | function [links,node_prop] = dataParser(links,nodes,origins,destinations,dt) 2 | 3 | % This file is part of the matlab package for dynamic traffic assignments 4 | % developed by the KULeuven. 5 | % 6 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 7 | % 8 | % This program is free software: you can redistribute it and/or modify 9 | % it under the terms of the GNU General Public License as published by 10 | % the Free Software Foundation, either version 3 of the License, or 11 | % any later version. 12 | % 13 | % This program is distributed in the hope that it will be useful, 14 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | % GNU General Public License for more details. 17 | % 18 | % You should have received a copy of the GNU General Public License 19 | % along with this program. If not, see . 20 | % 21 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 22 | % or contact: willem.himpe {@} kuleuven.be 23 | 24 | %Setup nodes 25 | node_prop=[]; 26 | 27 | %connectivity (incoming and outgoing links) 28 | totNodes = length(nodes.id); 29 | 30 | for n=1:totNodes 31 | node(n).incomingLinks = find([links.toNode]==n)'; 32 | node(n).outgoingLinks = find([links.fromNode]==n)'; 33 | end 34 | 35 | %connectivity of nodes with incoming links and outgoing links 36 | node_prop.incomingLinksList=[node.incomingLinks]'; 37 | node_prop.outgoingLinksList=[node.outgoingLinks]'; 38 | 39 | node_prop.nbIncomingLinks=zeros(totNodes,1); 40 | node_prop.nbOutgoingLinks=zeros(totNodes,1); 41 | 42 | node_prop.positionFirstIn=zeros(totNodes,1); 43 | node_prop.positionFirstOut=zeros(totNodes,1); 44 | node_prop.positionFirstIn(1)=1; 45 | node_prop.positionFirstOut(1)=1; 46 | node_prop.nbIncomingLinks(1)=length(node(1).incomingLinks); 47 | node_prop.nbOutgoingLinks(1)=length(node(1).outgoingLinks); 48 | 49 | 50 | for n=2:totNodes 51 | node_prop.nbIncomingLinks(n)=length(node(n).incomingLinks); 52 | node_prop.nbOutgoingLinks(n)=length(node(n).outgoingLinks); 53 | node_prop.positionFirstIn(n)=node_prop.positionFirstIn(n-1)+node_prop.nbIncomingLinks(n-1); 54 | node_prop.positionFirstOut(n)=node_prop.positionFirstOut(n-1)+node_prop.nbOutgoingLinks(n-1); 55 | end 56 | 57 | %Turning Fractions setup 58 | node_prop.nbTF = (max(1,node_prop.nbIncomingLinks)).*(max(1,node_prop.nbOutgoingLinks)); 59 | 60 | node_prop.incomingLinksTF = zeros(sum(node_prop.nbTF),1); 61 | node_prop.incomingLinksTFindex = zeros(sum(node_prop.nbTF),1); 62 | node_prop.outgoingLinksTF = zeros(sum(node_prop.nbTF),1); 63 | node_prop.outgoingLinksTFindex = zeros(sum(node_prop.nbTF),1); 64 | 65 | node_prop.positionFirstTF = zeros(totNodes,1); 66 | 67 | lpos=1; 68 | for n=1:totNodes 69 | if any(n==origins) 70 | node_prop.positionFirstTF(n)=lpos; 71 | for lout=1:max(1,node_prop.nbOutgoingLinks(n)) 72 | node_prop.incomingLinksTF(lpos) = 0; 73 | if node_prop.nbOutgoingLinks(n)>0 74 | node_prop.outgoingLinksTF(lpos) = node_prop.outgoingLinksList(node_prop.positionFirstOut(n)+lout-1); 75 | node_prop.outgoingLinksTFindex(lpos) = lout; 76 | else 77 | node_prop.outgoingLinksTF(lpos) = 0; 78 | end 79 | lpos=lpos+1; 80 | end 81 | elseif node_prop.nbIncomingLinks(n)>0 || node_prop.nbOutgoingLinks(n)>0 82 | node_prop.positionFirstTF(n)=lpos; 83 | for lin=1:max(1,node_prop.nbIncomingLinks(n)) 84 | for lout=1:max(1,node_prop.nbOutgoingLinks(n)) 85 | if node_prop.nbIncomingLinks(n)>0 86 | node_prop.incomingLinksTF(lpos) = node_prop.incomingLinksList(node_prop.positionFirstIn(n)+lin-1); 87 | node_prop.incomingLinksTFindex(lpos) = lin; 88 | else 89 | node_prop.incomingLinksTF(lpos) = 0; 90 | end 91 | if node_prop.nbOutgoingLinks(n)>0 92 | node_prop.outgoingLinksTF(lpos) = node_prop.outgoingLinksList(node_prop.positionFirstOut(n)+lout-1); 93 | node_prop.outgoingLinksTFindex(lpos) = lout; 94 | else 95 | node_prop.outgoingLinksTF(lpos) = 0; 96 | end 97 | lpos=lpos+1; 98 | end 99 | end 100 | end 101 | end 102 | 103 | % Setup links 104 | 105 | %spillbackspeed 106 | links.ws = - links.capacity./(links.kJam - links.capacity./links.freeSpeed); 107 | 108 | %initialization for ILTM 109 | links.vf_index = floor((links.length./links.freeSpeed)/dt); 110 | links.vf_ratio = links.vf_index-(links.length./links.freeSpeed)/dt+1; 111 | links.vf_index = -links.vf_index-1; 112 | links.vw_index = floor((-links.length./links.ws)/dt); 113 | links.vw_ratio = links.vw_index-(-links.length./links.ws)/dt+1; 114 | links.vw_index = -links.vw_index-1; 115 | 116 | %links towards destinations should have infinit storage capacity (because 117 | %they are sinks) if one tries to model a restricted source is should be 118 | %modelled as a constraint on the inflow capacity of the link (not the jam 119 | %densitity) 120 | for d_index=1:length(destinations) 121 | d=destinations(d_index); 122 | for l_index=1:node_prop.nbIncomingLinks(d) 123 | l=node_prop.incomingLinksList(node_prop.positionFirstIn(d)+l_index-1); 124 | links.kJam(l)=inf; 125 | end 126 | end 127 | 128 | end -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/dens2tt.m: -------------------------------------------------------------------------------- 1 | function simTT= dens2tt(dens,totT,links) 2 | 3 | % This file is part of the matlab package for dynamic traffic assignments 4 | % developed by the KULeuven. 5 | % 6 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 7 | % 8 | % This program is free software: you can redistribute it and/or modify 9 | % it under the terms of the GNU General Public License as published by 10 | % the Free Software Foundation, either version 3 of the License, or 11 | % any later version. 12 | % 13 | % This program is distributed in the hope that it will be useful, 14 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | % GNU General Public License for more details. 17 | % 18 | % You should have received a copy of the GNU General Public License 19 | % along with this program. If not, see . 20 | % 21 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 22 | % or contact: willem.himpe {@} kuleuven.be 23 | 24 | w = links.capacity./(links.kJam-links.capacity./links.freeSpeed); 25 | simTT = max(repmat(links.length./links.freeSpeed,1,totT+1),dens.*repmat(links.length,1,totT+1)./(repmat(w,1,totT+1).*(repmat(links.kJam,1,totT+1)-dens))); 26 | 27 | end 28 | 29 | 30 | -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/dijkstra.m: -------------------------------------------------------------------------------- 1 | function [parents, distance, path] = dijkstra(matrix,source,varargin) 2 | 3 | % This file is part of the matlab package for dynamic traffic assignments 4 | % developed by the KULeuven. 5 | % 6 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 7 | % 8 | % This program is free software: you can redistribute it and/or modify 9 | % it under the terms of the GNU General Public License as published by 10 | % the Free Software Foundation, either version 3 of the License, or 11 | % any later version. 12 | % 13 | % This program is distributed in the hope that it will be useful, 14 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | % GNU General Public License for more details. 17 | % 18 | % You should have received a copy of the GNU General Public License 19 | % along with this program. If not, see . 20 | % 21 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 22 | % or contact: willem.himpe {@} kuleuven.be 23 | 24 | %initialize 25 | n = size(matrix, 2); 26 | parents = zeros(n,1); 27 | distance= inf(n,1); 28 | visited = false(n,1); 29 | 30 | %use priority queue for fast ordering and lookup of minimal distance 31 | comp = javaObject('PairComparator'); 32 | nodesQueue = java.util.PriorityQueue(max(10,ceil(n/10)),comp); 33 | 34 | %add the source node the list 35 | nodesQueue.add([0, source]); 36 | 37 | if nargin==2 38 | %if no destination node is provided the algorithm runs until all nodes 39 | %are labeled 40 | maxnum = numel(nodesQueue.peek()); 41 | 42 | while numel(nodesQueue.peek()) > 0 %nodesQueue.size()>0 43 | %remove closest node 44 | elem = nodesQueue.poll(); 45 | u = elem(2);%node 46 | k = elem(1);%travel cost 47 | %if it is already visited ignore it 48 | if visited(u) 49 | continue; 50 | end 51 | %fix the distance 52 | distance(u)=k; 53 | visited(u)=true; 54 | %go over all unvisited neighbours 55 | [~,nv,s] = find(matrix(u,:)); 56 | for i = 1:length(nv) 57 | v=nv(i); 58 | if~visited(v) 59 | temp = s(i) + distance(u); 60 | if temp < distance(v) 61 | %if a new value is found it is added to the list 62 | nodesQueue.add([temp, v]); 63 | distance(v) = temp; 64 | parents(v) = u; 65 | end 66 | end 67 | end 68 | end 69 | path=[]; 70 | else 71 | %if a destination node is provided the algorithm runs until that node 72 | %is labeled 73 | destination = varargin{1}; 74 | u=source; 75 | while u~=destination && numel(nodesQueue.peek()) > 0 76 | %remove closest node 77 | elem = nodesQueue.poll(); 78 | u = elem(2);%node 79 | k = elem(1);%travel cost 80 | %if it is already visited ignore it 81 | if visited(u) 82 | continue; 83 | end 84 | %fix the distance 85 | distance(u)=k; 86 | visited(u)=true; 87 | %go over all unvisited neighbours 88 | [~,nv,s] = find(matrix(u,:)); 89 | for i = 1:length(nv) 90 | v=nv(i); 91 | if~visited(v) 92 | temp = s(i) + distance(u); 93 | if temp < distance(v) 94 | %if a new value is found it is added to the list 95 | nodesQueue.add([temp, v]); 96 | distance(v) = temp; 97 | parents(v) = u; 98 | end 99 | end 100 | end 101 | end 102 | 103 | %get the nodes that forms the shortest path 104 | path = dijkstra_getpath(source,destination,parents); 105 | end 106 | 107 | 108 | 109 | -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/projGradTF.m: -------------------------------------------------------------------------------- 1 | function [updateTF,gap_dt,gap_rc] = projGradTF(nodes,links,destinations,simTT,cvn_up,dt,totT,rc_dt,rc_agg,TF,alpha) 2 | 3 | % This file is part of the matlab package for dynamic traffic assignments 4 | % developed by the KULeuven. 5 | % 6 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 7 | % 8 | % This program is free software: you can redistribute it and/or modify 9 | % it under the terms of the GNU General Public License as published by 10 | % the Free Software Foundation, either version 3 of the License, or 11 | % any later version. 12 | % 13 | % This program is distributed in the hope that it will be useful, 14 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | % GNU General Public License for more details. 17 | % 18 | % You should have received a copy of the GNU General Public License 19 | % along with this program. If not, see . 20 | % 21 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 22 | % or contact: willem.himpe {@} kuleuven.be 23 | 24 | totDest = length(destinations); 25 | totNodes = length(nodes.id); 26 | totLinks = length(links.id); 27 | strN = links.fromNode; 28 | endN = links.toNode; 29 | 30 | updateTF = num2cell(ones(size(nodes.id,1),totT,totDest)); 31 | timeSteps = dt*[0:1:totT]; 32 | timeRC = rc_dt*[0:1:totT]; 33 | timeRC(timeRC>timeSteps(end))=[]; 34 | 35 | gap = zeros(totLinks,totT+1); 36 | gap_dt = 0; 37 | gap_rc = 0; 38 | act_t = false(1,totT+1); 39 | gVeh = floor(rc_dt/dt); 40 | 41 | switch rc_agg 42 | case 'first' 43 | timeVeh = 0; 44 | case 'middle' 45 | timeVeh = rc_dt/2; 46 | case 'last' 47 | timeVeh = rc_dt; 48 | case 'Null' 49 | for d_index=1:totDest 50 | arr_map_d(d_index); 51 | end 52 | updateTF=[]; 53 | return; 54 | case 'inst' 55 | for d_index=1:totDest 56 | d=destinations(d_index); 57 | netCostMatrix=sparse(endN,strN,simTT(:,1),totNodes,totNodes); 58 | [parent, ~] = dijkstra(netCostMatrix, d); 59 | for n=1:totNodes 60 | incomingLinks = find(endN==n); 61 | outgoingLinks = find(strN==n); 62 | TF{n,1,d_index}=zeros(max(1,length(incomingLinks)),length(outgoingLinks)); 63 | TF{n,1,d_index}(:,endN(outgoingLinks)==parent(n))=1; 64 | end 65 | end 66 | gap = []; 67 | return; 68 | 69 | end 70 | tVeh = floor(timeVeh/dt); 71 | fracVeh = timeVeh/dt-tVeh; 72 | 73 | for d_index=1:totDest 74 | %find the arrival map and update the gap function 75 | arr_map = arr_map_d(d_index); 76 | 77 | %no updating of the turning fractions is required 78 | if strcmp(rc_agg,'Null') 79 | continue; 80 | end 81 | 82 | %compute update of the turning fractions based on the projected 83 | %gradient 84 | for n=1:totNodes 85 | next_rc=1; 86 | incomingLinks = find(endN==n); 87 | outgoingLinks = find(strN==n); 88 | 89 | if length(outgoingLinks)<=1 90 | %no update is required for a non divergent node 91 | for t=1:totT 92 | updateTF{n,t,d_index}=zeros(max(1,length(incomingLinks)),max(1,length(outgoingLinks))); 93 | end 94 | else 95 | for t=1:totT 96 | %update all turning fractions within the route choice 97 | %interval by same value 98 | if timeSteps(t)>=timeRC(next_rc) 99 | next_rc = next_rc+1; 100 | act_t(min(totT+1,t+tVeh))=true; 101 | 102 | %compute arrival travel time at downstream end of all outgoing links 103 | time=timeSteps(t)+timeVeh+(1-fracVeh)*(simTT(outgoingLinks,min(totT+1,t+tVeh)))+fracVeh*simTT(outgoingLinks,min(totT+1,t+tVeh+1)); 104 | t1 = min(totT+1,1+floor(time/dt)); 105 | vec1=sub2ind([totNodes,totT+1],endN(outgoingLinks),t1); 106 | t2 = min(totT+1,t1+1); 107 | vec2=sub2ind([totNodes,totT+1],endN(outgoingLinks),t2); 108 | frac=(1+time/dt-t1); 109 | %compute cost difference 110 | costDiff = arr_map(vec1)+frac.*(arr_map(vec2)-arr_map(vec1))-((1-fracVeh)*arr_map(n,min(totT+1,t+tVeh))+fracVeh*arr_map(n,min(totT+1,t+tVeh+1))); 111 | costDiff(time>=timeSteps(end)) = time(time>=timeSteps(end))-dt*totT+arr_map(endN(outgoingLinks(time>=timeSteps(end))),end)-((1-fracVeh)*(arr_map(n,min(totT+1,t+tVeh)))+fracVeh*arr_map(n,min(totT+1,t+tVeh+1))); 112 | %next project the difference in the feasible probability space 113 | P=zeros(1,length(outgoingLinks)); 114 | %only update turn probability if there is a positive turning fraction 115 | %towards a longer route 116 | if sum(TF{n,t,d_index}(1,costDiff>eps))>eps 117 | %actual projection with scaling of the cost 118 | %difference 119 | P=-min(alpha*costDiff',TF{n,t,d_index}(1,:)); 120 | %quasi-reduced projection 121 | [~,i]=min(costDiff); 122 | P(i)=0; 123 | P(i)=-sum(P); 124 | end 125 | end 126 | %updating of the turning fractions 127 | updateTF{n,t,d_index}=zeros(max(1,length(incomingLinks)),length(outgoingLinks)); 128 | updateTF{n,t,d_index}=repmat(P,max(1,length(incomingLinks)),1); 129 | end 130 | end 131 | end 132 | 133 | gap_dt=gap_dt+sum(sum(gap(:,2:end).*diff(cvn_up,1,2))); 134 | gap_rc=gap_rc+sum(sum(gap(:,act_t).*diff(cvn_up(:,[1,find(act_t)+gVeh-tVeh],d_index),1,2))); 135 | end 136 | 137 | %Nested function used for finding the destination based arrival map 138 | function arr_map = arr_map_d(d_index) 139 | d=destinations(d_index); 140 | netCostMatrix=sparse(endN,strN,simTT(:,end),totNodes,totNodes); 141 | [par, dist] = dijkstra(netCostMatrix, d); 142 | parent = zeros(totNodes,totT+1); 143 | parent(:,totT+1) = par; 144 | arr_map = zeros(totNodes,totT+1); 145 | arr_map(:,totT+1)=dist+dt*totT; 146 | for t=totT+1:-1:1 147 | for n=1:totNodes 148 | if any(n==destinations) 149 | if n~=d 150 | arr_map(n,t)=inf; 151 | else 152 | arr_map(n,t)=(t-1)*dt; 153 | end 154 | continue; 155 | end 156 | outgoingLinks = find(strN==n); 157 | arr = inf; 158 | for l=outgoingLinks' 159 | time=timeSteps(t)+simTT(l,t); 160 | if time>=timeSteps(end) 161 | val=time-dt*totT+arr_map(endN(l),end); 162 | else 163 | t1 = min(totT+1,max(t+1,1+floor(time/dt))); 164 | t2 = min(totT+1,t1+1); 165 | val = arr_map(endN(l),t1,:)+max(0,(1+time/dt-t1))*(arr_map(endN(l),t2,:)-arr_map(endN(l),t1,:)); 166 | end 167 | if cvn_up(l,t,d_index)>0 168 | gap(l,t) = gap(l,t) + val; 169 | end 170 | if val0 177 | gap(l,t) = gap(l,t) - arr; 178 | end 179 | end 180 | arr_map(n,t)=arr; 181 | end 182 | end 183 | end 184 | end 185 | -------------------------------------------------------------------------------- /Dynamic Traffic Assignment/stochasticTF.m: -------------------------------------------------------------------------------- 1 | function [TF,gap_dt,gap_dt_s] = stochasticTF(nodes,links,destinations,simTT,cvn_up,dt,totT,rc_dt,rc_agg,theta) 2 | 3 | totDest = length(destinations); 4 | totNodes = length(nodes.id); 5 | totLinks = length(links.id); 6 | strN = links.fromNode; 7 | endN = links.toNode; 8 | 9 | if isempty(cvn_up) 10 | cvn_up=zeros(totLinks,totT+1,totDest); 11 | end 12 | 13 | updateTF = num2cell(ones(size(nodes.id,1),totT,totDest)); 14 | timeSteps = dt*[0:1:totT]; 15 | timeRC = rc_dt*[0:1:totT]; 16 | timeRC(timeRC>timeSteps(end))=[]; 17 | 18 | gap = zeros(totLinks,totT+1); 19 | gap_dt = 0; 20 | gap_s = zeros(totLinks,totT+1); 21 | gap_dt_s = 0; 22 | act_t = false(1,totT+1); 23 | gVeh = floor(rc_dt/dt); 24 | 25 | switch rc_agg 26 | case 'first' 27 | timeVeh = 0; 28 | case 'middle' 29 | timeVeh = rc_dt/2; 30 | case 'last' 31 | timeVeh = rc_dt; 32 | case 'Null' 33 | for d_index=1:totDest 34 | arr_map_d(d_index); 35 | end 36 | TF=[]; 37 | return; 38 | case 'inst' 39 | for d_index=1:totDest 40 | d=destinations(d_index); 41 | netCostMatrix=sparse(endN,strN,simTT(:,1),totNodes,totNodes); 42 | [parent, ~] = dijkstra(netCostMatrix, d); 43 | for n=1:totNodes 44 | incomingLinks = find(endN==n); 45 | outgoingLinks = find(strN==n); 46 | TF{n,1,d_index}=zeros(max(1,length(incomingLinks)),length(outgoingLinks)); 47 | TF{n,1,d_index}(:,endN(outgoingLinks)==parent(n))=1; 48 | end 49 | end 50 | gap = []; 51 | return; 52 | 53 | end 54 | tVeh = floor(timeVeh/dt); 55 | fracVeh = timeVeh/dt-tVeh; 56 | 57 | for d_index=1:totDest 58 | %find the arrival map and update the gap function 59 | [arr_map,~] = arr_map_d(d_index); 60 | 61 | %use the arrival map order to compute the maximum perceived utility 62 | util_map = max_perc_util_d(arr_map,d_index); 63 | 64 | %compute update of the turning fractions based on the utiliy values 65 | for n=1:totNodes 66 | next_rc=1; 67 | incomingLinks = find(endN==n); 68 | outgoingLinks = find(strN==n); 69 | 70 | if length(outgoingLinks)<=1 71 | %no update is required for a non divergent node 72 | for t=1:totT 73 | TF{n,t,d_index}=ones(max(1,length(incomingLinks)),max(1,length(outgoingLinks))); 74 | end 75 | else 76 | for t=1:totT 77 | %update all turning fractions within the route choice 78 | %interval by same value 79 | if timeSteps(t)>=timeRC(min(length(timeRC),next_rc)) 80 | next_rc = next_rc+1; 81 | act_t(min(totT+1,t+tVeh))=true; 82 | 83 | %compute the turn probabilities 84 | P=zeros(1,length(outgoingLinks)); 85 | for i=1:length(outgoingLinks) 86 | l=outgoingLinks(i); 87 | if arr_map(n,min(totT+1,t+tVeh))>arr_map(endN(l),min(totT+1,t+tVeh)) 88 | time=timeSteps(min(totT+1,t+tVeh))+simTT(l,min(totT+1,t+tVeh)); 89 | n_down=endN(l); 90 | if time>=timeSteps(end) 91 | P(i)=exp((-simTT(l,min(totT+1,t+tVeh))+util_map(n_down,end)-util_map(n,min(totT+1,t+tVeh)))/theta); 92 | else 93 | t1 = min(totT+1,max(t+tVeh+1,1+floor(time/dt))); 94 | t2 = min(totT+1,t1+1); 95 | val = util_map(n_down,t1,:)+max(0,(1+time/dt-t1))*(util_map(n_down,t2,:)-util_map(n_down,t1,:)); 96 | P(i)=exp((-simTT(l,min(totT+1,t+tVeh))+val-util_map(n,min(totT+1,t+tVeh)))/theta); 97 | end 98 | end 99 | end 100 | end 101 | %updating of the turning fractions 102 | TF{n,t,d_index}=zeros(max(1,length(incomingLinks)),length(outgoingLinks)); 103 | TF{n,t,d_index}=repmat(P,max(1,length(incomingLinks)),1); 104 | end 105 | end 106 | end 107 | 108 | gap_dt=gap_dt+sum(sum(gap(:,2:end).*diff(cvn_up(:,:,d_index),1,2))); 109 | gap_dt_s=gap_dt_s+sum(sum(gap_s(:,2:end).*diff(cvn_up(:,:,d_index),1,2))); 110 | end 111 | 112 | %Nested function used for finding the destination based arrival map 113 | function [arr_map,parent] = arr_map_d(d_index) 114 | d=destinations(d_index); 115 | netCostMatrix=sparse(endN,strN,simTT(:,end),totNodes,totNodes); 116 | [par, dist] = dijkstra(netCostMatrix, d); 117 | parent = zeros(totNodes,totT+1); 118 | parent(:,totT+1) = par; 119 | arr_map = zeros(totNodes,totT+1); 120 | arr_map(:,totT+1)=dist+dt*totT; 121 | for t=totT+1:-1:1 122 | for n=1:totNodes 123 | if any(n==destinations) 124 | if n~=d 125 | arr_map(n,t)=inf; 126 | else 127 | arr_map(n,t)=(t-1)*dt; 128 | end 129 | continue; 130 | end 131 | outgoingLinks = find(strN==n); 132 | arr = inf; 133 | min_phi = inf; 134 | for l=outgoingLinks' 135 | time=timeSteps(t)+simTT(l,t); 136 | if time>=timeSteps(end) 137 | val=time-dt*totT+arr_map(endN(l),end); 138 | else 139 | t1 = min(totT+1,max(t+1,1+floor(time/dt))); 140 | t2 = min(totT+1,t1+1); 141 | val = arr_map(endN(l),t1,:)+max(0,(1+time/dt-t1))*(arr_map(endN(l),t2,:)-arr_map(endN(l),t1,:)); 142 | end 143 | if cvn_up(l,t,d_index)>0 144 | gap(l,t) = gap(l,t) + val; 145 | phi = theta*log(cvn_up(l,t,d_index)-cvn_up(l,max(t-1,1),d_index))+val-(t-1)*dt; 146 | gap_s(l,t) = phi; 147 | if phi<=min_phi 148 | min_phi=phi; 149 | end 150 | end 151 | if val<=arr 152 | arr=val; 153 | parent(n,t) = endN(l); 154 | end 155 | end 156 | for l=outgoingLinks' 157 | if cvn_up(l,t,d_index)>0 158 | gap(l,t) = gap(l,t) - arr; 159 | gap_s(l,t) = gap_s(l,t)-min_phi; 160 | end 161 | end 162 | arr_map(n,t)=arr; 163 | end 164 | end 165 | end 166 | 167 | %Nested function used for finding the maximum perceived utility 168 | function [util_map] = max_perc_util_d(arr_map,d_index) 169 | d=destinations(d_index); 170 | util_map = zeros(totNodes,totT+1); 171 | %first do the last time slice 172 | [~,sorted_n]=sort(arr_map(:,end)); 173 | for n_index=1:totNodes 174 | n=sorted_n(n_index); 175 | if any(n==destinations) 176 | if n~=d 177 | util_map(n,t)=-inf; 178 | else 179 | util_map(n,t)=0; 180 | end 181 | continue; 182 | end 183 | outgoingLinks = find(strN==n); 184 | for l=outgoingLinks' 185 | if arr_map(n,end)>arr_map(endN(l),end) 186 | util_map(n,end)=util_map(n,end)+exp((-simTT(l,end)+util_map(endN(l),end))/theta); 187 | end 188 | end 189 | util_map(n,end) = theta*log(util_map(n,end)); 190 | end 191 | 192 | %next do the others in upwind order 193 | for t=totT:-1:1 194 | for n=1:totNodes 195 | if any(n==destinations) 196 | if n~=d 197 | util_map(n,t)=-inf; 198 | else 199 | util_map(n,t)=0; 200 | end 201 | continue; 202 | end 203 | outgoingLinks = find(strN==n); 204 | for l=outgoingLinks' 205 | if arr_map(n,t)>arr_map(endN(l),t) 206 | time=timeSteps(t)+simTT(l,t); 207 | n_down=endN(l); 208 | if time>=timeSteps(end) 209 | util_map(n,t)=util_map(n,t)+exp((-simTT(l,t)+util_map(n_down,end))/theta); 210 | else 211 | t1 = min(totT+1,max(t+1,1+floor(time/dt))); 212 | t2 = min(totT+1,t1+1); 213 | val = util_map(n_down,t1,:)+max(0,(1+time/dt-t1))*(util_map(n_down,t2,:)-util_map(n_down,t1,:)); 214 | util_map(n,t)=util_map(n,t)+exp((-simTT(l,t)+val)/theta); 215 | end 216 | end 217 | end 218 | util_map(n,t) = theta*log(util_map(n,t)); 219 | end 220 | end 221 | end 222 | end 223 | -------------------------------------------------------------------------------- /Network Data/net1.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HimpeWillem/MatlabTrafficToolbox/7e58302d0f493cfd6745c5f0daf340fcf6fdb63a/Network Data/net1.mat -------------------------------------------------------------------------------- /Network Data/net1_old.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HimpeWillem/MatlabTrafficToolbox/7e58302d0f493cfd6745c5f0daf340fcf6fdb63a/Network Data/net1_old.mat -------------------------------------------------------------------------------- /Network Data/net2.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HimpeWillem/MatlabTrafficToolbox/7e58302d0f493cfd6745c5f0daf340fcf6fdb63a/Network Data/net2.mat -------------------------------------------------------------------------------- /Network Data/net2_old.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HimpeWillem/MatlabTrafficToolbox/7e58302d0f493cfd6745c5f0daf340fcf6fdb63a/Network Data/net2_old.mat -------------------------------------------------------------------------------- /Network Data/net3.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HimpeWillem/MatlabTrafficToolbox/7e58302d0f493cfd6745c5f0daf340fcf6fdb63a/Network Data/net3.mat -------------------------------------------------------------------------------- /Network Data/net3_old.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HimpeWillem/MatlabTrafficToolbox/7e58302d0f493cfd6745c5f0daf340fcf6fdb63a/Network Data/net3_old.mat -------------------------------------------------------------------------------- /Network Data/net4.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HimpeWillem/MatlabTrafficToolbox/7e58302d0f493cfd6745c5f0daf340fcf6fdb63a/Network Data/net4.mat -------------------------------------------------------------------------------- /Network Data/net4_old.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HimpeWillem/MatlabTrafficToolbox/7e58302d0f493cfd6745c5f0daf340fcf6fdb63a/Network Data/net4_old.mat -------------------------------------------------------------------------------- /Network Data/net5.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HimpeWillem/MatlabTrafficToolbox/7e58302d0f493cfd6745c5f0daf340fcf6fdb63a/Network Data/net5.mat -------------------------------------------------------------------------------- /Network Data/net5_old.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HimpeWillem/MatlabTrafficToolbox/7e58302d0f493cfd6745c5f0daf340fcf6fdb63a/Network Data/net5_old.mat -------------------------------------------------------------------------------- /Network Data/net6.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HimpeWillem/MatlabTrafficToolbox/7e58302d0f493cfd6745c5f0daf340fcf6fdb63a/Network Data/net6.mat -------------------------------------------------------------------------------- /Network Data/net6_old.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HimpeWillem/MatlabTrafficToolbox/7e58302d0f493cfd6745c5f0daf340fcf6fdb63a/Network Data/net6_old.mat -------------------------------------------------------------------------------- /Network Data/net7.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HimpeWillem/MatlabTrafficToolbox/7e58302d0f493cfd6745c5f0daf340fcf6fdb63a/Network Data/net7.mat -------------------------------------------------------------------------------- /Network Data/net7_old.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HimpeWillem/MatlabTrafficToolbox/7e58302d0f493cfd6745c5f0daf340fcf6fdb63a/Network Data/net7_old.mat -------------------------------------------------------------------------------- /Network Data/result_net1.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HimpeWillem/MatlabTrafficToolbox/7e58302d0f493cfd6745c5f0daf340fcf6fdb63a/Network Data/result_net1.mat -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MatlabTrafficToolbox 2 | This is the matlab package for dynamic traffic assignments developed by the KULeuven 3 | https://www.mech.kuleuven.be/en/cib/traffic/downloads 4 | -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | This is the matlab package for dynamic traffic assignments developed by the KULeuven 2 | contact: willem.himpe {@} kuleuven.be 3 | 4 | Content of the tutorials: 5 | 6 | -Dynamic Network Loading 7 | 8 | tutorial 1: Visualizing network data 9 | tutorial 2: Introducing the Link Transmission Model (LTM) 10 | tutorial 3: Comparing different node models with LTM 11 | tutorial 4: Comparing LTM & CTM 12 | tutorial 5: Comparing single commodity (SC) & multi commodity (MC) 13 | tutorial 6: Comparing explicit and implicit schemes 14 | tutorial 7: Comparing explicit and implicit schemes on a corridor 15 | tutorial 8: Faster data types and compilation (mex) for the implicit scheme 16 | tutorial 9: Warm starting the implicit scheme of the LTM 17 | 18 | -Dynamic Traffic Assignment 19 | 20 | tutorial 10: Introducing deterministic equilibrium routing 21 | tutorial 11: Advanced deterministic equilibrium routing 22 | tutorial 12: Deterministic instantaneous routing 23 | tutorial 13: Introducing implicit stochastic equilibrium routing 24 | tutorial 14: Advanced deterministic equilibrium routing with warm start 25 | 26 | -------------------------------------------------------------------------------- /Visualization Tools/animateSimulation.m: -------------------------------------------------------------------------------- 1 | function animateSimulation(nodes,links,values,timeSteps,frate) 2 | %Animate a simulation 3 | % 4 | % 5 | % 6 | %SYNTAX 7 | % animateSimulation(nodes,links,values,timeSteps,option) 8 | % 9 | %DESCRIPTION 10 | % simulate the loads on the network 11 | % 12 | %INPUTS 13 | % nodes: list of all the nodes in the network. 14 | % Each entry of the list represents one node. Each node is a structure that 15 | % has at least a node ID and an x and y coordinate of the node 16 | % links: list of all the links in the network 17 | % Each entry of the list represents one link. Each link is a structure that 18 | % has at least a link ID and an upstream and downstream node. 19 | % values: time dependent loads 20 | % timeSteps: simulation time of each step 21 | % scale: scale 22 | % option: 23 | 24 | % This file is part of the matlab package for dynamic traffic assignments 25 | % developed by the KULeuven. 26 | % 27 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 28 | % 29 | % This program is free software: you can redistribute it and/or modify 30 | % it under the terms of the GNU General Public License as published by 31 | % the Free Software Foundation, either version 3 of the License, or 32 | % any later version. 33 | % 34 | % This program is distributed in the hope that it will be useful, 35 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 36 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 37 | % GNU General Public License for more details. 38 | % 39 | % You should have received a copy of the GNU General Public License 40 | % along with this program. If not, see . 41 | % 42 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 43 | % or contact: willem.himpe {@} kuleuven.be 44 | 45 | display('press space bar to continue'); 46 | [handle_fig,handle_txt]=plotNetwork(nodes,links,false,11); 47 | 48 | %set axis 49 | strN = links.fromNode; 50 | endN = links.toNode; 51 | load = max(values,[],2); 52 | x=nodes.xco; 53 | y=nodes.yco; 54 | upX=x(strN); 55 | downX=x(endN); 56 | upY=y(strN); 57 | downY=y(endN); 58 | maxc=max(max(values)); 59 | scale = 1/maxc*0.25*sqrt((max(x)-min(x))^2+(max(y)-min(y))^2)/length(upX)^(1/2); 60 | vx=downX-upX; 61 | vy=downY-upY; 62 | vl=sqrt(vx.^2+vy.^2); 63 | vx=vx./vl; 64 | vy=vy./vl; 65 | sc=scale; 66 | sc=eps+scale*load'; 67 | xrec=[upX';upX'+sc.*vy';downX'+sc.*vy';downX']; 68 | yrec=[upY';upY'-sc.*vx';downY'-sc.*vx';downY']; 69 | margX=0.1*(max(x)-min(x))+100*eps; 70 | margY=0.1*(max(y)-min(y))+100*eps; 71 | axis([min(min(xrec))-margX max(max(xrec))+margX min(min(yrec))-margY max(max(yrec))+margY]) 72 | 73 | pause() 74 | delete(handle_txt); 75 | colorbar('EastOutside'); 76 | for t=1:size(timeSteps,2) 77 | title(num2str(timeSteps(t))); 78 | [handle_fig,handle_rect,handle_txt] = plotLoadedLinks(nodes,links,values(:,t),false,handle_fig,[],max(max(values(:,:)))); 79 | 80 | if isinf(frate) 81 | pause(); 82 | else 83 | pause(1/frate) 84 | end 85 | if ~isempty(handle_rect) 86 | try 87 | delete(handle_rect); 88 | end 89 | end 90 | if~isempty(handle_txt) 91 | try 92 | delete(handle_txt); 93 | end 94 | end 95 | if ~ishandle(handle_fig) 96 | return; 97 | end 98 | end 99 | 100 | end -------------------------------------------------------------------------------- /Visualization Tools/plotLoadedLinks.m: -------------------------------------------------------------------------------- 1 | function [handle_fig,handle_rect,handle_txt] = plotLoadedLinks(nodes,links,load,show_labels,fig_num,scale,maxLoad) 2 | %Plots the link loads on a network 3 | % 4 | % This file is part of the matlab package for dynamic traffic assignments 5 | % developed by the KULeuven. 6 | % 7 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 8 | % 9 | % This program is free software: you can redistribute it and/or modify 10 | % it under the terms of the GNU General Public License as published by 11 | % the Free Software Foundation, either version 3 of the License, or 12 | % any later version. 13 | % 14 | % This program is distributed in the hope that it will be useful, 15 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | % GNU General Public License for more details. 18 | % 19 | % You should have received a copy of the GNU General Public License 20 | % along with this program. If not, see . 21 | % 22 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 23 | % or contact: willem.himpe {@} kuleuven.be 24 | % 25 | % 26 | %SYNTAX 27 | % [handle_fig,handle_rect,handle_txt] = plotLoadedLinks(nodes,links,load,show_labels,fig_num,scale,maxLoad) 28 | % 29 | %DESCRIPTION 30 | % plots loads on link in the network and returns figure handles of the loaded network 31 | % 32 | %INPUTS 33 | % nodes: list of all the nodes in the network. 34 | % Each entry of the list represents one node. Each node is a structure that 35 | % has at least a node ID and an x and y coordinate of the node 36 | % links: list of all the links in the network 37 | % Each entry of the list represents one link. Each link is a structure that 38 | % has at least a link ID and an upstream and downstream node. 39 | % load: list of the load on each link 40 | % show_text: flag for visualizing labels 41 | % fig: parameter for the figure handle 42 | % maxLoad: scale parameter used for determining the loads 43 | 44 | % This file is part of the matlab package for dynamic traffic assignments 45 | % developed by the KULeuven. 46 | % 47 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 48 | % 49 | % This program is free software: you can redistribute it and/or modify 50 | % it under the terms of the GNU General Public License as published by 51 | % the Free Software Foundation, either version 3 of the License, or 52 | % any later version. 53 | % 54 | % This program is distributed in the hope that it will be useful, 55 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 56 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 57 | % GNU General Public License for more details. 58 | % 59 | % You should have received a copy of the GNU General Public License 60 | % along with this program. If not, see . 61 | % 62 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 63 | % or contact: willem.himpe {@} kuleuven.be 64 | 65 | 66 | %local rename link properties 67 | strN = links.fromNode; 68 | endN = links.toNode; 69 | x=nodes.xco; 70 | y=nodes.yco; 71 | 72 | %open figure 73 | if isempty(fig_num) 74 | handle_fig = figure; 75 | hold on; 76 | plot(x,y,'.','MarkerEdgeColor',[0 0 0],'MarkerFaceColor',[0 0 0]); 77 | 78 | x_temp = zeros(length(strN)*3,1); 79 | x_temp(1:3:end) = x(strN); 80 | x_temp(2:3:end) = x(endN); 81 | x_temp(3:3:end) = NaN; 82 | 83 | y_temp = zeros(length(strN)*3,1); 84 | y_temp(1:3:end) = y(strN); 85 | y_temp(2:3:end) = y(endN); 86 | y_temp(3:3:end) = NaN; 87 | 88 | plot(x_temp, y_temp,'Color',[0 0 0]); %[0.9 0.7 0] 89 | 90 | %Setup figure 91 | margX=0.1*(max(x)-min(x))+100*eps; 92 | margY=0.1*(max(y)-min(y))+100*eps; 93 | axis([min(x)-margX max(x)+margX min(y)-margY max(y)+margY]); 94 | colorbar('EastOutside'); 95 | 96 | else 97 | handle_fig = figure(fig_num); 98 | hold on; 99 | end 100 | 101 | if isempty(maxLoad) 102 | maxc=max(load); 103 | else 104 | maxc=maxLoad; 105 | end 106 | 107 | % handle_ax=axes; 108 | 109 | %make a rectangular object for each link 110 | upX=x(strN); 111 | downX=x(endN); 112 | upY=y(strN); 113 | downY=y(endN); 114 | 115 | %set scale 116 | if isempty(scale) 117 | scale = 1/maxc*0.25*sqrt((max(x)-min(x))^2+(max(y)-min(y))^2)/length(upX)^(1/2); 118 | end 119 | 120 | vx=downX-upX; 121 | vy=downY-upY; 122 | vl=sqrt(vx.^2+vy.^2); 123 | vx=vx./vl; 124 | vy=vy./vl; 125 | 126 | sc=scale; 127 | sc=eps+scale*load'; 128 | 129 | xrec=[upX';upX'+sc.*vy';downX'+sc.*vy';downX']; 130 | yrec=[upY';upY'-sc.*vx';downY'-sc.*vx';downY']; 131 | 132 | 133 | %set the colours 134 | ctemp=hsv(128); 135 | cmap=colormap(ctemp(50:-1:1,:)); 136 | % cmap=colormap(handle_ax,ctemp(50:-1:1,:)); 137 | 138 | 139 | minc=0;%possible one could also use the minimal positive value of the load %max(0,min(load)); 140 | 141 | crec=cmap(ceil(49*(load'-minc+eps)/(maxc-minc+eps))',:); 142 | caxis([minc maxc]); 143 | 144 | %visualize all loads 145 | handle_rect=patch(xrec,yrec,load'); 146 | set(handle_rect,'FaceColor','flat','FaceVertexCData',crec); 147 | warning off verbose 148 | % colorbar('EastOutside'); 149 | 150 | handle_txt=[]; 151 | if show_labels 152 | cx=[x(strN)+x(endN)]/2; 153 | cy=[y(strN)+y(endN)]/2; 154 | id=[1:length(cx)]'; 155 | bl=(upX >= downX & upY <= downY); 156 | tl=(upX < downX & upY < downY); 157 | br=(upX >= downX & upY > downY); 158 | tr=(upX < downX & upY >= downY); 159 | t1=text(cx(bl),cy(bl),num2str(load(bl)),'Color',[0 0 0],'VerticalAlignment','Bottom','HorizontalAlignment','Left','FontWeight','bold','Clipping','on','hittest','off'); 160 | t2=text(cx(tl),cy(tl),num2str(load(tl)),'Color',[0 0 0],'VerticalAlignment','Top','HorizontalAlignment','Left','FontWeight','bold','Clipping','on','hittest','off'); 161 | t3=text(cx(br),cy(br),num2str(load(br)),'Color',[0 0 0],'VerticalAlignment','Bottom','HorizontalAlignment','Right','FontWeight','bold','Clipping','on','hittest','off'); 162 | t4=text(cx(tr),cy(tr),num2str(load(tr)),'Color',[0 0 0],'VerticalAlignment','Top','HorizontalAlignment','Right','FontWeight','bold','Clipping','on','hittest','off'); 163 | handle_txt=[t1;t2;t3;t4]; 164 | end 165 | 166 | % set(handle_fig, 'Position', [100, 100, 500, 300]); 167 | hold off; 168 | 169 | end -------------------------------------------------------------------------------- /Visualization Tools/plotLoadedNodes.m: -------------------------------------------------------------------------------- 1 | function [handle_fig,handle_sctr,handle_txt] = plotLoadedNodes(nodes,links,load,show_label,fig_num,scale,maxLoad) 2 | %Plots the node loads on a network 3 | % 4 | %SYNTAX 5 | % [handle_fig,handle_scatter,handle_txt] = plotLoadedNodes(nodes,links,load,show_label,fig,scale,maxLoad) 6 | % 7 | %DESCRIPTION 8 | % plots loads on link in the network and returns figure handles of the loaded network 9 | % 10 | %INPUTS 11 | % nodes: list of all the nodes in the network. 12 | % Each entry of the list represents one node. Each node is a structure that 13 | % has at least a node ID and an x and y coordinate of the node 14 | % links: list of all the links in the network 15 | % Each entry of the list represents one link. Each link is a structure that 16 | % has at least a link ID and an upstream and downstream node. 17 | % load: list of the load on each link 18 | % show_text: flag for visualizing labels 19 | % fig: parameter for the figure handle 20 | % maxLoad: scale parameter used for determining the loads 21 | 22 | % This file is part of the matlab package for dynamic traffic assignments 23 | % developed by the KULeuven. 24 | % 25 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 26 | % 27 | % This program is free software: you can redistribute it and/or modify 28 | % it under the terms of the GNU General Public License as published by 29 | % the Free Software Foundation, either version 3 of the License, or 30 | % any later version. 31 | % 32 | % This program is distributed in the hope that it will be useful, 33 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 34 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 35 | % GNU General Public License for more details. 36 | % 37 | % You should have received a copy of the GNU General Public License 38 | % along with this program. If not, see . 39 | % 40 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 41 | % or contact: willem.himpe {@} kuleuven.be 42 | 43 | 44 | %local rename link properties 45 | x=nodes.xco; 46 | y=nodes.yco; 47 | strN = links.fromNode; 48 | endN = links.toNode; 49 | 50 | totN = length(x); 51 | 52 | %open figure 53 | if isempty(fig_num) 54 | handle_fig = figure; 55 | hold on; 56 | plot(x,y,'.','MarkerEdgeColor',[0 0 0],'MarkerFaceColor',[0 0 0]); 57 | 58 | x_temp = zeros(length(strN)*3,1); 59 | x_temp(1:3:end) = x(strN); 60 | x_temp(2:3:end) = x(endN); 61 | x_temp(3:3:end) = NaN; 62 | 63 | y_temp = zeros(length(strN)*3,1); 64 | y_temp(1:3:end) = y(strN); 65 | y_temp(2:3:end) = y(endN); 66 | y_temp(3:3:end) = NaN; 67 | 68 | plot(x_temp, y_temp,'Color',[0 0 0]); %[0.9 0.7 0] 69 | else 70 | handle_fig = figure(fig_num); 71 | hold on; 72 | end 73 | % handle_ax=axes; 74 | 75 | %set scale 76 | if isempty(scale) 77 | scale = 1/max(load)*250*sqrt((max(x)-min(x))^2+(max(y)-min(y))^2)/length(strN)^1.5; 78 | end 79 | 80 | %make a point object for each link 81 | sc=scale*load'; 82 | 83 | 84 | %set the colours 85 | ctemp=hsv(128); 86 | cmap=colormap(ctemp(128:-1:78,:)); 87 | % cmap=colormap(handle_ax,ctemp(128:-1:78,:)); 88 | 89 | minc=0;%possible one could also use the minimal positive value of the load %max(0,min(load)); 90 | 91 | if isempty(maxLoad) 92 | maxc=max(load); 93 | else 94 | maxc=maxLoad; 95 | end 96 | 97 | crec=cmap(ceil(49*(load'-minc+10*eps)/(maxc-minc+10*eps))',:); 98 | 99 | %plot all loads 100 | handle_sctr=scatter(x,y,eps+sc,crec,'filled'); 101 | 102 | caxis([minc maxc]); 103 | colorbar('EastOutside'); 104 | % colorbar(handle_ax,'WestOutside'); 105 | 106 | handle_txt=[]; 107 | if show_label 108 | 109 | a = load>=0; 110 | handle_txt=text(x(a),y(a),num2str(load(a)),'Color',[0 0 0],'VerticalAlignment','Bottom','HorizontalAlignment','Left','FontWeight','bold'); 111 | end 112 | 113 | %Setup figure 114 | margX=0.1*(max(x)-min(x))+100*eps; 115 | margY=0.1*(max(y)-min(y))+100*eps; 116 | axis([min(x)-margX max(x)+margX min(y)-margY max(y)+margY]) 117 | 118 | 119 | % set(handle_fig, 'Position', [100, 100, 500, 300]); 120 | hold off; 121 | 122 | end -------------------------------------------------------------------------------- /Visualization Tools/plotNetwork.m: -------------------------------------------------------------------------------- 1 | function [handle_fig,handle_txt] = plotNetwork(nodes,links,show_label,fig_num) 2 | %Plot a network 3 | % 4 | % 5 | %SYNTAX 6 | % [handle] = PLOTNETWORK(nodes,links,scale) 7 | % 8 | %DESCRIPTION 9 | % returns the handle to a figure of the network 10 | % 11 | %INPUTS 12 | % nodes: list of all the nodes in the network. 13 | % Each entry of the list represents one node. Each node is a structure that 14 | % has at least a node ID and an x and y coordinate of the node 15 | % links: list of all the links in the network 16 | % Each entry of the list represents one link. Each link is a structure that 17 | % has at least a link ID and an upstream and downstream node. 18 | % scale: parameter used to control the size of lines, text, nodes,... 19 | 20 | % This file is part of the matlab package for dynamic traffic assignments 21 | % developed by the KULeuven. 22 | % 23 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 24 | % 25 | % This program is free software: you can redistribute it and/or modify 26 | % it under the terms of the GNU General Public License as published by 27 | % the Free Software Foundation, either version 3 of the License, or 28 | % any later version. 29 | % 30 | % This program is distributed in the hope that it will be useful, 31 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 32 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 33 | % GNU General Public License for more details. 34 | % 35 | % You should have received a copy of the GNU General Public License 36 | % along with this program. If not, see . 37 | % 38 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 39 | % or contact: willem.himpe {@} kuleuven.be 40 | 41 | %local rename link properties 42 | strN = links.fromNode; 43 | endN = links.toNode; 44 | x=nodes.xco; 45 | y=nodes.yco; 46 | 47 | %open figure 48 | if isempty(fig_num) 49 | handle_fig = figure; 50 | else 51 | handle_fig = figure(fig_num); 52 | end 53 | hold on; 54 | 55 | %visulize the network 56 | plot(x,y,'.','MarkerEdgeColor',[0 0 0],'MarkerFaceColor',[0 0 0]); 57 | 58 | x_temp = zeros(length(strN)*3,1); 59 | x_temp(1:3:end) = x(strN); 60 | x_temp(2:3:end) = x(endN); 61 | x_temp(3:3:end) = NaN; 62 | 63 | y_temp = zeros(length(strN)*3,1); 64 | y_temp(1:3:end) = y(strN); 65 | y_temp(2:3:end) = y(endN); 66 | y_temp(3:3:end) = NaN; 67 | 68 | plot(x_temp, y_temp,'Color',[0 0 0]); %[0.9 0.7 0] 69 | 70 | handle_txt=[]; 71 | if show_label 72 | a=[1:length(x)]'; 73 | id=[1:length(x)]'; 74 | t0=text(x(a),y(a),num2str(id(a)),'Color',[0 0 0],'VerticalAlignment','Bottom','HorizontalAlignment','Left','FontWeight','bold','Clipping','on','hittest','off'); 75 | 76 | upX=x(strN); 77 | downX=x(endN); 78 | upY=y(strN); 79 | downY=y(endN); 80 | 81 | cx=[x(strN)+x(endN)]/2; 82 | cy=[y(strN)+y(endN)]/2; 83 | id=[1:length(cx)]'; 84 | bl=(upX >= downX & upY <= downY); 85 | tl=(upX < downX & upY < downY); 86 | br=(upX >= downX & upY > downY); 87 | tr=(upX < downX & upY >= downY); 88 | t1=text(cx(bl),cy(bl),num2str(id(bl)),'Color',[1 0 0],'VerticalAlignment','Bottom','HorizontalAlignment','Left','FontWeight','bold','Clipping','on','hittest','off'); 89 | t2=text(cx(tl),cy(tl),num2str(id(tl)),'Color',[0 1 0],'VerticalAlignment','Top','HorizontalAlignment','Left','FontWeight','bold','Clipping','on','hittest','off'); 90 | t3=text(cx(br),cy(br),num2str(id(br)),'Color',[0 0 1],'VerticalAlignment','Bottom','HorizontalAlignment','Right','FontWeight','bold','Clipping','on','hittest','off'); 91 | t4=text(cx(tr),cy(tr),num2str(id(tr)),'Color',[0 1 1],'VerticalAlignment','Top','HorizontalAlignment','Right','FontWeight','bold','Clipping','on','hittest','off'); 92 | handle_txt=[t0;t1;t2;t3;t4]; 93 | end 94 | 95 | %Setup figure 96 | margX=0.1*(max(x)-min(x))+100*eps; 97 | margY=0.1*(max(y)-min(y))+100*eps; 98 | axis([min(x)-margX max(x)+margX min(y)-margY max(y)+margY]) 99 | 100 | title('Network Map') 101 | xlabel('X coordinate') 102 | ylabel('Y coordinate') 103 | 104 | % set(handle_fig, 'Position', [100, 100, 400, 300]); 105 | hold off; 106 | 107 | end 108 | 109 | -------------------------------------------------------------------------------- /Visualization Tools/plotTT.m: -------------------------------------------------------------------------------- 1 | function [handle_fig,instTT,forwTT,backTT]=plotTT(links,path,TT,dt,totT) 2 | %Plots a travel time plot 3 | % 4 | % 5 | % 6 | %SYNTAX 7 | % [handle_fig]=plotTT(links,path,TT,dt,totT) 8 | % 9 | %DESCRIPTION 10 | % simulate the loads on the network 11 | % 12 | %INPUTS 13 | % startNode: start node 14 | % endNode: end node 15 | % load: the load 16 | % nodes: list of all the nodes in the network. 17 | % Each entry of the list represents one node. Each node is a structure that 18 | % has at least a node ID and an x and y coordinate of the node 19 | % links: list of all the links in the network 20 | % Each entry of the list represents one link. Each link is a structure that 21 | % has at least a link ID and an upstream and downstream node. 22 | % timesteps: time steps of the simulation 23 | % ti: title 24 | 25 | % This file is part of the matlab package for dynamic traffic assignments 26 | % developed by the KULeuven. 27 | % 28 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 29 | % 30 | % This program is free software: you can redistribute it and/or modify 31 | % it under the terms of the GNU General Public License as published by 32 | % the Free Software Foundation, either version 3 of the License, or 33 | % any later version. 34 | % 35 | % This program is distributed in the hope that it will be useful, 36 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 37 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 38 | % GNU General Public License for more details. 39 | % 40 | % You should have received a copy of the GNU General Public License 41 | % along with this program. If not, see . 42 | % 43 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 44 | % or contact: willem.himpe {@} kuleuven.be 45 | 46 | 47 | handle_fig = figure('Units','pixels'); 48 | xlabel('Time [hr]','FontSize',12); 49 | ylabel('Travel Time [hr]','FontSize',12); 50 | title('Travel time graph','FontSize',14,'fontweight','b'); 51 | hold on; 52 | 53 | timeSteps=0:dt:totT*dt; 54 | instTT=sum(TT(path,:),1); 55 | plot(timeSteps,instTT,'g') 56 | 57 | forwTT=timeSteps; 58 | backTT=timeSteps; 59 | for l=path 60 | forwTT=forwTT+interp1(timeSteps,TT(l,:),forwTT); 61 | end 62 | forwTT=forwTT-timeSteps; 63 | forwTT(isnan(forwTT))=min(instTT); 64 | plot(timeSteps,forwTT,'b'); 65 | 66 | for l=path(end:-1:1) 67 | [x,un]=unique(timeSteps+TT(l,:),'last'); 68 | arTT = interp1(x,TT(l,un),timeSteps); 69 | arTT(isnan(arTT)) = TT(l,1); 70 | backTT=backTT-interp1(timeSteps,arTT,backTT); 71 | end 72 | backTT=timeSteps-backTT; 73 | backTT(isnan(backTT))=min(instTT); 74 | plot(timeSteps,backTT,'r'); 75 | 76 | plot(timeSteps,sum(links.length(path)./links.freeSpeed(path))*ones(1,totT+1),'--k') 77 | grid on 78 | legend('instantaneous','at departure','at arrival (experienced)','free flow travel time') 79 | axis([timeSteps(1),timeSteps(end),max(0,min(sum(TT(path,:),1)-0.025)),max(sum(TT(path,:),1))+0.025]); 80 | return 81 | -------------------------------------------------------------------------------- /Visualization Tools/plotXT.m: -------------------------------------------------------------------------------- 1 | function [h1]=plotXT(links,path,load,dt,totT) 2 | %Plots a space time plot 3 | % 4 | %SYNTAX 5 | % [handle_fig]=plot_speed_xt(startNode,endNode,load,links,timeSteps,ti) 6 | % 7 | %DESCRIPTION 8 | % simulate the loads on the network 9 | % 10 | %INPUTS 11 | % startNode: start node 12 | % endNode: end node 13 | % load: the load 14 | % nodes: list of all the nodes in the network. 15 | % Each entry of the list represents one node. Each node is a structure that 16 | % has at least a node ID and an x and y coordinate of the node 17 | % links: list of all the links in the network 18 | % Each entry of the list represents one link. Each link is a structure that 19 | % has at least a link ID and an upstream and downstream node. 20 | % timesteps: time steps of the simulation 21 | % ti: title 22 | 23 | % This file is part of the matlab package for dynamic traffic assignments 24 | % developed by the KULeuven. 25 | % 26 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 27 | % 28 | % This program is free software: you can redistribute it and/or modify 29 | % it under the terms of the GNU General Public License as published by 30 | % the Free Software Foundation, either version 3 of the License, or 31 | % any later version. 32 | % 33 | % This program is distributed in the hope that it will be useful, 34 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 35 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 36 | % GNU General Public License for more details. 37 | % 38 | % You should have received a copy of the GNU General Public License 39 | % along with this program. If not, see . 40 | % 41 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 42 | % or contact: willem.himpe {@} kuleuven.be 43 | 44 | %local rename link properties 45 | len=links.length; 46 | 47 | coordinatesEnd=cumsum([0;len(path)]'); 48 | x=0:length(coordinatesEnd)-1; 49 | x1=0.5:1:length(coordinatesEnd)-1; 50 | coordinates = interp1(x,coordinatesEnd,x1)'; 51 | plotLoad=load(path,:); 52 | 53 | handle_fig = figure('Units','pixels'); 54 | hold on 55 | surf(0:dt:totT*dt,coordinates,plotLoad,'facecolor','texturemap','LineStyle','none') 56 | view(2) 57 | axis('tight') 58 | xlabel('Time [hr]','FontSize',12) 59 | ylabel('Distance [km]','FontSize',12) 60 | caxis([0 max(max(plotLoad))]); 61 | colorbar('EastOutside'); 62 | my_jet = jet(512); 63 | my_jet(1:250,:)=[]; 64 | colormap(my_jet); 65 | grid on 66 | title('Space - Time graph','FontSize',14,'fontweight','b') 67 | % set(handle_fig, 'Position', [100, 100, 400, 300]); 68 | -------------------------------------------------------------------------------- /tutorial1.m: -------------------------------------------------------------------------------- 1 | %% Tutorial 1: Visualizing network data 2 | 3 | %% Disclaimer 4 | % This file is part of the matlab package for dynamic traffic assignments 5 | % developed by the KULeuven. 6 | % 7 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 8 | % 9 | % This program is free software: you can redistribute it and/or modify 10 | % it under the terms of the GNU General Public License as published by 11 | % the Free Software Foundation, either version 3 of the License, or 12 | % any later version. 13 | % 14 | % This program is distributed in the hope that it will be useful, 15 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | % GNU General Public License for more details. 18 | % 19 | % You should have received a copy of the GNU General Public License 20 | % along with this program. If not, see . 21 | % 22 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 23 | % or contact: willem.himpe {@} kuleuven.be 24 | 25 | %% Introduction 26 | % This tutorial illustrates some of tools used to visualize data on a 27 | % network. All of the results are preloaded and no traffic models are run 28 | % in this tutorial. To run the full script simply hit run in the editor 29 | % window or press F5. It is also possible to rerun a single section of the 30 | % script or inspect data and functions (right mouse click or by 31 | % pressing CTRL+D). For some sections additional information is printed to 32 | % the command window during the run. 33 | % 34 | 35 | %add these folders to the search path 36 | addpath('Visualization Tools','Network Data') 37 | %clear the work space 38 | clear 39 | %clear the command window 40 | clc 41 | %close all windows 42 | close all 43 | 44 | display('<<>>') 45 | 46 | %% Loading the data 47 | % Links and nodes are in table format. In older versions of matlab this 48 | % could potentially lead to problems (there are alternative network 49 | % codings in the Network Data map for older versions) 50 | % 51 | 52 | %network and demand data 53 | load net1.mat 54 | 55 | %results (travel times, flows and densities) for a simulation with: 56 | %dt: time interval 57 | %totT: total number of intervals 58 | load result_net1.mat 59 | 60 | %% Basic plotting functions 61 | % These functions are use to visualize the network graph and visualize 62 | % loads on the links (arcs) or nodes (edges). To illustrate link loads the 63 | % capacity of each link is plotted and to illustrate node loads the number 64 | % of outgoing links of each node is plotted. 65 | % 66 | 67 | %plot the network 68 | plotNetwork(nodes,links,true,[]); 69 | 70 | %plot loads (capacities) on the links 71 | plotLoadedLinks(nodes,links,links.capacity,true,[],[],[]); 72 | 73 | %plot loads (number of outgoing links) on the nodes 74 | nbOut = hist(links.fromNode,size(nodes.id,1)); 75 | plotLoadedNodes(nodes,links,nbOut',true,[],[],[]); 76 | 77 | %% Plotting results 78 | % These functions are used to visualize typical results of traffic 79 | % simulation. The flows along a specific path in the network are 80 | % depicted in space-time (or XT) diagrams. Within day travel times along 81 | % the same path are visualized for different integrations of the link 82 | % travel times. The variation of densities is animated in the network by 83 | % considering only every 10th simulation interval. 84 | % 85 | 86 | %Visualizing results using XT diagrams 87 | plotXT(links,1:27,flows,dt,totT-1); 88 | 89 | %Visualize the travel time along a route 90 | plotTT(links,1:27,tt,dt,totT); 91 | 92 | %Make an animation of the result 93 | fRate = 20; %set frame rate 94 | % fRate = inf; %allows the for manual control using space bar 95 | animateSimulation(nodes,links,densities(:,1:10:end),dt*[0:10:totT],fRate); 96 | 97 | %% Closing notes 98 | % 99 | % * The title of the XT-graphs can be changed using the same notation as 100 | % for regular figures. 101 | % * The animation plot can be manually controlled by setting the fRate to 102 | % inf and pressing the space bar to progress. 103 | % -------------------------------------------------------------------------------- /tutorial10.m: -------------------------------------------------------------------------------- 1 | %% Tutorial 10: Introducing deterministic equilibrium routing 2 | 3 | %% Disclaimer 4 | % This file is part of the matlab package for dynamic traffic assignments 5 | % developed by the KULeuven. 6 | % 7 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 8 | % 9 | % This program is free software: you can redistribute it and/or modify 10 | % it under the terms of the GNU General Public License as published by 11 | % the Free Software Foundation, either version 3 of the License, or 12 | % any later version. 13 | % 14 | % This program is distributed in the hope that it will be useful, 15 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | % GNU General Public License for more details. 18 | % 19 | % You should have received a copy of the GNU General Public License 20 | % along with this program. If not, see . 21 | % 22 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 23 | % or contact: willem.himpe {@} kuleuven.be 24 | 25 | %% Introduction 26 | % In this tutorial a basic algorithm is implemented to find deterministic 27 | % user equilibrium in a dynamic traffic assignment. The solution is found 28 | % by means of the method of successive averages (MSA). 29 | % 30 | 31 | %add these folders to the search path 32 | addpath('Dynamic Traffic Assignment','Visualization Tools','Network Data') 33 | javaclasspath('Dynamic Traffic Assignment'); 34 | %clear the work space 35 | clear 36 | %clear the command window 37 | clc 38 | %close all windows 39 | close all 40 | 41 | display('<<>>') 42 | 43 | %% Loading the data 44 | % The network represents a simple two-route network with a bottleneck on 45 | % the shortest alternative. 46 | % 47 | 48 | % Network and demand data 49 | load net6.mat 50 | 51 | % Plot the network 52 | plotNetwork(nodes,links,true,[]); 53 | 54 | %% Setup the simulation 55 | % Before the simulation can be run the time interval has to be set and the 56 | % total number of time steps has to be defined. 57 | % 58 | 59 | %setup the time interval and total number of time steps 60 | dt = 0.01; 61 | totT = round(1.5/dt); 62 | 63 | %build the full ODmatrix 64 | [ODmatrix,origins,destinations] = buildODmatrix(ODmatrices,timeSeries,dt,totT); 65 | 66 | %% Setup the dynamic equilibrium simulation 67 | % The routing behavior in the dynamic user equilibrium is aggregated over 68 | % larger time intervals to speed up computation. It is believed that the 69 | % route choice time intervals varies with a much lower frequency in reality 70 | % than the typical interval of a simulation. As travel time varies 71 | % continuously over the route choice interval, not all vehicles within the 72 | % same route choice interval experience the same travel time. Hence, the 73 | % modeler should select a travel time that is representative for the entire 74 | % route choice interval, e.g. that of the first/middle/last vehicle to 75 | % depart within that interval. 76 | % 77 | 78 | %time interval for the route choice 79 | rc_dt = 10*dt; 80 | max_it = 100; 81 | 82 | %Initialize the travel time aggragation for route choice behaviour 83 | rc_agg = 'last'; 84 | %last: last vehicle of the route choice interval (standard) 85 | %middle: middle vehicle of the route choice interval 86 | %first: first vehicle of the route choice interval 87 | 88 | %run DTA with deterministic route choice and MSA averaging 89 | tic 90 | [cvn_up,cvn_down,TF] = DTA_MSA(nodes,links,origins,destinations,ODmatrix,dt,totT,rc_dt,max_it,rc_agg); 91 | toc 92 | 93 | %% Transform CVN values to travel times 94 | % The upstream and dowsntream CVN functions of the link transmission model 95 | % are transformed into travel times for every link in the network. The 96 | % travel times are compared for the main route (from split to merge) and 97 | % the alternative route. 98 | % 99 | 100 | %calculate the simulated travel times 101 | [simTT] = cvn2tt(sum(cvn_up,3),sum(cvn_down,3),dt,totT,links); 102 | 103 | %visualize the travel time along the main route (from split to merge) 104 | [~,~,~,tt_m]=plotTT(links,2:5,simTT,dt,totT); 105 | title('Travel time graph main route','FontSize',14,'fontweight','b') 106 | %visualize the travel time along the alternative route (from split to merge) 107 | [~,~,~,tt_a]=plotTT(links,7:10,simTT,dt,totT); 108 | title('Travel time alternative route','FontSize',14,'fontweight','b') 109 | 110 | %compare both travel times 111 | figure; 112 | plot(dt*[0:totT],tt_m,'b',dt*[0:totT],tt_a,'r'); 113 | grid on 114 | legend('Main route','Alternative route') 115 | xlabel('Time [hr]','FontSize',12); 116 | ylabel('Travel Time [hr]','FontSize',12); 117 | title('Travel time graph (from split to merge)','FontSize',14,'fontweight','b'); 118 | 119 | 120 | %% Visualize the split rates at the diverge 121 | % The following lines of code visualize the splitting rates at the diverge. 122 | % 123 | 124 | sp=[TF{2,:,1}]; 125 | figure;plot(dt*[0:totT-1],sp(1:2:end),'r',dt*[0:totT-1],sp(2:2:end),'b'); 126 | grid on; 127 | legend('fraction using the main road', 'fraction using the alternative'); 128 | 129 | %% Closing notes 130 | % 131 | % * ITEM1 132 | % * ITEM2 133 | % -------------------------------------------------------------------------------- /tutorial11.m: -------------------------------------------------------------------------------- 1 | %% Tutorial 11: Advanced deterministic equilibrium routing 2 | 3 | %% Disclaimer 4 | % This file is part of the matlab package for dynamic traffic assignments 5 | % developed by the KULeuven. 6 | % 7 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 8 | % 9 | % This program is free software: you can redistribute it and/or modify 10 | % it under the terms of the GNU General Public License as published by 11 | % the Free Software Foundation, either version 3 of the License, or 12 | % any later version. 13 | % 14 | % This program is distributed in the hope that it will be useful, 15 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | % GNU General Public License for more details. 18 | % 19 | % You should have received a copy of the GNU General Public License 20 | % along with this program. If not, see . 21 | % 22 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 23 | % or contact: willem.himpe {@} kuleuven.be 24 | 25 | %% Introduction 26 | % In this tutorial an advanced algorithm is implemented to find 27 | % deterministic user equilibrium in a dynamic traffic assignment. The 28 | % solution is found by means of reduced gradient projection. 29 | % 30 | 31 | %add these folders to the search path 32 | addpath('Dynamic Traffic Assignment','Visualization Tools','Network Data') 33 | javaclasspath('Dynamic Traffic Assignment'); 34 | %clear the work space 35 | clear 36 | %clear the command window 37 | clc 38 | %close all windows 39 | close all 40 | 41 | display('<<>>') 42 | 43 | %% Loading the data 44 | % The network represents a simple two-route network with a bottleneck on 45 | % the shortest alternative. 46 | % 47 | 48 | % Network and demand data 49 | load net6.mat 50 | 51 | % Plot the network 52 | plotNetwork(nodes,links,true,[]); 53 | 54 | %% Setup the simulation 55 | % Before the simulation can be run the time interval has to be set and the 56 | % total number of time steps has to be defined. 57 | % 58 | 59 | %setup the time interval and total number of time steps 60 | dt = 0.01; 61 | totT = round(1.5/dt); 62 | 63 | %build the full ODmatrix 64 | [ODmatrix,origins,destinations] = buildODmatrix(ODmatrices,timeSeries,dt,totT); 65 | 66 | %% Setup the dynamic equilibrium simulation 67 | % The routing behavior in the dynamic user equilibrium is aggregated over 68 | % larger time intervals to speed up computation. It is believed that the 69 | % route choice time intervals varies with a much lower frequency in reality 70 | % than the typical interval of a simulation. The approximate gradient 71 | % projection method projects the travel time difference over the two 72 | % alternatives into the splitting rate space. The projection is scaled by a 73 | % parameter that can be used to speed up the process (larger values). 74 | % However for too large values the algorithme becomes unstable and unable to 75 | % find a solution. 76 | % 77 | 78 | %time interval for the route choice 79 | rc_dt = dt; 80 | %maximum number of iterations 81 | max_it = 100; 82 | %scaling parameter of the projection 83 | alpha = 10; 84 | 85 | %run DTA with deterministic route choice by simultaneous projected 86 | %gradient updating of the turning fractions 87 | tic 88 | [cvn_up,cvn_down,TF] = DTA_ProjGrad(nodes,links,origins,destinations,ODmatrix,dt,totT,rc_dt,max_it,alpha); 89 | toc 90 | 91 | %% Transform CVN values to travel times 92 | % The upstream and dowsntream CVN functions of the link transmission model 93 | % are transformed into travel times for every link in the network. The 94 | % travel times are compared for the main route (from split to merge) and 95 | % the alternative route. 96 | % 97 | 98 | %calculate the simulated travel times 99 | [simTT] = cvn2tt(sum(cvn_up,3),sum(cvn_down,3),dt,totT,links); 100 | 101 | %visualize the travel time along the main route (from split to merge) 102 | [~,~,~,tt_m]=plotTT(links,2:5,simTT,dt,totT); 103 | title('Travel time graph main route','FontSize',14,'fontweight','b') 104 | %visualize the travel time along the alternative route (from split to merge) 105 | [~,~,~,tt_a]=plotTT(links,7:10,simTT,dt,totT); 106 | title('Travel time graph alternative route','FontSize',14,'fontweight','b') 107 | 108 | %compare both travel times 109 | figure; 110 | plot(dt*[0:totT],tt_m,'b',dt*[0:totT],tt_a,'r'); 111 | grid on 112 | legend('Main route','Alternative route') 113 | xlabel('Time [hr]','FontSize',12); 114 | ylabel('Travel Time [hr]','FontSize',12); 115 | title('Travel time graph (from split to merge)','FontSize',14,'fontweight','b'); 116 | 117 | 118 | %% Visualize the split rates at the diverge 119 | % The following lines of code visualize the splitting rates at the diverge. 120 | % 121 | 122 | sp=[TF{2,:,1}]; 123 | figure;plot(dt*[0:totT-1],sp(1:2:end),'r',dt*[0:totT-1],sp(2:2:end),'b'); 124 | grid on; 125 | legend('fraction using the main road', 'fraction using the alternative'); 126 | 127 | %% Closing notes 128 | % 129 | % * The stability of the algorithm is a function of the scaling parameter, 130 | % the time interval of the route choice and the simulation. For larger 131 | % route choice intervals a smaller value of the scaling of the route choice 132 | % is typically used. 133 | % 134 | -------------------------------------------------------------------------------- /tutorial12.m: -------------------------------------------------------------------------------- 1 | %% Tutorial 12: Deterministic instantaneous routing 2 | 3 | %% Disclaimer 4 | % This file is part of the matlab package for dynamic traffic assignments 5 | % developed by the KULeuven. 6 | % 7 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 8 | % 9 | % This program is free software: you can redistribute it and/or modify 10 | % it under the terms of the GNU General Public License as published by 11 | % the Free Software Foundation, either version 3 of the License, or 12 | % any later version. 13 | % 14 | % This program is distributed in the hope that it will be useful, 15 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | % GNU General Public License for more details. 18 | % 19 | % You should have received a copy of the GNU General Public License 20 | % along with this program. If not, see . 21 | % 22 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 23 | % or contact: willem.himpe {@} kuleuven.be 24 | 25 | %% Introduction 26 | % In this tutorial an algorithm is implemented to find a deterministic 27 | % instantaneous dynamic traffic assignment. The solution does not require 28 | % iterations and is much faster compared to an equilibrium computation. But 29 | % the routing behaviour is less responsive and discrete (only zero or one 30 | % is allowed as a splitting rates). 31 | % 32 | 33 | %add these folders to the search path 34 | addpath('Dynamic Traffic Assignment','Visualization Tools','Network Data') 35 | javaclasspath('Dynamic Traffic Assignment'); 36 | %clear the work space 37 | clear 38 | %clear the command window 39 | clc 40 | %close all figures 41 | close all 42 | 43 | display('<<>>') 44 | 45 | %% Loading the data 46 | % The network represents a simple two-route network with a bottleneck on 47 | % the shortest alternative. 48 | % 49 | 50 | % Network and demand data 51 | load net6.mat 52 | 53 | % Plot the network 54 | plotNetwork(nodes,links,true,[]); 55 | 56 | %% Setup the simulation 57 | % Before the simulation can be run the time interval has to be set and the 58 | % total number of time steps has to be defined. 59 | % 60 | 61 | %setup the time interval and total number of time steps 62 | dt = 0.01; 63 | totT = round(1.5/dt); 64 | 65 | %build the full ODmatrix 66 | [ODmatrix,origins,destinations] = buildODmatrix(ODmatrices,timeSeries,dt,totT); 67 | 68 | %% Setup the instantaneous traffic assignment 69 | % The routing behavior in the instantaneous assignment is aggregated over 70 | % larger time intervals to speed up computation. It is believed that the 71 | % route choice time intervals varies with a much lower frequency in reality 72 | % than the typical interval of a simulation. 73 | 74 | %time interval for the route choice 75 | rc_dt = 6/60; 76 | 77 | %run DTA with instantaneous deterministic route choice 78 | tic 79 | [cvn_up,cvn_down,TF] = DTA_inst(nodes,links,origins,destinations,ODmatrix,dt,totT,dt); 80 | toc 81 | 82 | 83 | %% Transform CVN values to travel times 84 | % The upstream and dowsntream CVN functions of the link transmission model 85 | % are transformed into travel times for every link in the network. The 86 | % travel times are compared for the main route (from split to merge) and 87 | % the alternative route. 88 | % 89 | 90 | %calculate the simulated travel times 91 | [simTT] = cvn2tt(sum(cvn_up,3),sum(cvn_down,3),dt,totT,links); 92 | 93 | %visualize the travel time along the main route (from split to merge) 94 | [~,~,~,tt_m]=plotTT(links,2:5,simTT,dt,totT); 95 | title('Travel time graph main route','FontSize',14,'fontweight','b') 96 | %visualize the travel time along the alternative route (from split to merge) 97 | [~,~,~,tt_a]=plotTT(links,7:10,simTT,dt,totT); 98 | title('Travel time graph alternative route','FontSize',14,'fontweight','b') 99 | 100 | %compare both travel times 101 | figure; 102 | plot(dt*[0:totT],tt_m,'b',dt*[0:totT],tt_a,'r'); 103 | grid on 104 | legend('Main route','Alternative route') 105 | xlabel('Time [hr]','FontSize',12); 106 | ylabel('Travel Time [hr]','FontSize',12); 107 | title('Travel time graph','FontSize',14,'fontweight','b'); 108 | 109 | 110 | %% Visualize the split rates at the diverge 111 | % The following lines of code visualize the splitting rates at the diverge. 112 | % 113 | 114 | sp=[TF{2,:,1}]; 115 | figure;plot(dt*[0:totT-1],sp(1:2:end),'r',dt*[0:totT-1],sp(2:2:end),'b'); 116 | grid on; 117 | legend('fraction using the main road', 'fraction using the alternative'); 118 | 119 | %% Closing notes 120 | % 121 | % * ITEM1 122 | % * ITEM2 123 | % -------------------------------------------------------------------------------- /tutorial13.m: -------------------------------------------------------------------------------- 1 | %% Tutorial 13: Introducing implicit stochastic equilibrium routing 2 | 3 | %% Disclaimer 4 | % This file is part of the matlab package for dynamic traffic assignments 5 | % developed by the KULeuven. 6 | % 7 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 8 | % 9 | % This program is free software: you can redistribute it and/or modify 10 | % it under the terms of the GNU General Public License as published by 11 | % the Free Software Foundation, either version 3 of the License, or 12 | % any later version. 13 | % 14 | % This program is distributed in the hope that it will be useful, 15 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | % GNU General Public License for more details. 18 | % 19 | % You should have received a copy of the GNU General Public License 20 | % along with this program. If not, see . 21 | % 22 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 23 | % or contact: willem.himpe {@} kuleuven.be 24 | 25 | %% Introduction 26 | % In this tutorial a basic algorithm is implemented to find a stochastic 27 | % user equilibrium in a dynamic traffic assignment. The algorithm is based 28 | % on an implicit routing procedure. This requires formulating a topological 29 | % order of nodes to describe all available route alternatives without 30 | % circles. The topological order based on the shortest travel times has a 31 | % large influence on the convergence and solution of the algorithm. 32 | % 33 | 34 | %add these folders to the search path 35 | addpath('Dynamic Traffic Assignment','Visualization Tools','Network Data') 36 | javaclasspath('Dynamic Traffic Assignment'); 37 | %clear the work space 38 | clear 39 | %clear the command window 40 | clc 41 | %close all windows 42 | close all 43 | 44 | display('<<>>') 45 | 46 | %% Loading the data 47 | % The network represents a simple two-route network with a bottleneck on 48 | % the shortest alternative. The longer alternative has only a single link 49 | % which is longer. This link is the first link of the alternative. 50 | % 51 | 52 | % Network and demand data 53 | load net6.mat 54 | 55 | % Plot the network 56 | plotNetwork(nodes,links,true,[]); 57 | 58 | % Displays the link properties of the alternative 59 | links(7:10,:) 60 | 61 | %% Setup the simulation 62 | % Before the simulation can be run the time interval has to be set and the 63 | % total number of time steps has to be defined. 64 | % 65 | 66 | %setup the time interval and total number of time steps 67 | dt = 0.01; 68 | totT = round(1.5/dt); 69 | 70 | %build the full ODmatrix 71 | [ODmatrix,origins,destinations] = buildODmatrix(ODmatrices,timeSeries,dt,totT); 72 | 73 | %% Setup the dynamic equilibrium simulation 74 | % The routing behavior in the stochastic routing behaviour is aggregated 75 | % over larger time intervals to speed up computation. It is believed that 76 | % the route choice time intervals varies with a much lower frequency in 77 | % reality than the typical interval of a simulation. The routing is 78 | % described by a logit model with a scaling parameter to capture the 79 | % variance of the error term. 80 | % 81 | 82 | %time interval for the route choice 83 | rc_dt = dt; 84 | %maximum number of iterations 85 | max_it = 100; 86 | %scaling of the updates over iterations (<1) 87 | alpha = 0.25; 88 | %scaling of the utility (logit) 89 | theta = 0.010; 90 | 91 | %run DTA with deterministic route choice and MSA averaging 92 | tic 93 | [cvn_up,cvn_down,TF] = DTA_STOCH(nodes,links,origins,destinations,ODmatrix,dt,totT,rc_dt,max_it,alpha,theta); 94 | toc 95 | 96 | %% Transform CVN values to travel times 97 | % The upstream and dowsntream CVN functions of the link transmission model 98 | % are transformed into travel times for every link in the network. The 99 | % travel times are compared for the main route (from split to merge) and 100 | % the alternative route. 101 | % 102 | 103 | %calculate the simulated travel times 104 | [simTT] = cvn2tt(sum(cvn_up,3),sum(cvn_down,3),dt,totT,links); 105 | 106 | %visualize the travel time along the main route (from split to merge) 107 | [~,~,~,tt_m]=plotTT(links,2:5,simTT,dt,totT); 108 | title('Travel time graph main route','FontSize',14,'fontweight','b') 109 | %visualize the travel time along the alternative route (from split to merge) 110 | [~,~,~,tt_a]=plotTT(links,7:10,simTT,dt,totT); 111 | title('Travel time graph alternative route','FontSize',14,'fontweight','b') 112 | 113 | %compare both travel times 114 | figure; 115 | plot(dt*[0:totT],tt_m,'b',dt*[0:totT],tt_a,'r'); 116 | grid on 117 | legend('Main route','Alternative route') 118 | xlabel('Time [hr]','FontSize',12); 119 | ylabel('Travel Time [hr]','FontSize',12); 120 | title('Travel time graph','FontSize',14,'fontweight','b'); 121 | 122 | 123 | %% Visualize the split rates at the diverge 124 | % The following lines of code visualize the splitting rates at the diverge. 125 | % 126 | 127 | sp=[TF{2,:,1}]; 128 | figure;plot(dt*[0:totT-1],sp(1:2:end),'r',dt*[0:totT-1],sp(2:2:end),'b'); 129 | grid on; 130 | legend('fraction using the main road', 'fraction using the alternative'); 131 | 132 | 133 | %% Loading the data 134 | % The network represents a simple two-route network with a bottleneck on 135 | % the shortest alternative. The longer alternative has only a single link 136 | % which is longer. This link is the last link of the alternative. 137 | % 138 | 139 | % Network and demand data 140 | load net7.mat 141 | 142 | % Plot the network 143 | plotNetwork(nodes,links,true,[]); 144 | 145 | % Displays the link properties of the alternative 146 | links(7:10,:) 147 | 148 | %% Setup the simulation 149 | % Before the simulation can be run the time interval has to be set and the 150 | % total number of time steps has to be defined. 151 | % 152 | 153 | %setup the time interval and total number of time steps 154 | dt = 0.01; 155 | totT = round(1.5/dt); 156 | 157 | %build the full ODmatrix 158 | [ODmatrix,origins,destinations] = buildODmatrix(ODmatrices,timeSeries,dt,totT); 159 | 160 | %% Setup the dynamic equilibrium simulation 161 | % The routing behavior in the stochastic routing behaviour is aggregated 162 | % over larger time intervals to speed up computation. It is believed that 163 | % the route choice time intervals varies with a much lower frequency in 164 | % reality than the typical interval of a simulation. The routing is 165 | % described by a logit model with a scaling parameter to capture the 166 | % variance of the error term. 167 | % 168 | 169 | %time interval for the route choice 170 | rc_dt = dt; 171 | %maximum number of iterations 172 | max_it = 100; 173 | %scaling of the updates over iterations (<1) 174 | alpha = 0.05; 175 | %scaling of the utility (logit) 176 | theta = 0.010; 177 | 178 | %run DTA with deterministic route choice and MSA averaging 179 | tic 180 | [cvn_up,cvn_down,TF] = DTA_STOCH(nodes,links,origins,destinations,ODmatrix,dt,totT,rc_dt,max_it,alpha,theta); 181 | toc 182 | 183 | %% Transform CVN values to travel times 184 | % The upstream and dowsntream CVN functions of the link transmission model 185 | % are transformed into travel times for every link in the network. The 186 | % travel times are compared for the main route (from split to merge) and 187 | % the alternative route. 188 | % 189 | 190 | %calculate the simulated travel times 191 | [simTT] = cvn2tt(sum(cvn_up,3),sum(cvn_down,3),dt,totT,links); 192 | 193 | %visualize the travel time along the main route (from split to merge) 194 | [~,~,~,tt_m]=plotTT(links,2:5,simTT,dt,totT); 195 | title('Travel time graph main route','FontSize',14,'fontweight','b') 196 | %visualize the travel time along the alternative route (from split to merge) 197 | [~,~,~,tt_a]=plotTT(links,7:10,simTT,dt,totT); 198 | title('Travel time graph alternative route','FontSize',14,'fontweight','b') 199 | 200 | %compare both travel times 201 | figure; 202 | plot(dt*[0:totT],tt_m,'b',dt*[0:totT],tt_a,'r'); 203 | grid on 204 | legend('Main route','Alternative route') 205 | xlabel('Time [hr]','FontSize',12); 206 | ylabel('Travel Time [hr]','FontSize',12); 207 | title('Travel time graph','FontSize',14,'fontweight','b'); 208 | 209 | 210 | %% Visualize the split rates at the diverge 211 | % The following lines of code visualize the splitting rates at the diverge. 212 | % 213 | 214 | sp=[TF{2,:,1}]; 215 | figure;plot(dt*[0:totT-1],sp(1:2:end),'r',dt*[0:totT-1],sp(2:2:end),'b'); 216 | grid on; 217 | legend('fraction using the main road', 'fraction using the alternative'); 218 | 219 | %% Closing notes 220 | % 221 | % * ITEM1 222 | % * ITEM2 223 | % -------------------------------------------------------------------------------- /tutorial14.m: -------------------------------------------------------------------------------- 1 | %% Tutorial 14: Advanced deterministic equilibrium routing with warm start 2 | 3 | %% Disclaimer 4 | % This file is part of the matlab package for dynamic traffic assignments 5 | % developed by the KULeuven. 6 | % 7 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 8 | % 9 | % This program is free software: you can redistribute it and/or modify 10 | % it under the terms of the GNU General Public License as published by 11 | % the Free Software Foundation, either version 3 of the License, or 12 | % any later version. 13 | % 14 | % This program is distributed in the hope that it will be useful, 15 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | % GNU General Public License for more details. 18 | % 19 | % You should have received a copy of the GNU General Public License 20 | % along with this program. If not, see . 21 | % 22 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 23 | % or contact: willem.himpe {@} kuleuven.be 24 | 25 | %% Introduction 26 | % In this tutorial an advanced algorithm is implemented to find 27 | % deterministic user equilibrium in a dynamic traffic assignment. The 28 | % solution is found by means of reduced gradient projection which is 29 | % applied to all nodes simultaneously or sequentially. 30 | % 31 | 32 | %add these folders to the search path 33 | addpath('Dynamic Traffic Assignment','Visualization Tools','Network Data') 34 | javaclasspath('Dynamic Traffic Assignment'); 35 | %clear the work space 36 | clear 37 | %clear the command window 38 | close all 39 | 40 | display('<<>>') 41 | -------------------------------------------------------------------------------- /tutorial2.m: -------------------------------------------------------------------------------- 1 | %% Tutorial 2: Introducing the Link Transmission Model (LTM) 2 | 3 | %% Disclaimer 4 | % This file is part of the matlab package for dynamic traffic assignments 5 | % developed by the KULeuven. 6 | % 7 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 8 | % 9 | % This program is free software: you can redistribute it and/or modify 10 | % it under the terms of the GNU General Public License as published by 11 | % the Free Software Foundation, either version 3 of the License, or 12 | % any later version. 13 | % 14 | % This program is distributed in the hope that it will be useful, 15 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | % GNU General Public License for more details. 18 | % 19 | % You should have received a copy of the GNU General Public License 20 | % along with this program. If not, see . 21 | % 22 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 23 | % or contact: willem.himpe {@} kuleuven.be 24 | 25 | %% Introduction 26 | % This tutorial introduces a basic implementation of the link transmission 27 | % model. This module propagates traffic over a network guided by predefined 28 | % turning fractions (or splitting rates) at nodes. Congestion and spillback 29 | % is modelled with three different queuing principles. The standard queuing 30 | % approach (most realistic for traffic modelling) is based on first-order 31 | % kinematic wave theory and a triangular fundamental diagram. Implemented 32 | % alternatives are vertical queueing and horizontal queueing. 33 | % 34 | 35 | %add these folders to the search path 36 | addpath('Dynamic Traffic Assignment','Visualization Tools','Network Data') 37 | %clear the work space 38 | clear 39 | %clear the command window 40 | clc 41 | %close all windows 42 | close all 43 | 44 | display('<<>>') 45 | 46 | %% Loading the data 47 | % The network represents a two-lane highway with vehicles moving from right 48 | % to left. There are two on-ramps feeding additional traffic into the 49 | % system. The demand pattern is chosen such that the most downstream 50 | % merge forms a temporary bottleneck. 51 | % 52 | 53 | % Network and demand data 54 | load net1.mat 55 | 56 | % Plot the network 57 | plotNetwork(nodes,links,true,[]); 58 | 59 | %% Setup the simulation 60 | % Before the simulation can be run the time interval has to be set and the 61 | % total number of time steps has to be defined. These are used to transform 62 | % the different origin-destination (OD-) matrices into a 3D-matrix. The OD 63 | % matrices represent stationary demand rates inbetween the time tics 64 | % defined in the timeSeries variable. This makes it easy to adjust the time 65 | % interval (dt) of the simulation and update the input ODmatrix by 66 | % rerunning this section. The turning fractions have to be defined for each 67 | % time interval. They are stored in a cell array where each cell represents 68 | % the turns in matrix with size [#incoming links, #outgoing links]. All 69 | % turning fractions are equal to one because no diverges are present in 70 | % this network. 71 | % 72 | 73 | %setup the time interval and total number of time steps 74 | dt = 0.002; 75 | totT = 2/dt; 76 | 77 | %build the full ODmatrix 78 | [ODmatrix,origins,destinations] = buildODmatrix(ODmatrices,timeSeries,dt,totT); 79 | 80 | %initilize the Turning Fractions 81 | TF = num2cell(ones(size(nodes.id,1),totT)); 82 | for t=1:totT 83 | TF{10,t} = ones(2,1); 84 | TF{25,t} = ones(2,1); 85 | end 86 | clear t; 87 | 88 | %% Visualize the demand in the network 89 | % The demand between every origin-destination combination is plotted for 90 | % each time interval of the simulation. 91 | % 92 | 93 | figure; 94 | plot(dt*[0:totT-1],reshape(ODmatrix(1,1,:),1,[]),'d-b',dt*[0:totT-1],reshape(ODmatrix(2,1,:),1,[]),'.-r',dt*[0:totT-1],reshape(ODmatrix(3,1,:),1,[]),'x-g') 95 | legend('OD 1-28','OD 31-28','OD 34-28') 96 | xlabel('time (h)') 97 | ylabel('flow (veh/h)') 98 | 99 | %% Compute the single-commodity Dynamic Network Loading 100 | % The link transmission model propagates the traffic in the simulation. It 101 | % is composed of link model and node model which are both consistent with 102 | % first-order traffic flow theory with a triangular fundamental diagram. 103 | % For each link it is therefor required to define free-flow speed, capacity 104 | % and jam density. In hypercritical traffic states this results in delays 105 | % that are function of the density of vehicles. Alternatively it is also 106 | % possible to simulate vertical queues that do not propagate upstream or 107 | % horizontal queues that characterized by a single density for all 108 | % hypercritical states. In this implementation the queue principle is set 109 | % identical for all links in the network by suppling a 2-character string 110 | % to the LTM_SC function: 111 | % 112 | % * PQ: physical queue (standard) 113 | % * VQ: vertical queue 114 | % * HQ: horizontal queue 115 | % 116 | % Within the LTM_SC function the *sending flow* (representing the demand 117 | % towards a node) and the *receiving flow* (representing the maximum 118 | % number of vehicles alowed to propagate into a link) are evaluated for 119 | % every node in each time interval. The nested functions that compute them 120 | % are found at the end of the script. The node model governs the transfer 121 | % of flow over a node and is included as a seperate function. 122 | % 123 | 124 | display('Running LTM') 125 | 126 | %link model 127 | mode = 'PQ'; 128 | %PQ: physical queue (standard) 129 | %VQ: vertical queue 130 | %HQ: horizontal queue 131 | 132 | %run LTM 133 | tic 134 | [cvn_up,cvn_down] = LTM_SC(nodes,links,origins,destinations,ODmatrix,dt,totT,TF,mode); 135 | toc 136 | 137 | %% Visualize the resulting densities and flows using XT diagrams 138 | % The result of the link transmission model is expressed in cumulative 139 | % vehicle numbers (CVN) for every links upstream and downstream end over 140 | % the time domain. Flows and densities are computed in post processing 141 | % phase as time and space derivates of these CVN functions. Density and 142 | % flow are depicted in space-time (or XT) diagrams of the main road. 143 | % 144 | 145 | %compute the simulated densities & flows 146 | [simDensity] = cvn2dens(cvn_up,cvn_down,totT,links); 147 | [simFlows_down] = cvn2flows(sum(cvn_down,3),dt); 148 | 149 | %Main road 150 | plotXT(links,1:27,simDensity,dt,totT); 151 | title('XT-graph of densities on the main road: LTM','FontSize',14,'fontweight','b') 152 | plotXT(links,1:27,simFlows_down,dt,totT-1); 153 | title('XT-graph of downstream flows on the main road: LTM','FontSize',14,'fontweight','b') 154 | 155 | %% Transform CVN values to travel times 156 | % The upstream and dowsntream CVN functions are transformed into travel 157 | % times for every link in the network. This requires computing the time 158 | % window between the occurence of a specific vehicle number on upstream and 159 | % downstream end of the link. 160 | % 161 | 162 | %calculate the simulated travel times 163 | [simTT] = cvn2tt(cvn_up,cvn_down,dt,totT,links); 164 | 165 | %visualize the travel time along the main route (from split to merge) 166 | [~,~,~,tt]=plotTT(links,1:27,simTT,dt,totT); 167 | 168 | %% Make an animation of the result 169 | % The variation of densities is animated in the network by considering only 170 | % every 10th simulation interval. 171 | % 172 | 173 | % fRate = 20; %set frame rate 174 | fRate = inf; %allows the for manual control using space bar 175 | animateSimulation(nodes,links,simDensity(:,1:10:end),dt*[0:10:totT],fRate); %only shows every 10th frame 176 | 177 | %% Closing notes 178 | % 179 | % * The network is deliberatly chosen to be very detailed. This is done for 180 | % visualizing the results more accuratly. In tutorial 4 a coarser network 181 | % is presented with the same layout. 182 | % * To compare the queuing principles it is required to rerun the script 183 | % while the mode parameter is adjusted. 184 | % -------------------------------------------------------------------------------- /tutorial3.m: -------------------------------------------------------------------------------- 1 | %% Tutorial 3: Comparing different node models with LTM 2 | 3 | %% Disclaimer 4 | % This file is part of the matlab package for dynamic traffic assignments 5 | % developed by the KULeuven. 6 | % 7 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 8 | % 9 | % This program is free software: you can redistribute it and/or modify 10 | % it under the terms of the GNU General Public License as published by 11 | % the Free Software Foundation, either version 3 of the License, or 12 | % any later version. 13 | % 14 | % This program is distributed in the hope that it will be useful, 15 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | % GNU General Public License for more details. 18 | % 19 | % You should have received a copy of the GNU General Public License 20 | % along with this program. If not, see . 21 | % 22 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 23 | % or contact: willem.himpe {@} kuleuven.be 24 | 25 | %% Introduction 26 | % This tutorial illustrates three different implementations of the node 27 | % model. The node model forms a crurcial module of a dynamic network. 28 | % In an explicit scheme such as the base implementation of the LTM, it is 29 | % run for each node in the network and every time interval. If the node 30 | % model complies with all requirements for a general node model it will 31 | % propagate vehicles and spillback according to first-order kinematic wave 32 | % theory. Only one of the presented node models satisfies all requirements. 33 | % The alternatives do not. 34 | % 35 | 36 | %add these folders to the search path 37 | addpath('Dynamic Traffic Assignment','Visualization Tools','Network Data') 38 | %clear the work space 39 | clear 40 | %clear the command window 41 | clc 42 | %close all windows 43 | close all 44 | 45 | display('<<>>') 46 | 47 | %% Loading the data 48 | % The network represents a two-lane road with vehicles moving from right 49 | % to left. There are two on-ramps feeding additional traffic into the 50 | % system and one crossing road (moving from below to the top). 51 | % The demand pattern is chosen such that the most downstream 52 | % merge forms a temporary bottleneck and congestion spills back over the 53 | % node. 54 | % 55 | 56 | % Network and demand data 57 | load net2.mat 58 | 59 | % Plot the network 60 | plotNetwork(nodes,links,true,[]); 61 | 62 | %% Setup the simulation 63 | % Before the simulation can be run the time interval has to be set and the 64 | % total number of time steps has to be defined. These are used to transform 65 | % the different origin-destination (OD-) matrices into a 3D-matrix. 66 | % The turning fractions have to be defined for each time interval. A small 67 | % fraction (10%) of the vehicles on the main road make a right turn at the 68 | % crossing road. 69 | % 70 | 71 | %setup the time interval and total number of time steps 72 | dt = 0.004; 73 | totT = 2/dt; 74 | 75 | %build the full ODmatrix 76 | [ODmatrix,origins,destinations] = buildODmatrix(ODmatrices,timeSeries,dt,totT); 77 | 78 | %initilize the Turning Fractions (easy if no diverges in the network) 79 | TF = num2cell(ones(size(nodes.id,1),totT)); 80 | for t=1:totT 81 | TF{10,t} = ones(2,1); 82 | TF{25,t} = ones(2,1); 83 | TF{15,t} = [0.9 0.1; 84 | 0 1 ]; 85 | end 86 | clear t; 87 | 88 | %% Visualize the demand in the network 89 | % The demand between every origin-destination combination is plotted for 90 | % each time interval of the simulation. 91 | % 92 | 93 | figure; 94 | plot(dt*[0:totT-1],reshape(ODmatrix(1,1,:),1,[]),'d-b',dt*[0:totT-1],reshape(ODmatrix(3,1,:),1,[]),'.-r',dt*[0:totT-1],reshape(ODmatrix(4,1,:),1,[]),'x-g') 95 | legend('OD 1-28','OD 37-28','OD 40-28') 96 | xlabel('time (h)') 97 | ylabel('flow (veh/h)') 98 | 99 | figure; 100 | plot(dt*[0:totT-1],reshape(ODmatrix(1,2,:),1,[]),'d-b',dt*[0:totT-1],reshape(ODmatrix(3,2,:),1,[]),'.-r',dt*[0:totT-1],reshape(ODmatrix(2,2,:),1,[]),'x-g') 101 | legend('OD 1-31','OD 37-31','OD 34-31') 102 | xlabel('time (h)') 103 | ylabel('flow (veh/h)') 104 | 105 | %% Compute the single-commodity Dynamic Network Loading 106 | % The link transmission model propagates the traffic in the simulation. It 107 | % is composed of a link model and a node model that both have to be 108 | % consistent with first-order traffic flow theory. Only the oriented 109 | % capacity proportional model satisfies all requirements to be applied as 110 | % a general node model. The same model without redistribution doesn't 111 | % maximize throughput from a drivers perspective and the demand 112 | % proportional node model violates the invariance principle. 113 | % 114 | % 115 | % * OC: oriented capacity proportional (standard) 116 | % * NR: oriented capacity proportional with no redistribution 117 | % * DP: demand proportional 118 | % 119 | % bla 120 | % 121 | 122 | display('Running LTM') 123 | 124 | %link model 125 | Lmod = 'PQ'; 126 | 127 | %node model 128 | Nmod = 'OC'; 129 | %OC: oriented capacity proportional (standard) 130 | %NR: oriented capacity proportional with no redistribution 131 | %DP: demand proportional 132 | 133 | %run LTM 134 | tic 135 | [cvn_up,cvn_down] = LTM_SC(nodes,links,origins,destinations,ODmatrix,dt,totT,TF,Lmod,Nmod); 136 | toc 137 | 138 | %% Visualize the resulting densities and flows using XT diagrams 139 | % The result of the link transmission model is expressed in cumulative 140 | % vehicle numbers (CVN) for every links upstream and downstream end over 141 | % the time domain. Flows and densities are computed in post processing 142 | % phase as time and space derivates of these CVN functions. Density and 143 | % flow are depicted in space-time (or XT) diagrams of the main road annd 144 | % the crossing road. 145 | % 146 | 147 | %compute the simulated densities & flows 148 | [simDensity] = cvn2dens(cvn_up,cvn_down,totT,links); 149 | [simFlows_down] = cvn2flows(sum(cvn_down,3),dt); 150 | 151 | %Main road 152 | plotXT(links,1:27,abs(simDensity),dt,totT); 153 | plotXT(links,1:27,simFlows_down,dt,totT-1); 154 | 155 | %Crossing road 156 | plotXT(links,[33:-1:31,28:30]',simFlows_down,dt,totT-1); 157 | plotXT(links,[33:-1:31,28:30]',simDensity,dt,totT); 158 | 159 | %% Closing notes 160 | % 161 | % * To compare the node models it is required to rerun the script 162 | % while the Nmod parameter is adjusted. 163 | % * Note how for the node model without redistribution the link capacity 164 | % towards destination 31 is not fully used during spill back. 165 | % * To see the effect of the demand proportional node model the demand has to 166 | % be varied over a range of values. 167 | % -------------------------------------------------------------------------------- /tutorial4.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HimpeWillem/MatlabTrafficToolbox/7e58302d0f493cfd6745c5f0daf340fcf6fdb63a/tutorial4.m -------------------------------------------------------------------------------- /tutorial7.m: -------------------------------------------------------------------------------- 1 | %% Tutorial 7: Comparing explicit and implicit scheme on a corridor 2 | 3 | %% Disclaimer 4 | % This file is part of the matlab package for dynamic traffic assignments 5 | % developed by the KULeuven. 6 | % 7 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 8 | % 9 | % This program is free software: you can redistribute it and/or modify 10 | % it under the terms of the GNU General Public License as published by 11 | % the Free Software Foundation, either version 3 of the License, or 12 | % any later version. 13 | % 14 | % This program is distributed in the hope that it will be useful, 15 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | % GNU General Public License for more details. 18 | % 19 | % You should have received a copy of the GNU General Public License 20 | % along with this program. If not, see . 21 | % 22 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 23 | % or contact: willem.himpe {@} kuleuven.be 24 | 25 | %% Introduction 26 | % This tutorial illustrates the difference between an explicit and implicit 27 | % scheme for the link transmission model on a corridor. 28 | % 29 | 30 | %add these folders to the search path 31 | addpath('Dynamic Traffic Assignment','Visualization Tools','Network Data') 32 | javaclasspath('Dynamic Traffic Assignment'); 33 | %clear the work space 34 | clear 35 | %clear the command window 36 | clc 37 | %close all windows 38 | close all 39 | 40 | display('<<>>') 41 | 42 | %% Loading the data 43 | % The network represents a simple corridor of network that consists of the 44 | % interaction of three highways (R0-E40-E314) in the area between Leuven 45 | % and Brussels in Belgium. 46 | % 47 | 48 | % Network and demand data 49 | load net5.mat 50 | 51 | % Plot the network 52 | plotNetwork(nodes,links,true,[]); 53 | 54 | %% Setup the simulation (for small and large time intervals) 55 | % Before the simulation can be run the time interval has to be set and the 56 | % total number of time steps has to be defined. These are used to transform 57 | % the different origin-destination (OD-) matrices into a 3D-matrix. The 58 | % time interval is bound by CFL-conditions for the explicit scheme. 59 | % It can not be larger then the minimal travel time of the fastest 60 | % kinematic wave in the network. The time interval for the implicit scheme 61 | % is set ten times larger than the CFL-conditions. 62 | % 63 | 64 | %setup the time interval and total number of time steps 65 | dt = min(links.length./links.freeSpeed); 66 | totT = round(4/dt)+1; 67 | dt_l = 5/60; 68 | totT_l = round(4/dt_l); 69 | 70 | %build the full ODmatrix 71 | [ODmatrix,origins,destinations] = buildODmatrix(ODmatrices,timeSeries,dt,totT); 72 | [ODmatrix_l,origins,destinations] = buildODmatrix(ODmatrices,timeSeries,dt_l,totT_l); 73 | 74 | %% initilize the Destination Based Split rates 75 | % The destination based split rates are set such that only the shortest 76 | % path in free flow conditions is used. In this network there is no route 77 | % choice and it is not required to compute the users respons to delays. 78 | 79 | %Compute free flow travel times on each link for every time interval. 80 | tt_free = repmat(links.length./links.freeSpeed,1,totT+1); 81 | %Compute destination based turning fractions 82 | TF = allOrNothingTF(nodes,links,destinations,tt_free,[],dt,totT,10*dt,'last'); 83 | 84 | %% Compute a multi-commodity Dynamic Network Loading 85 | % First the explicit multi-commodity link transmission model is used to 86 | % propagate the traffic over the network. This model requires detailed time 87 | % discretization to provide a result. 88 | % 89 | 90 | display('Running LTM multi-commodity with an explicit scheme') 91 | display(['total number of node updates: ',num2str(totT*length(nodes.id))]); 92 | 93 | %run LTM 94 | tic 95 | [cvn_up_d,cvn_down_d] = LTM_MC(nodes,links,origins,destinations,ODmatrix,dt,totT,TF); 96 | toc 97 | 98 | %% Compute a multi-commodity Dynamic Network Loading with large time intervals 99 | % Now the implicit multi-commodity link transmission model is used to 100 | % propagate the traffic over the network. This model is able to compute a 101 | % result for much larger time intervals. This isn't only beneficial for 102 | % memory usage as less data has to be stored. It is also important for 103 | % computation time as now less node updates have to be computed. This 104 | % effect is however reduced by required iterations of each time slice. 105 | % 106 | 107 | display('Running I-LTM multi-commodity with large time intervals and an implicit scheme') 108 | 109 | %run ILTM 110 | tic 111 | [cvn_up_dl,cvn_down_dl] = ILTM_BASE(nodes,links,origins,destinations,ODmatrix_l,dt_l,totT_l,TF); 112 | toc 113 | 114 | %% Visualize the resulting densities using XT diagrams 115 | % Resulting densities and flows are depicted for both approaches in 116 | % space-time (or XT) diagrams of the E40 highway from east to west. 117 | % 118 | 119 | %compute the simulated densities & flows 120 | [simDensity_d] = cvn2dens(sum(cvn_up_d,3),sum(cvn_down_d,3),totT,links); 121 | [simFlows_down_d] = cvn2flows(sum(cvn_down_d,3),dt); 122 | [simDensity_dl] = cvn2dens(sum(cvn_up_dl,3),sum(cvn_down_dl,3),totT_l,links); 123 | [simFlows_down_dl] = cvn2flows(sum(cvn_down_dl,3),dt_l); 124 | 125 | %Main road 126 | route = [1,2,51,55,5,8,11,13,15,17,19,22,23,26,29,56,58,33]; 127 | plotXT(links,route,simDensity_d,dt,totT); 128 | title('XT-graph of densities (E40 E->W): Explicit LTM','FontSize',14,'fontweight','b') 129 | plotXT(links,route,simDensity_dl,dt_l,totT_l); 130 | title('XT-graph of densities (E40 E->W): Implicit LTM','FontSize',14,'fontweight','b') 131 | 132 | %% Compute the maximum difference between both solutions 133 | % The following lines of code compare the output of both models in terms of 134 | % difference in density and total travel time spend. The absolute 135 | % difference in density of both solutions is also depicted in a space-time 136 | % graph of the E40 highway from east to west. 137 | % 138 | maxDiff = 0; 139 | for d=1:length(destinations) 140 | for l=1:length(links.id) 141 | maxDiff = maxDiff + sum(abs(interp1(dt*[0:totT],cvn_up_d(l,:,d),dt_l*[0:totT_l])-cvn_up_dl(l,:,d))); 142 | maxDiff = maxDiff + sum(abs(interp1(dt*[0:totT],cvn_down_d(l,:,d),dt_l*[0:totT_l])-cvn_down_dl(l,:,d))); 143 | end 144 | end 145 | 146 | simDensity_d2 = zeros(size(simDensity_dl)); 147 | for l=1:length(links.id) 148 | simDensity_d2(l,:) = interp1(dt*[0:totT],simDensity_d(l,:),dt_l*[0:totT_l]); 149 | end 150 | 151 | fprintf(1,'\n'); 152 | display('Comparing LTM & ILTM') 153 | display(['- maximum difference in density: ',num2str(max(max(abs(simDensity_d2-simDensity_dl)))),' veh/km']); 154 | display(['- average difference in density: ',num2str(sum(sum(abs(simDensity_dl-simDensity_d2)))/sum(sum(simDensity_d2))),' veh/km']); 155 | display(['- absolute difference in total travel time spend: ',num2str(abs(sum(sum(dt_l/2*(simDensity_d2(:,1:end-1)+simDensity_d2(:,2:end)).*repmat(links.length,1,totT_l)))-sum(sum(dt_l/2*(simDensity_dl(:,1:end-1)+simDensity_dl(:,2:end).*repmat(links.length,1,totT_l)))))),' veh*h']); 156 | 157 | plotXT(links,route,abs(simDensity_d2-simDensity_dl),dt_l,totT_l); 158 | 159 | %% Transform CVN values to travel times 160 | % The upstream and dowsntream CVN functions of the link transmission model 161 | % are transformed into travel times for every link in the network. 162 | 163 | %calculate the simulated travel times 164 | [simTT] = cvn2tt(sum(cvn_up_d,3),sum(cvn_down_d,3),dt,totT,links); 165 | [simTT_l] = cvn2tt(sum(cvn_up_dl,3),sum(cvn_down_dl,3),dt_l,totT_l,links); 166 | 167 | %visualize the travel time along the main route (from split to merge) 168 | [~,~,~,tt]=plotTT(links,route,simTT,dt,totT); 169 | title('Travel time graph (E40 E->W): Explicit LTM','FontSize',14,'fontweight','b'); 170 | [~,~,~,tt_l]=plotTT(links,route,simTT_l,dt_l,totT_l); 171 | title('Travel time graph (E40 E->W): Implicit LTM','FontSize',14,'fontweight','b'); 172 | 173 | %compare both travel times 174 | figure; 175 | plot(dt*[0:totT],tt,'b',dt_l*[0:totT_l],tt_l,'r'); 176 | grid on 177 | legend('LTM','I-LTM') 178 | xlabel('Time [hr]','FontSize',12); 179 | ylabel('Travel Time [hr]','FontSize',12); 180 | title('Travel time graph','FontSize',14,'fontweight','b'); 181 | 182 | %% Closing notes 183 | % 184 | % * The implicit model can be run with increasing time intervals to inspect 185 | % the aggregation errors. It can be seen that travel times are biased 186 | % towards lower values. This is expected as the active period of the 187 | % bottleneck shortens with larger time intervals. 188 | % computations 189 | % -------------------------------------------------------------------------------- /tutorial8.m: -------------------------------------------------------------------------------- 1 | %% Tutorial 8: Advanced algorithms, faster data types and compilation (mex) for the implicit scheme 2 | 3 | %% Disclaimer 4 | % This file is part of the matlab package for dynamic traffic assignments 5 | % developed by the KULeuven. 6 | % 7 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 8 | % 9 | % This program is free software: you can redistribute it and/or modify 10 | % it under the terms of the GNU General Public License as published by 11 | % the Free Software Foundation, either version 3 of the License, or 12 | % any later version. 13 | % 14 | % This program is distributed in the hope that it will be useful, 15 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | % GNU General Public License for more details. 18 | % 19 | % You should have received a copy of the GNU General Public License 20 | % along with this program. If not, see . 21 | % 22 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 23 | % or contact: willem.himpe {@} kuleuven.be 24 | 25 | %% Introduction 26 | % This tutorial introduces faster data types and compilation techniques 27 | % for the implicit link transmission model. Additional the jacobi iteration 28 | % scheme is replaced by a Gauss-Seidel updating of the node updates within 29 | % a time slice. A node is only updated if its boundary conditions are 30 | % changed. This leads to a scheme with a minimum of redundant computations. 31 | % 32 | 33 | %add these folders to the search path 34 | addpath('Dynamic Traffic Assignment','Visualization Tools','Network Data') 35 | javaclasspath('Dynamic Traffic Assignment'); 36 | %clear the work space 37 | clear 38 | %clear the command window 39 | clc 40 | %close all windows 41 | close all 42 | 43 | display('<<>>') 44 | 45 | %% Loading the data 46 | % The network represents a simple corridor of network that consists of the 47 | % interaction of three highways (R0-E40-E314) in the area between Leuven 48 | % and Brussels in Belgium. 49 | % 50 | 51 | % Network and demand data 52 | load net5.mat 53 | 54 | % Plot the network 55 | plotNetwork(nodes,links,true,[]); 56 | 57 | %% Setup the simulation (for small and large time intervals) 58 | % Before the simulation can be run the time interval has to be set and the 59 | % total number of time steps has to be defined. These are used to transform 60 | % the different origin-destination (OD-) matrices into a 3D-matrix. If the 61 | % time interval is bound by CFL-conditions the iterative link transmission 62 | % model reduces to an explicit scheme. If the time interval is larger 63 | % iterations are required to find a consistent dynamic network loading. 64 | % 65 | 66 | %setup the time interval and total number of time steps 67 | dt = 5/60; 68 | totT = round(4/dt); 69 | 70 | %build the full ODmatrix 71 | [ODmatrix,origins,destinations] = buildODmatrix(ODmatrices,timeSeries,dt,totT); 72 | 73 | %% initilize the Destination Based Split rates 74 | % The destination based split rates are set such that only the shortest 75 | % path in free flow conditions is used. In this network there is no route 76 | % choice and it is not required to compute the users respons to delays. 77 | 78 | %Compute free flow travel times on each link for every time interval. 79 | tt_free = repmat(links.length./links.freeSpeed,1,totT+1); 80 | %Compute destination based turning fractions 81 | TF = allOrNothingTF(nodes,links,destinations,tt_free,[],dt,totT,10*dt,'last'); 82 | 83 | %% Compute a multi-commodity Dynamic Network Loading with large time intervals 84 | % First the base implementation of the link transmission model is used to 85 | % propagate the traffic over the network. This model updates nodes 86 | % repeatedly until no more changes are observed. All nodes are updates 87 | % simulateneously, like in Jacobi iterative scheme. 88 | % 89 | 90 | display('Running I-LTM base implementation') 91 | 92 | %run ILTM 93 | tic 94 | [cvn_up_d,cvn_down_d] = ILTM_BASE(nodes,links,origins,destinations,ODmatrix,dt,totT,TF); 95 | toc 96 | 97 | %% Faster computation 98 | % Now the advanced implementation of the link transmission model is used to 99 | % propagate the traffic over the network. This model updates nodes 100 | % repeatedly until no more changes are observed. All nodes are updates 101 | % sequentially, like in Gauss-Seidel iterative scheme. If the boundary 102 | % conditions of a node are unchanged with respect to the previous iteration 103 | % no update is performend. 104 | % 105 | 106 | fprintf(1,'\n'); 107 | display('Running ILTM advanded implementation') 108 | 109 | %set faster lookup structures 110 | load net5_old.mat 111 | [links,node_prop] = dataParser(links,nodes,origins,destinations,dt); 112 | %set turning fractions faster (based on free flow conditions) 113 | TF_f=TF_init(node_prop,links,destinations,dt,totT); 114 | 115 | %run ILTM 116 | tic 117 | [cvn_up_df,cvn_down_df] = ILTM(node_prop,links,origins,destinations,ODmatrix,dt,totT,TF_f); 118 | toc 119 | 120 | %cvn values are also in a different form 121 | cvn_up_tot=reshape(sum(cvn_up_df,2),[],totT+1); 122 | cvn_down_tot=reshape(sum(cvn_down_df,2),[],totT+1); 123 | 124 | %% Even faster computation 125 | % Finally the matlab functions are also compiled to machine code (mex-file). 126 | % This leads to an additional speedup compared to the native matlab code. 127 | % 128 | 129 | fprintf(1,'\n'); 130 | display('Running I-LTM mex implementation') 131 | 132 | %run ILTM 133 | tic 134 | [cvn_up_dm,cvn_down_dm] = ILTM_cold_mex(node_prop,links,origins,destinations,ODmatrix,dt,totT,TF_f); 135 | toc 136 | 137 | %cvn values are also in a different form 138 | cvn_up_totm=reshape(sum(cvn_up_dm,2),[],totT+1); 139 | cvn_down_totm=reshape(sum(cvn_down_dm,2),[],totT+1); 140 | 141 | %% Visualize the resulting densities using XT diagrams 142 | % Resulting densities and flows are depicted for all three approaches in 143 | % space-time (or XT) diagrams of the E40 highway from east to west. 144 | % 145 | 146 | %compute the simulated densities & flows 147 | [simDensity_d] = cvn2dens(sum(cvn_up_d,3),sum(cvn_down_d,3),totT,links); 148 | [simFlows_down_d] = cvn2flows(sum(cvn_down_d,3),dt); 149 | [simDensity_df] = cvn2dens(cvn_up_tot,cvn_down_tot,totT,links); 150 | [simFlows_down_df] = cvn2flows(cvn_down_tot,dt); 151 | [simDensity_dm] = cvn2dens(cvn_up_totm,cvn_down_totm,totT,links); 152 | [simFlows_down_dm] = cvn2flows(cvn_down_totm,dt); 153 | 154 | 155 | %Main road 156 | route = [1,2,51,55,5,8,11,13,15,17,19,22,23,26,29,56,58,33]; 157 | plotXT(links,route,simDensity_d,dt,totT); 158 | title('XT-graph of densities (E40 E->W): base ILTM','FontSize',14,'fontweight','b') 159 | plotXT(links,route,simDensity_df,dt,totT); 160 | title('XT-graph of densities (E40 E->W): advance ILTM','FontSize',14,'fontweight','b') 161 | plotXT(links,route,simDensity_dm,dt,totT); 162 | title('XT-graph of densities (E40 E->W): mex ILTM','FontSize',14,'fontweight','b') 163 | 164 | %% Compute the maximum difference between all solutions 165 | % The following lines of code compare the output of both models in terms of 166 | % difference in densities. 167 | % 168 | 169 | fprintf(1,'\n'); 170 | display('Comparing I-LTM base & I-LTM') 171 | display(['- maximum difference in density: ',num2str(max(max(abs(simDensity_d-simDensity_df)))),' veh/km']); 172 | display(['- average difference in density: ',num2str(sum(sum(abs(simDensity_df-simDensity_d)))/sum(sum(simDensity_d))),' veh/km']); 173 | 174 | fprintf(1,'\n'); 175 | display('Comparing I-LTM & I-LTM mex') 176 | display(['- maximum difference in density: ',num2str(max(max(abs(simDensity_dm-simDensity_df)))),' veh/km']); 177 | display(['- average difference in density: ',num2str(sum(sum(abs(simDensity_df-simDensity_dm)))/sum(sum(simDensity_df))),' veh/km']); 178 | 179 | %% Closing notes 180 | % 181 | % * ITEM1 182 | % * ITEM2 183 | % -------------------------------------------------------------------------------- /tutorial9.m: -------------------------------------------------------------------------------- 1 | %% Tutorial 9: Warm starting the implicit scheme of the LTM 2 | 3 | %% Disclaimer 4 | % This file is part of the matlab package for dynamic traffic assignments 5 | % developed by the KULeuven. 6 | % 7 | % Copyright (C) 2016 Himpe Willem, Leuven, Belgium 8 | % 9 | % This program is free software: you can redistribute it and/or modify 10 | % it under the terms of the GNU General Public License as published by 11 | % the Free Software Foundation, either version 3 of the License, or 12 | % any later version. 13 | % 14 | % This program is distributed in the hope that it will be useful, 15 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | % GNU General Public License for more details. 18 | % 19 | % You should have received a copy of the GNU General Public License 20 | % along with this program. If not, see . 21 | % 22 | % More information at: http://www.mech.kuleuven.be/en/cib/traffic/downloads 23 | % or contact: willem.himpe {@} kuleuven.be 24 | 25 | %% Introduction 26 | % This tutorial illustrates the warm starting capabilities of the iterative 27 | % link transmission scheme. Because the solution is computed iteratively, 28 | % it has to be initialized. A previous solution that is closer to the new 29 | % scenarios is computationally less demanding. 30 | % 31 | 32 | %add these folders to the search path 33 | addpath('Dynamic Traffic Assignment','Visualization Tools','Network Data') 34 | javaclasspath('Dynamic Traffic Assignment'); 35 | %clear the work space 36 | clear 37 | %clear the command window 38 | clc 39 | %close all windows 40 | close all 41 | 42 | display('<<>>') 43 | 44 | %% Loading the data 45 | % The network represents a simple corridor of network that consists of the 46 | % interaction of three highways (R0-E40-E314) in the area between Leuven 47 | % and Brussels in Belgium. 48 | % 49 | 50 | % Network and demand data 51 | load net5_old.mat 52 | 53 | % Plot the network 54 | plotNetwork(nodes,links,true,[]); 55 | 56 | %% Setup the simulation (for small and large time intervals) 57 | % Before the simulation can be run the time interval has to be set and the 58 | % total number of time steps has to be defined. These are used to transform 59 | % the different origin-destination (OD-) matrices into a 3D-matrix. If the 60 | % time interval is bound by CFL-conditions the iterative link transmission 61 | % model reduces to an explicit scheme. If the time interval is larger 62 | % iterations are required to find a consistent dynamic network loading. 63 | % 64 | 65 | %setup the time interval and total number of time steps 66 | dt = 5/60; 67 | totT = round(4/dt); 68 | 69 | %build the full ODmatrix 70 | [ODmatrix,origins,destinations] = buildODmatrix(ODmatrices,timeSeries,dt,totT); 71 | 72 | %% Computation of the base scenario 73 | % First the base scenario is computed with a cold start. The iteration 74 | % procedure of each time interval is initialized with CVN values of the 75 | % previous time slice. This assumes that initially no vehicles proceed to 76 | % the next time interval. 77 | % 78 | 79 | display('Running I-LTM with a cold start') 80 | 81 | %set faster lookup structures 82 | [links,node_prop] = dataParser(links,nodes,origins,destinations,dt); 83 | %set turning fractions faster (based on free flow conditions) 84 | TF=TF_init(node_prop,links,destinations,dt,totT); 85 | 86 | %run I-LTM 87 | tic 88 | [cvn_up_db,cvn_down_db,con_up,con_down] = ILTM_cold_mex(node_prop,links,origins,destinations,ODmatrix,dt,totT,TF); 89 | toc 90 | 91 | %compute the density 92 | cvn_up_totb=reshape(sum(cvn_up_db,2),[],totT+1); 93 | cvn_down_totb=reshape(sum(cvn_down_db,2),[],totT+1); 94 | [simDensity_db] = cvn2dens(cvn_up_totb,cvn_down_totb,totT,links); 95 | 96 | %visualize the result 97 | route = [1,2,51,55,5,8,11,13,15,17,19,22,23,26,29,56,58,33]; 98 | plotXT(links,route,simDensity_db,dt,totT); 99 | title('XT-graph of densities (E40 E->W): Cold start','FontSize',10,'fontweight','b') 100 | 101 | %% Compute the response of a marginal change 102 | % Now a new scenario is computed intialized or warm started with the base 103 | % scenario. Fewer node updates and iterations are required to find a 104 | % solution in this case which is beneficial for computation times. First a 105 | % variation (adding an amount of vehicles to the ODmatrix) is applied 106 | % before the peak hour. Because the solution is computed in terms of 107 | % cumulative vehicle numbers such a variation persists until the end of the 108 | % simulation. Next a variation during the peak hour is applied. Because 109 | % such a variation alters the congestion pattern. Much more nodes have to 110 | % be updated thus increasing computation times. Finally a variation is 111 | % applied after the peak hour. Now the region in space and time that is 112 | % affected by this change is limited and computational gaines are the 113 | % largest. 114 | % 115 | 116 | display('Running I-LTM with a warm start for a variation before the peak') 117 | 118 | %select odt combination and amount of vehicles to add to the ODmatrix 119 | o = 1; 120 | d = 3; 121 | t = 3; 122 | val = 1*dt; 123 | 124 | %activate the changed origin 125 | nodes2update = false(length(nodes.id),totT+1); 126 | nodes2update(origins(1),:) = true; 127 | 128 | %update the ODmatrix 129 | ODmatrix_t = ODmatrix; 130 | ODmatrix_t(o,d,t)=ODmatrix(o,d,t)+val; 131 | 132 | %run ILTM 133 | tic 134 | [cvn_up_d,cvn_down_d]=ILTM_warm_mex(node_prop,links,origins,destinations,ODmatrix_t,dt,totT,TF,cvn_up_db,cvn_down_db,con_up,con_down,nodes2update); 135 | toc 136 | 137 | %compute the density 138 | cvn_up_tot=reshape(sum(cvn_up_d,2),[],totT+1); 139 | cvn_down_tot=reshape(sum(cvn_down_d,2),[],totT+1); 140 | [simDensity_d] = cvn2dens(cvn_up_tot,cvn_down_tot,totT,links); 141 | 142 | %visualize the result 143 | plotXT(links,route,abs(simDensity_db-simDensity_d),dt,totT); 144 | title('Warm start for a variation before the peak','FontSize',10,'fontweight','b') 145 | 146 | %% 147 | 148 | display('Running I-LTM with a warm start for a variation during the peak') 149 | 150 | %select odt combination and amount of vehicles to add to the ODmatrix 151 | o = 1; 152 | d = 3; 153 | t = 10; 154 | val = 1*dt; 155 | 156 | %activate the changed origin 157 | nodes2update = false(length(nodes.id),totT+1); 158 | nodes2update(origins(1),:) = true; 159 | 160 | %update the ODmatrix 161 | ODmatrix_t = ODmatrix; 162 | ODmatrix_t(o,d,t)=ODmatrix(o,d,t)+val; 163 | 164 | %run ILTM 165 | tic 166 | [cvn_up_d,cvn_down_d]=ILTM_warm_mex(node_prop,links,origins,destinations,ODmatrix_t,dt,totT,TF,cvn_up_db,cvn_down_db,con_up,con_down,nodes2update); 167 | toc 168 | 169 | %compute the density 170 | cvn_up_tot=reshape(sum(cvn_up_d,2),[],totT+1); 171 | cvn_down_tot=reshape(sum(cvn_down_d,2),[],totT+1); 172 | [simDensity_d] = cvn2dens(cvn_up_tot,cvn_down_tot,totT,links); 173 | 174 | %visualize the result 175 | plotXT(links,route,abs(simDensity_db-simDensity_d),dt,totT); 176 | title('Warm start for a variation during the peak','FontSize',10,'fontweight','b') 177 | 178 | %% 179 | 180 | display('Running I-LTM with a warm start for a variation after the peak') 181 | 182 | %select odt combination and amount of vehicles to add to the ODmatrix 183 | o = 1; 184 | d = 3; 185 | t = 40; 186 | val = 1*dt; 187 | 188 | %activate the changed origin 189 | nodes2update = false(length(nodes.id),totT+1); 190 | nodes2update(origins(1),:) = true; 191 | 192 | %update the ODmatrix 193 | ODmatrix_t = ODmatrix; 194 | ODmatrix_t(o,d,t)=ODmatrix(o,d,t)+val; 195 | 196 | %run ILTM 197 | tic 198 | [cvn_up_d,cvn_down_d]=ILTM_warm_mex(node_prop,links,origins,destinations,ODmatrix_t,dt,totT,TF,cvn_up_db,cvn_down_db,con_up,con_down,nodes2update); 199 | toc 200 | 201 | %compute the density 202 | cvn_up_tot=reshape(sum(cvn_up_d,2),[],totT+1); 203 | cvn_down_tot=reshape(sum(cvn_down_d,2),[],totT+1); 204 | [simDensity_d] = cvn2dens(cvn_up_tot,cvn_down_tot,totT,links); 205 | 206 | %visualize the result 207 | plotXT(links,route,abs(simDensity_db-simDensity_d),dt,totT); 208 | title('Warm start for a variation after the peak','FontSize',10,'fontweight','b') 209 | 210 | %% Closing notes 211 | % 212 | % * ITEM1 213 | % * ITEM2 214 | % --------------------------------------------------------------------------------