-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
348 lines (297 loc) · 12.5 KB
/
Form1.cs
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Ch3_ART1_Clustering
{
public partial class Form1 : Form
{
//Fields
new ART1 art1 = new ART1();
public Form1()
{
InitializeComponent();
this.Height = 600;
}
private void Form1_Load(object sender, EventArgs e)
{
List<string> columns = new List<string>() {
"Hammer", "Paper", "Snickers", "ScrewDriver", "Pen", "Kit-Kat", "Wrench", "Pencil", "Heath Bar", "Tape Measure", "Binder" };
List<string> columnsShort = new List<string>() {
"Hmr", "Ppr", "Snk", "Scr", "Pen", "Kkt", "Wrn", "Pcl", "Hth", "Tpm", "Bdr" };
List<int[]> database = new List<int[]>
{
/* Hmr Ppr Snk Scr Pen Kkt Wrn Pcl Hth Tpm Bdr */
new int[] { 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0}, //0
new int[] { 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1}, //1
new int[] { 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0}, //2
new int[] { 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1}, //3
new int[] { 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0}, //4
new int[] { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1}, //5
new int[] { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, //6
new int[] { 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0}, //7
new int[] { 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0}, //8
new int[] { 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0} //9
};
//Analyzie datbase using ART1
art1.addData(database);
//Show clusters
tbResults.Text += "CLUSTERS" + Environment.NewLine;
tbResults.Text += " " + String.Join(" ", columnsShort.ToArray()) + Environment.NewLine;
tbResults.Text += art1.getClusters();
//Spacer
tbResults.Text += Environment.NewLine;
//Show recommendations
tbResults.Text += "RECOMMENDATIONS" + Environment.NewLine;
tbResults.Text += art1.getRecommendations();
}
}
public class ART1
{
//Fields
public List<FeatureVector> customers = new List<FeatureVector>();
public List<Cluster> clusters = new List<Cluster>();
double tieFactor = 1.0; //beta Tie factor (recommendation: small integer)
double vigilenceFactor = 0.39; //rho, Vigilence factor (0 to 1)
//Methods
public void addData(List<int[]> inputCustomers)
{
//Convert customers into feature vectors
int customerIndex = 0;
foreach (int[] customer in inputCustomers)
{
this.customers.Add(new FeatureVector(customerIndex, customer));
customerIndex++;
}
//Create Clusters
createClusters();
//Make Recommendations
makeRecommendations();
}
private void createClusters()
{
//Repeat process until there are no more changes
bool done = false; int limit = 50;
while (!done)
{
//Assume done. If there is a change, this will be flipped.
done = true;
//Cycle through each customer
foreach (FeatureVector customer in this.customers)
{
//Compare to each cluster
foreach (Cluster cluster in clusters)
{
//Check for same cluster
if (cluster == customer.cluster)
{ continue; }
//If passes proximity test
if (ProximityTest(cluster.featuresPrototype, customer.features))
{
//If passes vigilence test
if (VigilenceTest(cluster.featuresPrototype, customer.features))
{
//Record current cluster
Cluster oldCluster = customer.cluster;
//Move customer to new cluster
customer.cluster = cluster;
//Rebuild old cluster's prototype
if (oldCluster != null)
{
//Get customers in old cluster
List<FeatureVector> oldCustomers = customers.FindAll(c => c.cluster == oldCluster);
//If there are none, delete this cluster
if (oldCustomers.Count == 0) { clusters.Remove(oldCluster); }
//Rebuild the old cluster's prototype
if (oldCustomers.Count > 0) oldCluster.featuresPrototype = oldCustomers[0].features; //Reset to first item in list.
foreach (FeatureVector c in oldCustomers)
{
oldCluster.featuresPrototype = BitwiseAnd(oldCluster.featuresPrototype, c.features);
}
}
//Get customers in new cluster
List<FeatureVector> newCustomers = customers.FindAll(c => c.cluster == cluster);
//Rebuild new cluster's prototype
if (newCustomers.Count > 0) cluster.featuresPrototype = newCustomers[0].features; //Reset to first item in list.
foreach (FeatureVector c in newCustomers)
{
cluster.featuresPrototype = BitwiseAnd(cluster.featuresPrototype, c.features);
}
//A change was found, so required one more pass
done = false;
break;
}
}
}
//Create a prototype for customers that do not match an existing prototype
if (customer.cluster == null)
{
//Create new cluster and add to list
Cluster newCluster = new Cluster(customer);
clusters.Add(newCluster);
//Set the customer to use this cluster
customer.cluster = newCluster;
//Keep processing, as something changed.
done = false;
}
}
//Check limit
limit--;
if (limit == 0) break;
}
}
private void makeRecommendations()
{
foreach (FeatureVector customer in customers)
{
//Clear current customer recomendation
customer.recommendation = new int[customer.features.Length];
//Get all other customers in the same cluster
foreach(FeatureVector clusterMember in customers.FindAll(cm=> cm!= customer && cm.cluster == customer.cluster))
{
//Go through each feature
for(int f =0; f< customer.features.Length; f++)
{
//If the customer is missing this feature, count how many of the cluster members have it.
if (customer.features[f] == 0)
{ customer.recommendation[f] += clusterMember.features[f]; }
}
}
}
}
//Methods - Display
public string getClusters()
{
string s = "";
//Clusters
foreach (Cluster cluster in clusters)
{
//Show prototype vector
s += " Prototype: " + itemToString(cluster.featuresPrototype, " ") + Environment.NewLine;
//Show members
foreach (FeatureVector customer in customers.FindAll(c => c.cluster == cluster))
{
s += "Customer " + customer.index + ": " + itemToString(customer.features, " ") + Environment.NewLine;
}
//Drop down a line
s += Environment.NewLine; ;
}
return s;
}
public string getRecommendations()
{
string s = "";
foreach (FeatureVector customer in customers)
{
//Show recommendation vector
s += "Customer " + customer.index + ": " + itemToString(customer.recommendation, " ");
if (customer.recommendation.Sum() != 0)
{
s += "(Item " + customer.recommendation.ToList().IndexOf(customer.recommendation.Max()) + ")" + Environment.NewLine;
}
else
{
s += "(None)";
}
}
return s;
}
private string itemToString(int[] item, string del)
{
string s = "";
foreach (int i in item)
{
s += i + del;
}
return s;
}
//Methods - Processing
private bool ProximityTest(int[] prototype, int[] newItem)
{
//Check that dimensions agree
if (newItem.Length != prototype.Length)
{ throw new ArgumentException("The features vectors must be the same size."); }
//Calculate comparison
double leftSide = ((double)BitwiseAnd(newItem, prototype).Sum()) / (double)(tieFactor + prototype.Sum());
double rightSide = ((double)newItem.Sum()) / (tieFactor + prototype.Length);
//Compute comparison
return leftSide > rightSide;
}
private bool VigilenceTest(int[] prototype, int[] newItem)
{
//Calculate the bitwise AND comparison and compare it to the number of total in the newItem.
double vigCalculation = ((double)BitwiseAnd(newItem, prototype).Sum()) / newItem.Sum();
//If the calculation is greater than the vigilence factor, pass back true.
return vigCalculation < vigilenceFactor;
}
private int[] BitwiseAnd(int[] A, int[] B)
{
//Check that dimensions agree
if (A.Length != B.Length)
{ throw new ArgumentException("The features vectors must be the same size."); }
//Compare
int[] result = new int[A.Length];
for (int i = 0; i < A.Length; i++)
{
if (A[i] == 1 && B[i] == 1)
{ result[i] = 1; }
}
//Return bitwise AND comparison
return result;
}
}
public class Cluster
{
//Fields
public int[] featuresPrototype;
//Constructor
public Cluster(FeatureVector fv)
{
this.featuresPrototype = fv.features;
}
//Property
public string prototypeAsString
{
get
{
string s = "";
foreach (int i in featuresPrototype)
{
s += i + " ";
}
return s;
}
}
}
public class FeatureVector
{
//Fields
public int index;
public Cluster cluster = null;
public int[] features;
public int[] recommendation;
//Indexer
public int this[int index]
{
get { return features[index]; }
set { features[index] = value; }
}
//Constructor
public FeatureVector(int index, int[] features)
{
this.index = index;
this.features = features;
}
//Properties
public Cluster Cluster
{
get { return cluster; }
set { cluster = value; }
}
}
}