diff options
author | Pacien TRAN-GIRARD | 2015-11-24 15:23:36 +0100 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2015-11-24 15:37:19 +0100 |
commit | 33e497e3083c8446588244c1fa9e69f70af05664 (patch) | |
tree | 3ef665e3c9478f196bb6d61dd15a71c0a9919216 /src/ch/epfl/maze | |
parent | 6f2f503bbf75901273376c8c009a6b96886650c8 (diff) | |
download | maze-solver-33e497e3083c8446588244c1fa9e69f70af05664.tar.gz |
Refactor Mouse strategy
Diffstat (limited to 'src/ch/epfl/maze')
7 files changed, 183 insertions, 2 deletions
diff --git a/src/ch/epfl/maze/physical/stragegies/picker/BlindPicker.java b/src/ch/epfl/maze/physical/stragegies/picker/BlindPicker.java new file mode 100644 index 0000000..6dcf229 --- /dev/null +++ b/src/ch/epfl/maze/physical/stragegies/picker/BlindPicker.java | |||
@@ -0,0 +1,20 @@ | |||
1 | package ch.epfl.maze.physical.stragegies.picker; | ||
2 | |||
3 | import ch.epfl.maze.physical.World; | ||
4 | import ch.epfl.maze.util.Direction; | ||
5 | |||
6 | import java.util.Set; | ||
7 | |||
8 | /** | ||
9 | * A decision maker unaware of its distant environment. | ||
10 | * | ||
11 | * @author Pacien TRAN-GIRARD | ||
12 | */ | ||
13 | public interface BlindPicker extends ChoicePicker { | ||
14 | |||
15 | @Override | ||
16 | default Direction pick(Set<Direction> choices, World world) { | ||
17 | return this.pick(choices); | ||
18 | } | ||
19 | |||
20 | } | ||
diff --git a/src/ch/epfl/maze/physical/stragegies/picker/ChoicePicker.java b/src/ch/epfl/maze/physical/stragegies/picker/ChoicePicker.java new file mode 100644 index 0000000..76fe3eb --- /dev/null +++ b/src/ch/epfl/maze/physical/stragegies/picker/ChoicePicker.java | |||
@@ -0,0 +1,44 @@ | |||
1 | package ch.epfl.maze.physical.stragegies.picker; | ||
2 | |||
3 | import ch.epfl.maze.physical.World; | ||
4 | import ch.epfl.maze.util.Direction; | ||
5 | import ch.epfl.maze.util.Vector2D; | ||
6 | |||
7 | import java.util.Set; | ||
8 | |||
9 | /** | ||
10 | * A decision maker that can pick a Direction given multiple choices. | ||
11 | * | ||
12 | * @author Pacien TRAN-GIRARD | ||
13 | */ | ||
14 | public interface ChoicePicker { | ||
15 | |||
16 | Direction FALLBACK_DIRECTION = Direction.NONE; | ||
17 | |||
18 | /** | ||
19 | * Retrieves the next direction of the animal, by selecting one choice among | ||
20 | * the ones available from its position. | ||
21 | * | ||
22 | * @param choices The choices left to the animal at its current position (see | ||
23 | * {@link ch.epfl.maze.physical.World#getChoices(Vector2D) | ||
24 | * World.getChoices(Vector2D)}) | ||
25 | * @return The next direction of the animal, chosen in {@code choices} | ||
26 | */ | ||
27 | Direction pick(Set<Direction> choices); | ||
28 | |||
29 | /** | ||
30 | * Retrieves the next direction of the animal, by selecting one choice among | ||
31 | * the ones available from its position. | ||
32 | * <p> | ||
33 | * In this variation, the animal knows the world entirely. It can therefore | ||
34 | * use the position of other animals in the daedalus to move more effectively. | ||
35 | * | ||
36 | * @param choices The choices left to the animal at its current position (see | ||
37 | * {@link ch.epfl.maze.physical.World#getChoices(Vector2D) | ||
38 | * World.getChoices(Vector2D)}) | ||
39 | * @param world The world in which the animal moves | ||
40 | * @return The next direction of the animal, chosen in {@code choices} | ||
41 | */ | ||
42 | Direction pick(Set<Direction> choices, World world); | ||
43 | |||
44 | } | ||
diff --git a/src/ch/epfl/maze/physical/stragegies/picker/RandomPicker.java b/src/ch/epfl/maze/physical/stragegies/picker/RandomPicker.java new file mode 100644 index 0000000..427c0eb --- /dev/null +++ b/src/ch/epfl/maze/physical/stragegies/picker/RandomPicker.java | |||
@@ -0,0 +1,27 @@ | |||
1 | package ch.epfl.maze.physical.stragegies.picker; | ||
2 | |||
3 | import ch.epfl.maze.util.Direction; | ||
4 | |||
5 | import java.util.ArrayList; | ||
6 | import java.util.List; | ||
7 | import java.util.Random; | ||
8 | import java.util.Set; | ||
9 | |||
10 | /** | ||
11 | * A probabilistic decision maker. | ||
12 | * | ||
13 | * @author Pacien TRAN-GIRARD | ||
14 | */ | ||
15 | public interface RandomPicker extends BlindPicker { | ||
16 | |||
17 | Random RANDOM_SOURCE = new Random(); | ||
18 | |||
19 | @Override | ||
20 | default Direction pick(Set<Direction> choices) { | ||
21 | if (choices.isEmpty()) return FALLBACK_DIRECTION; | ||
22 | |||
23 | List<Direction> choiceList = new ArrayList<>(choices); | ||
24 | return choiceList.get(RANDOM_SOURCE.nextInt(choices.size())); | ||
25 | } | ||
26 | |||
27 | } | ||
diff --git a/src/ch/epfl/maze/physical/stragegies/reducer/BackwardReducer.java b/src/ch/epfl/maze/physical/stragegies/reducer/BackwardReducer.java new file mode 100644 index 0000000..4ef2ff8 --- /dev/null +++ b/src/ch/epfl/maze/physical/stragegies/reducer/BackwardReducer.java | |||
@@ -0,0 +1,22 @@ | |||
1 | package ch.epfl.maze.physical.stragegies.reducer; | ||
2 | |||
3 | import ch.epfl.maze.util.Direction; | ||
4 | |||
5 | import java.util.Set; | ||
6 | |||
7 | /** | ||
8 | * A filter removing the possibility to go backward. | ||
9 | * | ||
10 | * @author Pacien TRAN-GIRARD | ||
11 | */ | ||
12 | public interface BackwardReducer extends BlindChoiceReducer { | ||
13 | |||
14 | Direction getDirection(); | ||
15 | |||
16 | @Override | ||
17 | default Set<Direction> reduce(Set<Direction> choices) { | ||
18 | choices.remove(this.getDirection().reverse()); | ||
19 | return choices; | ||
20 | } | ||
21 | |||
22 | } | ||
diff --git a/src/ch/epfl/maze/physical/stragegies/reducer/BlindChoiceReducer.java b/src/ch/epfl/maze/physical/stragegies/reducer/BlindChoiceReducer.java new file mode 100644 index 0000000..169a8f6 --- /dev/null +++ b/src/ch/epfl/maze/physical/stragegies/reducer/BlindChoiceReducer.java | |||
@@ -0,0 +1,20 @@ | |||
1 | package ch.epfl.maze.physical.stragegies.reducer; | ||
2 | |||
3 | import ch.epfl.maze.physical.World; | ||
4 | import ch.epfl.maze.util.Direction; | ||
5 | |||
6 | import java.util.Set; | ||
7 | |||
8 | /** | ||
9 | * A decision filter unaware of its distant environment. | ||
10 | * | ||
11 | * @author Pacien TRAN-GIRARD | ||
12 | */ | ||
13 | public interface BlindChoiceReducer extends ChoiceReducer { | ||
14 | |||
15 | @Override | ||
16 | default Set<Direction> reduce(Set<Direction> choices, World world) { | ||
17 | return this.reduce(choices); | ||
18 | } | ||
19 | |||
20 | } | ||
diff --git a/src/ch/epfl/maze/physical/stragegies/reducer/ChoiceReducer.java b/src/ch/epfl/maze/physical/stragegies/reducer/ChoiceReducer.java new file mode 100644 index 0000000..3cb744d --- /dev/null +++ b/src/ch/epfl/maze/physical/stragegies/reducer/ChoiceReducer.java | |||
@@ -0,0 +1,34 @@ | |||
1 | package ch.epfl.maze.physical.stragegies.reducer; | ||
2 | |||
3 | import ch.epfl.maze.physical.World; | ||
4 | import ch.epfl.maze.util.Direction; | ||
5 | import ch.epfl.maze.util.Vector2D; | ||
6 | |||
7 | import java.util.Set; | ||
8 | |||
9 | /** | ||
10 | * A choice filter that can exclude choices. | ||
11 | * | ||
12 | * @author Pacien TRAN-GIRARD | ||
13 | */ | ||
14 | public interface ChoiceReducer { | ||
15 | |||
16 | /** | ||
17 | * Reduces the Direction choices by eliminating the improper ones from the given choices. | ||
18 | * | ||
19 | * @param choices The choices left to the animal | ||
20 | * @return A subset of possible direction of the animal, chosen in {@code choices} | ||
21 | */ | ||
22 | Set<Direction> reduce(Set<Direction> choices); | ||
23 | |||
24 | /** | ||
25 | * Reduces the Direction choices by eliminating the improper ones from the given choices. | ||
26 | * In this variation, the animal knows the world entirely. It can therefore | ||
27 | * use the position of other animals in the world to move more effectively. | ||
28 | * | ||
29 | * @param choices The choices left to the animal | ||
30 | * @return A subset of possible direction of the animal, chosen in {@code choices} | ||
31 | */ | ||
32 | Set<Direction> reduce(Set<Direction> choices, World world); | ||
33 | |||
34 | } | ||
diff --git a/src/ch/epfl/maze/physical/zoo/Mouse.java b/src/ch/epfl/maze/physical/zoo/Mouse.java index f7084d7..8f68af6 100644 --- a/src/ch/epfl/maze/physical/zoo/Mouse.java +++ b/src/ch/epfl/maze/physical/zoo/Mouse.java | |||
@@ -1,16 +1,20 @@ | |||
1 | package ch.epfl.maze.physical.zoo; | 1 | package ch.epfl.maze.physical.zoo; |
2 | 2 | ||
3 | import ch.epfl.maze.physical.Animal; | 3 | import ch.epfl.maze.physical.Animal; |
4 | import ch.epfl.maze.physical.ProbabilisticAnimal; | 4 | import ch.epfl.maze.physical.stragegies.picker.RandomPicker; |
5 | import ch.epfl.maze.physical.stragegies.reducer.BackwardReducer; | ||
6 | import ch.epfl.maze.util.Direction; | ||
5 | import ch.epfl.maze.util.Vector2D; | 7 | import ch.epfl.maze.util.Vector2D; |
6 | 8 | ||
9 | import java.util.Set; | ||
10 | |||
7 | /** | 11 | /** |
8 | * Mouse A.I. that remembers only the previous choice it has made. | 12 | * Mouse A.I. that remembers only the previous choice it has made. |
9 | * | 13 | * |
10 | * @author EPFL | 14 | * @author EPFL |
11 | * @author Pacien |