summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/util/Statistics.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/ch/epfl/maze/util/Statistics.java')
-rw-r--r--src/ch/epfl/maze/util/Statistics.java357
1 files changed, 172 insertions, 185 deletions
diff --git a/src/ch/epfl/maze/util/Statistics.java b/src/ch/epfl/maze/util/Statistics.java
index 0a4e7c6..ae16392 100644
--- a/src/ch/epfl/maze/util/Statistics.java
+++ b/src/ch/epfl/maze/util/Statistics.java
@@ -1,197 +1,184 @@
1package ch.epfl.maze.util; 1package ch.epfl.maze.util;
2 2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.LinkedList;
6import java.util.List;
7import java.util.Map;
8import java.util.TreeMap;
9
10import ch.epfl.maze.physical.Animal; 3import ch.epfl.maze.physical.Animal;
11import ch.epfl.maze.simulation.Simulation; 4import ch.epfl.maze.simulation.Simulation;
12 5
6import java.util.*;
7
13/** 8/**
14 * Utility class that allows to compute statistics on a list of results. 9 * Utility class that allows to compute statistics on a list of results.
15 *
16 */ 10 */
17 11
18public final class Statistics { 12public final class Statistics {
19 13
20 /* constants for the length of the distribution axis */ 14 /* constants for the length of the distribution axis */
21 public static final int X_LENGTH = 40; 15 public static final int X_LENGTH = 40;
22 public static final int Y_LENGTH = 13; 16 public static final int Y_LENGTH = 13;
23 17
24 /** 18 /**
25 * Returns the sum of all the numbers in results. 19 * Returns the sum of all the numbers in results.
26 * 20 *
27 * @param results 21 * @param results List of numbers
28 * List of numbers 22 * @return The total of the list
29 * @return The total of the list 23 */
30 */ 24
31 25 public static int total(List<Integer> results) {
32 public static int total(List<Integer> results) { 26 int total = 0;
33 int total = 0; 27 for (Integer result : results) {
34 for (Integer result : results) { 28 if (result == Integer.MAX_VALUE) {
35 if (result == Integer.MAX_VALUE) { 29 return Integer.MAX_VALUE;
36 return Integer.MAX_VALUE; 30 }
37 } 31 total += result;
38 total += result; 32 }
39 } 33 return total;
40 return total; 34 }
41 } 35
42 36 /**
43 /** 37 * Returns the mean of the numbers in results.
44 * Returns the mean of the numbers in results. 38 * <p>
45 * <p> 39 * mean(<b>X</b>) = total(<b>X</b>) / N
46 * mean(<b>X</b>) = total(<b>X</b>) / N 40 *
47 * 41 * @param results List of numbers
48 * @param results 42 * @return The mean of the results
49 * List of numbers 43 */
50 * @return The mean of the results 44
51 */ 45 public static int mean(List<Integer> results) {
52 46 int total = total(results);
53 public static int mean(List<Integer> results) { 47 if (total == Integer.MAX_VALUE) {
54 int total = total(results); 48 return Integer.MAX_VALUE;
55 if (total == Integer.MAX_VALUE) { 49 }
56 return Integer.MAX_VALUE; 50 return total / results.size();
57 } 51 }
58 return total / results.size(); 52
59 } 53 /**
60 54 * Returns the variance of the numbers in results.
61 /** 55 * <p>
62 * Returns the variance of the numbers in results. 56 * var(<b>X</b>) = (<b>X</b> - mean(<b>X</b>)) / N
63 * <p> 57 *
64 * var(<b>X</b>) = (<b>X</b> - mean(<b>X</b>)) / N 58 * @param results List of numbers
65 * 59 * @return The variance of the results
66 * @param results 60 */
67 * List of numbers 61
68 * @return The variance of the results 62 public static double var(List<Integer> results) {
69 */ 63 double mean = mean(results);
70 64 if (mean == Integer.MAX_VALUE) {
71 public static double var(List<Integer> results) { 65 return Integer.MAX_VALUE;
72 double mean = mean(results); 66 }
73 if (mean == Integer.MAX_VALUE) { 67 double var = 0;
74 return Integer.MAX_VALUE; 68 for (Integer result : results) {
75 } 69 var += (result - mean) * (result - mean);
76 double var = 0; 70 }
77 for (Integer result : results) { 71 return var / results.size();
78 var += (result - mean) * (result - mean); 72 }
79 } 73
80 return var / results.size(); 74 /**
81 } 75 * Returns the standard deviation of the numbers in results.
82 76 * <p>
83 /** 77 * std(<b>X</b>) = sqrt(var(<b>X</b>))
84 * Returns the standard deviation of the numbers in results. 78 *
85 * <p> 79 * @param results List of numbers
86 * std(<b>X</b>) = sqrt(var(<b>X</b>)) 80 * @return The variance of the results
87 * 81 */
88 * @param results 82
89 * List of numbers 83 public static double std(List<Integer> results) {
90 * @return The variance of the results 84 return Math.sqrt(var(results));
91 */ 85 }
92 86
93 public static double std(List<Integer> results) { 87 /**
94 return Math.sqrt(var(results)); 88 * Computes distribution for each animal in simulation
95 } 89 *
96 90 * @param simulation Simulation to make statistics on
97 /** 91 * @param numberOfSimulations The number of simulations
98 * Computes distribution for each animal in simulation 92 */
99 * 93
100 * @param simulation 94 public static Map<String, List<Integer>> computeStatistics(
101 * Simulation to make statistics on 95 Simulation simulation, int numberOfSimulations) {
102 * @param numberOfSimulations 96 // maps animals' names with their overall results (which are linked-list)
103 * The number of simulations 97 Map<String, List<Integer>> results = new TreeMap<String, List<Integer>>();
104 */ 98
105 99 for (Animal a : simulation.getWorld().getAnimals()) {
106 public static Map<String, List<Integer>> computeStatistics( 100 results.put(a.getClass().getSimpleName(), new LinkedList<Integer>());
107 Simulation simulation, int numberOfSimulations) { 101 }
108 // maps animals' names with their overall results (which are linked-list) 102
109 Map<String, List<Integer>> results = new TreeMap<String, List<Integer>>(); 103 // simulates world a lot of times
110 104 for (int i = 0; i < numberOfSimulations; i++) {
111 for (Animal a : simulation.getWorld().getAnimals()) { 105
112 results.put(a.getClass().getSimpleName(), new LinkedList<Integer>()); 106 // simulates world until the end
113 } 107 simulation.restart();
114 108 while (!simulation.isOver()) {
115 // simulates world a lot of times 109 simulation.move(null);
116 for (int i = 0; i < numberOfSimulations; i++) { 110 }
117 111
118 // simulates world until the end 112 // retrieves arrival times and appends them to the results
119 simulation.restart(); 113 Map<Integer, List<Animal>> arrivalTimes = simulation.getArrivalTimes();
120 while (!simulation.isOver()) { 114 for (Map.Entry<Integer, List<Animal>> entry : arrivalTimes.entrySet()) {
121 simulation.move(null); 115 for (Animal a : entry.getValue()) {
122 } 116 String animalName = a.getClass().getSimpleName();
123 117 List<Integer> list = results.get(animalName);
124 // retrieves arrival times and appends them to the results 118 list.add(entry.getKey());
125 Map<Integer, List<Animal>> arrivalTimes = simulation.getArrivalTimes(); 119 }
126 for (Map.Entry<Integer, List<Animal>> entry : arrivalTimes.entrySet()) { 120 }
127 for (Animal a : entry.getValue()) { 121 }
128 String animalName = a.getClass().getSimpleName(); 122
129 List<Integer> list = results.get(animalName); 123 return results;
130 list.add(entry.getKey()); 124 }
131 } 125
132 } 126 /**
133 } 127 * Prints the distribution of all the results.
134 128 *
135 return results; 129 * @param results List of numbers
136 } 130 */
137 131
138 /** 132 public static void printDistribution(List<Integer> results) {
139 * Prints the distribution of all the results. 133
140 * 134 int min = results.get(0);
141 * @param results 135 int max = results.get(results.size() - 1);
142 * List of numbers 136 int length = (max - min) / X_LENGTH;
143 */ 137
144 138 // counts number of steps inside a range
145 public static void printDistribution(List<Integer> results) { 139 int lowerBound = Integer.MIN_VALUE;
146 140 int upperBound = min + length;