summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/main/Console.java
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2015-11-21 10:36:18 +0100
committerPacien TRAN-GIRARD2015-11-21 10:36:18 +0100
commit655ac88f4e73b2df532a451aedf5a561ea1b0d2c (patch)
treeef6f914a465575f313e2b280bf0639d87a4cbd58 /src/ch/epfl/maze/main/Console.java
parent56279eb59ccdea48b18daa027a5095d861b4e2f4 (diff)
downloadmaze-solver-655ac88f4e73b2df532a451aedf5a561ea1b0d2c.tar.gz
Import project structure
Diffstat (limited to 'src/ch/epfl/maze/main/Console.java')
-rw-r--r--src/ch/epfl/maze/main/Console.java162
1 files changed, 162 insertions, 0 deletions
diff --git a/src/ch/epfl/maze/main/Console.java b/src/ch/epfl/maze/main/Console.java
new file mode 100644
index 0000000..edad4b2
--- /dev/null
+++ b/src/ch/epfl/maze/main/Console.java
@@ -0,0 +1,162 @@
1package ch.epfl.maze.main;
2
3import java.util.Collections;
4import java.util.List;
5import java.util.Map;
6
7import ch.epfl.maze.physical.Daedalus;
8import ch.epfl.maze.physical.Maze;
9import ch.epfl.maze.physical.pacman.Blinky;
10import ch.epfl.maze.physical.pacman.Clyde;
11import ch.epfl.maze.physical.pacman.Inky;
12import ch.epfl.maze.physical.pacman.PacMan;
13import ch.epfl.maze.physical.pacman.Pinky;
14import ch.epfl.maze.physical.zoo.Bear;
15import ch.epfl.maze.physical.zoo.Hamster;
16import ch.epfl.maze.physical.zoo.Monkey;
17import ch.epfl.maze.physical.zoo.Mouse;
18import ch.epfl.maze.physical.zoo.Panda;
19import ch.epfl.maze.simulation.DaedalusSimulation;
20import ch.epfl.maze.simulation.MazeSimulation;
21import ch.epfl.maze.simulation.Simulation;
22import ch.epfl.maze.util.LabyrinthGenerator;
23import ch.epfl.maze.util.Statistics;
24import ch.epfl.maze.util.Vector2D;
25
26/**
27 * Mini-project main program that will run the simulations multiple times and
28 * show statistics on the console.
29 *
30 */
31
32public class Console {
33
34 /** Number of simulations launched. */
35 public static final int NUMBER_OF_SIMULATIONS = 1000;
36
37 public static void main(String[] args) {
38 Simulation simulation;
39
40 simulation = getMazeSimulation();
41 //simulation = getDaedalusSimulation();
42
43 System.out.print("Launching " + NUMBER_OF_SIMULATIONS + " simulations...");
44 Map<String, List<Integer>> results =
45 Statistics.computeStatistics(simulation, NUMBER_OF_SIMULATIONS);
46 System.out.println(" done !");
47
48 printStats(results);
49 }
50
51 /**
52 * Creates a {@code MazeSimulation} suitable for statistics.
53 * <p>
54 * Note that there should be only <b>ONE</b> animal of each kind in the
55 * corresponding {@code Maze}.
56 *
57 * @return A {@code MazeSimulation} suitable for statistics
58 */
59
60 public static Simulation getMazeSimulation() {
61 int[][] labyrinth = LabyrinthGenerator.getMedium();
62 Maze m = new Maze(labyrinth);
63 Simulation simulation = new MazeSimulation(m);
64
65 // adds a Mouse
66 m.addAnimal(new Mouse(m.getStart()));
67
68 // adds a Monkey
69 m.addAnimal(new Monkey(m.getStart()));
70
71 // adds a Hamster
72 m.addAnimal(new Hamster(m.getStart()));
73
74 // adds a Bear (if this bonus is coded)
75 // m.addAnimal(new Bear(m.getStart()));
76
77 // adds a Panda
78 m.addAnimal(new Panda(m.getStart()));
79
80 return simulation;
81 }
82
83 /**
84 * Creates a {@code DaedalusSimulation} suitable for statistics.
85 * <p>
86 * Note that there should be only <b>ONE</b> animal of each kind in the
87 * corresponding {@code Daedalus}.
88 *
89 * @return A {@code DaedalusSimulation} suitable for statistics
90 */
91
92 public static Simulation getDaedalusSimulation() {
93 int[][] labyrinth = LabyrinthGenerator.getPacMan();
94 Daedalus d = new Daedalus(labyrinth);
95 Simulation simulation = new DaedalusSimulation(d);
96
97 // adds Pac-Man
98 d.addPrey(new PacMan(new Vector2D(9, 15)));
99
100 // adds Blinky
101 d.addPredator(new Blinky(new Vector2D(17, 1)));
102
103 // adds Pinky
104 d.addPredator(new Pinky(new Vector2D(1, 1)));
105
106 // adds Inky
107 d.addPredator(new Inky(new Vector2D(17, 17)));
108
109 // adds Clyde
110 d.addPredator(new Clyde(new Vector2D(1, 17)));
111
112 return simulation;
113 }
114
115 /**
116 * Pretty-prints the statistics computed in the parameters.
117 *
118 * @param results
119 * Statistics of arrival times for every animals/preys
120 */
121
122 public static void printStats(Map<String, List<Integer>> results) {
123 // computes statistics
124 for (Map.Entry<String, List<Integer>> entry : results.entrySet()) {
125 String name = entry.getKey();
126 List<Integer> list = entry.getValue();
127 if (list.isEmpty()) {
128 continue;
129 }
130 Collections.sort(list);
131
132 String max, min, std, mean, median, total;
133 // handles infinite values
134 if (Statistics.total(list) == Integer.MAX_VALUE) {
135 total = "Infinite";
136 mean = "Infinite";
137 std = "Infinite";
138 max = "Infinite";
139 } else {
140 total = Integer.toString(Statistics.total(list));
141 mean = Integer.toString(Statistics.mean(list));
142 std = Double.toString(Statistics.std(list));
143 max = Integer.toString(list.get(list.size() - 1));
144 }
145 // min and median are special
146 min = (list.get(0) == Integer.MAX_VALUE) ?
147 "Infinite" : Integer.toString(list.get(0));
148 median = (list.get(list.size() / 2) == Integer.MAX_VALUE) ?
149 "Infinite" : Integer.toString(list.get(list.size() / 2));
150
151 System.out.println("\n\n========== " + name + " ==========\n");
152 System.out.println(" * total number of steps : " + total);
153 System.out.println(" * average steps : " + mean);
154 System.out.println(" * median steps : " + median);
155 System.out.println(" * standard deviation : " + std);
156 System.out.println(" * minimum steps : " + min);
157 System.out.println(" * maximum steps : " + max);
158 System.out.println("\nDistribution :");
159 Statistics.printDistribution(list);
160 }
161 }
162}