From 3a6f78e015466c72513e5ba0de7b8ac09edeac11 Mon Sep 17 00:00:00 2001 From: K20014 <82937878+K20014@users.noreply.github.com> Date: Fri, 3 Nov 2023 01:33:21 +0900 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E6=A5=AD=E9=80=94=E4=B8=AD=E3=81=AE?= =?UTF-8?q?=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/module.cfg | 2 +- .../AuctionCommandExecutorFire.java | 157 +++++++++++++++++- .../module/complex/AuctionFBSearch.java | 48 +++++- .../complex/AuctionFireTargetAllocator.java | 61 +++++-- 4 files changed, 249 insertions(+), 19 deletions(-) diff --git a/config/module.cfg b/config/module.cfg index e482302..dedcf5b 100755 --- a/config/module.cfg +++ b/config/module.cfg @@ -79,7 +79,7 @@ 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.ExtActionFireFighting : adf.impl.extaction.DefaultExtActionFireFighting DefaultCommandExecutorFire.ExtActionMove : adf.impl.extaction.DefaultExtActionMove ## DefaultCommandExecutorPolice diff --git a/src/main/java/autumn_2023/centralized/AuctionCommandExecutorFire.java b/src/main/java/autumn_2023/centralized/AuctionCommandExecutorFire.java index 2eb8030..ac3f999 100644 --- a/src/main/java/autumn_2023/centralized/AuctionCommandExecutorFire.java +++ b/src/main/java/autumn_2023/centralized/AuctionCommandExecutorFire.java @@ -1,5 +1,7 @@ package autumn_2023.centralized; +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.StandardMessagePriority; import adf.core.agent.communication.standard.bundle.centralized.CommandFire; @@ -9,7 +11,9 @@ 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 autumn_2023.module.comm.infomation.MessageCost; import rescuecore2.standard.entities.*; @@ -23,17 +27,57 @@ import static rescuecore2.standard.entities.StandardEntityURN.REFUGE; public class AuctionCommandExecutorFire 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; + /* 救急司令所からすでに受け取った埋没市民のIDの集合 */ private final Set bidCentreCivilians = new HashSet<>(); - private final PathPlanning pathPlanning; public AuctionCommandExecutorFire(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( + "AuctionCommandExecutorFire.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 (command.isToIDDefined() && Objects.requireNonNull(command.getToID()).getValue() == agentID.getValue()) + { + this.commandType = command.getAction(); + this.target = command.getTargetID(); + this.commanderID = command.getSenderID(); + } - this.pathPlanning = moduleManager.getModule("SampleHumanDetector.PathPlanning", - "adf.impl.module.algorithm.DijkstraPathPlanning"); + return this; } @Override @@ -49,9 +93,12 @@ public class AuctionCommandExecutorFire extends CommandExecutor { .collect(Collectors.toSet()); // 新規メッセージクラスを用いてタスク実行時のコストを救急司令所に送信 - receivedCentreCivilians.parallelStream() + if (this.result == null) + { + receivedCentreCivilians.parallelStream() .map(e -> new MessageCost(true, StandardMessagePriority.HIGH, e, CalculationCost(e))) .forEach(messageManager::addMessage); + } // 知覚した救助対象の市民の情報をMessageCivilianとして指令所に送信 this.worldInfo.getChanged().getChangedEntities().parallelStream() @@ -68,14 +115,110 @@ public class AuctionCommandExecutorFire extends CommandExecutor { } @Override - public CommandExecutor setCommand(CommandFire command) { - // TODO + 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() { - // TODO + this.result = null; + switch (this.commandType) + { + case ACTION_REST: + EntityID position = this.agentInfo.getPosition(); + if (this.target == null) + { + Collection 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) + { + 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(); + } + } + return this; } diff --git a/src/main/java/autumn_2023/module/complex/AuctionFBSearch.java b/src/main/java/autumn_2023/module/complex/AuctionFBSearch.java index 317a660..25197a8 100644 --- a/src/main/java/autumn_2023/module/complex/AuctionFBSearch.java +++ b/src/main/java/autumn_2023/module/complex/AuctionFBSearch.java @@ -1,5 +1,51 @@ package autumn_2023.module.complex; -public class AuctionFBSearch { +import adf.core.agent.info.*; +import adf.core.component.module.complex.Search; +import adf.core.component.module.algorithm.Clustering; +import adf.core.agent.module.ModuleManager; +import adf.core.agent.develop.DevelopData; +import rescuecore2.worldmodel.EntityID; +import java.util.*; + +public class AuctionFBSearch extends Search{ + private EntityID result; + private Random random = new Random(1); + + private Clustering clustering; + + public AuctionFBSearch( + AgentInfo ai, WorldInfo wi, ScenarioInfo si, + ModuleManager mm, DevelopData dd) + { + super(ai, wi, si, mm, dd); + this.clustering =mm.getModule("SampleSearch.Clustering.Fire", + "adf.sample.module.algorithm.SampleKMeans"); + this.registerModule(this.clustering); + } + + @Override + public EntityID getTarget() + { + return this.result; + } + + @Override + public Search calc() + { + EntityID me = this.agentInfo.getID(); + int idx = this.clustering.getClusterIndex(me); + Collection cluster = this.clustering.getClusterEntityIDs(idx); + + // 擬似乱数を用いてランダムに選択するために,Listに変換します + List list = new ArrayList<>(cluster); + + // 擬似乱数を用いてランダムに選択します + int n = list.size(); + int r = this.random.nextInt(n); + this.result = list.get(r); + + return this; + } } diff --git a/src/main/java/autumn_2023/module/complex/AuctionFireTargetAllocator.java b/src/main/java/autumn_2023/module/complex/AuctionFireTargetAllocator.java index f0d5a37..d697f63 100644 --- a/src/main/java/autumn_2023/module/complex/AuctionFireTargetAllocator.java +++ b/src/main/java/autumn_2023/module/complex/AuctionFireTargetAllocator.java @@ -33,6 +33,7 @@ public class AuctionFireTargetAllocator extends FireTargetAllocator{ // 分散エージェントからのコストのメッセージリスト private List rescueCosts; private Map rescueResult; + private Map calclationResult; public AuctionFireTargetAllocator(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) { @@ -42,6 +43,7 @@ public class AuctionFireTargetAllocator extends FireTargetAllocator{ this.finishreceivedCivilians = new ArrayList<>(); this.rescueCosts = new ArrayList<>(); this.rescueResult = new HashMap<>(); + this.calclationResult = new HashMap<>(); } @@ -66,13 +68,41 @@ public class AuctionFireTargetAllocator extends FireTargetAllocator{ @Override public Map getResult() { - return this.rescueResult; + return rescueResult; } @Override public FireTargetAllocator calc() { + // mapの初期化 + if (!this.calclationResult.isEmpty()) this.calclationResult.clear(); + if (!this.rescueResult.isEmpty()) this.rescueResult.clear(); + + // メッセージがなければ + if (this.rescueCosts.isEmpty()) + { + return this; + }else{ + for(MessageCost mesCos : this.rescueCosts) + { + // コスト計算用マップが初期化状態もしくはキーがない場合 + if (this.calclationResult.isEmpty() || !this.calclationResult.containsKey(mesCos.getTargetID())) + { + this.calclationResult.put(mesCos.getTargetID(), + new RescueCivilianCost(mesCos.getSenderID(), mesCos.cost())); + // コストが現状より小さい場合 + }else if (this.calclationResult.get(mesCos.getTargetID()).rescueCost > mesCos.cost()) { + this.calclationResult.replace(mesCos.getTargetID(), + new RescueCivilianCost(mesCos.getSenderID(), mesCos.cost())); + } + } + // CommandPicker送信用のマップに変更 + for(EntityID id : this.calclationResult.keySet()) { + this.rescueResult.put(this.calclationResult.get(id).agentID, id); + } + this.rescueCosts.clear(); + } return this; } @@ -92,16 +122,16 @@ public class AuctionFireTargetAllocator extends FireTargetAllocator{ if (mes instanceof MessageCivilian) { MessageCivilian mesCiv = (MessageCivilian) mes; - if(mesCiv.getSendingPriority() == StandardMessagePriority.HIGH) + if(mesCiv.getHP() != 0 && mesCiv.getBuriedness() != 0) { MessageUtil.reflectMessage(this.worldInfo, mesCiv); if (this.worldInfo.getEntity(mesCiv.getSenderID()) instanceof AmbulanceTeam || this.worldInfo.getEntity(mesCiv.getSenderID()) instanceof FireBrigade) { - if(!receivedAgentCivilians.contains(mesCiv.getAgentID()) - || !finishreceivedCivilians.contains(mesCiv.getAgentID())) + if(!this.receivedAgentCivilians.contains(mesCiv.getAgentID()) + || !this.finishreceivedCivilians.contains(mesCiv.getAgentID())) { - receivedAgentCivilians.add(mesCiv.getAgentID()); + this.receivedAgentCivilians.add(mesCiv.getAgentID()); } } } @@ -111,20 +141,31 @@ public class AuctionFireTargetAllocator extends FireTargetAllocator{ MessageCost mesCos = (MessageCost) mes; if (this.worldInfo.getEntity(mesCos.getSenderID()) instanceof FireBrigade) { - rescueCosts.add(mesCos); + this.rescueCosts.add(mesCos); } } } // 送信処理 // 埋没している市民の情報の送信 - for(EntityID res : receivedAgentCivilians) + for(EntityID res : this.receivedAgentCivilians) { messageManager.addMessage(new MessageCivilian(true, StandardMessagePriority.HIGH, (Civilian)this.worldInfo.getEntity(res))); - finishreceivedCivilians.add(res); + this.finishreceivedCivilians.add(res); } - receivedAgentCivilians.clear(); + this.receivedAgentCivilians.clear(); return this; } -} + + private class RescueCivilianCost { + EntityID agentID; + int rescueCost; + + RescueCivilianCost(EntityID id, int cost) + { + agentID = id; + rescueCost = cost; + } + } +} \ No newline at end of file -- GitLab