summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/main/Console.java
blob: ebe1e3e97f0dd249f1babda0fcde151cdce8e3a6 (plain)
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
package ch.epfl.maze.main;

import ch.epfl.maze.simulation.Simulation;
import ch.epfl.maze.util.SimulationGenerator;
import ch.epfl.maze.util.Statistics;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Mini-project main program that will run the simulations multiple times and
 * show statistics on the console.
 *
 * @author EPFL
 */
public class Console {

    /**
     * Number of simulations launched.
     */
    public static final int NUMBER_OF_SIMULATIONS = 1000;
    public static final String DEFAULT_SIM = "maze";

    public static void main(String[] args) {
        String simName = args.length > 0 ? args[0] : DEFAULT_SIM;
        Simulation simulation = SimulationGenerator.getSimulation(simName);

        System.out.print("Launching " + NUMBER_OF_SIMULATIONS + " simulations...");
        Map<String, List<Integer>> results =
                Statistics.computeStatistics(simulation, NUMBER_OF_SIMULATIONS);
        System.out.println(" done !");

        printStats(results);
    }

    /**
     * Pretty-prints the statistics computed in the parameters.
     *
     * @param results Statistics of arrival times for every animals/preys
     */
    public static void printStats(Map<String, List<Integer>> results) {
        // computes statistics
        for (Map.Entry<String, List<Integer>> entry : results.entrySet()) {
            String name = entry.getKey();
            List<Integer> list = entry.getValue();
            if (list.isEmpty()) {
                continue;
            }
            Collections.sort(list);

            String max, min, std, mean, median, total;
            // handles infinite values
            if (Statistics.total(list) == Integer.MAX_VALUE) {
                total = "Infinite";
                mean = "Infinite";
                std = "Infinite";
                max = "Infinite";
            } else {
                total = Integer.toString(Statistics.total(list));
                mean = Integer.toString(Statistics.mean(list));
                std = Double.toString(Statistics.std(list));
                max = Integer.toString(list.get(list.size() - 1));
            }
            // min and median are special
            min = (list.get(0) == Integer.MAX_VALUE) ?
                    "Infinite" : Integer.toString(list.get(0));
            median = (list.get(list.size() / 2) == Integer.MAX_VALUE) ?
                    "Infinite" : Integer.toString(list.get(list.size() / 2));

            System.out.println("\n\n========== " + name + " ==========\n");
            System.out.println(" * total number of steps : " + total);
            System.out.println(" * average steps : " + mean);
            System.out.println(" * median steps : " + median);
            System.out.println(" * standard deviation : " + std);
            System.out.println(" * minimum steps : " + min);
            System.out.println(" * maximum steps : " + max);
            System.out.println("\nDistribution :");
            Statistics.printDistribution(list);
        }
    }

}