-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGenerateDemandMatrix.m
131 lines (120 loc) · 6.01 KB
/
GenerateDemandMatrix.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
close all
clear
clc
SYSTEMS = {'Minneapolis'; 'Denver'; 'London'}; % bicycle sharing systems to import data from
INTERVALS = [minutes(1); minutes(5); minutes(10); minutes(15)]; % time interval to iterate over
for s = 1:length(SYSTEMS)
for m = 1:length(INTERVALS)
% Set which bicyle sharing system to import data from
System = SYSTEMS{s}; % options: 'Minneapolis', 'Denver', 'London'
% Set timing parameters - make sure averageLength is a multiple of timeInterval
timeInterval = INTERVALS(m); % possible formats: minutes(x), hours(x), days(x)
averageLength = days(1); % possible formats: minutes(x), hours(x), days(x)
% Set starting and ending dates to average the demand over
if strcmp(System,'Minneapolis')
startingDate = datetime(2016,4,4,0,0,0);
endingDate = datetime(2016,11,6,23,59,59);
elseif strcmp(System,'Denver')
startingDate = datetime(2016,1,1,0,0,0);
endingDate = datetime(2016,12,31,23,59,59);
elseif strcmp(System,'London')
startingDate = datetime(2017,9,1,0,0,0);
endingDate = datetime(2017,9,30,23,59,59);
end
% Initialization and data import
if strcmp(System,'Minneapolis')
[Startdate,~,Startstationnumber,Enddate,~,Endstationnumber,~,~] = ImportMinneapolisData('Nice_ride_trip_history_2016_season.csv');
StartDate = Startdate;
EndDate = Enddate;
StartStationId = Startstationnumber;
EndStationId = Endstationnumber;
elseif strcmp(System,'Denver')
[~,~,~,~,CheckoutDate,CheckoutTime,CheckoutKiosk,ReturnDate,ReturnTime,ReturnKiosk,~] = ImportDenverData('2016denverbcycletripdata_public.csv');
CheckoutDate.Format = 'MM/dd/yyyy HH:mm';
StartDate = CheckoutDate + timeofday(CheckoutTime);
ReturnDate.Format = 'MM/dd/yyyy HH:mm';
EndDate = ReturnDate + timeofday(ReturnTime);
StartStationId = CheckoutKiosk;
EndStationId = ReturnKiosk;
elseif strcmp(System,'London')
filename{1} = '73JourneyDataExtract30Aug2017-05Sep2017.csv';
filename{2} = '74JourneyDataExtract06Sep2017-12Sep2017.csv';
filename{3} = '75JourneyDataExtract13Sep2017-19Sep2017.csv';
filename{4} = '76JourneyDataExtract20Sep2017-26Sep2017.csv';
filename{5} = '77JourneyDataExtract27Sep2017-03Oct2017.csv';
StartDate = [];
EndDate = [];
StartStationId = [];
EndStationId = [];
for k = 1:length(filename)
[~,~,~,TempEndDate,TempEndStationId,~,TempStartDate,TempStartStationId,~] = ImportLondonData(filename{k});
StartDate = [StartDate; TempStartDate];
EndDate = [EndDate; TempEndDate];
StartStationId = [StartStationId; TempStartStationId];
EndStationId = [EndStationId; TempEndStationId];
end
end
% Sort stations by ID number
if ~iscategorical([StartStationId;EndStationId])
StationIds = unique([StartStationId(~isnan(StartStationId));EndStationId(~isnan(EndStationId))]);
else
StationIds = unique([StartStationId;EndStationId]);
end
% Create start station demand matrix
[SortedStartDate,I] = sort(StartDate);
SortedStartStationId = StartStationId(I);
SortedEndStationId = EndStationId(I);
StartDemand = zeros(length(StationIds),length(StationIds),averageLength/timeInterval);
initialDate = startingDate;
initial_Date = startingDate;
i = 1;
k = 1;
regularization = 1;
while (i <= length(SortedStartDate)) && (SortedStartDate(i) <= endingDate)
while (i <= length(SortedStartDate)) && (SortedStartDate(i) < initialDate + timeInterval)
StartDemand(SortedStartStationId(i)==StationIds,SortedEndStationId(i)==StationIds,k) = StartDemand(SortedStartStationId(i)==StationIds,SortedEndStationId(i)==StationIds,k) + 1;
i = i + 1;
end
initialDate = initialDate + timeInterval;
if i <= length(SortedStartDate)
if initialDate < initial_Date + averageLength
k = k + 1;
else
k = 1;
initial_Date = initial_Date + averageLength;
regularization = regularization + 1;
end
end
end
StartDemand = StartDemand/regularization;
% Create end station demand matrix
[SortedEndDate,I2] = sort(EndDate);
SortedStartStationId2 = StartStationId(I2);
SortedEndStationId2 = EndStationId(I2);
EndDemand = zeros(length(StationIds),length(StationIds),averageLength/timeInterval);
initialDate2 = startingDate;
initial_Date2 = startingDate;
i = 1;
k = 1;
regularization2 = 1;
while (i <= length(SortedEndDate)) && (SortedEndDate(i) <= endingDate)
while (i <= length(SortedEndDate)) && (SortedEndDate(i) < initialDate2 + timeInterval)
EndDemand(SortedStartStationId2(i)==StationIds,SortedEndStationId2(i)==StationIds,k) = EndDemand(SortedStartStationId2(i)==StationIds,SortedEndStationId2(i)==StationIds,k) + 1;
i = i + 1;
end
initialDate2 = initialDate2 + timeInterval;
if i <= length(SortedEndDate)
if initialDate2 < initial_Date2 + averageLength
k = k + 1;
else
k = 1;
initial_Date2 = initial_Date2 + averageLength;
regularization2 = regularization2 + 1;
end
end
end
EndDemand = EndDemand/regularization2;
% Save start and end station demand matrices
save([System ' ' char(timeInterval) '.mat'],'StartDemand','EndDemand','StationIds','-v7.3');
end
end