Grouping.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package analyzer;
  2. import analyzer.models.Client;
  3. import analyzer.models.Group;
  4. import analyzer.models.Round;
  5. import java.util.ArrayList;
  6. import java.util.Comparator;
  7. import java.util.List;
  8. public class Grouping {
  9. private static List<Client> clients;
  10. private static List<Round> rounds;
  11. private static List<Group> groups;
  12. private static final int threshold = 20;
  13. private static final int learningRounds = 20;
  14. public static void main(String[] args) throws InterruptedException {
  15. Analyzer a = new Analyzer();
  16. clients = new ArrayList<>();
  17. groups = new ArrayList<>();
  18. rounds = new ArrayList<>(ServerLogParser.rounds.values());
  19. rounds.sort(Comparator.comparingInt(Round::getNo));
  20. rounds = rounds.subList(0, learningRounds);
  21. run();
  22. }
  23. public static void run(){
  24. System.out.println("threshold: " + threshold);
  25. System.out.println("learningRounds: " + learningRounds);
  26. for(int i = 1; i <ServerLogParser.rounds.values().size(); i++) {
  27. Round r = ServerLogParser.rounds.get(i);
  28. rounds.add(r);
  29. ClientLogParser.clients.values().forEach(c -> {
  30. if(c.getRounds().containsKey(r))
  31. if(c.getTotalPosts() >= 100 && !clients.contains(c))
  32. clients.add(c);
  33. });
  34. if(i >= learningRounds) {
  35. group(i);
  36. int progress = (int) (((float) i /(float) ServerLogParser.rounds.values().size()) * 100F);
  37. System.out.print("\r");
  38. System.out.print(progress + " %");
  39. }
  40. }
  41. System.out.println();
  42. System.out.println("Group no: " + groups.size());
  43. int ungrouped = 0;
  44. for (Client c : clients) {
  45. if(c.getGroup() == null)
  46. ungrouped++;
  47. }
  48. System.out.println("ungrouped: " + ungrouped);
  49. int sumSize = 0;
  50. for(Group g : groups) {
  51. sumSize += g.clients().size();
  52. System.out.print(g.clients().size() + ",");
  53. }
  54. System.out.println();
  55. System.out.println("Avg Size of Groups: " + sumSize/groups.size());
  56. System.out.println("Avg No of Cover Messages: " + coverMessagesCount()/(clients.size() - ungrouped));
  57. }
  58. private static void group(int currentRound) {
  59. for(Client c1 : clients) {
  60. for(Client c2 : clients) {
  61. if(c1.equals(c2) || c2.getGroup() != null)
  62. continue;
  63. else {
  64. int distance = euclideanDistance(c1, c2, currentRound);
  65. Group group = c1.getGroup();
  66. if(group == null && distance < threshold) {
  67. group = new Group(0);
  68. group.addClient(c1);
  69. group.addClient(c2);
  70. groups.add(group);
  71. }
  72. else {
  73. if(group != null && suitable(c2, group, currentRound))
  74. group.addClient(c2);
  75. }
  76. }
  77. }
  78. }
  79. }
  80. private static boolean suitable(Client client, Group group, int currentRound) {
  81. int sum = 0;
  82. for(Client c : group.clients()) {
  83. if(euclideanDistance(c, client, currentRound) >= threshold)
  84. return false;
  85. }
  86. return true;
  87. }
  88. private static int coverMessagesCount() {
  89. int sum = 0;
  90. for(Group g : groups) {
  91. for(Client c : g.clients())
  92. for(Round r : ServerLogParser.rounds.values()) {
  93. Integer value1 = g.getRounds().get(r);
  94. Integer value2 = c.getRounds().get(r);
  95. if(value1 == null)
  96. value1 = 0;
  97. if(value2 == null)
  98. value2 = 0;
  99. int diff = value1 - value2;
  100. sum += diff;
  101. }
  102. }
  103. return sum;
  104. }
  105. private static int euclideanDistance(Client c1, Client c2, int currentRound) {
  106. int sum = 0;
  107. List<Round> subset = rounds.subList(currentRound - learningRounds, currentRound + 1);
  108. for(Round r : subset) {
  109. Integer value1 = c1.getRounds().get(r);
  110. Integer value2 = c2.getRounds().get(r);
  111. if(value1 == null)
  112. value1 = 0;
  113. if(value2 == null)
  114. value2 = 0;
  115. int diff = Math.abs(value1 - value2);
  116. sum += diff;
  117. }
  118. return sum;
  119. }
  120. }