public override void onMetadata(Metadata metadata) { _userGroupId = -1; ParallelWorker worker = new ParallelWorker(8); bool found = false; for (int it = 0; it < _statistics.Count && !found; it++) { int i = it; worker.Queue(delegate { if (_statistics[i].users.Contains(metadata.userId)) { _userGroupId = i; found = true; } }); } metadata.WriteToStream(_writer); worker.Wait(); }
private static List <List <User> > getBoxes(List <User> allUsers2) { List <List <User> > allBoxes = new List <List <User> >(); const int N = 100; for (int b = 0; b < N; b++) { int min = b * allUsers2.Count / N; int max = (b + 1) * allUsers2.Count / N; var users = allUsers2.GetRange(min, max - min); var boxes2 = getSmallBoxes(users); const float MIN_SIM = 0.5f; ParallelWorker tmpWorker = new ParallelWorker(8); for (int it = 0; it < boxes2.Count; it++) { int i = it; tmpWorker.Queue(delegate { float bestSim = 0; int bestBox = -1; int nBoxes; lock (allBoxes) { nBoxes = allBoxes.Count; } for (int k = 0; k < nBoxes; k++) { float sim = boxes2[i][0].GetSim(allBoxes[k][0]); if (bestSim < sim) { bestSim = sim; bestBox = k; } } lock (allBoxes) { for (int k = nBoxes; k < allBoxes.Count; k++) { float sim = boxes2[i][0].GetSim(allBoxes[k][0]); if (bestSim < sim) { bestSim = sim; bestBox = k; } } if (bestSim < MIN_SIM) { bestBox = allBoxes.Count; allBoxes.Add(new List <User>()); } allBoxes[bestBox].AddRange(boxes2[i]); } }); } tmpWorker.Wait(); Console.WriteLine("Done {0}%", 100.0f * b / N); } return(allBoxes); }
private static List <List <User> > getSmallBoxes(List <User> allUsers) { allUsers.Sort((u1, u2) => { return(u1.terms.Count - u2.terms.Count); }); List <List <User> > boxes = new List <List <User> >(); ParallelWorker tmpWorker = new ParallelWorker(8); const float MIN_SIM = 0.7f; for (int it = 0; it < allUsers.Count; it++) { int i = it; tmpWorker.Queue(delegate { float bestSim = 0; int bestBox = -1; int nBoxes; lock (boxes) { nBoxes = boxes.Count; } for (int k = 0; k < nBoxes; k++) { float sim = allUsers[i].GetSim(boxes[k][0]); if (bestSim < sim) { bestSim = sim; bestBox = k; } } lock (boxes) { for (int k = nBoxes; k < boxes.Count; k++) { float sim = allUsers[i].GetSim(boxes[k][0]); if (bestSim < sim) { bestSim = sim; bestBox = k; } } if (bestSim < MIN_SIM) { bestBox = boxes.Count; boxes.Add(new List <User>()); } boxes[bestBox].Add(allUsers[i]); } }); if (it % 1000 == 0) { Console.Write("Users: {0:0.00}%\t({1})\r", 100.0f * it / allUsers.Count, boxes.Count); } } tmpWorker.Wait(); return(boxes); }