add multiple food scenario

This commit is contained in:
Jan Löwenstrom 2020-04-19 20:55:42 +02:00
parent 7d3d097599
commit 19d5a87ce0
8 changed files with 42 additions and 26 deletions

View File

@ -86,8 +86,10 @@ public class OpeningDialog {
rl = new RLControllerGUI<DinoAction>(new DinoWorld(), (Method) algorithmSelection.getSelectedItem(), DinoAction.values());
} else if(selectedScenario == Scenario.JUMPING_DINO_ADVANCED) {
rl = new RLControllerGUI<DinoAction>(new DinoWorldAdvanced(), (Method) algorithmSelection.getSelectedItem(), DinoAction.values());
} else if(selectedScenario == Scenario.ANTGAME) {
rl = new RLControllerGUI<AntAction>(new AntWorldContinuous(Constants.DEFAULT_GRID_WIDTH, Constants.DEFAULT_GRID_HEIGHT), (Method) algorithmSelection.getSelectedItem(), AntAction.values());
} else if(selectedScenario == Scenario.ANTGAME_ONE_FOOD) {
rl = new RLControllerGUI<AntAction>(new AntWorldContinuous(Constants.DEFAULT_GRID_WIDTH, Constants.DEFAULT_GRID_HEIGHT, 1), (Method) algorithmSelection.getSelectedItem(), AntAction.values());
} else if(selectedScenario == Scenario.ANTGAME_TWO_FOOD) {
rl = new RLControllerGUI<AntAction>(new AntWorldContinuous(Constants.DEFAULT_GRID_WIDTH, Constants.DEFAULT_GRID_HEIGHT, 2), (Method) algorithmSelection.getSelectedItem(), AntAction.values());
} else if(selectedScenario == Scenario.BLACKJACK) {
rl = new RLControllerGUI<PlayerAction>(new BlackJackTable(), (Method) algorithmSelection.getSelectedItem(), PlayerAction.values());
} else {
@ -107,7 +109,8 @@ public class OpeningDialog {
private enum Scenario {
JUMPING_DINO_SIMPLE,
JUMPING_DINO_ADVANCED,
ANTGAME,
ANTGAME_ONE_FOOD,
ANTGAME_TWO_FOOD,
BLACKJACK
}

View File

@ -36,12 +36,14 @@ public class AntWorld implements Environment<AntAction>, Visualizable {
* various lectures could be possible as well.
*/
protected AntAgent antAgent;
protected int numberOfConcurrentFood;
protected int tick;
private int maxEpisodeTicks;
public AntWorld(int width, int height) {
grid = new Grid(width, height);
public AntWorld(int width, int height, int numberOfConcurrentFood) {
this.numberOfConcurrentFood = numberOfConcurrentFood;
grid = new Grid(width, height, numberOfConcurrentFood);
antAgent = new AntAgent(width, height);
myAnt = new Ant();
maxEpisodeTicks = 1000;
@ -49,7 +51,7 @@ public class AntWorld implements Environment<AntAction>, Visualizable {
}
public AntWorld(){
this(Constants.DEFAULT_GRID_WIDTH, Constants.DEFAULT_GRID_HEIGHT);
this(Constants.DEFAULT_GRID_WIDTH, Constants.DEFAULT_GRID_HEIGHT, Constants.DEFAULT_CONCURRENT_FOOD);
}
protected StepCalculation processStep(AntAction action) {
@ -62,7 +64,7 @@ public class AntWorld implements Environment<AntAction>, Visualizable {
sc.stayOnCell = true;
// flag to enable a check if all food has been collected only fired if food was dropped
// on the starting position
sc.checkCompletion = false;
sc.foodCollected = false;
switch(action) {
case MOVE_UP:
@ -110,7 +112,7 @@ public class AntWorld implements Environment<AntAction>, Visualizable {
} else {
sc.reward = Reward.FOOD_DROP_DOWN_SUCCESS;
myAnt.setPoints(myAnt.getPoints() + 1);
sc.checkCompletion = true;
sc.foodCollected = true;
}
}
break;
@ -140,13 +142,9 @@ public class AntWorld implements Environment<AntAction>, Visualizable {
if(!sc.stayOnCell) {
myAnt.getPos().setLocation(sc.potentialNextPos);
antAgent.getCell(myAnt.getPos());// the ant will move to a cell that was previously unknown
// TODO: not optimal for going straight for food
// sc.reward = Reward.UNKNOWN_FIELD_EXPLORED;
}
if(sc.checkCompletion) {
if(sc.foodCollected) {
sc.done = grid.isAllFoodCollected();
}
@ -181,7 +179,7 @@ public class AntWorld implements Environment<AntAction>, Visualizable {
boolean stayOnCell = true;
// flag to enable a check if all food has been collected only fired if food was dropped
// on the starting position
boolean checkCompletion = false;
boolean foodCollected = false;
}
public State reset() {

View File

@ -4,8 +4,8 @@ import core.State;
import core.StepResultEnvironment;
public class AntWorldContinuous extends AntWorld {
public AntWorldContinuous(int width, int height) {
super(width, height);
public AntWorldContinuous(int width, int height, int numberOfConcurrentFood) {
super(width, height, numberOfConcurrentFood);
}
public AntWorldContinuous() {
@ -14,13 +14,15 @@ public class AntWorldContinuous extends AntWorld {
@Override
public StepResultEnvironment step(AntAction action) {
Cell currentCell = grid.getCell(myAnt.getPos());
StepCalculation sc = processStep(action);
// flag is set to true if food gets dropped onto starts
if(sc.checkCompletion) {
grid.spawnNewFood();
if(sc.foodCollected) {
grid.removeAllFood();
System.out.println(numberOfConcurrentFood);
for(int i = 0; i < numberOfConcurrentFood; ++i) {
grid.spawnNewFood();
}
}
// valid movement
if(!sc.stayOnCell) {

View File

@ -3,8 +3,8 @@ package evironment.antGame;
import core.State;
public class AntWorldContinuousOriginalState extends AntWorldContinuous {
public AntWorldContinuousOriginalState(int width, int height) {
super(width, height);
public AntWorldContinuousOriginalState(int width, int height, int numberOfConcurrentFood) {
super(width, height, numberOfConcurrentFood);
}
public AntWorldContinuousOriginalState() {

View File

@ -1,6 +1,7 @@
package evironment.antGame;
public class Constants {
public static final int DEFAULT_CONCURRENT_FOOD = 1;
public static final int DEFAULT_GRID_WIDTH = 8;
public static final int DEFAULT_GRID_HEIGHT = 8;
public static final int START_X = 5;

View File

@ -10,10 +10,12 @@ public class Grid {
private Point start;
private Cell[][] grid;
private Cell[][] initialGrid;
private int numberOfConcurrentFood;
public Grid(int width, int height) {
public Grid(int width, int height, int numberOfConcurrentFood) {
this.width = width;
this.height = height;
this.numberOfConcurrentFood = numberOfConcurrentFood;
grid = new Cell[width][height];
initialGrid = new Cell[width][height];
start = new Point(Constants.START_X, Constants.START_Y);
@ -32,7 +34,9 @@ public class Grid {
}
spawnObstacles();
initialGrid[start.x][start.y] = new Cell(new Point(start.x, start.y), CellType.START);
spawnNewFood(initialGrid);
for(int i = 0; i < numberOfConcurrentFood; ++i) {
spawnNewFood(initialGrid);
}
}
//TODO
@ -70,6 +74,14 @@ public class Grid {
}
}
public void removeAllFood() {
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
grid[x][y].setFood(0);
}
}
}
public void spawnNewFood() {
spawnNewFood(grid);
}

View File

@ -11,7 +11,7 @@ public class ContinuousAnt {
public static void main(String[] args) {
RNG.setSeed(13, true);
RLController<AntAction> rl = new RLControllerGUI<>(
new AntWorldContinuous(8, 8),
new AntWorldContinuous(8, 8, 1),
Method.Q_LEARNING_OFF_POLICY_CONTROL,
AntAction.values());
rl.setDelay(200);

View File

@ -12,7 +12,7 @@ public class RunningAnt {
RNG.setSeed(56);
RLController<AntAction> rl = new RLControllerGUI<>(
new AntWorld(8, 8),
new AntWorld(8, 8, 1),
Method.Q_LEARNING_OFF_POLICY_CONTROL,
AntAction.values());