From 694936bb11df5dd92f121d2a27c07f3ba7cc0d8a Mon Sep 17 00:00:00 2001 From: Balazs Toldi Date: Thu, 26 May 2022 18:49:49 +0200 Subject: [PATCH] Rescue drones --- .../executionHistory/executionHistory.lock | Bin 17 -> 17 bytes .gradle/7.4/fileHashes/fileHashes.lock | Bin 17 -> 17 bytes .../buildOutputCleanup.lock | Bin 17 -> 17 bytes .gradle/file-system.probe | Bin 8 -> 8 bytes miner.asl | 4 + operator.asl | 26 +-- rescueDrone.asl | 181 ++++++++++++++++++ searchDrone.asl | 6 +- src/java/common/Constants.java | 4 +- src/java/operator/WorldEnvironment.java | 22 ++- src/java/operator/WorldModel.java | 83 +++++++- src/java/operator/WorldView.java | 19 +- 12 files changed, 313 insertions(+), 32 deletions(-) create mode 100644 rescueDrone.asl diff --git a/.gradle/7.4/executionHistory/executionHistory.lock b/.gradle/7.4/executionHistory/executionHistory.lock index cd2ee3afc8db12c902f33a42298d65da10ccf6a1..d8d6ea170ac2b0a2026fcad8ce63f5f8bb1b589f 100644 GIT binary patch literal 17 UcmZP$b6n1St7Sqb0|ayf04@XsPXGV_ literal 17 UcmZP$b6n1St7Sqb0|Yb!04@9kM*si- diff --git a/.gradle/7.4/fileHashes/fileHashes.lock b/.gradle/7.4/fileHashes/fileHashes.lock index 276e1c8b0cf2a23cde945ad312fd00b150941266..1919788923f53dadad6d75e5626ab12d0108a015 100644 GIT binary patch literal 17 VcmZSH^Jdus|6C?j1~6cJ4gfW|1ib(N literal 17 VcmZSH^Jdus|6C?j1~6be1pqai1fT!_ diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index a3097c452ea493e8bc2a4d1d6507dcd37ea0487e..0310f448a89a4fe2907797fd823e280ab0079993 100644 GIT binary patch literal 17 TcmZQBDm 0; + .print("Uncommitted victim distances: ",LD,LLD); + .min(LD,d(_,NewG)); + .print("Next victim is ",NewG); + !!handle(NewG). +-!choose_victim <- -+free. + ++!calc_victim_distance([],[]). ++!calc_victim_distance([victim(GX,GY)|R],[d(D,victim(GX,GY))|RD]) + : pos(IX,IY) & not committed_to(victim(GX,GY)) + <- operator.dist(IX,IY,GX,GY,D); + !calc_victim_distance(R,RD). ++!calc_victim_distance([_|R],RD) + <- !calc_victim_distance(R,RD). + ++!pos(X,Y) : pos(X,Y) <- .print("I've reached ",X,"x",Y). ++!pos(X,Y) : pos(X-1,Y) & not carrying_victim | pos(X,Y-1) & not carrying_victim | pos(X+1,Y) & not carrying_victim | pos(X,Y+1) & not carrying_victim + <- .print("I've kinda reached ",X,"x",Y). ++!pos(X,Y) : not pos(X,Y) + <- !next_step(X,Y); + !pos(X,Y). + + ++!ensure(pick,_) : cell(X,Y,victim,V) + <- .print("Picking up victim"); + do(pick); ?carrying_victim. +// fail if no victim there or not carrying_victim after pick! +// handle(G) will "catch" this failure. + ++!ensure(drop) : pos(X,Y) & depot(X,Y) + <- do(drop). //TODO: not ?carrying_victim. + + ++!next_step(X,Y) + : pos(AgX,AgY) + <- searchDrone.get_direction(AgX, AgY, X, Y, D); + //.print("from ",AgX,"x",AgY," to ", X,"x",Y," -> ",D); + -+last_dir(D); + do(D). ++!next_step(X,Y) : not pos(_,_) // I still do not know my position + <- !next_step(X,Y). +-!next_step(X,Y) : true // failure handling -> start again! + <- .print("Failed next_step to ", X,"x",Y," fixing and trying again!"); + -+last_dir(null); + !next_step(X,Y). +// someone else picked up the victim I am going to go, +// so drops the intention and chose another victim +@ppgd[atomic] ++picked(G)[source(A)] + : .desire(handle(G)) | .desire(init_handle(G)) + <- .print(A," has taken ",G," that I am pursuing! Dropping my intention."); + .abolish(G); + .drop_desire(handle(G)); + !!choose_victim. + +// someone else picked up a victim I know about, +// remove from my belief base ++picked(victim(X,Y)) + <- -victim(X,Y)[source(_)]. + +// I perceived unknown victim and I am free, handle it +@pcell[atomic] // atomic: so as not to handle another + // event until handle victim is initialised ++cell(X,Y,victim,V) + : not carrying_victim & free + <- -free; + +victim(X,Y); + .print("Victim perceived: ",victim(X,Y)); + !init_handle(victim(X,Y)). + ++cell(X,Y,victim,V) + : not victim(X,Y) + <- +victim(X,Y); + .print("Announcing ",victim(X,Y)," to others"); + .broadcast(tell,victim(X,Y)); + .send(V,tell,stayHere(X,Y)). ++free : pos(AgX,AgY) + <- + victim.get_direction(AgX, AgY, D); + do(D). + diff --git a/searchDrone.asl b/searchDrone.asl index 445aaa1..f18eef8 100644 --- a/searchDrone.asl +++ b/searchDrone.asl @@ -101,6 +101,8 @@ calc_new_y(AgY,_,Y) :- Y = AgY+2. /* Finding victims */ +cell(X,Y,victim,V): true - <- .print("Found vimctim",V); - .send(V,tell,stayHere(X,Y)). + <- .print("Found vimctim",V); + .print("Announcing ",victim(X,Y)," to others"); + .broadcast(tell,victim(X,Y)); + .send(V,tell,stayHere(X,Y)). diff --git a/src/java/common/Constants.java b/src/java/common/Constants.java index 354db88..d8e1d1d 100644 --- a/src/java/common/Constants.java +++ b/src/java/common/Constants.java @@ -3,6 +3,8 @@ package common; public class Constants { public static int victimCount = 4; public static int searchDroneCount = 2; + public static int rescueDroneCount = 3; - private Constants(){} + private Constants() { + } } diff --git a/src/java/operator/WorldEnvironment.java b/src/java/operator/WorldEnvironment.java index 246a9d0..038fb30 100644 --- a/src/java/operator/WorldEnvironment.java +++ b/src/java/operator/WorldEnvironment.java @@ -42,6 +42,7 @@ public class WorldEnvironment extends Environment { model = WorldModel.senario1(); clearPercepts(); addPercept(Literal.parseLiteral("gsize(" + model.getWidth() + "," + model.getHeight() + ")")); + addPercept(Literal.parseLiteral("depot(" + (model.getWidth()-1) + "," + (model.getHeight()-1) + ")")); updateAgents(); informAgsEnvironmentChanged(); } @@ -69,7 +70,9 @@ public class WorldEnvironment extends Environment { clearPercepts(agName); // its location Location l = model.getAgPos(ag); - + if (model.agWithVictim.contains(ag)) { + addPercept(agName, Literal.parseLiteral("carrying_victim")); + } addPercept(agName, Literal.parseLiteral("pos(" + l.x + "," + l.y + ")")); updateAgPercept(agName, l.x - 1, l.y - 1); updateAgPercept(agName, l.x - 1, l.y); @@ -94,7 +97,7 @@ public class WorldEnvironment extends Environment { addPercept(agName, Literal.parseLiteral("cell(" + x + "," + y + ",enemy)")); } if (model.hasObject(WorldModel.AGENT, x, y)) { - if(agName.startsWith("searchDrone")) { + if(agName.startsWith("searchDrone") || agName.startsWith("rescueDrone")) { for (int i = 0; i < Constants.victimCount; i++) { Location victimLocation = model.getAgPos(i); if(victimLocation.x == x && victimLocation.y == y){ @@ -122,9 +125,10 @@ public class WorldEnvironment extends Environment { try { if (sleep > 0) { Thread.sleep(sleep); + }else { + Thread.sleep(10); } int agId = getAgIdBasedOnName(ag); - if (action.equals(up)) { result = model.move(Move.UP, agId); } else if (action.equals(down)) { @@ -135,7 +139,11 @@ public class WorldEnvironment extends Environment { result = model.move(Move.LEFT, agId); } else if (action.equals(skip)) { result = true; - } else if (action.getFunctor().equals("decLife")) { + }else if (action.equals(pick)) { + result = model.pick(agId); + } else if (action.equals(drop)) { + result = model.drop(agId); + }else if (action.getFunctor().equals("decLife")) { int life = (int) ((NumberTerm) action.getTerm(0)).solve(); System.out.println(life); result = true; @@ -146,6 +154,7 @@ public class WorldEnvironment extends Environment { } if (result) { updateAgPercept(agId); + view.updateRescuedVictims(); return true; } } catch (Exception e) { @@ -163,6 +172,9 @@ public class WorldEnvironment extends Environment { if (agName.startsWith("searchDrone")) { return i + Constants.victimCount; } + if (agName.startsWith("rescueDrone")) { + return i + Constants.victimCount+Constants.searchDroneCount; + } return -1; } @@ -171,6 +183,8 @@ public class WorldEnvironment extends Environment { return "victim"+(i+1); } else if (i < Constants.victimCount+Constants.searchDroneCount) { return "searchDrone"+(i-(Constants.victimCount-1)); + } else if (i < Constants.victimCount+Constants.searchDroneCount+Constants.rescueDroneCount) { + return "rescueDrone"+(i-(Constants.victimCount+Constants.searchDroneCount-1)); } return ""; } diff --git a/src/java/operator/WorldModel.java b/src/java/operator/WorldModel.java index a869cec..4a3f99b 100644 --- a/src/java/operator/WorldModel.java +++ b/src/java/operator/WorldModel.java @@ -1,9 +1,12 @@ package operator; +import common.Constants; import jason.environment.grid.GridWorldModel; import jason.environment.grid.Location; +import java.util.HashSet; import java.util.Random; +import java.util.Set; public class WorldModel extends GridWorldModel { @@ -11,8 +14,9 @@ public class WorldModel extends GridWorldModel { public static final int VICTIM = 16; public static final int DEPOT = 32; public static final int ENEMY = 64; - + public int rescuedVictims = 0; protected static WorldModel model = null; + Set agWithVictim = new HashSet(); public WorldModel(int w, int h, int nbAgs) { super(w, h, nbAgs); @@ -20,19 +24,24 @@ public class WorldModel extends GridWorldModel { static WorldModel senario1() { - WorldModel result = new WorldModel(35, 35, 6); + WorldModel result = new WorldModel(35, 35, Constants.victimCount + Constants.searchDroneCount + Constants.rescueDroneCount); Random random1 = new Random(); for (int i = 0; i <= 4; i++) { - result.add(WorldModel.OBSTACLE, random1.nextInt(34), random1.nextInt(34)); + //result.add(WorldModel.OBSTACLE, random1.nextInt(34), random1.nextInt(34)); } + // Victim agents result.setAgPos(0, 1, 0); result.setAgPos(1, 20, 0); result.setAgPos(2, 3, 20); result.setAgPos(3, 20, 20); - System.out.println(result.getNbOfAgs()); - result.setAgPos(4,33,34); - result.setAgPos(5,0,34); - result.add(DEPOT,34,34); + // Search Drone agents + result.setAgPos(4, 33, 34); + result.setAgPos(5, 0, 34); + // Rescue Drone agents + result.setAgPos(6, 18, 33); + result.setAgPos(7, 19, 33); + result.setAgPos(8, 17, 33); + result.add(DEPOT, 34, 34); model = result; return result; } @@ -64,6 +73,62 @@ public class WorldModel extends GridWorldModel { return true; } + private int getAgentIdByLocation(Location l) { + for (int i = 0; i < getNbOfAgs(); i++) { + if (agPos[i].x == l.x && agPos[i].y == l.y) return i; + } + return -1; + } + + public boolean pick(int ag) { + + boolean result = false; + Location l = getAgPos(ag); + result = pickLocation(ag, new Location(l.x - 1, l.y - 1)); + if (!result) + result = pickLocation(ag, new Location(l.x - 1, l.y)); + if (!result) + result = pickLocation(ag, new Location(l.x - 1, l.y + 1)); + if (!result) + result = pickLocation(ag, new Location(l.x, l.y - 1)); + if (!result) + result = pickLocation(ag, new Location(l.x, l.y + 1)); + if (!result) + result = pickLocation(ag, new Location(l.x + 1, l.y - 1)); + if (!result) + result = pickLocation(ag, new Location(l.x + 1, l.y)); + if (!result) + result = pickLocation(ag, new Location(l.x + 1, l.y + 1)); + if(!result) + System.out.println("Na itt baj van...."); + return result; + } + + public boolean pickLocation(int ag, Location l) { + if (hasObject(WorldModel.AGENT, l.x, l.y)) { + if (!agWithVictim.contains(ag) && getAgentIdByLocation(l) < Constants.victimCount) { + remove(WorldModel.AGENT, l.x, l.y); + agWithVictim.add(ag); + return true; + } + } + return false; + } + + public boolean drop(int ag) { + Location l = getAgPos(ag); + if (hasObject(WorldModel.DEPOT, l.x, l.y) + || hasObject(WorldModel.DEPOT, l.x - 1, l.y) + || hasObject(WorldModel.DEPOT, l.x, l.y - 1) + || hasObject(WorldModel.DEPOT, l.x + 1, l.y) + || hasObject(WorldModel.DEPOT, l.x, l.y + 1)) { + agWithVictim.remove(ag); + rescuedVictims++; + return true; + } + return false; + } + @Override public Location getAgPos(int ag) { return super.getAgPos(ag); @@ -86,4 +151,8 @@ public class WorldModel extends GridWorldModel { public static void destroy() { model = null; } + + public int getRescuedVictims() { + return rescuedVictims; + } } diff --git a/src/java/operator/WorldView.java b/src/java/operator/WorldView.java index fc392f4..d66119b 100644 --- a/src/java/operator/WorldView.java +++ b/src/java/operator/WorldView.java @@ -1,5 +1,6 @@ package operator; +import common.Constants; import jason.environment.grid.GridWorldModel; import jason.environment.grid.GridWorldView; @@ -125,11 +126,12 @@ public class WorldView extends GridWorldView { } } }); + } - public void udpateCollectedGolds() { + public void updateRescuedVictims() { WorldModel wm = (WorldModel) model; - //jGoldsC.setText(wm.getGoldsInDepot() + "/" + wm.getInitialNbGolds()); + jGoldsC.setText(String.valueOf(wm.getRescuedVictims())); } @Override @@ -144,22 +146,29 @@ public class WorldView extends GridWorldView { case WorldModel.ENEMY: drawEnemy(g, x, y); break; + default: + drawEmpty(g,x,y); } } @Override public void drawAgent(Graphics g, int x, int y, Color c, int id) { Color idColor = Color.black; - if(id <4){ + int idOffset = 0; + if(id < Constants.victimCount){ idColor = Color.RED; - } else if (id < 6) { + } else if (id < Constants.victimCount+Constants.searchDroneCount) { idColor = Color.MAGENTA; + idOffset = Constants.victimCount; + } else if (id < Constants.victimCount+Constants.searchDroneCount+Constants.rescueDroneCount){ + idColor = Color.ORANGE; + idOffset = Constants.victimCount+Constants.searchDroneCount; } super.drawAgent(g, x, y, idColor, -1); idColor = Color.white; //} g.setColor(idColor); - drawString(g, x, y, defaultFont, String.valueOf(id + 1)); + drawString(g, x, y, defaultFont, String.valueOf(id + 1-idOffset)); } public void drawDepot(Graphics g, int x, int y) {