-
Notifications
You must be signed in to change notification settings - Fork 1
/
envData.m
103 lines (89 loc) · 3.3 KB
/
envData.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
clear all;
close all;
% Environment
numNodes = 100;
Length = 1000;
width = 1200;
maxSpeed = 20; % m/s
minSpeed = 1; % m/s
numHospitals = 5;
% create nodes
xHospitalLocation = Length*rand(numHospitals,1);
yHospitalLocation = width*rand(numHospitals,1);
xNodes = Length*rand(numNodes,1);
yNodes = width*rand(numNodes,1);
Nodes = [xNodes yNodes];
Hospital_Location = [xHospitalLocation yHospitalLocation];
% create delaunay triangulation for roads
dt = delaunayTriangulation(xNodes,yNodes);
dt1 = [dt.ConnectivityList dt.ConnectivityList(:,1)];
dt2 = [dt1(:,1) dt1(:,2); dt1(:,2) dt1(:,3); dt1(:,3) dt1(:,4)];
numEdges = length(dt2);
% create adjacency matrix for edges
adjM = zeros(numNodes+numHospitals,numNodes+numHospitals);
for i = 1:numNodes
v = find(dt2(:,1) == i);
adjM(i,dt2(v)) = 1;
adjM(dt2(v),i) = 1;
end
% connect hospital to the nearest node
% Hospital nodes are from numNodes+1 to numNodes+numHospitals
for i = 1:numHospitals
dists = sqrt((xHospitalLocation(i)-xNodes(:)).^2 + (yHospitalLocation(i)-yNodes(:)).^2);
[V,I] = sort(dists);
adjM(numNodes+i,I(2)) = 1;
adjM(I(2),numNodes+i) = 1;
end
% create traffic;
edgeSpeeds = ceil(rand(numEdges,1)*maxSpeed);
numHours = 2;
numSeconds = numHours*3600; % hours*seconds
numChanges = numHours/0.25;
trafficChangeTime = ceil(sort(numSeconds*rand(numChanges,1)));
trafficChangeTime = [trafficChangeTime; numSeconds+1];
percentageEdgesChangeTraffic = .50;
numEdgeChanges = ceil(numEdges*percentageEdgesChangeTraffic);
maxSpeedChange = 0.25;
% determine the changes whos traffic is changing
% for i = 1:numChanges
% tempEdgeChanges = ceil(rand(ceil(length(dt2)*percentageEdgesChangeTraffic),1)*length(dt2));
% trafficChange(i).edges = dt2(edgeChanges,:);
% trafficChange(i).edgeSpeeds = randn(length(edgeChanges),1)*0.25;
% end
% Accident data
numAccidents = 1000;
accidentKeyNodes = 10;
accidentKeyNodeIndex = ceil(rand(accidentKeyNodes,1)*numNodes);
accidentTimes = sort(ceil(rand(numAccidents,1)*numSeconds));
numNearestNeighbor = 10;
accidentNodes = zeros(numAccidents,1);
for i = 1:numAccidents
anode = accidentKeyNodeIndex(ceil(rand*accidentKeyNodes));
dists = sqrt((xNodes(anode)- xNodes(:)).^2 + (yNodes(anode)-yNodes(:)).^2);
[V,I] = sort(dists);
accidentNodes(i) = I(ceil(rand*numNearestNeighbor));
end
% accident time and nodes where the accident happens
accidentData = [accidentTimes accidentNodes];
% time lapse data
trafficData = zeros(numSeconds,numEdges);
trafficChangeFlag = 1;
tempEdgeSpeeds = edgeSpeeds;
for i = 1:numSeconds
trafficData(i,:) = tempEdgeSpeeds;
if i > trafficChangeTime(trafficChangeFlag)
tempEdgeChanges = ceil(rand(numEdgeChanges,1)*length(dt2));
for j = 1:length(tempEdgeChanges)
tempEdgeSpeeds(tempEdgeChanges(j)) = edgeSpeeds(tempEdgeChanges(j)) + newSpeed(edgeSpeeds(tempEdgeChanges(j)),maxSpeedChange);
trafficData(i,tempEdgeChanges(j)) = tempEdgeSpeeds(tempEdgeChanges(j));
tempEdgeSpeeds(tempEdgeChanges(j)) = trafficData(i,tempEdgeChanges(j));
end
trafficChangeFlag = trafficChangeFlag + 1;
end
end
csvwrite('trafficData.csv',trafficData);
csvwrite('collisionData.csv', accidentData);
csvwrite('adjM.csv', adjM);
csvwrite('nodes.csv', Nodes);
csvwrite('hospital.csv', Hospital_Location);
csvwrite('edges.csv', dt2);