From 841614b92259070a5e096ccf31f8b9940a611b3f Mon Sep 17 00:00:00 2001 From: adash Date: Tue, 3 Mar 2009 23:37:46 +0000 Subject: [PATCH] bug fixes to prevent starvation of a lumberjack --- .../RainForest/dsm/AStarPathFinder.java | 28 ++++-- .../Distributed/RainForest/dsm/Barrier.java | 5 +- .../Distributed/RainForest/dsm/Player.java | 59 ++++++++---- .../RainForest/dsm/RainForest.java | 91 ++++++++----------- .../Distributed/RainForest/dsm/makefile | 2 +- 5 files changed, 105 insertions(+), 80 deletions(-) diff --git a/Robust/src/Benchmarks/Distributed/RainForest/dsm/AStarPathFinder.java b/Robust/src/Benchmarks/Distributed/RainForest/dsm/AStarPathFinder.java index 55f511d9..c0d7c9d9 100644 --- a/Robust/src/Benchmarks/Distributed/RainForest/dsm/AStarPathFinder.java +++ b/Robust/src/Benchmarks/Distributed/RainForest/dsm/AStarPathFinder.java @@ -68,9 +68,18 @@ public class AStarPathFinder { int sx = gamer.getX(); int sy = gamer.getY(); + int type = gamer.kind(); + // easy first check, if the destination is blocked, we can't get there - if(land[tx][ty].hasTree() || land[tx][ty].hasRock()) { - return null; + + if(type == 1) { //1 => PLANTER + if(land[tx][ty].hasTree() || land[tx][ty].hasRock()) { + return null; + } + } else { //LUMBERJACK + if((!land[tx][ty].hasTree()) || land[tx][ty].hasRock()) { + return null; + } } // initial state for A*. The closed group is empty. Only the starting @@ -123,7 +132,7 @@ public class AStarPathFinder { int xp = x + current.x; int yp = y + current.y; - if (isValidLocation(sx,sy,xp,yp)) { + if (isValidLocation(gamer,sx,sy,xp,yp)) { // the cost to get to this node is cost the current plus the movement // cost to reach this node. Note that the heursitic value is only used // in the sorted open list @@ -183,6 +192,7 @@ public class AStarPathFinder { /** ** Check if a given location is valid for the supplied gamer ** + ** @param gamer The Player moving in the map ** @param sx The starting x coordinate ** @param sy The starting y coordinate ** @param xp The x coordinate of the location to check @@ -191,12 +201,18 @@ public class AStarPathFinder { **/ - public boolean isValidLocation(int sx, int sy, int xp, int yp) { + public boolean isValidLocation(Player gamer, int sx, int sy, int xp, int yp) { boolean invalid = (xp <= 0) || (yp <= 0) || (xp >= rows-1) || (yp >= columns-1); if ((!invalid) && ((sx != xp) || (sy != yp))) { - if (land[xp][yp].hasTree() || land[xp][yp].hasRock()) { - invalid = true; + if(gamer.kind() == 1) { //1=> PLANTER + if (land[xp][yp].hasTree() || land[xp][yp].hasRock()) { + invalid = true; + } + } else { //LUMBERJACK + if (land[xp][yp].hasRock()) { + invalid = true; + } } } return !invalid; diff --git a/Robust/src/Benchmarks/Distributed/RainForest/dsm/Barrier.java b/Robust/src/Benchmarks/Distributed/RainForest/dsm/Barrier.java index cba383ce..927f8497 100644 --- a/Robust/src/Benchmarks/Distributed/RainForest/dsm/Barrier.java +++ b/Robust/src/Benchmarks/Distributed/RainForest/dsm/Barrier.java @@ -15,17 +15,20 @@ public class BarrierServer extends Thread { ** @param cols The number of columns in the map **/ public void updateAge(GameMap[][] land, int maxage, int rows, int cols) { + int countTrees = 0; for(int i = 0; i maxage) { land[i][j].tree = null; } else { - land[i][j].tree.incrementFiveYrs(); + land[i][j].tree.incrementage(); } + countTrees++; } } } + /* Debugging-> System.println("Tree count= "+countTrees); */ } public void run() { diff --git a/Robust/src/Benchmarks/Distributed/RainForest/dsm/Player.java b/Robust/src/Benchmarks/Distributed/RainForest/dsm/Player.java index aaf89be6..b7f83a97 100644 --- a/Robust/src/Benchmarks/Distributed/RainForest/dsm/Player.java +++ b/Robust/src/Benchmarks/Distributed/RainForest/dsm/Player.java @@ -1,6 +1,6 @@ /** ** An object representing the entity in the game that - ** is going to moving along the path. This allows us to pass around entity/state + ** is going to move along the path. This allows us to pass around entity/state ** information to determine whether a particular tile is blocked, or how much ** cost to apply on a particular tile. ** @@ -14,24 +14,25 @@ public class Player { private int type; private int x; private int y; - private int id; private int lowx, highx; private int lowy, highy; private int state; private int goalx, goaly; + private int rows, cols; + private Random rand; public Player(int type, int x, int y) { this.type = type; this.x = x; this.y = y; - id = -1; } - public Player(int type, int x, int y, int id, int rows, int cols, int bounds) { + public Player(int type, int x, int y, int rows, int cols, int bounds) { this.type = type; this.x = x; this.y = y; - this.id = id; + this.rows = rows; + this.cols = cols; lowx = x - bounds; highx = x + bounds; lowy = y - bounds; @@ -45,16 +46,39 @@ public class Player { highx = rows-2; if (highy >= cols) highy = cols-2; + rand = new Random(30); //seed to generate random numbers } - public void reset(int row, int col, int bounds) { - int seed = x + y; - Random rand = new Random(seed); - x = (rand.nextInt(Math.abs(row - 2) + 1)) + 1; - y = (rand.nextInt(Math.abs(col - 2) + 1)) + 1; - goalx = -1; - goaly = -1; - setBoundary(bounds, row, col); + public void reset(GameMap[][] land, int row, int col, int bounds) { + //Teleport to new location + if(type == 1) { //PLANTER + x = (rand.nextInt(Math.abs(row - 2) + 1)) + 1; + y = (rand.nextInt(Math.abs(col - 2) + 1)) + 1; + goalx = -1; + goaly = -1; + setBoundary(bounds, row, col); + } + + if(type == 0) { //LUMBERJACK + int trycount = 5; //try a few more times before teleporting + int i = 0; + while(i MOVING state + return; + } + i++; + } + x = (rand.nextInt(Math.abs(row - 2) + 1)) + 1; + y = (rand.nextInt(Math.abs(col - 2) + 1)) + 1; + goalx = -1; + goaly = -1; + setBoundary(bounds, row, col); + } } public void setBoundary(int bounds, int rows, int cols) { @@ -120,13 +144,13 @@ public class Player { **/ public int findGoal(GameMap[][] land) { /* Try setting the goal for try count times - * if not possible , then select a completely new goal + * if not possible, then select a completely new goal */ int trycount = (highx - lowx) + (highy - lowy); int i; + Random rand = new Random(0); for (i = 0; i < trycount; i++) { - Random rand = new Random(i); int row = (rand.nextInt(Math.abs(highx - lowx)) + 1) + lowx; int col = (rand.nextInt(Math.abs(highy - lowy)) + 1) + lowy; if (type == 1 && (land[row][col].hasTree() == false) && (land[row][col].hasRock() == false)) { @@ -140,9 +164,6 @@ public class Player { return 0; } } - if (i == trycount) { - /* System.println("Timeout trying ... \n") Only for Debugging */ - } return -1; } @@ -163,7 +184,7 @@ public class Player { ** Only for debugging **/ public debugPlayer() { - System.println("State= "+ state+ " Curr X= "+ x + " Curr Y= " + y + " Goal X= "+ goalx + " Goal Y= "+ goaly + " Type = " + type + "\n"); + System.println("State= "+ state+ " Curr X= "+ x + " Curr Y= " + y + " Goal X= "+ goalx + " Goal Y= "+ goaly + " Type = " + type); } /** diff --git a/Robust/src/Benchmarks/Distributed/RainForest/dsm/RainForest.java b/Robust/src/Benchmarks/Distributed/RainForest/dsm/RainForest.java index f5b0e1f6..e7d0cc97 100644 --- a/Robust/src/Benchmarks/Distributed/RainForest/dsm/RainForest.java +++ b/Robust/src/Benchmarks/Distributed/RainForest/dsm/RainForest.java @@ -1,11 +1,11 @@ -#define ROW 7 /* columns in the map */ -#define COLUMN 7 /* rows of in the map */ -#define ROUNDS 20 /* Number of moves by each player */ +#define ROW 100 /* columns in the map */ +#define COLUMN 100 /* rows of in the map */ +#define ROUNDS 256 /* Number of moves by each player */ #define PLAYERS 20 /* Number of Players when num Players != num of client machines */ #define RATI0 0.5 /* Number of lumberjacks to number of planters */ #define BLOCK 3 /* Area around the gamer to consider */ #define TREE_ZONE 0.4 /* Max percentage of trees in a zone */ -#define AGEUPDATETHRESHOLD 2 /* How frequently/how many rounds to increment age of tree */ +#define AGEUPDATETHRESHOLD 10 /* How frequently/how many rounds to increment age of tree */ #define MAXAGE 100 /* Max age of a tree */ @@ -21,11 +21,6 @@ public class RainForest extends Thread { **/ GameMap[][] land; - /** - * The gamer per thread: shared array where players do not see each other - **/ - Player gamer; - /** ** The shared BarrierServer object updated when trees increment age ** only updated by one thread running server @@ -47,29 +42,45 @@ public class RainForest extends Thread { } - public RainForest(GameMap[][] land, Player gamer, BarrierServer barrserver, int threadid, int numThreads) { + public RainForest(GameMap[][] land, BarrierServer barrserver, int threadid, int numThreads) { this.land = land; - this.gamer = gamer; this.threadid = threadid; this.barrserver = barrserver; this.numThreads = numThreads; } public void run() { - //Do N rounds - //do one move per round and synchronise + //Barrier for synchronizing moves Barrier barr; int id; atomic { id = threadid; } barr = new Barrier("128.195.136.162"); + + Random rand = new Random(id); + // Generate random numbers between 1 and row index/column index + int maxValue = ROW - 1; + int minValue = 1; + int row = (rand.nextInt(Math.abs(maxValue - minValue) + 1)) + minValue; + maxValue = COLUMN -1; + int col = (rand.nextInt(Math.abs(maxValue - minValue) + 1)) + minValue; + int person; + if((id&1) == 0) { //same as id%2 + person = LUMBERJACK; + } else { + person = PLANTER; + } + Player gamer = new Player(person, row, col, ROW, COLUMN, BLOCK); + + //Do N rounds + //do one move per round and synchronise for(int i = 0; i gamer.debugPlayer(); */ return; } } diff --git a/Robust/src/Benchmarks/Distributed/RainForest/dsm/makefile b/Robust/src/Benchmarks/Distributed/RainForest/dsm/makefile index 6b6c6995..af0c5ad4 100644 --- a/Robust/src/Benchmarks/Distributed/RainForest/dsm/makefile +++ b/Robust/src/Benchmarks/Distributed/RainForest/dsm/makefile @@ -10,7 +10,7 @@ SRC=tmp${MAINCLASS}.java \ Node.java \ AStarPathFinder.java -FLAGS1=-dsm -nooptimize -debug -mainclass ${MAINCLASS} +FLAGS1=-dsm -optimize -transstats -mainclass ${MAINCLASS} FLAGS2=-dsm -dsmcaching -optimize -mainclass ${MAINCLASS} FLAGS3=-dsm -dsmcaching -prefetch -optimize -mainclass ${MAINCLASS} -trueprob 0.90 FLAGS4=-dsm -dsmcaching -rangeprefetch -optimize -mainclass ${MAINCLASS} -trueprob 0.90 -- 2.34.1