-
Notifications
You must be signed in to change notification settings - Fork 0
/
SantasSledges_SubCluster.mos
245 lines (188 loc) · 5.51 KB
/
SantasSledges_SubCluster.mos
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
model "SantasSledges Clustering At Scale"
uses "r", "mmsystem", "mmsheet"
parameters
NUMCLUSTERS=3
CLUSTER=1
SLEDGECAPACITY=1000
SLEDGEWEIGHT=10
end-parameters
declarations
Index : set of integer
GiftIDs : array(Index) of integer
Latitude : array(Index) of real
Longitude : array(Index) of real
Weight : array(Index) of real
clustering : dynamic array(set of integer) of integer
ClusterMembers : dynamic array(set of integer) of set of integer
sortWeights : dynamic array(set of integer) of real
sortInd : list of integer
sortedGiftInd : list of integer
sortedInd : list of integer
sortedWeights : dynamic array(set of integer) of real
sortedGiftIDs : dynamic array(set of integer) of integer
end-declarations
!!!!!! ENTERS DIFFERENT CWD
setparam('workdir','./'+CLUSTER)
initializations from "mmsheet.csv:cluster_" + CLUSTER + ".csv"
[GiftIDs, Latitude, Longitude, Weight] as "skiph;[A:E]"
end-initializations
writeln("Total gift count: ", getsize(GiftIDs))
nc := NUMCLUSTERS
total := 0.0
overweight := true
cnt := 0
Rset('Latitude', Latitude)
Rset('Longitude', Longitude)
Reval('Latitude <- Latitude + 90')
Reval('Longitude <- Longitude + 180')
Reval("d<-cbind(Latitude,Longitude)")
while (overweight) do
delcell(ClusterMembers)
delcell(clustering)
giftcount:= 0
overweight := false
Reval("k <- kmeans(d,"+nc+")")
Rgetarr('k$cluster',clustering)
cnt:= 1
forall(g in Index) do
ClusterMembers(clustering(g)) += {g}
end-do
cnt:= 1
fopen("subcluster_"+CLUSTER+".csv", F_OUTPUT)
writeln("GiftID,Latitude,Longitude,Weight,TripID")
forall(c in 1..nc) do
total:=0
forall(g in ClusterMembers(c)) do
writeln(GiftIDs(g),",",Latitude(g),",",Longitude(g),",",Weight(g),",",c)
total += Weight(g)
end-do
if total > (SLEDGECAPACITY-SLEDGEWEIGHT) then
overweight := true
end-if
giftcount += getsize(ClusterMembers(c))
! writeln("Total = ", total)
end-do
fclose(F_OUTPUT)
write("Total size for clusters is ", giftcount, ". ")
nc += 1
writeln("Clustering at nc: ", nc-1, " finished.")
end-do
writeln("Sorting gifts by weight for each unique trip...")
fopen("subcluster_"+CLUSTER+"_costs.csv", F_OUTPUT)
writeln("ID,GiftID,TripID,Weight")
cnt := 1
forall(c in 1..(nc-1)) do
forall(g in ClusterMembers(c)) do
sortWeights(g) := Weight(g)
end-do
qsort(false,sortWeights,sortedGiftInd,ClusterMembers(c))
sortedInd := sortedGiftInd
forall(g in ClusterMembers(c)) do
sortedWeights(g) := Weight(getfirst(sortedGiftInd))
sortedGiftIDs(g) := GiftIDs(getfirst(sortedGiftInd))
cuthead(sortedGiftInd,1)
end-do
forall(g in ClusterMembers(c)) do
writeln(cnt,",",sortedGiftIDs(g),",",c,",",sortedWeights(g))
cnt+=1
end-do
sortedGiftInd := []
sortedInd := []
delcell(sortWeights)
delcell(sortedGiftIDs)
delcell(sortedWeights)
end-do
fclose(F_OUTPUT)
delcell(ClusterMembers)
delcell(clustering)
nc -= 2
writeln
writeln("Evaluating larger cluster at nc=",nc)
Reval("k <- kmeans(d,"+nc+")")
Rgetarr('k$cluster',clustering)
forall(g in Index) do
ClusterMembers(clustering(g)) += {g}
end-do
fopen("subcluster_"+CLUSTER+".csv", F_APPEND)
forall(c in 1..nc) do
total:=0
forall(g in ClusterMembers(c)) do
total += Weight(g)
end-do
if total <= (SLEDGECAPACITY) then
forall(g in ClusterMembers(c)) do
writeln(GiftIDs(g), ",", Latitude(g),",", Longitude(g), ",", Weight(g),",",nc+1+c)
end-if
end-do
fclose(F_APPEND)
fopen("subcluster_"+CLUSTER+"_costs.csv", F_APPEND)
forall(c in 1..nc) do
forall(g in ClusterMembers(c)) do
sortWeights(g) := Weight(g)
end-do
qsort(false,sortWeights,sortedGiftInd,ClusterMembers(c))
sortedInd := sortedGiftInd
forall(g in ClusterMembers(c)) do
sortedWeights(g) := Weight(getfirst(sortedGiftInd))
sortedGiftIDs(g) := GiftIDs(getfirst(sortedGiftInd))
cuthead(sortedGiftInd,1)
end-do
forall(g in ClusterMembers(c)) do
writeln(cnt,",",sortedGiftIDs(g),",",nc + 1 + c,",",sortedWeights(g))
cnt+=1
end-do
sortedGiftInd := []
sortedInd := []
delcell(sortWeights)
delcell(sortedGiftIDs)
delcell(sortedWeights)
end-do
fclose(F_APPEND)
delcell(clustering)
delcell(ClusterMembers)
nc += 2
writeln
writeln("Evaluating smaller cluster at nc=",nc)
Reval("k <- kmeans(d,"+nc+")")
Rgetarr('k$cluster',clustering)
forall(g in Index) do
ClusterMembers(clustering(g)) += {g}
end-do
fopen("subcluster_"+CLUSTER+".csv", F_APPEND)
forall(c in 1..nc) do
total:=0
forall(g in ClusterMembers(c)) do
writeln(GiftIDs(g),",",Latitude(g),",",Longitude(g),",",Weight(g),",",2*nc - 3 + c)
total += Weight(g)
end-do
end-do
fclose(F_APPEND)
fopen("subcluster_"+CLUSTER+"_costs.csv", F_APPEND)
forall(c in 1..nc) do
forall(g in ClusterMembers(c)) do
sortWeights(g) := Weight(g)
end-do
qsort(false,sortWeights,sortedGiftInd,ClusterMembers(c))
sortedInd := sortedGiftInd
forall(g in ClusterMembers(c)) do
sortedWeights(g) := Weight(getfirst(sortedGiftInd))
sortedGiftIDs(g) := GiftIDs(getfirst(sortedGiftInd))
cuthead(sortedGiftInd,1)
end-do
forall(g in ClusterMembers(c)) do
writeln(cnt,",",sortedGiftIDs(g),",",2*nc - 3 + c,",",sortedWeights(g))
cnt+=1
end-do
sortedGiftInd := []
sortedInd := []
delcell(sortWeights)
delcell(sortedGiftIDs)
delcell(sortedWeights)
end-do
fclose(F_APPEND)
!!!!!!! GOES BACK OUT OF CHANGED CWD
setparam('workdir','../')
writeln("Done clustering ", CLUSTER, ". Evaluated with ", nc-1, " clusters.")
writeln
writeln
end-model