enhance save/load feature and change thread handling

- saving monte carlo did not include returnSum and returnCount, so it the state would be wrong after loading. Learning, EpisodicLearning and MonteCarlo classes are all overriding custom save and load methods, calling super() each time but including fields that are necessary to replace on runtime.
- moved generic episodic behaviour from monteCarlo to abstract top level class
- using AtomicInteger for episodesToLearn
- moved learning-Thread-handling from controller to model. Learning got one extra Leaning thread.
- add feature to use custom speed and distance for dino world obstacles
This commit is contained in:
2019-12-29 01:12:11 +01:00
parent 64355e0b93
commit 195722e98f
8 changed files with 193 additions and 75 deletions
@@ -14,12 +14,20 @@ import java.awt.*;
public class DinoWorld implements Environment<DinoAction>, Visualizable {
private Dino dino;
private Obstacle currentObstacle;
private boolean randomObstacleSpeed;
private boolean randomObstacleDistance;
public DinoWorld(){
public DinoWorld(boolean randomObstacleSpeed, boolean randomObstacleDistance){
this.randomObstacleSpeed = randomObstacleSpeed;
this.randomObstacleDistance = randomObstacleDistance;
dino = new Dino(Config.DINO_SIZE, Config.DINO_STARTING_X, Config.FRAME_HEIGHT - Config.GROUND_Y - Config.DINO_SIZE, 0, 0, Color.GREEN);
spawnNewObstacle();
}
public DinoWorld(){
this(false, false);
}
private boolean ranIntoObstacle(){
Obstacle o = currentObstacle;
Dino p = dino;
@@ -32,6 +40,7 @@ public class DinoWorld implements Environment<DinoAction>, Visualizable {
return xAxis && yAxis;
}
private int getDistanceToObstacle(){
return currentObstacle.getX() - dino.getX() + Config.DINO_SIZE;
}
@@ -57,8 +66,27 @@ public class DinoWorld implements Environment<DinoAction>, Visualizable {
return new StepResultEnvironment(new DinoState(getDistanceToObstacle()), reward, done, "");
}
private void spawnNewObstacle(){
currentObstacle = new Obstacle(Config.OBSTACLE_SIZE, Config.FRAME_WIDTH + Config.OBSTACLE_SIZE, Config.FRAME_HEIGHT - Config.GROUND_Y - Config.OBSTACLE_SIZE, -Config.OBSTACLE_SPEED, 0, Color.BLACK);
int dx;
int xSpawn;
if(randomObstacleSpeed){
dx = -(int)((Math.random() + 0.5) * Config.OBSTACLE_SPEED);
}else{
dx = -Config.OBSTACLE_SPEED;
}
if(randomObstacleDistance){
// randomly spawning more right outside of the screen
xSpawn = (int)(Math.random() + 0.5 * Config.FRAME_WIDTH + Config.FRAME_WIDTH + Config.OBSTACLE_SIZE);
}else{
// instantly respawning on the left screen border
xSpawn = Config.FRAME_WIDTH + Config.OBSTACLE_SIZE;
}
currentObstacle = new Obstacle(Config.OBSTACLE_SIZE, xSpawn, Config.FRAME_HEIGHT - Config.GROUND_Y - Config.OBSTACLE_SIZE, dx, 0, Color.BLACK);
}
private void spawnDino(){