From 5657baac66269cf6b918d391ef11d082a7216757 Mon Sep 17 00:00:00 2001 From: Balazs Toldi Date: Thu, 26 May 2022 11:14:05 +0200 Subject: [PATCH] Better search drone logic --- .../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 airSeaRescue.mas2j | 4 +- searchDrone.asl | 180 +----------------- src/java/common/Constants.java | 8 + src/java/operator/WorldEnvironment.java | 38 ++-- src/java/searchDrone/get_direction.java | 20 +- src/java/victim/get_direction.java | 9 +- victim.asl | 12 +- 11 files changed, 58 insertions(+), 213 deletions(-) create mode 100644 src/java/common/Constants.java diff --git a/.gradle/7.4/executionHistory/executionHistory.lock b/.gradle/7.4/executionHistory/executionHistory.lock index 1083b4841d0f192fb8bf2e45a9905f94da73d561..cd2ee3afc8db12c902f33a42298d65da10ccf6a1 100644 GIT binary patch literal 17 UcmZP$b6n1St7Sqb0|Yb!04@9kM*si- literal 17 UcmZP$b6n1St7Sqb0|Zn904?+cKL7v# diff --git a/.gradle/7.4/fileHashes/fileHashes.lock b/.gradle/7.4/fileHashes/fileHashes.lock index 58760a565c2edbf71bdb976351b1d07647c9e90f..276e1c8b0cf2a23cde945ad312fd00b150941266 100644 GIT binary patch literal 17 VcmZSH^Jdus|6C?j1~6be1pqai1fT!_ literal 17 VcmZSH^Jdus|6C?j1~6b;2>>;F1b+Yk diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index e229a4709f471ac84cb9a44fc6cdff6a5ca4639e..a3097c452ea493e8bc2a4d1d6507dcd37ea0487e 100644 GIT binary patch literal 17 TcmZQBDm 0; - .print("Uncommitted gold distances: ",LD,LLD); - .min(LD,d(_,NewG)); - .print("Next gold is ",NewG); - !!handle(NewG). --!choose_gold <- -+free. - -+!calc_gold_distance([],[]). -+!calc_gold_distance([gold(GX,GY)|R],[d(D,gold(GX,GY))|RD]) - : pos(IX,IY) & not committed_to(gold(GX,GY)) - <- operator.dist(IX,IY,GX,GY,D); - !calc_gold_distance(R,RD). -+!calc_gold_distance([_|R],RD) - <- !calc_gold_distance(R,RD). - - -// BCG! -// !pos is used when it is algways possible to go -// so this plans should not be used: +!pos(X,Y) : last_dir(skip) <- -// .print("It is not possible to go to ",X,"x",Y). -// in the future -//+last_dir(skip) <- .drop_goal(pos) -+!pos(X,Y) : pos(X,Y) <- .print("I've reached ",X,"x",Y). -+!pos(X,Y) : not pos(X,Y) - <- !next_step(X,Y); - !pos(X,Y). - - -+!ensure(pick,_) : pos(X,Y) & cell(X,Y,gold) - <- do(pick); ?carrying_gold. -// fail if no gold there or not carrying_gold after pick! -// handle(G) will "catch" this failure. - -+!ensure(drop, _) : pos(X,Y) & depot(_,X,Y) - <- do(drop). //TODO: not ?carrying_gold. +/* Finding victims */ ++cell(X,Y,victim,V): true + <- .print("Found vimctim",V); + .send(V,tell,stayHere(X,Y)). + diff --git a/src/java/common/Constants.java b/src/java/common/Constants.java new file mode 100644 index 0000000..354db88 --- /dev/null +++ b/src/java/common/Constants.java @@ -0,0 +1,8 @@ +package common; + +public class Constants { + public static int victimCount = 4; + public static int searchDroneCount = 2; + + private Constants(){} +} diff --git a/src/java/operator/WorldEnvironment.java b/src/java/operator/WorldEnvironment.java index b51c8ca..246a9d0 100644 --- a/src/java/operator/WorldEnvironment.java +++ b/src/java/operator/WorldEnvironment.java @@ -1,5 +1,6 @@ package operator; +import common.Constants; import jason.asSyntax.*; import jason.environment.Environment; import jason.environment.grid.Location; @@ -52,20 +53,12 @@ public class WorldEnvironment extends Environment { } private void updateAgetPos(int i) { - String prefix = ""; - if (i < 4) - prefix = "victim"; - else if (i < 6) { - prefix = "searchDrone"; - } - int offset = 0; - if (i > 3 && i < 6) { - offset = 4; - } - clearPercepts(prefix + (i + 1)); + String agentName = getNameFromId(i); + System.out.println(agentName); + clearPercepts(agentName); Location l = model.getAgPos(i); - addPercept(prefix + (i + 1 - offset), Literal.parseLiteral("pos(" + l.x + "," + l.y + ")")); + addPercept(agentName, Literal.parseLiteral("pos(" + l.x + "," + l.y + ")")); } private void updateAgPercept(int ag) { @@ -76,12 +69,13 @@ public class WorldEnvironment extends Environment { clearPercepts(agName); // its location Location l = model.getAgPos(ag); + addPercept(agName, Literal.parseLiteral("pos(" + l.x + "," + l.y + ")")); updateAgPercept(agName, l.x - 1, l.y - 1); updateAgPercept(agName, l.x - 1, l.y); updateAgPercept(agName, l.x - 1, l.y + 1); updateAgPercept(agName, l.x, l.y - 1); - updateAgPercept(agName, l.x, l.y); + //updateAgPercept(agName, l.x, l.y); updateAgPercept(agName, l.x, l.y + 1); updateAgPercept(agName, l.x + 1, l.y - 1); updateAgPercept(agName, l.x + 1, l.y); @@ -100,7 +94,15 @@ public class WorldEnvironment extends Environment { addPercept(agName, Literal.parseLiteral("cell(" + x + "," + y + ",enemy)")); } if (model.hasObject(WorldModel.AGENT, x, y)) { - addPercept(agName, Literal.parseLiteral("cell(" + x + "," + y + ",ally)")); + if(agName.startsWith("searchDrone")) { + for (int i = 0; i < Constants.victimCount; i++) { + Location victimLocation = model.getAgPos(i); + if(victimLocation.x == x && victimLocation.y == y){ + addPercept(agName, Literal.parseLiteral("cell(" + x + "," + y + ",victim,victim"+(i+1)+")")); + } + } + addPercept(agName, Literal.parseLiteral("cell(" + x + "," + y + ",ally)")); + } } } } @@ -159,16 +161,16 @@ public class WorldEnvironment extends Environment { return i; } if (agName.startsWith("searchDrone")) { - return i + 4; + return i + Constants.victimCount; } return -1; } public String getNameFromId(int i) { - if(i < 4) { + if(i < Constants.victimCount) { return "victim"+(i+1); - } else if (i < 6) { - return "searchDrone"+(i+5); + } else if (i < Constants.victimCount+Constants.searchDroneCount) { + return "searchDrone"+(i-(Constants.victimCount-1)); } return ""; } diff --git a/src/java/searchDrone/get_direction.java b/src/java/searchDrone/get_direction.java index 12eae8e..511b912 100644 --- a/src/java/searchDrone/get_direction.java +++ b/src/java/searchDrone/get_direction.java @@ -27,25 +27,23 @@ public class get_direction extends DefaultInternalAction { if (model.inGrid(to_x,to_y)) { Location agnet_location = new Location(agent_x, agent_y); - while (!model.isFreeOfObstacle(to_x,to_y) && to_x > 0) to_x--; - while (!model.isFreeOfObstacle(to_x,to_y) && to_x < model.getWidth()) to_x++; - while (!model.isFreeOfObstacle(to_x,to_y) && to_y > 0) to_y--; - while (!model.isFreeOfObstacle(to_x,to_y) && to_y < model.getHeight()) to_y++; + while (!model.isFree(to_x,to_y) && to_x > 0) to_x--; + while (!model.isFree(to_x,to_y) && to_x < model.getWidth()) to_x++; + while (!model.isFree(to_x,to_y) && to_y > 0) to_y--; + while (!model.isFree(to_x,to_y) && to_y < model.getHeight()) to_y++; - System.out.println("To: "+to_x+", "+to_y+"\t"+agent_x+", "+agent_y); - if(to_x < agent_x && model.isFreeOfObstacle(agent_x-1,agent_y)){ + if(to_x < agent_x && model.isFree(agent_x-1,agent_y)){ sAction = "left"; - }else if (to_x > agent_x && model.isFreeOfObstacle(agent_x+1,agent_y)){ + }else if (to_x > agent_x && model.isFree(agent_x+1,agent_y)){ sAction = "right"; } - if (to_y < agent_y && model.isFreeOfObstacle(agent_x,agent_y-1)){ - sAction = "up"; - } else if (to_y > agent_y && model.isFreeOfObstacle(agent_x,agent_y+1)) { + if (to_y > agent_y && model.isFree(agent_x,agent_y+1)) { sAction = "down"; + } else if (to_y < agent_y && model.isFree(agent_x,agent_y-1)){ + sAction = "up"; } } - System.out.println(sAction); return un.unifies(terms[4], new Atom(sAction)); } catch (Throwable e) { e.printStackTrace(); diff --git a/src/java/victim/get_direction.java b/src/java/victim/get_direction.java index 0342d10..1dfc1f1 100644 --- a/src/java/victim/get_direction.java +++ b/src/java/victim/get_direction.java @@ -16,23 +16,24 @@ import java.util.Random; public class get_direction extends DefaultInternalAction { Random r = new Random(); - + static int skip = 0; @Override public Object execute(TransitionSystem ts, Unifier un, Term[] terms) throws Exception { try { String sAction = "skip"; - + if (skip++ % 5 == 0) + return un.unifies(terms[2], new Atom(sAction)); WorldModel model = WorldModel.get(); int AgX = (int)((NumberTerm)terms[0]).solve(); int AgY = (int)((NumberTerm)terms[1]).solve(); List actions = new ArrayList<>(); - if(AgX < 34) + if(AgX < model.getWidth()-1) actions.add("right"); if(AgX > 0) actions.add("left"); - if(AgY < 34) + if(AgY < model.getHeight()-1) actions.add("down"); if(AgY >= 0) actions.add("up"); diff --git a/victim.asl b/victim.asl index 8c9bae8..efbd3b8 100644 --- a/victim.asl +++ b/victim.asl @@ -2,7 +2,7 @@ /* Initial beliefs and rules */ free. -life(500). +life(10). /* Initial goals */ +gsize(_,_) : true @@ -13,10 +13,14 @@ life(500). <- .wait("+pos(X,Y)", 500); // wait for it and try again !!send_init_pos(S). -+free : pos(AgX,AgY) & life(L) > 0 ++free : pos(AgX,AgY) & life(L) > 0 & not saved <- victim.get_direction(AgX, AgY, D); do(D); - -+life(L-1); - .wait("",500); + //-+life(L-1); -+free. + ++stayHere(X,Y): free + <- .print("I will be saved!"); + +saved; + -free.