From 8c3c2b659e40c44040b03b2b436822c859a57777 Mon Sep 17 00:00:00 2001 From: k20066 <87357429+k20066@users.noreply.github.com> Date: Fri, 3 Nov 2023 01:13:06 +0900 Subject: [PATCH 1/9] add hungarian module --- .DS_Store | Bin 6148 -> 8196 bytes .gitignore | 2 + clean.sh | 3 + compile.sh | 9 + config/module.cfg | 32 +- docs/.DS_Store | Bin 6148 -> 6148 bytes precompute.sh | 35 ++ ...> CentralizedControlCommandExecutorA.java} | 5 +- .../CentralizedControlCommandExecutorF.java | 330 ++++++++++++++++ .../CentralizedControlCommandPickerA.java | 80 ++++ .../ComplimentTacticsExtActionFireRescue.java | 239 +++++++++++ .../CentralizedControlMessageCoordinator.java | 215 ++++++++++ .../module/comm/DefaultChannelSubscriber.java | 100 +++++ .../CentralizedControlATAllocator.java | 371 ++++++++++++++++++ .../CentralizedControlATHumanDetector.java | 255 ++++++++++++ .../CentralizedControlFBAllocator.java | 273 +++++++++++++ .../CentralizedControlFBHumanDetector.java | 241 ++++++++++++ .../SampleAmbulanceTargetAllocator.java | 53 --- start.sh | 35 ++ submit.sh | 10 + 20 files changed, 2219 insertions(+), 69 deletions(-) create mode 100755 clean.sh create mode 100755 compile.sh create mode 100755 precompute.sh rename src/main/java/autumn_2023/centralized/{HungarianCommandExecutorAmbulance.java => CentralizedControlCommandExecutorA.java} (97%) create mode 100644 src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorF.java create mode 100644 src/main/java/autumn_2023/centralized/CentralizedControlCommandPickerA.java create mode 100644 src/main/java/autumn_2023/extaction/ComplimentTacticsExtActionFireRescue.java create mode 100644 src/main/java/autumn_2023/module/comm/CentralizedControlMessageCoordinator.java create mode 100644 src/main/java/autumn_2023/module/comm/DefaultChannelSubscriber.java create mode 100755 src/main/java/autumn_2023/module/complex/CentralizedControlATAllocator.java create mode 100755 src/main/java/autumn_2023/module/complex/CentralizedControlATHumanDetector.java create mode 100755 src/main/java/autumn_2023/module/complex/CentralizedControlFBAllocator.java create mode 100755 src/main/java/autumn_2023/module/complex/CentralizedControlFBHumanDetector.java delete mode 100755 src/main/java/autumn_2023/module/complex/SampleAmbulanceTargetAllocator.java create mode 100755 start.sh create mode 100755 submit.sh diff --git a/.DS_Store b/.DS_Store index 9c7a65b5c6b718343b6028f838bf4f25b066ebb6..dc44f3e6614782a35458620bbff21591bcf9b06b 100644 GIT binary patch literal 8196 zcmeHMziSjh6n>N4yIfE)jd+b@{{WE-Vq@bPE|rDgSy(7~KjJCgWzS14(MGl~LM-fr z)QZ*?Ru*DJEyT)NL=+MK2ZMg|<8J5O?gp`tLuX*;E%V;Y_susmJ3G5XBo+p(d7?5A zm2gQry9ZfDl74xm(v&HwzzWo-PJ6!AjJmy4q$_X=I0c*nP64NYQ(#gQz@9BrDQDl; zUY9!soC5!)0`mC~;F2~pHr7@>I*_R&0IZKL?FPTxe{pt*%bO96p3W7UqN^6gt`uR5*!HTbDZpoC0YDyzBy+ST8?VtpH!Q=bT?6>gvE)S?b8F?9dzd9*$jVRM*i z#kbxK6o*t)!X!V6@hQ9jm_l2l6>3rhMw}1Pv2=vZ=hWl(7q(VMe5`i5QNkxbit*78 zL5~_3m1U-j_h5|BbOsEV-{t_{{iknU$@w@IlPHiE!+5A|4(p+X z8PKE^#-VaxKiU}iZ2X-&b35Bwd&{3-XZaa!MXiS-R$=yPpzr5Y-z>k4&-%t<5)OGW zjECM_eawIeTzLPzug_HfjnBs4+AMy4c+)%&M*ajl%g=EA5KYrw>Y@KjY8G^He-YzQ zr&X%6aiD!Sc8j%n{kVI0@wDcZ+qOMZe?-K%OhSo7YeY%E~BGq0W8!7T`XGi7m$Y?KzQ${4?XI|bUNtY&G0Z%h5%k%$Y z{`dch?}sDk6mSasxdOtk)~XAruebknL-MR0;JSoMCK(rNtEwQAA+lT@$6-f53~?VA pUubNsEj&nn{X;-R?A^lN>D>N5e4nsY!5|IaSls@v=Ipovc;JhiW!O+l3^Mla`CE@(?o-r7&dH7WMNy( w&cPwb43QAv2GXt|+czeDXP(S2W2pd=0NT$4rCC5UkOQ(Ew0ZLOLRsaA1 diff --git a/.gitignore b/.gitignore index 5b46933..3531263 100755 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,5 @@ bin # Project precomp_data test-results + +.DS_Store \ No newline at end of file diff --git a/clean.sh b/clean.sh new file mode 100755 index 0000000..f4e580b --- /dev/null +++ b/clean.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +./gradlew clean diff --git a/compile.sh b/compile.sh new file mode 100755 index 0000000..b6a0156 --- /dev/null +++ b/compile.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +cd `dirname $0` + +rm -rf build +mkdir build + +chmod +x gradlew +./gradlew build diff --git a/config/module.cfg b/config/module.cfg index af44b75..fae5e10 100755 --- a/config/module.cfg +++ b/config/module.cfg @@ -1,17 +1,18 @@ ## DefaultTacticsAmbulanceTeam -DefaultTacticsAmbulanceTeam.HumanDetector : sample_team.module.complex.SampleHumanDetector +DefaultTacticsAmbulanceTeam.HumanDetector : autumn_2023.module.complex.CentralizedControlATHumanDetector DefaultTacticsAmbulanceTeam.Search : sample_team.module.complex.SampleSearch DefaultTacticsAmbulanceTeam.ExtActionTransport : adf.impl.extaction.DefaultExtActionTransport DefaultTacticsAmbulanceTeam.ExtActionMove : adf.impl.extaction.DefaultExtActionMove -DefaultTacticsAmbulanceTeam.CommandExecutorAmbulance : adf.impl.centralized.DefaultCommandExecutorAmbulance +#DefaultTacticsAmbulanceTeam.CommandExecutorAmbulance : adf.impl.centralized.DefaultCommandExecutorAmbulance +DefaultTacticsAmbulanceTeam.CommandExecutorAmbulance : autumn_2023.centralized.CentralizedControlCommandExecutorA DefaultTacticsAmbulanceTeam.CommandExecutorScout : adf.impl.centralized.DefaultCommandExecutorScout ## DefaultTacticsFireBrigade -DefaultTacticsFireBrigade.HumanDetector : sample_team.module.complex.SampleHumanDetector +DefaultTacticsFireBrigade.HumanDetector : autumn_2023.module.complex.CentralizedControlFBHumanDetector DefaultTacticsFireBrigade.Search : sample_team.module.complex.SampleSearch -DefaultTacticsFireBrigade.ExtActionFireRescue : adf.impl.extaction.DefaultExtActionFireRescue +DefaultTacticsFireBrigade.ExtActionFireRescue : autumn_2023.extaction.ComplimentTacticsExtActionFireRescue DefaultTacticsFireBrigade.ExtActionMove : adf.impl.extaction.DefaultExtActionMove -DefaultTacticsFireBrigade.CommandExecutorFire : adf.impl.centralized.DefaultCommandExecutorFire +DefaultTacticsFireBrigade.CommandExecutorFire : autumn_2023.centralized.CentralizedControlCommandExecutorF DefaultTacticsFireBrigade.CommandExecutorScout : adf.impl.centralized.DefaultCommandExecutorScout ## DefaultTacticsPoliceForce @@ -23,12 +24,15 @@ DefaultTacticsPoliceForce.CommandExecutorPolice : adf.impl.centralized.DefaultCo DefaultTacticsPoliceForce.CommandExecutorScout : adf.impl.centralized.DefaultCommandExecutorScoutPolice ## DefaultTacticsAmbulanceCentre -DefaultTacticsAmbulanceCentre.TargetAllocator : sample_team.module.complex.SampleAmbulanceTargetAllocator -DefaultTacticsAmbulanceCentre.CommandPicker : adf.impl.centralized.DefaultCommandPickerAmbulance +#DefaultTacticsAmbulanceCentre.TargetAllocator : sample_team.module.complex.SampleAmbulanceTargetAllocator +#DefaultTacticsAmbulanceCentre.CommandPicker : adf.impl.centralized.DefaultCommandPickerAmbulance +DefaultTacticsAmbulanceCentre.TargetAllocator : autumn_2023.module.complex.CentralizedControlATAllocator +DefaultTacticsAmbulanceCentre.CommandPicker : autumn_2023.centralized.CentralizedControlCommandPickerA ## DefaultTacticsFireStation -DefaultTacticsFireStation.TargetAllocator : sample_team.module.complex.SampleFireTargetAllocator +#DefaultTacticsFireStation.TargetAllocator : sample_team.module.complex.SampleFireTargetAllocator DefaultTacticsFireStation.CommandPicker : adf.impl.centralized.DefaultCommandPickerFire +TacticsFireStation.TargetAllocator : autumn_2023.module.complex.CentralizedControlFBAllocator ## DefaultTacticsPoliceOffice DefaultTacticsPoliceOffice.TargetAllocator : sample_team.module.complex.SamplePoliceTargetAllocator @@ -75,8 +79,8 @@ DefaultCommandExecutorAmbulance.ExtActionMove : adf.impl.extaction.DefaultExtAct ## DefaultCommandExecutorFire DefaultCommandExecutorFire.PathPlanning : adf.impl.module.algorithm.DijkstraPathPlanning -DefaultCommandExecutorFire.EtxActionFireRescue : adf.impl.extaction.DefaultExtActionFireRescue -DefaultCommandExecutorFire.EtxActionFireFighting : adf.impl.extaction.DefaultExtActionFireFighting +DefaultCommandExecutorFire.ExtActionFireRescue : autumn_2023.extaction.ComplimentTacticsExtActionFireRescue +DefaultCommandExecutorFire.ExtActionFireFighting : adf.impl.extaction.DefaultExtActionFireFighting DefaultCommandExecutorFire.ExtActionMove : adf.impl.extaction.DefaultExtActionMove ## DefaultCommandExecutorPolice @@ -92,10 +96,10 @@ DefaultCommandExecutorScoutPolice.PathPlanning : adf.impl.module.algorithm.Dijks DefaultCommandExecutorScoutPolice.ExtActionClear : adf.impl.extaction.DefaultExtActionClear ## MessageManager -MessageManager.PlatoonChannelSubscriber : adf.impl.module.comm.DefaultChannelSubscriber -MessageManager.CenterChannelSubscriber : adf.impl.module.comm.DefaultChannelSubscriber -MessageManager.PlatoonMessageCoordinator : adf.impl.module.comm.DefaultMessageCoordinator -MessageManager.CenterMessageCoordinator : adf.impl.module.comm.DefaultMessageCoordinator +MessageManager.PlatoonChannelSubscriber : autumn_2023.module.comm.DefaultChannelSubscriber +MessageManager.CenterChannelSubscriber : autumn_2023.module.comm.DefaultChannelSubscriber +MessageManager.PlatoonMessageCoordinator : autumn_2023.module.comm.CentralizedControlMessageCoordinator +MessageManager.CenterMessageCoordinator : autumn_2023.module.comm.CentralizedControlMessageCoordinator ## VisualDebug VisualDebug : true \ No newline at end of file diff --git a/docs/.DS_Store b/docs/.DS_Store index 26b27a2e5d6d384691db1aa916d47bd5fbdb815b..81fe50bda9452aa93a904087a5452c16494dc7b1 100644 GIT binary patch delta 36 mcmZoMXffDuhl%}Sm)zfjg-0jzGK)|C#l!>SZ{}tGE&>22!Vm%g delta 32 ocmZoMXffDuhl!nmfnn;FV@D?QGK)|C#l$l?hlzW$AoFJt0K3u)00000 diff --git a/precompute.sh b/precompute.sh new file mode 100755 index 0000000..acfa047 --- /dev/null +++ b/precompute.sh @@ -0,0 +1,35 @@ +#!/bin/sh + +LOADER="adf.impl.DefaultLoader" +PARAMS=$* + +cd `dirname $0` + +if [ ! -z "$1" ]; then + ./gradlew launch --args="${LOADER} ${PARAMS} -pre 1" +else + echo "Options:" + echo "-tn Team name" + echo "-t [FB],[FS],[PF],[PO],[AT],[AC] Number of agents" + echo "-fb [FB] Number of FireBrigade" + echo "-fs [FS] Number of FireStation" + echo "-pf [PF] Number of PoliceForce" + echo "-po [PO] Number of PoliceOffice" + echo "-at [AT] Number of AmbulanceTeam" + echo "-ac [AC] Number of AmbulanceCentre" + echo "-s [HOST]:[PORT] RCRS server host and port" + echo "-h [HOST] RCRS server host (port:27931)" + echo "-pre [0|1] Precompute flag" + echo "-d [0|1] Debug flag" + echo "-dev [0|1] Development mode" + echo "-mc [FILE] ModuleConfig file name" + echo "-md [JSON] ModuleConfig JSON" + echo "-df [FILE] DevelopData JSON file" + echo "-dd [JSON] DevelopData JSON" + echo "-all [alias] -t -1,-1,-1,-1,-1,-1" + echo "-allp [alias] -t 1,0,1,0,1,0," + echo "-local [alias] -h localhost" + echo "-precompute [alias] -pre 1" + echo "-debug [alias] -d 1" + echo "-develop [alias] -dev 1" +fi diff --git a/src/main/java/autumn_2023/centralized/HungarianCommandExecutorAmbulance.java b/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorA.java similarity index 97% rename from src/main/java/autumn_2023/centralized/HungarianCommandExecutorAmbulance.java rename to src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorA.java index 33309c9..83841ed 100644 --- a/src/main/java/autumn_2023/centralized/HungarianCommandExecutorAmbulance.java +++ b/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorA.java @@ -25,7 +25,7 @@ import rescuecore2.standard.entities.Human; import rescuecore2.standard.entities.StandardEntity; import rescuecore2.worldmodel.EntityID; -public class HungarianCommandExecutorAmbulance +public class CentralizedControlCommandExecutorA extends CommandExecutor { private static final int ACTION_UNKNOWN = -1; @@ -45,7 +45,7 @@ public class HungarianCommandExecutorAmbulance private EntityID target; private EntityID commanderID; - public HungarianCommandExecutorAmbulance(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) { + public CentralizedControlCommandExecutorA(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) { super(ai, wi, si, moduleManager, developData); this.commandType = ACTION_UNKNOWN; switch (scenarioInfo.getMode()) { @@ -149,6 +149,7 @@ public class HungarianCommandExecutorAmbulance @Override public CommandExecutor calc() { this.result = null; + //System.out.println("time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" excutor"); switch (this.commandType) { case ACTION_REST: EntityID position = this.agentInfo.getPosition(); diff --git a/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorF.java b/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorF.java new file mode 100644 index 0000000..11c64db --- /dev/null +++ b/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorF.java @@ -0,0 +1,330 @@ +package autumn_2023.centralized; + +import static rescuecore2.standard.entities.StandardEntityURN.CIVILIAN; +import static rescuecore2.standard.entities.StandardEntityURN.REFUGE; + +import adf.core.agent.action.Action; +import adf.core.agent.action.common.ActionMove; +import adf.core.agent.action.common.ActionRest; +import adf.core.agent.communication.MessageManager; +import adf.core.agent.communication.standard.bundle.centralized.CommandFire; +import adf.core.agent.communication.standard.bundle.centralized.MessageReport; +import adf.core.agent.develop.DevelopData; +import adf.core.agent.info.AgentInfo; +import adf.core.agent.info.ScenarioInfo; +import adf.core.agent.info.WorldInfo; +import adf.core.agent.module.ModuleManager; +import adf.core.agent.precompute.PrecomputeData; +import adf.core.component.centralized.CommandExecutor; +import adf.core.component.extaction.ExtAction; +import adf.core.component.module.algorithm.PathPlanning; +import java.util.Collection; +import java.util.List; +import java.util.Objects; +import rescuecore2.standard.entities.Area; +import rescuecore2.standard.entities.Human; +import rescuecore2.standard.entities.StandardEntity; +import rescuecore2.worldmodel.EntityID; + +public class CentralizedControlCommandExecutorF extends CommandExecutor { + + private static final int ACTION_UNKNOWN = -1; + private static final int ACTION_REST = CommandFire.ACTION_REST; + private static final int ACTION_MOVE = CommandFire.ACTION_MOVE; + private static final int ACTION_RESCUE = CommandFire.ACTION_RESCUE; + private static final int ACTION_AUTONOMY = CommandFire.ACTION_AUTONOMY; + + private PathPlanning pathPlanning; + + private ExtAction actionFireRescue; + private ExtAction actionExtMove; + + private int commandType; + private EntityID target; + private EntityID commanderID; + + private boolean commandCompleted; + + public CentralizedControlCommandExecutorF(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) { + super(ai, wi, si, moduleManager, developData); + this.commandType = ACTION_UNKNOWN; + this.target = null; + this.commanderID = null; + switch (scenarioInfo.getMode()) { + case PRECOMPUTATION_PHASE: + case PRECOMPUTED: + case NON_PRECOMPUTE: + this.pathPlanning = moduleManager.getModule( + "DefaultCommandExecutorFire.PathPlanning", + "adf.impl.module.algorithm.DijkstraPathPlanning"); + this.actionFireRescue = moduleManager.getExtAction( + "DefaultCommandExecutorFire.ExtActionFireRescue", + "adf.impl.extaction.DefaultExtActionFireRescue"); + this.actionExtMove = moduleManager.getExtAction( + "DefaultCommandExecutorFire.ExtActionMove", + "adf.impl.extaction.DefaultExtActionMove"); + break; + } + } + + + @Override + public CommandExecutor setCommand(CommandFire command) { + EntityID agentID = this.agentInfo.getID(); + if(!this.commandCompleted) return this; + if (command.isToIDDefined() && Objects.requireNonNull(command.getToID()) + .getValue() == agentID.getValue()) { + this.commandType = command.getAction(); + this.target = command.getTargetID(); + this.commanderID = command.getSenderID(); + //System.out.println("[FireBrigade] recievecommand time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" tagID:"+this.target+" commandType:"+this.commandType); + } + return this; + } + + + @Override + public CommandExecutor updateInfo(MessageManager messageManager) { + super.updateInfo(messageManager); + if (this.getCountUpdateInfo() >= 2) { + return this; + } + this.pathPlanning.updateInfo(messageManager); + this.actionFireRescue.updateInfo(messageManager); + this.actionExtMove.updateInfo(messageManager); + + this.commandCompleted = this.isCommandCompleted(this.commandType); + //System.out.println("[FireBrigade] isCommandCompleted ["+this.commandCompleted+"] time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" tagID:"+this.target+" commandType:"+this.commandType); + if (this.commandCompleted) { + if (this.commandType != ACTION_UNKNOWN) { + messageManager + .addMessage(new MessageReport(true, true, false, this.target)); + + this.commandType = ACTION_UNKNOWN; + this.target = null; + this.commanderID = null; + } + } + return this; + } + + + @Override + public CommandExecutor precompute(PrecomputeData precomputeData) { + super.precompute(precomputeData); + if (this.getCountPrecompute() >= 2) { + return this; + } + this.pathPlanning.precompute(precomputeData); + this.actionFireRescue.precompute(precomputeData); + this.actionExtMove.precompute(precomputeData); + return this; + } + + + @Override + public CommandExecutor resume(PrecomputeData precomputeData) { + super.resume(precomputeData); + if (this.getCountResume() >= 2) { + return this; + } + this.pathPlanning.resume(precomputeData); + this.actionFireRescue.resume(precomputeData); + this.actionExtMove.resume(precomputeData); + return this; + } + + + @Override + public CommandExecutor preparate() { + super.preparate(); + if (this.getCountPreparate() >= 2) { + return this; + } + this.pathPlanning.preparate(); + this.actionFireRescue.preparate(); + this.actionExtMove.preparate(); + return this; + } + + + @Override + public CommandExecutor calc() { + this.result = null; + switch (this.commandType) { + case ACTION_REST: + EntityID position = this.agentInfo.getPosition(); + if (this.target == null) { + Collection< + EntityID> refuges = this.worldInfo.getEntityIDsOfType(REFUGE); + if (refuges.contains(position)) { + this.result = new ActionRest(); + } else { + this.pathPlanning.setFrom(position); + this.pathPlanning.setDestination(refuges); + List path = this.pathPlanning.calc().getResult(); + if (path != null && path.size() > 0) { + this.result = new ActionMove(path); + } else { + this.result = new ActionRest(); + } + } + return this; + } + if (position.getValue() != this.target.getValue()) { + List path = this.pathPlanning.getResult(position, + this.target); + if (path != null && path.size() > 0) { + this.result = new ActionMove(path); + return this; + } + } + this.result = new ActionRest(); + return this; + case ACTION_MOVE: + if (this.target != null) { + this.result = this.actionExtMove.setTarget(this.target).calc() + .getAction(); + } + return this; + case ACTION_RESCUE: + if (this.target != null) { + this.result = this.actionFireRescue.setTarget(this.target).calc() + .getAction(); + } + return this; + case ACTION_AUTONOMY: + if (this.target == null) { + System.out.println("[FireBrigade] excutor notarget time:"+this.agentInfo.getTime()+" id:"+this.commanderID+" tagID:"+this.target+" commandType:"+this.commandType); + return this; + } + StandardEntity targetEntity = this.worldInfo.getEntity(this.target); + if (targetEntity instanceof Area) { + this.result = this.actionExtMove.setTarget(this.target).calc() + .getAction(); + } else if (targetEntity instanceof Human) { + this.result = this.actionFireRescue.setTarget(this.target).calc() + .getAction(); + System.out.println("[FireBrigade] excutor "+this.result+" time:"+this.agentInfo.getTime()+" id:"+this.commanderID+" tagID:"+this.target+" commandType:"+this.commandType); + } + } + return this; + } + + /*private Action computeCommand(int commandType, EntityID target, EntityID commanderID){ + Action action = null; + switch (commandType) { + case ACTION_REST: + EntityID position = this.agentInfo.getPosition(); + if (target == null) { + Collection< + EntityID> refuges = this.worldInfo.getEntityIDsOfType(REFUGE); + if (refuges.contains(position)) { + action = new ActionRest(); + } else { + this.pathPlanning.setFrom(position); + this.pathPlanning.setDestination(refuges); + List path = this.pathPlanning.calc().getResult(); + if (path != null && path.size() > 0) { + action = new ActionMove(path); + } else { + action = new ActionRest(); + } + } + return action; + } + if (position.getValue() != target.getValue()) { + List path = this.pathPlanning.getResult(position, + target); + if (path != null && path.size() > 0) { + action = new ActionMove(path); + return action; + } + } + action = new ActionRest(); + return action; + case ACTION_MOVE: + if (target != null) { + action = this.actionExtMove.setTarget(target).calc() + .getAction(); + } + return action; + case ACTION_RESCUE: + if (target != null) { + action = this.actionFireRescue.setTarget(target).calc() + .getAction(); + } + return action; + case ACTION_AUTONOMY: + if (target == null) { + return action; + } + StandardEntity targetEntity = this.worldInfo.getEntity(target); + if (targetEntity instanceof Area) { + action = this.actionExtMove.setTarget(target).calc() + .getAction(); + } else if (targetEntity instanceof Human) { + action = this.actionFireRescue.setTarget(target).calc() + .getAction(); + } + } + return action; + }*/ + + private boolean isCommandCompleted(int commandType) { + Human agent = (Human) this.agentInfo.me(); + switch (commandType) { + case ACTION_REST: + if (this.target == null) { + return (agent.getDamage() == 0); + } + if (Objects.requireNonNull(this.worldInfo.getEntity(this.target)) + .getStandardURN() == REFUGE) { + if (agent.getPosition().getValue() == this.target.getValue()) { + return (agent.getDamage() == 0); + } + } + return false; + case ACTION_MOVE: + return this.target == null || this.agentInfo.getPosition() + .getValue() == this.target.getValue(); + case ACTION_RESCUE: + if (this.target == null) { + return true; + } + Human human = (Human) Objects + .requireNonNull(this.worldInfo.getEntity(this.target)); + return human.isBuriednessDefined() && human.getBuriedness() == 0 + || (human.isHPDefined() && human.getHP() == 0); + case ACTION_AUTONOMY: + if (this.target != null) { + StandardEntity targetEntity = this.worldInfo.getEntity(this.target); + if (targetEntity instanceof Area) { + commandType = ACTION_MOVE; + return this.isCommandCompleted(commandType); + } else if (targetEntity instanceof Human) { + //commandType = ACTION_MOVE; + //if(!this.isCommandCompleted(commandType)) return false; + + Human h = (Human) targetEntity; + EntityID position = h.getPosition(); + /*if (this.agentInfo.getPosition().getValue() != position.getValue()) { + return false; + }*/ + commandType = ACTION_RESCUE; + + /*if ((h.isHPDefined() && h.getHP() == 0)) { + return true; + } + //commandType = h.getStandardURN() == CIVILIAN ? ACTION_RESCUE : null; + if (h.getStandardURN() == CIVILIAN) { + commandType = ACTION_RESCUE; + }*/ + return this.isCommandCompleted(commandType); + } + } + return true; + } + return true; + } +} \ No newline at end of file diff --git a/src/main/java/autumn_2023/centralized/CentralizedControlCommandPickerA.java b/src/main/java/autumn_2023/centralized/CentralizedControlCommandPickerA.java new file mode 100644 index 0000000..66b9cc0 --- /dev/null +++ b/src/main/java/autumn_2023/centralized/CentralizedControlCommandPickerA.java @@ -0,0 +1,80 @@ +package autumn_2023.centralized; + +import adf.core.agent.communication.standard.bundle.centralized.CommandAmbulance; +import adf.core.agent.communication.standard.bundle.centralized.CommandScout; +import adf.core.agent.develop.DevelopData; +import adf.core.agent.info.AgentInfo; +import adf.core.agent.info.ScenarioInfo; +import adf.core.agent.info.WorldInfo; +import adf.core.agent.module.ModuleManager; +import adf.core.component.centralized.CommandPicker; +import adf.core.component.communication.CommunicationMessage; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Map; +import rescuecore2.standard.entities.Area; +import rescuecore2.standard.entities.Human; +import rescuecore2.standard.entities.StandardEntity; +import rescuecore2.standard.entities.StandardEntityURN; +import rescuecore2.worldmodel.EntityID; + +public class CentralizedControlCommandPickerA extends CommandPicker { + + private int scoutDistance; + + private Collection messages; + private Map allocationData; + + public CentralizedControlCommandPickerA(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) { + super(ai, wi, si, moduleManager, developData); + this.messages = new ArrayList<>(); + this.allocationData = null; + this.scoutDistance = developData.getInteger( + "adf.impl.centralized.DefaultCommandPickerAmbulance.scoutDistance", + 40000); + } + + + @Override + public CommandPicker + setAllocatorResult(Map allocationData) { + this.allocationData = allocationData; + return this; + } + + + @Override + public CommandPicker calc() { + //System.out.println("time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" picker"); + this.messages.clear(); + if (this.allocationData == null) { + return this; + } + for (EntityID agentID : this.allocationData.keySet()) { + StandardEntity agent = this.worldInfo.getEntity(agentID); + if (agent != null + && agent.getStandardURN() == StandardEntityURN.AMBULANCE_TEAM) { + StandardEntity target = this.worldInfo + .getEntity(this.allocationData.get(agentID)); + if (target != null) { + if (target instanceof Human) { + CommandAmbulance command = new CommandAmbulance(true, agentID, + target.getID(), CommandAmbulance.ACTION_AUTONOMY); + this.messages.add(command); + } else if (target instanceof Area) { + CommandScout command = new CommandScout(true, agentID, + target.getID(), this.scoutDistance); + this.messages.add(command); + } + } + } + } + return this; + } + + + @Override + public Collection getResult() { + return this.messages; + } +} \ No newline at end of file diff --git a/src/main/java/autumn_2023/extaction/ComplimentTacticsExtActionFireRescue.java b/src/main/java/autumn_2023/extaction/ComplimentTacticsExtActionFireRescue.java new file mode 100644 index 0000000..bf2e6f5 --- /dev/null +++ b/src/main/java/autumn_2023/extaction/ComplimentTacticsExtActionFireRescue.java @@ -0,0 +1,239 @@ +package autumn_2023.extaction; + +import static rescuecore2.standard.entities.StandardEntityURN.BLOCKADE; +import adf.core.agent.action.Action; +import adf.core.agent.action.ambulance.ActionRescue; +import adf.core.agent.action.common.ActionMove; +import adf.core.agent.communication.MessageManager; +import adf.core.agent.communication.standard.bundle.information.MessageFireBrigade; +import adf.core.agent.develop.DevelopData; +import adf.core.agent.info.AgentInfo; +import adf.core.agent.info.ScenarioInfo; +import adf.core.agent.info.WorldInfo; +import adf.core.agent.module.ModuleManager; +import adf.core.agent.precompute.PrecomputeData; +import adf.core.component.extaction.ExtAction; +import adf.core.component.module.algorithm.PathPlanning; +import java.util.ArrayList; +import java.util.List; +import rescuecore2.config.NoSuchConfigOptionException; +import rescuecore2.standard.entities.Area; +import rescuecore2.standard.entities.Blockade; +import rescuecore2.standard.entities.FireBrigade; +import rescuecore2.standard.entities.Human; +import rescuecore2.standard.entities.StandardEntity; +import rescuecore2.worldmodel.EntityID; + +public class ComplimentTacticsExtActionFireRescue extends ExtAction { + + private MessageManager mm; + private PathPlanning pathPlanning; + + private int thresholdRest; + private int kernelTime; + + private EntityID target; + + public ComplimentTacticsExtActionFireRescue(AgentInfo agentInfo, WorldInfo worldInfo, ScenarioInfo scenarioInfo, ModuleManager moduleManager, DevelopData developData) { + super(agentInfo, worldInfo, scenarioInfo, moduleManager, developData); + this.target = null; + this.thresholdRest = developData + .getInteger("adf.impl.extaction.DefaultExtActionFireRescue.rest", 100); + + switch (scenarioInfo.getMode()) { + case PRECOMPUTATION_PHASE: + case PRECOMPUTED: + case NON_PRECOMPUTE: + this.pathPlanning = moduleManager.getModule( + "DefaultExtActionFireRescue.PathPlanning", + "adf.impl.module.algorithm.DijkstraPathPlanning"); + break; + } + } + + + public ExtAction precompute(PrecomputeData precomputeData) { + super.precompute(precomputeData); + if (this.getCountPrecompute() >= 2) { + return this; + } + this.pathPlanning.precompute(precomputeData); + try { + this.kernelTime = this.scenarioInfo.getKernelTimesteps(); + } catch (NoSuchConfigOptionException e) { + this.kernelTime = -1; + } + return this; + } + + + public ExtAction resume(PrecomputeData precomputeData) { + super.resume(precomputeData); + if (this.getCountResume() >= 2) { + return this; + } + this.pathPlanning.resume(precomputeData); + try { + this.kernelTime = this.scenarioInfo.getKernelTimesteps(); + } catch (NoSuchConfigOptionException e) { + this.kernelTime = -1; + } + return this; + } + + + public ExtAction preparate() { + super.preparate(); + if (this.getCountPreparate() >= 2) { + return this; + } + this.pathPlanning.preparate(); + try { + this.kernelTime = this.scenarioInfo.getKernelTimesteps(); + } catch (NoSuchConfigOptionException e) { + this.kernelTime = -1; + } + return this; + } + + + public ExtAction updateInfo(MessageManager messageManager) { + super.updateInfo(messageManager); + if (this.getCountUpdateInfo() >= 2) { + return this; + } + this.mm = messageManager; + this.pathPlanning.updateInfo(messageManager); + return this; + } + + + @Override + public ExtAction setTarget(EntityID target) { + this.target = null; + if (target != null) { + StandardEntity entity = this.worldInfo.getEntity(target); + if (entity instanceof Human || entity instanceof Area) { + this.target = target; + return this; + } + } + return this; + } + + + @Override + public ExtAction calc() { + this.result = null; + FireBrigade agent = (FireBrigade) this.agentInfo.me(); + + if (this.needRest(agent)) { + EntityID areaID = this.convertArea(this.target); + ArrayList targets = new ArrayList<>(); + if (areaID != null) { + targets.add(areaID); + } + } + if (this.target != null) { + this.result = this.calcRescue(agent, this.pathPlanning, this.target); + } + if(this.result != null){ + Class actionClass = this.result.getClass(); + if (actionClass == ActionRescue.class) { + //System.out.println("[FireRescue time:"+this.agentInfo.getTime()+" myid:"+this.agentInfo.getID()); + this.mm.addMessage(new MessageFireBrigade(true, agent, MessageFireBrigade.ACTION_RESCUE, this.target)); + } + } + return this; + } + + + private Action calcRescue(FireBrigade agent, PathPlanning pathPlanning, + EntityID targetID) { + StandardEntity targetEntity = this.worldInfo.getEntity(targetID); + if (targetEntity == null) { + return null; + } + EntityID agentPosition = agent.getPosition(); + if (targetEntity instanceof Human) { + Human human = (Human) targetEntity; + if (!human.isPositionDefined()) { + return null; + } + if (human.isHPDefined() && human.getHP() == 0) { + return null; + } + EntityID targetPosition = human.getPosition(); + if (agentPosition.getValue() == targetPosition.getValue()) { + if (human.isBuriednessDefined() && human.getBuriedness() > 0) { + return new ActionRescue(human); + } + } else { + List path = pathPlanning.getResult(agentPosition, + targetPosition); + if (path != null && path.size() > 0) { + return new ActionMove(path); + } + } + return null; + } + if (targetEntity.getStandardURN() == BLOCKADE) { + Blockade blockade = (Blockade) targetEntity; + if (blockade.isPositionDefined()) { + targetEntity = this.worldInfo.getEntity(blockade.getPosition()); + } + } + if (targetEntity instanceof Area) { + List path = pathPlanning.getResult(agentPosition, + targetEntity.getID()); + if (path != null && path.size() > 0) { + return new ActionMove(path); + } + } + return null; + } + + + private boolean needRest(Human agent) { + int hp = agent.getHP(); + int damage = agent.getDamage(); + if (hp == 0 || damage == 0) { + return false; + } + int activeTime = (hp / damage) + ((hp % damage) != 0 ? 1 : 0); + if (this.kernelTime == -1) { + try { + this.kernelTime = this.scenarioInfo.getKernelTimesteps(); + } catch (NoSuchConfigOptionException e) { + this.kernelTime = -1; + } + } + return damage >= this.thresholdRest + || (activeTime + this.agentInfo.getTime()) < this.kernelTime; + } + + + private EntityID convertArea(EntityID targetID) { + StandardEntity entity = this.worldInfo.getEntity(targetID); + if (entity == null) { + return null; + } + if (entity instanceof Human) { + Human human = (Human) entity; + if (human.isPositionDefined()) { + EntityID position = human.getPosition(); + if (this.worldInfo.getEntity(position) instanceof Area) { + return position; + } + } + } else if (entity instanceof Area) { + return targetID; + } else if (entity.getStandardURN() == BLOCKADE) { + Blockade blockade = (Blockade) entity; + if (blockade.isPositionDefined()) { + return blockade.getPosition(); + } + } + return null; + } +} \ No newline at end of file diff --git a/src/main/java/autumn_2023/module/comm/CentralizedControlMessageCoordinator.java b/src/main/java/autumn_2023/module/comm/CentralizedControlMessageCoordinator.java new file mode 100644 index 0000000..5db7e1a --- /dev/null +++ b/src/main/java/autumn_2023/module/comm/CentralizedControlMessageCoordinator.java @@ -0,0 +1,215 @@ +package autumn_2023.module.comm; + +import adf.core.agent.communication.MessageManager; +import adf.core.agent.communication.standard.bundle.StandardMessage; +import adf.core.agent.communication.standard.bundle.StandardMessagePriority; +import adf.core.agent.communication.standard.bundle.centralized.CommandAmbulance; +import adf.core.agent.communication.standard.bundle.centralized.CommandFire; +import adf.core.agent.communication.standard.bundle.centralized.CommandPolice; +import adf.core.agent.communication.standard.bundle.centralized.CommandScout; +import adf.core.agent.communication.standard.bundle.centralized.MessageReport; +import adf.core.agent.communication.standard.bundle.information.MessageAmbulanceTeam; +import adf.core.agent.communication.standard.bundle.information.MessageBuilding; +import adf.core.agent.communication.standard.bundle.information.MessageCivilian; +import adf.core.agent.communication.standard.bundle.information.MessageFireBrigade; +import adf.core.agent.communication.standard.bundle.information.MessagePoliceForce; +import adf.core.agent.communication.standard.bundle.information.MessageRoad; +import adf.core.agent.info.AgentInfo; +import adf.core.agent.info.ScenarioInfo; +import adf.core.agent.info.WorldInfo; +import adf.core.component.communication.CommunicationMessage; +import adf.core.component.communication.MessageCoordinator; +import java.util.ArrayList; +import java.util.List; +import rescuecore2.standard.entities.StandardEntityURN; + +public class CentralizedControlMessageCoordinator extends MessageCoordinator { + + @Override + public void coordinate(AgentInfo agentInfo, WorldInfo worldInfo, + ScenarioInfo scenarioInfo, MessageManager messageManager, + ArrayList sendMessageList, + List> channelSendMessageList) { + + // have different lists for every agent + ArrayList policeMessages = new ArrayList<>(); + ArrayList ambulanceMessages = new ArrayList<>(); + ArrayList fireBrigadeMessages = new ArrayList<>(); + + ArrayList voiceMessages = new ArrayList<>(); + + StandardEntityURN agentType = getAgentType(agentInfo, worldInfo); + + for (CommunicationMessage msg : sendMessageList) { + if (msg instanceof StandardMessage + && !((StandardMessage) msg).isRadio()) { + voiceMessages.add(msg); + } else { + if (msg instanceof MessageBuilding) { + fireBrigadeMessages.add(msg); + } else if (msg instanceof MessageCivilian) { + fireBrigadeMessages.add(msg); + ambulanceMessages.add(msg); + } else if (msg instanceof MessageRoad) { + fireBrigadeMessages.add(msg); + ambulanceMessages.add(msg); + policeMessages.add(msg); + } else if (msg instanceof CommandAmbulance) { + ambulanceMessages.add(msg); + } else if (msg instanceof CommandFire) { + fireBrigadeMessages.add(msg); + } else if (msg instanceof CommandPolice) { + policeMessages.add(msg); + } else if (msg instanceof CommandScout) { + if (agentType == StandardEntityURN.FIRE_STATION) { + fireBrigadeMessages.add(msg); + } else if (agentType == StandardEntityURN.POLICE_OFFICE) { + policeMessages.add(msg); + } else if (agentType == StandardEntityURN.AMBULANCE_CENTRE) { + ambulanceMessages.add(msg); + } + } else if (msg instanceof MessageReport) { + if (agentType == StandardEntityURN.FIRE_BRIGADE) { + fireBrigadeMessages.add(msg); + } else if (agentType == StandardEntityURN.POLICE_FORCE) { + policeMessages.add(msg); + } else if (agentType == StandardEntityURN.AMBULANCE_TEAM) { + ambulanceMessages.add(msg); + } + } else if (msg instanceof MessageFireBrigade) { + fireBrigadeMessages.add(msg); + ambulanceMessages.add(msg); + policeMessages.add(msg); + } else if (msg instanceof MessagePoliceForce) { + ambulanceMessages.add(msg); + policeMessages.add(msg); + } else if (msg instanceof MessageAmbulanceTeam) { + ambulanceMessages.add(msg); + policeMessages.add(msg); + } + } + } + + if (scenarioInfo.getCommsChannelsCount() > 1) { + // send radio messages if there are more than one communication channel + int[] channelSize = new int[scenarioInfo.getCommsChannelsCount() - 1]; + + setSendMessages(scenarioInfo, StandardEntityURN.POLICE_FORCE, agentInfo, + worldInfo, policeMessages, channelSendMessageList, channelSize); + setSendMessages(scenarioInfo, StandardEntityURN.AMBULANCE_TEAM, agentInfo, + worldInfo, ambulanceMessages, channelSendMessageList, channelSize); + setSendMessages(scenarioInfo, StandardEntityURN.FIRE_BRIGADE, agentInfo, + worldInfo, fireBrigadeMessages, channelSendMessageList, channelSize); + } + + ArrayList voiceMessageLowList = new ArrayList<>(); + ArrayList voiceMessageNormalList = new ArrayList<>(); + ArrayList voiceMessageHighList = new ArrayList<>(); + + for (CommunicationMessage msg : voiceMessages) { + if (msg instanceof StandardMessage) { + StandardMessage m = (StandardMessage) msg; + switch (m.getSendingPriority()) { + case LOW: + voiceMessageLowList.add(m); + break; + case NORMAL: + voiceMessageNormalList.add(m); + break; + case HIGH: + voiceMessageHighList.add(m); + break; + } + } + } + + // set the voice channel messages + channelSendMessageList.get(0).addAll(voiceMessageHighList); + channelSendMessageList.get(0).addAll(voiceMessageNormalList); + channelSendMessageList.get(0).addAll(voiceMessageLowList); + } + + + protected int[] getChannelsByAgentType(StandardEntityURN agentType, + AgentInfo agentInfo, WorldInfo worldInfo, ScenarioInfo scenarioInfo, + int channelIndex) { + int numChannels = scenarioInfo.getCommsChannelsCount() - 1; // 0th channel + // is the voice + // channel + int maxChannelCount = 0; + boolean isPlatoon = isPlatoonAgent(agentInfo, worldInfo); + if (isPlatoon) { + maxChannelCount = scenarioInfo.getCommsChannelsMaxPlatoon(); + } else { + maxChannelCount = scenarioInfo.getCommsChannelsMaxOffice(); + } + int[] channels = new int[maxChannelCount]; + + for (int i = 0; i < maxChannelCount; i++) { + channels[i] = DefaultChannelSubscriber.getChannelNumber(agentType, i, + numChannels); + } + + String a = "isPlatoon:"+isPlatoon; + for(int i = 0;i < channels.length; i++){ + a += " channels["+i+"]:"+channels[i]; + } + //System.out.println("time:"+agentInfo.getTime()+" myid:"+agentInfo.getID()+a); + + return channels; + } + + + protected boolean isPlatoonAgent(AgentInfo agentInfo, WorldInfo worldInfo) { + StandardEntityURN agentType = getAgentType(agentInfo, worldInfo); + if (agentType == StandardEntityURN.FIRE_BRIGADE + || agentType == StandardEntityURN.POLICE_FORCE + || agentType == StandardEntityURN.AMBULANCE_TEAM) { + return true; + } + return false; + } + + + protected StandardEntityURN getAgentType(AgentInfo agentInfo, + WorldInfo worldInfo) { + StandardEntityURN agentType = worldInfo.getEntity(agentInfo.getID()) + .getStandardURN(); + return agentType; + } + + + protected void setSendMessages(ScenarioInfo scenarioInfo, + StandardEntityURN agentType, AgentInfo agentInfo, WorldInfo worldInfo, + List messages, + List> channelSendMessageList, + int[] channelSize) { + int channelIndex = 0; + int[] channels = getChannelsByAgentType(agentType, agentInfo, worldInfo, + scenarioInfo, channelIndex); + int channel = channels[channelIndex]; + int channelCapacity = scenarioInfo.getCommsChannelBandwidth(channel); + // start from HIGH, NORMAL, to LOW + for (int i = StandardMessagePriority.values().length - 1; i >= 0; i--) { + for (CommunicationMessage msg : messages) { + StandardMessage smsg = (StandardMessage) msg; + if (smsg.getSendingPriority() == StandardMessagePriority.values()[i]) { + channelSize[channel - 1] += smsg.getByteArraySize(); + if (channelSize[channel - 1] > channelCapacity) { + channelSize[channel - 1] -= smsg.getByteArraySize(); + channelIndex++; + if (channelIndex < channels.length) { + channel = channels[channelIndex]; + channelCapacity = scenarioInfo.getCommsChannelBandwidth(channel); + channelSize[channel - 1] += smsg.getByteArraySize(); + } else { + // if there is no new channel for that message types, just break + break; + } + } + channelSendMessageList.get(channel).add(smsg); + } + } + } + } +} \ No newline at end of file diff --git a/src/main/java/autumn_2023/module/comm/DefaultChannelSubscriber.java b/src/main/java/autumn_2023/module/comm/DefaultChannelSubscriber.java new file mode 100644 index 0000000..c5de79e --- /dev/null +++ b/src/main/java/autumn_2023/module/comm/DefaultChannelSubscriber.java @@ -0,0 +1,100 @@ +package autumn_2023.module.comm; + +import adf.core.agent.communication.MessageManager; +import adf.core.agent.info.AgentInfo; +import adf.core.agent.info.ScenarioInfo; +import adf.core.agent.info.WorldInfo; +import adf.core.component.communication.ChannelSubscriber; +import rescuecore2.standard.entities.StandardEntityURN; + +public class DefaultChannelSubscriber extends ChannelSubscriber { + + @Override + public void subscribe(AgentInfo agentInfo, WorldInfo worldInfo, + ScenarioInfo scenarioInfo, MessageManager messageManager) { + // subscribe only once at the beginning + if (agentInfo.getTime() == 1) { + int numChannels = scenarioInfo.getCommsChannelsCount() - 1; // 0th channel + // is the + // voice + // channel + + int maxChannelCount = 0; + boolean isPlatoon = isPlatoonAgent(agentInfo, worldInfo); + if (isPlatoon) { + maxChannelCount = scenarioInfo.getCommsChannelsMaxPlatoon(); + } else { + maxChannelCount = scenarioInfo.getCommsChannelsMaxOffice(); + } + + StandardEntityURN agentType = getAgentType(agentInfo, worldInfo); + int[] channels = new int[maxChannelCount]; + for (int i = 0; i < maxChannelCount; i++) { + channels[i] = getChannelNumber(agentType, i, numChannels); + } + + messageManager.subscribeToChannels(channels); + } + } + + + protected boolean isPlatoonAgent(AgentInfo agentInfo, WorldInfo worldInfo) { + StandardEntityURN agentType = getAgentType(agentInfo, worldInfo); + if (agentType == StandardEntityURN.FIRE_BRIGADE + || agentType == StandardEntityURN.POLICE_FORCE + || agentType == StandardEntityURN.AMBULANCE_TEAM) { + return true; + } + return false; + } + + + protected StandardEntityURN getAgentType(AgentInfo agentInfo, + WorldInfo worldInfo) { + StandardEntityURN agentType = worldInfo.getEntity(agentInfo.getID()) + .getStandardURN(); + return agentType; + } + + + public static int getChannelNumber(StandardEntityURN agentType, + int channelIndex, int numChannels) { + int agentIndex = 0; + if (agentType == StandardEntityURN.FIRE_BRIGADE + || agentType == StandardEntityURN.FIRE_STATION) { + agentIndex = 1; + } else if (agentType == StandardEntityURN.POLICE_FORCE + || agentType == StandardEntityURN.POLICE_OFFICE) { + agentIndex = 2; + } else if (agentType == StandardEntityURN.AMBULANCE_TEAM + || agentType == StandardEntityURN.AMBULANCE_CENTRE) { + agentIndex = 3; + } + + int index = (3 * channelIndex) + agentIndex; + if ((index % numChannels) == 0) { + index = numChannels; + } else { + index = index % numChannels; + } + return index; + } + + + public static void main(String[] args) { + int numChannels = 6; + int maxChannels = 2; + for (int i = 0; i < maxChannels; i++) { + System.out.println("FIREBRIGADE-" + i + ":" + + getChannelNumber(StandardEntityURN.FIRE_BRIGADE, i, numChannels)); + } + for (int i = 0; i < maxChannels; i++) { + System.out.println("POLICE-" + i + ":" + + getChannelNumber(StandardEntityURN.POLICE_OFFICE, i, numChannels)); + } + for (int i = 0; i < maxChannels; i++) { + System.out.println("AMB-" + i + ":" + getChannelNumber( + StandardEntityURN.AMBULANCE_CENTRE, i, numChannels)); + } + } +} \ No newline at end of file diff --git a/src/main/java/autumn_2023/module/complex/CentralizedControlATAllocator.java b/src/main/java/autumn_2023/module/complex/CentralizedControlATAllocator.java new file mode 100755 index 0000000..3ef3a29 --- /dev/null +++ b/src/main/java/autumn_2023/module/complex/CentralizedControlATAllocator.java @@ -0,0 +1,371 @@ +package autumn_2023.module.complex; + +import static rescuecore2.standard.entities.StandardEntityURN.REFUGE; +import adf.core.agent.communication.MessageManager; +import adf.core.agent.communication.standard.bundle.MessageUtil; +import adf.core.agent.communication.standard.bundle.centralized.CommandAmbulance; +import adf.core.agent.communication.standard.bundle.centralized.MessageReport; +import adf.core.agent.communication.standard.bundle.information.MessageAmbulanceTeam; +import adf.core.agent.communication.standard.bundle.information.MessageCivilian; +import adf.core.agent.communication.standard.bundle.information.MessageFireBrigade; +import adf.core.agent.communication.standard.bundle.information.MessagePoliceForce; +import adf.core.agent.develop.DevelopData; +import adf.core.agent.info.AgentInfo; +import adf.core.agent.info.ScenarioInfo; +import adf.core.agent.info.WorldInfo; +import adf.core.agent.module.ModuleManager; +import adf.core.agent.precompute.PrecomputeData; +import adf.core.component.communication.CommunicationMessage; +import adf.core.component.module.complex.AmbulanceTargetAllocator; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import rescuecore2.standard.entities.AmbulanceTeam; +import rescuecore2.standard.entities.Area; +import rescuecore2.standard.entities.Human; +import rescuecore2.standard.entities.StandardEntity; +import rescuecore2.standard.entities.StandardEntityURN; +import rescuecore2.worldmodel.EntityID; + +public class CentralizedControlATAllocator extends AmbulanceTargetAllocator { + + private Collection priorityHumans; + private Collection targetHumans; + + private Map ambulanceTeamInfoMap; + + public CentralizedControlATAllocator(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) { + super(ai, wi, si, moduleManager, developData); + this.priorityHumans = new HashSet<>(); + this.targetHumans = new HashSet<>(); + this.ambulanceTeamInfoMap = new HashMap<>(); + } + + + @Override + public AmbulanceTargetAllocator resume(PrecomputeData precomputeData) { + super.resume(precomputeData); + if (this.getCountResume() >= 2) { + return this; + } + for (EntityID id : this.worldInfo + .getEntityIDsOfType(StandardEntityURN.AMBULANCE_TEAM)) { + this.ambulanceTeamInfoMap.put(id, new AmbulanceTeamInfo(id)); + } + return this; + } + + + @Override + public AmbulanceTargetAllocator preparate() { + super.preparate(); + if (this.getCountPrecompute() >= 2) { + return this; + } + for (EntityID id : this.worldInfo + .getEntityIDsOfType(StandardEntityURN.AMBULANCE_TEAM)) { + this.ambulanceTeamInfoMap.put(id, new AmbulanceTeamInfo(id)); + } + return this; + } + + + @Override + public Map getResult() { + return this.convert(this.ambulanceTeamInfoMap); + } + + + @Override + public AmbulanceTargetAllocator calc() { + //System.out.println("time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" allocator"); + List agents = this + .getActionAgents(this.ambulanceTeamInfoMap); + Collection removes = new ArrayList<>(); + int currentTime = this.agentInfo.getTime(); + for (EntityID target : this.priorityHumans) { + if (agents.size() > 0) { + StandardEntity targetEntity = this.worldInfo.getEntity(target); + if (targetEntity != null && targetEntity instanceof Human + && ((Human) targetEntity).isPositionDefined()) { + agents.sort(new DistanceSorter(this.worldInfo, targetEntity)); + StandardEntity result = agents.get(0); + agents.remove(0); + AmbulanceTeamInfo info = this.ambulanceTeamInfoMap + .get(result.getID()); + if (info != null) { + info.canNewAction = false; + info.target = target; + info.commandTime = currentTime; + this.ambulanceTeamInfoMap.put(result.getID(), info); + removes.add(target); + } + } + } + } + this.priorityHumans.removeAll(removes); + removes.clear(); + for (EntityID target : this.targetHumans) { + if (agents.size() > 0) { + StandardEntity targetEntity = this.worldInfo.getEntity(target); + if (targetEntity != null && targetEntity instanceof Human + && ((Human) targetEntity).isPositionDefined()) { + agents.sort(new DistanceSorter(this.worldInfo, targetEntity)); + StandardEntity result = agents.get(0); + agents.remove(0); + AmbulanceTeamInfo info = this.ambulanceTeamInfoMap + .get(result.getID()); + if (info != null) { + info.canNewAction = false; + info.target = target; + info.commandTime = currentTime; + this.ambulanceTeamInfoMap.put(result.getID(), info); + removes.add(target); + } + } + } + } + this.targetHumans.removeAll(removes); + return this; + } + + + @Override + public AmbulanceTargetAllocator updateInfo(MessageManager messageManager) { + super.updateInfo(messageManager); + if (this.getCountUpdateInfo() >= 2) { + return this; + } + int currentTime = this.agentInfo.getTime(); + for (CommunicationMessage message : messageManager + .getReceivedMessageList()) { + Class messageClass = message.getClass(); + if (messageClass == MessageCivilian.class) { + MessageCivilian mc = (MessageCivilian) message; + MessageUtil.reflectMessage(this.worldInfo, mc); + if (mc.isBuriednessDefined() && mc.getBuriedness() > 0) { + this.targetHumans.add(mc.getAgentID()); + } else { + this.priorityHumans.remove(mc.getAgentID()); + this.targetHumans.remove(mc.getAgentID()); + } + } else if (messageClass == MessageFireBrigade.class) { + MessageFireBrigade mfb = (MessageFireBrigade) message; + MessageUtil.reflectMessage(this.worldInfo, mfb); + if (mfb.isBuriednessDefined() && mfb.getBuriedness() > 0) { + this.priorityHumans.add(mfb.getAgentID()); + } else { + this.priorityHumans.remove(mfb.getAgentID()); + this.targetHumans.remove(mfb.getAgentID()); + } + } else if (messageClass == MessagePoliceForce.class) { + MessagePoliceForce mpf = (MessagePoliceForce) message; + MessageUtil.reflectMessage(this.worldInfo, mpf); + if (mpf.isBuriednessDefined() && mpf.getBuriedness() > 0) { + this.priorityHumans.add(mpf.getAgentID()); + } else { + this.priorityHumans.remove(mpf.getAgentID()); + this.targetHumans.remove(mpf.getAgentID()); + } + } + } + for (CommunicationMessage message : messageManager + .getReceivedMessageList(MessageAmbulanceTeam.class)) { + MessageAmbulanceTeam mat = (MessageAmbulanceTeam) message; + MessageUtil.reflectMessage(this.worldInfo, mat); + if (mat.isBuriednessDefined() && mat.getBuriedness() > 0) { + this.priorityHumans.add(mat.getAgentID()); + } else { + this.priorityHumans.remove(mat.getAgentID()); + this.targetHumans.remove(mat.getAgentID()); + } + AmbulanceTeamInfo info = this.ambulanceTeamInfoMap.get(mat.getAgentID()); + if (info == null) { + info = new AmbulanceTeamInfo(mat.getAgentID()); + } + if (currentTime >= info.commandTime + 2) { + this.ambulanceTeamInfoMap.put(mat.getAgentID(), this.update(info, mat)); + } + } + for (CommunicationMessage message : messageManager + .getReceivedMessageList(CommandAmbulance.class)) { + CommandAmbulance command = (CommandAmbulance) message; + if (command.getAction() == CommandAmbulance.ACTION_RESCUE + && command.isBroadcast()) { + this.priorityHumans.add(command.getTargetID()); + this.targetHumans.add(command.getTargetID()); + } else if (command.getAction() == CommandAmbulance.ACTION_LOAD + && command.isBroadcast()) { + this.priorityHumans.add(command.getTargetID()); + this.targetHumans.add(command.getTargetID()); + } + } + for (CommunicationMessage message : messageManager + .getReceivedMessageList(MessageReport.class)) { + MessageReport report = (MessageReport) message; + AmbulanceTeamInfo info = this.ambulanceTeamInfoMap + .get(report.getSenderID()); + if (info != null && report.isDone()) { + info.canNewAction = true; + this.priorityHumans.remove(info.target); + this.targetHumans.remove(info.target); + info.target = null; + this.ambulanceTeamInfoMap.put(info.agentID, info); + } + } + return this; + } + + + private Map + convert(Map map) { + Map result = new HashMap<>(); + for (EntityID id : map.keySet()) { + AmbulanceTeamInfo info = map.get(id); + if (info != null && info.target != null) { + result.put(id, info.target); + } + } + return result; + } + + + private List + getActionAgents(Map map) { + List result = new ArrayList<>(); + for (StandardEntity entity : this.worldInfo + .getEntitiesOfType(StandardEntityURN.POLICE_FORCE)) { + AmbulanceTeamInfo info = map.get(entity.getID()); + if (info != null && info.canNewAction + && ((AmbulanceTeam) entity).isPositionDefined()) { + result.add(entity); + } + } + return result; + } + + + private AmbulanceTeamInfo update(AmbulanceTeamInfo info, + MessageAmbulanceTeam message) { + if (message.isBuriednessDefined() && message.getBuriedness() > 0) { + info.canNewAction = false; + if (info.target != null) { + this.targetHumans.add(info.target); + info.target = null; + } + return info; + } + if (message.getAction() == MessageAmbulanceTeam.ACTION_REST) { + info.canNewAction = true; + if (info.target != null) { + this.targetHumans.add(info.target); + info.target = null; + } + } else if (message.getAction() == MessageAmbulanceTeam.ACTION_MOVE) { + if (message.getTargetID() != null) { + StandardEntity entity = this.worldInfo.getEntity(message.getTargetID()); + if (entity != null) { + if (entity instanceof Area) { + if (entity.getStandardURN() == REFUGE) { + info.canNewAction = false; + return info; + } + StandardEntity targetEntity = this.worldInfo.getEntity(info.target); + if (targetEntity != null) { + if (targetEntity instanceof Human) { + targetEntity = this.worldInfo.getPosition((Human) targetEntity); + if (targetEntity == null) { + this.priorityHumans.remove(info.target); + this.targetHumans.remove(info.target); + info.canNewAction = true; + info.target = null; + return info; + } + } + if (targetEntity.getID().getValue() == entity.getID() + .getValue()) { + info.canNewAction = false; + } else { + info.canNewAction = true; + if (info.target != null) { + this.targetHumans.add(info.target); + info.target = null; + } + } + } else { + info.canNewAction = true; + info.target = null; + } + return info; + } else if (entity instanceof Human) { + if (entity.getID().getValue() == info.target.getValue()) { + info.canNewAction = false; + } else { + info.canNewAction = true; + this.targetHumans.add(info.target); + this.targetHumans.add(entity.getID()); + info.target = null; + } + return info; + } + } + } + info.canNewAction = true; + if (info.target != null) { + this.targetHumans.add(info.target); + info.target = null; + } + } else if (message.getAction() == MessageAmbulanceTeam.ACTION_RESCUE) { + info.canNewAction = true; + if (info.target != null) { + this.targetHumans.add(info.target); + info.target = null; + } + } else if (message.getAction() == MessageAmbulanceTeam.ACTION_LOAD) { + info.canNewAction = false; + } else if (message.getAction() == MessageAmbulanceTeam.ACTION_UNLOAD) { + info.canNewAction = true; + this.priorityHumans.remove(info.target); + this.targetHumans.remove(info.target); + info.target = null; + } + return info; + } + + private class AmbulanceTeamInfo { + + EntityID agentID; + EntityID target; + boolean canNewAction; + int commandTime; + + AmbulanceTeamInfo(EntityID id) { + agentID = id; + target = null; + canNewAction = true; + commandTime = -1; + } + } + + private class DistanceSorter implements Comparator { + + private StandardEntity reference; + private WorldInfo worldInfo; + + DistanceSorter(WorldInfo wi, StandardEntity reference) { + this.reference = reference; + this.worldInfo = wi; + } + + + public int compare(StandardEntity a, StandardEntity b) { + int d1 = this.worldInfo.getDistance(this.reference, a); + int d2 = this.worldInfo.getDistance(this.reference, b); + return d1 - d2; + } + } +} \ No newline at end of file diff --git a/src/main/java/autumn_2023/module/complex/CentralizedControlATHumanDetector.java b/src/main/java/autumn_2023/module/complex/CentralizedControlATHumanDetector.java new file mode 100755 index 0000000..3c9d8f1 --- /dev/null +++ b/src/main/java/autumn_2023/module/complex/CentralizedControlATHumanDetector.java @@ -0,0 +1,255 @@ +package autumn_2023.module.complex; + +import static rescuecore2.standard.entities.StandardEntityURN.AMBULANCE_TEAM; +import static rescuecore2.standard.entities.StandardEntityURN.CIVILIAN; +import static rescuecore2.standard.entities.StandardEntityURN.REFUGE; + +import adf.core.agent.action.Action; +import adf.core.agent.communication.MessageManager; +import adf.core.agent.communication.standard.bundle.StandardMessageBundle; +import adf.core.agent.communication.standard.bundle.StandardMessagePriority; +import adf.core.agent.communication.standard.bundle.information.MessageBuilding; +import adf.core.agent.communication.standard.bundle.information.MessageCivilian; +import adf.core.agent.develop.DevelopData; +import adf.core.agent.info.AgentInfo; +import adf.core.agent.info.ScenarioInfo; +import adf.core.agent.info.WorldInfo; +import adf.core.agent.module.ModuleManager; +import adf.core.component.communication.CommunicationMessage; +import adf.core.component.module.algorithm.Clustering; +import adf.core.component.module.complex.HumanDetector; +import adf.core.debug.DefaultLogger; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Comparator; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.apache.log4j.Logger; + +import rescuecore2.standard.entities.Building; +import rescuecore2.standard.entities.Civilian; +import rescuecore2.standard.entities.Human; +import rescuecore2.standard.entities.StandardEntity; +import rescuecore2.standard.entities.StandardEntityURN; +import rescuecore2.worldmodel.EntityID; + +public class CentralizedControlATHumanDetector extends HumanDetector { + + private Clustering clustering; + + private EntityID result; + + private Logger logger; + + public CentralizedControlATHumanDetector(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) { + super(ai, wi, si, moduleManager, developData); + logger = DefaultLogger.getLogger(agentInfo.me()); + this.clustering = moduleManager.getModule("SampleHumanDetector.Clustering", + "adf.impl.module.algorithm.KMeansClustering"); + registerModule(this.clustering); + } + + + @Override + public HumanDetector updateInfo(MessageManager messageManager) { + logger.debug("Time:" + agentInfo.getTime()); + super.updateInfo(messageManager); + + int time = this.agentInfo.getTime(); + Action action = this.agentInfo.getExecutedAction(time - 1); + //System.out.println("time:"+time+" myid:"+this.agentInfo.getID()+" -1step action:"+action); + + int[] chan = messageManager.getChannels(); + String str = ""; + for(int i=0; i mes = messageManager.getReceivedMessageList(MessageCivilian.class); + + Set changes = this.worldInfo.getChanged().getChangedEntities(); + for(EntityID id : changes){ + StandardEntity entity = this.worldInfo.getEntity(id); + if(!(entity instanceof Civilian)) continue; + Civilian civ = (Civilian) entity; + + if(!this.isNeedRescueHuman(civ)){ + continue; + } + StandardMessagePriority level = StandardMessagePriority.HIGH; + //MessageCivilian civ_mes = new MessageDamageCivilian(true, level, civ); + MessageCivilian civ_mes = new MessageCivilian(true, level, civ); + //Building building = (Building) this.worldInfo.getPosition(id); + + //System.out.println("time:"+time+" myid:"+this.agentInfo.getID()+" civ:"+civ.getID()+" level:"+civ_mes.getSendingPriority()); + + //messageManager.addMessage(building_mes,true); + messageManager.addMessage(civ_mes,true); + } + + return this; + } + + + @Override + public HumanDetector calc() { + Human transportHuman = this.agentInfo.someoneOnBoard(); + if (transportHuman != null) { + logger.debug("someoneOnBoard:" + transportHuman); + this.result = transportHuman.getID(); + return this; + } + if (this.result != null) { + Human target = (Human) this.worldInfo.getEntity(this.result); + if (!isValidHuman(target)) { + logger.debug("Invalid Human:" + target + " ==>reset target"); + this.result = null; + } + } + if (this.result == null) { + this.result = calcTarget(); + } + return this; + } + + + private EntityID calcTarget() { + List rescueTargets = filterRescueTargets( + this.worldInfo.getEntitiesOfType(CIVILIAN)); + List rescueTargetsInCluster = filterInCluster(rescueTargets); + List targets = rescueTargetsInCluster; + if (targets.isEmpty()) + targets = rescueTargets; + + logger.debug("Targets:" + targets); + if (!targets.isEmpty()) { + targets.sort(new DistanceSorter(this.worldInfo, this.agentInfo.me())); + Human selected = targets.get(0); + logger.debug("Selected:" + selected); + return selected.getID(); + } + + return null; + } + + + @Override + public EntityID getTarget() { + return this.result; + } + + + private List + filterRescueTargets(Collection list) { + List rescueTargets = new ArrayList<>(); + for (StandardEntity next : list) { + if (!(next instanceof Human)) + continue; + Human h = (Human) next; + if (!isValidHuman(h)) + continue; + if (h.getBuriedness() == 0) + continue; + rescueTargets.add(h); + } + return rescueTargets; + } + + + private List + filterInCluster(Collection entities) { + int clusterIndex = clustering.getClusterIndex(this.agentInfo.getID()); + List filter = new ArrayList<>(); + HashSet inCluster = new HashSet<>( + clustering.getClusterEntities(clusterIndex)); + for (StandardEntity next : entities) { + if (!(next instanceof Human)) + continue; + Human h = (Human) next; + if (!h.isPositionDefined()) + continue; + StandardEntity position = this.worldInfo.getPosition(h); + if (position == null) + continue; + if (!inCluster.contains(position)) + continue; + filter.add(h); + } + return filter; + + } + + private class DistanceSorter implements Comparator { + + private StandardEntity reference; + private WorldInfo worldInfo; + + DistanceSorter(WorldInfo wi, StandardEntity reference) { + this.reference = reference; + this.worldInfo = wi; + } + + + public int compare(StandardEntity a, StandardEntity b) { + int d1 = this.worldInfo.getDistance(this.reference, a); + int d2 = this.worldInfo.getDistance(this.reference, b); + return d1 - d2; + } + } + + private boolean isValidHuman(StandardEntity entity) { + if (entity == null) + return false; + if (!(entity instanceof Human)) + return false; + + Human target = (Human) entity; + if (!target.isHPDefined() || target.getHP() == 0) + return false; + if (!target.isPositionDefined()) + return false; + if (!target.isDamageDefined() || target.getDamage() == 0) + return false; + if (!target.isBuriednessDefined()) + return false; + + StandardEntity position = worldInfo.getPosition(target); + if (position == null) + return false; + + StandardEntityURN positionURN = position.getStandardURN(); + if (positionURN == REFUGE || positionURN == AMBULANCE_TEAM) + return false; + + return true; + } + + private boolean isNeedRescueHuman(StandardEntity entity){ + if (entity == null) + return false; + if (!(entity instanceof Human)) + return false; + + Human target = (Human) entity; + if (!target.isHPDefined() || target.getHP() == 0) + return false; + if (!target.isPositionDefined()) + return false; + if (!target.isBuriednessDefined() || target.getBuriedness() == 0) + return false; + + StandardEntity position = worldInfo.getPosition(target); + if (position == null) + return false; + + StandardEntityURN positionURN = position.getStandardURN(); + if (positionURN == REFUGE || positionURN == AMBULANCE_TEAM) + return false; + + return true; + } +} \ No newline at end of file diff --git a/src/main/java/autumn_2023/module/complex/CentralizedControlFBAllocator.java b/src/main/java/autumn_2023/module/complex/CentralizedControlFBAllocator.java new file mode 100755 index 0000000..3751e41 --- /dev/null +++ b/src/main/java/autumn_2023/module/complex/CentralizedControlFBAllocator.java @@ -0,0 +1,273 @@ +package autumn_2023.module.complex; + +import autumn_2023.module.algorithm.Hungarian; +import adf.core.agent.communication.MessageManager; +import adf.core.agent.communication.standard.bundle.MessageUtil; +import adf.core.agent.communication.standard.bundle.StandardMessageBundle; +import adf.core.agent.communication.standard.bundle.StandardMessagePriority; +import adf.core.agent.communication.standard.bundle.centralized.MessageReport; +import adf.core.agent.communication.standard.bundle.information.MessageBuilding; +import adf.core.agent.communication.standard.bundle.information.MessageCivilian; +import adf.core.agent.communication.standard.bundle.information.MessageFireBrigade; +import adf.core.agent.develop.DevelopData; +import adf.core.agent.info.AgentInfo; +import adf.core.agent.info.ScenarioInfo; +import adf.core.agent.info.WorldInfo; +import adf.core.agent.module.ModuleManager; +import adf.core.agent.precompute.PrecomputeData; +import adf.core.component.communication.CommunicationMessage; +import adf.core.component.module.complex.FireTargetAllocator; + +import static rescuecore2.standard.entities.StandardEntityURN.AMBULANCE_TEAM; +import static rescuecore2.standard.entities.StandardEntityURN.REFUGE; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collector; +import java.util.stream.Collectors; + +import rescuecore2.standard.entities.Building; +import rescuecore2.standard.entities.FireBrigade; +import rescuecore2.standard.entities.Human; +import rescuecore2.standard.entities.StandardEntity; +import rescuecore2.standard.entities.StandardEntityURN; +import rescuecore2.worldmodel.EntityID; + +public class CentralizedControlFBAllocator extends FireTargetAllocator { + + private Map fireBrigadeInfoMap; + private Set taskSet; + private Map allocateTaskMap; + private Set finishEntityIDs; + + public CentralizedControlFBAllocator(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) { + super(ai, wi, si, moduleManager, developData); + this.fireBrigadeInfoMap = new HashMap<>(); + this.taskSet = new HashSet<>(); + this.allocateTaskMap = new HashMap<>(); + this.finishEntityIDs = new HashSet<>(); + } + + + @Override + public FireTargetAllocator resume(PrecomputeData precomputeData) { + super.resume(precomputeData); + if (this.getCountResume() >= 2) { + return this; + } + for (EntityID id : this.worldInfo + .getEntityIDsOfType(StandardEntityURN.FIRE_BRIGADE)) { + this.fireBrigadeInfoMap.put(id, new FireBrigadeInfo(id)); + } + return this; + } + + + @Override + public FireTargetAllocator preparate() { + super.preparate(); + if (this.getCountPrecompute() >= 2) { + return this; + } + for (EntityID id : this.worldInfo + .getEntityIDsOfType(StandardEntityURN.FIRE_BRIGADE)) { + this.fireBrigadeInfoMap.put(id, new FireBrigadeInfo(id)); + } + return this; + } + + + @Override + public Map getResult() { + return this.convert(this.fireBrigadeInfoMap); + } + + private Map convert(Map map) { + Map result = new HashMap<>(); + for (EntityID id : map.keySet()) { + FireBrigadeInfo info = map.get(id); + if (info != null && info.target != null) { + result.put(id, info.target); + } + } + return result; + } + + @Override + public FireTargetAllocator calc() { + if(this.agentInfo.getTime() < scenarioInfo.getKernelAgentsIgnoreuntil()) return this; + + if(this.fireBrigadeInfoMap.keySet().size()==0||this.taskSet.size()==0) return this; + + int row = this.fireBrigadeInfoMap.keySet().size(); + int col = this.taskSet.size(); + boolean reverse = false; + List rowlist = new ArrayList<>(this.fireBrigadeInfoMap.keySet()); + List collist = new ArrayList<>(this.taskSet); + + if(row > col){ + reverse = true; + int tmp = row; + row = col; + col = tmp; + List tmplist = rowlist; + rowlist = collist; + collist = tmplist; + } + + int[][] costs = new int[row][col]; + for(int i = 0; i tmplist = rowlist; + rowlist = collist; + collist = tmplist; + int[] reverse_list = new int[row]; + for(int i = 0; i mescivlist = messageManager.getReceivedMessageList(MessageCivilian.class); + for(CommunicationMessage mes : mescivlist){ + MessageCivilian mesciv = (MessageCivilian) mes; + StandardMessagePriority level = mesciv.getSendingPriority(); + //if(level != StandardMessagePriority.HIGH) continue; + MessageUtil.reflectMessage(this.worldInfo, mesciv); + //boolean mdc = (mesciv instanceof MessageDamageCivilian); + //if(!(mesciv instanceof MessageDamageCivilian)) continue; + EntityID civID = mesciv.getAgentID(); + StandardEntity civ = this.worldInfo.getEntity(civID); + if(!this.isNeedRescueHuman(civ)) continue; + if(finishEntityIDs.contains(civID)) continue; + EntityID buildingID = mesciv.getPosition(); + //int time = this.agentInfo.getTime(); + //System.out.println("[FBCenter] time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" civID:"+civID+" buildingID:"+buildingID+" size:"+mesciv.getByteArraySize()+" mdc:"+mdc); + System.out.println("[FBCenter] time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" civID:"+civID+" buildingID:"+buildingID+" size:"+mesciv.getByteArraySize()+" isNeedRescueHuman:"+this.isNeedRescueHuman(civ)); + this.taskSet.add(civID); + }*/ + + Set recievetasks = messageManager.getReceivedMessageList(MessageCivilian.class).stream() + .map(e -> { + MessageCivilian mesciv = (MessageCivilian) e; + MessageUtil.reflectMessage(this.worldInfo, mesciv); + return(mesciv); + }) + .map(MessageCivilian::getAgentID) + .filter(e -> this.isNeedRescueHuman(this.worldInfo.getEntity(e))) + .filter(e -> !(finishEntityIDs.contains(e))) + .collect(Collectors.toSet()); + + this.taskSet.addAll(recievetasks); + + List mesfblist = messageManager.getReceivedMessageList(MessageFireBrigade.class); + for(CommunicationMessage mes : mesfblist){ + MessageFireBrigade mesfb = (MessageFireBrigade) mes; + MessageUtil.reflectMessage(this.worldInfo, mesfb); + EntityID fbID = mesfb.getAgentID(); + System.out.println("[FBCenter] time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" fbID:"+fbID+" posi:"+mesfb.getPosition()+" Action:"+mesfb.getAction()+" level:"+mesfb.getSendingPriority()+" size:"+mesfb.getByteArraySize()); + FireBrigadeInfo fbi = this.fireBrigadeInfoMap.get(fbID); + fbi.setInfo(mesfb.getTargetID(), mesfb.getPosition(), mesfb.getBuriedness(), + (mesfb.getAction() == MessageFireBrigade.ACTION_RESCUE) ? false : true, this.agentInfo.getTime()); + } + + List reportlist = messageManager.getReceivedMessageList(MessageReport.class); + for(CommunicationMessage mes : reportlist){ + MessageReport report = (MessageReport) mes; + EntityID targetID = report.getFromID(); + //System.out.println("[FBCenter] report time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" fbID:"+senderID+" tagID:"+targetID); + boolean done = report.isDone(); + if(done) { + this.finishEntityIDs.add(targetID); + this.taskSet.remove(targetID); + } + } + + return this; + } + + private boolean isNeedRescueHuman(StandardEntity entity){ + if (entity == null) + return false; + if (!(entity instanceof Human)) + return false; + + Human target = (Human) entity; + if (!target.isHPDefined() || target.getHP() == 0) + return false; + if (!target.isPositionDefined()) + return false; + if (!target.isBuriednessDefined() || target.getBuriedness() == 0) + return false; + + StandardEntity position = worldInfo.getPosition(target); + if (position == null) + return false; + + StandardEntityURN positionURN = position.getStandardURN(); + if (positionURN == REFUGE || positionURN == AMBULANCE_TEAM) + return false; + + return true; + } + + private class FireBrigadeInfo { + + EntityID agentID; + EntityID prevtarget; + EntityID position; + int buriedness; + EntityID target; + boolean canNewAction; + int commandTime; + + FireBrigadeInfo(EntityID id) { + agentID = id; + prevtarget = null; + position = null; + buriedness = -1; + target = null; + canNewAction = true; + commandTime = -1; + } + public void setInfo(EntityID prevtarget, EntityID position, int buriedness, boolean canNewAction, int commandTime){ + this.prevtarget = prevtarget; + this.position = position; + this.buriedness = buriedness; + this.canNewAction = canNewAction; + this.commandTime = commandTime; + } + } +} \ No newline at end of file diff --git a/src/main/java/autumn_2023/module/complex/CentralizedControlFBHumanDetector.java b/src/main/java/autumn_2023/module/complex/CentralizedControlFBHumanDetector.java new file mode 100755 index 0000000..f563c69 --- /dev/null +++ b/src/main/java/autumn_2023/module/complex/CentralizedControlFBHumanDetector.java @@ -0,0 +1,241 @@ +package autumn_2023.module.complex; + +import static rescuecore2.standard.entities.StandardEntityURN.AMBULANCE_TEAM; +import static rescuecore2.standard.entities.StandardEntityURN.CIVILIAN; +import static rescuecore2.standard.entities.StandardEntityURN.REFUGE; + +import adf.core.agent.action.Action; +import adf.core.agent.communication.MessageManager; +import adf.core.agent.communication.standard.bundle.StandardMessageBundle; +import adf.core.agent.communication.standard.bundle.StandardMessagePriority; +import adf.core.agent.communication.standard.bundle.information.MessageBuilding; +import adf.core.agent.communication.standard.bundle.information.MessageCivilian; +import adf.core.agent.communication.standard.bundle.information.MessageFireBrigade; +import adf.core.agent.develop.DevelopData; +import adf.core.agent.info.AgentInfo; +import adf.core.agent.info.ScenarioInfo; +import adf.core.agent.info.WorldInfo; +import adf.core.agent.module.ModuleManager; +import adf.core.component.module.algorithm.Clustering; +import adf.core.component.module.complex.HumanDetector; +import adf.core.debug.DefaultLogger; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Comparator; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.apache.log4j.Logger; + +import rescuecore2.standard.entities.Building; +import rescuecore2.standard.entities.Civilian; +import rescuecore2.standard.entities.FireBrigade; +import rescuecore2.standard.entities.Human; +import rescuecore2.standard.entities.StandardEntity; +import rescuecore2.standard.entities.StandardEntityURN; +import rescuecore2.worldmodel.EntityID; + +public class CentralizedControlFBHumanDetector extends HumanDetector { + + private Clustering clustering; + + private EntityID result; + + private Logger logger; + + private MessageManager mm; + + public CentralizedControlFBHumanDetector(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) { + super(ai, wi, si, moduleManager, developData); + logger = DefaultLogger.getLogger(agentInfo.me()); + this.clustering = moduleManager.getModule("SampleHumanDetector.Clustering", + "adf.impl.module.algorithm.KMeansClustering"); + registerModule(this.clustering); + } + + + @Override + public HumanDetector updateInfo(MessageManager messageManager) { + logger.debug("Time:" + agentInfo.getTime()); + super.updateInfo(messageManager); + + this.mm = messageManager; + + int time = this.agentInfo.getTime(); + + int[] chan = messageManager.getChannels(); + String str = ""; + for(int i=0; i changes = this.worldInfo.getChanged().getChangedEntities(); + for(EntityID id : changes){ + StandardEntity entity = this.worldInfo.getEntity(id); + if(!(entity instanceof Civilian)) continue; + Civilian civ = (Civilian) entity; + + if(!this.isNeedRescueHuman(civ)){ + continue; + } + StandardMessagePriority level = StandardMessagePriority.HIGH; + MessageCivilian civ_mes = new MessageCivilian(true, level, civ); + + //System.out.println("time:"+time+" myid:"+this.agentInfo.getID()+" civ:"+civ.getID()+" level:"+civ_mes.getSendingPriority()); + + messageManager.addMessage(civ_mes,true); + } + + return this; + } + + + @Override + public HumanDetector calc() { + if(this.agentInfo.getTime() < scenarioInfo.getKernelAgentsIgnoreuntil()) return this; + + this.result = null; + + return this; + } + + + private EntityID calcTarget() { + List rescueTargets = filterRescueTargets( + this.worldInfo.getEntitiesOfType(CIVILIAN)); + List rescueTargetsInCluster = filterInCluster(rescueTargets); + List targets = rescueTargetsInCluster; + if (targets.isEmpty()) + targets = rescueTargets; + + logger.debug("Targets:" + targets); + if (!targets.isEmpty()) { + targets.sort(new DistanceSorter(this.worldInfo, this.agentInfo.me())); + Human selected = targets.get(0); + logger.debug("Selected:" + selected); + return selected.getID(); + } + + return null; + } + + + @Override + public EntityID getTarget() { + return this.result; + } + + + private List + filterRescueTargets(Collection list) { + List rescueTargets = new ArrayList<>(); + for (StandardEntity next : list) { + if (!(next instanceof Human)) + continue; + Human h = (Human) next; + if (!isValidHuman(h)) + continue; + if (h.getBuriedness() == 0) + continue; + rescueTargets.add(h); + } + return rescueTargets; + } + + + private List + filterInCluster(Collection entities) { + int clusterIndex = clustering.getClusterIndex(this.agentInfo.getID()); + List filter = new ArrayList<>(); + HashSet inCluster = new HashSet<>( + clustering.getClusterEntities(clusterIndex)); + for (StandardEntity next : entities) { + if (!(next instanceof Human)) + continue; + Human h = (Human) next; + if (!h.isPositionDefined()) + continue; + StandardEntity position = this.worldInfo.getPosition(h); + if (position == null) + continue; + if (!inCluster.contains(position)) + continue; + filter.add(h); + } + return filter; + + } + + private class DistanceSorter implements Comparator { + + private StandardEntity reference; + private WorldInfo worldInfo; + + DistanceSorter(WorldInfo wi, StandardEntity reference) { + this.reference = reference; + this.worldInfo = wi; + } + + + public int compare(StandardEntity a, StandardEntity b) { + int d1 = this.worldInfo.getDistance(this.reference, a); + int d2 = this.worldInfo.getDistance(this.reference, b); + return d1 - d2; + } + } + + private boolean isValidHuman(StandardEntity entity) { + if (entity == null) + return false; + if (!(entity instanceof Human)) + return false; + + Human target = (Human) entity; + if (!target.isHPDefined() || target.getHP() == 0) + return false; + if (!target.isPositionDefined()) + return false; + if (!target.isDamageDefined() || target.getDamage() == 0) + return false; + if (!target.isBuriednessDefined()) + return false; + + StandardEntity position = worldInfo.getPosition(target); + if (position == null) + return false; + + StandardEntityURN positionURN = position.getStandardURN(); + if (positionURN == REFUGE || positionURN == AMBULANCE_TEAM) + return false; + + return true; + } + + private boolean isNeedRescueHuman(StandardEntity entity){ + if (entity == null) + return false; + if (!(entity instanceof Human)) + return false; + + Human target = (Human) entity; + if (!target.isHPDefined() || target.getHP() == 0) + return false; + if (!target.isPositionDefined()) + return false; + if (!target.isBuriednessDefined() || target.getBuriedness() == 0) + return false; + + StandardEntity position = worldInfo.getPosition(target); + if (position == null) + return false; + + StandardEntityURN positionURN = position.getStandardURN(); + if (positionURN == REFUGE || positionURN == AMBULANCE_TEAM) + return false; + + return true; + } +} \ No newline at end of file diff --git a/src/main/java/autumn_2023/module/complex/SampleAmbulanceTargetAllocator.java b/src/main/java/autumn_2023/module/complex/SampleAmbulanceTargetAllocator.java deleted file mode 100755 index 453de8a..0000000 --- a/src/main/java/autumn_2023/module/complex/SampleAmbulanceTargetAllocator.java +++ /dev/null @@ -1,53 +0,0 @@ -package autumn_2023.module.complex; - -import adf.core.agent.communication.MessageManager; -import adf.core.agent.develop.DevelopData; -import adf.core.agent.info.AgentInfo; -import adf.core.agent.info.ScenarioInfo; -import adf.core.agent.info.WorldInfo; -import adf.core.agent.module.ModuleManager; -import adf.core.agent.precompute.PrecomputeData; -import adf.core.component.module.complex.AmbulanceTargetAllocator; -import java.util.HashMap; -import java.util.Map; -import rescuecore2.worldmodel.EntityID; - -public class SampleAmbulanceTargetAllocator extends AmbulanceTargetAllocator { - - public SampleAmbulanceTargetAllocator(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) { - super(ai, wi, si, moduleManager, developData); - } - - - @Override - public AmbulanceTargetAllocator resume(PrecomputeData precomputeData) { - super.resume(precomputeData); - return this; - } - - - @Override - public AmbulanceTargetAllocator preparate() { - super.preparate(); - return this; - } - - - @Override - public Map getResult() { - return new HashMap<>(); - } - - - @Override - public AmbulanceTargetAllocator calc() { - return this; - } - - - @Override - public AmbulanceTargetAllocator updateInfo(MessageManager messageManager) { - super.updateInfo(messageManager); - return this; - } -} \ No newline at end of file diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..c6c7052 --- /dev/null +++ b/start.sh @@ -0,0 +1,35 @@ +#!/bin/sh + +LOADER="adf.impl.DefaultLoader" +PARAMS=$* + +cd `dirname $0` + +if [ ! -z "$1" ]; then + ./gradlew launch --args="${LOADER} ${PARAMS} -pre 0" +else + echo "Options:" + echo "-tn Team name" + echo "-t [FB],[FS],[PF],[PO],[AT],[AC] Number of agents" + echo "-fb [FB] Number of FireBrigade" + echo "-fs [FS] Number of FireStation" + echo "-pf [PF] Number of PoliceForce" + echo "-po [PO] Number of PoliceOffice" + echo "-at [AT] Number of AmbulanceTeam" + echo "-ac [AC] Number of AmbulanceCentre" + echo "-s [HOST]:[PORT] RCRS server host and port" + echo "-h [HOST] RCRS server host (port:27931)" + echo "-pre [0|1] Precompute flag" + echo "-d [0|1] Debug flag" + echo "-dev [0|1] Development mode" + echo "-mc [FILE] ModuleConfig file name" + echo "-md [JSON] ModuleConfig JSON" + echo "-df [FILE] DevelopData JSON file" + echo "-dd [JSON] DevelopData JSON" + echo "-all [alias] -t -1,-1,-1,-1,-1,-1" + echo "-allp [alias] -t 1,0,1,0,1,0," + echo "-local [alias] -h localhost" + echo "-precompute [alias] -pre 1" + echo "-debug [alias] -d 1" + echo "-develop [alias] -dev 1" +fi diff --git a/submit.sh b/submit.sh new file mode 100755 index 0000000..39604c8 --- /dev/null +++ b/submit.sh @@ -0,0 +1,10 @@ +#!/bin/sh +TEAM=ait +mkdir ../${TEAM} +cp -r src ../${TEAM} +cp -r lib ../${TEAM} +mv ../${TEAM}/lib ../${TEAM}/library +cp -r config ../${TEAM} +cp -r data ../${TEAM} +cd ../${TEAM} +tar cvzf ait.tgz * -- GitLab From a830e49c5e3592a17f342c672ade32bd795242d4 Mon Sep 17 00:00:00 2001 From: k20066 <87357429+k20066@users.noreply.github.com> Date: Fri, 3 Nov 2023 01:27:43 +0900 Subject: [PATCH 2/9] remove unnecessary module --- config/module.cfg | 13 +- .../CentralizedControlCommandExecutorA.java | 311 --------------- .../CentralizedControlCommandPickerA.java | 80 ---- .../module/comm/DefaultChannelSubscriber.java | 100 ----- .../CentralizedControlATAllocator.java | 371 ------------------ 5 files changed, 5 insertions(+), 870 deletions(-) delete mode 100644 src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorA.java delete mode 100644 src/main/java/autumn_2023/centralized/CentralizedControlCommandPickerA.java delete mode 100644 src/main/java/autumn_2023/module/comm/DefaultChannelSubscriber.java delete mode 100755 src/main/java/autumn_2023/module/complex/CentralizedControlATAllocator.java diff --git a/config/module.cfg b/config/module.cfg index fae5e10..7827515 100755 --- a/config/module.cfg +++ b/config/module.cfg @@ -3,7 +3,7 @@ DefaultTacticsAmbulanceTeam.HumanDetector : autumn_2023.module.complex.Centraliz DefaultTacticsAmbulanceTeam.Search : sample_team.module.complex.SampleSearch DefaultTacticsAmbulanceTeam.ExtActionTransport : adf.impl.extaction.DefaultExtActionTransport DefaultTacticsAmbulanceTeam.ExtActionMove : adf.impl.extaction.DefaultExtActionMove -#DefaultTacticsAmbulanceTeam.CommandExecutorAmbulance : adf.impl.centralized.DefaultCommandExecutorAmbulance +DefaultTacticsAmbulanceTeam.CommandExecutorAmbulance : adf.impl.centralized.DefaultCommandExecutorAmbulance DefaultTacticsAmbulanceTeam.CommandExecutorAmbulance : autumn_2023.centralized.CentralizedControlCommandExecutorA DefaultTacticsAmbulanceTeam.CommandExecutorScout : adf.impl.centralized.DefaultCommandExecutorScout @@ -24,13 +24,10 @@ DefaultTacticsPoliceForce.CommandExecutorPolice : adf.impl.centralized.DefaultCo DefaultTacticsPoliceForce.CommandExecutorScout : adf.impl.centralized.DefaultCommandExecutorScoutPolice ## DefaultTacticsAmbulanceCentre -#DefaultTacticsAmbulanceCentre.TargetAllocator : sample_team.module.complex.SampleAmbulanceTargetAllocator -#DefaultTacticsAmbulanceCentre.CommandPicker : adf.impl.centralized.DefaultCommandPickerAmbulance -DefaultTacticsAmbulanceCentre.TargetAllocator : autumn_2023.module.complex.CentralizedControlATAllocator -DefaultTacticsAmbulanceCentre.CommandPicker : autumn_2023.centralized.CentralizedControlCommandPickerA +DefaultTacticsAmbulanceCentre.TargetAllocator : sample_team.module.complex.SampleAmbulanceTargetAllocator +DefaultTacticsAmbulanceCentre.CommandPicker : adf.impl.centralized.DefaultCommandPickerAmbulance ## DefaultTacticsFireStation -#DefaultTacticsFireStation.TargetAllocator : sample_team.module.complex.SampleFireTargetAllocator DefaultTacticsFireStation.CommandPicker : adf.impl.centralized.DefaultCommandPickerFire TacticsFireStation.TargetAllocator : autumn_2023.module.complex.CentralizedControlFBAllocator @@ -96,8 +93,8 @@ DefaultCommandExecutorScoutPolice.PathPlanning : adf.impl.module.algorithm.Dijks DefaultCommandExecutorScoutPolice.ExtActionClear : adf.impl.extaction.DefaultExtActionClear ## MessageManager -MessageManager.PlatoonChannelSubscriber : autumn_2023.module.comm.DefaultChannelSubscriber -MessageManager.CenterChannelSubscriber : autumn_2023.module.comm.DefaultChannelSubscriber +MessageManager.PlatoonChannelSubscriber : adf.impl.module.comm.DefaultChannelSubscriber +MessageManager.CenterChannelSubscriber : adf.impl.module.comm.DefaultChannelSubscriber MessageManager.PlatoonMessageCoordinator : autumn_2023.module.comm.CentralizedControlMessageCoordinator MessageManager.CenterMessageCoordinator : autumn_2023.module.comm.CentralizedControlMessageCoordinator diff --git a/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorA.java b/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorA.java deleted file mode 100644 index 83841ed..0000000 --- a/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorA.java +++ /dev/null @@ -1,311 +0,0 @@ -package autumn_2023.centralized; - -import static rescuecore2.standard.entities.StandardEntityURN.AMBULANCE_TEAM; -import static rescuecore2.standard.entities.StandardEntityURN.CIVILIAN; -import static rescuecore2.standard.entities.StandardEntityURN.REFUGE; -import adf.core.agent.action.common.ActionMove; -import adf.core.agent.action.common.ActionRest; -import adf.core.agent.communication.MessageManager; -import adf.core.agent.communication.standard.bundle.centralized.CommandAmbulance; -import adf.core.agent.communication.standard.bundle.centralized.MessageReport; -import adf.core.agent.develop.DevelopData; -import adf.core.agent.info.AgentInfo; -import adf.core.agent.info.ScenarioInfo; -import adf.core.agent.info.WorldInfo; -import adf.core.agent.module.ModuleManager; -import adf.core.agent.precompute.PrecomputeData; -import adf.core.component.centralized.CommandExecutor; -import adf.core.component.extaction.ExtAction; -import adf.core.component.module.algorithm.PathPlanning; -import java.util.Collection; -import java.util.List; -import java.util.Objects; -import rescuecore2.standard.entities.Area; -import rescuecore2.standard.entities.Human; -import rescuecore2.standard.entities.StandardEntity; -import rescuecore2.worldmodel.EntityID; - -public class CentralizedControlCommandExecutorA - extends CommandExecutor { - - private static final int ACTION_UNKNOWN = -1; - private static final int ACTION_REST = CommandAmbulance.ACTION_REST; - private static final int ACTION_MOVE = CommandAmbulance.ACTION_MOVE; - private static final int ACTION_RESCUE = CommandAmbulance.ACTION_RESCUE; - private static final int ACTION_LOAD = CommandAmbulance.ACTION_LOAD; - private static final int ACTION_UNLOAD = CommandAmbulance.ACTION_UNLOAD; - private static final int ACTION_AUTONOMY = CommandAmbulance.ACTION_AUTONOMY; - - private PathPlanning pathPlanning; - - private ExtAction actionTransport; - private ExtAction actionExtMove; - - private int commandType; - private EntityID target; - private EntityID commanderID; - - public CentralizedControlCommandExecutorA(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) { - super(ai, wi, si, moduleManager, developData); - this.commandType = ACTION_UNKNOWN; - switch (scenarioInfo.getMode()) { - case PRECOMPUTATION_PHASE: - case PRECOMPUTED: - case NON_PRECOMPUTE: - this.pathPlanning = moduleManager.getModule( - "DefaultCommandExecutorAmbulance.PathPlanning", - "adf.impl.module.algorithm.DijkstraPathPlanning"); - this.actionTransport = moduleManager.getExtAction( - "DefaultCommandExecutorAmbulance.ExtActionTransport", - "adf.impl.extaction.DefaultExtActionTransport"); - this.actionExtMove = moduleManager.getExtAction( - "DefaultCommandExecutorAmbulance.ExActionMove", - "adf.impl.extaction.DefaultExtActionMove"); - break; - } - } - - - @Override - public CommandExecutor setCommand(CommandAmbulance command) { - EntityID agentID = this.agentInfo.getID(); - if (command.isToIDDefined() && Objects.requireNonNull(command.getToID()) - .getValue() == agentID.getValue()) { - this.commandType = command.getAction(); - this.target = command.getTargetID(); - this.commanderID = command.getSenderID(); - } - return this; - } - - - @Override - public CommandExecutor updateInfo(MessageManager messageManager) { - super.updateInfo(messageManager); - if (this.getCountUpdateInfo() >= 2) { - return this; - } - this.pathPlanning.updateInfo(messageManager); - this.actionTransport.updateInfo(messageManager); - this.actionExtMove.updateInfo(messageManager); - - if (this.isCommandCompleted()) { - if (this.commandType != ACTION_UNKNOWN) { - messageManager - .addMessage(new MessageReport(true, true, false, this.commanderID)); - if (this.commandType == ACTION_LOAD) { - this.commandType = ACTION_UNLOAD; - this.target = null; - } else { - this.commandType = ACTION_UNKNOWN; - this.target = null; - this.commanderID = null; - } - } - } - return this; - } - - - @Override - public CommandExecutor precompute(PrecomputeData precomputeData) { - super.precompute(precomputeData); - if (this.getCountPrecompute() >= 2) { - return this; - } - this.pathPlanning.precompute(precomputeData); - this.actionTransport.precompute(precomputeData); - this.actionExtMove.precompute(precomputeData); - return this; - } - - - @Override - public CommandExecutor resume(PrecomputeData precomputeData) { - super.resume(precomputeData); - if (this.getCountResume() >= 2) { - return this; - } - this.pathPlanning.resume(precomputeData); - this.actionTransport.resume(precomputeData); - this.actionExtMove.resume(precomputeData); - return this; - } - - - @Override - public CommandExecutor preparate() { - super.preparate(); - if (this.getCountPreparate() >= 2) { - return this; - } - this.pathPlanning.preparate(); - this.actionTransport.preparate(); - this.actionExtMove.preparate(); - return this; - } - - - @Override - public CommandExecutor calc() { - this.result = null; - //System.out.println("time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" excutor"); - switch (this.commandType) { - case ACTION_REST: - EntityID position = this.agentInfo.getPosition(); - if (this.target == null) { - Collection< - EntityID> refuges = this.worldInfo.getEntityIDsOfType(REFUGE); - if (refuges.contains(position)) { - this.result = new ActionRest(); - } else { - this.pathPlanning.setFrom(position); - this.pathPlanning.setDestination(refuges); - List path = this.pathPlanning.calc().getResult(); - if (path != null && path.size() > 0) { - this.result = new ActionMove(path); - } else { - this.result = new ActionRest(); - } - } - return this; - } - if (position.getValue() != this.target.getValue()) { - List path = this.pathPlanning.getResult(position, - this.target); - if (path != null && path.size() > 0) { - this.result = new ActionMove(path); - return this; - } - } - this.result = new ActionRest(); - return this; - case ACTION_MOVE: - if (this.target != null) { - this.result = this.actionExtMove.setTarget(this.target).calc() - .getAction(); - } - return this; - case ACTION_RESCUE: - if (this.target != null) { - this.result = this.actionTransport.setTarget(this.target).calc() - .getAction(); - } - return this; - case ACTION_LOAD: - if (this.target != null) { - this.result = this.actionTransport.setTarget(this.target).calc() - .getAction(); - } - return this; - case ACTION_UNLOAD: - if (this.target != null) { - this.result = this.actionTransport.setTarget(this.target).calc() - .getAction(); - } - return this; - case ACTION_AUTONOMY: - if (this.target == null) { - return this; - } - StandardEntity targetEntity = this.worldInfo.getEntity(this.target); - if (targetEntity instanceof Area) { - if (this.agentInfo.someoneOnBoard() == null) { - this.result = this.actionExtMove.setTarget(this.target).calc() - .getAction(); - } else { - this.result = this.actionTransport.setTarget(this.target).calc() - .getAction(); - } - } else if (targetEntity instanceof Human) { - this.result = this.actionTransport.setTarget(this.target).calc() - .getAction(); - } - } - return this; - } - - - private boolean isCommandCompleted() { - Human agent = (Human) this.agentInfo.me(); - switch (this.commandType) { - case ACTION_REST: - if (this.target == null) { - return (agent.getDamage() == 0); - } - if (Objects.requireNonNull(this.worldInfo.getEntity(this.target)) - .getStandardURN() == REFUGE) { - if (agent.getPosition().getValue() == this.target.getValue()) { - return (agent.getDamage() == 0); - } - } - return false; - case ACTION_MOVE: - return this.target == null || this.agentInfo.getPosition() - .getValue() == this.target.getValue(); - case ACTION_RESCUE: - if (this.target == null) { - return true; - } - Human human = (Human) Objects - .requireNonNull(this.worldInfo.getEntity(this.target)); - return human.isBuriednessDefined() && human.getBuriedness() == 0 - || (human.isHPDefined() && human.getHP() == 0); - case ACTION_LOAD: - if (this.target == null) { - return true; - } - Human human1 = (Human) Objects - .requireNonNull(this.worldInfo.getEntity(this.target)); - if ((human1.isHPDefined() && human1.getHP() == 0)) { - return true; - } - if (human1.getStandardURN() != CIVILIAN) { - this.commandType = ACTION_RESCUE; - return this.isCommandCompleted(); - } - if (human1.isPositionDefined()) { - EntityID position = human1.getPosition(); - if (this.worldInfo.getEntityIDsOfType(AMBULANCE_TEAM) - .contains(position)) { - return true; - } else if (this.worldInfo.getEntity(position) - .getStandardURN() == REFUGE) { - return true; - } - } - return false; - case ACTION_UNLOAD: - if (this.target != null) { - StandardEntity entity = this.worldInfo.getEntity(this.target); - if (entity != null && entity instanceof Area) { - if (this.target.getValue() != this.agentInfo.getPosition() - .getValue()) { - return false; - } - } - } - return (this.agentInfo.someoneOnBoard() == null); - case ACTION_AUTONOMY: - if (this.target != null) { - StandardEntity targetEntity = this.worldInfo.getEntity(this.target); - if (targetEntity instanceof Area) { - this.commandType = this.agentInfo.someoneOnBoard() == null - ? ACTION_MOVE - : ACTION_UNLOAD; - return this.isCommandCompleted(); - } else if (targetEntity instanceof Human) { - Human h = (Human) targetEntity; - if ((h.isHPDefined() && h.getHP() == 0)) { - return true; - } - this.commandType = h.getStandardURN() == CIVILIAN ? ACTION_LOAD - : ACTION_RESCUE; - return this.isCommandCompleted(); - } - } - return true; - } - return true; - } -} \ No newline at end of file diff --git a/src/main/java/autumn_2023/centralized/CentralizedControlCommandPickerA.java b/src/main/java/autumn_2023/centralized/CentralizedControlCommandPickerA.java deleted file mode 100644 index 66b9cc0..0000000 --- a/src/main/java/autumn_2023/centralized/CentralizedControlCommandPickerA.java +++ /dev/null @@ -1,80 +0,0 @@ -package autumn_2023.centralized; - -import adf.core.agent.communication.standard.bundle.centralized.CommandAmbulance; -import adf.core.agent.communication.standard.bundle.centralized.CommandScout; -import adf.core.agent.develop.DevelopData; -import adf.core.agent.info.AgentInfo; -import adf.core.agent.info.ScenarioInfo; -import adf.core.agent.info.WorldInfo; -import adf.core.agent.module.ModuleManager; -import adf.core.component.centralized.CommandPicker; -import adf.core.component.communication.CommunicationMessage; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Map; -import rescuecore2.standard.entities.Area; -import rescuecore2.standard.entities.Human; -import rescuecore2.standard.entities.StandardEntity; -import rescuecore2.standard.entities.StandardEntityURN; -import rescuecore2.worldmodel.EntityID; - -public class CentralizedControlCommandPickerA extends CommandPicker { - - private int scoutDistance; - - private Collection messages; - private Map allocationData; - - public CentralizedControlCommandPickerA(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) { - super(ai, wi, si, moduleManager, developData); - this.messages = new ArrayList<>(); - this.allocationData = null; - this.scoutDistance = developData.getInteger( - "adf.impl.centralized.DefaultCommandPickerAmbulance.scoutDistance", - 40000); - } - - - @Override - public CommandPicker - setAllocatorResult(Map allocationData) { - this.allocationData = allocationData; - return this; - } - - - @Override - public CommandPicker calc() { - //System.out.println("time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" picker"); - this.messages.clear(); - if (this.allocationData == null) { - return this; - } - for (EntityID agentID : this.allocationData.keySet()) { - StandardEntity agent = this.worldInfo.getEntity(agentID); - if (agent != null - && agent.getStandardURN() == StandardEntityURN.AMBULANCE_TEAM) { - StandardEntity target = this.worldInfo - .getEntity(this.allocationData.get(agentID)); - if (target != null) { - if (target instanceof Human) { - CommandAmbulance command = new CommandAmbulance(true, agentID, - target.getID(), CommandAmbulance.ACTION_AUTONOMY); - this.messages.add(command); - } else if (target instanceof Area) { - CommandScout command = new CommandScout(true, agentID, - target.getID(), this.scoutDistance); - this.messages.add(command); - } - } - } - } - return this; - } - - - @Override - public Collection getResult() { - return this.messages; - } -} \ No newline at end of file diff --git a/src/main/java/autumn_2023/module/comm/DefaultChannelSubscriber.java b/src/main/java/autumn_2023/module/comm/DefaultChannelSubscriber.java deleted file mode 100644 index c5de79e..0000000 --- a/src/main/java/autumn_2023/module/comm/DefaultChannelSubscriber.java +++ /dev/null @@ -1,100 +0,0 @@ -package autumn_2023.module.comm; - -import adf.core.agent.communication.MessageManager; -import adf.core.agent.info.AgentInfo; -import adf.core.agent.info.ScenarioInfo; -import adf.core.agent.info.WorldInfo; -import adf.core.component.communication.ChannelSubscriber; -import rescuecore2.standard.entities.StandardEntityURN; - -public class DefaultChannelSubscriber extends ChannelSubscriber { - - @Override - public void subscribe(AgentInfo agentInfo, WorldInfo worldInfo, - ScenarioInfo scenarioInfo, MessageManager messageManager) { - // subscribe only once at the beginning - if (agentInfo.getTime() == 1) { - int numChannels = scenarioInfo.getCommsChannelsCount() - 1; // 0th channel - // is the - // voice - // channel - - int maxChannelCount = 0; - boolean isPlatoon = isPlatoonAgent(agentInfo, worldInfo); - if (isPlatoon) { - maxChannelCount = scenarioInfo.getCommsChannelsMaxPlatoon(); - } else { - maxChannelCount = scenarioInfo.getCommsChannelsMaxOffice(); - } - - StandardEntityURN agentType = getAgentType(agentInfo, worldInfo); - int[] channels = new int[maxChannelCount]; - for (int i = 0; i < maxChannelCount; i++) { - channels[i] = getChannelNumber(agentType, i, numChannels); - } - - messageManager.subscribeToChannels(channels); - } - } - - - protected boolean isPlatoonAgent(AgentInfo agentInfo, WorldInfo worldInfo) { - StandardEntityURN agentType = getAgentType(agentInfo, worldInfo); - if (agentType == StandardEntityURN.FIRE_BRIGADE - || agentType == StandardEntityURN.POLICE_FORCE - || agentType == StandardEntityURN.AMBULANCE_TEAM) { - return true; - } - return false; - } - - - protected StandardEntityURN getAgentType(AgentInfo agentInfo, - WorldInfo worldInfo) { - StandardEntityURN agentType = worldInfo.getEntity(agentInfo.getID()) - .getStandardURN(); - return agentType; - } - - - public static int getChannelNumber(StandardEntityURN agentType, - int channelIndex, int numChannels) { - int agentIndex = 0; - if (agentType == StandardEntityURN.FIRE_BRIGADE - || agentType == StandardEntityURN.FIRE_STATION) { - agentIndex = 1; - } else if (agentType == StandardEntityURN.POLICE_FORCE - || agentType == StandardEntityURN.POLICE_OFFICE) { - agentIndex = 2; - } else if (agentType == StandardEntityURN.AMBULANCE_TEAM - || agentType == StandardEntityURN.AMBULANCE_CENTRE) { - agentIndex = 3; - } - - int index = (3 * channelIndex) + agentIndex; - if ((index % numChannels) == 0) { - index = numChannels; - } else { - index = index % numChannels; - } - return index; - } - - - public static void main(String[] args) { - int numChannels = 6; - int maxChannels = 2; - for (int i = 0; i < maxChannels; i++) { - System.out.println("FIREBRIGADE-" + i + ":" - + getChannelNumber(StandardEntityURN.FIRE_BRIGADE, i, numChannels)); - } - for (int i = 0; i < maxChannels; i++) { - System.out.println("POLICE-" + i + ":" - + getChannelNumber(StandardEntityURN.POLICE_OFFICE, i, numChannels)); - } - for (int i = 0; i < maxChannels; i++) { - System.out.println("AMB-" + i + ":" + getChannelNumber( - StandardEntityURN.AMBULANCE_CENTRE, i, numChannels)); - } - } -} \ No newline at end of file diff --git a/src/main/java/autumn_2023/module/complex/CentralizedControlATAllocator.java b/src/main/java/autumn_2023/module/complex/CentralizedControlATAllocator.java deleted file mode 100755 index 3ef3a29..0000000 --- a/src/main/java/autumn_2023/module/complex/CentralizedControlATAllocator.java +++ /dev/null @@ -1,371 +0,0 @@ -package autumn_2023.module.complex; - -import static rescuecore2.standard.entities.StandardEntityURN.REFUGE; -import adf.core.agent.communication.MessageManager; -import adf.core.agent.communication.standard.bundle.MessageUtil; -import adf.core.agent.communication.standard.bundle.centralized.CommandAmbulance; -import adf.core.agent.communication.standard.bundle.centralized.MessageReport; -import adf.core.agent.communication.standard.bundle.information.MessageAmbulanceTeam; -import adf.core.agent.communication.standard.bundle.information.MessageCivilian; -import adf.core.agent.communication.standard.bundle.information.MessageFireBrigade; -import adf.core.agent.communication.standard.bundle.information.MessagePoliceForce; -import adf.core.agent.develop.DevelopData; -import adf.core.agent.info.AgentInfo; -import adf.core.agent.info.ScenarioInfo; -import adf.core.agent.info.WorldInfo; -import adf.core.agent.module.ModuleManager; -import adf.core.agent.precompute.PrecomputeData; -import adf.core.component.communication.CommunicationMessage; -import adf.core.component.module.complex.AmbulanceTargetAllocator; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Comparator; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import rescuecore2.standard.entities.AmbulanceTeam; -import rescuecore2.standard.entities.Area; -import rescuecore2.standard.entities.Human; -import rescuecore2.standard.entities.StandardEntity; -import rescuecore2.standard.entities.StandardEntityURN; -import rescuecore2.worldmodel.EntityID; - -public class CentralizedControlATAllocator extends AmbulanceTargetAllocator { - - private Collection priorityHumans; - private Collection targetHumans; - - private Map ambulanceTeamInfoMap; - - public CentralizedControlATAllocator(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) { - super(ai, wi, si, moduleManager, developData); - this.priorityHumans = new HashSet<>(); - this.targetHumans = new HashSet<>(); - this.ambulanceTeamInfoMap = new HashMap<>(); - } - - - @Override - public AmbulanceTargetAllocator resume(PrecomputeData precomputeData) { - super.resume(precomputeData); - if (this.getCountResume() >= 2) { - return this; - } - for (EntityID id : this.worldInfo - .getEntityIDsOfType(StandardEntityURN.AMBULANCE_TEAM)) { - this.ambulanceTeamInfoMap.put(id, new AmbulanceTeamInfo(id)); - } - return this; - } - - - @Override - public AmbulanceTargetAllocator preparate() { - super.preparate(); - if (this.getCountPrecompute() >= 2) { - return this; - } - for (EntityID id : this.worldInfo - .getEntityIDsOfType(StandardEntityURN.AMBULANCE_TEAM)) { - this.ambulanceTeamInfoMap.put(id, new AmbulanceTeamInfo(id)); - } - return this; - } - - - @Override - public Map getResult() { - return this.convert(this.ambulanceTeamInfoMap); - } - - - @Override - public AmbulanceTargetAllocator calc() { - //System.out.println("time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" allocator"); - List agents = this - .getActionAgents(this.ambulanceTeamInfoMap); - Collection removes = new ArrayList<>(); - int currentTime = this.agentInfo.getTime(); - for (EntityID target : this.priorityHumans) { - if (agents.size() > 0) { - StandardEntity targetEntity = this.worldInfo.getEntity(target); - if (targetEntity != null && targetEntity instanceof Human - && ((Human) targetEntity).isPositionDefined()) { - agents.sort(new DistanceSorter(this.worldInfo, targetEntity)); - StandardEntity result = agents.get(0); - agents.remove(0); - AmbulanceTeamInfo info = this.ambulanceTeamInfoMap - .get(result.getID()); - if (info != null) { - info.canNewAction = false; - info.target = target; - info.commandTime = currentTime; - this.ambulanceTeamInfoMap.put(result.getID(), info); - removes.add(target); - } - } - } - } - this.priorityHumans.removeAll(removes); - removes.clear(); - for (EntityID target : this.targetHumans) { - if (agents.size() > 0) { - StandardEntity targetEntity = this.worldInfo.getEntity(target); - if (targetEntity != null && targetEntity instanceof Human - && ((Human) targetEntity).isPositionDefined()) { - agents.sort(new DistanceSorter(this.worldInfo, targetEntity)); - StandardEntity result = agents.get(0); - agents.remove(0); - AmbulanceTeamInfo info = this.ambulanceTeamInfoMap - .get(result.getID()); - if (info != null) { - info.canNewAction = false; - info.target = target; - info.commandTime = currentTime; - this.ambulanceTeamInfoMap.put(result.getID(), info); - removes.add(target); - } - } - } - } - this.targetHumans.removeAll(removes); - return this; - } - - - @Override - public AmbulanceTargetAllocator updateInfo(MessageManager messageManager) { - super.updateInfo(messageManager); - if (this.getCountUpdateInfo() >= 2) { - return this; - } - int currentTime = this.agentInfo.getTime(); - for (CommunicationMessage message : messageManager - .getReceivedMessageList()) { - Class messageClass = message.getClass(); - if (messageClass == MessageCivilian.class) { - MessageCivilian mc = (MessageCivilian) message; - MessageUtil.reflectMessage(this.worldInfo, mc); - if (mc.isBuriednessDefined() && mc.getBuriedness() > 0) { - this.targetHumans.add(mc.getAgentID()); - } else { - this.priorityHumans.remove(mc.getAgentID()); - this.targetHumans.remove(mc.getAgentID()); - } - } else if (messageClass == MessageFireBrigade.class) { - MessageFireBrigade mfb = (MessageFireBrigade) message; - MessageUtil.reflectMessage(this.worldInfo, mfb); - if (mfb.isBuriednessDefined() && mfb.getBuriedness() > 0) { - this.priorityHumans.add(mfb.getAgentID()); - } else { - this.priorityHumans.remove(mfb.getAgentID()); - this.targetHumans.remove(mfb.getAgentID()); - } - } else if (messageClass == MessagePoliceForce.class) { - MessagePoliceForce mpf = (MessagePoliceForce) message; - MessageUtil.reflectMessage(this.worldInfo, mpf); - if (mpf.isBuriednessDefined() && mpf.getBuriedness() > 0) { - this.priorityHumans.add(mpf.getAgentID()); - } else { - this.priorityHumans.remove(mpf.getAgentID()); - this.targetHumans.remove(mpf.getAgentID()); - } - } - } - for (CommunicationMessage message : messageManager - .getReceivedMessageList(MessageAmbulanceTeam.class)) { - MessageAmbulanceTeam mat = (MessageAmbulanceTeam) message; - MessageUtil.reflectMessage(this.worldInfo, mat); - if (mat.isBuriednessDefined() && mat.getBuriedness() > 0) { - this.priorityHumans.add(mat.getAgentID()); - } else { - this.priorityHumans.remove(mat.getAgentID()); - this.targetHumans.remove(mat.getAgentID()); - } - AmbulanceTeamInfo info = this.ambulanceTeamInfoMap.get(mat.getAgentID()); - if (info == null) { - info = new AmbulanceTeamInfo(mat.getAgentID()); - } - if (currentTime >= info.commandTime + 2) { - this.ambulanceTeamInfoMap.put(mat.getAgentID(), this.update(info, mat)); - } - } - for (CommunicationMessage message : messageManager - .getReceivedMessageList(CommandAmbulance.class)) { - CommandAmbulance command = (CommandAmbulance) message; - if (command.getAction() == CommandAmbulance.ACTION_RESCUE - && command.isBroadcast()) { - this.priorityHumans.add(command.getTargetID()); - this.targetHumans.add(command.getTargetID()); - } else if (command.getAction() == CommandAmbulance.ACTION_LOAD - && command.isBroadcast()) { - this.priorityHumans.add(command.getTargetID()); - this.targetHumans.add(command.getTargetID()); - } - } - for (CommunicationMessage message : messageManager - .getReceivedMessageList(MessageReport.class)) { - MessageReport report = (MessageReport) message; - AmbulanceTeamInfo info = this.ambulanceTeamInfoMap - .get(report.getSenderID()); - if (info != null && report.isDone()) { - info.canNewAction = true; - this.priorityHumans.remove(info.target); - this.targetHumans.remove(info.target); - info.target = null; - this.ambulanceTeamInfoMap.put(info.agentID, info); - } - } - return this; - } - - - private Map - convert(Map map) { - Map result = new HashMap<>(); - for (EntityID id : map.keySet()) { - AmbulanceTeamInfo info = map.get(id); - if (info != null && info.target != null) { - result.put(id, info.target); - } - } - return result; - } - - - private List - getActionAgents(Map map) { - List result = new ArrayList<>(); - for (StandardEntity entity : this.worldInfo - .getEntitiesOfType(StandardEntityURN.POLICE_FORCE)) { - AmbulanceTeamInfo info = map.get(entity.getID()); - if (info != null && info.canNewAction - && ((AmbulanceTeam) entity).isPositionDefined()) { - result.add(entity); - } - } - return result; - } - - - private AmbulanceTeamInfo update(AmbulanceTeamInfo info, - MessageAmbulanceTeam message) { - if (message.isBuriednessDefined() && message.getBuriedness() > 0) { - info.canNewAction = false; - if (info.target != null) { - this.targetHumans.add(info.target); - info.target = null; - } - return info; - } - if (message.getAction() == MessageAmbulanceTeam.ACTION_REST) { - info.canNewAction = true; - if (info.target != null) { - this.targetHumans.add(info.target); - info.target = null; - } - } else if (message.getAction() == MessageAmbulanceTeam.ACTION_MOVE) { - if (message.getTargetID() != null) { - StandardEntity entity = this.worldInfo.getEntity(message.getTargetID()); - if (entity != null) { - if (entity instanceof Area) { - if (entity.getStandardURN() == REFUGE) { - info.canNewAction = false; - return info; - } - StandardEntity targetEntity = this.worldInfo.getEntity(info.target); - if (targetEntity != null) { - if (targetEntity instanceof Human) { - targetEntity = this.worldInfo.getPosition((Human) targetEntity); - if (targetEntity == null) { - this.priorityHumans.remove(info.target); - this.targetHumans.remove(info.target); - info.canNewAction = true; - info.target = null; - return info; - } - } - if (targetEntity.getID().getValue() == entity.getID() - .getValue()) { - info.canNewAction = false; - } else { - info.canNewAction = true; - if (info.target != null) { - this.targetHumans.add(info.target); - info.target = null; - } - } - } else { - info.canNewAction = true; - info.target = null; - } - return info; - } else if (entity instanceof Human) { - if (entity.getID().getValue() == info.target.getValue()) { - info.canNewAction = false; - } else { - info.canNewAction = true; - this.targetHumans.add(info.target); - this.targetHumans.add(entity.getID()); - info.target = null; - } - return info; - } - } - } - info.canNewAction = true; - if (info.target != null) { - this.targetHumans.add(info.target); - info.target = null; - } - } else if (message.getAction() == MessageAmbulanceTeam.ACTION_RESCUE) { - info.canNewAction = true; - if (info.target != null) { - this.targetHumans.add(info.target); - info.target = null; - } - } else if (message.getAction() == MessageAmbulanceTeam.ACTION_LOAD) { - info.canNewAction = false; - } else if (message.getAction() == MessageAmbulanceTeam.ACTION_UNLOAD) { - info.canNewAction = true; - this.priorityHumans.remove(info.target); - this.targetHumans.remove(info.target); - info.target = null; - } - return info; - } - - private class AmbulanceTeamInfo { - - EntityID agentID; - EntityID target; - boolean canNewAction; - int commandTime; - - AmbulanceTeamInfo(EntityID id) { - agentID = id; - target = null; - canNewAction = true; - commandTime = -1; - } - } - - private class DistanceSorter implements Comparator { - - private StandardEntity reference; - private WorldInfo worldInfo; - - DistanceSorter(WorldInfo wi, StandardEntity reference) { - this.reference = reference; - this.worldInfo = wi; - } - - - public int compare(StandardEntity a, StandardEntity b) { - int d1 = this.worldInfo.getDistance(this.reference, a); - int d2 = this.worldInfo.getDistance(this.reference, b); - return d1 - d2; - } - } -} \ No newline at end of file -- GitLab From a650472043075dfedd3aa7af54eeb548234c3537 Mon Sep 17 00:00:00 2001 From: k20066 <87357429+k20066@users.noreply.github.com> Date: Fri, 3 Nov 2023 01:30:00 +0900 Subject: [PATCH 3/9] modify error --- .../module/comm/CentralizedControlMessageCoordinator.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/autumn_2023/module/comm/CentralizedControlMessageCoordinator.java b/src/main/java/autumn_2023/module/comm/CentralizedControlMessageCoordinator.java index 5db7e1a..abeddf6 100644 --- a/src/main/java/autumn_2023/module/comm/CentralizedControlMessageCoordinator.java +++ b/src/main/java/autumn_2023/module/comm/CentralizedControlMessageCoordinator.java @@ -19,6 +19,8 @@ import adf.core.agent.info.ScenarioInfo; import adf.core.agent.info.WorldInfo; import adf.core.component.communication.CommunicationMessage; import adf.core.component.communication.MessageCoordinator; +import adf.impl.module.comm.DefaultChannelSubscriber; + import java.util.ArrayList; import java.util.List; import rescuecore2.standard.entities.StandardEntityURN; -- GitLab From f9623601dc9259ec6d7cea0b949691b608e41703 Mon Sep 17 00:00:00 2001 From: k20066 <87357429+k20066@users.noreply.github.com> Date: Fri, 3 Nov 2023 01:48:51 +0900 Subject: [PATCH 4/9] refactoring FBAllocator --- .../CentralizedControlFBAllocator.java | 38 ++++--------------- 1 file changed, 8 insertions(+), 30 deletions(-) diff --git a/src/main/java/autumn_2023/module/complex/CentralizedControlFBAllocator.java b/src/main/java/autumn_2023/module/complex/CentralizedControlFBAllocator.java index 3751e41..93d3f9c 100755 --- a/src/main/java/autumn_2023/module/complex/CentralizedControlFBAllocator.java +++ b/src/main/java/autumn_2023/module/complex/CentralizedControlFBAllocator.java @@ -159,25 +159,6 @@ public class CentralizedControlFBAllocator extends FireTargetAllocator { @Override public FireTargetAllocator updateInfo(MessageManager messageManager) { super.updateInfo(messageManager); - - /*List mescivlist = messageManager.getReceivedMessageList(MessageCivilian.class); - for(CommunicationMessage mes : mescivlist){ - MessageCivilian mesciv = (MessageCivilian) mes; - StandardMessagePriority level = mesciv.getSendingPriority(); - //if(level != StandardMessagePriority.HIGH) continue; - MessageUtil.reflectMessage(this.worldInfo, mesciv); - //boolean mdc = (mesciv instanceof MessageDamageCivilian); - //if(!(mesciv instanceof MessageDamageCivilian)) continue; - EntityID civID = mesciv.getAgentID(); - StandardEntity civ = this.worldInfo.getEntity(civID); - if(!this.isNeedRescueHuman(civ)) continue; - if(finishEntityIDs.contains(civID)) continue; - EntityID buildingID = mesciv.getPosition(); - //int time = this.agentInfo.getTime(); - //System.out.println("[FBCenter] time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" civID:"+civID+" buildingID:"+buildingID+" size:"+mesciv.getByteArraySize()+" mdc:"+mdc); - System.out.println("[FBCenter] time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" civID:"+civID+" buildingID:"+buildingID+" size:"+mesciv.getByteArraySize()+" isNeedRescueHuman:"+this.isNeedRescueHuman(civ)); - this.taskSet.add(civID); - }*/ Set recievetasks = messageManager.getReceivedMessageList(MessageCivilian.class).stream() .map(e -> { @@ -203,17 +184,14 @@ public class CentralizedControlFBAllocator extends FireTargetAllocator { (mesfb.getAction() == MessageFireBrigade.ACTION_RESCUE) ? false : true, this.agentInfo.getTime()); } - List reportlist = messageManager.getReceivedMessageList(MessageReport.class); - for(CommunicationMessage mes : reportlist){ - MessageReport report = (MessageReport) mes; - EntityID targetID = report.getFromID(); - //System.out.println("[FBCenter] report time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" fbID:"+senderID+" tagID:"+targetID); - boolean done = report.isDone(); - if(done) { - this.finishEntityIDs.add(targetID); - this.taskSet.remove(targetID); - } - } + List reportlist = messageManager.getReceivedMessageList(MessageReport.class).stream() + .map(MessageReport.class::cast) + .filter(MessageReport::isDone) + .map(MessageReport::getFromID) + .collect(Collectors.toList()); + + this.finishEntityIDs.addAll(reportlist); + this.taskSet.removeAll(reportlist); return this; } -- GitLab From ea76c045250f6b9acce670c8d8485ac2895091e5 Mon Sep 17 00:00:00 2001 From: k20066 <87357429+k20066@users.noreply.github.com> Date: Fri, 3 Nov 2023 02:24:53 +0900 Subject: [PATCH 5/9] comment delete --- .../CentralizedControlCommandExecutorF.java | 80 ------------------- .../ComplimentTacticsExtActionFireRescue.java | 1 - .../CentralizedControlMessageCoordinator.java | 6 -- .../CentralizedControlATHumanDetector.java | 19 ----- .../CentralizedControlFBAllocator.java | 16 +++- .../CentralizedControlFBHumanDetector.java | 11 --- 6 files changed, 12 insertions(+), 121 deletions(-) diff --git a/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorF.java b/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorF.java index 11c64db..dc1a22b 100644 --- a/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorF.java +++ b/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorF.java @@ -77,7 +77,6 @@ public class CentralizedControlCommandExecutorF extends CommandExecutor refuges = this.worldInfo.getEntityIDsOfType(REFUGE); - if (refuges.contains(position)) { - action = new ActionRest(); - } else { - this.pathPlanning.setFrom(position); - this.pathPlanning.setDestination(refuges); - List path = this.pathPlanning.calc().getResult(); - if (path != null && path.size() > 0) { - action = new ActionMove(path); - } else { - action = new ActionRest(); - } - } - return action; - } - if (position.getValue() != target.getValue()) { - List path = this.pathPlanning.getResult(position, - target); - if (path != null && path.size() > 0) { - action = new ActionMove(path); - return action; - } - } - action = new ActionRest(); - return action; - case ACTION_MOVE: - if (target != null) { - action = this.actionExtMove.setTarget(target).calc() - .getAction(); - } - return action; - case ACTION_RESCUE: - if (target != null) { - action = this.actionFireRescue.setTarget(target).calc() - .getAction(); - } - return action; - case ACTION_AUTONOMY: - if (target == null) { - return action; - } - StandardEntity targetEntity = this.worldInfo.getEntity(target); - if (targetEntity instanceof Area) { - action = this.actionExtMove.setTarget(target).calc() - .getAction(); - } else if (targetEntity instanceof Human) { - action = this.actionFireRescue.setTarget(target).calc() - .getAction(); - } - } - return action; - }*/ - private boolean isCommandCompleted(int commandType) { Human agent = (Human) this.agentInfo.me(); switch (commandType) { @@ -303,23 +239,7 @@ public class CentralizedControlCommandExecutorF extends CommandExecutor actionClass = this.result.getClass(); if (actionClass == ActionRescue.class) { - //System.out.println("[FireRescue time:"+this.agentInfo.getTime()+" myid:"+this.agentInfo.getID()); this.mm.addMessage(new MessageFireBrigade(true, agent, MessageFireBrigade.ACTION_RESCUE, this.target)); } } diff --git a/src/main/java/autumn_2023/module/comm/CentralizedControlMessageCoordinator.java b/src/main/java/autumn_2023/module/comm/CentralizedControlMessageCoordinator.java index abeddf6..5272c14 100644 --- a/src/main/java/autumn_2023/module/comm/CentralizedControlMessageCoordinator.java +++ b/src/main/java/autumn_2023/module/comm/CentralizedControlMessageCoordinator.java @@ -152,12 +152,6 @@ public class CentralizedControlMessageCoordinator extends MessageCoordinator { numChannels); } - String a = "isPlatoon:"+isPlatoon; - for(int i = 0;i < channels.length; i++){ - a += " channels["+i+"]:"+channels[i]; - } - //System.out.println("time:"+agentInfo.getTime()+" myid:"+agentInfo.getID()+a); - return channels; } diff --git a/src/main/java/autumn_2023/module/complex/CentralizedControlATHumanDetector.java b/src/main/java/autumn_2023/module/complex/CentralizedControlATHumanDetector.java index 3c9d8f1..13a5987 100755 --- a/src/main/java/autumn_2023/module/complex/CentralizedControlATHumanDetector.java +++ b/src/main/java/autumn_2023/module/complex/CentralizedControlATHumanDetector.java @@ -57,20 +57,7 @@ public class CentralizedControlATHumanDetector extends HumanDetector { public HumanDetector updateInfo(MessageManager messageManager) { logger.debug("Time:" + agentInfo.getTime()); super.updateInfo(messageManager); - - int time = this.agentInfo.getTime(); - Action action = this.agentInfo.getExecutedAction(time - 1); - //System.out.println("time:"+time+" myid:"+this.agentInfo.getID()+" -1step action:"+action); - - int[] chan = messageManager.getChannels(); - String str = ""; - for(int i=0; i mes = messageManager.getReceivedMessageList(MessageCivilian.class); - Set changes = this.worldInfo.getChanged().getChangedEntities(); for(EntityID id : changes){ StandardEntity entity = this.worldInfo.getEntity(id); @@ -81,13 +68,7 @@ public class CentralizedControlATHumanDetector extends HumanDetector { continue; } StandardMessagePriority level = StandardMessagePriority.HIGH; - //MessageCivilian civ_mes = new MessageDamageCivilian(true, level, civ); MessageCivilian civ_mes = new MessageCivilian(true, level, civ); - //Building building = (Building) this.worldInfo.getPosition(id); - - //System.out.println("time:"+time+" myid:"+this.agentInfo.getID()+" civ:"+civ.getID()+" level:"+civ_mes.getSendingPriority()); - - //messageManager.addMessage(building_mes,true); messageManager.addMessage(civ_mes,true); } diff --git a/src/main/java/autumn_2023/module/complex/CentralizedControlFBAllocator.java b/src/main/java/autumn_2023/module/complex/CentralizedControlFBAllocator.java index 93d3f9c..4197f90 100755 --- a/src/main/java/autumn_2023/module/complex/CentralizedControlFBAllocator.java +++ b/src/main/java/autumn_2023/module/complex/CentralizedControlFBAllocator.java @@ -145,13 +145,13 @@ public class CentralizedControlFBAllocator extends FireTargetAllocator { } allocate = reverse_list; } - String str = " allocate"; + for(int i = 0; i rowlist, List collist, int row){ + String str = " allocate"; + for(int i = 0; i changes = this.worldInfo.getChanged().getChangedEntities(); for(EntityID id : changes){ StandardEntity entity = this.worldInfo.getEntity(id); @@ -84,8 +75,6 @@ public class CentralizedControlFBHumanDetector extends HumanDetector { StandardMessagePriority level = StandardMessagePriority.HIGH; MessageCivilian civ_mes = new MessageCivilian(true, level, civ); - //System.out.println("time:"+time+" myid:"+this.agentInfo.getID()+" civ:"+civ.getID()+" level:"+civ_mes.getSendingPriority()); - messageManager.addMessage(civ_mes,true); } -- GitLab From 68c5b1e6010ffbc7ad9a3ebf4abca3b393e9484a Mon Sep 17 00:00:00 2001 From: k20066 <87357429+k20066@users.noreply.github.com> Date: Sat, 4 Nov 2023 00:36:33 +0900 Subject: [PATCH 6/9] modify priority --- .../module/complex/CentralizedControlATHumanDetector.java | 3 ++- .../module/complex/CentralizedControlFBAllocator.java | 1 - .../module/complex/CentralizedControlFBHumanDetector.java | 5 ++--- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/autumn_2023/module/complex/CentralizedControlATHumanDetector.java b/src/main/java/autumn_2023/module/complex/CentralizedControlATHumanDetector.java index 13a5987..6627f8b 100755 --- a/src/main/java/autumn_2023/module/complex/CentralizedControlATHumanDetector.java +++ b/src/main/java/autumn_2023/module/complex/CentralizedControlATHumanDetector.java @@ -67,8 +67,9 @@ public class CentralizedControlATHumanDetector extends HumanDetector { if(!this.isNeedRescueHuman(civ)){ continue; } - StandardMessagePriority level = StandardMessagePriority.HIGH; + StandardMessagePriority level = StandardMessagePriority.NORMAL; MessageCivilian civ_mes = new MessageCivilian(true, level, civ); + messageManager.addMessage(civ_mes,true); } diff --git a/src/main/java/autumn_2023/module/complex/CentralizedControlFBAllocator.java b/src/main/java/autumn_2023/module/complex/CentralizedControlFBAllocator.java index 4197f90..b9a4e57 100755 --- a/src/main/java/autumn_2023/module/complex/CentralizedControlFBAllocator.java +++ b/src/main/java/autumn_2023/module/complex/CentralizedControlFBAllocator.java @@ -151,7 +151,6 @@ public class CentralizedControlFBAllocator extends FireTargetAllocator { this.fireBrigadeInfoMap.get(rowlist.get(i)).target = collist.get(allocate[i]); } //this.printAllocate(allocate, rowlist, collist, row); - return this; } diff --git a/src/main/java/autumn_2023/module/complex/CentralizedControlFBHumanDetector.java b/src/main/java/autumn_2023/module/complex/CentralizedControlFBHumanDetector.java index 93ddd6b..3857c38 100755 --- a/src/main/java/autumn_2023/module/complex/CentralizedControlFBHumanDetector.java +++ b/src/main/java/autumn_2023/module/complex/CentralizedControlFBHumanDetector.java @@ -60,7 +60,6 @@ public class CentralizedControlFBHumanDetector extends HumanDetector { public HumanDetector updateInfo(MessageManager messageManager) { logger.debug("Time:" + agentInfo.getTime()); super.updateInfo(messageManager); - this.mm = messageManager; Set changes = this.worldInfo.getChanged().getChangedEntities(); @@ -72,9 +71,9 @@ public class CentralizedControlFBHumanDetector extends HumanDetector { if(!this.isNeedRescueHuman(civ)){ continue; } - StandardMessagePriority level = StandardMessagePriority.HIGH; + StandardMessagePriority level = StandardMessagePriority.NORMAL; MessageCivilian civ_mes = new MessageCivilian(true, level, civ); - + messageManager.addMessage(civ_mes,true); } -- GitLab From 088d09de3fb5af42f28337bc4fb3000df674bab8 Mon Sep 17 00:00:00 2001 From: k20066 <87357429+k20066@users.noreply.github.com> Date: Sat, 4 Nov 2023 03:05:28 +0900 Subject: [PATCH 7/9] finished --- config/module.cfg | 13 +- ...entralizedControlCommandExecutorFire.java} | 58 +++++---- .../CentralizedControlCommandPickerFire.java | 83 ++++++++++++ .../ComplimentTacticsExtActionFireRescue.java | 1 + .../CentralizedControlChannelSubscriber.java | 100 +++++++++++++++ .../CentralizedControlATHumanDetector.java | 14 +- .../CentralizedControlFBAllocator.java | 44 ++++++- .../CentralizedControlFBHumanDetector.java | 16 ++- .../module/complex/SampleSearch.java | 121 ++++++++++++++++++ 9 files changed, 412 insertions(+), 38 deletions(-) rename src/main/java/autumn_2023/centralized/{CentralizedControlCommandExecutorF.java => CentralizedControlCommandExecutorFire.java} (82%) create mode 100644 src/main/java/autumn_2023/centralized/CentralizedControlCommandPickerFire.java create mode 100644 src/main/java/autumn_2023/module/comm/CentralizedControlChannelSubscriber.java create mode 100644 src/main/java/autumn_2023/module/complex/SampleSearch.java diff --git a/config/module.cfg b/config/module.cfg index 7827515..7421f82 100755 --- a/config/module.cfg +++ b/config/module.cfg @@ -1,18 +1,17 @@ ## DefaultTacticsAmbulanceTeam DefaultTacticsAmbulanceTeam.HumanDetector : autumn_2023.module.complex.CentralizedControlATHumanDetector -DefaultTacticsAmbulanceTeam.Search : sample_team.module.complex.SampleSearch +DefaultTacticsAmbulanceTeam.Search : autumn_2023.module.complex.SampleSearch DefaultTacticsAmbulanceTeam.ExtActionTransport : adf.impl.extaction.DefaultExtActionTransport DefaultTacticsAmbulanceTeam.ExtActionMove : adf.impl.extaction.DefaultExtActionMove DefaultTacticsAmbulanceTeam.CommandExecutorAmbulance : adf.impl.centralized.DefaultCommandExecutorAmbulance -DefaultTacticsAmbulanceTeam.CommandExecutorAmbulance : autumn_2023.centralized.CentralizedControlCommandExecutorA DefaultTacticsAmbulanceTeam.CommandExecutorScout : adf.impl.centralized.DefaultCommandExecutorScout ## DefaultTacticsFireBrigade DefaultTacticsFireBrigade.HumanDetector : autumn_2023.module.complex.CentralizedControlFBHumanDetector -DefaultTacticsFireBrigade.Search : sample_team.module.complex.SampleSearch +DefaultTacticsFireBrigade.Search : autumn_2023.module.complex.SampleSearch DefaultTacticsFireBrigade.ExtActionFireRescue : autumn_2023.extaction.ComplimentTacticsExtActionFireRescue DefaultTacticsFireBrigade.ExtActionMove : adf.impl.extaction.DefaultExtActionMove -DefaultTacticsFireBrigade.CommandExecutorFire : autumn_2023.centralized.CentralizedControlCommandExecutorF +DefaultTacticsFireBrigade.CommandExecutorFire : autumn_2023.centralized.CentralizedControlCommandExecutorFire DefaultTacticsFireBrigade.CommandExecutorScout : adf.impl.centralized.DefaultCommandExecutorScout ## DefaultTacticsPoliceForce @@ -28,7 +27,7 @@ DefaultTacticsAmbulanceCentre.TargetAllocator : sample_team.module.complex.Sampl DefaultTacticsAmbulanceCentre.CommandPicker : adf.impl.centralized.DefaultCommandPickerAmbulance ## DefaultTacticsFireStation -DefaultTacticsFireStation.CommandPicker : adf.impl.centralized.DefaultCommandPickerFire +DefaultTacticsFireStation.CommandPicker : autumn_2023.centralized.CentralizedControlCommandPickerFire TacticsFireStation.TargetAllocator : autumn_2023.module.complex.CentralizedControlFBAllocator ## DefaultTacticsPoliceOffice @@ -93,8 +92,8 @@ DefaultCommandExecutorScoutPolice.PathPlanning : adf.impl.module.algorithm.Dijks DefaultCommandExecutorScoutPolice.ExtActionClear : adf.impl.extaction.DefaultExtActionClear ## MessageManager -MessageManager.PlatoonChannelSubscriber : adf.impl.module.comm.DefaultChannelSubscriber -MessageManager.CenterChannelSubscriber : adf.impl.module.comm.DefaultChannelSubscriber +MessageManager.PlatoonChannelSubscriber : autumn_2023.module.comm.CentralizedControlChannelSubscriber +MessageManager.CenterChannelSubscriber : autumn_2023.module.comm.CentralizedControlChannelSubscriber MessageManager.PlatoonMessageCoordinator : autumn_2023.module.comm.CentralizedControlMessageCoordinator MessageManager.CenterMessageCoordinator : autumn_2023.module.comm.CentralizedControlMessageCoordinator diff --git a/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorF.java b/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorFire.java similarity index 82% rename from src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorF.java rename to src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorFire.java index dc1a22b..2dc706a 100644 --- a/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorF.java +++ b/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorFire.java @@ -26,7 +26,7 @@ import rescuecore2.standard.entities.Human; import rescuecore2.standard.entities.StandardEntity; import rescuecore2.worldmodel.EntityID; -public class CentralizedControlCommandExecutorF extends CommandExecutor { +public class CentralizedControlCommandExecutorFire extends CommandExecutor { private static final int ACTION_UNKNOWN = -1; private static final int ACTION_REST = CommandFire.ACTION_REST; @@ -45,7 +45,7 @@ public class CentralizedControlCommandExecutorF extends CommandExecutor refuges = this.worldInfo.getEntityIDsOfType(REFUGE); + Collection refuges = this.worldInfo.getEntityIDsOfType(REFUGE); if (refuges.contains(position)) { this.result = new ActionRest(); } else { @@ -181,14 +189,12 @@ public class CentralizedControlCommandExecutorF extends CommandExecutor messages; + private Map allocationData; + + public CentralizedControlCommandPickerFire(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) { + super(ai, wi, si, moduleManager, developData); + this.messages = new ArrayList<>(); + this.allocationData = null; + this.scoutDistance = developData.getInteger( + "adf.impl.centralized.DefaultCommandPickerFire.scoutDistance", 40000); + } + + /** + * TargetAllocatorの計算結果を入力するためのメソッド + * + * @param allocationData TargetAllocatorのgetResultメソッドの戻り値 + */ + @Override + public CommandPicker + setAllocatorResult(Map allocationData) { + this.allocationData = allocationData; + return this; + } + + /** + * 司令を生成・リスト化するためのメソッド + * 司令はTargetAllocatorから受け取った司令内容をもとに生成される + */ + @Override + public CommandPicker calc() { + this.messages.clear(); + if (this.allocationData == null) { + return this; + } + for (EntityID agentID : this.allocationData.keySet()) { + StandardEntity agent = this.worldInfo.getEntity(agentID); + if (agent != null + && agent.getStandardURN() == StandardEntityURN.FIRE_BRIGADE) { + StandardEntity target = this.worldInfo.getEntity(this.allocationData.get(agentID)); + if (target != null) { + + if (target instanceof Human) { + CommandFire command = new CommandFire(true, agentID, target.getID(), CommandFire.ACTION_AUTONOMY); + this.messages.add(command); + } + } + } + } + return this; + } + + /** + * calcメソッドで生成した司令リストを取得するためのメソッド + * @return 司令リスト + */ + @Override + public Collection getResult() { + return this.messages; + } +} \ No newline at end of file diff --git a/src/main/java/autumn_2023/extaction/ComplimentTacticsExtActionFireRescue.java b/src/main/java/autumn_2023/extaction/ComplimentTacticsExtActionFireRescue.java index e263aae..7290b55 100644 --- a/src/main/java/autumn_2023/extaction/ComplimentTacticsExtActionFireRescue.java +++ b/src/main/java/autumn_2023/extaction/ComplimentTacticsExtActionFireRescue.java @@ -24,6 +24,7 @@ import rescuecore2.standard.entities.Human; import rescuecore2.standard.entities.StandardEntity; import rescuecore2.worldmodel.EntityID; +// ADFのDefaultTacticsFireBrigadeの修正用プログラム 無視してよい public class ComplimentTacticsExtActionFireRescue extends ExtAction { private MessageManager mm; diff --git a/src/main/java/autumn_2023/module/comm/CentralizedControlChannelSubscriber.java b/src/main/java/autumn_2023/module/comm/CentralizedControlChannelSubscriber.java new file mode 100644 index 0000000..e945e7b --- /dev/null +++ b/src/main/java/autumn_2023/module/comm/CentralizedControlChannelSubscriber.java @@ -0,0 +1,100 @@ +package autumn_2023.module.comm; + +import adf.core.agent.communication.MessageManager; +import adf.core.agent.info.AgentInfo; +import adf.core.agent.info.ScenarioInfo; +import adf.core.agent.info.WorldInfo; +import adf.core.component.communication.ChannelSubscriber; +import rescuecore2.standard.entities.StandardEntityURN; + +public class CentralizedControlChannelSubscriber extends ChannelSubscriber { + + @Override + public void subscribe(AgentInfo agentInfo, WorldInfo worldInfo, + ScenarioInfo scenarioInfo, MessageManager messageManager) { + // subscribe only once at the beginning + if (agentInfo.getTime() == 1) { + int numChannels = scenarioInfo.getCommsChannelsCount() - 1; // 0th channel + // is the + // voice + // channel + + int maxChannelCount = 0; + boolean isPlatoon = isPlatoonAgent(agentInfo, worldInfo); + if (isPlatoon) { + maxChannelCount = scenarioInfo.getCommsChannelsMaxPlatoon(); + } else { + maxChannelCount = scenarioInfo.getCommsChannelsMaxOffice(); + } + + StandardEntityURN agentType = getAgentType(agentInfo, worldInfo); + int[] channels = new int[maxChannelCount]; + for (int i = 0; i < maxChannelCount; i++) { + channels[i] = getChannelNumber(agentType, i, numChannels); + } + + messageManager.subscribeToChannels(channels); + } + } + + + protected boolean isPlatoonAgent(AgentInfo agentInfo, WorldInfo worldInfo) { + StandardEntityURN agentType = getAgentType(agentInfo, worldInfo); + if (agentType == StandardEntityURN.FIRE_BRIGADE + || agentType == StandardEntityURN.POLICE_FORCE + || agentType == StandardEntityURN.AMBULANCE_TEAM) { + return true; + } + return false; + } + + + protected StandardEntityURN getAgentType(AgentInfo agentInfo, + WorldInfo worldInfo) { + StandardEntityURN agentType = worldInfo.getEntity(agentInfo.getID()) + .getStandardURN(); + return agentType; + } + + + public static int getChannelNumber(StandardEntityURN agentType, + int channelIndex, int numChannels) { + int agentIndex = 0; + if (agentType == StandardEntityURN.FIRE_BRIGADE + || agentType == StandardEntityURN.FIRE_STATION) { + agentIndex = 1; + } else if (agentType == StandardEntityURN.POLICE_FORCE + || agentType == StandardEntityURN.POLICE_OFFICE) { + agentIndex = 2; + } else if (agentType == StandardEntityURN.AMBULANCE_TEAM + || agentType == StandardEntityURN.AMBULANCE_CENTRE) { + agentIndex = 3; + } + + int index = (3 * channelIndex) + agentIndex; + if ((index % numChannels) == 0) { + index = numChannels; + } else { + index = index % numChannels; + } + return index; + } + + + public static void main(String[] args) { + int numChannels = 6; + int maxChannels = 2; + for (int i = 0; i < maxChannels; i++) { + System.out.println("FIREBRIGADE-" + i + ":" + + getChannelNumber(StandardEntityURN.FIRE_BRIGADE, i, numChannels)); + } + for (int i = 0; i < maxChannels; i++) { + System.out.println("POLICE-" + i + ":" + + getChannelNumber(StandardEntityURN.POLICE_OFFICE, i, numChannels)); + } + for (int i = 0; i < maxChannels; i++) { + System.out.println("AMB-" + i + ":" + getChannelNumber( + StandardEntityURN.AMBULANCE_CENTRE, i, numChannels)); + } + } +} \ No newline at end of file diff --git a/src/main/java/autumn_2023/module/complex/CentralizedControlATHumanDetector.java b/src/main/java/autumn_2023/module/complex/CentralizedControlATHumanDetector.java index 6627f8b..e1321db 100755 --- a/src/main/java/autumn_2023/module/complex/CentralizedControlATHumanDetector.java +++ b/src/main/java/autumn_2023/module/complex/CentralizedControlATHumanDetector.java @@ -52,7 +52,11 @@ public class CentralizedControlATHumanDetector extends HumanDetector { registerModule(this.clustering); } - + /** + * 新しく受信したメッセージから情報を取得するためのメソッド + * + * 現在のステップで知覚した埋没市民を送信する + */ @Override public HumanDetector updateInfo(MessageManager messageManager) { logger.debug("Time:" + agentInfo.getTime()); @@ -76,7 +80,9 @@ public class CentralizedControlATHumanDetector extends HumanDetector { return this; } - + /** + * 搬送対象のIDを求めるメソッド + */ @Override public HumanDetector calc() { Human transportHuman = this.agentInfo.someoneOnBoard(); @@ -118,7 +124,9 @@ public class CentralizedControlATHumanDetector extends HumanDetector { return null; } - + /** + * 救助対象IDを返すメソッド + */ @Override public EntityID getTarget() { return this.result; diff --git a/src/main/java/autumn_2023/module/complex/CentralizedControlFBAllocator.java b/src/main/java/autumn_2023/module/complex/CentralizedControlFBAllocator.java index b9a4e57..ef32971 100755 --- a/src/main/java/autumn_2023/module/complex/CentralizedControlFBAllocator.java +++ b/src/main/java/autumn_2023/module/complex/CentralizedControlFBAllocator.java @@ -80,12 +80,22 @@ public class CentralizedControlFBAllocator extends FireTargetAllocator { return this; } - + /** + * calcメソッドで算出した司令内容を返すためのメソッド + */ @Override public Map getResult() { return this.convert(this.fireBrigadeInfoMap); } + /** + * 消防隊情報を + * key:命令対象のエージェントのID + * value:やるべきタスク(市民)のID + * のMapに変換するメソッド + * @param map 消防隊IDと消防隊情報に関連するMap + * @return 司令情報となるMap + */ private Map convert(Map map) { Map result = new HashMap<>(); for (EntityID id : map.keySet()) { @@ -97,6 +107,11 @@ public class CentralizedControlFBAllocator extends FireTargetAllocator { return result; } + /** + * 分散エージェントへの司令内容を算出するためのメソッド + * + * ハンガリアンアルゴリズムを利用して消防隊に担当させるタスクを決定する + */ @Override public FireTargetAllocator calc() { if(this.agentInfo.getTime() < scenarioInfo.getKernelAgentsIgnoreuntil()) return this; @@ -154,7 +169,13 @@ public class CentralizedControlFBAllocator extends FireTargetAllocator { return this; } - + /** + * 新しく受信したメッセージから情報を取得するためのメソッド + * + * 埋没市民Setを更新 + * 消防隊情報Setを更新 + * 消防隊から司令完了を受けたときタスク完了リストを更新 + */ @Override public FireTargetAllocator updateInfo(MessageManager messageManager) { super.updateInfo(messageManager); @@ -194,6 +215,12 @@ public class CentralizedControlFBAllocator extends FireTargetAllocator { return this; } + /** + * ある市民について消防隊による救助が必要かを判定するメソッド + * + * @param entity エンティティ + * @return true : 救助必要 false : 救助不必要 + */ private boolean isNeedRescueHuman(StandardEntity entity){ if (entity == null) return false; @@ -219,6 +246,13 @@ public class CentralizedControlFBAllocator extends FireTargetAllocator { return true; } + /** + * ハンガリアンアルゴリズムの割当結果をみるためのメソッド + * @param allocate 消防隊に対応するタスク割当結果配列 + * @param rowlist 消防隊IDリスト + * @param collist 埋没市民IDリスト + * @param row 消防隊の数 + */ private void printAllocate(int[] allocate, List rowlist, List collist, int row){ String str = " allocate"; for(int i = 0; i searchedSet = new HashSet<>(); + private List list; + private MessageManager messageManager; + private static final int ACTION_LOAD = CommandAmbulance.ACTION_LOAD; + + public SampleSearch( + AgentInfo ai, WorldInfo wi, ScenarioInfo si, + ModuleManager mm, DevelopData dd) + { + super(ai, wi, si, mm, dd); + + String clusteringKey = "SampleSearch.Clustering.Ambulance"; + this.clustering = + mm.getModule(clusteringKey, + "adf.sample.module.algorithm.SampleKMeans"); + this.registerModule(this.clustering); + } + + @Override + public EntityID getTarget() + { + return this.result; + } + + @Override + public Search updateInfo(MessageManager mm) + { + super.updateInfo(mm); + if (this.getCountUpdateInfo() > 1) return this; + + // そのステップで受信した(前ステップで送信された)メッセージ一覧 + List messages = mm.getReceivedMessageList(); + this.messageManager = mm; + + // エージェントが現在位置する建物や道路のIDを取得する + EntityID position = this.agentInfo.getPosition(); + // 探索済みの (辿り着いたことがある) 対象を候補から除外する + this.searchedSet.add(position); + + return this; + + } + + @Override + public Search calc() + { + EntityID me = this.agentInfo.getID(); + int idx = this.clustering.getClusterIndex(me); + if (idx == -1) { + return this; + } + //System.out.println("time:"+this.agentInfo.getTime()+" id:"+me+" target:"+this.getTarget()); + + Collection cluster = + this.clustering.getClusterEntityIDs(idx); + if (cluster == null) { + return this; + } + + // Listに変換 + if (this.result == null){ + list = new ArrayList<>(cluster); + //System.out.println(list); + } + + // エージェントが現在位置する建物や道路のIDを取得する + EntityID position = this.agentInfo.getPosition(); + + if (this.getTarget() != null && !position.equals(this.getTarget())) { + return this; + } + + // 探索済みの (辿り着いたことがある) 対象を候補から除外する + list.removeAll(this.searchedSet); + + // すべての対象が探索済みの場合,候補をリセットする + if (list.size() <= 0) { + this.searchedSet.clear(); + list = new ArrayList<>(cluster); + } + + double near = Double.MAX_VALUE; + for (EntityID lists : this.list) { + StandardEntity test = this.worldInfo.getEntity(lists); + if(test instanceof Building) { + double distance = this.worldInfo.getDistance(position, lists); + if (distance < near){ + near = distance; + this.result = lists; + } + } + } + + return this; + } +} \ No newline at end of file -- GitLab From f9f134f9e1c96062b50eac976e650fa6fb950cd8 Mon Sep 17 00:00:00 2001 From: k20066 <87357429+k20066@users.noreply.github.com> Date: Tue, 7 Nov 2023 14:41:31 +0900 Subject: [PATCH 8/9] analysis allocate --- ...CentralizedControlCommandExecutorFire.java | 1 + .../CentralizedControlFBAllocator.java | 50 +++++++++++++------ 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorFire.java b/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorFire.java index 2dc706a..7f37c42 100644 --- a/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorFire.java +++ b/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorFire.java @@ -74,6 +74,7 @@ public class CentralizedControlCommandExecutorFire extends CommandExecutor recievetasks = messageManager.getReceivedMessageList(MessageCivilian.class).stream() - .map(e -> { - MessageCivilian mesciv = (MessageCivilian) e; - MessageUtil.reflectMessage(this.worldInfo, mesciv); - return(mesciv); - }) - .map(MessageCivilian::getAgentID) - .filter(e -> this.isNeedRescueHuman(this.worldInfo.getEntity(e))) - .filter(e -> !(finishEntityIDs.contains(e))) - .collect(Collectors.toSet()); - + .map(e -> { + MessageCivilian mesciv = (MessageCivilian) e; + MessageUtil.reflectMessage(this.worldInfo, mesciv); + return(mesciv); + }) + .map(MessageCivilian::getAgentID) + .filter(e -> this.isNeedRescueHuman(this.worldInfo.getEntity(e))) + .filter(e -> !(finishEntityIDs.contains(e))) + .collect(Collectors.toSet()); + + System.out.println("time:"+this.agentInfo.getTime()+" centerid:"+this.agentInfo.getID()+" recievetasks:"+recievetasks); this.taskSet.addAll(recievetasks); + // 消防隊の情報をリセット + for(EntityID fbid : this.fireBrigadeInfoMap.keySet()){ + FireBrigadeInfo fbi = this.fireBrigadeInfoMap.get(fbid); + fbi.resetInfo(); + } + List fblist = new ArrayList<>(); List mesfblist = messageManager.getReceivedMessageList(MessageFireBrigade.class); for(CommunicationMessage mes : mesfblist){ MessageFireBrigade mesfb = (MessageFireBrigade) mes; MessageUtil.reflectMessage(this.worldInfo, mesfb); EntityID fbID = mesfb.getAgentID(); + fblist.add(fbID); FireBrigadeInfo fbi = this.fireBrigadeInfoMap.get(fbID); fbi.setInfo(mesfb.getTargetID(), mesfb.getPosition(), mesfb.getBuriedness(), (mesfb.getAction() == MessageFireBrigade.ACTION_RESCUE) ? false : true, this.agentInfo.getTime()); } + System.out.println("time:"+this.agentInfo.getTime()+" centerid:"+this.agentInfo.getID()+" fblist:"+fblist); List reportlist = messageManager.getReceivedMessageList(MessageReport.class).stream() - .map(MessageReport.class::cast) - .filter(MessageReport::isDone) - .map(MessageReport::getFromID) - .collect(Collectors.toList()); + .map(MessageReport.class::cast) + .filter(MessageReport::isDone) + .map(MessageReport::getFromID) + .collect(Collectors.toList()); this.finishEntityIDs.addAll(reportlist); this.taskSet.removeAll(reportlist); @@ -294,5 +303,16 @@ public class CentralizedControlFBAllocator extends FireTargetAllocator { this.canNewAction = canNewAction; this.commandTime = commandTime; } + /** + * 消防隊に関する情報をリセットするメソッド + */ + public void resetInfo(){ + prevtarget = null; + position = null; + buriedness = -1; + target = null; + canNewAction = true; + commandTime = -1; + } } } \ No newline at end of file -- GitLab From af8744d9e5c5bea08f5e0b82e46fcb0c18ea5c0f Mon Sep 17 00:00:00 2001 From: k20066 <87357429+k20066@users.noreply.github.com> Date: Tue, 7 Nov 2023 21:43:33 +0900 Subject: [PATCH 9/9] search modify --- ...CentralizedControlCommandExecutorFire.java | 1 - .../CentralizedControlFBAllocator.java | 4 +- .../module/complex/SampleSearch.java | 79 +++++++++++-------- 3 files changed, 49 insertions(+), 35 deletions(-) diff --git a/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorFire.java b/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorFire.java index 7f37c42..2dc706a 100644 --- a/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorFire.java +++ b/src/main/java/autumn_2023/centralized/CentralizedControlCommandExecutorFire.java @@ -74,7 +74,6 @@ public class CentralizedControlCommandExecutorFire extends CommandExecutor !(finishEntityIDs.contains(e))) .collect(Collectors.toSet()); - System.out.println("time:"+this.agentInfo.getTime()+" centerid:"+this.agentInfo.getID()+" recievetasks:"+recievetasks); this.taskSet.addAll(recievetasks); // 消防隊の情報をリセット @@ -210,7 +209,6 @@ public class CentralizedControlFBAllocator extends FireTargetAllocator { fbi.setInfo(mesfb.getTargetID(), mesfb.getPosition(), mesfb.getBuriedness(), (mesfb.getAction() == MessageFireBrigade.ACTION_RESCUE) ? false : true, this.agentInfo.getTime()); } - System.out.println("time:"+this.agentInfo.getTime()+" centerid:"+this.agentInfo.getID()+" fblist:"+fblist); List reportlist = messageManager.getReceivedMessageList(MessageReport.class).stream() .map(MessageReport.class::cast) diff --git a/src/main/java/autumn_2023/module/complex/SampleSearch.java b/src/main/java/autumn_2023/module/complex/SampleSearch.java index 4762efa..2134a6c 100644 --- a/src/main/java/autumn_2023/module/complex/SampleSearch.java +++ b/src/main/java/autumn_2023/module/complex/SampleSearch.java @@ -14,6 +14,7 @@ import adf.core.agent.develop.DevelopData; import rescuecore2.standard.entities.*; import rescuecore2.worldmodel.EntityID; import java.util.*; +import java.util.stream.Collector; import java.util.stream.Collectors; import static java.lang.Double.POSITIVE_INFINITY; @@ -57,11 +58,39 @@ public class SampleSearch extends Search List messages = mm.getReceivedMessageList(); this.messageManager = mm; + int idx = this.clustering.getClusterIndex(this.agentInfo.me()); + Collection cluster = this.clustering.getClusterEntityIDs(idx); + if (cluster == null) return this; + + // エージェントが動けるステップではないなら + if(this.agentInfo.getTime() < this.scenarioInfo.getKernelAgentsIgnoreuntil()) return this; + // エージェントが動けるステップのはじめに + if(this.agentInfo.getTime() == this.scenarioInfo.getKernelAgentsIgnoreuntil()){ + this.list = new ArrayList<>(cluster); + } + + // エージェントが現在位置する建物や道路のIDを取得する EntityID position = this.agentInfo.getPosition(); // 探索済みの (辿り着いたことがある) 対象を候補から除外する this.searchedSet.add(position); + // 探索済みの (辿り着いたことがある) 対象を候補から除外する + list.removeAll(this.searchedSet); + + // クラスタ内の建物や道路のIDリストから建物のIDリストを取得 + List buildinglist = this.list.stream()// listのストリームを取得 + .map(this.worldInfo::getEntity)// EntityIDをStandardEntityに変換 + .filter(e -> (e instanceof Building))// StandardEntityオブジェクトのうちBuildingクラスのものだけ抽出 + .map(StandardEntity::getID)// StandardEntityをEntityIDに変換 + .collect(Collectors.toList());// ストリームをリストにして取得 + + // すべての対象が探索済みの場合,探索済みリストをリセット候補をリセットする + if (buildinglist.isEmpty()){ + this.searchedSet.clear(); + this.list = new ArrayList<>(cluster); + } + return this; } @@ -69,24 +98,17 @@ public class SampleSearch extends Search @Override public Search calc() { + // エージェントが動けるステップではないなら + if(this.agentInfo.getTime() < this.scenarioInfo.getKernelAgentsIgnoreuntil()) return this; + EntityID me = this.agentInfo.getID(); int idx = this.clustering.getClusterIndex(me); - if (idx == -1) { - return this; - } - //System.out.println("time:"+this.agentInfo.getTime()+" id:"+me+" target:"+this.getTarget()); + if (idx == -1) return this; - Collection cluster = - this.clustering.getClusterEntityIDs(idx); - if (cluster == null) { - return this; - } + Collection cluster = this.clustering.getClusterEntityIDs(idx); + if (cluster == null) return this; - // Listに変換 - if (this.result == null){ - list = new ArrayList<>(cluster); - //System.out.println(list); - } + //System.out.println("time:"+this.agentInfo.getTime()+" id:"+me+" target:"+this.getTarget()); // エージェントが現在位置する建物や道路のIDを取得する EntityID position = this.agentInfo.getPosition(); @@ -94,25 +116,20 @@ public class SampleSearch extends Search if (this.getTarget() != null && !position.equals(this.getTarget())) { return this; } - - // 探索済みの (辿り着いたことがある) 対象を候補から除外する - list.removeAll(this.searchedSet); - - // すべての対象が探索済みの場合,候補をリセットする - if (list.size() <= 0) { - this.searchedSet.clear(); - list = new ArrayList<>(cluster); - } + + // クラスタ内の建物や道路のIDリストから建物のIDリストを取得 + List buildinglist = list.stream()// listのストリームを取得 + .map(this.worldInfo::getEntity)// EntityIDをStandardEntityに変換 + .filter(e -> (e instanceof Building))// StandardEntityオブジェクトのうちBuildingクラスのものだけ抽出 + .map(StandardEntity::getID)// StandardEntityをEntityIDに変換 + .collect(Collectors.toList());// ストリームをリストにして取得 double near = Double.MAX_VALUE; - for (EntityID lists : this.list) { - StandardEntity test = this.worldInfo.getEntity(lists); - if(test instanceof Building) { - double distance = this.worldInfo.getDistance(position, lists); - if (distance < near){ - near = distance; - this.result = lists; - } + for (EntityID lists : buildinglist) { + double distance = this.worldInfo.getDistance(position, lists); + if (distance < near){ + near = distance; + this.result = lists; } } -- GitLab