add separate class for intern Ant representation and adopt gui cell size to panel size

This commit is contained in:
Jan Löwenstrom 2019-12-09 12:08:53 +01:00
parent c11cc2c3f2
commit 2fb218a129
7 changed files with 56 additions and 32 deletions

View File

@ -5,13 +5,9 @@
<output-test url="file://$MODULE_DIR$/build/classes/java/test" /> <output-test url="file://$MODULE_DIR$/build/classes/java/test" />
<exclude-output /> <exclude-output />
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/.gradle" /> <excludeFolder url="file://$MODULE_DIR$/.gradle" />
<excludeFolder url="file://$MODULE_DIR$/build" /> <excludeFolder url="file://$MODULE_DIR$/build" />
</content> </content>
<content url="file://$MODULE_DIR$/build/generated/sources/annotationProcessor/java/main">
<sourceFolder url="file://$MODULE_DIR$/build/generated/sources/annotationProcessor/java/main" isTestSource="false" generated="true" />
</content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: org.projectlombok:lombok:1.18.10" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Gradle: org.projectlombok:lombok:1.18.10" level="project" />

View File

@ -0,0 +1,22 @@
package evironment.antGame;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import java.awt.*;
@AllArgsConstructor
@Getter
@Setter
public class Ant {
private Point pos;
private boolean spawned;
@Getter(AccessLevel.NONE)
private boolean hasFood;
public boolean hasFood(){
return hasFood;
}
}

View File

@ -14,7 +14,7 @@ public class AntWorld {
* Intern (backend) representation of the ant. * Intern (backend) representation of the ant.
* The AntWorld essentially acts like the game host of the original AntGame. * The AntWorld essentially acts like the game host of the original AntGame.
*/ */
private MyAnt myAnt; private Ant myAnt;
/** /**
* The client agent. In the original AntGame the host would send jade messages * The client agent. In the original AntGame the host would send jade messages
* of the current observation to each client on every tick. * of the current observation to each client on every tick.
@ -46,16 +46,11 @@ public class AntWorld {
public MainFrame getGui(){ public MainFrame getGui(){
return gui; return gui;
} }
public AntWorld(){ public AntWorld(){
this(Constants.DEFAULT_GRID_WIDTH, Constants.DEFAULT_GRID_HEIGHT, Constants.DEFAULT_FOOD_DENSITY); this(Constants.DEFAULT_GRID_WIDTH, Constants.DEFAULT_GRID_HEIGHT, Constants.DEFAULT_FOOD_DENSITY);
} }
public class MyAnt{
public Point pos;
public boolean hasFood;
public boolean spawned;
}
public StepResult step(DiscreteAction<AntAction> action){ public StepResult step(DiscreteAction<AntAction> action){
AntObservation observation; AntObservation observation;
State newState; State newState;
@ -63,18 +58,17 @@ public class AntWorld {
String info = ""; String info = "";
boolean done = false; boolean done = false;
if(!myAnt.spawned){ if(!myAnt.isSpawned()){
myAnt.spawned = true; myAnt.setSpawned(true);
myAnt.pos = grid.getStartPoint(); myAnt.setPos(grid.getStartPoint());
observation = new AntObservation(grid.getCell(myAnt.getPos()), myAnt.getPos(), myAnt.hasFood());
observation = new AntObservation(grid.getCell(myAnt.pos), myAnt.pos, myAnt.hasFood);
newState = antAgent.feedObservation(observation); newState = antAgent.feedObservation(observation);
reward = 0.0; reward = 0.0;
return new StepResult(newState, reward, false, "Just spawned on the map"); return new StepResult(newState, reward, false, "Just spawned on the map");
} }
Cell currentCell = grid.getCell(myAnt.pos); Cell currentCell = grid.getCell(myAnt.getPos());
Point potentialNextPos = new Point(myAnt.pos.x, myAnt.pos.y); Point potentialNextPos = new Point(myAnt.getPos().x, myAnt.getPos().y);
boolean stayOnCell = true; boolean stayOnCell = true;
// flag to enable a check if all food has been collected only fired if food was dropped // flag to enable a check if all food has been collected only fired if food was dropped
// on the starting position // on the starting position
@ -98,7 +92,7 @@ public class AntWorld {
stayOnCell = false; stayOnCell = false;
break; break;
case PICK_UP: case PICK_UP:
if(myAnt.hasFood){ if(myAnt.hasFood()){
// Ant tries to pick up food but can only hold one piece // Ant tries to pick up food but can only hold one piece
reward = Reward.FOOD_PICK_UP_FAIL_HAS_FOOD_ALREADY; reward = Reward.FOOD_PICK_UP_FAIL_HAS_FOOD_ALREADY;
}else if(currentCell.getFood() == 0){ }else if(currentCell.getFood() == 0){
@ -107,18 +101,18 @@ public class AntWorld {
}else if(currentCell.getFood() > 0){ }else if(currentCell.getFood() > 0){
// Ant successfully picks up food // Ant successfully picks up food
currentCell.setFood(currentCell.getFood() - 1); currentCell.setFood(currentCell.getFood() - 1);
myAnt.hasFood = true; myAnt.setHasFood(true);
reward = Reward.FOOD_DROP_DOWN_SUCCESS; reward = Reward.FOOD_DROP_DOWN_SUCCESS;
} }
break; break;
case DROP_DOWN: case DROP_DOWN:
if(!myAnt.hasFood){ if(!myAnt.hasFood()){
// Ant had no food to drop // Ant had no food to drop
reward = Reward.FOOD_DROP_DOWN_FAIL_NO_FOOD; reward = Reward.FOOD_DROP_DOWN_FAIL_NO_FOOD;
}else{ }else{
// Drop food onto the ground // Drop food onto the ground
currentCell.setFood(currentCell.getFood() + 1); currentCell.setFood(currentCell.getFood() + 1);
myAnt.hasFood = false; myAnt.setHasFood(false);
// negative reward if the agent drops food on any other field // negative reward if the agent drops food on any other field
// than the starting point // than the starting point
@ -147,8 +141,8 @@ public class AntWorld {
// valid movement // valid movement
if(!stayOnCell){ if(!stayOnCell){
myAnt.pos = potentialNextPos; myAnt.setPos(potentialNextPos);
if(antAgent.getCell(myAnt.pos).getType() == CellType.UNKNOWN){ if(antAgent.getCell(myAnt.getPos()).getType() == CellType.UNKNOWN){
// the ant will move to a cell that was previously unknown // the ant will move to a cell that was previously unknown
reward = Reward.UNKNOWN_FIELD_EXPLORED; reward = Reward.UNKNOWN_FIELD_EXPLORED;
}else{ }else{
@ -157,7 +151,7 @@ public class AntWorld {
} }
// get observation after action was computed // get observation after action was computed
observation = new AntObservation(grid.getCell(myAnt.pos), myAnt.pos, myAnt.hasFood); observation = new AntObservation(grid.getCell(myAnt.getPos()), myAnt.getPos(), myAnt.hasFood());
// let the ant agent process the observation to create a valid markov state // let the ant agent process the observation to create a valid markov state
newState = antAgent.feedObservation(observation); newState = antAgent.feedObservation(observation);
@ -183,7 +177,7 @@ public class AntWorld {
public void reset() { public void reset() {
RNG.reseed(); RNG.reseed();
grid.initRandomWorld(); grid.initRandomWorld();
myAnt = new MyAnt(); myAnt = new Ant(new Point(-1,-1), false, false);
} }
public void setMaxEpisodeLength(int maxTicks){ public void setMaxEpisodeLength(int maxTicks){
@ -197,12 +191,12 @@ public class AntWorld {
return grid.getGrid(); return grid.getGrid();
} }
public MyAnt getMyAnt(){ public Ant getAnt(){
return myAnt; return myAnt;
} }
public static void main(String[] args) { public static void main(String[] args) {
RNG.setSeed(1993); RNG.setSeed(1993);
AntWorld a = new AntWorld(30, 30, 0.1); AntWorld a = new AntWorld(10, 10, 0.1);
System.out.println("ayay"); System.out.println("ayay");
a.getGui().repaint(); a.getGui().repaint();
for(int i = 0; i< 10; ++i){ for(int i = 0; i< 10; ++i){

View File

@ -1,7 +1,7 @@
package evironment.antGame; package evironment.antGame;
public class Constants { public class Constants {
public static final int DEFAULT_GRID_WIDTH = 30; public static final int DEFAULT_GRID_WIDTH = 10;
public static final int DEFAULT_GRID_HEIGHT = 30; public static final int DEFAULT_GRID_HEIGHT = 10;
public static final double DEFAULT_FOOD_DENSITY = 0.1; public static final double DEFAULT_FOOD_DENSITY = 0.1;
} }

View File

@ -7,11 +7,14 @@ import java.awt.*;
public class CellsScrollPane extends JScrollPane { public class CellsScrollPane extends JScrollPane {
private int cellSize; private int cellSize;
private final int paneWidth = 500;
private final int paneHeight = 500;
public CellsScrollPane(Cell[][] cells, int size){ public CellsScrollPane(Cell[][] cells, int size){
super(); super();
cellSize = size; cellSize = size;
setPreferredSize(new Dimension(500, 500)); setPreferredSize(new Dimension(paneWidth, paneHeight));
cellSize = (paneWidth- cells.length) /cells.length;
JPanel worldPanel = new JPanel(){ JPanel worldPanel = new JPanel(){
{ {
setPreferredSize(new Dimension(cells.length * cellSize, cells[0].length * cellSize)); setPreferredSize(new Dimension(cells.length * cellSize, cells[0].length * cellSize));

View File

@ -0,0 +1,9 @@
package evironment.antGame.gui;
import javax.swing.*;
public class HistoryPanel extends JPanel {
public HistoryPanel(){
}
}

View File

@ -10,7 +10,7 @@ public class MainFrame extends JFrame {
public MainFrame(AntWorld antWorld, AntAgent antAgent){ public MainFrame(AntWorld antWorld, AntAgent antAgent){
setLayout(new BorderLayout()); setLayout(new BorderLayout());
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
CellsScrollPane worldPane = new CellsScrollPane(antWorld.getCellArray(), 10); CellsScrollPane worldPane = new CellsScrollPane(antWorld.getCellArray(), 10);
CellsScrollPane antBrainPane = new CellsScrollPane(antAgent.getKnownWorld(), 10); CellsScrollPane antBrainPane = new CellsScrollPane(antAgent.getKnownWorld(), 10);