From 33e497e3083c8446588244c1fa9e69f70af05664 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 24 Nov 2015 15:23:36 +0100 Subject: Refactor Mouse strategy --- .../physical/stragegies/picker/BlindPicker.java | 20 ++++++++++ .../physical/stragegies/picker/ChoicePicker.java | 44 ++++++++++++++++++++++ .../physical/stragegies/picker/RandomPicker.java | 27 +++++++++++++ .../stragegies/reducer/BackwardReducer.java | 22 +++++++++++ .../stragegies/reducer/BlindChoiceReducer.java | 20 ++++++++++ .../physical/stragegies/reducer/ChoiceReducer.java | 34 +++++++++++++++++ src/ch/epfl/maze/physical/zoo/Mouse.java | 18 ++++++++- 7 files changed, 183 insertions(+), 2 deletions(-) create mode 100644 src/ch/epfl/maze/physical/stragegies/picker/BlindPicker.java create mode 100644 src/ch/epfl/maze/physical/stragegies/picker/ChoicePicker.java create mode 100644 src/ch/epfl/maze/physical/stragegies/picker/RandomPicker.java create mode 100644 src/ch/epfl/maze/physical/stragegies/reducer/BackwardReducer.java create mode 100644 src/ch/epfl/maze/physical/stragegies/reducer/BlindChoiceReducer.java create mode 100644 src/ch/epfl/maze/physical/stragegies/reducer/ChoiceReducer.java 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 @@ +package ch.epfl.maze.physical.stragegies.picker; + +import ch.epfl.maze.physical.World; +import ch.epfl.maze.util.Direction; + +import java.util.Set; + +/** + * A decision maker unaware of its distant environment. + * + * @author Pacien TRAN-GIRARD + */ +public interface BlindPicker extends ChoicePicker { + + @Override + default Direction pick(Set choices, World world) { + return this.pick(choices); + } + +} 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 @@ +package ch.epfl.maze.physical.stragegies.picker; + +import ch.epfl.maze.physical.World; +import ch.epfl.maze.util.Direction; +import ch.epfl.maze.util.Vector2D; + +import java.util.Set; + +/** + * A decision maker that can pick a Direction given multiple choices. + * + * @author Pacien TRAN-GIRARD + */ +public interface ChoicePicker { + + Direction FALLBACK_DIRECTION = Direction.NONE; + + /** + * Retrieves the next direction of the animal, by selecting one choice among + * the ones available from its position. + * + * @param choices The choices left to the animal at its current position (see + * {@link ch.epfl.maze.physical.World#getChoices(Vector2D) + * World.getChoices(Vector2D)}) + * @return The next direction of the animal, chosen in {@code choices} + */ + Direction pick(Set choices); + + /** + * Retrieves the next direction of the animal, by selecting one choice among + * the ones available from its position. + *

+ * In this variation, the animal knows the world entirely. It can therefore + * use the position of other animals in the daedalus to move more effectively. + * + * @param choices The choices left to the animal at its current position (see + * {@link ch.epfl.maze.physical.World#getChoices(Vector2D) + * World.getChoices(Vector2D)}) + * @param world The world in which the animal moves + * @return The next direction of the animal, chosen in {@code choices} + */ + Direction pick(Set choices, World world); + +} 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 @@ +package ch.epfl.maze.physical.stragegies.picker; + +import ch.epfl.maze.util.Direction; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.Set; + +/** + * A probabilistic decision maker. + * + * @author Pacien TRAN-GIRARD + */ +public interface RandomPicker extends BlindPicker { + + Random RANDOM_SOURCE = new Random(); + + @Override + default Direction pick(Set choices) { + if (choices.isEmpty()) return FALLBACK_DIRECTION; + + List choiceList = new ArrayList<>(choices); + return choiceList.get(RANDOM_SOURCE.nextInt(choices.size())); + } + +} 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 @@ +package ch.epfl.maze.physical.stragegies.reducer; + +import ch.epfl.maze.util.Direction; + +import java.util.Set; + +/** + * A filter removing the possibility to go backward. + * + * @author Pacien TRAN-GIRARD + */ +public interface BackwardReducer extends BlindChoiceReducer { + + Direction getDirection(); + + @Override + default Set reduce(Set choices) { + choices.remove(this.getDirection().reverse()); + return choices; + } + +} 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 @@ +package ch.epfl.maze.physical.stragegies.reducer; + +import ch.epfl.maze.physical.World; +import ch.epfl.maze.util.Direction; + +import java.util.Set; + +/** + * A decision filter unaware of its distant environment. + * + * @author Pacien TRAN-GIRARD + */ +public interface BlindChoiceReducer extends ChoiceReducer { + + @Override + default Set reduce(Set choices, World world) { + return this.reduce(choices); + } + +} 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 @@ +package ch.epfl.maze.physical.stragegies.reducer; + +import ch.epfl.maze.physical.World; +import ch.epfl.maze.util.Direction; +import ch.epfl.maze.util.Vector2D; + +import java.util.Set; + +/** + * A choice filter that can exclude choices. + * + * @author Pacien TRAN-GIRARD + */ +public interface ChoiceReducer { + + /** + * Reduces the Direction choices by eliminating the improper ones from the given choices. + * + * @param choices The choices left to the animal + * @return A subset of possible direction of the animal, chosen in {@code choices} + */ + Set reduce(Set choices); + + /** + * Reduces the Direction choices by eliminating the improper ones from the given choices. + * In this variation, the animal knows the world entirely. It can therefore + * use the position of other animals in the world to move more effectively. + * + * @param choices The choices left to the animal + * @return A subset of possible direction of the animal, chosen in {@code choices} + */ + Set reduce(Set choices, World world); + +} 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 @@ package ch.epfl.maze.physical.zoo; import ch.epfl.maze.physical.Animal; -import ch.epfl.maze.physical.ProbabilisticAnimal; +import ch.epfl.maze.physical.stragegies.picker.RandomPicker; +import ch.epfl.maze.physical.stragegies.reducer.BackwardReducer; +import ch.epfl.maze.util.Direction; import ch.epfl.maze.util.Vector2D; +import java.util.Set; + /** * Mouse A.I. that remembers only the previous choice it has made. * * @author EPFL * @author Pacien TRAN-GIRARD */ -public class Mouse extends ProbabilisticAnimal { +public class Mouse extends Animal implements BackwardReducer, RandomPicker { /** * Constructs a mouse with a starting position. @@ -21,6 +25,16 @@ public class Mouse extends ProbabilisticAnimal { super(position); } + /** + * Moves according to an improved version of a random walk : the + * mouse does not directly retrace its steps. + */ + @Override + public Direction move(Set choices) { + Set smartChoices = choices.size() > 1 ? this.reduce(choices) : choices; + return this.pick(smartChoices); + } + @Override public Animal copy() { return new Mouse(this.getPosition()); -- cgit v1.2.3