Commit 6b1f1495 authored by k20066's avatar k20066
Browse files

initialize cluster and centralized

parent 0d776ef2
...@@ -36,21 +36,22 @@ DefaultTacticsPoliceOffice.CommandPicker : adf.impl.centralized.DefaultCommandPi ...@@ -36,21 +36,22 @@ DefaultTacticsPoliceOffice.CommandPicker : adf.impl.centralized.DefaultCommandPi
## SampleSearch ## SampleSearch
SampleSearch.PathPlanning.Ambulance : adf.impl.module.algorithm.DijkstraPathPlanning 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.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.PathPlanning.Police : adf.impl.module.algorithm.DijkstraPathPlanning
SampleSearch.Clustering.Police : adf.impl.module.algorithm.KMeansClustering SampleSearch.Clustering.Police : autumn_2023.module.algorithm.KmeansPPClustering
## SampleBuildDetector ## SampleBuildDetector
SampleBuildingDetector.Clustering : adf.impl.module.algorithm.KMeansClustering #SampleBuildingDetector.Clustering : adf.impl.module.algorithm.KMeansClustering
SampleBuildingDetector.Clustering : autumn_2023.module.algorithm.KmeansPPClustering
## SampleRoadDetector ## SampleRoadDetector
SampleRoadDetector.Clustering : adf.impl.module.algorithm.KMeansClustering SampleRoadDetector.Clustering : autumn_2023.module.algorithm.KmeansPPClustering
SampleRoadDetector.PathPlanning : adf.impl.module.algorithm.DijkstraPathPlanning SampleRoadDetector.PathPlanning : adf.impl.module.algorithm.DijkstraPathPlanning
## SampleHumanDetector ## SampleHumanDetector
SampleHumanDetector.Clustering : adf.impl.module.algorithm.KMeansClustering SampleHumanDetector.Clustering : autumn_2023.module.algorithm.KmeansPPClustering
## DefaultExtActionClear ## DefaultExtActionClear
DefaultExtActionClear.PathPlanning : adf.impl.module.algorithm.DijkstraPathPlanning DefaultExtActionClear.PathPlanning : adf.impl.module.algorithm.DijkstraPathPlanning
......
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<CommandAmbulance> {
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<EntityID> 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<EntityID> 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
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<n;++i) {
toRight[i] = -1;
ofsLeft[i] = 0;
}
for (int i=0;i<m;++i) {
ofsRight[i] = 0;
toLeft[i] = -1;
}
for (int r=0;r<n;++r) {
boolean[] left = new boolean[n];
boolean[] right = new boolean[m];
int[] trace = new int[m];
int[] ptr = new int[m];
left[r] = true;
for (int i=0;i<n;++i) left[i] = false;
for (int i=0;i<m;++i) {
right[i] = false;
trace[i] = -1;
ptr[i] = r;
}
left[r] = true;
for (;;) {
int d = Integer.MAX_VALUE;
for (int j=0;j<m;++j) if (!right[j]) d = Math.min(d, residue(mat, ofsLeft, ofsRight, ptr[j], j));
for (int i=0;i<n;++i) if (left[i]) ofsLeft[i] -= d;
for (int j=0;j<m;++j) if (right[j]) ofsRight[j] += d;
int b = -1;
for (int j=0;j<m;++j) if (!right[j] && residue(mat, ofsLeft, ofsRight, ptr[j], j)==0) b = j;
trace[b] = ptr[b];
int c = toLeft[b];
if (c < 0) {
while (b >= 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<m;++j) if(residue(mat, ofsLeft, ofsRight, c, j) < residue(mat, ofsLeft, ofsRight, ptr[j], j)) ptr[j] = c;
}
}
for (int i=0;i<n;++i) ret[i] = toRight[i];
return ret;
}
public static int residue(int[][] mat, int[] ofsL, int[] ofsR, int i, int j){
return mat[i][j] + ofsL[i] + ofsR[j];
}
}
package autumn_2023.module.algorithm;
import rescuecore2.worldmodel.EntityID;
import java.util.*;
import static java.util.Comparator.*;
public class KmeansPP
{
private EntityID[] targets;
private double[] xs;
private double[] ys;
private Cluster[] result;
private int n;
private static final int COMMON_SEED = 123456789;
public KmeansPP(
EntityID[] targets, double[] xs, double[] ys, int n)
{
this.targets = targets;
this.xs = xs;
this.ys = ys;
this.n = n;
}
public KmeansPP(
int n, List<Collection<EntityID>> memberz)
{
this.result = new Cluster[n];
this.n = n;
for (int i=0; i<n; ++i)
{
Collection<EntityID> 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<rep; ++i)
{
Arrays.stream(this.result).forEach(Cluster::clearMembers);
for (int j=0; j<this.targets.length; ++j)
assign(this.result, this.targets[j], this.xs[j], this.ys[j]);
Arrays.stream(this.result).forEach(Cluster::updateCenter);
}
}
public int getClusterNumber()
{
return this.result.length;
}
public double getClusterX(int i)
{
return this.result[i].getCX();
}
public double getClusterY(int i)
{
return this.result[i].getCY();
}
public Collection<EntityID> 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<n; ++i) ret[i] = new Cluster();
Random random = new Random(COMMON_SEED);
int r = random.nextInt(targets.length);
ret[0].addMember(targets[r], xs[r], ys[r]);
ret[0].updateCenter();
boolean[] assigned = new boolean[targets.length];
assigned[r] = true;
for (int i=0; i<n-1; ++i)
{
double[] ds = new double[targets.length];
double sumd = 0.0;
for (int j=0; j<targets.length; ++j)
{
if (assigned[j]) continue;
double cx = ret[i].getCX();
double cy = ret[i].getCY();
double x = xs[j];
double y = ys[j];
ds[j] = Math.hypot(x-cx, y-cy);
sumd += ds[j];
}
for(int j=0; j<targets.length; ++j) ds[j] /= sumd;
double p = random.nextDouble();
double accp = 0.0;
for(int j=0; j<targets.length; ++j)
{
if (assigned[j]) continue;
accp += ds[j];
if (p <= accp)
{
ret[i+1].addMember(targets[j], xs[j], ys[j]);
ret[i+1].updateCenter();
assigned[j] = true;
break;
}
}
}
return ret;
}
private static void assign(
Cluster[] clusters, EntityID id, double x, double y)
{
Optional<Cluster> 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<EntityID> members = new LinkedList<>();
private double sumx = 0.0;
private double sumy = 0.0;
public Cluster() {}
public Cluster(Collection<EntityID> 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<EntityID> 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;
}
}
}
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<EntityID, Integer> 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<EntityID> 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<StandardEntity> getClusterEntities(int i)
{
if (i < 0 || i >= this.n) return null;
Collection<EntityID> ids = this.getClusterEntityIDs(i);
Collection<StandardEntity> ret = new ArrayList<>(ids.size());
for (EntityID id : ids)
{
ret.add(this.worldInfo.getEntity(id));
}
return ret;
}
// 他のモジュールがi番目のクラスタ要素をEntityIDで取得する際に使います
@Override
public Collection<EntityID> 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<StandardEntity> 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<size; ++i)
{
// StandardEntityクラスでは座標を得ることができません.
// 地図上のオブジェクトを表すAreaクラスにダウンキャストします.
// (エージェントの場合はHumanクラス)
Area area = (Area)entities.get(i);
is[i] = area.getID();
xs[i] = area.getX();
ys[i] = area.getY();
}
// KmeansPPの初期化をおこないます.
this.clusterer = new KmeansPP(is, xs, ys, this.n);
}
private void pair()
{
// 同種類のエージェントを全て取得します.
List<StandardEntity> 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<this.n; ++row)
{
Human agent = (Human)agents.get(row);
double x = agent.getX();
double y = agent.getY();
for (int col=0; col<this.n; ++col)
{
// col番目のクラスタのX/Y座標を取得します.
double cx = this.clusterer.getClusterX(col);
double cy = this.clusterer.getClusterY(col);
// エージェントの座標とクラスタの座標間の距離を計算します.
costs[row][col] = (int)Math.hypot(cx-x, cy-y);
}
}
// Hungarianアルゴリズムに距離の二次元配列を渡して結果を得る.
// 結果 row番目: row番目のエージェントが割り当てられたクラスタの番号
int[] result = Hungarian.execute(costs);
for (int row=0; row<n; ++row)
{
EntityID id = agents.get(row).getID();
// エージェントのIDとクラスタの番号を結びつけて保存する.
this.assignment.put(id, result[row]);
}
}
}
package sample_team.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 DefaultCommandExecutorAmbulance
extends CommandExecutor<CommandAmbulance> {
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<EntityID> 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<EntityID> 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
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<CommandAmbulance> {
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<EntityID> 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<EntityID> 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
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<CommandPolice> {
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<EntityID> 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<EntityID> 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<EntityID> 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
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<CommandScout> {
private static final int ACTION_UNKNOWN = -1;
private static final int ACTION_SCOUT = 1;
private PathPlanning pathPlanning;
private int type;
private Collection<EntityID> 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<EntityID> 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
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<CommandScout> {
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<EntityID> 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<CommandScout> 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<EntityID> 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
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<CommunicationMessage> messages;
private Map<EntityID, EntityID> 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<EntityID, EntityID> 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<CommunicationMessage> getResult() {
return this.messages;
}
}
\ No newline at end of file
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<CommunicationMessage> messages;
private Map<EntityID, EntityID> 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<EntityID, EntityID> 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<CommunicationMessage> getResult() {
return this.messages;
}
}
\ No newline at end of file
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<CommunicationMessage> messages;
private Map<EntityID, EntityID> 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<EntityID, EntityID> 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<CommunicationMessage> getResult() {
return this.messages;
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment