├── ALINEA.m ├── DQN_PMPC_validate.m ├── DQN_RM_reset.m ├── DQN_RM_step.m ├── DQN_RM_validate.m ├── Density.m ├── Desired_speed.m ├── Freeway_model.m ├── Freeway_model_predict.m ├── MPC_RM_VSL.m ├── MPC_imp_RM.m ├── Main_openloop.m ├── Outflow_main.m ├── Outflow_ramp.m ├── PMPC_DQN_RM.m ├── PMPC_RM_VSL.m ├── Para_RM_control_law.m ├── README.md ├── Ramp_metering_learning.m ├── Ramp_reset.m ├── Ramp_step.m ├── Speed.m ├── demando0.m ├── demando1.m ├── demando2.m ├── demando3.m ├── parameters_predict.m ├── parameters_real.m ├── rou_36_down.m ├── sat_rm.m └── u_para_RM.m /ALINEA.m: -------------------------------------------------------------------------------- 1 | clear; 2 | % clc 3 | % close all 4 | %% Initial state 5 | x=[zeros(62,1);0]; 6 | v_free=102; 7 | u_rm_1=1; 8 | u_rm_2=1; 9 | u_rm_3=1; 10 | k_gain=0.1; 11 | u=[v_free,v_free,v_free,v_free,v_free,v_free,v_free,v_free,v_free,u_rm_1,u_rm_2,u_rm_3];Weather=[]; 12 | % noise_o0=random('Normal',0,150,1,151); % normal distributed noise on the demand 13 | % noise_o1=random('Normal',0,60,1,151); 14 | % noise_o2=random('Normal',0,60,1,151); 15 | % noise_o3=random('Normal',0,60,1,151); 16 | initial_weather=3; 17 | end_weather=2; 18 | M=6; % control time step 19 | weather=initial_weather; % weather condition: 1 is sunny, 2 is rainy, 3 is storming 20 | for i=1:60 21 | x=Freeway_model(x,u, 1); 22 | end 23 | xx=[]; 24 | % u=[102,102,102,102,102,102,1,1,1]; 25 | % load('weather_01.mat'); 26 | % u=[100*ones(2,900);u_ramp]; 27 | % u=repelem(U,1,6); 28 | switch initial_weather 29 | case 1 30 | rou_crit=36.9; 31 | case 2 32 | rou_crit=29.5; 33 | case 3 34 | rou_crit=22.5; 35 | end 36 | for i=1:720/M 37 | 38 | % weather=3; 39 | % u=U(:,i); 40 | % weather=(end_weather-initial_weather)/720*i+initial_weather; 41 | if i>360/M 42 | weather=end_weather; 43 | end 44 | Rou_15=x(13); 45 | Rou_25=x(31); 46 | Rou_35=x(49); 47 | u_rm_1=Para_RM_control_law(k_gain,u_rm_1,rou_crit, Rou_15,1); 48 | u_rm_2=Para_RM_control_law(k_gain,u_rm_2,rou_crit, Rou_25,1); 49 | u_rm_3=Para_RM_control_law(k_gain,u_rm_3,rou_crit, Rou_35,1); 50 | u=[v_free,v_free,v_free,v_free,v_free,v_free,v_free,v_free,v_free,u_rm_1,u_rm_2,u_rm_3]; 51 | for j=1:M 52 | 53 | x=Freeway_model(x,u, weather); 54 | xx=[xx x]; 55 | 56 | Weather=[Weather weather]; 57 | end 58 | 59 | end 60 | rou_11=xx(1,:); 61 | v_11=xx(2,:); 62 | q_11=xx(3,:); 63 | rou_12=xx(4,:); 64 | v_12=xx(5,:); 65 | q_12=xx(6,:); 66 | rou_13=xx(7,:); 67 | v_13=xx(8,:); 68 | q_13=xx(9,:); 69 | rou_14=xx(10,:); 70 | v_14=xx(11,:); 71 | q_14=xx(12,:); 72 | rou_15=xx(13,:); 73 | v_15=xx(14,:); 74 | q_15=xx(15,:); 75 | rou_16=xx(16,:); 76 | v_16=xx(17,:); 77 | q_16=xx(18,:); 78 | rou_21=xx(19,:); 79 | v_21=xx(20,:); 80 | q_21=xx(21,:); 81 | rou_22=xx(22,:); 82 | v_22=xx(23,:); 83 | q_22=xx(24,:); 84 | rou_23=xx(25,:); 85 | v_23=xx(26,:); 86 | q_23=xx(27,:); 87 | rou_24=xx(28,:); 88 | v_24=xx(29,:); 89 | q_24=xx(30,:); 90 | rou_25=xx(31,:); 91 | v_25=xx(32,:); 92 | q_25=xx(33,:); 93 | rou_26=xx(34,:); 94 | v_26=xx(35,:); 95 | q_26=xx(36,:); 96 | rou_31=xx(37,:); 97 | v_31=xx(38,:); 98 | q_31=xx(39,:); 99 | rou_32=xx(40,:); 100 | v_32=xx(41,:); 101 | q_32=xx(42,:); 102 | rou_33=xx(43,:); 103 | v_33=xx(44,:); 104 | q_33=xx(45,:); 105 | rou_34=xx(46,:); 106 | v_34=xx(47,:); 107 | q_34=xx(48,:); 108 | rou_35=xx(49,:); 109 | v_35=xx(50,:); 110 | q_35=xx(51,:); 111 | rou_36=xx(52,:); 112 | v_36=xx(53,:); 113 | q_36=xx(54,:); 114 | q_o0=xx(55,:); 115 | w_o0=xx(56,:); 116 | q_o1=xx(57,:); 117 | w_o1=xx(58,:); 118 | q_o2=xx(59,:); 119 | w_o2=xx(60,:); 120 | q_o3=xx(61,:); 121 | w_o3=xx(62,:); 122 | Total_veh=(rou_11+rou_12+rou_13+rou_14+rou_15+rou_16+rou_21+rou_22+rou_23+rou_24+rou_25+rou_26+... 123 | rou_31+rou_32+rou_33+rou_34+rou_35+rou_36).*1000./1000.*2+w_o0+w_o1+w_o2+w_o3; 124 | TTS=10/3600.*Total_veh; 125 | % Flow_ave=sum(q_22)/360; 126 | %% 127 | fprintf('TTS is %.3f veh*h \n', sum(TTS)) 128 | % fprintf('Average outflow is %.3f veh*h \n', sum(Flow_ave)) 129 | figure(); 130 | t=1/360:1/360:2.0; 131 | subplot(4,2,1) 132 | plot(t, v_11, '-', 'linewidth', 1.5); 133 | hold on; 134 | plot(t, v_15, '--', 'linewidth', 1.5); 135 | hold on; 136 | plot(t, v_21, ':', 'linewidth', 1.5); 137 | hold on; 138 | plot(t, v_25, '-', 'linewidth', 1.5); 139 | hold on; 140 | plot(t, v_31, '--', 'linewidth', 1.5); 141 | hold on; 142 | plot(t, v_35, ':', 'linewidth', 1.5); 143 | legend('Segmeng 1-1','Segmeng 1-5','Segmeng 2-1','Segmeng 2-5','Segmeng 3-1','Segmeng 3-5') 144 | xlabel('Time [h]'); 145 | ylabel('Speed [km/h]') 146 | 147 | subplot(4,2,2) 148 | plot(t, q_11, '-', 'linewidth', 1.5); 149 | hold on; 150 | plot(t, q_15, '--', 'linewidth', 1.5); 151 | hold on; 152 | plot(t, q_21, ':', 'linewidth', 1.5); 153 | hold on; 154 | plot(t, q_25, '-', 'linewidth', 1.5); 155 | hold on; 156 | plot(t, q_31, '--', 'linewidth', 1.5); 157 | hold on; 158 | plot(t, q_35, ':', 'linewidth', 1.5); 159 | xlabel('Time [h]'); 160 | ylabel('Flow [veh/h]') 161 | 162 | subplot(4,2,3) 163 | plot(t, rou_11, '-', 'linewidth', 1.5); 164 | hold on; 165 | plot(t, rou_15, '--', 'linewidth', 1.5); 166 | hold on; 167 | plot(t, rou_21, ':', 'linewidth', 1.5); 168 | hold on; 169 | plot(t, rou_25, '-', 'linewidth', 1.5); 170 | hold on; 171 | plot(t, rou_31, '--', 'linewidth', 1.5); 172 | hold on; 173 | plot(t, rou_35, ':', 'linewidth', 1.5); 174 | xlabel('Time [h]'); 175 | ylabel('Density [veh/km]') 176 | 177 | subplot(4,2,4) 178 | plot(t, demando0(91:810), '-', 'linewidth', 1.5); 179 | hold on; 180 | plot(t, demando1(91:810), '-.', 'linewidth', 1.5); 181 | hold on; 182 | plot(t, demando2(91:810), ':', 'linewidth', 1.5); 183 | hold on; 184 | plot(t, demando3(91:810), '--', 'linewidth', 1.5); 185 | xlabel('Time [h]'); 186 | ylabel('Demand [veh/h]'); 187 | legend('O_0','O_1','O_2','O_3') 188 | % ylim([0 4000]) 189 | 190 | subplot(4,2,5) 191 | plot(t, q_o0, '-', 'linewidth', 1.5); 192 | hold on; 193 | plot(t, q_o1, '--', 'linewidth', 1.5); 194 | hold on; 195 | plot(t, q_o2, '-.', 'linewidth', 1.5); 196 | hold on; 197 | plot(t, q_o3, ':', 'linewidth', 1.5); 198 | legend('O_0','O_1','O_2','O_3') 199 | xlabel('Time [h]'); 200 | ylabel('Outflow [veh/h]') 201 | % ylim([0 4000]) 202 | 203 | subplot(4,2,6) 204 | plot(t,w_o0,'-', 'linewidth', 1.5); 205 | hold on; 206 | plot(t,w_o1,'--', 'linewidth', 1.5); 207 | hold on; 208 | plot(t,w_o2,'-.', 'linewidth', 1.5); 209 | hold on; 210 | plot(t,w_o3,':', 'linewidth', 1.5); 211 | legend('O_0','O_1','O_2','O_3') 212 | xlabel('Time [h]'); 213 | ylabel('Queue length [veh]') 214 | 215 | subplot(4,2,7) 216 | plot(t,Total_veh,'-', 'linewidth', 1.5); 217 | xlabel('Time [h]'); 218 | ylabel('Total vehicles [veh]') 219 | 220 | subplot(4,2,8) 221 | plot(t,Weather,'-','linewidth', 1.5); 222 | xlabel('Time [h]'); 223 | ylabel('Weather') 224 | 225 | figure 226 | plot(t, demando0(61:780), '-', 'linewidth', 1.5); 227 | hold on; 228 | plot(t, demando1(61:780), '-.', 'linewidth', 1.5); 229 | hold on; 230 | plot(t, demando2(61:780), ':', 'linewidth', 1.5); 231 | hold on; 232 | plot(t, demando3(61:780), '--', 'linewidth', 1.5); 233 | xlabel('Time [h]'); 234 | ylabel('Demand [veh/h]'); 235 | ylim([0 3500]); 236 | legend('O_0','O_1','O_2','O_3') -------------------------------------------------------------------------------- /DQN_PMPC_validate.m: -------------------------------------------------------------------------------- 1 | % clear; 2 | % clc 3 | % close all 4 | %% Initialize 5 | Exit=[]; 6 | x=[zeros(62,1);0]; 7 | % ramp_only=0; 8 | u=[102,102,102,102,102,102,102,102,102,1,1,1]; 9 | initial_weather=3; 10 | % weather=initial_weather; % taken as the nominal weather 11 | end_weather=1; 12 | Weather_real=[ones(1,60) initial_weather*ones(1,360) end_weather*ones(1,510)]; 13 | Weather_predict=[ones(1,60) initial_weather*ones(1,870)]; 14 | Weather=Weather_predict; 15 | for i=1:60 16 | x=Freeway_model(x,u,1); % real model use the real weather 17 | end 18 | 19 | %% Parameters 20 | Np=3; % Np times M is the prediction horizon 21 | Nc=3; % Nc is the variables of theta during the prediction 22 | M=30; % MPC operation time: 30 simulation steps 23 | Mc=6; % control sampling time: 6 simulation steps 24 | N=720; 25 | % N_max=15; % multi-start point 26 | xx=[]; 27 | % [tao, kai, yita_high, yita_low, rou_max, sigma, am, v_free, rou_crit, alpha, T, lambda, Lm, ~, v_min, Co2, xi_r, xi_s,xi_theta,~] = parameters_real(weather); 28 | % dim_speed=6; 29 | % dim_ramp=3; 30 | % para=[Lm, lambda, v_free, M, dim_speed, T, xi_r, xi_s, weather, rou_crit, Mc, xi_theta]; 31 | % options = optimoptions(@fmincon,'Algorithm','sqp','Display','off','TolFun',1e-2, 'TolX',1e-2, 'TolCon', 1e-2); 32 | % 33 | % % v_control=repmat(100*ones(dim_speed,1),1,Nc); 34 | % % v_control_pre=v_control(:,1); 35 | % 36 | dim_theta=1; 37 | U=[]; 38 | Theta=zeros(dim_theta,N); 39 | u_pre=u'; % 40 | % % theta_lb=repmat([0;-100;-100;0],1,Nc); 41 | % % theta_ub=repmat([2;100;100;10],1,Nc); 42 | % % theta_pre=repmat(ones(dim_theta,1), 1, Nc); 43 | % theta_lb=repmat(-10,dim_theta,Nc); 44 | % theta_ub=repmat(10,dim_theta,Nc); 45 | theta_pre=repmat(ones(dim_theta,1), 1, Nc); 46 | % Rou=repelem([35 15], 1, 12); 47 | %% 48 | for i=1:N/M 49 | weather=Weather(x(63)+1); 50 | [tao, kai, yita_high, yita_low, rou_max, sigma, am, v_free, ~, alpha, T, lambda, Lm, ~, v_min, Co2, xi_r, xi_s,xi_theta,~] ... 51 | = parameters_predict(weather); 52 | % rou_crit=36.5; 53 | %% DQN action 54 | if rem(i,6)==1 55 | rou_13=x(7); 56 | v_13=x(8); 57 | rou_14=x(10); 58 | v_14=x(11); 59 | rou_15=x(13); 60 | v_15=x(14); 61 | rou_23=x(25); 62 | v_23=x(26); 63 | rou_24=x(28); 64 | v_24=x(29); 65 | rou_25=x(31); 66 | v_25=x(32); 67 | rou_33=x(43); 68 | v_33=x(44); 69 | rou_34=x(46); 70 | v_34=x(47); 71 | rou_35=x(49); 72 | v_35=x(50); 73 | 74 | w_o0=x(56,:); 75 | w_o1=x(58); 76 | w_o2=x(60); 77 | w_o3=x(62); 78 | k=x(63); 79 | norm_state=[rou_13 v_13 rou_14 v_14 rou_15 v_15 rou_23 v_23 rou_24 v_24 rou_25 v_25 rou_33 v_33 rou_34 v_34 rou_35 v_35]'/100; 80 | norm_queue=[w_o0;w_o1;w_o2;w_o3]/100; 81 | norm_demand=[demando0(k)/1000; demando1(k)/1000; demando2(k)/1000; demando3(k)/1000]; 82 | Observation=[norm_state; norm_demand;norm_queue;u_pre(10:12);theta_pre(:,1);rou_36_down(k)/100;Weather_real(k+1)]; 83 | act = getAction(saved_agent, Observation); 84 | Rou=[15 17.5 20.0 22.5 25.0 27.5 30.0 32.5 35.0 37.5 40.0]; 85 | rou_crit=Rou(act{1}+1); 86 | % rou_crit=20.4750; 87 | % rou_crit=Rou(i); 88 | end 89 | theta_opt=MPC_imp_RM(x,u_pre,theta_pre, Weather_predict,rou_crit); % give the wrong weather, but rou_crit can be adjusted 90 | for j=1:M/Mc % every control step 91 | u=u_para_RM(theta_opt(:,1), x, u_pre, rou_crit, v_free); 92 | for k=1:Mc 93 | x=Freeway_model(x,u,Weather_real(x(63)+1)); 94 | xx=[xx x]; 95 | % step=x(63)-60; 96 | % weather=(end_weather-initial_weather)/720*step+initial_weather; 97 | end 98 | u_pre=u; 99 | U=[U repmat(u,1,Mc)]; 100 | end 101 | 102 | Theta(:,M*(i-1)+1:M*i)=repmat(theta_opt(:,1),1,M); 103 | % theta_pre=theta_t{index}; 104 | theta_pre=theta_opt; 105 | end 106 | %% figure 107 | u_speed=U(1:6,:); 108 | u_ramp=U(7:9,:); 109 | rou_11=xx(1,:); 110 | v_11=xx(2,:); 111 | q_11=xx(3,:); 112 | rou_12=xx(4,:); 113 | v_12=xx(5,:); 114 | q_12=xx(6,:); 115 | rou_13=xx(7,:); 116 | v_13=xx(8,:); 117 | q_13=xx(9,:); 118 | rou_14=xx(10,:); 119 | v_14=xx(11,:); 120 | q_14=xx(12,:); 121 | rou_15=xx(13,:); 122 | v_15=xx(14,:); 123 | q_15=xx(15,:); 124 | rou_16=xx(16,:); 125 | v_16=xx(17,:); 126 | q_16=xx(18,:); 127 | rou_21=xx(19,:); 128 | v_21=xx(20,:); 129 | q_21=xx(21,:); 130 | rou_22=xx(22,:); 131 | v_22=xx(23,:); 132 | q_22=xx(24,:); 133 | rou_23=xx(25,:); 134 | v_23=xx(26,:); 135 | q_23=xx(27,:); 136 | rou_24=xx(28,:); 137 | v_24=xx(29,:); 138 | q_24=xx(30,:); 139 | rou_25=xx(31,:); 140 | v_25=xx(32,:); 141 | q_25=xx(33,:); 142 | rou_26=xx(34,:); 143 | v_26=xx(35,:); 144 | q_26=xx(36,:); 145 | rou_31=xx(37,:); 146 | v_31=xx(38,:); 147 | q_31=xx(39,:); 148 | rou_32=xx(40,:); 149 | v_32=xx(41,:); 150 | q_32=xx(42,:); 151 | rou_33=xx(43,:); 152 | v_33=xx(44,:); 153 | q_33=xx(45,:); 154 | rou_34=xx(46,:); 155 | v_34=xx(47,:); 156 | q_34=xx(48,:); 157 | rou_35=xx(49,:); 158 | v_35=xx(50,:); 159 | q_35=xx(51,:); 160 | rou_36=xx(52,:); 161 | v_36=xx(53,:); 162 | q_36=xx(54,:); 163 | q_o0=xx(55,:); 164 | w_o0=xx(56,:); 165 | q_o1=xx(57,:); 166 | w_o1=xx(58,:); 167 | q_o2=xx(59,:); 168 | w_o2=xx(60,:); 169 | q_o3=xx(61,:); 170 | w_o3=xx(62,:); 171 | Total_veh=(rou_11+rou_12+rou_13+rou_14+rou_15+rou_16+rou_21+rou_22+rou_23+rou_24+rou_25+rou_26+... 172 | rou_31+rou_32+rou_33+rou_34+rou_35+rou_36).*1000./1000.*2+w_o0+w_o1+w_o2+w_o3; 173 | TTS=10/3600.*Total_veh; 174 | %% 175 | fprintf('TTS is %.3f veh*h \n', sum(TTS)) 176 | % figure(); 177 | % t=1/360:1/360:2.0; 178 | % subplot(4,2,1) 179 | % plot(t, v_11, '-', 'linewidth', 1.5); 180 | % hold on; 181 | % plot(t, v_15, '--', 'linewidth', 1.5); 182 | % hold on; 183 | % plot(t, v_21, ':', 'linewidth', 1.5); 184 | % hold on; 185 | % plot(t, v_25, '-', 'linewidth', 1.5); 186 | % hold on; 187 | % plot(t, v_31, '--', 'linewidth', 1.5); 188 | % hold on; 189 | % plot(t, v_35, ':', 'linewidth', 1.5); 190 | % legend('Segmeng 1-1','Segmeng 1-5','Segmeng 2-1','Segmeng 2-5','Segmeng 3-1','Segmeng 3-5') 191 | % xlabel('Time [h]'); 192 | % ylabel('Speed [km/h]') 193 | % 194 | % subplot(4,2,2) 195 | % plot(t, q_11, '-', 'linewidth', 1.5); 196 | % hold on; 197 | % plot(t, q_15, '--', 'linewidth', 1.5); 198 | % hold on; 199 | % plot(t, q_21, ':', 'linewidth', 1.5); 200 | % hold on; 201 | % plot(t, q_25, '-', 'linewidth', 1.5); 202 | % hold on; 203 | % plot(t, q_31, '--', 'linewidth', 1.5); 204 | % hold on; 205 | % plot(t, q_35, ':', 'linewidth', 1.5); 206 | % xlabel('Time [h]'); 207 | % ylabel('Flow [veh/h]') 208 | % 209 | % subplot(4,2,3) 210 | % plot(t, rou_11, '-', 'linewidth', 1.5); 211 | % hold on; 212 | % plot(t, rou_15, '--', 'linewidth', 1.5); 213 | % hold on; 214 | % plot(t, rou_21, ':', 'linewidth', 1.5); 215 | % hold on; 216 | % plot(t, rou_25, '-', 'linewidth', 1.5); 217 | % hold on; 218 | % plot(t, rou_31, '--', 'linewidth', 1.5); 219 | % hold on; 220 | % plot(t, rou_35, ':', 'linewidth', 1.5); 221 | % xlabel('Time [h]'); 222 | % ylabel('Density [veh/km]') 223 | % 224 | % subplot(4,2,4) 225 | % subplot(3,1,1) 226 | % plot(t,Theta, 'linewidth', 1.5); 227 | % xlabel('Time [h]'); 228 | % ylabel('Theta'); 229 | % legend('0','1','2','3') 230 | % % ylim([0 4000]) 231 | % 232 | % subplot(4,2,5) 233 | % plot(t, q_o0, '-', 'linewidth', 1.5); 234 | % hold on; 235 | % plot(t, q_o1, '--', 'linewidth', 1.5); 236 | % hold on; 237 | % plot(t, q_o2, '-.', 'linewidth', 1.5); 238 | % hold on; 239 | % plot(t, q_o3, ':', 'linewidth', 1.5); 240 | % legend('O_0','O_1','O_2','O_3') 241 | % xlabel('Time [h]'); 242 | % ylabel('Outflow [veh/h]') 243 | % % ylim([0 4000]) 244 | % 245 | % subplot(4,2,6) 246 | % plot(t,w_o0,'-', 'linewidth', 1.5); 247 | % hold on; 248 | % plot(t,w_o1,'--', 'linewidth', 1.5); 249 | % hold on; 250 | % plot(t,w_o2,'-.', 'linewidth', 1.5); 251 | % hold on; 252 | % plot(t,w_o3,':', 'linewidth', 1.5); 253 | % legend('O_0','O_1','O_2','O_3') 254 | % xlabel('Time [h]'); 255 | % ylabel('Queue length [veh]') 256 | % 257 | % subplot(4,2,7) 258 | % subplot(3,1,2) 259 | % plot(t,u_speed,'-', 'linewidth', 1.5); 260 | % legend('v-11','v-12','v-21','v-22','v-31','v-32'); 261 | % xlabel('Time [h]'); 262 | % ylabel('Speed limits') 263 | % % 264 | % % subplot(4,2,8) 265 | % subplot(3,1,3) 266 | % plot(t,u_ramp,'-','linewidth',1.0); 267 | % legend('O_1','O_2','O_3'); 268 | % ylim([0 1]); 269 | % xlabel('Time [h]'); 270 | % ylabel('Ramp metering') 271 | %% 272 | function J=ModelState(x, theta, Nc, Np, para, u_pre, theta_pre) 273 | Lm=para(1); 274 | Lambda=para(2); 275 | v_free=para(3); 276 | M=para(4); 277 | dim_speed=para(5); 278 | T=para(6); 279 | xi_r=para(7); 280 | xi_s=para(8); 281 | weather=para(9); 282 | rou_crit=para(10); 283 | Mc=para(11); 284 | xi_theta=para(12); 285 | % uc=[u repelem(u(:, end),1,Np-Nc)]; 286 | % uc_full=repelem(uc,1,M); 287 | theta_full=repelem(theta,1,Np/Nc); 288 | yy=[]; 289 | Diff=zeros(size(u_pre,1),Np); % the erros between the successive control inputs 290 | Diff_theta=zeros(size(theta_pre,1),Nc); 291 | uc_full=zeros(size(u_pre,1),Np); 292 | for i=1:Np 293 | for j=1:M/Mc 294 | u=u_para(theta_full(:,i), x, u_pre, rou_crit); 295 | uc_full(:,i)=u; 296 | for k=1:Mc 297 | % step=x(63)-60; 298 | % initial_weather=0; 299 | % end_weather=2; 300 | % weather=(end_weather-initial_weather)/720*step+initial_weather; 301 | x=Freeway_model(x,u,weather,0); 302 | yy=[yy x]; 303 | end 304 | u_pre=u; 305 | end 306 | end 307 | u_diff=[u_pre uc_full]; 308 | theta_diff=[theta_pre theta]; 309 | % for i=1:Np 310 | % Diff(:,i)=u_diff(:,i+1)-u_diff(:,i); 311 | % end 312 | for i=1:Nc 313 | Diff_theta(:,i)=theta_diff(:,i+1)-theta_diff(:,i); 314 | end 315 | rou_11=yy(1,:);rou_12=yy(4,:);rou_13=yy(7,:);rou_14=yy(10,:);rou_15=yy(13,:);rou_16=yy(16,:); 316 | rou_21=yy(19,:);rou_22=yy(22,:);rou_23=yy(25,:);rou_24=yy(28,:);rou_25=yy(31,:);rou_26=yy(34,:); 317 | rou_31=yy(37,:);rou_32=yy(40,:);rou_33=yy(43,:);rou_34=yy(46,:);rou_35=yy(49,:);rou_36=yy(52,:); 318 | w_o0=yy(56,:);w_o1=yy(58,:);w_o2=yy(60,:);w_o3=yy(62,:); 319 | TTS=(sum((rou_11+rou_12+rou_13+rou_14+rou_15+rou_16+rou_21+rou_22+rou_23+rou_24+rou_25+rou_26... 320 | +rou_31+rou_32+rou_33+rou_34+rou_35+rou_36))*Lm/1000*Lambda+sum(w_o0+w_o1+w_o2+w_o3))*T/3600; 321 | % Diff_speed=Diff(1:dim_speed,:); 322 | % Diff_r=Diff(dim_speed+1:end,:); 323 | % penalty=xi_r*sum(sum(Diff_r.^2))+xi_s*sum(sum((Diff_speed/v_free).^2)); 324 | penalty=xi_theta*sum(sum(Diff_theta.^2)); 325 | J=TTS+penalty; 326 | end 327 | function [c, ceq]=ModelCons(x, theta, Nc, Np, para,u_pre) 328 | ceq=[]; 329 | M=para(4); 330 | weather=para(9); 331 | rou_crit=para(10); 332 | % uc=[theta repelem(theta(:, end),1,Np-Nc)]; 333 | % uc_full=repelem(uc,1,M); 334 | yy=zeros(size(x,1),Np*M); 335 | theta_full=repelem(theta,1,Np/Nc); 336 | u_vsl=zeros(6, Np); 337 | u_rm=zeros(3,Np); 338 | for i=1:Np 339 | u=u_para(theta_full(:,i), x, u_pre, rou_crit); 340 | u_vsl(:,i)=u(1:6); 341 | u_rm(:,i)=u(7:9); 342 | for j=1:M 343 | x=Freeway_model(x,u,weather); 344 | yy(:,j+M*(i-1))=x; 345 | end 346 | end 347 | w_o0=yy(56,:);w_o1=yy(58,:); 348 | w_o2=yy(60,:);w_o3=yy(62,:); 349 | c=[w_o0'-100;w_o1'-100;w_o2'-100;w_o3'-100]; 350 | % u_vsl_full=u_vsl(:); 351 | % u_rm_full=u_rm(:); 352 | % c=[u_vsl_full-102;20-u_vsl_full;u_rm_full-1;-u_rm_full]; 353 | end 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | -------------------------------------------------------------------------------- /DQN_RM_reset.m: -------------------------------------------------------------------------------- 1 | function [InitialObservation,LoggedSignals] = DQN_RM_reset 2 | initial_weather=1; 3 | end_weather=3; 4 | Weather_real=[ones(1,60) initial_weather*ones(1,360) end_weather*ones(1,510)]; 5 | Weather_predict=[ones(1,60) initial_weather*ones(1,870)]; 6 | dim_theta=1; 7 | Nc=3; 8 | x=[zeros(62,1);0]; 9 | u_rm_initial=[1 1 1]'; 10 | u_initial=[102; 102; 102; 102; 102; 102; 102; 102; 102; u_rm_initial]; 11 | [~, ~, ~, ~, ~, ~, ~, ~, rou_crit, ~, ~, ~, ~, ~, ~, ~, ~, ~,~,~] = parameters_real(initial_weather); 12 | for i=1:60 13 | x=Freeway_model(x, u_initial, 1); 14 | end 15 | theta_pre=repmat(ones(dim_theta,1), 1, Nc); 16 | u_pre=u_initial; 17 | theta_mpc=MPC_imp_RM(x, u_pre, theta_pre, Weather_real, rou_crit); 18 | 19 | k=x(63); 20 | rou_11=x(1); 21 | v_11=x(2); 22 | q_11=x(3); 23 | rou_12=x(4); 24 | v_12=x(5); 25 | q_12=x(6); 26 | rou_13=x(7); 27 | v_13=x(8); 28 | q_13=x(9); 29 | rou_14=x(10); 30 | v_14=x(11); 31 | q_14=x(12); 32 | rou_15=x(13); 33 | v_15=x(14); 34 | q_15=x(15); 35 | rou_16=x(16); 36 | v_16=x(17); 37 | q_16=x(18); 38 | rou_21=x(19); 39 | v_21=x(20); 40 | q_21=x(21); 41 | rou_22=x(22); 42 | v_22=x(23); 43 | q_22=x(24); 44 | rou_23=x(25); 45 | v_23=x(26); 46 | q_23=x(27); 47 | rou_24=x(28); 48 | v_24=x(29); 49 | q_24=x(30); 50 | rou_25=x(31); 51 | v_25=x(32); 52 | q_25=x(33); 53 | rou_26=x(34); 54 | v_26=x(35); 55 | q_26=x(36); 56 | rou_31=x(37); 57 | v_31=x(38); 58 | q_31=x(39); 59 | rou_32=x(40); 60 | v_32=x(41); 61 | q_32=x(42); 62 | rou_33=x(43); 63 | v_33=x(44); 64 | q_33=x(45); 65 | rou_34=x(46); 66 | v_34=x(47); 67 | q_34=x(48); 68 | rou_35=x(49); 69 | v_35=x(50); 70 | q_35=x(51); 71 | rou_36=x(52); 72 | v_36=x(53); 73 | q_36=x(54); 74 | w_o0=x(56,:); 75 | w_o1=x(58); 76 | w_o2=x(60); 77 | w_o3=x(62); 78 | 79 | norm_state=[rou_13 v_13 rou_14 v_14 rou_15 v_15 rou_23 v_23 rou_24 v_24 rou_25 v_25 rou_33 v_33 rou_34 v_34 rou_35 v_35]'/100; 80 | norm_queue=[w_o0;w_o1;w_o2;w_o3]/100; 81 | norm_demand=[demando0(k)/1000; demando1(k)/1000; demando2(k)/1000; demando3(k)/1000]; 82 | 83 | LoggedSignals.k=k; 84 | LoggedSignals.x=x; 85 | LoggedSignals.theta_mpc=theta_mpc; 86 | LoggedSignals.u_pre=u_pre; 87 | LoggedSignals.Weather_real=Weather_real; 88 | LoggedSignals.Weather_predict=Weather_predict; 89 | InitialObservation=[norm_state; norm_demand;norm_queue;u_rm_initial;theta_mpc(:,1);rou_36_down(k)/100;Weather_real(k+1)]; 90 | end 91 | 92 | -------------------------------------------------------------------------------- /DQN_RM_step.m: -------------------------------------------------------------------------------- 1 | function [Observation,Reward,IsDone,LoggedSignals] = DQN_RM_step(u_rou,LoggedSignals) 2 | k=LoggedSignals.k; 3 | x=LoggedSignals.x; 4 | Weather_real=LoggedSignals.Weather_real; 5 | Weather_predict=LoggedSignals.Weather_predict; 6 | u_pre=LoggedSignals.u_pre; 7 | theta_mpc=LoggedSignals.theta_mpc; 8 | % rou_crit=33.5; 9 | xx=zeros(63,180); 10 | % u_theta=sat_theta(u_theta); % define sat_theta 11 | % step=k-60; 12 | % weather=(end_weather-initial_weather)/720*step+initial_weather; 13 | % implememt u_theta 14 | % weather=initial_weather; 15 | Rou=[15 17.5 20.0 22.5 25.0 27.5 30.0 32.5 35.0 37.5 40.0]; 16 | rou_crit=Rou(u_rou+1); 17 | % rou_crit=33.5; 18 | for m=1:6 19 | theta_mpc=MPC_imp_RM(x, u_pre, theta_mpc, Weather_predict, rou_crit); % new mpc input; fake weather condition 20 | u_theta=theta_mpc(:,1); 21 | for i=1:5 % MPC operation time: 5 min 22 | u_vslrm=u_para_RM(u_theta, x, u_pre, rou_crit); 23 | % implement u_vslrm 24 | for j=1:6 % control sampling time: 1min 25 | x=Freeway_model_predict(x,u_vslrm, Weather_real(k+1)); % use the true weather condition 26 | k=k+1; 27 | % step=k-60; 28 | % weather=(end_weather-initial_weather)/720*step+initial_weather; 29 | xx(:,j+6*(i-1)+30*(m-1))=x; 30 | end 31 | u_pre=u_vslrm; 32 | end 33 | end 34 | % calculate the mpc input for next step 35 | 36 | % calculate the reward: TTS 37 | rou_11=xx(1,:);rou_12=xx(4,:);rou_13=xx(7,:);rou_14=xx(10,:);rou_15=xx(13,:);rou_16=xx(16,:); 38 | rou_21=xx(19,:);rou_22=xx(22,:);rou_23=xx(25,:);rou_24=xx(28,:);rou_25=xx(31,:);rou_26=xx(34,:); 39 | rou_31=xx(37,:);rou_32=xx(40,:);rou_33=xx(43,:);rou_34=xx(46,:);rou_35=xx(49,:);rou_36=xx(52,:); 40 | w_o0=xx(56,:);w_o1=xx(58,:);w_o2=xx(60,:);w_o3=xx(62,:); 41 | TTS=(sum((rou_11+rou_12+rou_13+rou_14+rou_15+rou_16+rou_21+rou_22+rou_23+rou_24+rou_25+rou_26... 42 | +rou_31+rou_32+rou_33+rou_34+rou_35+rou_36))*1000/1000*2+sum(w_o0+w_o1+w_o2+w_o3))*10/3600; 43 | Reward=-TTS; 44 | 45 | rou_11=x(1); 46 | v_11=x(2); 47 | q_11=x(3); 48 | rou_12=x(4); 49 | v_12=x(5); 50 | q_12=x(6); 51 | rou_13=x(7); 52 | v_13=x(8); 53 | q_13=x(9); 54 | rou_14=x(10); 55 | v_14=x(11); 56 | q_14=x(12); 57 | rou_15=x(13); 58 | v_15=x(14); 59 | q_15=x(15); 60 | rou_16=x(16); 61 | v_16=x(17); 62 | q_16=x(18); 63 | rou_21=x(19); 64 | v_21=x(20); 65 | q_21=x(21); 66 | rou_22=x(22); 67 | v_22=x(23); 68 | q_22=x(24); 69 | rou_23=x(25); 70 | v_23=x(26); 71 | q_23=x(27); 72 | rou_24=x(28); 73 | v_24=x(29); 74 | q_24=x(30); 75 | rou_25=x(31); 76 | v_25=x(32); 77 | q_25=x(33); 78 | rou_26=x(34); 79 | v_26=x(35); 80 | q_26=x(36); 81 | rou_31=x(37); 82 | v_31=x(38); 83 | q_31=x(39); 84 | rou_32=x(40); 85 | v_32=x(41); 86 | q_32=x(42); 87 | rou_33=x(43); 88 | v_33=x(44); 89 | q_33=x(45); 90 | rou_34=x(46); 91 | v_34=x(47); 92 | q_34=x(48); 93 | rou_35=x(49); 94 | v_35=x(50); 95 | q_35=x(51); 96 | rou_36=x(52); 97 | v_36=x(53); 98 | q_36=x(54); 99 | w_o0=x(56,:); 100 | w_o1=x(58); 101 | w_o2=x(60); 102 | w_o3=x(62); 103 | 104 | norm_state=[rou_13 v_13 rou_14 v_14 rou_15 v_15 rou_23 v_23 rou_24 v_24 rou_25 v_25 rou_33 v_33 rou_34 v_34 rou_35 v_35]'/100; 105 | norm_queue=[w_o0;w_o1;w_o2;w_o3]/100; 106 | norm_demand=[demando0(k)/1000; demando1(k)/1000; demando2(k)/1000; demando3(k)/1000]; 107 | Observation=[norm_state; norm_demand;norm_queue;u_vslrm(10:12);theta_mpc(:,1);rou_36_down(k)/100;Weather_real(k+1)]; 108 | 109 | if k>779 110 | IsDone=true; 111 | else 112 | IsDone=false; 113 | end 114 | 115 | LoggedSignals.k=k; 116 | LoggedSignals.x=x; 117 | LoggedSignals.theta_mpc=theta_mpc; 118 | LoggedSignals.u_pre=u_pre; 119 | LoggedSignals.Weather_real=Weather_real; 120 | LoggedSignals.Weather_predict=Weather_predict; 121 | end 122 | 123 | -------------------------------------------------------------------------------- /DQN_RM_validate.m: -------------------------------------------------------------------------------- 1 | % clear; 2 | % clc 3 | % close all 4 | %% Initialize 5 | Exit=[]; 6 | x=[zeros(62,1);0]; 7 | % ramp_only=0; 8 | u=[102,102,102,102,102,102,102,102,102,1,1,1]; 9 | initial_weather=2; 10 | weather=initial_weather; % taken as the nominal weather 11 | end_weather=1; 12 | nominal_model=1; 13 | for i=1:60 14 | x=Freeway_model(x,u,1); % real model use the real weather 15 | end 16 | 17 | %% Parameters 18 | Np=3; % Np times M is the prediction horizon 19 | Nc=3; % Nc is the variables of theta during the prediction 20 | M=30; % MPC operation time: 30 simulation steps 21 | Mc=6; % control sampling time: 6 simulation steps 22 | N=720; 23 | % N_max=15; % multi-start point 24 | xx=[]; 25 | % [tao, kai, yita_high, yita_low, rou_max, sigma, am, v_free, rou_crit, alpha, T, lambda, Lm, ~, v_min, Co2, xi_r, xi_s,xi_theta,~] = parameters_real(weather); 26 | % dim_speed=6; 27 | % dim_ramp=3; 28 | % para=[Lm, lambda, v_free, M, dim_speed, T, xi_r, xi_s, weather, rou_crit, Mc, xi_theta]; 29 | % options = optimoptions(@fmincon,'Algorithm','sqp','Display','off','TolFun',1e-2, 'TolX',1e-2, 'TolCon', 1e-2); 30 | % 31 | % % v_control=repmat(100*ones(dim_speed,1),1,Nc); 32 | % % v_control_pre=v_control(:,1); 33 | % 34 | dim_theta=1; 35 | U=[]; 36 | Theta=zeros(dim_theta,N); 37 | u_pre=u'; % 38 | rm_pre=1; 39 | % % theta_lb=repmat([0;-100;-100;0],1,Nc); 40 | % % theta_ub=repmat([2;100;100;10],1,Nc); 41 | % % theta_pre=repmat(ones(dim_theta,1), 1, Nc); 42 | % theta_lb=repmat(-10,dim_theta,Nc); 43 | % theta_ub=repmat(10,dim_theta,Nc); 44 | theta_pre=repmat(ones(dim_theta,1), 1, Nc); 45 | % Rou=repelem([35 15], 1, 12); 46 | %% 47 | for i=1:N/Mc 48 | if x(63)>=420 49 | weather=end_weather; 50 | end 51 | %% DQN action 52 | rou_13=x(7); 53 | v_13=x(8); 54 | rou_14=x(10); 55 | v_14=x(11); 56 | rou_15=x(13); 57 | v_15=x(14); 58 | rou_23=x(25); 59 | v_23=x(26); 60 | rou_24=x(28); 61 | v_24=x(29); 62 | rou_25=x(31); 63 | v_25=x(32); 64 | rou_33=x(43); 65 | v_33=x(44); 66 | rou_34=x(46); 67 | v_34=x(47); 68 | rou_35=x(49); 69 | v_35=x(50); 70 | 71 | w_o0=x(56,:); 72 | w_o1=x(58); 73 | w_o2=x(60); 74 | w_o3=x(62); 75 | k=x(63); 76 | norm_state=[rou_13 v_13 rou_14 v_14 rou_15 v_15 rou_23 v_23 rou_24 v_24 rou_25 v_25 rou_33 v_33 rou_34 v_34 rou_35 v_35]'/100; 77 | norm_queue=[w_o0;w_o1;w_o2;w_o3]/100; 78 | norm_demand=[demando0(k)/1000; demando1(k)/1000; demando2(k)/1000; demando3(k)/1000]; 79 | Observation=[norm_state; norm_demand;norm_queue;rm_pre;rou_36_down(k)/100;weather]; 80 | act = getAction(agent, Observation); 81 | u_rm=act{1}/10; 82 | u=[102; 102; 102; 102; 102; 102; 102; 102; 102; u_rm; u_rm; u_rm]; 83 | for k=1:Mc 84 | x=Freeway_model(x,u,weather); 85 | xx=[xx x]; 86 | end 87 | u_pre=u; 88 | rm_pre=u_rm; 89 | U=[U repmat(u,1,Mc)]; 90 | end 91 | %% figure 92 | u_speed=U(1:9,:); 93 | u_ramp=U(10:12,:); 94 | rou_11=xx(1,:); 95 | v_11=xx(2,:); 96 | q_11=xx(3,:); 97 | rou_12=xx(4,:); 98 | v_12=xx(5,:); 99 | q_12=xx(6,:); 100 | rou_13=xx(7,:); 101 | v_13=xx(8,:); 102 | q_13=xx(9,:); 103 | rou_14=xx(10,:); 104 | v_14=xx(11,:); 105 | q_14=xx(12,:); 106 | rou_15=xx(13,:); 107 | v_15=xx(14,:); 108 | q_15=xx(15,:); 109 | rou_16=xx(16,:); 110 | v_16=xx(17,:); 111 | q_16=xx(18,:); 112 | rou_21=xx(19,:); 113 | v_21=xx(20,:); 114 | q_21=xx(21,:); 115 | rou_22=xx(22,:); 116 | v_22=xx(23,:); 117 | q_22=xx(24,:); 118 | rou_23=xx(25,:); 119 | v_23=xx(26,:); 120 | q_23=xx(27,:); 121 | rou_24=xx(28,:); 122 | v_24=xx(29,:); 123 | q_24=xx(30,:); 124 | rou_25=xx(31,:); 125 | v_25=xx(32,:); 126 | q_25=xx(33,:); 127 | rou_26=xx(34,:); 128 | v_26=xx(35,:); 129 | q_26=xx(36,:); 130 | rou_31=xx(37,:); 131 | v_31=xx(38,:); 132 | q_31=xx(39,:); 133 | rou_32=xx(40,:); 134 | v_32=xx(41,:); 135 | q_32=xx(42,:); 136 | rou_33=xx(43,:); 137 | v_33=xx(44,:); 138 | q_33=xx(45,:); 139 | rou_34=xx(46,:); 140 | v_34=xx(47,:); 141 | q_34=xx(48,:); 142 | rou_35=xx(49,:); 143 | v_35=xx(50,:); 144 | q_35=xx(51,:); 145 | rou_36=xx(52,:); 146 | v_36=xx(53,:); 147 | q_36=xx(54,:); 148 | q_o0=xx(55,:); 149 | w_o0=xx(56,:); 150 | q_o1=xx(57,:); 151 | w_o1=xx(58,:); 152 | q_o2=xx(59,:); 153 | w_o2=xx(60,:); 154 | q_o3=xx(61,:); 155 | w_o3=xx(62,:); 156 | Total_veh=(rou_11+rou_12+rou_13+rou_14+rou_15+rou_16+rou_21+rou_22+rou_23+rou_24+rou_25+rou_26+... 157 | rou_31+rou_32+rou_33+rou_34+rou_35+rou_36).*1000./1000.*2+w_o0+w_o1+w_o2+w_o3; 158 | TTS=10/3600.*Total_veh; 159 | %% 160 | fprintf('TTS is %.3f veh*h \n', sum(TTS)) 161 | figure(); 162 | t=1/360:1/360:2.0; 163 | subplot(4,2,1) 164 | plot(t, v_11, '-', 'linewidth', 1.5); 165 | hold on; 166 | plot(t, v_15, '--', 'linewidth', 1.5); 167 | hold on; 168 | plot(t, v_21, ':', 'linewidth', 1.5); 169 | hold on; 170 | plot(t, v_25, '-', 'linewidth', 1.5); 171 | hold on; 172 | plot(t, v_31, '--', 'linewidth', 1.5); 173 | hold on; 174 | plot(t, v_35, ':', 'linewidth', 1.5); 175 | legend('Segmeng 1-1','Segmeng 1-5','Segmeng 2-1','Segmeng 2-5','Segmeng 3-1','Segmeng 3-5') 176 | xlabel('Time [h]'); 177 | ylabel('Speed [km/h]') 178 | 179 | subplot(4,2,2) 180 | plot(t, q_11, '-', 'linewidth', 1.5); 181 | hold on; 182 | plot(t, q_15, '--', 'linewidth', 1.5); 183 | hold on; 184 | plot(t, q_21, ':', 'linewidth', 1.5); 185 | hold on; 186 | plot(t, q_25, '-', 'linewidth', 1.5); 187 | hold on; 188 | plot(t, q_31, '--', 'linewidth', 1.5); 189 | hold on; 190 | plot(t, q_35, ':', 'linewidth', 1.5); 191 | xlabel('Time [h]'); 192 | ylabel('Flow [veh/h]') 193 | 194 | subplot(4,2,3) 195 | plot(t, rou_11, '-', 'linewidth', 1.5); 196 | hold on; 197 | plot(t, rou_15, '--', 'linewidth', 1.5); 198 | hold on; 199 | plot(t, rou_21, ':', 'linewidth', 1.5); 200 | hold on; 201 | plot(t, rou_25, '-', 'linewidth', 1.5); 202 | hold on; 203 | plot(t, rou_31, '--', 'linewidth', 1.5); 204 | hold on; 205 | plot(t, rou_35, ':', 'linewidth', 1.5); 206 | xlabel('Time [h]'); 207 | ylabel('Density [veh/km]') 208 | 209 | subplot(4,2,4) 210 | % subplot(3,1,1) 211 | plot(t,Theta, 'linewidth', 1.5); 212 | xlabel('Time [h]'); 213 | ylabel('Theta'); 214 | legend('0','1','2','3') 215 | % ylim([0 4000]) 216 | 217 | subplot(4,2,5) 218 | plot(t, q_o0, '-', 'linewidth', 1.5); 219 | hold on; 220 | plot(t, q_o1, '--', 'linewidth', 1.5); 221 | hold on; 222 | plot(t, q_o2, '-.', 'linewidth', 1.5); 223 | hold on; 224 | plot(t, q_o3, ':', 'linewidth', 1.5); 225 | legend('O_0','O_1','O_2','O_3') 226 | xlabel('Time [h]'); 227 | ylabel('Outflow [veh/h]') 228 | % ylim([0 4000]) 229 | 230 | subplot(4,2,6) 231 | plot(t,w_o0,'-', 'linewidth', 1.5); 232 | hold on; 233 | plot(t,w_o1,'--', 'linewidth', 1.5); 234 | hold on; 235 | plot(t,w_o2,'-.', 'linewidth', 1.5); 236 | hold on; 237 | plot(t,w_o3,':', 'linewidth', 1.5); 238 | legend('O_0','O_1','O_2','O_3') 239 | xlabel('Time [h]'); 240 | ylabel('Queue length [veh]') 241 | 242 | subplot(4,2,7) 243 | % subplot(3,1,2) 244 | plot(t,u_speed,'-', 'linewidth', 1.5); 245 | legend('v-11','v-12','v-21','v-22','v-31','v-32'); 246 | xlabel('Time [h]'); 247 | ylabel('Speed limits') 248 | % 249 | subplot(4,2,8) 250 | % subplot(3,1,3) 251 | plot(t,u_ramp,'-','linewidth',1.0); 252 | legend('O_1','O_2','O_3'); 253 | ylim([0 1]); 254 | xlabel('Time [h]'); 255 | ylabel('Ramp metering') 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | -------------------------------------------------------------------------------- /Density.m: -------------------------------------------------------------------------------- 1 | function rou_ = Density(rou, flow_i,flow_up,para) 2 | %DENSITY para=[T,Lm,lambda]; 3 | % 4 | T=para(1)/3600; 5 | Lm=para(2)/1000; 6 | lambda=para(3); 7 | rou_=max(0, rou+T/(Lm*lambda)*(flow_up-flow_i)); 8 | end 9 | 10 | -------------------------------------------------------------------------------- /Desired_speed.m: -------------------------------------------------------------------------------- 1 | function V = Desired_speed(rou,para,v_control) 2 | %DESIRED_SPEED input: density of current segment, speed control, alpha, am,v_free, rou_crit; output: the desired speed; para=[v_free, am, rou_crit, alpha]; 3 | v_free=para(1); 4 | am=para(2); 5 | rou_crit=para(3); 6 | alpha=para(4); 7 | V=min([v_free*exp(-1/am*(rou/rou_crit)^am), (1+alpha)*v_control]); 8 | end 9 | 10 | -------------------------------------------------------------------------------- /Freeway_model.m: -------------------------------------------------------------------------------- 1 | function x_=Freeway_model(x, u, weather) 2 | %% Initial conditions 3 | rou_11=x(1); 4 | v_11=x(2); 5 | q_11=x(3); 6 | rou_12=x(4); 7 | v_12=x(5); 8 | q_12=x(6); 9 | rou_13=x(7); 10 | v_13=x(8); 11 | q_13=x(9); 12 | rou_14=x(10); 13 | v_14=x(11); 14 | q_14=x(12); 15 | rou_15=x(13); 16 | v_15=x(14); 17 | q_15=x(15); 18 | rou_16=x(16); 19 | v_16=x(17); 20 | q_16=x(18); 21 | rou_21=x(19); 22 | v_21=x(20); 23 | q_21=x(21); 24 | rou_22=x(22); 25 | v_22=x(23); 26 | q_22=x(24); 27 | rou_23=x(25); 28 | v_23=x(26); 29 | q_23=x(27); 30 | rou_24=x(28); 31 | v_24=x(29); 32 | q_24=x(30); 33 | rou_25=x(31); 34 | v_25=x(32); 35 | q_25=x(33); 36 | rou_26=x(34); 37 | v_26=x(35); 38 | q_26=x(36); 39 | rou_31=x(37); 40 | v_31=x(38); 41 | q_31=x(39); 42 | rou_32=x(40); 43 | v_32=x(41); 44 | q_32=x(42); 45 | rou_33=x(43); 46 | v_33=x(44); 47 | q_33=x(45); 48 | rou_34=x(46); 49 | v_34=x(47); 50 | q_34=x(48); 51 | rou_35=x(49); 52 | v_35=x(50); 53 | q_35=x(51); 54 | rou_36=x(52); 55 | v_36=x(53); 56 | q_36=x(54); 57 | q_o0=x(55); 58 | w_o0=x(56); 59 | q_o1=x(57); 60 | w_o1=x(58); 61 | q_o2=x(59); 62 | w_o2=x(60); 63 | q_o3=x(61); 64 | w_o3=x(62); 65 | k=x(63); 66 | 67 | %% Noise 68 | noise_15=0; 69 | noise_25=0; 70 | noise_35=0; 71 | % if weather==1 72 | % noise_15=random('Normal',-1,1); 73 | % noise_25=random('Normal',-1,1); 74 | % noise_35=random('Normal',-1,1); 75 | % elseif weather==2 76 | % noise_15=random('Normal',-2,2); 77 | % noise_25=random('Normal',-2,2); 78 | % noise_35=random('Normal',-2,2); 79 | % elseif weather==3 80 | % noise_15=random('Normal',-3,3); 81 | % noise_25=random('Normal',-3,3); 82 | % noise_35=random('Normal',-3,3); 83 | % end 84 | % % % if rou_15<=30 85 | % % % noise_15=random('Normal',-1,1); 86 | % % % elseif rou_15>30 && rou_15<40 87 | % % % noise_15=random('Normal',-3,2); 88 | % % % elseif rou_15>=40 89 | % % % noise_15=random('Normal',-5,3); 90 | % % % end 91 | % % % if rou_25<=30 92 | % % % noise_25=random('Normal',-1,1); 93 | % % % elseif rou_25>30 && rou_25<40 94 | % % % noise_25=random('Normal',-3,2); 95 | % % % elseif rou_25>=40 96 | % % % noise_25=random('Normal',-5,3); 97 | % % % end 98 | % % % if rou_35<=30 99 | % % % noise_35=random('Normal',-1,1); 100 | % % % elseif rou_35>30 && rou_35<40 101 | % % % noise_35=random('Normal',-3,2); 102 | % % % elseif rou_35>=40 103 | % % % noise_35=random('Normal',-5,3); 104 | % % % end 105 | %% Parameters 106 | % if nominal ==1 107 | % [tao, kai, yita_high, yita_low, rou_max, sigma, am, v_free, rou_crit, alpha, T, lambda, Lm, v_control, v_min, Co, ~, ~, ~,off_rate] = parameters; 108 | % else 109 | [tao, kai, yita_high, yita_low, rou_max, sigma, am, v_free, rou_crit, alpha, T, lambda, Lm, v_control, v_min, Co, ~, ~, ~,off_rate] = parameters_real(weather); 110 | % end 111 | para_speed=[T,tao,yita_high, yita_low, kai,Lm,sigma,lambda]; 112 | para_density=[T,Lm,lambda]; 113 | para_desire=[v_free, am, rou_crit, alpha]; 114 | para_outmain=[T, lambda, rou_crit, am, v_free, alpha]; 115 | para_outramp=[T, rou_max, rou_crit]; 116 | % Initial state of the source 117 | % q_o1(1)=Outflow_main(demando1(1), v_free, w_o1(1), v_control, para_outmain); 118 | % q_o2(1)=Outflow_ramp(demando2(1), w_o2(1), Co2, ro, rou_21(1), para_outramp); 119 | % v_11(1)=v_free; 120 | % v_12(1)=v_free; 121 | % v_13(1)=v_free; 122 | % v_14(1)=v_free; 123 | % v_21(1)=v_free; 124 | % v_22(1)=v_free; 125 | %% Control input 126 | u_v1=u(1); 127 | u_v2=u(2); 128 | u_v3=u(3); 129 | u_v4=u(4); 130 | u_v5=u(5); 131 | u_v6=u(6); 132 | u_v7=u(7); 133 | u_v8=u(8); 134 | u_v9=u(9); 135 | r_o1=u(10); 136 | r_o2=u(11); 137 | r_o3=u(12); 138 | % r_o1=1; 139 | % r_o2=1; 140 | % r_o3=1; 141 | %% State evolution 142 | % Segment 1,1 143 | rou_11_=Density(rou_11, q_11, q_o0, para_density); 144 | V_desire_11=Desired_speed(rou_11, para_desire, v_control); 145 | v_11_=Speed(v_11, V_desire_11, v_11, rou_11, rou_12, para_speed, 0, v_min); 146 | q_11_=rou_11_*v_11_*lambda; 147 | % Segment 1,2 148 | rou_12_=Density(rou_12, q_12, q_11, para_density); 149 | V_desire_12=Desired_speed(rou_12, para_desire, u_v1); 150 | v_12_=Speed(v_12, V_desire_12, v_11, rou_12, rou_13, para_speed, 0, v_min); 151 | q_12_=rou_12_*v_12_*lambda; 152 | % Segment 1,3 153 | rou_13_=Density(rou_13, q_13, q_12, para_density); 154 | V_desire_13=Desired_speed(rou_13, para_desire, u_v2); 155 | v_13_=Speed(v_13, V_desire_13, v_12, rou_13, rou_14, para_speed, 0, v_min); 156 | q_13_=rou_13_*v_13_*lambda; 157 | % Segment 1,4 158 | rou_14_=Density(rou_14, q_14, q_13*off_rate, para_density); 159 | V_desire_14=Desired_speed(rou_14, para_desire, u_v3); 160 | v_14_=Speed(v_14, V_desire_14, v_13, rou_14, rou_15, para_speed, 0, v_min); 161 | q_14_=rou_14_*v_14_*lambda; 162 | % Segment 1,5 163 | rou_15_=Density(rou_15, q_15, q_14+q_o1, para_density); 164 | V_desire_15=Desired_speed(rou_15, para_desire, v_control); 165 | v_15_=Speed(v_15, V_desire_15, v_14, rou_15, rou_16, para_speed, q_o1, v_min)+noise_15; 166 | q_15_=rou_15_*v_15_*lambda; 167 | % Segment 1,6 168 | rou_16_=Density(rou_16, q_16, q_15, para_density); 169 | V_desire_16=Desired_speed(rou_16, para_desire, v_control); 170 | v_16_=Speed(v_16, V_desire_16, v_15, rou_16, rou_21, para_speed, 0, v_min); 171 | q_16_=rou_16_*v_16_*lambda; 172 | % Segment 2,1 173 | rou_21_=Density(rou_21, q_21, q_16, para_density); 174 | V_desire_21=Desired_speed(rou_21, para_desire, v_control); 175 | v_21_=Speed(v_21, V_desire_21, v_16, rou_21, rou_22, para_speed, 0, v_min); 176 | q_21_=rou_21_*v_21_*lambda; 177 | % Segment 2,2 178 | rou_22_=Density(rou_22, q_22, q_21, para_density); 179 | V_desire_22=Desired_speed(rou_22, para_desire, u_v4); 180 | v_22_=Speed(v_22, V_desire_22, v_21, rou_22, rou_23, para_speed, 0, v_min); 181 | q_22_=rou_22_*v_22_*lambda; 182 | % Segment 2,3 183 | rou_23_=Density(rou_23, q_23, q_22, para_density); 184 | V_desire_23=Desired_speed(rou_23, para_desire, u_v5); 185 | v_23_=Speed(v_23, V_desire_23, v_22, rou_23, rou_24, para_speed, 0, v_min); 186 | q_23_=rou_23_*v_23_*lambda; 187 | % Segment 2,4 188 | rou_24_=Density(rou_24, q_24, q_23*off_rate, para_density); 189 | V_desire_24=Desired_speed(rou_24, para_desire, u_v6); 190 | v_24_=Speed(v_24, V_desire_24, v_23, rou_24, rou_25, para_speed, 0, v_min); 191 | q_24_=rou_24_*v_24_*lambda; 192 | % Segment 2,5 193 | rou_25_=Density(rou_25, q_25, q_24+q_o2, para_density); 194 | V_desire_25=Desired_speed(rou_25, para_desire, v_control); 195 | v_25_=Speed(v_25, V_desire_25, v_24, rou_25, rou_26, para_speed, q_o2, v_min)+noise_25; 196 | q_25_=rou_25_*v_25_*lambda; 197 | % Segment 2,6 198 | rou_26_=Density(rou_26, q_26, q_25, para_density); 199 | V_desire_26=Desired_speed(rou_26, para_desire, v_control); 200 | v_26_=Speed(v_26, V_desire_26, v_25, rou_26, rou_31, para_speed, 0, v_min); 201 | q_26_=rou_26_*v_26_*lambda; 202 | % Segment 3,1 203 | rou_31_=Density(rou_31, q_31, q_26, para_density); 204 | V_desire_31=Desired_speed(rou_31, para_desire, v_control); 205 | v_31_=Speed(v_31, V_desire_31, v_26, rou_31, rou_32, para_speed, 0, v_min); 206 | q_31_=rou_31_*v_31_*lambda; 207 | % Segment 3,2 208 | rou_32_=Density(rou_32, q_32, q_31, para_density); 209 | V_desire_32=Desired_speed(rou_32, para_desire, u_v7); 210 | v_32_=Speed(v_32, V_desire_32, v_31, rou_32, rou_33, para_speed, 0, v_min); 211 | q_32_=rou_32_*v_32_*lambda; 212 | % Segment 3,3 213 | rou_33_=Density(rou_33, q_33, q_32, para_density); 214 | V_desire_33=Desired_speed(rou_33, para_desire, u_v8); 215 | v_33_=Speed(v_33, V_desire_33, v_32, rou_33, rou_34, para_speed, 0, v_min); 216 | q_33_=rou_33_*v_33_*lambda; 217 | % Segment 3,4 218 | rou_34_=Density(rou_34, q_34, q_33*off_rate, para_density); 219 | V_desire_34=Desired_speed(rou_34, para_desire, u_v9); 220 | v_34_=Speed(v_34, V_desire_34, v_33, rou_34, rou_35, para_speed, 0, v_min); 221 | q_34_=rou_34_*v_34_*lambda; 222 | % Segment 3,5 223 | rou_35_=Density(rou_35, q_35, q_34+q_o3, para_density); 224 | V_desire_35=Desired_speed(rou_35, para_desire, v_control); 225 | v_35_=Speed(v_35, V_desire_35, v_34, rou_35, rou_36, para_speed, q_o3, v_min)+noise_35; 226 | q_35_=rou_35_*v_35_*lambda; 227 | % Segment 3,6 228 | rou_36_=Density(rou_36, q_36, q_35, para_density); 229 | V_desire_36=Desired_speed(rou_36, para_desire, v_control); 230 | v_36_=Speed(v_36, V_desire_36, v_35, rou_36, rou_36_down(k), para_speed, 0, v_min); 231 | q_36_=rou_36_*v_36_*lambda; 232 | % Source nodes 233 | % o0 234 | w_o0_=w_o0+T/3600*(demando0(k-1)-q_o0); 235 | q_o0_=Outflow_main(demando0(k), v_11_, w_o0_, v_control, para_outmain); 236 | % o1 237 | w_o1_=w_o1+T/3600*(demando1(k-1)-q_o1); 238 | q_o1_=Outflow_ramp(demando1(k), w_o1_, Co, r_o1, rou_15_, para_outramp); 239 | % o2 240 | w_o2_=w_o2+T/3600*(demando2(k-1)-q_o2); 241 | q_o2_=Outflow_ramp(demando2(k), w_o2_, Co, r_o2, rou_25_, para_outramp); 242 | % o3 243 | w_o3_=w_o3+T/3600*(demando3(k-1)-q_o3); 244 | q_o3_=Outflow_ramp(demando3(k), w_o3_, Co, r_o3, rou_35_, para_outramp); 245 | 246 | x_=[rou_11_; 247 | v_11_; 248 | q_11_; 249 | rou_12_; 250 | v_12_; 251 | q_12_; 252 | rou_13_; 253 | v_13_; 254 | q_13_; 255 | rou_14_; 256 | v_14_; 257 | q_14_; 258 | rou_15_; 259 | v_15_; 260 | q_15_; 261 | rou_16_; 262 | v_16_; 263 | q_16_; 264 | rou_21_; 265 | v_21_; 266 | q_21_; 267 | rou_22_; 268 | v_22_; 269 | q_22_; 270 | rou_23_; 271 | v_23_; 272 | q_23_; 273 | rou_24_; 274 | v_24_; 275 | q_24_; 276 | rou_25_; 277 | v_25_; 278 | q_25_; 279 | rou_26_; 280 | v_26_; 281 | q_26_; 282 | rou_31_; 283 | v_31_; 284 | q_31_; 285 | rou_32_; 286 | v_32_; 287 | q_32_; 288 | rou_33_; 289 | v_33_; 290 | q_33_; 291 | rou_34_; 292 | v_34_; 293 | q_34_; 294 | rou_35_; 295 | v_35_; 296 | q_35_; 297 | rou_36_; 298 | v_36_; 299 | q_36_; 300 | q_o0_; 301 | w_o0_; 302 | q_o1_; 303 | w_o1_; 304 | q_o2_; 305 | w_o2_; 306 | q_o3_; 307 | w_o3_; 308 | k+1]; 309 | end 310 | % fprintf('TTS is %d veh*h \n', sum(TTS(60:960))) 311 | % figure(); 312 | % t=0:1/360:2.5; 313 | % subplot(3,2,1) 314 | % plot(t, v_11(61:961), '-', 'linewidth', 1.0); 315 | % hold on; 316 | % plot(t, v_12(61:961), '--', 'linewidth', 1.0); 317 | % hold on; 318 | % plot(t, v_13(61:961), ':', 'linewidth', 1.0); 319 | % hold on; 320 | % plot(t, v_14(61:961), '-.', 'linewidth', 1.0); 321 | % hold on; 322 | % plot(t, v_21(61:961), '-o', 'MarkerIndices',1:30:length(v_21(61:961)), 'linewidth', 1.0); 323 | % hold on; 324 | % plot(t, v_22(61:961), '-x', 'MarkerIndices',1:30:length(v_22(61:961)), 'linewidth', 1.0); 325 | % legend('Segmeng 1-1','Segmeng 1-2','Segmeng 1-3','Segmeng 1-4','Segmeng 2-1','Segmeng 2-2') 326 | % xlabel('Time [h]'); 327 | % ylabel('Speed [km/h]') 328 | % 329 | % subplot(3,2,2) 330 | % plot(t, q_11(60:960), '-', 'linewidth', 1.0); 331 | % hold on; 332 | % plot(t, q_12(60:960), '--', 'linewidth', 1.0); 333 | % hold on; 334 | % plot(t, q_13(60:960), ':', 'linewidth', 1.0); 335 | % hold on; 336 | % plot(t, q_14(60:960), '-.', 'linewidth', 1.0); 337 | % hold on; 338 | % plot(t, q_21(60:960), '-o', 'MarkerIndices',1:30:length(q_21(61:961)), 'linewidth', 1.0); 339 | % hold on; 340 | % plot(t, q_22(60:960), '-x', 'MarkerIndices',1:30:length(q_22(61:961)), 'linewidth', 1.0); 341 | % % legend('Segmeng 1-1','Segmeng 1-2','Segmeng 1-3','Segmeng 1-4','Segmeng 2-1','Segmeng 2-2') 342 | % xlabel('Time [h]'); 343 | % ylabel('Flow [veh/h]') 344 | % 345 | % subplot(3,2,3) 346 | % t=0:1/360:2.5; 347 | % plot(t, rou_11(61:961), '-', 'linewidth', 1.0); 348 | % hold on; 349 | % plot(t, rou_12(61:961), '--', 'linewidth', 1.0); 350 | % hold on; 351 | % plot(t, rou_13(61:961), ':', 'linewidth', 1.0); 352 | % hold on; 353 | % plot(t, rou_14(61:961), '-.', 'linewidth', 1.0); 354 | % hold on; 355 | % plot(t, rou_21(61:961), '-o', 'MarkerIndices',1:30:length(rou_21(61:961)), 'linewidth', 1.0); 356 | % hold on; 357 | % plot(t, rou_22(61:961), '-x', 'MarkerIndices',1:30:length(rou_22(61:961)), 'linewidth', 1.0); 358 | % % legend('Segmeng 1-1','Segmeng 1-2','Segmeng 1-3','Segmeng 1-4','Segmeng 2-1','Segmeng 2-2') 359 | % xlabel('Time [h]'); 360 | % ylabel('Density [veh/km]') 361 | % 362 | % subplot(3,2,5) 363 | % plot(t, q_o1(61:961), '-', 'linewidth', 1.0); 364 | % hold on; 365 | % plot(t, q_o2(61:961), '--', 'linewidth', 1.0); 366 | % legend('O_1','O_2') 367 | % 368 | % subplot(3,2,6) 369 | % plot(t,w_o1(61:961),'-', 'linewidth', 1.0); 370 | % hold on; 371 | % plot(t,w_o2(61:961),'--', 'linewidth', 1.0); 372 | % legend('O_1','O_2') 373 | 374 | -------------------------------------------------------------------------------- /Freeway_model_predict.m: -------------------------------------------------------------------------------- 1 | function x_=Freeway_model_predict(x, u, weather) 2 | %% Initial conditions 3 | rou_11=x(1); 4 | v_11=x(2); 5 | q_11=x(3); 6 | rou_12=x(4); 7 | v_12=x(5); 8 | q_12=x(6); 9 | rou_13=x(7); 10 | v_13=x(8); 11 | q_13=x(9); 12 | rou_14=x(10); 13 | v_14=x(11); 14 | q_14=x(12); 15 | rou_15=x(13); 16 | v_15=x(14); 17 | q_15=x(15); 18 | rou_16=x(16); 19 | v_16=x(17); 20 | q_16=x(18); 21 | rou_21=x(19); 22 | v_21=x(20); 23 | q_21=x(21); 24 | rou_22=x(22); 25 | v_22=x(23); 26 | q_22=x(24); 27 | rou_23=x(25); 28 | v_23=x(26); 29 | q_23=x(27); 30 | rou_24=x(28); 31 | v_24=x(29); 32 | q_24=x(30); 33 | rou_25=x(31); 34 | v_25=x(32); 35 | q_25=x(33); 36 | rou_26=x(34); 37 | v_26=x(35); 38 | q_26=x(36); 39 | rou_31=x(37); 40 | v_31=x(38); 41 | q_31=x(39); 42 | rou_32=x(40); 43 | v_32=x(41); 44 | q_32=x(42); 45 | rou_33=x(43); 46 | v_33=x(44); 47 | q_33=x(45); 48 | rou_34=x(46); 49 | v_34=x(47); 50 | q_34=x(48); 51 | rou_35=x(49); 52 | v_35=x(50); 53 | q_35=x(51); 54 | rou_36=x(52); 55 | v_36=x(53); 56 | q_36=x(54); 57 | q_o0=x(55); 58 | w_o0=x(56); 59 | q_o1=x(57); 60 | w_o1=x(58); 61 | q_o2=x(59); 62 | w_o2=x(60); 63 | q_o3=x(61); 64 | w_o3=x(62); 65 | k=x(63); 66 | 67 | %% Noise 68 | noise_15=0; 69 | noise_25=0; 70 | noise_35=0; 71 | % if rou_15<=30 72 | % noise_15=random('Normal',-1,1); 73 | % elseif rou_15>30 && rou_15<40 74 | % noise_15=random('Normal',-3,2); 75 | % elseif rou_15>=40 76 | % noise_15=random('Normal',-5,3); 77 | % end 78 | % if rou_25<=30 79 | % noise_25=random('Normal',-1,1); 80 | % elseif rou_25>30 && rou_25<40 81 | % noise_25=random('Normal',-3,2); 82 | % elseif rou_25>=40 83 | % noise_25=random('Normal',-5,3); 84 | % end 85 | % if rou_35<=30 86 | % noise_35=random('Normal',-1,1); 87 | % elseif rou_35>30 && rou_35<40 88 | % noise_35=random('Normal',-3,2); 89 | % elseif rou_35>=40 90 | % noise_35=random('Normal',-5,3); 91 | % end 92 | %% Parameters 93 | % if nominal ==1 94 | % [tao, kai, yita_high, yita_low, rou_max, sigma, am, v_free, rou_crit, alpha, T, lambda, Lm, v_control, v_min, Co, ~, ~, ~,off_rate] = parameters; 95 | % else 96 | [tao, kai, yita_high, yita_low, rou_max, sigma, am, v_free, rou_crit, alpha, T, lambda, Lm, v_control, v_min, Co, ~, ~, ~,off_rate] = parameters_predict(weather); 97 | % end 98 | para_speed=[T,tao,yita_high, yita_low, kai,Lm,sigma,lambda]; 99 | para_density=[T,Lm,lambda]; 100 | para_desire=[v_free, am, rou_crit, alpha]; 101 | para_outmain=[T, lambda, rou_crit, am, v_free, alpha]; 102 | para_outramp=[T, rou_max, rou_crit]; 103 | % Initial state of the source 104 | % q_o1(1)=Outflow_main(demando1(1), v_free, w_o1(1), v_control, para_outmain); 105 | % q_o2(1)=Outflow_ramp(demando2(1), w_o2(1), Co2, ro, rou_21(1), para_outramp); 106 | % v_11(1)=v_free; 107 | % v_12(1)=v_free; 108 | % v_13(1)=v_free; 109 | % v_14(1)=v_free; 110 | % v_21(1)=v_free; 111 | % v_22(1)=v_free; 112 | %% Control input 113 | u_v1=u(1); 114 | u_v2=u(2); 115 | u_v3=u(3); 116 | u_v4=u(4); 117 | u_v5=u(5); 118 | u_v6=u(6); 119 | u_v7=u(7); 120 | u_v8=u(8); 121 | u_v9=u(9); 122 | r_o1=u(10); 123 | r_o2=u(11); 124 | r_o3=u(12); 125 | % r_o1=1; 126 | % r_o2=1; 127 | % r_o3=1; 128 | %% State evolution 129 | % Segment 1,1 130 | rou_11_=Density(rou_11, q_11, q_o0, para_density); 131 | V_desire_11=Desired_speed(rou_11, para_desire, v_control); 132 | v_11_=Speed(v_11, V_desire_11, v_11, rou_11, rou_12, para_speed, 0, v_min); 133 | q_11_=rou_11_*v_11_*lambda; 134 | % Segment 1,2 135 | rou_12_=Density(rou_12, q_12, q_11, para_density); 136 | V_desire_12=Desired_speed(rou_12, para_desire, u_v1); 137 | v_12_=Speed(v_12, V_desire_12, v_11, rou_12, rou_13, para_speed, 0, v_min); 138 | q_12_=rou_12_*v_12_*lambda; 139 | % Segment 1,3 140 | rou_13_=Density(rou_13, q_13, q_12, para_density); 141 | V_desire_13=Desired_speed(rou_13, para_desire, u_v2); 142 | v_13_=Speed(v_13, V_desire_13, v_12, rou_13, rou_14, para_speed, 0, v_min); 143 | q_13_=rou_13_*v_13_*lambda; 144 | % Segment 1,4 145 | rou_14_=Density(rou_14, q_14, q_13*off_rate, para_density); 146 | V_desire_14=Desired_speed(rou_14, para_desire, u_v3); 147 | v_14_=Speed(v_14, V_desire_14, v_13, rou_14, rou_15, para_speed, 0, v_min); 148 | q_14_=rou_14_*v_14_*lambda; 149 | % Segment 1,5 150 | rou_15_=Density(rou_15, q_15, q_14+q_o1, para_density); 151 | V_desire_15=Desired_speed(rou_15, para_desire, v_control); 152 | v_15_=Speed(v_15, V_desire_15, v_14, rou_15, rou_16, para_speed, q_o1, v_min)+noise_15; 153 | q_15_=rou_15_*v_15_*lambda; 154 | % Segment 1,6 155 | rou_16_=Density(rou_16, q_16, q_15, para_density); 156 | V_desire_16=Desired_speed(rou_16, para_desire, v_control); 157 | v_16_=Speed(v_16, V_desire_16, v_15, rou_16, rou_21, para_speed, 0, v_min); 158 | q_16_=rou_16_*v_16_*lambda; 159 | % Segment 2,1 160 | rou_21_=Density(rou_21, q_21, q_16, para_density); 161 | V_desire_21=Desired_speed(rou_21, para_desire, v_control); 162 | v_21_=Speed(v_21, V_desire_21, v_16, rou_21, rou_22, para_speed, 0, v_min); 163 | q_21_=rou_21_*v_21_*lambda; 164 | % Segment 2,2 165 | rou_22_=Density(rou_22, q_22, q_21, para_density); 166 | V_desire_22=Desired_speed(rou_22, para_desire, u_v4); 167 | v_22_=Speed(v_22, V_desire_22, v_21, rou_22, rou_23, para_speed, 0, v_min); 168 | q_22_=rou_22_*v_22_*lambda; 169 | % Segment 2,3 170 | rou_23_=Density(rou_23, q_23, q_22, para_density); 171 | V_desire_23=Desired_speed(rou_23, para_desire, u_v5); 172 | v_23_=Speed(v_23, V_desire_23, v_22, rou_23, rou_24, para_speed, 0, v_min); 173 | q_23_=rou_23_*v_23_*lambda; 174 | % Segment 2,4 175 | rou_24_=Density(rou_24, q_24, q_23*off_rate, para_density); 176 | V_desire_24=Desired_speed(rou_24, para_desire, u_v6); 177 | v_24_=Speed(v_24, V_desire_24, v_23, rou_24, rou_25, para_speed, 0, v_min); 178 | q_24_=rou_24_*v_24_*lambda; 179 | % Segment 2,5 180 | rou_25_=Density(rou_25, q_25, q_24+q_o2, para_density); 181 | V_desire_25=Desired_speed(rou_25, para_desire, v_control); 182 | v_25_=Speed(v_25, V_desire_25, v_24, rou_25, rou_26, para_speed, q_o2, v_min)+noise_25; 183 | q_25_=rou_25_*v_25_*lambda; 184 | % Segment 2,6 185 | rou_26_=Density(rou_26, q_26, q_25, para_density); 186 | V_desire_26=Desired_speed(rou_26, para_desire, v_control); 187 | v_26_=Speed(v_26, V_desire_26, v_25, rou_26, rou_31, para_speed, 0, v_min); 188 | q_26_=rou_26_*v_26_*lambda; 189 | % Segment 3,1 190 | rou_31_=Density(rou_31, q_31, q_26, para_density); 191 | V_desire_31=Desired_speed(rou_31, para_desire, v_control); 192 | v_31_=Speed(v_31, V_desire_31, v_26, rou_31, rou_32, para_speed, 0, v_min); 193 | q_31_=rou_31_*v_31_*lambda; 194 | % Segment 3,2 195 | rou_32_=Density(rou_32, q_32, q_31, para_density); 196 | V_desire_32=Desired_speed(rou_32, para_desire, u_v7); 197 | v_32_=Speed(v_32, V_desire_32, v_31, rou_32, rou_33, para_speed, 0, v_min); 198 | q_32_=rou_32_*v_32_*lambda; 199 | % Segment 3,3 200 | rou_33_=Density(rou_33, q_33, q_32, para_density); 201 | V_desire_33=Desired_speed(rou_33, para_desire, u_v8); 202 | v_33_=Speed(v_33, V_desire_33, v_32, rou_33, rou_34, para_speed, 0, v_min); 203 | q_33_=rou_33_*v_33_*lambda; 204 | % Segment 3,4 205 | rou_34_=Density(rou_34, q_34, q_33*off_rate, para_density); 206 | V_desire_34=Desired_speed(rou_34, para_desire, u_v9); 207 | v_34_=Speed(v_34, V_desire_34, v_33, rou_34, rou_35, para_speed, 0, v_min); 208 | q_34_=rou_34_*v_34_*lambda; 209 | % Segment 3,5 210 | rou_35_=Density(rou_35, q_35, q_34+q_o3, para_density); 211 | V_desire_35=Desired_speed(rou_35, para_desire, v_control); 212 | v_35_=Speed(v_35, V_desire_35, v_34, rou_35, rou_36, para_speed, q_o3, v_min)+noise_35; 213 | q_35_=rou_35_*v_35_*lambda; 214 | % Segment 3,6 215 | rou_36_=Density(rou_36, q_36, q_35, para_density); 216 | V_desire_36=Desired_speed(rou_36, para_desire, v_control); 217 | v_36_=Speed(v_36, V_desire_36, v_35, rou_36, rou_36_down(k), para_speed, 0, v_min); 218 | q_36_=rou_36_*v_36_*lambda; 219 | % Source nodes 220 | % o0 221 | w_o0_=w_o0+T/3600*(demando0(k-1)-q_o0); 222 | q_o0_=Outflow_main(demando0(k), v_11_, w_o0_, v_control, para_outmain); 223 | % o1 224 | w_o1_=w_o1+T/3600*(demando1(k-1)-q_o1); 225 | q_o1_=Outflow_ramp(demando1(k), w_o1_, Co, r_o1, rou_15_, para_outramp); 226 | % o2 227 | w_o2_=w_o2+T/3600*(demando2(k-1)-q_o2); 228 | q_o2_=Outflow_ramp(demando2(k), w_o2_, Co, r_o2, rou_25_, para_outramp); 229 | % o3 230 | w_o3_=w_o3+T/3600*(demando3(k-1)-q_o3); 231 | q_o3_=Outflow_ramp(demando3(k), w_o3_, Co, r_o3, rou_35_, para_outramp); 232 | 233 | x_=[rou_11_; 234 | v_11_; 235 | q_11_; 236 | rou_12_; 237 | v_12_; 238 | q_12_; 239 | rou_13_; 240 | v_13_; 241 | q_13_; 242 | rou_14_; 243 | v_14_; 244 | q_14_; 245 | rou_15_; 246 | v_15_; 247 | q_15_; 248 | rou_16_; 249 | v_16_; 250 | q_16_; 251 | rou_21_; 252 | v_21_; 253 | q_21_; 254 | rou_22_; 255 | v_22_; 256 | q_22_; 257 | rou_23_; 258 | v_23_; 259 | q_23_; 260 | rou_24_; 261 | v_24_; 262 | q_24_; 263 | rou_25_; 264 | v_25_; 265 | q_25_; 266 | rou_26_; 267 | v_26_; 268 | q_26_; 269 | rou_31_; 270 | v_31_; 271 | q_31_; 272 | rou_32_; 273 | v_32_; 274 | q_32_; 275 | rou_33_; 276 | v_33_; 277 | q_33_; 278 | rou_34_; 279 | v_34_; 280 | q_34_; 281 | rou_35_; 282 | v_35_; 283 | q_35_; 284 | rou_36_; 285 | v_36_; 286 | q_36_; 287 | q_o0_; 288 | w_o0_; 289 | q_o1_; 290 | w_o1_; 291 | q_o2_; 292 | w_o2_; 293 | q_o3_; 294 | w_o3_; 295 | k+1]; 296 | end 297 | % fprintf('TTS is %d veh*h \n', sum(TTS(60:960))) 298 | % figure(); 299 | % t=0:1/360:2.5; 300 | % subplot(3,2,1) 301 | % plot(t, v_11(61:961), '-', 'linewidth', 1.0); 302 | % hold on; 303 | % plot(t, v_12(61:961), '--', 'linewidth', 1.0); 304 | % hold on; 305 | % plot(t, v_13(61:961), ':', 'linewidth', 1.0); 306 | % hold on; 307 | % plot(t, v_14(61:961), '-.', 'linewidth', 1.0); 308 | % hold on; 309 | % plot(t, v_21(61:961), '-o', 'MarkerIndices',1:30:length(v_21(61:961)), 'linewidth', 1.0); 310 | % hold on; 311 | % plot(t, v_22(61:961), '-x', 'MarkerIndices',1:30:length(v_22(61:961)), 'linewidth', 1.0); 312 | % legend('Segmeng 1-1','Segmeng 1-2','Segmeng 1-3','Segmeng 1-4','Segmeng 2-1','Segmeng 2-2') 313 | % xlabel('Time [h]'); 314 | % ylabel('Speed [km/h]') 315 | % 316 | % subplot(3,2,2) 317 | % plot(t, q_11(60:960), '-', 'linewidth', 1.0); 318 | % hold on; 319 | % plot(t, q_12(60:960), '--', 'linewidth', 1.0); 320 | % hold on; 321 | % plot(t, q_13(60:960), ':', 'linewidth', 1.0); 322 | % hold on; 323 | % plot(t, q_14(60:960), '-.', 'linewidth', 1.0); 324 | % hold on; 325 | % plot(t, q_21(60:960), '-o', 'MarkerIndices',1:30:length(q_21(61:961)), 'linewidth', 1.0); 326 | % hold on; 327 | % plot(t, q_22(60:960), '-x', 'MarkerIndices',1:30:length(q_22(61:961)), 'linewidth', 1.0); 328 | % % legend('Segmeng 1-1','Segmeng 1-2','Segmeng 1-3','Segmeng 1-4','Segmeng 2-1','Segmeng 2-2') 329 | % xlabel('Time [h]'); 330 | % ylabel('Flow [veh/h]') 331 | % 332 | % subplot(3,2,3) 333 | % t=0:1/360:2.5; 334 | % plot(t, rou_11(61:961), '-', 'linewidth', 1.0); 335 | % hold on; 336 | % plot(t, rou_12(61:961), '--', 'linewidth', 1.0); 337 | % hold on; 338 | % plot(t, rou_13(61:961), ':', 'linewidth', 1.0); 339 | % hold on; 340 | % plot(t, rou_14(61:961), '-.', 'linewidth', 1.0); 341 | % hold on; 342 | % plot(t, rou_21(61:961), '-o', 'MarkerIndices',1:30:length(rou_21(61:961)), 'linewidth', 1.0); 343 | % hold on; 344 | % plot(t, rou_22(61:961), '-x', 'MarkerIndices',1:30:length(rou_22(61:961)), 'linewidth', 1.0); 345 | % % legend('Segmeng 1-1','Segmeng 1-2','Segmeng 1-3','Segmeng 1-4','Segmeng 2-1','Segmeng 2-2') 346 | % xlabel('Time [h]'); 347 | % ylabel('Density [veh/km]') 348 | % 349 | % subplot(3,2,5) 350 | % plot(t, q_o1(61:961), '-', 'linewidth', 1.0); 351 | % hold on; 352 | % plot(t, q_o2(61:961), '--', 'linewidth', 1.0); 353 | % legend('O_1','O_2') 354 | % 355 | % subplot(3,2,6) 356 | % plot(t,w_o1(61:961),'-', 'linewidth', 1.0); 357 | % hold on; 358 | % plot(t,w_o2(61:961),'--', 'linewidth', 1.0); 359 | % legend('O_1','O_2') 360 | 361 | -------------------------------------------------------------------------------- /MPC_RM_VSL.m: -------------------------------------------------------------------------------- 1 | clear; 2 | % clc 3 | % close all 4 | %% Initialize 5 | Exit=[];Time=[]; 6 | x=[zeros(62,1);0]; 7 | ramp_only=1; 8 | u=[102,102,102,102,102,102,102,102,102,1,1,1]; 9 | initial_weather=2; 10 | % weather=initial_weather; % taken as the nominal weather 11 | end_weather=3; 12 | 13 | Weather_real=[ones(1,60) initial_weather*ones(1,360) end_weather*ones(1,810)]; 14 | Weather_predict=[ones(1,60) initial_weather*ones(1,1170)]; 15 | Weather=Weather_predict; 16 | for i=1:60 17 | weather=Weather(x(63)+1); 18 | x=Freeway_model(x,u,1); 19 | end 20 | 21 | %% Parameters 22 | Mc=6; % control sampling time: 60s 23 | M=6; % operation sampling time: 300s 24 | Ns=M/Mc; 25 | Np=15*Ns; % 26 | Nc=15*Ns; % 27 | N=720; 28 | N_max=40; % multi-start point 29 | xx=zeros(size(x,1),N); 30 | % use nominal parameters to calculate objective values 31 | [tao, kai, yita_high, yita_low, rou_max, sigma, am, v_free, rou_crit, alpha, T, lambda, Lm, ~, v_min, Co2, xi_r, xi_s, ~, ~] = parameters_predict(weather); 32 | dim_speed=9; 33 | dim_ramp=3; 34 | U=zeros(dim_speed+dim_ramp,N); 35 | 36 | para=[Lm, lambda, v_free, Mc, dim_speed, T, xi_r, xi_s]; % pass the weather information through this 37 | options = optimoptions(@fmincon,'Algorithm','sqp','Display','off','TolFun',1e-2, 'TolX',1e-2, 'TolCon', 1e-2); 38 | 39 | % v_control=repmat(100*ones(dim_speed,1),1,Nc); 40 | % v_control_pre=v_control(:,1); 41 | if ramp_only 42 | dim_control=dim_ramp; 43 | v_control=repmat(102*ones(dim_speed,1),1,Nc); 44 | v_control_pre=v_control(:,1); 45 | u_pre=ones(dim_control,Nc); % 46 | u_lb=repmat(zeros(dim_control,1),1,Nc); 47 | u_ub=repmat(ones(dim_control,1),1,Nc); 48 | else 49 | dim_control=dim_speed+dim_ramp; 50 | v_control=[]; 51 | v_control_pre=[]; 52 | u_pre=[102*ones(dim_speed,Nc);ones(dim_ramp,Nc)]; % 53 | u_lb=repmat([20*ones(dim_speed,1);0*ones(dim_ramp,1)],1,Nc); 54 | u_ub=repmat([v_free*ones(dim_speed,1);ones(dim_ramp,1)],1,Nc); 55 | end 56 | 57 | 58 | %% 59 | for i=1:N/M 60 | % if x(63)>=420 61 | % weather=end_weather; 62 | % end 63 | % para=[Lm, lambda, v_free, M, dim_speed, T, xi_r, xi_s, weather]; 64 | objfun=@(co) ModelState(x, [v_control; co], Nc, Np, para, [v_control_pre;u_pre(:,1)], Weather); 65 | confun=@(co) ModelCons(x, [v_control; co], Nc, Np, Mc, Weather); % Incorrect 66 | u0=cell(1,N_max);u_t=cell(1,N_max);Fval=nan(1,N_max);Exitflag=nan(1,N_max); 67 | tic 68 | for l=1:N_max 69 | if l==1 70 | u0{l}=u_pre; 71 | elseif l==2 72 | u0{l}=u_lb; 73 | elseif l==3 74 | u0{l}=u_ub; 75 | else 76 | if ramp_only 77 | u0{l}=repmat(rand,dim_control, Nc); 78 | else 79 | u0{l}=repmat([rand(dim_speed,1)*80+20;rand(dim_ramp,1)],1, Nc); 80 | end 81 | end 82 | [u_t{l}, Fval(l), exitflag] = fmincon(objfun, u0{l}, [], [], [], [], u_lb, u_ub, [], options); 83 | if exitflag == 0 84 | warning('the solver does not succeed'); 85 | Fval(l)=Fval(l)+1e6; 86 | elseif exitflag < 0 87 | warning('the solver does not succeed'); 88 | Fval(l)=Fval(l)+1e6; 89 | end 90 | Exitflag(l)=exitflag; 91 | end 92 | time=toc; 93 | Time=[Time time]; 94 | [fval, index]=min(Fval); 95 | Exit=[Exit Exitflag']; 96 | u_opt=[v_control;u_t{index}]; 97 | % u_opt=[v_control;[1 1 1]]; 98 | U(:,M*(i-1)+1:M*i)=repelem(u_opt(:,1:Ns),1,Mc); 99 | u_imp=repelem(u_opt, 1, Mc); 100 | for j=1:M 101 | weather=Weather_real(x(63)+1); 102 | x=Freeway_model(x,u_imp(:,j),weather); % use real parameters 103 | xx(:,M*(i-1)+j)=x; 104 | end 105 | u_pre=u_t{index}; 106 | end 107 | %% figure 108 | u_speed=U(1:dim_speed,:); 109 | u_ramp=U(dim_speed+1:dim_speed+dim_ramp,:); 110 | rou_11=xx(1,:); 111 | v_11=xx(2,:); 112 | q_11=xx(3,:); 113 | rou_12=xx(4,:); 114 | v_12=xx(5,:); 115 | q_12=xx(6,:); 116 | rou_13=xx(7,:); 117 | v_13=xx(8,:); 118 | q_13=xx(9,:); 119 | rou_14=xx(10,:); 120 | v_14=xx(11,:); 121 | q_14=xx(12,:); 122 | rou_15=xx(13,:); 123 | v_15=xx(14,:); 124 | q_15=xx(15,:); 125 | rou_16=xx(16,:); 126 | v_16=xx(17,:); 127 | q_16=xx(18,:); 128 | rou_21=xx(19,:); 129 | v_21=xx(20,:); 130 | q_21=xx(21,:); 131 | rou_22=xx(22,:); 132 | v_22=xx(23,:); 133 | q_22=xx(24,:); 134 | rou_23=xx(25,:); 135 | v_23=xx(26,:); 136 | q_23=xx(27,:); 137 | rou_24=xx(28,:); 138 | v_24=xx(29,:); 139 | q_24=xx(30,:); 140 | rou_25=xx(31,:); 141 | v_25=xx(32,:); 142 | q_25=xx(33,:); 143 | rou_26=xx(34,:); 144 | v_26=xx(35,:); 145 | q_26=xx(36,:); 146 | rou_31=xx(37,:); 147 | v_31=xx(38,:); 148 | q_31=xx(39,:); 149 | rou_32=xx(40,:); 150 | v_32=xx(41,:); 151 | q_32=xx(42,:); 152 | rou_33=xx(43,:); 153 | v_33=xx(44,:); 154 | q_33=xx(45,:); 155 | rou_34=xx(46,:); 156 | v_34=xx(47,:); 157 | q_34=xx(48,:); 158 | rou_35=xx(49,:); 159 | v_35=xx(50,:); 160 | q_35=xx(51,:); 161 | rou_36=xx(52,:); 162 | v_36=xx(53,:); 163 | q_36=xx(54,:); 164 | q_o0=xx(55,:); 165 | w_o0=xx(56,:); 166 | q_o1=xx(57,:); 167 | w_o1=xx(58,:); 168 | q_o2=xx(59,:); 169 | w_o2=xx(60,:); 170 | q_o3=xx(61,:); 171 | w_o3=xx(62,:); 172 | Total_veh=(rou_11+rou_12+rou_13+rou_14+rou_15+rou_16+rou_21+rou_22+rou_23+rou_24+rou_25+rou_26+... 173 | rou_31+rou_32+rou_33+rou_34+rou_35+rou_36).*1000./1000.*2+w_o0+w_o1+w_o2+w_o3; 174 | TTS=10/3600.*Total_veh; 175 | %% 176 | fprintf('TTS is %.3f veh*h \n', sum(TTS)) 177 | figure(); 178 | t=1/360:1/360:2.0; 179 | subplot(4,2,1) 180 | plot(t, v_11, '-', 'linewidth', 1.5); 181 | hold on; 182 | plot(t, v_15, '--', 'linewidth', 1.5); 183 | hold on; 184 | plot(t, v_21, ':', 'linewidth', 1.5); 185 | hold on; 186 | plot(t, v_25, '-', 'linewidth', 1.5); 187 | hold on; 188 | plot(t, v_31, '--', 'linewidth', 1.5); 189 | hold on; 190 | plot(t, v_35, ':', 'linewidth', 1.5); 191 | legend('Segmeng 1-1','Segmeng 1-5','Segmeng 2-1','Segmeng 2-5','Segmeng 3-1','Segmeng 3-5') 192 | xlabel('Time [h]'); 193 | ylabel('Speed [km/h]') 194 | 195 | subplot(4,2,2) 196 | plot(t, q_11, '-', 'linewidth', 1.5); 197 | hold on; 198 | plot(t, q_15, '--', 'linewidth', 1.5); 199 | hold on; 200 | plot(t, q_21, ':', 'linewidth', 1.5); 201 | hold on; 202 | plot(t, q_25, '-', 'linewidth', 1.5); 203 | hold on; 204 | plot(t, q_31, '--', 'linewidth', 1.5); 205 | hold on; 206 | plot(t, q_35, ':', 'linewidth', 1.5); 207 | xlabel('Time [h]'); 208 | ylabel('Flow [veh/h]') 209 | 210 | subplot(4,2,3) 211 | plot(t, rou_11, '-', 'linewidth', 1.5); 212 | hold on; 213 | plot(t, rou_15, '--', 'linewidth', 1.5); 214 | hold on; 215 | plot(t, rou_21, ':', 'linewidth', 1.5); 216 | hold on; 217 | plot(t, rou_25, '-', 'linewidth', 1.5); 218 | hold on; 219 | plot(t, rou_31, '--', 'linewidth', 1.5); 220 | hold on; 221 | plot(t, rou_35, ':', 'linewidth', 1.5); 222 | xlabel('Time [h]'); 223 | ylabel('Density [veh/km]') 224 | 225 | subplot(4,2,4) 226 | plot(t, demando0(61:780), '-', 'linewidth', 1.5); 227 | hold on; 228 | plot(t, demando1(61:780), '-.', 'linewidth', 1.5); 229 | hold on; 230 | plot(t, demando2(61:780), ':', 'linewidth', 1.5); 231 | hold on; 232 | plot(t, demando3(61:780), '--', 'linewidth', 1.5); 233 | xlabel('Time [h]'); 234 | ylabel('Demand [veh/h]'); 235 | legend('O_0','O_1','O_2','O_3') 236 | % ylim([0 4000]) 237 | 238 | subplot(4,2,5) 239 | plot(t, q_o0, '-', 'linewidth', 1.5); 240 | hold on; 241 | plot(t, q_o1, '--', 'linewidth', 1.5); 242 | hold on; 243 | plot(t, q_o2, '-.', 'linewidth', 1.5); 244 | hold on; 245 | plot(t, q_o3, ':', 'linewidth', 1.5); 246 | legend('O_0','O_1','O_2','O_3') 247 | xlabel('Time [h]'); 248 | ylabel('Outflow [veh/h]') 249 | % ylim([0 4000]) 250 | 251 | subplot(4,2,6) 252 | plot(t,w_o0,'-', 'linewidth', 1.5); 253 | hold on; 254 | plot(t,w_o1,'--', 'linewidth', 1.5); 255 | hold on; 256 | plot(t,w_o2,'-.', 'linewidth', 1.5); 257 | hold on; 258 | plot(t,w_o3,':', 'linewidth', 1.5); 259 | legend('O_0','O_1','O_2','O_3') 260 | xlabel('Time [h]'); 261 | ylabel('Queue length [veh]') 262 | 263 | subplot(4,2,7) 264 | plot(t,u_speed,'-', 'linewidth', 1.5); 265 | legend('v-11','v-12','v-13','v-21','v-22','v-23','v-31','v-32','v-33'); 266 | xlabel('Time [h]'); 267 | ylabel('Speed limits') 268 | 269 | subplot(4,2,8) 270 | plot(t,u_ramp,'-','linewidth',1.0); 271 | legend('O_1','O_2','O_3'); 272 | ylim([0 1]); 273 | xlabel('Time [h]'); 274 | ylabel('Ramp metering') 275 | %% 276 | function J=ModelState(x, u, Nc, Np, para, u_pre, Weather) 277 | Lm=para(1); 278 | Lambda=para(2); 279 | v_free=para(3); 280 | Mc=para(4); 281 | dim_speed=para(5); 282 | T=para(6); 283 | xi_r=para(7); 284 | xi_s=para(8); 285 | uc=[u repelem(u(:, end),1,Np-Nc)]; 286 | uc_full=repelem(uc,1,Mc); 287 | u_diff=[u_pre u]; 288 | yy=nan(size(x,1),Np*Mc); 289 | Diff=zeros(size(u,1),Nc); % the erros between the successive control inputs 290 | for i=1:Nc 291 | Diff(:,i)=u_diff(:,i+1)-u_diff(:,i); 292 | end 293 | for i=1:Np*Mc 294 | weather=Weather(x(63)+1); 295 | x=Freeway_model_predict(x,uc_full(:,i),weather); % use real parameters 296 | yy(:,i)=x; 297 | end 298 | rou_11=yy(1,:);rou_12=yy(4,:);rou_13=yy(7,:);rou_14=yy(10,:);rou_15=yy(13,:);rou_16=yy(16,:); 299 | rou_21=yy(19,:);rou_22=yy(22,:);rou_23=yy(25,:);rou_24=yy(28,:);rou_25=yy(31,:);rou_26=yy(34,:); 300 | rou_31=yy(37,:);rou_32=yy(40,:);rou_33=yy(43,:);rou_34=yy(46,:);rou_35=yy(49,:);rou_36=yy(52,:); 301 | w_o0=yy(56,:);w_o1=yy(58,:);w_o2=yy(60,:);w_o3=yy(62,:); 302 | TTS=(sum((rou_11+rou_12+rou_13+rou_14+rou_15+rou_16+rou_21+rou_22+rou_23+rou_24+rou_25+rou_26... 303 | +rou_31+rou_32+rou_33+rou_34+rou_35+rou_36))*1000/1000*2+sum(w_o0+w_o1+w_o2+w_o3))*10/3600; 304 | Diff_speed=Diff(1:dim_speed,:); 305 | Diff_r=Diff(dim_speed+1:end,:); 306 | penalty=xi_r*sum(sum(Diff_r.^2))+xi_s*sum(sum((Diff_speed/v_free).^2)); 307 | J=TTS; 308 | end 309 | function [c, ceq]=ModelCons(x, u, Nc, Np, Mc, Weather) 310 | ceq=[]; 311 | uc=[u repelem(u(:, end),1,Np-Nc)]; 312 | uc_full=repelem(uc,1,Mc); 313 | yy=zeros(size(x,1),Np*Mc); 314 | for i=1:Np*Mc 315 | weather=Weather(x(63)+1); 316 | x=Freeway_model(x,uc_full(:,i), weather); 317 | yy(:,i)=x; 318 | end 319 | w_o0=yy(56,:);w_o1=yy(58,:); 320 | w_o2=yy(60,:);w_o3=yy(62,:); 321 | c=[w_o1'-100;w_o2'-100;w_o3'-100]; 322 | end 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | -------------------------------------------------------------------------------- /MPC_imp_RM.m: -------------------------------------------------------------------------------- 1 | function u_theta = MPC_imp_RM(x, u_pre, theta_pre, Weather, rou_crit) 2 | Np=3; 3 | Nc=3; 4 | M=30; 5 | Mc=6; 6 | N_max=40; % multi-start point 7 | dim_speed=9; 8 | % dim_ramp=3; 9 | dim_theta=1; 10 | % fake weather condition; rou_crit can be adjusted 11 | weather=Weather(x(63)+1); 12 | [~, ~, ~, ~, ~, ~, ~, v_free, ~, ~, T, lambda, Lm, ~, ~, ~, xi_r, xi_s,xi_theta,~] = parameters_predict(weather); 13 | para=[Lm, lambda, v_free, M, dim_speed, T, xi_r, xi_s, weather, rou_crit, Mc, xi_theta]; 14 | options = optimoptions(@fmincon,'Algorithm','sqp','Display','off','TolFun',1e-2, 'TolX',1e-2, 'TolCon', 1e-2); 15 | theta_lb=repmat(-5,dim_theta,Nc); 16 | theta_ub=repmat(5,dim_theta,Nc); 17 | objfun=@(theta) ModelState(x, theta, Nc, Np, para, u_pre(:,1), theta_pre(:,1), Weather); 18 | confun=@(theta) ModelCons(x, theta, Nc, Np, para, u_pre(:,1), Weather); 19 | theta0=cell(1,N_max);theta_t=cell(1,N_max);Fval=nan(1,N_max); 20 | parfor l=1:N_max 21 | if l==1 22 | theta0{l}=theta_pre; 23 | elseif l==2 24 | theta0{l}=theta_lb; 25 | elseif l==3 26 | theta0{l}=theta_ub; 27 | elseif l==4 28 | theta0{l}=(theta_lb+theta_ub)/2; 29 | else 30 | % theta0{l}=repmat([2*rand;200*rand-100;200*rand-100;200*rand],1, Nc); 31 | theta0{l}=repmat(10*rand-5,dim_theta, Nc); 32 | end 33 | [theta_t{l}, Fval(l), exitflag] = fmincon(objfun, theta0{l}, [], [], [], [], theta_lb, theta_ub, [], options); 34 | if exitflag == 0 35 | Fval(l)=Fval(l)+1e6; 36 | elseif exitflag < 0 37 | Fval(l)=Fval(l)+1e6; 38 | end 39 | end 40 | [~, index]=min(Fval); 41 | u_theta=theta_t{index}; 42 | end 43 | 44 | function J=ModelState(x, theta, Nc, Np, para, u_pre, theta_pre, Weather) 45 | Lm=para(1); 46 | Lambda=para(2); 47 | v_free=para(3); 48 | M=para(4); 49 | dim_speed=para(5); 50 | T=para(6); 51 | xi_r=para(7); 52 | xi_s=para(8); 53 | % weather=para(9); 54 | rou_crit=para(10); 55 | Mc=para(11); 56 | xi_theta=para(12); 57 | % uc=[u repelem(u(:, end),1,Np-Nc)]; 58 | % uc_full=repelem(uc,1,M); 59 | theta_full=repelem(theta,1,Np/Nc); 60 | yy=[]; 61 | % Diff=zeros(size(u_pre,1),Np); % the erros between the successive control inputs 62 | Diff_theta=zeros(size(theta_pre,1),Nc); 63 | uc_full=zeros(size(u_pre,1),Np); 64 | for i=1:Np 65 | for j=1:M/Mc 66 | u=u_para_RM(theta_full(:,i), x, u_pre, rou_crit); 67 | uc_full(:,i)=u; 68 | for k=1:Mc 69 | weather=Weather(x(63)+1); 70 | x=Freeway_model_predict(x,u,weather); 71 | yy=[yy x]; 72 | end 73 | u_pre=u; 74 | end 75 | end 76 | u_diff=[u_pre uc_full]; 77 | theta_diff=[theta_pre theta]; 78 | % for i=1:Np 79 | % Diff(:,i)=u_diff(:,i+1)-u_diff(:,i); 80 | % end 81 | for i=1:Nc 82 | Diff_theta(:,i)=theta_diff(:,i+1)-theta_diff(:,i); 83 | end 84 | rou_11=yy(1,:);rou_12=yy(4,:);rou_13=yy(7,:);rou_14=yy(10,:);rou_15=yy(13,:);rou_16=yy(16,:); 85 | rou_21=yy(19,:);rou_22=yy(22,:);rou_23=yy(25,:);rou_24=yy(28,:);rou_25=yy(31,:);rou_26=yy(34,:); 86 | rou_31=yy(37,:);rou_32=yy(40,:);rou_33=yy(43,:);rou_34=yy(46,:);rou_35=yy(49,:);rou_36=yy(52,:); 87 | w_o0=yy(56,:);w_o1=yy(58,:);w_o2=yy(60,:);w_o3=yy(62,:); 88 | TTS=(sum((rou_11+rou_12+rou_13+rou_14+rou_15+rou_16+rou_21+rou_22+rou_23+rou_24+rou_25+rou_26... 89 | +rou_31+rou_32+rou_33+rou_34+rou_35+rou_36))*1000/1000*2+sum(w_o0+w_o1+w_o2+w_o3))*T/3600; 90 | % Diff_speed=Diff(1:dim_speed,:); 91 | % Diff_r=Diff(dim_speed+1:end,:); 92 | % penalty=xi_r*sum(sum(Diff_r.^2))+xi_s*sum(sum((Diff_speed/v_free).^2)); 93 | % penalty=xi_theta*sum(sum(Diff_theta.^2)); 94 | J=TTS; 95 | end 96 | 97 | function [c, ceq]=ModelCons(x, theta, Nc, Np, para,u_pre, Weather) 98 | ceq=[]; 99 | M=para(4); 100 | % weather=para(9); 101 | rou_crit=para(10); 102 | Mc=para(11); 103 | % uc=[u repelem(u(:, end),1,Np-Nc)]; 104 | % uc_full=repelem(uc,1,M); 105 | theta_full=repelem(theta,1,Np/Nc); 106 | yy=[]; 107 | uc_full=zeros(size(u_pre,1),Np); 108 | for i=1:Np 109 | for j=1:M/Mc 110 | u=u_para_RM(theta_full(:,i), x, u_pre, rou_crit); 111 | uc_full(:,i)=u; 112 | for k=1:Mc 113 | weather=Weather(x(63)+1); 114 | x=Freeway_model(x,u,weather); 115 | yy=[yy x]; 116 | end 117 | u_pre=u; 118 | end 119 | end 120 | w_o0=yy(56,:);w_o1=yy(58,:); 121 | w_o2=yy(60,:);w_o3=yy(62,:); 122 | c=[w_o1'-100;w_o2'-100;w_o3'-100]; 123 | % u_vsl_full=u_vsl(:); 124 | % u_rm_full=u_rm(:); 125 | % c=[u_vsl_full-102;20-u_vsl_full;u_rm_full-1;-u_rm_full]; 126 | end -------------------------------------------------------------------------------- /Main_openloop.m: -------------------------------------------------------------------------------- 1 | clear; 2 | % clc 3 | % close all 4 | %% Initial state 5 | x=[zeros(62,1);0]; 6 | v_free=102; 7 | u=[v_free,v_free,v_free,v_free,v_free,v_free,v_free,v_free,v_free,1,1,1];Weather=[]; 8 | % noise_o0=random('Normal',0,150,1,151); % normal distributed noise on the demand 9 | % noise_o1=random('Normal',0,60,1,151); 10 | % noise_o2=random('Normal',0,60,1,151); 11 | % noise_o3=random('Normal',0,60,1,151); 12 | initial_weather=2; 13 | end_weather=3; 14 | weather=initial_weather; % weather condition: 1 is sunny, 2 is rainy, 3 is storming 15 | for i=1:60 16 | x=Freeway_model(x,u, 1); 17 | end 18 | xx=[]; 19 | % u=[102,102,102,102,102,102,1,1,1]; 20 | % load('weather_01.mat'); 21 | % u=[100*ones(2,900);u_ramp]; 22 | % u=repelem(U,1,6); 23 | for i=1:720 24 | 25 | % weather=3; 26 | % u=U(:,i); 27 | % weather=(end_weather-initial_weather)/720*i+initial_weather; 28 | x=Freeway_model(x,u, weather); 29 | xx=[xx x]; 30 | if i>=360 31 | weather=end_weather; 32 | end 33 | Weather=[Weather weather]; 34 | end 35 | rou_11=xx(1,:); 36 | v_11=xx(2,:); 37 | q_11=xx(3,:); 38 | rou_12=xx(4,:); 39 | v_12=xx(5,:); 40 | q_12=xx(6,:); 41 | rou_13=xx(7,:); 42 | v_13=xx(8,:); 43 | q_13=xx(9,:); 44 | rou_14=xx(10,:); 45 | v_14=xx(11,:); 46 | q_14=xx(12,:); 47 | rou_15=xx(13,:); 48 | v_15=xx(14,:); 49 | q_15=xx(15,:); 50 | rou_16=xx(16,:); 51 | v_16=xx(17,:); 52 | q_16=xx(18,:); 53 | rou_21=xx(19,:); 54 | v_21=xx(20,:); 55 | q_21=xx(21,:); 56 | rou_22=xx(22,:); 57 | v_22=xx(23,:); 58 | q_22=xx(24,:); 59 | rou_23=xx(25,:); 60 | v_23=xx(26,:); 61 | q_23=xx(27,:); 62 | rou_24=xx(28,:); 63 | v_24=xx(29,:); 64 | q_24=xx(30,:); 65 | rou_25=xx(31,:); 66 | v_25=xx(32,:); 67 | q_25=xx(33,:); 68 | rou_26=xx(34,:); 69 | v_26=xx(35,:); 70 | q_26=xx(36,:); 71 | rou_31=xx(37,:); 72 | v_31=xx(38,:); 73 | q_31=xx(39,:); 74 | rou_32=xx(40,:); 75 | v_32=xx(41,:); 76 | q_32=xx(42,:); 77 | rou_33=xx(43,:); 78 | v_33=xx(44,:); 79 | q_33=xx(45,:); 80 | rou_34=xx(46,:); 81 | v_34=xx(47,:); 82 | q_34=xx(48,:); 83 | rou_35=xx(49,:); 84 | v_35=xx(50,:); 85 | q_35=xx(51,:); 86 | rou_36=xx(52,:); 87 | v_36=xx(53,:); 88 | q_36=xx(54,:); 89 | q_o0=xx(55,:); 90 | w_o0=xx(56,:); 91 | q_o1=xx(57,:); 92 | w_o1=xx(58,:); 93 | q_o2=xx(59,:); 94 | w_o2=xx(60,:); 95 | q_o3=xx(61,:); 96 | w_o3=xx(62,:); 97 | Total_veh=(rou_11+rou_12+rou_13+rou_14+rou_15+rou_16+rou_21+rou_22+rou_23+rou_24+rou_25+rou_26+... 98 | rou_31+rou_32+rou_33+rou_34+rou_35+rou_36).*1000./1000.*2+w_o0+w_o1+w_o2+w_o3; 99 | TTS=10/3600.*Total_veh; 100 | % Flow_ave=sum(q_22)/360; 101 | %% 102 | fprintf('TTS is %.3f veh*h \n', sum(TTS)) 103 | % fprintf('Average outflow is %.3f veh*h \n', sum(Flow_ave)) 104 | figure(); 105 | t=1/360:1/360:2.0; 106 | subplot(4,2,1) 107 | plot(t, v_11, '-', 'linewidth', 1.5); 108 | hold on; 109 | plot(t, v_15, '--', 'linewidth', 1.5); 110 | hold on; 111 | plot(t, v_21, ':', 'linewidth', 1.5); 112 | hold on; 113 | plot(t, v_25, '-', 'linewidth', 1.5); 114 | hold on; 115 | plot(t, v_31, '--', 'linewidth', 1.5); 116 | hold on; 117 | plot(t, v_35, ':', 'linewidth', 1.5); 118 | legend('Segmeng 1-1','Segmeng 1-5','Segmeng 2-1','Segmeng 2-5','Segmeng 3-1','Segmeng 3-5') 119 | xlabel('Time [h]'); 120 | ylabel('Speed [km/h]') 121 | 122 | subplot(4,2,2) 123 | plot(t, q_11, '-', 'linewidth', 1.5); 124 | hold on; 125 | plot(t, q_15, '--', 'linewidth', 1.5); 126 | hold on; 127 | plot(t, q_21, ':', 'linewidth', 1.5); 128 | hold on; 129 | plot(t, q_25, '-', 'linewidth', 1.5); 130 | hold on; 131 | plot(t, q_31, '--', 'linewidth', 1.5); 132 | hold on; 133 | plot(t, q_35, ':', 'linewidth', 1.5); 134 | xlabel('Time [h]'); 135 | ylabel('Flow [veh/h]') 136 | 137 | subplot(4,2,3) 138 | plot(t, rou_11, '-', 'linewidth', 1.5); 139 | hold on; 140 | plot(t, rou_15, '--', 'linewidth', 1.5); 141 | hold on; 142 | plot(t, rou_21, ':', 'linewidth', 1.5); 143 | hold on; 144 | plot(t, rou_25, '-', 'linewidth', 1.5); 145 | hold on; 146 | plot(t, rou_31, '--', 'linewidth', 1.5); 147 | hold on; 148 | plot(t, rou_35, ':', 'linewidth', 1.5); 149 | xlabel('Time [h]'); 150 | ylabel('Density [veh/km]') 151 | 152 | subplot(4,2,4) 153 | plot(t, demando0(91:810), '-', 'linewidth', 1.5); 154 | hold on; 155 | plot(t, demando1(91:810), '-.', 'linewidth', 1.5); 156 | hold on; 157 | plot(t, demando2(91:810), ':', 'linewidth', 1.5); 158 | hold on; 159 | plot(t, demando3(91:810), '--', 'linewidth', 1.5); 160 | xlabel('Time [h]'); 161 | ylabel('Demand [veh/h]'); 162 | legend('O_0','O_1','O_2','O_3') 163 | % ylim([0 4000]) 164 | 165 | subplot(4,2,5) 166 | plot(t, q_o0, '-', 'linewidth', 1.5); 167 | hold on; 168 | plot(t, q_o1, '--', 'linewidth', 1.5); 169 | hold on; 170 | plot(t, q_o2, '-.', 'linewidth', 1.5); 171 | hold on; 172 | plot(t, q_o3, ':', 'linewidth', 1.5); 173 | legend('O_0','O_1','O_2','O_3') 174 | xlabel('Time [h]'); 175 | ylabel('Outflow [veh/h]') 176 | % ylim([0 4000]) 177 | 178 | subplot(4,2,6) 179 | plot(t,w_o0,'-', 'linewidth', 1.5); 180 | hold on; 181 | plot(t,w_o1,'--', 'linewidth', 1.5); 182 | hold on; 183 | plot(t,w_o2,'-.', 'linewidth', 1.5); 184 | hold on; 185 | plot(t,w_o3,':', 'linewidth', 1.5); 186 | legend('O_0','O_1','O_2','O_3') 187 | xlabel('Time [h]'); 188 | ylabel('Queue length [veh]') 189 | 190 | subplot(4,2,7) 191 | plot(t,Total_veh,'-', 'linewidth', 1.5); 192 | xlabel('Time [h]'); 193 | ylabel('Total vehicles [veh]') 194 | 195 | subplot(4,2,8) 196 | plot(t,Weather,'-','linewidth', 1.5); 197 | xlabel('Time [h]'); 198 | ylabel('Weather') 199 | 200 | figure 201 | plot(t, demando0(61:780), '-', 'linewidth', 1.5); 202 | hold on; 203 | plot(t, demando1(61:780), '-.', 'linewidth', 1.5); 204 | hold on; 205 | plot(t, demando2(61:780), ':', 'linewidth', 1.5); 206 | hold on; 207 | plot(t, demando3(61:780), '--', 'linewidth', 1.5); 208 | xlabel('Time [h]'); 209 | ylabel('Demand [veh/h]'); 210 | ylim([0 3500]); 211 | legend('O_0','O_1','O_2','O_3') -------------------------------------------------------------------------------- /Outflow_main.m: -------------------------------------------------------------------------------- 1 | function outflow = Outflow_main(demand,V,wo,v_control,para) 2 | %OUTFLOW_MAIN para=[T, lambda, rou_crit, am, v_free, alpha]; 3 | % 4 | T=para(1)/3600; 5 | lambda=para(2); 6 | rou_crit=para(3); 7 | am=para(4); 8 | v_free=para(5); 9 | alpha=para(6); 10 | V_desire=Desired_speed(rou_crit, [v_free, am, rou_crit, alpha], v_control); 11 | q_cap=lambda*V_desire*rou_crit; 12 | v_lim=min([v_control, V]); 13 | q_speed=lambda*v_lim*rou_crit*(-am*log(v_lim/v_free))^(1/am); 14 | q_lim=q_speed*(v_lim=V_desire); 15 | outflow=min([demand+wo/T, q_lim]); 16 | end 17 | 18 | -------------------------------------------------------------------------------- /Outflow_ramp.m: -------------------------------------------------------------------------------- 1 | function outflow = Outflow_ramp(demand,wo,Co,ro,rou,para) 2 | %OUTFLOW para=[T,rou_max,rou_crit]; 3 | % Detailed explanation goes here 4 | T=para(1)/3600; 5 | rou_max=para(2); 6 | rou_crit=para(3); 7 | outflow=min([demand+wo/T, Co*ro, Co*(rou_max-rou)/(rou_max-rou_crit)]); 8 | end 9 | 10 | -------------------------------------------------------------------------------- /PMPC_DQN_RM.m: -------------------------------------------------------------------------------- 1 | state_space=[32 1]; % Continuous states 2 | action_space=0:10; % Discrete actions 3 | obsInfo = rlNumericSpec(state_space); 4 | obsInfo.Name='Weather and density'; 5 | actInfo = rlFiniteSetSpec(action_space); 6 | actInfo.Name='Critical density'; 7 | 8 | criticNet=[featureInputLayer(prod(obsInfo.Dimension),'Normalization','none','Name','observation') 9 | fullyConnectedLayer(64,'Name','ActorFC1') 10 | reluLayer('Name','ActorRelu1') 11 | fullyConnectedLayer(256,'Name','ActorFC2') 12 | reluLayer('Name','ActorRelu2') 13 | fullyConnectedLayer(64,'Name','ActorFC3') 14 | reluLayer('Name','ActorRelu3') 15 | fullyConnectedLayer(numel(actInfo.Elements),'Name','ActorFC4') 16 | ]; 17 | critic = rlVectorQValueFunction(criticNet,obsInfo,actInfo); 18 | 19 | agent = rlDQNAgent(critic); % Default DQN agent 20 | 21 | agent.AgentOptions.UseDoubleDQN = false; 22 | agent.AgentOptions.TargetSmoothFactor = 1e-2; 23 | agent.AgentOptions.TargetUpdateFrequency = 10; 24 | agent.AgentOptions.ExperienceBufferLength = 1e5; 25 | agent.AgentOptions.MiniBatchSize = 512; 26 | agent.AgentOptions.CriticOptimizerOptions.LearnRate = 1e-3; 27 | agent.AgentOptions.CriticOptimizerOptions.GradientThreshold = 1; 28 | agent.AgentOptions.NumStepsToLookAhead=4; 29 | agent.AgentOptions.EpsilonGreedyExploration.EpsilonMin=0.01; 30 | agent.AgentOptions.EpsilonGreedyExploration.EpsilonDecay=0.005; 31 | 32 | env = rlFunctionEnv(obsInfo,actInfo,'DQN_RM_step','DQN_RM_reset'); 33 | 34 | trainOpts = rlTrainingOptions; 35 | trainOpts.MaxEpisodes = 2000; 36 | trainOpts.MaxStepsPerEpisode = 24; 37 | trainOpts.SaveAgentCriteria = 'AverageReward'; 38 | trainOpts.SaveAgentValue = -7000; 39 | 40 | Training = train(agent,env,trainOpts); -------------------------------------------------------------------------------- /PMPC_RM_VSL.m: -------------------------------------------------------------------------------- 1 | clear; 2 | % clc 3 | % close all 4 | %% Initialize 5 | Exit=[]; 6 | x=[zeros(62,1);0]; 7 | % ramp_only=0; 8 | u=[102,102,102,102,102,102,102,102,102,1,1,1]; 9 | initial_weather=3; 10 | % weather=initial_weather; % taken as the nominal weather 11 | end_weather=1; 12 | Weather_real=[ones(1,60) initial_weather*ones(1,360) end_weather*ones(1,510)]; 13 | Weather_predict=[ones(1,60) initial_weather*ones(1,870)]; 14 | Weather=Weather_predict; 15 | for i=1:60 16 | weather=Weather(x(63)+1); 17 | x=Freeway_model(x,u,1); % real model use the real weather 18 | end 19 | 20 | %% Parameters 21 | Np=3; % Np times M is the prediction horizon 22 | Nc=3; % Nc is the variables of theta during the prediction 23 | M=30; % MPC operation time: 30 simulation steps 24 | Mc=6; % control sampling time: 6 simulation steps 25 | N=720; 26 | % N_max=15; % multi-start point 27 | xx=[]; 28 | % [tao, kai, yita_high, yita_low, rou_max, sigma, am, v_free, rou_crit, alpha, T, lambda, Lm, ~, v_min, Co2, xi_r, xi_s,xi_theta,~] = parameters_real(weather); 29 | % dim_speed=6; 30 | % dim_ramp=3; 31 | % para=[Lm, lambda, v_free, M, dim_speed, T, xi_r, xi_s, weather, rou_crit, Mc, xi_theta]; 32 | % options = optimoptions(@fmincon,'Algorithm','sqp','Display','off','TolFun',1e-2, 'TolX',1e-2, 'TolCon', 1e-2); 33 | % 34 | % % v_control=repmat(100*ones(dim_speed,1),1,Nc); 35 | % % v_control_pre=v_control(:,1); 36 | % 37 | dim_theta=1; 38 | U=[]; 39 | Theta=zeros(dim_theta,N); 40 | u_pre=u'; % 41 | % % theta_lb=repmat([0;-100;-100;0],1,Nc); 42 | % % theta_ub=repmat([2;100;100;10],1,Nc); 43 | % % theta_pre=repmat(ones(dim_theta,1), 1, Nc); 44 | % theta_lb=repmat(-10,dim_theta,Nc); 45 | % theta_ub=repmat(10,dim_theta,Nc); 46 | theta_pre=repmat(ones(dim_theta,1), 1, Nc); 47 | %% 48 | for i=1:N/M 49 | weather=Weather(x(63)+1); 50 | [tao, kai, yita_high, yita_low, rou_max, sigma, am, v_free, rou_crit, alpha, T, lambda, Lm, ~, v_min, Co2, xi_r, xi_s,xi_theta,~]... 51 | = parameters_predict(weather); 52 | % rou_crit=39.5; 53 | % objfun=@(theta) ModelState(x, theta, Nc, Np, para, u_pre(:,1), theta_pre(:,1)); 54 | % confun=@(theta) ModelCons(x, theta, Nc, Np, para, u_pre(:,1)); 55 | % theta0=cell(1,N_max);theta_t=cell(1,N_max);Fval=nan(1,N_max);Exitflag=nan(1,N_max); 56 | % parfor l=1:N_max 57 | % if l==1 58 | % theta0{l}=theta_pre; 59 | % elseif l==2 60 | % theta0{l}=theta_lb; 61 | % elseif l==3 62 | % theta0{l}=theta_ub; 63 | % elseif l==4 64 | % theta0{l}=(theta_lb+theta_ub)/2; 65 | % else 66 | % % theta0{l}=repmat([2*rand;200*rand-100;200*rand-100;200*rand],1, Nc); 67 | % theta0{l}=repmat(20*rand-10,dim_theta, Nc); 68 | % end 69 | % [theta_t{l}, Fval(l), exitflag] = fmincon(objfun, theta0{l}, [], [], [], [], theta_lb, theta_ub, [], options); 70 | % if exitflag == 0 71 | % warning('the solver does not succeed'); 72 | % Fval(l)=Fval(l)+1e6; 73 | % elseif exitflag < 0 74 | % warning('the solver does not succeed'); 75 | % Fval(l)=Fval(l)+1e6; 76 | % end 77 | % Exitflag(l)=exitflag; 78 | % end 79 | % [fval, index]=min(Fval); 80 | % Exit=[Exit Exitflag']; 81 | % theta_opt=theta_t{index}(:,1); 82 | theta_opt=MPC_imp_RM(x,u_pre,theta_pre, Weather,rou_crit); % give the wrong weather, but rou_crit can be adjusted 83 | % u_opt=[v_control;[1 1 1]]; 84 | for j=1:M/Mc % every control step 85 | u=u_para_RM(theta_opt(:,1), x, u_pre, rou_crit, v_free); 86 | for k=1:Mc 87 | weather=Weather_real(x(63)+1); 88 | x=Freeway_model(x,u,weather); 89 | xx=[xx x]; 90 | % step=x(63)-60; 91 | % weather=(end_weather-initial_weather)/720*step+initial_weather; 92 | end 93 | u_pre=u; 94 | U=[U repmat(u,1,Mc)]; 95 | end 96 | 97 | Theta(:,M*(i-1)+1:M*i)=repmat(theta_opt(:,1),1,M); 98 | % theta_pre=theta_t{index}; 99 | theta_pre=theta_opt; 100 | end 101 | %% figure 102 | u_speed=U(1:9,:); 103 | u_ramp=U(10:12,:); 104 | rou_11=xx(1,:); 105 | v_11=xx(2,:); 106 | q_11=xx(3,:); 107 | rou_12=xx(4,:); 108 | v_12=xx(5,:); 109 | q_12=xx(6,:); 110 | rou_13=xx(7,:); 111 | v_13=xx(8,:); 112 | q_13=xx(9,:); 113 | rou_14=xx(10,:); 114 | v_14=xx(11,:); 115 | q_14=xx(12,:); 116 | rou_15=xx(13,:); 117 | v_15=xx(14,:); 118 | q_15=xx(15,:); 119 | rou_16=xx(16,:); 120 | v_16=xx(17,:); 121 | q_16=xx(18,:); 122 | rou_21=xx(19,:); 123 | v_21=xx(20,:); 124 | q_21=xx(21,:); 125 | rou_22=xx(22,:); 126 | v_22=xx(23,:); 127 | q_22=xx(24,:); 128 | rou_23=xx(25,:); 129 | v_23=xx(26,:); 130 | q_23=xx(27,:); 131 | rou_24=xx(28,:); 132 | v_24=xx(29,:); 133 | q_24=xx(30,:); 134 | rou_25=xx(31,:); 135 | v_25=xx(32,:); 136 | q_25=xx(33,:); 137 | rou_26=xx(34,:); 138 | v_26=xx(35,:); 139 | q_26=xx(36,:); 140 | rou_31=xx(37,:); 141 | v_31=xx(38,:); 142 | q_31=xx(39,:); 143 | rou_32=xx(40,:); 144 | v_32=xx(41,:); 145 | q_32=xx(42,:); 146 | rou_33=xx(43,:); 147 | v_33=xx(44,:); 148 | q_33=xx(45,:); 149 | rou_34=xx(46,:); 150 | v_34=xx(47,:); 151 | q_34=xx(48,:); 152 | rou_35=xx(49,:); 153 | v_35=xx(50,:); 154 | q_35=xx(51,:); 155 | rou_36=xx(52,:); 156 | v_36=xx(53,:); 157 | q_36=xx(54,:); 158 | q_o0=xx(55,:); 159 | w_o0=xx(56,:); 160 | q_o1=xx(57,:); 161 | w_o1=xx(58,:); 162 | q_o2=xx(59,:); 163 | w_o2=xx(60,:); 164 | q_o3=xx(61,:); 165 | w_o3=xx(62,:); 166 | Total_veh=(rou_11+rou_12+rou_13+rou_14+rou_15+rou_16+rou_21+rou_22+rou_23+rou_24+rou_25+rou_26+... 167 | rou_31+rou_32+rou_33+rou_34+rou_35+rou_36).*1000./1000.*2+w_o0+w_o1+w_o2+w_o3; 168 | TTS=10/3600.*Total_veh; 169 | %% 170 | fprintf('TTS is %.3f veh*h \n', sum(TTS)) 171 | figure(); 172 | t=1/360:1/360:2.0; 173 | subplot(4,2,1) 174 | plot(t, v_11, '-', 'linewidth', 1.5); 175 | hold on; 176 | plot(t, v_15, '--', 'linewidth', 1.5); 177 | hold on; 178 | plot(t, v_21, ':', 'linewidth', 1.5); 179 | hold on; 180 | plot(t, v_25, '-', 'linewidth', 1.5); 181 | hold on; 182 | plot(t, v_31, '--', 'linewidth', 1.5); 183 | hold on; 184 | plot(t, v_35, ':', 'linewidth', 1.5); 185 | legend('Segmeng 1-1','Segmeng 1-5','Segmeng 2-1','Segmeng 2-5','Segmeng 3-1','Segmeng 3-5') 186 | xlabel('Time [h]'); 187 | ylabel('Speed [km/h]') 188 | 189 | subplot(4,2,2) 190 | plot(t, q_11, '-', 'linewidth', 1.5); 191 | hold on; 192 | plot(t, q_15, '--', 'linewidth', 1.5); 193 | hold on; 194 | plot(t, q_21, ':', 'linewidth', 1.5); 195 | hold on; 196 | plot(t, q_25, '-', 'linewidth', 1.5); 197 | hold on; 198 | plot(t, q_31, '--', 'linewidth', 1.5); 199 | hold on; 200 | plot(t, q_35, ':', 'linewidth', 1.5); 201 | xlabel('Time [h]'); 202 | ylabel('Flow [veh/h]') 203 | 204 | subplot(4,2,3) 205 | plot(t, rou_11, '-', 'linewidth', 1.5); 206 | hold on; 207 | plot(t, rou_15, '--', 'linewidth', 1.5); 208 | hold on; 209 | plot(t, rou_21, ':', 'linewidth', 1.5); 210 | hold on; 211 | plot(t, rou_25, '-', 'linewidth', 1.5); 212 | hold on; 213 | plot(t, rou_31, '--', 'linewidth', 1.5); 214 | hold on; 215 | plot(t, rou_35, ':', 'linewidth', 1.5); 216 | xlabel('Time [h]'); 217 | ylabel('Density [veh/km]') 218 | 219 | subplot(4,2,4) 220 | % subplot(3,1,1) 221 | plot(t,Theta, 'linewidth', 1.5); 222 | xlabel('Time [h]'); 223 | ylabel('Theta'); 224 | % legend('0','1','2','3') 225 | % ylim([0 4000]) 226 | 227 | subplot(4,2,5) 228 | plot(t, q_o0, '-', 'linewidth', 1.5); 229 | hold on; 230 | plot(t, q_o1, '--', 'linewidth', 1.5); 231 | hold on; 232 | plot(t, q_o2, '-.', 'linewidth', 1.5); 233 | hold on; 234 | plot(t, q_o3, ':', 'linewidth', 1.5); 235 | legend('O_0','O_1','O_2','O_3') 236 | xlabel('Time [h]'); 237 | ylabel('Outflow [veh/h]') 238 | % ylim([0 4000]) 239 | 240 | subplot(4,2,6) 241 | plot(t,w_o0,'-', 'linewidth', 1.5); 242 | hold on; 243 | plot(t,w_o1,'--', 'linewidth', 1.5); 244 | hold on; 245 | plot(t,w_o2,'-.', 'linewidth', 1.5); 246 | hold on; 247 | plot(t,w_o3,':', 'linewidth', 1.5); 248 | legend('O_0','O_1','O_2','O_3') 249 | xlabel('Time [h]'); 250 | ylabel('Queue length [veh]') 251 | 252 | subplot(4,2,7) 253 | % subplot(3,1,2) 254 | plot(t,u_speed,'-', 'linewidth', 1.5); 255 | legend('v-11','v-12','v-21','v-22','v-31','v-32'); 256 | xlabel('Time [h]'); 257 | ylabel('Speed limits') 258 | % 259 | subplot(4,2,8) 260 | % subplot(3,1,3) 261 | plot(t,u_ramp,'-','linewidth',1.0); 262 | legend('O_1','O_2','O_3'); 263 | ylim([0 1]); 264 | xlabel('Time [h]'); 265 | ylabel('Ramp metering') 266 | %% 267 | function J=ModelState(x, theta, Nc, Np, para, u_pre, theta_pre) 268 | Lm=para(1); 269 | Lambda=para(2); 270 | v_free=para(3); 271 | M=para(4); 272 | dim_speed=para(5); 273 | T=para(6); 274 | xi_r=para(7); 275 | xi_s=para(8); 276 | weather=para(9); 277 | rou_crit=para(10); 278 | Mc=para(11); 279 | xi_theta=para(12); 280 | % uc=[u repelem(u(:, end),1,Np-Nc)]; 281 | % uc_full=repelem(uc,1,M); 282 | theta_full=repelem(theta,1,Np/Nc); 283 | yy=[]; 284 | Diff=zeros(size(u_pre,1),Np); % the erros between the successive control inputs 285 | Diff_theta=zeros(size(theta_pre,1),Nc); 286 | uc_full=zeros(size(u_pre,1),Np); 287 | for i=1:Np 288 | for j=1:M/Mc 289 | u=u_para(theta_full(:,i), x, u_pre, rou_crit); 290 | uc_full(:,i)=u; 291 | for k=1:Mc 292 | % step=x(63)-60; 293 | % initial_weather=0; 294 | % end_weather=2; 295 | % weather=(end_weather-initial_weather)/720*step+initial_weather; 296 | x=Freeway_model(x,u,weather,0); 297 | yy=[yy x]; 298 | end 299 | u_pre=u; 300 | end 301 | end 302 | u_diff=[u_pre uc_full]; 303 | theta_diff=[theta_pre theta]; 304 | % for i=1:Np 305 | % Diff(:,i)=u_diff(:,i+1)-u_diff(:,i); 306 | % end 307 | for i=1:Nc 308 | Diff_theta(:,i)=theta_diff(:,i+1)-theta_diff(:,i); 309 | end 310 | rou_11=yy(1,:);rou_12=yy(4,:);rou_13=yy(7,:);rou_14=yy(10,:);rou_15=yy(13,:);rou_16=yy(16,:); 311 | rou_21=yy(19,:);rou_22=yy(22,:);rou_23=yy(25,:);rou_24=yy(28,:);rou_25=yy(31,:);rou_26=yy(34,:); 312 | rou_31=yy(37,:);rou_32=yy(40,:);rou_33=yy(43,:);rou_34=yy(46,:);rou_35=yy(49,:);rou_36=yy(52,:); 313 | w_o0=yy(56,:);w_o1=yy(58,:);w_o2=yy(60,:);w_o3=yy(62,:); 314 | TTS=(sum((rou_11+rou_12+rou_13+rou_14+rou_15+rou_16+rou_21+rou_22+rou_23+rou_24+rou_25+rou_26... 315 | +rou_31+rou_32+rou_33+rou_34+rou_35+rou_36))*Lm/1000*Lambda+sum(w_o0+w_o1+w_o2+w_o3))*T/3600; 316 | % Diff_speed=Diff(1:dim_speed,:); 317 | % Diff_r=Diff(dim_speed+1:end,:); 318 | % penalty=xi_r*sum(sum(Diff_r.^2))+xi_s*sum(sum((Diff_speed/v_free).^2)); 319 | penalty=xi_theta*sum(sum(Diff_theta.^2)); 320 | J=TTS+penalty; 321 | end 322 | function [c, ceq]=ModelCons(x, theta, Nc, Np, para,u_pre) 323 | ceq=[]; 324 | M=para(4); 325 | weather=para(9); 326 | rou_crit=para(10); 327 | % uc=[theta repelem(theta(:, end),1,Np-Nc)]; 328 | % uc_full=repelem(uc,1,M); 329 | yy=zeros(size(x,1),Np*M); 330 | theta_full=repelem(theta,1,Np/Nc); 331 | u_vsl=zeros(6, Np); 332 | u_rm=zeros(3,Np); 333 | for i=1:Np 334 | u=u_para(theta_full(:,i), x, u_pre, rou_crit); 335 | u_vsl(:,i)=u(1:6); 336 | u_rm(:,i)=u(7:9); 337 | for j=1:M 338 | x=Freeway_model(x,u,weather); 339 | yy(:,j+M*(i-1))=x; 340 | end 341 | end 342 | w_o0=yy(56,:);w_o1=yy(58,:); 343 | w_o2=yy(60,:);w_o3=yy(62,:); 344 | c=[w_o0'-300;w_o1'-200;w_o2'-200;w_o3'-200]; 345 | % u_vsl_full=u_vsl(:); 346 | % u_rm_full=u_rm(:); 347 | % c=[u_vsl_full-102;20-u_vsl_full;u_rm_full-1;-u_rm_full]; 348 | end 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | -------------------------------------------------------------------------------- /Para_RM_control_law.m: -------------------------------------------------------------------------------- 1 | function u_rm = Para_RM_control_law(theta, u_rm_pre, rou_crit, rou,w) 2 | %PARAMETERIZED_CONTROL_LAW Summary of this function goes here 3 | % Detailed explanation goes here 4 | u_rm=u_rm_pre+theta(1)*(rou_crit-rou); 5 | end 6 | 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RL-based-PMPC 2 | ## Case study codes of the paper "Adaptive Parameterized Model Predictive Control Based on Reinforcement Learning: A Synthesis Framework" 3 | 4 | There are 6 controllers for comparison, each of which corresponds to a .m file: 5 | 6 | No-control case --> Main_openloop.m 7 | ALINEA controller --> ALINEA.m 8 | Standalone MPC controller --> MPC_RM_VSL.m 9 | Standalone PMPC controller --> PMPC_RM_VSL.m 10 | Standalone RL controller --> Ramp_metering_learning.m 11 | RL-PMPC controller --> PMPC_DQN_RM.m 12 | 13 | Note that Ramp_metering_learning.m and PMPC_DQN_RM.m are the files to start the learning process. After the learning is finished, one implement the learnt agent to validate its control performance. To validate the RL-based controller, run the DQN_RM_validate.m file; to validate the RL-PMPC controller, run the DQN_PMPC_validate.m file. Please make sure that the corresponding agent is loaded before validating. 14 | 15 | Also notice that set the correct weather scenarios before simulating, which can be specified in each file separately. 16 | -------------------------------------------------------------------------------- /Ramp_metering_learning.m: -------------------------------------------------------------------------------- 1 | state_space=[29 1]; % Continuous states 2 | action_space=0:10; % Discrete actions 3 | obsInfo = rlNumericSpec(state_space); 4 | obsInfo.Name='Weather and density'; 5 | actInfo = rlFiniteSetSpec(action_space); 6 | actInfo.Name='Critical density'; 7 | 8 | criticNet=[featureInputLayer(prod(obsInfo.Dimension),'Normalization','none','Name','observation') 9 | fullyConnectedLayer(64,'Name','ActorFC1') 10 | reluLayer('Name','ActorRelu1') 11 | fullyConnectedLayer(256,'Name','ActorFC2') 12 | reluLayer('Name','ActorRelu2') 13 | fullyConnectedLayer(64,'Name','ActorFC3') 14 | reluLayer('Name','ActorRelu3') 15 | fullyConnectedLayer(numel(actInfo.Elements),'Name','ActorFC4') 16 | ]; 17 | critic = rlVectorQValueFunction(criticNet,obsInfo,actInfo); 18 | 19 | agent = rlDQNAgent(critic); % Default DQN agent 20 | 21 | agent.AgentOptions.UseDoubleDQN = false; 22 | agent.AgentOptions.TargetSmoothFactor = 1e-2; 23 | agent.AgentOptions.TargetUpdateFrequency = 10; 24 | agent.AgentOptions.ExperienceBufferLength = 1e5; 25 | agent.AgentOptions.MiniBatchSize = 512; 26 | agent.AgentOptions.CriticOptimizerOptions.LearnRate = 1e-3; 27 | agent.AgentOptions.CriticOptimizerOptions.GradientThreshold = 1; 28 | agent.AgentOptions.NumStepsToLookAhead=10; 29 | agent.AgentOptions.EpsilonGreedyExploration.EpsilonMin=0.01; 30 | agent.AgentOptions.EpsilonGreedyExploration.EpsilonDecay=0.005; 31 | 32 | env = rlFunctionEnv(obsInfo,actInfo,'Ramp_step','Ramp_reset'); 33 | 34 | trainOpts = rlTrainingOptions; 35 | trainOpts.MaxEpisodes = 2000; 36 | trainOpts.MaxStepsPerEpisode = 120; 37 | trainOpts.SaveAgentCriteria = 'AverageReward'; 38 | trainOpts.SaveAgentValue = -7000; 39 | 40 | Training = train(agent,env,trainOpts); 41 | 42 | % save('Saved_results/DQN_RM_weather23_realmodel.mat'); -------------------------------------------------------------------------------- /Ramp_reset.m: -------------------------------------------------------------------------------- 1 | function [InitialObservation,LoggedSignals] = Ramp_reset 2 | initial_weather=1; 3 | end_weather=3; 4 | weather=initial_weather; 5 | 6 | x=[zeros(62,1);0]; 7 | u_rm_initial=[1 1 1]'; 8 | u_initial=[102; 102; 102; 102; 102; 102; 102; 102; 102; u_rm_initial]; 9 | for i=1:60 10 | x=Freeway_model(x, u_initial, 1); 11 | end 12 | 13 | k=x(63); 14 | rou_11=x(1); 15 | v_11=x(2); 16 | q_11=x(3); 17 | rou_12=x(4); 18 | v_12=x(5); 19 | q_12=x(6); 20 | rou_13=x(7); 21 | v_13=x(8); 22 | q_13=x(9); 23 | rou_14=x(10); 24 | v_14=x(11); 25 | q_14=x(12); 26 | rou_15=x(13); 27 | v_15=x(14); 28 | q_15=x(15); 29 | rou_16=x(16); 30 | v_16=x(17); 31 | q_16=x(18); 32 | rou_21=x(19); 33 | v_21=x(20); 34 | q_21=x(21); 35 | rou_22=x(22); 36 | v_22=x(23); 37 | q_22=x(24); 38 | rou_23=x(25); 39 | v_23=x(26); 40 | q_23=x(27); 41 | rou_24=x(28); 42 | v_24=x(29); 43 | q_24=x(30); 44 | rou_25=x(31); 45 | v_25=x(32); 46 | q_25=x(33); 47 | rou_26=x(34); 48 | v_26=x(35); 49 | q_26=x(36); 50 | rou_31=x(37); 51 | v_31=x(38); 52 | q_31=x(39); 53 | rou_32=x(40); 54 | v_32=x(41); 55 | q_32=x(42); 56 | rou_33=x(43); 57 | v_33=x(44); 58 | q_33=x(45); 59 | rou_34=x(46); 60 | v_34=x(47); 61 | q_34=x(48); 62 | rou_35=x(49); 63 | v_35=x(50); 64 | q_35=x(51); 65 | rou_36=x(52); 66 | v_36=x(53); 67 | q_36=x(54); 68 | w_o0=x(56,:); 69 | w_o1=x(58); 70 | w_o2=x(60); 71 | w_o3=x(62); 72 | 73 | norm_state=[rou_13 v_13 rou_14 v_14 rou_15 v_15 rou_23 v_23 rou_24 v_24 rou_25 v_25 rou_33 v_33 rou_34 v_34 rou_35 v_35]'/100; 74 | norm_queue=[w_o0;w_o1;w_o2;w_o3]/100; 75 | norm_demand=[demando0(k)/1000; demando1(k)/1000; demando2(k)/1000; demando3(k)/1000]; 76 | 77 | LoggedSignals.k=k; 78 | LoggedSignals.x=x; 79 | LoggedSignals.weather=weather; 80 | LoggedSignals.end_weather=end_weather; 81 | InitialObservation=[norm_state; norm_demand;norm_queue;1;rou_36_down(k)/100;weather]; 82 | end 83 | 84 | -------------------------------------------------------------------------------- /Ramp_step.m: -------------------------------------------------------------------------------- 1 | function [Observation,Reward,IsDone,LoggedSignals] = Ramp_step(u_rm,LoggedSignals) 2 | k=LoggedSignals.k; 3 | x=LoggedSignals.x; 4 | end_weather=LoggedSignals.end_weather; 5 | weather=LoggedSignals.weather; 6 | % rou_crit=33.5; 7 | xx=zeros(63,6); 8 | % u_theta=sat_theta(u_theta); % define sat_theta 9 | % step=k-60; 10 | % weather=(end_weather-initial_weather)/720*step+initial_weather; 11 | % implememt u_theta 12 | % weather=initial_weather; 13 | u_rm=u_rm/10; 14 | u_vslrm=[102; 102; 102; 102; 102; 102; 102; 102; 102; u_rm; u_rm; u_rm]; 15 | % implement u_vslrm 16 | for j=1:6 % control sampling time: 1min 17 | x=Freeway_model_predict(x,u_vslrm, weather); % use the true weather condition 18 | k=k+1; 19 | xx(:,j)=x; 20 | end 21 | % calculate the mpc input for next step 22 | if k>=420 23 | weather=end_weather; 24 | end 25 | % calculate the reward: TTS 26 | rou_11=xx(1,:);rou_12=xx(4,:);rou_13=xx(7,:);rou_14=xx(10,:);rou_15=xx(13,:);rou_16=xx(16,:); 27 | rou_21=xx(19,:);rou_22=xx(22,:);rou_23=xx(25,:);rou_24=xx(28,:);rou_25=xx(31,:);rou_26=xx(34,:); 28 | rou_31=xx(37,:);rou_32=xx(40,:);rou_33=xx(43,:);rou_34=xx(46,:);rou_35=xx(49,:);rou_36=xx(52,:); 29 | w_o0=xx(56,:);w_o1=xx(58,:);w_o2=xx(60,:);w_o3=xx(62,:); 30 | TTS=(sum((rou_11+rou_12+rou_13+rou_14+rou_15+rou_16+rou_21+rou_22+rou_23+rou_24+rou_25+rou_26... 31 | +rou_31+rou_32+rou_33+rou_34+rou_35+rou_36))*1000/1000*2+sum(w_o0+w_o1+w_o2+w_o3))*10/3600; 32 | % penalty=-10.*(max(w_o1-100)>0)-10.*(max(w_o2-100)>0)-10.*(max(w_o3-100)>0); 33 | Reward=-TTS; 34 | 35 | rou_11=x(1); 36 | v_11=x(2); 37 | q_11=x(3); 38 | rou_12=x(4); 39 | v_12=x(5); 40 | q_12=x(6); 41 | rou_13=x(7); 42 | v_13=x(8); 43 | q_13=x(9); 44 | rou_14=x(10); 45 | v_14=x(11); 46 | q_14=x(12); 47 | rou_15=x(13); 48 | v_15=x(14); 49 | q_15=x(15); 50 | rou_16=x(16); 51 | v_16=x(17); 52 | q_16=x(18); 53 | rou_21=x(19); 54 | v_21=x(20); 55 | q_21=x(21); 56 | rou_22=x(22); 57 | v_22=x(23); 58 | q_22=x(24); 59 | rou_23=x(25); 60 | v_23=x(26); 61 | q_23=x(27); 62 | rou_24=x(28); 63 | v_24=x(29); 64 | q_24=x(30); 65 | rou_25=x(31); 66 | v_25=x(32); 67 | q_25=x(33); 68 | rou_26=x(34); 69 | v_26=x(35); 70 | q_26=x(36); 71 | rou_31=x(37); 72 | v_31=x(38); 73 | q_31=x(39); 74 | rou_32=x(40); 75 | v_32=x(41); 76 | q_32=x(42); 77 | rou_33=x(43); 78 | v_33=x(44); 79 | q_33=x(45); 80 | rou_34=x(46); 81 | v_34=x(47); 82 | q_34=x(48); 83 | rou_35=x(49); 84 | v_35=x(50); 85 | q_35=x(51); 86 | rou_36=x(52); 87 | v_36=x(53); 88 | q_36=x(54); 89 | w_o0=x(56,:); 90 | w_o1=x(58); 91 | w_o2=x(60); 92 | w_o3=x(62); 93 | 94 | norm_state=[rou_13 v_13 rou_14 v_14 rou_15 v_15 rou_23 v_23 rou_24 v_24 rou_25 v_25 rou_33 v_33 rou_34 v_34 rou_35 v_35]'/100; 95 | norm_queue=[w_o0;w_o1;w_o2;w_o3]/100; 96 | norm_demand=[demando0(k)/1000; demando1(k)/1000; demando2(k)/1000; demando3(k)/1000]; 97 | Observation=[norm_state; norm_demand;norm_queue;u_rm;rou_36_down(k)/100;weather]; 98 | 99 | if k>779 100 | IsDone=true; 101 | else 102 | IsDone=false; 103 | end 104 | 105 | LoggedSignals.k=k; 106 | LoggedSignals.x=x; 107 | LoggedSignals.weather=weather; 108 | LoggedSignals.end_weather=end_weather; 109 | end 110 | 111 | -------------------------------------------------------------------------------- /Speed.m: -------------------------------------------------------------------------------- 1 | function V_ = Speed(V,V_desire,V_up,rou,rou_down,para,q_o,v_min) 2 | %SPEED para=[T,tao,yita,kai,Lm,sigma,lambda]; 3 | % 4 | T=para(1)/3600; 5 | tao=para(2)/3600; 6 | yita_high=para(3); 7 | yita_low=para(4); 8 | kai=para(5); 9 | Lm=para(6)/1000; 10 | sigma=para(7); 11 | lambda=para(8); 12 | if rou_down>= rou 13 | yita=yita_high; 14 | else 15 | yita=yita_low; 16 | end 17 | V_=max(v_min, V... 18 | +T/tao*(V_desire-V)... 19 | +T/Lm*V*(V_up-V)... 20 | -(yita*T)/(tao*Lm)*(rou_down-rou)/(rou+kai)... 21 | -sigma*T*q_o*V/(Lm*lambda*(rou+kai))); 22 | end 23 | 24 | -------------------------------------------------------------------------------- /demando0.m: -------------------------------------------------------------------------------- 1 | function demand = demando0(k) 2 | %DEMANDO1 Summary of this function goes here 3 | % Detailed explanation goes here 4 | % k=k-1; 5 | k=k-60; 6 | Max=3080; 7 | t1=360; 8 | t2=450; 9 | demand=Max.*(k<=t1)+(Max-(Max-1000)/(t2-t1)*(k-t1)).*(k>t1 & k<=t2)+1000.*(k>t2); 10 | end 11 | 12 | -------------------------------------------------------------------------------- /demando1.m: -------------------------------------------------------------------------------- 1 | function demand = demando1(k) 2 | %DEMANDO2 Summary of this function goes here 3 | % Detailed explanation goes here 4 | % k=k-1; 5 | k=k-60; 6 | % demand=0; 7 | max=1500; 8 | min=500; 9 | demand=750.*(k<0)+(min+(max-min)/(40-0)*k).*(k>=0 & k<=40)+max.*(k>40 & k<130)+(max-(max-min)/(170-130)*(k-130)).*(k>=130 & k<=170)+min.*(k>170); 10 | end 11 | 12 | -------------------------------------------------------------------------------- /demando2.m: -------------------------------------------------------------------------------- 1 | function demand = demando2(k) 2 | %DEMANDO2 Summary of this function goes here 3 | % Detailed explanation goes here 4 | % k=k-1; 5 | k=k-60; 6 | % demand=0; 7 | max=1600; 8 | min=400; 9 | t1=0;t2=t1+40;t3=t2+90;t4=t3+40; 10 | demand=500.*(k<0)+min.*(k>=0 & k=t1 & k<=t2)+max.*(k>t2 & k<=t3)... 11 | +(max-(max-min)/(t4-t3)*(k-t3)).*(k>t3 & k<=t4)+min.*(k>t4); 12 | end 13 | 14 | -------------------------------------------------------------------------------- /demando3.m: -------------------------------------------------------------------------------- 1 | function demand = demando3(k) 2 | %DEMANDO2 Summary of this function goes here 3 | % Detailed explanation goes here 4 | % k=k-1; 5 | k=k-60; 6 | % demand=0; 7 | max=1400; 8 | min=600; 9 | t1=0;t2=t1+40;t3=t2+90;t4=t3+40; 10 | demand=500.*(k<0)+min.*(k>=0 & k=t1 & k<=t2)+max.*(k>t2 & k<=t3)... 11 | +(max-(max-min)/(t4-t3)*(k-t3)).*(k>t3 & k<=t4)+min.*(k>t4); 12 | end 13 | 14 | -------------------------------------------------------------------------------- /parameters_predict.m: -------------------------------------------------------------------------------- 1 | function [tao, kai, yita_high, yita_low, rou_max, sigma, am, v_free, rou_crit, alpha, T, lambda, Lm, v_control, v_min, Co2, xi_ramp, xi_speed, xi_theta, off_rate] = parameters_predict(weather) 2 | % tao=18+9*weather; % unit: s 3 | % kai=40+20*weather; 4 | % yita_high=65; % km^2/h 5 | % yita_low=30-weather*5; 6 | % rou_max=180-weather*30; % veh/km/lane 7 | % sigma=0.1+weather*0.2; 8 | % am=1.867-weather*0.2; 9 | % v_free=102-10*weather; % km/h 10 | % rou_crit=33.5-6*weather; % veh/km/lane 11 | % alpha=0.1-0.1*weather; 12 | T=10; % unit: s 13 | lambda=2; 14 | % Lm=1000-50*weather; % unit: m 15 | v_control=200; % km/h 16 | v_min=7; 17 | % ro=1; 18 | % Co1=4000; % veh/h 19 | Co2=2000; % veh/h 20 | xi_ramp=0.4; 21 | xi_speed=0.4; 22 | xi_theta=1; 23 | off_rate=0.95; 24 | switch weather 25 | case 1 26 | tao=18*1.05; 27 | kai=40*1.05; 28 | yita_high=65*1.05; 29 | yita_low=30*1.05; 30 | rou_max=180*1.05; 31 | sigma=0.1*1.05; 32 | am=1.867*1.05; 33 | v_free=102*1.05; 34 | rou_crit=33.5*1.05; 35 | alpha=0.1*1.05; 36 | Lm=1000*1.05; 37 | case 2 38 | tao=27*1.05; 39 | kai=60*1.05; 40 | yita_high=55*1.05; 41 | yita_low=25*1.05; 42 | rou_max=150*1.05; 43 | sigma=0.3*1.05; 44 | am=1.667*1.05; 45 | v_free=92*1.05; 46 | rou_crit=26.5*1.05; 47 | alpha=0*1.05; 48 | Lm=900*1.05; 49 | case 3 50 | tao=36*1.05; 51 | kai=80*1.05; 52 | yita_high=45*1.05; 53 | yita_low=20*1.05; 54 | rou_max=120*1.05; 55 | sigma=0.5*1.05; 56 | am=1.467*1.05; 57 | v_free=82*1.05; 58 | rou_crit=19.5*1.05; 59 | alpha=-0.1*1.05; 60 | Lm=800*1.05; 61 | end 62 | 63 | end 64 | 65 | 66 | -------------------------------------------------------------------------------- /parameters_real.m: -------------------------------------------------------------------------------- 1 | function [tao, kai, yita_high, yita_low, rou_max, sigma, am, v_free, rou_crit, alpha, T, lambda, Lm, v_control, v_min, Co2, xi_ramp, xi_speed, xi_theta, off_rate] = parameters_real(weather) 2 | % tao=18+9*weather; % unit: s 3 | % kai=40+20*weather; 4 | % yita_high=65; % km^2/h 5 | % yita_low=30-weather*5; 6 | % rou_max=180-weather*30; % veh/km/lane 7 | % sigma=0.1+weather*0.2; 8 | % am=1.867-weather*0.2; 9 | % v_free=102-10*weather; % km/h 10 | % rou_crit=33.5-6*weather; % veh/km/lane 11 | % alpha=0.1-0.1*weather; 12 | T=10; % unit: s 13 | lambda=2; 14 | % Lm=1000-50*weather; % unit: m 15 | v_control=200; % km/h 16 | v_min=7; 17 | % ro=1; 18 | % Co1=4000; % veh/h 19 | Co2=2000; % veh/h 20 | xi_ramp=0.4; 21 | xi_speed=0.4; 22 | xi_theta=1; 23 | off_rate=0.95; 24 | switch weather 25 | case 1 26 | tao=18; 27 | kai=40; 28 | yita_high=65; 29 | yita_low=30; 30 | rou_max=180; 31 | sigma=0.1; 32 | am=1.867; 33 | v_free=102; 34 | rou_crit=33.5; 35 | alpha=0.1; 36 | Lm=1000; 37 | case 2 38 | tao=27; 39 | kai=60; 40 | yita_high=55; 41 | yita_low=25; 42 | rou_max=150; 43 | sigma=0.3; 44 | am=1.667; 45 | v_free=92; 46 | rou_crit=26.5; 47 | alpha=0; 48 | Lm=900; 49 | case 3 50 | tao=36; 51 | kai=80; 52 | yita_high=45; 53 | yita_low=20; 54 | rou_max=120; 55 | sigma=0.5; 56 | am=1.467; 57 | v_free=82; 58 | rou_crit=19.5; 59 | alpha=-0.1; 60 | Lm=800; 61 | end 62 | 63 | end 64 | 65 | 66 | -------------------------------------------------------------------------------- /rou_36_down.m: -------------------------------------------------------------------------------- 1 | function rou = rou_36_down(k) 2 | k=k-0; 3 | rou_min=33.5;rou_max=75;t1=250;t2=t1+36;t3=t2+36; 4 | rou=rou_min.*(k<=t1)+(rou_min+((rou_max-rou_min)/(t2-t1))*(k-t1)).*(k>t1 & k<=t2)+(rou_max-((rou_max-rou_min)/(t3-t2))*(k-t2)).*(k>t2 & k<=t3)+rou_min.*(k>t3); 5 | end 6 | 7 | -------------------------------------------------------------------------------- /sat_rm.m: -------------------------------------------------------------------------------- 1 | function sat_A = sat_rm(A) 2 | if A>=1 3 | sat_A=1; 4 | elseif A<=0 5 | sat_A=0; 6 | else 7 | sat_A=A; 8 | end 9 | end 10 | 11 | -------------------------------------------------------------------------------- /u_para_RM.m: -------------------------------------------------------------------------------- 1 | function u = u_para_RM(theta,x,u_pre,rou_crit, v_free) 2 | %Map the states to control input according to theta 3 | % Detailed explanation goes here 4 | % u_vsl_pre_11=u_pre(1); 5 | % u_vsl_pre_12=u_pre(2); 6 | % u_vsl_pre_21=u_pre(3); 7 | % u_vsl_pre_22=u_pre(4); 8 | % u_vsl_pre_31=u_pre(5); 9 | % u_vsl_pre_32=u_pre(6); 10 | u_rm_pre_1=u_pre(10); 11 | u_rm_pre_2=u_pre(11); 12 | u_rm_pre_3=u_pre(12); 13 | 14 | rou_12=x(4); 15 | v_12=x(5); 16 | rou_13=x(7); 17 | v_13=x(8); 18 | rou_14=x(10); 19 | v_14=x(11); 20 | rou_15=x(13); 21 | v_15=x(14); 22 | rou_22=x(22); 23 | v_22=x(23); 24 | rou_23=x(25); 25 | v_23=x(26); 26 | rou_24=x(28); 27 | v_24=x(29); 28 | rou_25=x(31); 29 | v_25=x(32); 30 | rou_32=x(40); 31 | v_32=x(41); 32 | rou_33=x(43); 33 | v_33=x(44); 34 | rou_34=x(46); 35 | v_34=x(47); 36 | rou_35=x(49); 37 | v_35=x(50); 38 | w_o1=x(58); 39 | w_o2=x(60); 40 | w_o3=x(62); 41 | 42 | % u_vsl_11=Para_VSL_control_law(theta(1:3), u_vsl_pre_11, rou_13, v_13, rou_14, v_14); 43 | % u_vsl_12=Para_VSL_control_law(theta(1:3), u_vsl_pre_12, rou_14, v_14, rou_15, v_15); 44 | % u_vsl_21=Para_VSL_control_law(theta(1:3), u_vsl_pre_21, rou_23, v_23, rou_24, v_24); 45 | % u_vsl_22=Para_VSL_control_law(theta(1:3), u_vsl_pre_22, rou_24, v_24, rou_25, v_25); 46 | % u_vsl_31=Para_VSL_control_law(theta(1:3), u_vsl_pre_31, rou_33, v_33, rou_34, v_34); 47 | % u_vsl_32=Para_VSL_control_law(theta(1:3), u_vsl_pre_32, rou_34, v_34, rou_35, v_35); 48 | u_rm_1=Para_RM_control_law(theta(1), u_rm_pre_1, rou_crit, rou_15, w_o1); 49 | u_rm_2=Para_RM_control_law(theta(1), u_rm_pre_2, rou_crit, rou_25, w_o2); 50 | u_rm_3=Para_RM_control_law(theta(1), u_rm_pre_3, rou_crit, rou_35, w_o3); 51 | 52 | % u_vsl_11=sat_vsl(u_vsl_11); 53 | % u_vsl_12=sat_vsl(u_vsl_12); 54 | % u_vsl_21=sat_vsl(u_vsl_21); 55 | % u_vsl_22=sat_vsl(u_vsl_22); 56 | % u_vsl_31=sat_vsl(u_vsl_31); 57 | % u_vsl_32=sat_vsl(u_vsl_32); 58 | u_vsl_11=102; 59 | u_vsl_12=102; 60 | u_vsl_13=102; 61 | u_vsl_21=102; 62 | u_vsl_22=102; 63 | u_vsl_23=102; 64 | u_vsl_31=102; 65 | u_vsl_32=102; 66 | u_vsl_33=102; 67 | u_rm_1=sat_rm(u_rm_1); 68 | u_rm_2=sat_rm(u_rm_2); 69 | u_rm_3=sat_rm(u_rm_3); 70 | % u_rm_1=1; 71 | % u_rm_2=1; 72 | % u_rm_3=1; 73 | 74 | u=[u_vsl_11 u_vsl_12 u_vsl_13 u_vsl_21 u_vsl_22 u_vsl_23 u_vsl_31 u_vsl_32 u_vsl_33 u_rm_1 u_rm_2 u_rm_3]'; 75 | 76 | end 77 | 78 | --------------------------------------------------------------------------------