diff --git a/config/module.cfg b/config/module.cfg index 897af59a9058fe213c1065a25059c5f8eed2a782..af44b7579bcdf17aa02bb5e6831138a474c4af15 100755 --- a/config/module.cfg +++ b/config/module.cfg @@ -36,21 +36,22 @@ DefaultTacticsPoliceOffice.CommandPicker : adf.impl.centralized.DefaultCommandPi ## SampleSearch SampleSearch.PathPlanning.Ambulance : adf.impl.module.algorithm.DijkstraPathPlanning -SampleSearch.Clustering.Ambulance : adf.impl.module.algorithm.KMeansClustering +SampleSearch.Clustering.Ambulance : autumn_2023.module.algorithm.KmeansPPClustering SampleSearch.PathPlanning.Fire : adf.impl.module.algorithm.DijkstraPathPlanning -SampleSearch.Clustering.Fire : adf.impl.module.algorithm.KMeansClustering +SampleSearch.Clustering.Fire : autumn_2023.module.algorithm.KmeansPPClustering SampleSearch.PathPlanning.Police : adf.impl.module.algorithm.DijkstraPathPlanning -SampleSearch.Clustering.Police : adf.impl.module.algorithm.KMeansClustering +SampleSearch.Clustering.Police : autumn_2023.module.algorithm.KmeansPPClustering ## SampleBuildDetector -SampleBuildingDetector.Clustering : adf.impl.module.algorithm.KMeansClustering +#SampleBuildingDetector.Clustering : adf.impl.module.algorithm.KMeansClustering +SampleBuildingDetector.Clustering : autumn_2023.module.algorithm.KmeansPPClustering ## SampleRoadDetector -SampleRoadDetector.Clustering : adf.impl.module.algorithm.KMeansClustering +SampleRoadDetector.Clustering : autumn_2023.module.algorithm.KmeansPPClustering SampleRoadDetector.PathPlanning : adf.impl.module.algorithm.DijkstraPathPlanning ## SampleHumanDetector -SampleHumanDetector.Clustering : adf.impl.module.algorithm.KMeansClustering +SampleHumanDetector.Clustering : autumn_2023.module.algorithm.KmeansPPClustering ## DefaultExtActionClear DefaultExtActionClear.PathPlanning : adf.impl.module.algorithm.DijkstraPathPlanning diff --git a/src/main/java/autumn_2023/centralized/HungarianCommandExecutorAmbulance.java b/src/main/java/autumn_2023/centralized/HungarianCommandExecutorAmbulance.java new file mode 100644 index 0000000000000000000000000000000000000000..33309c96809dd361d4719a15c9f23cdad36c360e --- /dev/null +++ b/src/main/java/autumn_2023/centralized/HungarianCommandExecutorAmbulance.java @@ -0,0 +1,310 @@ +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 HungarianCommandExecutorAmbulance + 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 HungarianCommandExecutorAmbulance(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; + 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/module/algorithm/Hungarian.java b/src/main/java/autumn_2023/module/algorithm/Hungarian.java new file mode 100644 index 0000000000000000000000000000000000000000..f77a859064da5dc32013fe003723eadc2e9891d9 --- /dev/null +++ b/src/main/java/autumn_2023/module/algorithm/Hungarian.java @@ -0,0 +1,65 @@ +package autumn_2023.module.algorithm; + +import java.util.*; + +public class Hungarian{ + public static int[] execute(int[][] mat){ + int n = mat.length; + int m = mat[0].length; + int[] ret = new int[n]; + int[] toRight = new int[n]; + int[] toLeft = new int[m]; + int[] ofsLeft = new int[n]; + int[] ofsRight = new int[m]; + + for (int i=0;i= 0) { + int a = trace[b]; + int z = toRight[a]; + toLeft[b] = a; + toRight[a] = b; + b = z; + } + break; + } + right[b] = left[c] = true; + for (int j=0;j> memberz) + { + this.result = new Cluster[n]; + this.n = n; + + for (int i=0; i members = memberz.get(i); + this.result[i] = new Cluster(members); + } + } + + public void execute(int rep) + { + this.result = init(this.targets, this.xs, this.ys, this.n); + + for (int i=0; i getClusterMembers(int i) + { + return this.result[i].getMembers(); + } + + private static Cluster[] init( + EntityID[] targets, double[] xs, double[] ys, int n) + { + Cluster[] ret = new Cluster[n]; + for (int i=0; i cluster = Arrays.stream(clusters) + .min(comparing(c -> { + double cx = c.getCX(); + double cy = c.getCY(); + return Math.hypot(cx-x, cy-y); + })); + + cluster.get().addMember(id, x, y); + } + + private static class Cluster + { + private double cx = 0.0; + private double cy = 0.0; + + private Collection members = new LinkedList<>(); + + private double sumx = 0.0; + private double sumy = 0.0; + + public Cluster() {} + + public Cluster(Collection members) + { + this.members = members; + } + + public void addMember(EntityID id, double x, double y) + { + this.members.add(id); + this.sumx += x; + this.sumy += y; + } + + public Collection getMembers() + { + return new ArrayList<>(this.members); + } + + public void clearMembers() + { + this.members.clear(); + this.sumx = 0.0; + this.sumy = 0.0; + } + + public void updateCenter() + { + if (this.members.isEmpty()) return; + + this.cx = this.sumx / this.members.size(); + this.cy = this.sumy / this.members.size(); + } + + public double getCX() + { + return this.cx; + } + + public double getCY() + { + return this.cy; + } + } +} diff --git a/src/main/java/autumn_2023/module/algorithm/KmeansPPClustering.java b/src/main/java/autumn_2023/module/algorithm/KmeansPPClustering.java new file mode 100644 index 0000000000000000000000000000000000000000..c4462f1a2f0e1fd480173c4a967f573904555f29 --- /dev/null +++ b/src/main/java/autumn_2023/module/algorithm/KmeansPPClustering.java @@ -0,0 +1,264 @@ +package autumn_2023.module.algorithm; + +import adf.core.agent.info.*; +import adf.core.component.module.algorithm.Clustering; +import adf.core.component.module.algorithm.StaticClustering; +import adf.core.agent.module.ModuleManager; +import adf.core.agent.develop.DevelopData; +import adf.core.agent.precompute.PrecomputeData; +import rescuecore2.worldmodel.EntityID; +import rescuecore2.standard.entities.*; +import static rescuecore2.standard.entities.StandardEntityURN.*; +import java.util.*; +import static java.util.stream.Collectors.*; +import static java.util.Comparator.*; + +public class KmeansPPClustering extends StaticClustering +{ + // エージェントとクラスタの結びつけを保存 + private Map assignment = new HashMap<>(); + // グループ分けとグループの保存 + private KmeansPP clusterer; + // グループ数 + private int n = 0; + // エージェントの種類 + private StandardEntityURN urn; + + // K-means++ 繰り返し実行回数の定義 + private final static int REP_PRECOMPUTE = 20; + + // 事前計算の結果保存用キーの定義 + // 事前計算の結果はPrecomputeDataクラスを通して保存します. + // 文字列によるキーと値のペアで保存できます. + private final static String MODULE_NAME = + "autumn_2023.module.algorithm.KmeansPPClustering"; + private final static String PD_CLUSTER_N = MODULE_NAME + ".n"; + private final static String PD_CLUSTER_M = MODULE_NAME + ".m"; + private final static String PD_CLUSTER_A = MODULE_NAME + ".a"; + + public KmeansPPClustering( + AgentInfo ai, WorldInfo wi, ScenarioInfo si, + ModuleManager mm, DevelopData dd) + { + super(ai, wi, si, mm, dd); + this.urn = this.agentInfo.me().getStandardURN(); + } + + // 保存用キーにエージェントの種類を区別するための接尾辞を足すメソッド + private String addSuffixToKey(String path) + { + return path + "." + this.urn; + } + + // 保存用キーにエージェントの種類と要素番号を区別するための接尾辞を足すメソッド + private String addSuffixToKey(String path, int i) + { + return this.addSuffixToKey(path) + "." + i; + } + + // 事前計算 - 計算実行 & 結果保存部 + @Override + public Clustering precompute(PrecomputeData pd) + { + super.precompute(pd); + // 重複した処理の実行を回避 + if (this.getCountPrecompute() > 1) return this; + + this.initN(); + this.initClusterer(); + // K-means++を実行します. + // 引数は繰り返し実行回数(K-means++アルゴリズムを参照して下さい)を表します. + this.clusterer.execute(REP_PRECOMPUTE); + this.pair(); + + // グループ数を保存します. + // 保存時の引数は (キー, 値)です. + pd.setInteger(this.addSuffixToKey(PD_CLUSTER_N), this.n); + for (EntityID agent : this.assignment.keySet()) + { + int i = this.assignment.get(agent); + // i番目のクラスタの全ての要素を取得します. + Collection cluster = this.clusterer.getClusterMembers(i); + + // i番目のクラスタの要素を保存します. + pd.setEntityIDList( + this.addSuffixToKey(PD_CLUSTER_M, i), + new ArrayList<>(cluster)); + // i番目のクラスタと結び付けられたエージェントを保存します. + pd.setEntityID(this.addSuffixToKey(PD_CLUSTER_A, i), agent); + } + + return this; + } + + // 事前計算 - 計算結果読み込み部 + @Override + public Clustering resume(PrecomputeData pd) + { + super.resume(pd); + // 重複した処理の実行を回避 + if (this.getCountResume() > 1) return this; + + return this; + } + + // 事前計算がおこなわれない場合の処理 + // おそらくprepareの間違い + @Override + public Clustering preparate() + { + super.preparate(); + // 重複した処理の実行を回避 + if (this.getCountPreparate() > 1) return this; + + return this; + } + + @Override + public Clustering calc() + { + return this; + } + + // 他のモジュールがクラスタ数を取得する際に使います + @Override + public int getClusterNumber() + { + return this.n; + } + + // 他のモジュールがentityに関連したクラスタの番号を取得する際に使います + @Override + public int getClusterIndex(StandardEntity entity) + { + return this.getClusterIndex(entity.getID()); + } + + // 他のモジュールがidに関連したクラスタの番号を取得する際に使います + @Override + public int getClusterIndex(EntityID id) + { + if (!this.assignment.containsKey(id)) return -1; + return this.assignment.get(id); + } + + // 他のモジュールがi番目のクラスタ要素をStandardEntityで取得する際に使います + @Override + public Collection getClusterEntities(int i) + { + if (i < 0 || i >= this.n) return null; + + Collection ids = this.getClusterEntityIDs(i); + Collection ret = new ArrayList<>(ids.size()); + for (EntityID id : ids) + { + ret.add(this.worldInfo.getEntity(id)); + } + return ret; + } + + // 他のモジュールがi番目のクラスタ要素をEntityIDで取得する際に使います + @Override + public Collection getClusterEntityIDs(int i) + { + if (i < 0 || i >= this.n) return null; + return this.clusterer.getClusterMembers(i); + } + + private void initN() + { + switch (this.urn) + { + // 消防隊 + case FIRE_BRIGADE: + // グループ数 = シミュレーション全体の消防隊数 + this.n = this.scenarioInfo.getScenarioAgentsFb(); + break; + // 土木隊 + case POLICE_FORCE: + // グループ数 = シミュレーション全体の土木隊数 + this.n = this.scenarioInfo.getScenarioAgentsPf(); + break; + // 救急隊 + case AMBULANCE_TEAM: + // グループ数 = シミュレーション全体の救急隊数 + this.n = this.scenarioInfo.getScenarioAgentsAt(); + break; + default: + this.n = 0; + } + } + private void initClusterer() + { + // 次のオブジェクトを全て取得します. + // 道路/消火栓 + // 建物/ガソリンスタンド + // 避難所 + // 土木隊司令所/消防隊司令所/救急隊司令所 + List entities = new ArrayList<>( + this.worldInfo.getEntitiesOfType( + ROAD, HYDRANT, + BUILDING, GAS_STATION, + REFUGE, + POLICE_OFFICE, FIRE_STATION, AMBULANCE_CENTRE)); + // リストをIDでソートします. + entities.sort(comparing(e -> e.getID().getValue())); + + // データをID/X座標/Y座標の配列に整形します. + int size = entities.size(); + EntityID[] is = new EntityID[size]; + double[] xs = new double[size]; + double[] ys = new double[size]; + + for (int i=0; i agents = new ArrayList<>( + this.worldInfo.getEntitiesOfType(this.urn)); + //リストをIDでソートします. + agents.sort(comparing(e -> e.getID().getValue())); + + // エージェントとクラスタの間の距離を二次元配列に整形します. + int[][] costs = new int[this.n][this.n]; + for (int row=0; row { + + 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 DefaultCommandExecutorAmbulance(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; + 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/sample_team/centralized/DefaultCommandExecutorFire.java b/src/main/java/sample_team/centralized/DefaultCommandExecutorFire.java new file mode 100644 index 0000000000000000000000000000000000000000..ce8af5c077c4f027ee276f8692ba5933c6f8213f --- /dev/null +++ b/src/main/java/sample_team/centralized/DefaultCommandExecutorFire.java @@ -0,0 +1,249 @@ +package sample_team.centralized; + +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 DefaultCommandExecutorFire 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_AUTONOMY = CommandAmbulance.ACTION_AUTONOMY; + + private PathPlanning pathPlanning; + + private ExtAction actionFireRescue; + private ExtAction actionExtMove; + + private int commandType; + private EntityID target; + private EntityID commanderID; + + public DefaultCommandExecutorFire(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( + "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(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.actionFireRescue.updateInfo(messageManager); + this.actionExtMove.updateInfo(messageManager); + + if (this.isCommandCompleted()) { + if (this.commandType != ACTION_UNKNOWN) { + messageManager + .addMessage(new MessageReport(true, true, false, this.commanderID)); + + 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) { + 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; + } + + + 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_AUTONOMY: + if (this.target != null) { + StandardEntity targetEntity = this.worldInfo.getEntity(this.target); + if (targetEntity instanceof Area) { + this.commandType = ACTION_MOVE; + return this.isCommandCompleted(); + } else if (targetEntity instanceof Human) { + Human h = (Human) targetEntity; + if ((h.isHPDefined() && h.getHP() == 0)) { + return true; + } + if (h.getStandardURN() == CIVILIAN) { + this.commandType = ACTION_RESCUE; + } + return this.isCommandCompleted(); + } + } + return true; + } + return true; + } +} \ No newline at end of file diff --git a/src/main/java/sample_team/centralized/DefaultCommandExecutorPolice.java b/src/main/java/sample_team/centralized/DefaultCommandExecutorPolice.java new file mode 100644 index 0000000000000000000000000000000000000000..8db15926c9ecf730cd911b1e502dfb99a73b49ba --- /dev/null +++ b/src/main/java/sample_team/centralized/DefaultCommandExecutorPolice.java @@ -0,0 +1,309 @@ +package sample_team.centralized; + +import static rescuecore2.standard.entities.StandardEntityURN.BLOCKADE; +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.CommandPolice; +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.List; +import java.util.Objects; +import rescuecore2.standard.entities.Area; +import rescuecore2.standard.entities.Blockade; +import rescuecore2.standard.entities.Human; +import rescuecore2.standard.entities.PoliceForce; +import rescuecore2.standard.entities.Road; +import rescuecore2.standard.entities.StandardEntity; +import rescuecore2.worldmodel.EntityID; + +public class DefaultCommandExecutorPolice extends CommandExecutor { + + private static final int ACTION_UNKNOWN = -1; + private static final int ACTION_REST = CommandPolice.ACTION_REST; + private static final int ACTION_MOVE = CommandPolice.ACTION_MOVE; + private static final int ACTION_CLEAR = CommandPolice.ACTION_CLEAR; + private static final int ACTION_AUTONOMY = CommandPolice.ACTION_AUTONOMY; + + private int commandType; + private EntityID target; + private EntityID commanderID; + + private PathPlanning pathPlanning; + + private ExtAction actionExtClear; + private ExtAction actionExtMove; + + public DefaultCommandExecutorPolice(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( + "DefaultCommandExecutorPolice.PathPlanning", + "adf.impl.module.algorithm.DijkstraPathPlanning"); + this.actionExtClear = moduleManager.getExtAction( + "DefaultCommandExecutorPolice.ExtActionClear", + "adf.impl.extaction.DefaultExtActionClear"); + this.actionExtMove = moduleManager.getExtAction( + "DefaultCommandExecutorPolice.ExtActionMove", + "adf.impl.extaction.DefaultExtActionMove"); + break; + } + } + + + @Override + public CommandExecutor setCommand(CommandPolice 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; + } + + + public CommandExecutor precompute(PrecomputeData precomputeData) { + super.precompute(precomputeData); + if (this.getCountPrecompute() >= 2) { + return this; + } + this.pathPlanning.precompute(precomputeData); + this.actionExtClear.precompute(precomputeData); + this.actionExtMove.precompute(precomputeData); + return this; + } + + + public CommandExecutor resume(PrecomputeData precomputeData) { + super.resume(precomputeData); + if (this.getCountResume() >= 2) { + return this; + } + this.pathPlanning.resume(precomputeData); + this.actionExtClear.resume(precomputeData); + this.actionExtMove.resume(precomputeData); + return this; + } + + + public CommandExecutor preparate() { + super.preparate(); + if (this.getCountPreparate() >= 2) { + return this; + } + this.pathPlanning.preparate(); + this.actionExtClear.preparate(); + this.actionExtMove.preparate(); + return this; + } + + + public CommandExecutor updateInfo(MessageManager messageManager) { + super.updateInfo(messageManager); + if (this.getCountUpdateInfo() >= 2) { + return this; + } + this.pathPlanning.updateInfo(messageManager); + this.actionExtClear.updateInfo(messageManager); + this.actionExtMove.updateInfo(messageManager); + + if (this.isCommandCompleted()) { + if (this.commandType != ACTION_UNKNOWN) { + messageManager + .addMessage(new MessageReport(true, true, false, this.commanderID)); + this.commandType = ACTION_UNKNOWN; + this.target = null; + this.commanderID = null; + } + } + return this; + } + + + @Override + public CommandExecutor calc() { + this.result = null; + EntityID position = this.agentInfo.getPosition(); + switch (this.commandType) { + case ACTION_REST: + if (this.target == null) { + if (worldInfo.getEntity(position).getStandardURN() != REFUGE) { + this.pathPlanning.setFrom(position); + this.pathPlanning + .setDestination(this.worldInfo.getEntityIDsOfType(REFUGE)); + List path = this.pathPlanning.calc().getResult(); + if (path != null && path.size() > 0) { + Action action = this.actionExtClear + .setTarget(path.get(path.size() - 1)).calc().getAction(); + if (action == null) { + action = new ActionMove(path); + } + this.result = action; + return this; + } + } + } else if (position.getValue() != this.target.getValue()) { + List path = this.pathPlanning.getResult(position, + this.target); + if (path != null && path.size() > 0) { + Action action = this.actionExtClear + .setTarget(path.get(path.size() - 1)).calc().getAction(); + if (action == null) { + action = new ActionMove(path); + } + this.result = action; + return this; + } + } + this.result = new ActionRest(); + return this; + case ACTION_MOVE: + if (this.target != null) { + this.result = this.actionExtClear.setTarget(this.target).calc() + .getAction(); + } + return this; + case ACTION_CLEAR: + if (this.target != null) { + this.result = this.actionExtClear.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.getStandardURN() == REFUGE) { + PoliceForce agent = (PoliceForce) this.agentInfo.me(); + if (agent.getDamage() > 0) { + if (position.getValue() != this.target.getValue()) { + List path = this.pathPlanning.getResult(position, + this.target); + if (path != null && path.size() > 0) { + Action action = this.actionExtClear + .setTarget(path.get(path.size() - 1)).calc().getAction(); + if (action == null) { + action = new ActionMove(path); + } + this.result = action; + return this; + } + } + this.result = new ActionRest(); + } else { + this.result = this.actionExtClear.setTarget(this.target).calc() + .getAction(); + } + } else if (targetEntity instanceof Area) { + this.result = this.actionExtClear.setTarget(this.target).calc() + .getAction(); + return this; + } else if (targetEntity instanceof Human) { + Human h = (Human) targetEntity; + if ((h.isHPDefined() && h.getHP() == 0)) { + return this; + } + if (h.isPositionDefined() + && this.worldInfo.getPosition(h) instanceof Area) { + this.target = h.getPosition(); + this.result = this.actionExtClear.setTarget(this.target).calc() + .getAction(); + } + } else if (targetEntity.getStandardURN() == BLOCKADE) { + Blockade blockade = (Blockade) targetEntity; + if (blockade.isPositionDefined()) { + this.target = blockade.getPosition(); + this.result = this.actionExtClear.setTarget(this.target).calc() + .getAction(); + } + } + } + return this; + } + + + private boolean isCommandCompleted() { + PoliceForce agent = (PoliceForce) this.agentInfo.me(); + switch (this.commandType) { + case ACTION_REST: + if (this.target == null) { + return (agent.getDamage() == 0); + } + if (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_CLEAR: + if (this.target == null) { + return true; + } + StandardEntity entity = this.worldInfo.getEntity(this.target); + if (entity instanceof Road) { + Road road = (Road) entity; + if (road.isBlockadesDefined()) { + return road.getBlockades().isEmpty(); + } + if (this.agentInfo.getPosition().getValue() != this.target + .getValue()) { + return false; + } + } + return true; + case ACTION_AUTONOMY: + if (this.target != null) { + StandardEntity targetEntity = this.worldInfo.getEntity(this.target); + if (targetEntity.getStandardURN() == REFUGE) { + this.commandType = agent.getDamage() > 0 ? ACTION_REST + : ACTION_CLEAR; + return this.isCommandCompleted(); + } else if (targetEntity instanceof Area) { + this.commandType = ACTION_CLEAR; + return this.isCommandCompleted(); + } else if (targetEntity instanceof Human) { + Human h = (Human) targetEntity; + if ((h.isHPDefined() && h.getHP() == 0)) { + return true; + } + if (h.isPositionDefined() + && this.worldInfo.getPosition(h) instanceof Area) { + this.target = h.getPosition(); + this.commandType = ACTION_CLEAR; + return this.isCommandCompleted(); + } + } else if (targetEntity.getStandardURN() == BLOCKADE) { + Blockade blockade = (Blockade) targetEntity; + if (blockade.isPositionDefined()) { + this.target = blockade.getPosition(); + this.commandType = ACTION_CLEAR; + return this.isCommandCompleted(); + } + } + } + return true; + } + return true; + } +} \ No newline at end of file diff --git a/src/main/java/sample_team/centralized/DefaultCommandExecutorScout.java b/src/main/java/sample_team/centralized/DefaultCommandExecutorScout.java new file mode 100644 index 0000000000000000000000000000000000000000..eb3ea2fd33202e5c3914cd554a93b5113046fb9c --- /dev/null +++ b/src/main/java/sample_team/centralized/DefaultCommandExecutorScout.java @@ -0,0 +1,154 @@ +package sample_team.centralized; + +import static rescuecore2.standard.entities.StandardEntityURN.REFUGE; +import adf.core.agent.action.common.ActionMove; +import adf.core.agent.communication.MessageManager; +import adf.core.agent.communication.standard.bundle.centralized.CommandScout; +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.module.algorithm.PathPlanning; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; +import rescuecore2.standard.entities.Area; +import rescuecore2.worldmodel.AbstractEntity; +import rescuecore2.worldmodel.EntityID; + +public class DefaultCommandExecutorScout extends CommandExecutor { + + private static final int ACTION_UNKNOWN = -1; + private static final int ACTION_SCOUT = 1; + + private PathPlanning pathPlanning; + + private int type; + private Collection scoutTargets; + private EntityID commanderID; + + public DefaultCommandExecutorScout(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) { + super(ai, wi, si, moduleManager, developData); + this.type = ACTION_UNKNOWN; + switch (scenarioInfo.getMode()) { + case PRECOMPUTATION_PHASE: + case PRECOMPUTED: + case NON_PRECOMPUTE: + this.pathPlanning = moduleManager.getModule( + "DefaultCommandExecutorScout.PathPlanning", + "adf.impl.module.algorithm.DijkstraPathPlanning"); + break; + } + } + + + @Override + public CommandExecutor setCommand(CommandScout command) { + EntityID agentID = this.agentInfo.getID(); + if (command.isToIDDefined() && (Objects.requireNonNull(command.getToID()) + .getValue() == agentID.getValue())) { + EntityID target = command.getTargetID(); + if (target == null) { + target = this.agentInfo.getPosition(); + } + this.type = ACTION_SCOUT; + this.commanderID = command.getSenderID(); + this.scoutTargets = new HashSet<>(); + this.scoutTargets.addAll( + worldInfo.getObjectsInRange(target, command.getRange()).stream() + .filter(e -> e instanceof Area && e.getStandardURN() != REFUGE) + .map(AbstractEntity::getID).collect(Collectors.toList())); + } + return this; + } + + + @Override + public CommandExecutor updateInfo(MessageManager messageManager) { + super.updateInfo(messageManager); + if (this.getCountUpdateInfo() >= 2) { + return this; + } + this.pathPlanning.updateInfo(messageManager); + + if (this.isCommandCompleted()) { + if (this.type != ACTION_UNKNOWN) { + messageManager + .addMessage(new MessageReport(true, true, false, this.commanderID)); + this.type = ACTION_UNKNOWN; + this.scoutTargets = 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); + return this; + } + + + @Override + public CommandExecutor resume(PrecomputeData precomputeData) { + super.resume(precomputeData); + if (this.getCountResume() >= 2) { + return this; + } + this.pathPlanning.resume(precomputeData); + return this; + } + + + @Override + public CommandExecutor preparate() { + super.preparate(); + if (this.getCountPreparate() >= 2) { + return this; + } + this.pathPlanning.preparate(); + return this; + } + + + @Override + public CommandExecutor calc() { + this.result = null; + if (this.type == ACTION_SCOUT) { + if (this.scoutTargets == null || this.scoutTargets.isEmpty()) { + return this; + } + this.pathPlanning.setFrom(this.agentInfo.getPosition()); + this.pathPlanning.setDestination(this.scoutTargets); + List path = this.pathPlanning.calc().getResult(); + if (path != null) { + this.result = new ActionMove(path); + } + } + return this; + } + + + private boolean isCommandCompleted() { + if (this.type == ACTION_SCOUT) { + if (this.scoutTargets != null) { + this.scoutTargets + .removeAll(this.worldInfo.getChanged().getChangedEntities()); + } + return (this.scoutTargets == null || this.scoutTargets.isEmpty()); + } + return true; + } +} \ No newline at end of file diff --git a/src/main/java/sample_team/centralized/DefaultCommandExecutorScoutPolice.java b/src/main/java/sample_team/centralized/DefaultCommandExecutorScoutPolice.java new file mode 100644 index 0000000000000000000000000000000000000000..ffd235f5872bc8c7bf1a28726b8e182b1c4d6250 --- /dev/null +++ b/src/main/java/sample_team/centralized/DefaultCommandExecutorScoutPolice.java @@ -0,0 +1,173 @@ +package sample_team.centralized; + +import static rescuecore2.standard.entities.StandardEntityURN.REFUGE; +import adf.core.agent.action.Action; +import adf.core.agent.action.common.ActionMove; +import adf.core.agent.communication.MessageManager; +import adf.core.agent.communication.standard.bundle.centralized.CommandScout; +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.HashSet; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; +import rescuecore2.standard.entities.Area; +import rescuecore2.worldmodel.AbstractEntity; +import rescuecore2.worldmodel.EntityID; + +public class DefaultCommandExecutorScoutPolice + extends CommandExecutor { + + private static final int ACTION_UNKNOWN = -1; + private static final int ACTION_SCOUT = 1; + + private PathPlanning pathPlanning; + + private ExtAction actionExtClear; + + private int commandType; + private Collection scoutTargets; + private EntityID commanderID; + + public DefaultCommandExecutorScoutPolice(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) { + super(ai, wi, si, moduleManager, developData); + this.commandType = ACTION_UNKNOWN; + this.scoutTargets = new HashSet<>(); + this.commanderID = null; + + switch (scenarioInfo.getMode()) { + case PRECOMPUTATION_PHASE: + case PRECOMPUTED: + case NON_PRECOMPUTE: + this.pathPlanning = moduleManager.getModule( + "DefaultCommandExecutorScoutPolice.PathPlanning", + "adf.impl.module.algorithm.DijkstraPathPlanning"); + this.actionExtClear = moduleManager.getExtAction( + "DefaultCommandExecutorScoutPolice.ExtActionClear", + "adf.impl.extaction.DefaultExtActionClear"); + break; + } + } + + + @Override + public CommandExecutor setCommand(CommandScout command) { + EntityID agentID = this.agentInfo.getID(); + if (command.isToIDDefined() && (Objects.requireNonNull(command.getToID()) + .getValue() == agentID.getValue())) { + EntityID target = command.getTargetID(); + if (target == null) { + target = this.agentInfo.getPosition(); + } + this.commandType = ACTION_SCOUT; + this.commanderID = command.getSenderID(); + this.scoutTargets = new HashSet<>(); + this.scoutTargets.addAll( + worldInfo.getObjectsInRange(target, command.getRange()).stream() + .filter(e -> e instanceof Area && e.getStandardURN() != REFUGE) + .map(AbstractEntity::getID).collect(Collectors.toList())); + } + return this; + } + + + public CommandExecutor precompute(PrecomputeData precomputeData) { + super.precompute(precomputeData); + if (this.getCountPrecompute() >= 2) { + return this; + } + this.pathPlanning.precompute(precomputeData); + this.actionExtClear.precompute(precomputeData); + return this; + } + + + public CommandExecutor resume(PrecomputeData precomputeData) { + super.resume(precomputeData); + if (this.getCountResume() >= 2) { + return this; + } + this.pathPlanning.resume(precomputeData); + this.actionExtClear.resume(precomputeData); + return this; + } + + + public CommandExecutor preparate() { + super.preparate(); + if (this.getCountPreparate() >= 2) { + return this; + } + this.pathPlanning.preparate(); + this.actionExtClear.preparate(); + return this; + } + + + public CommandExecutor updateInfo(MessageManager messageManager) { + super.updateInfo(messageManager); + if (this.getCountUpdateInfo() >= 2) { + return this; + } + this.pathPlanning.updateInfo(messageManager); + this.actionExtClear.updateInfo(messageManager); + + if (this.isCommandCompleted()) { + if (this.commandType != ACTION_UNKNOWN) { + messageManager + .addMessage(new MessageReport(true, true, false, this.commanderID)); + this.commandType = ACTION_UNKNOWN; + this.scoutTargets = new HashSet<>(); + this.commanderID = null; + } + } + return this; + } + + + @Override + public CommandExecutor calc() { + this.result = null; + EntityID position = this.agentInfo.getPosition(); + if (this.commandType == ACTION_SCOUT) { + if (this.scoutTargets == null || this.scoutTargets.isEmpty()) { + return this; + } + this.pathPlanning.setFrom(position); + this.pathPlanning.setDestination(this.scoutTargets); + List path = this.pathPlanning.calc().getResult(); + if (path != null) { + EntityID target = path.size() > 0 ? path.get(path.size() - 1) + : position; + Action action = this.actionExtClear.setTarget(target).calc() + .getAction(); + if (action == null) { + action = new ActionMove(path); + } + this.result = action; + } + } + return this; + } + + + private boolean isCommandCompleted() { + if (this.commandType == ACTION_SCOUT) { + if (this.scoutTargets != null) { + this.scoutTargets + .removeAll(this.worldInfo.getChanged().getChangedEntities()); + } + return (this.scoutTargets == null || this.scoutTargets.isEmpty()); + } + return true; + } +} \ No newline at end of file diff --git a/src/main/java/sample_team/centralized/DefaultCommandPickerAmbulance.java b/src/main/java/sample_team/centralized/DefaultCommandPickerAmbulance.java new file mode 100644 index 0000000000000000000000000000000000000000..cafee256ac4a700f6b3f3a103c19700e17e90f59 --- /dev/null +++ b/src/main/java/sample_team/centralized/DefaultCommandPickerAmbulance.java @@ -0,0 +1,79 @@ +package sample_team.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 DefaultCommandPickerAmbulance extends CommandPicker { + + private int scoutDistance; + + private Collection messages; + private Map allocationData; + + public DefaultCommandPickerAmbulance(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() { + 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/sample_team/centralized/DefaultCommandPickerFire.java b/src/main/java/sample_team/centralized/DefaultCommandPickerFire.java new file mode 100644 index 0000000000000000000000000000000000000000..8db724dac211c7b9f58062f5ca7e4965ec523493 --- /dev/null +++ b/src/main/java/sample_team/centralized/DefaultCommandPickerFire.java @@ -0,0 +1,78 @@ +package sample_team.centralized; + +import adf.core.agent.communication.standard.bundle.centralized.CommandFire; +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 DefaultCommandPickerFire extends CommandPicker { + + private int scoutDistance; + + private Collection messages; + private Map allocationData; + + public DefaultCommandPickerFire(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); + } + + + @Override + public CommandPicker + setAllocatorResult(Map allocationData) { + this.allocationData = allocationData; + return this; + } + + + @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); + } 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/sample_team/centralized/DefaultCommandPickerPolice.java b/src/main/java/sample_team/centralized/DefaultCommandPickerPolice.java new file mode 100644 index 0000000000000000000000000000000000000000..1e40a9fa19aae08b29df0665820c2bece3130ca7 --- /dev/null +++ b/src/main/java/sample_team/centralized/DefaultCommandPickerPolice.java @@ -0,0 +1,68 @@ +package sample_team.centralized; + +import adf.core.agent.communication.standard.bundle.centralized.CommandPolice; +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.StandardEntity; +import rescuecore2.standard.entities.StandardEntityURN; +import rescuecore2.worldmodel.EntityID; + +public class DefaultCommandPickerPolice extends CommandPicker { + + private Collection messages; + private Map allocationData; + + public DefaultCommandPickerPolice(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) { + super(ai, wi, si, moduleManager, developData); + this.messages = new ArrayList<>(); + this.allocationData = null; + } + + + @Override + public CommandPicker + setAllocatorResult(Map allocationData) { + this.allocationData = allocationData; + return this; + } + + + @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.POLICE_FORCE) { + StandardEntity target = this.worldInfo + .getEntity(this.allocationData.get(agentID)); + if (target != null) { + if (target instanceof Area) { + CommandPolice command = new CommandPolice(true, agentID, + target.getID(), CommandPolice.ACTION_AUTONOMY); + this.messages.add(command); + } + } + } + } + return this; + } + + + @Override + public Collection getResult() { + return this.messages; + } +} \ No newline at end of file