-
Notifications
You must be signed in to change notification settings - Fork 2
/
Clustering_update.R
208 lines (168 loc) · 5.82 KB
/
Clustering_update.R
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
library(ggplot2)
library(RCurl)
library(RJSONIO)
library(plyr)
library(class)
library(stringr)
url <- function(address, return.call = "json", sensor = "false") {
root <- "http://maps.google.com/maps/api/geocode/"
u <- paste(root, return.call, "?address=", address, "&sensor=", sensor, sep = "")
return(URLencode(u))
}
geoCode <- function(address,verbose=FALSE) {
if(verbose) cat(address,"\n")
u <- url(address)
doc <- getURL(u)
x <- fromJSON(doc,simplify = FALSE)
if(x$status=="OK") {
lat <- x$results[[1]]$geometry$location$lat
lng <- x$results[[1]]$geometry$location$lng
location_type <- x$results[[1]]$geometry$location_type
formatted_address <- x$results[[1]]$formatted_address
return(c(lat, lng, location_type, formatted_address))
Sys.sleep(0.5)
} else {
return(c(NA,NA,NA, NA))
}
}
address <- c("The White House, Washington, DC","The Capitol, Washington, DC")
locations <- ldply(address, function(x) geoCode(x))
names(locations) <- c("lat","lon","location_type", "formatted")
head(locations)
# Data
data <- read.csv("Book2.csv", header = TRUE)
View(data)
# Route Optimization Function
?kmeans()
# Need to convert Matt's matrix to a regular matrix? Can we remove the extra useless data?
bothwell_elementary = read.csv("bothwell_elementary.csv")
bothwell_elementary = bothwell_elementary[, c(-1, -2)]
ggplot(bothwell_elementary[,5:6], aes(x = bothwell_elementary$lattitude, y = bothwell_elementary$longitude)) + geom_point()
# Cluster Function: Take dataframe and return dataframe with cluster vector added.
pathSplit <- function(df, k){
# Run K-Means
df.kmean = df[ , 5:6]
km.out <- kmeans(df.kmean, k, nstart = 25)
newdf <- cbind(df, km.out$cluster)
if("km.out$cluster" %in% colnames(newdf)){
colnames(newdf)[colnames(newdf) == 'km.out$cluster'] <- "Cluster"
}
newdf$Cluster = as.factor(newdf$Cluster)
return(newdf)
}
pathSplit.out = pathSplit(bothwell_elementary, 4)
View(pathSplit.out)
plot1 <- ggplot(pathSplit.out, aes(x = lattitude, y = longitude, color = Cluster)) +
geom_point() +
labs(title = "Walk Clusters\n") +
scale_color_manual(labels = c("Cluster 1", "Cluster 2", "Cluser 3", "Cluster 4"), values = c("blue", "red", "green", "purple")) +
ggtitle("Walking Clusters Example") +
theme(plot.title = element_text(hjust = 0.5)) +
ggsave("walking_clusters_eg2.png")
plot1
# Bothwell Location
# 17070 102 Ave, Surrey, BC V4N 4N6
#
# First need to set up format that callAPI likes. Remove spaces, change to +.
# Next select first two rows. Then loop through and keep adding to the path more.
#
#"Maple+Green+Elementary+Surrey,BC" "8619+150+street+Surrey,BC"
opt_func <- function(df, maxD, maxSize, firstRun){
# Initialize
if(firstRun = TRUE){
k = 1
# Change data strings to be readable by Matt's functions
df <- gsub(" ", df, replacement = "+")
location <- vector(mode = "chr", length = nrow(df))
df2 = cbind(df, location)
for(i in 1:nrow(df2)){
location[i] = paste0(df2$address,"+", df2$city, "+", ",BC")
}
}
# Base call to callAPI()
# Arg1 = first address, Arg2 = second address
holder = callAPI(df2[1,7], df2[2,7])
holder2 = holder[[2]]
for(i in 3:nrows(df2)){
holder2 = callAPI(holder2, df2[i,7])[[2]]
}
busPath = bestPath(holder2, "Bothwell+Elementary+School+Surrey", "17070+102+Ave+Surrey,BC")
#Test to see how big df is. K-mean again if too big
if(nrow(df) >= maxSize){
k = k + 1
testSplit = pathSplit(df, k)
opt_func(testSplit, maxD, maxSize, FALSE)
}else{ # Not too big. Split based off of number of clusters
for(i in 1:length(unique(pathSplit.out$Cluster))){
temp <- df2 %>% filter(Cluster == i)
l <- bestPath(temp)[[1]]
if (l <= maxD){ # If distance is less than max then finish. Return df with clusters
return(temp)
}else{
k = k + 1
temp2 <- pathSplit(temp, k)
opt_func(temp2, maxD, maxSize, FALSE)
}
}
}
}
optim.df <- opt_func2(bothwell_elementary, 1, 15)
fakeData = pathSplit.out
fakeDist = runif(60, 1, 3000)
fakeData = cbind(fakeData, fakeDist)
View(fakeData)
write.csv(fakeData, "fakeData.csv")
opt_func2 <- function(df, maxD, firstRun){
# Initialize
if(firstRun == TRUE){
if (nrow(df) <= 11){
k = 1
# Change data strings to be readable by Matt's functions
for (i in 1:ncol(df)){
df[, i] <- gsub(" ", df[,i], replacement = "+") }
location <- NULL
for(i in 1:nrow(df)){
location[i] = paste0(df$address,"+", df$city, ",BC")
}} else{
k = floor(nrow(df)/12)
# Change data strings to be readable by Matt's functions
for (i in 1:ncol(df)){
df[, i] <- gsub(" ", df[,i], replacement = "+") }
location <- NULL
for(i in 1:nrow(df)){
location[i] = paste0(df$address,"+", df$city, ",BC")
}
df2 <- cbind(df, location)
}
}
# Base call to callAPI()
# Arg1 = first address, Arg2 = second address
testSplit = pathSplit(df2, k)
busPath = NULL
for (i in 1:length(unique(testSplit$Cluster))){
temp <- testSplit %>% filter(Cluster == i)
if(nrow(temp) <= 1){
return()
}
holder = callAPI(temp[1,7], temp[2,7])
holder2 = holder[[2]]
for(j in 3:nrow(df2)){
holder2 = callAPI(holder2, df2[j,7])[[2]]
}
busPath[i] = bestPath(holder2, "Bothwell+Elementary+School+Surrey,BC", "17070 102 Ave")
}
#Test to see how big df is. K-mean again if too big
for (i in 1:length(unique(testSplit$Cluster))){
if(busPath[i][[1]] >= maxD){
k = k + 1
testSplit = pathSplit(df, k)
opt_func(testSplit, maxD, FALSE)
} else{ # Not too big. Split based off of number of clusters
return(as.list(testSplit, busPath, k))
}
}
}
opt_func2(bothwell_elementary, 1500, TRUE)
debugonce(opt_func2)
nrow(bothwell_elementary)
bothwell_elementary$address