From 2fb218a12993d65458dc8f3606ac88dd545c4a89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20L=C3=B6wenstrom?= Date: Mon, 9 Dec 2019 12:08:53 +0100 Subject: [PATCH] add separate class for intern Ant representation and adopt gui cell size to panel size --- .idea/refo.iml | 4 -- src/main/java/evironment/antGame/Ant.java | 22 ++++++++++ .../java/evironment/antGame/AntWorld.java | 42 ++++++++----------- .../java/evironment/antGame/Constants.java | 4 +- .../antGame/gui/CellsScrollPane.java | 5 ++- .../evironment/antGame/gui/HistoryPanel.java | 9 ++++ .../evironment/antGame/gui/MainFrame.java | 2 +- 7 files changed, 56 insertions(+), 32 deletions(-) create mode 100644 src/main/java/evironment/antGame/Ant.java create mode 100644 src/main/java/evironment/antGame/gui/HistoryPanel.java diff --git a/.idea/refo.iml b/.idea/refo.iml index 2530107..dbef0c9 100644 --- a/.idea/refo.iml +++ b/.idea/refo.iml @@ -5,13 +5,9 @@ - - - - diff --git a/src/main/java/evironment/antGame/Ant.java b/src/main/java/evironment/antGame/Ant.java new file mode 100644 index 0000000..d35a392 --- /dev/null +++ b/src/main/java/evironment/antGame/Ant.java @@ -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; + } +} diff --git a/src/main/java/evironment/antGame/AntWorld.java b/src/main/java/evironment/antGame/AntWorld.java index 3ad3cb9..18db7eb 100644 --- a/src/main/java/evironment/antGame/AntWorld.java +++ b/src/main/java/evironment/antGame/AntWorld.java @@ -14,7 +14,7 @@ public class AntWorld { * Intern (backend) representation of the ant. * 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 * of the current observation to each client on every tick. @@ -46,16 +46,11 @@ public class AntWorld { public MainFrame getGui(){ return gui; } + public AntWorld(){ 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 action){ AntObservation observation; State newState; @@ -63,18 +58,17 @@ public class AntWorld { String info = ""; boolean done = false; - if(!myAnt.spawned){ - myAnt.spawned = true; - myAnt.pos = grid.getStartPoint(); - - observation = new AntObservation(grid.getCell(myAnt.pos), myAnt.pos, myAnt.hasFood); + if(!myAnt.isSpawned()){ + myAnt.setSpawned(true); + myAnt.setPos(grid.getStartPoint()); + observation = new AntObservation(grid.getCell(myAnt.getPos()), myAnt.getPos(), myAnt.hasFood()); newState = antAgent.feedObservation(observation); reward = 0.0; return new StepResult(newState, reward, false, "Just spawned on the map"); } - Cell currentCell = grid.getCell(myAnt.pos); - Point potentialNextPos = new Point(myAnt.pos.x, myAnt.pos.y); + Cell currentCell = grid.getCell(myAnt.getPos()); + Point potentialNextPos = new Point(myAnt.getPos().x, myAnt.getPos().y); boolean stayOnCell = true; // flag to enable a check if all food has been collected only fired if food was dropped // on the starting position @@ -98,7 +92,7 @@ public class AntWorld { stayOnCell = false; break; case PICK_UP: - if(myAnt.hasFood){ + if(myAnt.hasFood()){ // Ant tries to pick up food but can only hold one piece reward = Reward.FOOD_PICK_UP_FAIL_HAS_FOOD_ALREADY; }else if(currentCell.getFood() == 0){ @@ -107,18 +101,18 @@ public class AntWorld { }else if(currentCell.getFood() > 0){ // Ant successfully picks up food currentCell.setFood(currentCell.getFood() - 1); - myAnt.hasFood = true; + myAnt.setHasFood(true); reward = Reward.FOOD_DROP_DOWN_SUCCESS; } break; case DROP_DOWN: - if(!myAnt.hasFood){ + if(!myAnt.hasFood()){ // Ant had no food to drop reward = Reward.FOOD_DROP_DOWN_FAIL_NO_FOOD; }else{ // Drop food onto the ground currentCell.setFood(currentCell.getFood() + 1); - myAnt.hasFood = false; + myAnt.setHasFood(false); // negative reward if the agent drops food on any other field // than the starting point @@ -147,8 +141,8 @@ public class AntWorld { // valid movement if(!stayOnCell){ - myAnt.pos = potentialNextPos; - if(antAgent.getCell(myAnt.pos).getType() == CellType.UNKNOWN){ + myAnt.setPos(potentialNextPos); + if(antAgent.getCell(myAnt.getPos()).getType() == CellType.UNKNOWN){ // the ant will move to a cell that was previously unknown reward = Reward.UNKNOWN_FIELD_EXPLORED; }else{ @@ -157,7 +151,7 @@ public class AntWorld { } // 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 newState = antAgent.feedObservation(observation); @@ -183,7 +177,7 @@ public class AntWorld { public void reset() { RNG.reseed(); grid.initRandomWorld(); - myAnt = new MyAnt(); + myAnt = new Ant(new Point(-1,-1), false, false); } public void setMaxEpisodeLength(int maxTicks){ @@ -197,12 +191,12 @@ public class AntWorld { return grid.getGrid(); } - public MyAnt getMyAnt(){ + public Ant getAnt(){ return myAnt; } public static void main(String[] args) { RNG.setSeed(1993); - AntWorld a = new AntWorld(30, 30, 0.1); + AntWorld a = new AntWorld(10, 10, 0.1); System.out.println("ayay"); a.getGui().repaint(); for(int i = 0; i< 10; ++i){ diff --git a/src/main/java/evironment/antGame/Constants.java b/src/main/java/evironment/antGame/Constants.java index 416b647..10077ba 100644 --- a/src/main/java/evironment/antGame/Constants.java +++ b/src/main/java/evironment/antGame/Constants.java @@ -1,7 +1,7 @@ package evironment.antGame; public class Constants { - public static final int DEFAULT_GRID_WIDTH = 30; - public static final int DEFAULT_GRID_HEIGHT = 30; + public static final int DEFAULT_GRID_WIDTH = 10; + public static final int DEFAULT_GRID_HEIGHT = 10; public static final double DEFAULT_FOOD_DENSITY = 0.1; } diff --git a/src/main/java/evironment/antGame/gui/CellsScrollPane.java b/src/main/java/evironment/antGame/gui/CellsScrollPane.java index b704c38..ba96850 100644 --- a/src/main/java/evironment/antGame/gui/CellsScrollPane.java +++ b/src/main/java/evironment/antGame/gui/CellsScrollPane.java @@ -7,11 +7,14 @@ import java.awt.*; public class CellsScrollPane extends JScrollPane { private int cellSize; + private final int paneWidth = 500; + private final int paneHeight = 500; public CellsScrollPane(Cell[][] cells, int size){ super(); cellSize = size; - setPreferredSize(new Dimension(500, 500)); + setPreferredSize(new Dimension(paneWidth, paneHeight)); + cellSize = (paneWidth- cells.length) /cells.length; JPanel worldPanel = new JPanel(){ { setPreferredSize(new Dimension(cells.length * cellSize, cells[0].length * cellSize)); diff --git a/src/main/java/evironment/antGame/gui/HistoryPanel.java b/src/main/java/evironment/antGame/gui/HistoryPanel.java new file mode 100644 index 0000000..490191c --- /dev/null +++ b/src/main/java/evironment/antGame/gui/HistoryPanel.java @@ -0,0 +1,9 @@ +package evironment.antGame.gui; + +import javax.swing.*; + +public class HistoryPanel extends JPanel { + + public HistoryPanel(){ + } +} diff --git a/src/main/java/evironment/antGame/gui/MainFrame.java b/src/main/java/evironment/antGame/gui/MainFrame.java index d50a493..881d71d 100644 --- a/src/main/java/evironment/antGame/gui/MainFrame.java +++ b/src/main/java/evironment/antGame/gui/MainFrame.java @@ -10,7 +10,7 @@ public class MainFrame extends JFrame { public MainFrame(AntWorld antWorld, AntAgent antAgent){ setLayout(new BorderLayout()); - + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); CellsScrollPane worldPane = new CellsScrollPane(antWorld.getCellArray(), 10); CellsScrollPane antBrainPane = new CellsScrollPane(antAgent.getKnownWorld(), 10);