From 1b84f7452c7a310398bc66bdbfa7274b703907f1 Mon Sep 17 00:00:00 2001
From: Axel <wiepke@uni-potsdam.de>
Date: Wed, 8 Aug 2018 13:46:28 +0200
Subject: [PATCH] feat: CheatChecker works with variance.

Started to implement machine learning.
---
 .../controller/service/PeerAssessment.java    | 110 +++++++-----------
 .../modules/peerassessment/HashMapTest.java   |  56 ++++++---
 2 files changed, 84 insertions(+), 82 deletions(-)

diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/assessment/controller/service/PeerAssessment.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/assessment/controller/service/PeerAssessment.java
index e7e04d83..88def594 100644
--- a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/assessment/controller/service/PeerAssessment.java
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/assessment/controller/service/PeerAssessment.java
@@ -77,8 +77,8 @@ public class PeerAssessment implements IPeerAssessment {
             int size = 0;
             while (it.hasNext()) {
                 HashMap.Entry pair = (HashMap.Entry) it.next();
-                Integer rating = (Integer) pair.getValue();
-                allGrades[i] += (double) rating;
+                Double rating = (Double) pair.getValue();
+                allGrades[i] += rating;
                 it.remove(); // avoids a ConcurrentModificationException
                 size++;
             }
@@ -91,7 +91,7 @@ public class PeerAssessment implements IPeerAssessment {
         return Arrays.asList(grading);
     }
 
-    private Map<String, Double> meanOfWorkRatings(ArrayList<Map<String, Integer>> workRatings) {
+    private Map<String, Double> meanOfWorkRatings(ArrayList<Map<String, Double>> workRatings) {
         HashMap<String, Double> mean = new HashMap();
         double size = (double) workRatings.size();
         Iterator it = workRatings.get(0).entrySet().iterator();
@@ -109,22 +109,41 @@ public class PeerAssessment implements IPeerAssessment {
         }
         return mean;
     }
-//todo: funktioniert noch nicht. Fliegt aus der foreach schleife, kA warum.
-    public ArrayList<Map> cheatChecker(ArrayList<Map<String, Integer>> workRatings) {
-        ArrayList<Map<String, Integer>> possiblyCheating;
-        ArrayList<Map<String, Double>> means = new ArrayList<>();
+
+    public ArrayList<Map<String, Double>> cheatChecker(ArrayList<Map<String, Double>> workRatings, String method) {
+        ArrayList<Map<String, Double>> oneExcludedMeans = new ArrayList<>();
+        ArrayList<Map<String, Double>> result = new ArrayList<>();
         Double threshold = 0.4;
-        boolean cheat;
-        for (Map rating : workRatings) {
-            possiblyCheating = workRatings;
-            possiblyCheating.remove(rating);
-            means.add(meanOfWorkRatings(possiblyCheating));
+        if (workRatings.size() > 1) {
+            for (Map rating : workRatings) {
+                ArrayList<Map<String, Double>> possiblyCheating = new ArrayList<>(workRatings);
+                possiblyCheating.remove(rating);
+                oneExcludedMeans.add(meanOfWorkRatings(possiblyCheating));
+            }
+        } else {
+            oneExcludedMeans.add(meanOfWorkRatings(workRatings));
         }
-        means.sort(byResponsibility);
-        String resp = "responosibility";
-        if (means.get(0).get(resp) - threshold > means.get(1).get(resp))
-            cheat = true;
-        return null;
+        if (method.equals("median")) {
+            workRatings.sort(byMean);
+            result.add(oneExcludedMeans.get(oneExcludedMeans.size() / 2)); //in favor of student
+        }
+        if (method.equals("variance")) {
+            Map<String, Double> meanWorkRating = new HashMap<>(meanOfWorkRatings(oneExcludedMeans));
+            ArrayList<Map<String, Double>> deviation = new ArrayList<>();
+            for (Map<String, Double> rating: oneExcludedMeans){
+                for (String key: rating.keySet()){
+                    HashMap<String, Double> shuttle = new HashMap<>();
+                    Double value = (rating.get(key)-meanWorkRating.get(key))*(rating.get(key)-meanWorkRating.get(key));
+                    shuttle.put(key, value);
+                    deviation.add(shuttle);
+                }
+
+            }
+            result.add(meanOfWorkRatings(oneExcludedMeans));
+        }
+
+
+        return result;
     }
 
     @Override
@@ -147,57 +166,18 @@ public class PeerAssessment implements IPeerAssessment {
     public void answerQuiz(StudentAndQuiz studentAndQuiz, QuizAnswer quizAnswer) {
 
     }
-///todo: das ist nicht die feine englische. Kann ich das hübschivieren?
-    final String sortCase1 = "responsibility";
-    Comparator<Map<String, Double>> byResponsibility = (o1, o2) -> {
-        Double first = o1.get(sortCase1);
-        Double second = o2.get(sortCase1);
-        if (first.equals(second)) {
-            return 0;
-        } else {
-            return first < second ? -1 : 1;
-        }
-    };
-    final String sortCase2 = "partOfWork";
-    Comparator<Map<String, Double>> byPartOfWork = (o1, o2) -> {
-        Double first = o1.get(sortCase2);
-        Double second = o2.get(sortCase2);
-        if (first.equals(second)) {
-            return 0;
-        } else {
-            return first < second ? -1 : 1;
-        }
-    };
-    final String sortCase3 = "cooperation";
-    Comparator<Map<String, Double>> byCooperation = (o1, o2) -> {
-        Double first = o1.get(sortCase3);
-        Double second = o2.get(sortCase3);
-        if (first.equals(second)) {
-            return 0;
-        } else {
-            return first < second ? -1 : 1;
-        }
-    };
-    final String sortCase4 = "communication";
-    Comparator<Map<String, Double>> byCommunication = (o1, o2) -> {
-        Double first = o1.get(sortCase4);
-        Double second = o2.get(sortCase4);
-        if (first.equals(second)) {
-            return 0;
-        } else {
-            return first < second ? -1 : 1;
+
+    Comparator<Map<String, Double>> byMean = (o1, o2) -> {
+        Double sumOfO1 = 0.;
+        Double sumOfO2 = 0.;
+        for (String key : o1.keySet()) {
+            sumOfO1 += o1.get(key);
+            sumOfO2 += o2.get(key);
         }
-    };
-    final String sortCase5 = "autonomous";
-    Comparator<Map<String, Double>> byAutonomous = (o1, o2) -> {
-        Double first = o1.get(sortCase5);
-        Double second = o2.get(sortCase5);
-        if (first.equals(second)) {
+        if (sumOfO1.equals(sumOfO2)) {
             return 0;
         } else {
-            return first < second ? -1 : 1;
+            return sumOfO1 > sumOfO2 ? -1 : 1;
         }
     };
-
-
 }
diff --git a/gemeinsamforschen/src/test/java/unipotsdam/gf/modules/peerassessment/HashMapTest.java b/gemeinsamforschen/src/test/java/unipotsdam/gf/modules/peerassessment/HashMapTest.java
index 4e81b6cf..aa0678d3 100644
--- a/gemeinsamforschen/src/test/java/unipotsdam/gf/modules/peerassessment/HashMapTest.java
+++ b/gemeinsamforschen/src/test/java/unipotsdam/gf/modules/peerassessment/HashMapTest.java
@@ -25,19 +25,26 @@ public class HashMapTest {
         }
         return mean;
     }
+
     final String sortCase1 = "responsibility";
     Comparator<Map<String, Double>> byResponsibility = (o1, o2) -> {
-        Double first = o1.get(sortCase1);
-        Double second = o2.get(sortCase1);
-        if (first.equals(second)) {
+        Double sumOfO1 = 0.;
+        Double sumOfO2 = 0.;
+        for (String key : o1.keySet()) {
+            sumOfO1 += o1.get(key);
+            sumOfO2 += o2.get(key);
+        }
+        //Double first = o1.get(sortCase1);
+        //Double second = o2.get(sortCase1);
+        if (sumOfO1.equals(sumOfO2)) {
             return 0;
         } else {
-            return first < second ? -1 : 1;
+            return sumOfO1 < sumOfO2 ? -1 : 1;
         }
     };
 
     @Test
-    public void sortTest(){
+    public void sortTest() {
         Map work = new HashMap<String, Double>();
         work.put("responsibility", 1.);
         work.put("partOfWork", 1.);
@@ -50,23 +57,38 @@ public class HashMapTest {
         work2.put("cooperation", 5.);
         work2.put("communication", 3.);
         work2.put("autonomous", 4.);
-        ArrayList<Map<String,Integer>> workRatings = new ArrayList<>();
+        Map work3 = new HashMap<String, Double>();
+        work3.put("responsibility", 2.);
+        work3.put("partOfWork", 3.);
+        work3.put("cooperation", 5.);
+        work3.put("communication", 2.);
+        work3.put("autonomous", 1.);
+        Map work4 = new HashMap<String, Double>();
+        work4.put("responsibility", 5.);
+        work4.put("partOfWork", 5.);
+        work4.put("cooperation", 4.);
+        work4.put("communication", 4.);
+        work4.put("autonomous", 5.);
+        ArrayList<Map<String, Integer>> workRatings = new ArrayList<>();
         workRatings.add(work);
         workRatings.add(work2);
-        workRatings.add(work2);
-        ArrayList<Map<String, Integer>> possiblyCheating;
+        workRatings.add(work3);
+        workRatings.add(work4);
         ArrayList<Map<String, Double>> means = new ArrayList<>();
         Double threshold = 0.4;
-        boolean cheat;
-        for (Map rating : workRatings) {
-            possiblyCheating = workRatings;
-            possiblyCheating.remove(rating);
-            means.add(meanOfWorkRatings(possiblyCheating));
+        if (workRatings.size() > 1) {
+            for (Map rating : workRatings) {
+                ArrayList<Map<String, Integer>> possiblyCheating = new ArrayList<>(workRatings);
+                possiblyCheating.remove(rating);
+                means.add(meanOfWorkRatings(possiblyCheating));
+            }
+        } else {
+            for (Map rating : workRatings) {
+                means.add(meanOfWorkRatings(workRatings));
+            }
         }
         means.sort(byResponsibility);
-        String resp = "responosibility";
-        if (means.get(0).get(resp) - threshold > means.get(1).get(resp))
-            cheat = true;
+        System.out.println(means.get(means.size() / 2).toString());
         System.out.println(means.toString());
 
     }
@@ -85,7 +107,7 @@ public class HashMapTest {
         work2.put("cooperation", 5.);
         work2.put("communication", 3.);
         work2.put("autonomous", 4.);
-        ArrayList<Map<String,Integer>> workRatings = new ArrayList<>();
+        ArrayList<Map<String, Integer>> workRatings = new ArrayList<>();
         workRatings.add(work);
         workRatings.add(work2);
         workRatings.add(work2);
-- 
GitLab