Commit 367bf39b authored by K20014's avatar K20014
Browse files

作業途中のコミット

parent 11dbfe3d
...@@ -7,7 +7,8 @@ DefaultTacticsAmbulanceTeam.CommandExecutorAmbulance : adf.impl.centralized.Defa ...@@ -7,7 +7,8 @@ DefaultTacticsAmbulanceTeam.CommandExecutorAmbulance : adf.impl.centralized.Defa
DefaultTacticsAmbulanceTeam.CommandExecutorScout : adf.impl.centralized.DefaultCommandExecutorScout DefaultTacticsAmbulanceTeam.CommandExecutorScout : adf.impl.centralized.DefaultCommandExecutorScout
## DefaultTacticsFireBrigade ## DefaultTacticsFireBrigade
DefaultTacticsFireBrigade.HumanDetector : sample_team.module.complex.SampleHumanDetector ## DefaultTacticsFireBrigade.HumanDetector : sample_team.module.complex.SampleHumanDetector
DefaultTacticsFireBrigade.HumanDetector : autumn_2023.module.complex.SampleHumanDetector
DefaultTacticsFireBrigade.Search : sample_team.module.complex.SampleSearch DefaultTacticsFireBrigade.Search : sample_team.module.complex.SampleSearch
DefaultTacticsFireBrigade.ExtActionFireRescue : adf.impl.extaction.DefaultExtActionFireRescue DefaultTacticsFireBrigade.ExtActionFireRescue : adf.impl.extaction.DefaultExtActionFireRescue
DefaultTacticsFireBrigade.ExtActionMove : adf.impl.extaction.DefaultExtActionMove DefaultTacticsFireBrigade.ExtActionMove : adf.impl.extaction.DefaultExtActionMove
...@@ -52,6 +53,8 @@ SampleRoadDetector.PathPlanning : adf.impl.module.algorithm.DijkstraPathPlanning ...@@ -52,6 +53,8 @@ SampleRoadDetector.PathPlanning : adf.impl.module.algorithm.DijkstraPathPlanning
## SampleHumanDetector ## SampleHumanDetector
SampleHumanDetector.Clustering : autumn_2023.module.algorithm.KmeansPPClustering SampleHumanDetector.Clustering : autumn_2023.module.algorithm.KmeansPPClustering
# 追加
SampleHumanDetector.PathPlanning : adf.impl.module.algorithm.DijkstraPathPlanning
## DefaultExtActionClear ## DefaultExtActionClear
DefaultExtActionClear.PathPlanning : adf.impl.module.algorithm.DijkstraPathPlanning DefaultExtActionClear.PathPlanning : adf.impl.module.algorithm.DijkstraPathPlanning
......
package autumn_2023.centralized; package autumn_2023.centralized;
import static rescuecore2.standard.entities.StandardEntityURN.CIVILIAN; public class AuctionCommandExecutorFire {
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 AuctionCommandExecutorFire 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 AuctionCommandExecutorFire(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData)
{
super(ai, wi, si, moduleManager, developData);
this.commandType = ACTION_UNKNOWN;
switch (scenarioInfo.getMode())
{
case PRECOMPUTATION_PHASE:
case PRECOMPUTED:
case NON_PRECOMPUTE:
// autumn_2023用に書き換える・module.config
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;
}
} }
package autumn_2023.centralized; package autumn_2023.centralized;
public class AuctionCommandExecutorAmbulance { public class AuctionCommandPickerFire {
} }
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.comm;
import adf.core.agent.communication.MessageManager;
import adf.core.agent.info.AgentInfo;
import adf.core.agent.info.ScenarioInfo;
import adf.core.agent.info.WorldInfo;
import adf.core.component.communication.ChannelSubscriber;
import rescuecore2.standard.entities.StandardEntityURN;
public class AuctionChannelSubscriber extends ChannelSubscriber{
@Override
public void subscribe(AgentInfo agentInfo, WorldInfo worldInfo,
ScenarioInfo scenarioInfo, MessageManager messageManager) {
// subscribe only once at the beginning
if (agentInfo.getTime() == 1) {
int numChannels = scenarioInfo.getCommsChannelsCount() - 1; // 0th channel
// is the
// voice
// channel
int maxChannelCount = 0;
boolean isPlatoon = isPlatoonAgent(agentInfo, worldInfo);
if (isPlatoon) {
maxChannelCount = scenarioInfo.getCommsChannelsMaxPlatoon();
} else {
maxChannelCount = scenarioInfo.getCommsChannelsMaxOffice();
}
StandardEntityURN agentType = getAgentType(agentInfo, worldInfo);
int[] channels = new int[maxChannelCount];
for (int i = 0; i < maxChannelCount; i++) {
channels[i] = getChannelNumber(agentType, i, numChannels);
}
messageManager.subscribeToChannels(channels);
}
}
protected boolean isPlatoonAgent(AgentInfo agentInfo, WorldInfo worldInfo) {
StandardEntityURN agentType = getAgentType(agentInfo, worldInfo);
if (agentType == StandardEntityURN.FIRE_BRIGADE
|| agentType == StandardEntityURN.POLICE_FORCE
|| agentType == StandardEntityURN.AMBULANCE_TEAM) {
return true;
}
return false;
}
protected StandardEntityURN getAgentType(AgentInfo agentInfo,
WorldInfo worldInfo) {
StandardEntityURN agentType = worldInfo.getEntity(agentInfo.getID())
.getStandardURN();
return agentType;
}
public static int getChannelNumber(StandardEntityURN agentType,
int channelIndex, int numChannels) {
int agentIndex = 0;
if (agentType == StandardEntityURN.FIRE_BRIGADE
|| agentType == StandardEntityURN.FIRE_STATION) {
agentIndex = 1;
} else if (agentType == StandardEntityURN.POLICE_FORCE
|| agentType == StandardEntityURN.POLICE_OFFICE) {
agentIndex = 2;
} else if (agentType == StandardEntityURN.AMBULANCE_TEAM
|| agentType == StandardEntityURN.AMBULANCE_CENTRE) {
agentIndex = 3;
}
int index = (3 * channelIndex) + agentIndex;
if ((index % numChannels) == 0) {
index = numChannels;
} else {
index = index % numChannels;
}
return index;
}
public static void main(String[] args) {
int numChannels = 6;
int maxChannels = 2;
for (int i = 0; i < maxChannels; i++) {
System.out.println("FIREBRIGADE-" + i + ":"
+ getChannelNumber(StandardEntityURN.FIRE_BRIGADE, i, numChannels));
}
for (int i = 0; i < maxChannels; i++) {
System.out.println("POLICE-" + i + ":"
+ getChannelNumber(StandardEntityURN.POLICE_OFFICE, i, numChannels));
}
for (int i = 0; i < maxChannels; i++) {
System.out.println("AMB-" + i + ":" + getChannelNumber(
StandardEntityURN.AMBULANCE_CENTRE, i, numChannels));
}
}
}
package autumn_2023.module.comm;
import adf.core.agent.communication.MessageManager;
import adf.core.agent.communication.standard.bundle.StandardMessage;
import adf.core.agent.communication.standard.bundle.StandardMessagePriority;
import adf.core.agent.communication.standard.bundle.centralized.CommandAmbulance;
import adf.core.agent.communication.standard.bundle.centralized.CommandFire;
import adf.core.agent.communication.standard.bundle.centralized.CommandPolice;
import adf.core.agent.communication.standard.bundle.centralized.CommandScout;
import adf.core.agent.communication.standard.bundle.centralized.MessageReport;
import adf.core.agent.communication.standard.bundle.information.MessageAmbulanceTeam;
import adf.core.agent.communication.standard.bundle.information.MessageBuilding;
import adf.core.agent.communication.standard.bundle.information.MessageCivilian;
import adf.core.agent.communication.standard.bundle.information.MessageFireBrigade;
import adf.core.agent.communication.standard.bundle.information.MessagePoliceForce;
import adf.core.agent.communication.standard.bundle.information.MessageRoad;
import adf.core.agent.info.AgentInfo;
import adf.core.agent.info.ScenarioInfo;
import adf.core.agent.info.WorldInfo;
import adf.core.component.communication.CommunicationMessage;
import adf.core.component.communication.MessageCoordinator;
import autumn_2023.module.comm.infomation.MessageCost;
import java.util.ArrayList;
import java.util.List;
import rescuecore2.standard.entities.StandardEntityURN;
public class AuctionMessageCoordinator extends MessageCoordinator{
@Override
public void coordinate(AgentInfo agentInfo, WorldInfo worldInfo,
ScenarioInfo scenarioInfo, MessageManager messageManager,
ArrayList<CommunicationMessage> sendMessageList,
List<List<CommunicationMessage>> channelSendMessageList) {
// have different lists for every agent
ArrayList<CommunicationMessage> policeMessages = new ArrayList<>();
ArrayList<CommunicationMessage> ambulanceMessages = new ArrayList<>();
ArrayList<CommunicationMessage> fireBrigadeMessages = new ArrayList<>();
ArrayList<CommunicationMessage> voiceMessages = new ArrayList<>();
StandardEntityURN agentType = getAgentType(agentInfo, worldInfo);
for (CommunicationMessage msg : sendMessageList) {
if (msg instanceof StandardMessage
&& !((StandardMessage) msg).isRadio()) {
voiceMessages.add(msg);
} else {
if (msg instanceof MessageBuilding) {
fireBrigadeMessages.add(msg);
} else if (msg instanceof MessageCivilian) {
ambulanceMessages.add(msg);
// 追加
fireBrigadeMessages.add(msg);
} else if (msg instanceof MessageRoad) {
fireBrigadeMessages.add(msg);
ambulanceMessages.add(msg);
policeMessages.add(msg);
} else if (msg instanceof CommandAmbulance) {
ambulanceMessages.add(msg);
} else if (msg instanceof CommandFire) {
fireBrigadeMessages.add(msg);
} else if (msg instanceof CommandPolice) {
policeMessages.add(msg);
} else if (msg instanceof CommandScout) {
if (agentType == StandardEntityURN.FIRE_STATION) {
fireBrigadeMessages.add(msg);
} else if (agentType == StandardEntityURN.POLICE_OFFICE) {
policeMessages.add(msg);
} else if (agentType == StandardEntityURN.AMBULANCE_CENTRE) {
ambulanceMessages.add(msg);
}
} else if (msg instanceof MessageReport) {
if (agentType == StandardEntityURN.FIRE_BRIGADE) {
fireBrigadeMessages.add(msg);
} else if (agentType == StandardEntityURN.POLICE_FORCE) {
policeMessages.add(msg);
} else if (agentType == StandardEntityURN.AMBULANCE_TEAM) {
ambulanceMessages.add(msg);
}
} else if (msg instanceof MessageFireBrigade) {
fireBrigadeMessages.add(msg);
ambulanceMessages.add(msg);
policeMessages.add(msg);
} else if (msg instanceof MessagePoliceForce) {
ambulanceMessages.add(msg);
policeMessages.add(msg);
} else if (msg instanceof MessageAmbulanceTeam) {
ambulanceMessages.add(msg);
policeMessages.add(msg);
// 新規メッセージクラス
} else if (msg instanceof MessageCost) {
ambulanceMessages.add(msg);
fireBrigadeMessages.add(msg);
}
}
}
if (scenarioInfo.getCommsChannelsCount() > 1) {
// send radio messages if there are more than one communication channel
int[] channelSize = new int[scenarioInfo.getCommsChannelsCount() - 1];
setSendMessages(scenarioInfo, StandardEntityURN.POLICE_FORCE, agentInfo,
worldInfo, policeMessages, channelSendMessageList, channelSize);
setSendMessages(scenarioInfo, StandardEntityURN.AMBULANCE_TEAM, agentInfo,
worldInfo, ambulanceMessages, channelSendMessageList, channelSize);
setSendMessages(scenarioInfo, StandardEntityURN.FIRE_BRIGADE, agentInfo,
worldInfo, fireBrigadeMessages, channelSendMessageList, channelSize);
}
ArrayList<StandardMessage> voiceMessageLowList = new ArrayList<>();
ArrayList<StandardMessage> voiceMessageNormalList = new ArrayList<>();
ArrayList<StandardMessage> voiceMessageHighList = new ArrayList<>();
for (CommunicationMessage msg : voiceMessages) {
if (msg instanceof StandardMessage) {
StandardMessage m = (StandardMessage) msg;
switch (m.getSendingPriority()) {
case LOW:
voiceMessageLowList.add(m);
break;
case NORMAL:
voiceMessageNormalList.add(m);
break;
case HIGH:
voiceMessageHighList.add(m);
break;
}
}
}
// set the voice channel messages
channelSendMessageList.get(0).addAll(voiceMessageHighList);
channelSendMessageList.get(0).addAll(voiceMessageNormalList);
channelSendMessageList.get(0).addAll(voiceMessageLowList);
}
protected int[] getChannelsByAgentType(StandardEntityURN agentType,
AgentInfo agentInfo, WorldInfo worldInfo, ScenarioInfo scenarioInfo,
int channelIndex) {
int numChannels = scenarioInfo.getCommsChannelsCount() - 1; // 0th channel
// is the voice
// channel
int maxChannelCount = 0;
boolean isPlatoon = isPlatoonAgent(agentInfo, worldInfo);
if (isPlatoon) {
maxChannelCount = scenarioInfo.getCommsChannelsMaxPlatoon();
} else {
maxChannelCount = scenarioInfo.getCommsChannelsMaxOffice();
}
int[] channels = new int[maxChannelCount];
for (int i = 0; i < maxChannelCount; i++) {
channels[i] = AuctionChannelSubscriber.getChannelNumber(agentType, i,
numChannels);
}
return channels;
}
protected boolean isPlatoonAgent(AgentInfo agentInfo, WorldInfo worldInfo) {
StandardEntityURN agentType = getAgentType(agentInfo, worldInfo);
if (agentType == StandardEntityURN.FIRE_BRIGADE
|| agentType == StandardEntityURN.POLICE_FORCE
|| agentType == StandardEntityURN.AMBULANCE_TEAM) {
return true;
}
return false;
}
protected StandardEntityURN getAgentType(AgentInfo agentInfo,
WorldInfo worldInfo) {
StandardEntityURN agentType = worldInfo.getEntity(agentInfo.getID())
.getStandardURN();
return agentType;
}
protected void setSendMessages(ScenarioInfo scenarioInfo,
StandardEntityURN agentType, AgentInfo agentInfo, WorldInfo worldInfo,
List<CommunicationMessage> messages,
List<List<CommunicationMessage>> channelSendMessageList,
int[] channelSize) {
int channelIndex = 0;
int[] channels = getChannelsByAgentType(agentType, agentInfo, worldInfo,
scenarioInfo, channelIndex);
int channel = channels[channelIndex];
int channelCapacity = scenarioInfo.getCommsChannelBandwidth(channel);
// start from HIGH, NORMAL, to LOW
for (int i = StandardMessagePriority.values().length - 1; i >= 0; i--) {
for (CommunicationMessage msg : messages) {
StandardMessage smsg = (StandardMessage) msg;
if (smsg.getSendingPriority() == StandardMessagePriority.values()[i]) {
channelSize[channel - 1] += smsg.getByteArraySize();
if (channelSize[channel - 1] > channelCapacity) {
channelSize[channel - 1] -= smsg.getByteArraySize();
channelIndex++;
if (channelIndex < channels.length) {
channel = channels[channelIndex];
channelCapacity = scenarioInfo.getCommsChannelBandwidth(channel);
channelSize[channel - 1] += smsg.getByteArraySize();
} else {
// if there is no new channel for that message types, just break
break;
}
}
channelSendMessageList.get(channel).add(smsg);
}
}
}
}
}
package autumn_2023.module.comm.infomation;
import javax.annotation.Nonnull;
import adf.core.agent.communication.standard.bundle.StandardMessage;
import adf.core.agent.communication.standard.bundle.StandardMessagePriority;
import adf.core.component.communication.util.BitOutputStream;
import adf.core.component.communication.util.BitStreamReader;
import rescuecore2.worldmodel.EntityID;
public class MessageCost extends StandardMessage{
// int型の容量(32bit:4byte)
public static final int SIZE_COST = 32;
public static final int SIZE_TARGET = 32;
private final EntityID targetId;
private final int cost;
public MessageCost(boolean isRadio, EntityID target, int cost)
{
super(isRadio, StandardMessagePriority.NORMAL);
this.targetId = target;
this.cost = cost;
}
public MessageCost(boolean isRadio, StandardMessagePriority sendingPriority, EntityID target, int cost)
{
super(isRadio, sendingPriority);
this.targetId = target;
this.cost = cost;
}
public MessageCost(boolean isRadio, int from, int ttl, @Nonnull BitStreamReader bitStreamReader)
{
super(isRadio, from, ttl, bitStreamReader);
this.targetId = new EntityID(bitStreamReader.getBits(SIZE_TARGET));
this.cost = bitStreamReader.getBits(SIZE_COST);
}
public EntityID getTargetID()
{
return targetId;
}
public int cost()
{
return cost;
}
@Override
public int getByteArraySize()
{
return this.toBitOutputStream().size();
}
@Override
public byte[] toByteArray()
{
return this.toBitOutputStream().toByteArray();
}
@Override
public BitOutputStream toBitOutputStream()
{
final BitOutputStream bitOutputStream = new BitOutputStream();
bitOutputStream.writeBits(this.targetId.getValue(), SIZE_TARGET);
bitOutputStream.writeBits(this.cost, SIZE_COST);
return bitOutputStream;
}
@Override
public String getCheckKey()
{
return String.format("TargetID : " + this.targetId.getValue() + " Cost : " + this.cost);
}
}
package autumn_2023.module.complex;
public class AuctionFBHumanDetector {
}
package autumn_2023.module.complex;
public class AuctionFBSearch {
}
package autumn_2023.module.complex;
import adf.core.agent.communication.MessageManager;
import adf.core.agent.communication.standard.bundle.MessageUtil;
import adf.core.agent.communication.standard.bundle.information.MessageCivilian;
import adf.core.agent.develop.DevelopData;
import adf.core.agent.info.AgentInfo;
import adf.core.agent.info.ScenarioInfo;
import adf.core.agent.info.WorldInfo;
import adf.core.agent.module.ModuleManager;
import adf.core.agent.precompute.PrecomputeData;
import adf.core.component.communication.CommunicationMessage;
import adf.core.component.module.complex.FireTargetAllocator;
import autumn_2023.module.comm.infomation.MessageCost;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import rescuecore2.standard.entities.AmbulanceCentre;
import rescuecore2.standard.entities.AmbulanceTeam;
import rescuecore2.standard.entities.FireBrigade;
import rescuecore2.worldmodel.EntityID;
public class AuctionFireTargetAllocator extends FireTargetAllocator{
// 分散エージェントからの埋没市民のメッセージリスト
private List<EntityID> receivedAgentCivilians = new ArrayList<>();
private List<EntityID> finishreceivedCivilians = new ArrayList<>();
// 分散エージェントからのコストのメッセージリスト
private List<MessageCost> RescueCosts = new ArrayList<>();
public AuctionFireTargetAllocator(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData)
{
super(ai, wi, si, moduleManager, developData);
}
@Override
public FireTargetAllocator resume(PrecomputeData precomputeData)
{
super.resume(precomputeData);
return this;
}
@Override
public FireTargetAllocator preparate()
{
super.preparate();
return this;
}
@Override
public Map<EntityID, EntityID> getResult()
{
return new HashMap<>();
}
@Override
public FireTargetAllocator calc()
{
return this;
}
@Override
public FireTargetAllocator updateInfo(MessageManager messageManager)
{
super.updateInfo(messageManager);
// そのステップで受信した(前ステップで送信された)メッセージ一覧
List<CommunicationMessage> messages = messageManager.getReceivedMessageList();
for(CommunicationMessage mes : messages)
{
// mesが市民の情報であれば
if (mes instanceof MessageCivilian)
{
MessageCivilian mesCiv = (MessageCivilian) mes;
MessageUtil.reflectMessage(this.worldInfo, mesCiv);
if (this.worldInfo.getEntity(mesCiv.getSenderID()) instanceof AmbulanceTeam
|| this.worldInfo.getEntity(mesCiv.getSenderID()) instanceof FireBrigade)
{
if(!receivedAgentCivilians.contains(mesCiv.getAgentID())
|| !finishreceivedCivilians.contains(mesCiv.getAgentID()))
{
receivedAgentCivilians.add(mesCiv.getAgentID());
}
}
}
// mesがコストの情報であれば
if (mes instanceof MessageCost) {
}
}
return this;
}
}
package autumn_2023.module.complex;
import adf.core.agent.communication.MessageManager;
import adf.core.agent.develop.DevelopData;
import adf.core.agent.info.AgentInfo;
import adf.core.agent.info.ScenarioInfo;
import adf.core.agent.info.WorldInfo;
import adf.core.agent.module.ModuleManager;
import adf.core.agent.precompute.PrecomputeData;
import adf.core.component.module.complex.AmbulanceTargetAllocator;
import java.util.HashMap;
import java.util.Map;
import rescuecore2.worldmodel.EntityID;
public class SampleAmbulanceTargetAllocator extends AmbulanceTargetAllocator {
public SampleAmbulanceTargetAllocator(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) {
super(ai, wi, si, moduleManager, developData);
}
@Override
public AmbulanceTargetAllocator resume(PrecomputeData precomputeData) {
super.resume(precomputeData);
return this;
}
@Override
public AmbulanceTargetAllocator preparate() {
super.preparate();
return this;
}
@Override
public Map<EntityID, EntityID> getResult() {
return new HashMap<>();
}
@Override
public AmbulanceTargetAllocator calc() {
return this;
}
@Override
public AmbulanceTargetAllocator updateInfo(MessageManager messageManager) {
super.updateInfo(messageManager);
return this;
}
}
\ No newline at end of file
package autumn_2023.module.complex;
import static rescuecore2.standard.entities.StandardEntityURN.AMBULANCE_TEAM;
import static rescuecore2.standard.entities.StandardEntityURN.CIVILIAN;
import static rescuecore2.standard.entities.StandardEntityURN.REFUGE;
import adf.core.agent.communication.MessageManager;
import adf.core.agent.communication.standard.bundle.MessageUtil;
import adf.core.agent.communication.standard.bundle.StandardMessagePriority;
import adf.core.agent.communication.standard.bundle.information.MessageCivilian;
import adf.core.agent.develop.DevelopData;
import adf.core.agent.info.AgentInfo;
import adf.core.agent.info.ScenarioInfo;
import adf.core.agent.info.WorldInfo;
import adf.core.agent.module.ModuleManager;
import adf.core.component.communication.CommunicationMessage;
import adf.core.component.module.algorithm.Clustering;
import adf.core.component.module.algorithm.PathPlanning;
import adf.core.component.module.complex.HumanDetector;
import adf.core.debug.DefaultLogger;
import autumn_2023.module.comm.infomation.MessageCost;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.log4j.Logger;
import rescuecore2.standard.entities.AmbulanceCentre;
import rescuecore2.standard.entities.Civilian;
import rescuecore2.standard.entities.Human;
import rescuecore2.standard.entities.StandardEntity;
import rescuecore2.standard.entities.StandardEntityURN;
import rescuecore2.worldmodel.EntityID;
public class SampleHumanDetector extends HumanDetector {
private Clustering clustering;
private EntityID result;
private Logger logger;
// 救急司令所からの埋没市民のメッセージリスト
private List<EntityID> receivedCentreCivilians = new ArrayList<>();
// コスト計算用パスプランニング
private PathPlanning pathplanning;
public SampleHumanDetector(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) {
super(ai, wi, si, moduleManager, developData);
logger = DefaultLogger.getLogger(agentInfo.me());
this.clustering = moduleManager.getModule("SampleHumanDetector.Clustering",
"adf.impl.module.algorithm.KMeansClustering");
// 追加
this.pathplanning = moduleManager.getModule("SampleHumanDetector.PathPlanning", "adf.impl.module.algorithm.DijkstraPathPlanning");
registerModule(this.clustering);
}
@Override
public HumanDetector updateInfo(MessageManager messageManager) {
logger.debug("Time:" + agentInfo.getTime());
super.updateInfo(messageManager);
this.pathplanning.updateInfo(messageManager);
// そのステップで受信した(前ステップで送信された)メッセージ一覧
List<CommunicationMessage> messages = messageManager.getReceivedMessageList();
for(CommunicationMessage ms : messages)
{
// msが市民の情報であれば
if (ms instanceof MessageCivilian)
{
MessageCivilian msCiv = (MessageCivilian) ms;
MessageUtil.reflectMessage(this.worldInfo, msCiv);
if (this.worldInfo.getEntity(msCiv.getSenderID()) instanceof AmbulanceCentre)
{
if(!receivedCentreCivilians.contains(msCiv.getAgentID()))
{
receivedCentreCivilians.add(msCiv.getAgentID());
}
}
}
}
for(EntityID receive : receivedCentreCivilians)
{
int taskcost = CalculationCost(receive);
// 新規メッセージクラスを用いて救急司令所に送信
CommunicationMessage costmes = new MessageCost(true, StandardMessagePriority.HIGH, receive, taskcost);
messageManager.addMessage(costmes);
}
// このステップ内で知覚したエンティティのうち,救助対象の市民リストを作成
Set<EntityID> changed = this.worldInfo.getChanged().getChangedEntities();
List<EntityID> changedRescueTargets = filterRescueTargets((Collection)changed);
// 司令所に送るメッセージを作成・送信(現状:救急司令所のみ)
// 市民の情報を送信
for(EntityID change : changedRescueTargets)
{
Civilian changeCivilian = (Civilian)this.worldInfo.getEntity(change);
CommunicationMessage message = new MessageCivilian(true, StandardMessagePriority.HIGH, changeCivilian);
messageManager.addMessage(message);
}
return this;
}
@Override
public HumanDetector calc() {
Human transportHuman = this.agentInfo.someoneOnBoard();
if (transportHuman != null) {
logger.debug("someoneOnBoard:" + transportHuman);
this.result = transportHuman.getID();
return this;
}
if (this.result != null) {
Human target = (Human) this.worldInfo.getEntity(this.result);
if (!isValidHuman(target)) {
logger.debug("Invalid Human:" + target + " ==>reset target");
this.result = null;
}
}
if (this.result == null) {
this.result = calcTarget();
}
return this;
}
private EntityID calcTarget() {
List<Human> rescueTargets = filterRescueTargets(
this.worldInfo.getEntitiesOfType(CIVILIAN));
List<Human> rescueTargetsInCluster = filterInCluster(rescueTargets);
List<Human> targets = rescueTargetsInCluster;
if (targets.isEmpty())
targets = rescueTargets;
logger.debug("Targets:" + targets);
if (!targets.isEmpty()) {
targets.sort(new DistanceSorter(this.worldInfo, this.agentInfo.me()));
Human selected = targets.get(0);
logger.debug("Selected:" + selected);
return selected.getID();
}
return null;
}
@Override
public EntityID getTarget() {
return this.result;
}
private List<Human>
filterRescueTargets(Collection<? extends StandardEntity> list) {
List<Human> rescueTargets = new ArrayList<>();
for (StandardEntity next : list) {
if (!(next instanceof Human))
continue;
Human h = (Human) next;
if (!isValidHuman(h))
continue;
if (h.getBuriedness() == 0)
continue;
rescueTargets.add(h);
}
return rescueTargets;
}
private List<Human>
filterInCluster(Collection<? extends StandardEntity> entities) {
int clusterIndex = clustering.getClusterIndex(this.agentInfo.getID());
List<Human> filter = new ArrayList<>();
HashSet<StandardEntity> inCluster = new HashSet<>(
clustering.getClusterEntities(clusterIndex));
for (StandardEntity next : entities) {
if (!(next instanceof Human))
continue;
Human h = (Human) next;
if (!h.isPositionDefined())
continue;
StandardEntity position = this.worldInfo.getPosition(h);
if (position == null)
continue;
if (!inCluster.contains(position))
continue;
filter.add(h);
}
return filter;
}
private class DistanceSorter implements Comparator<StandardEntity> {
private StandardEntity reference;
private WorldInfo worldInfo;
DistanceSorter(WorldInfo wi, StandardEntity reference) {
this.reference = reference;
this.worldInfo = wi;
}
public int compare(StandardEntity a, StandardEntity b) {
int d1 = this.worldInfo.getDistance(this.reference, a);
int d2 = this.worldInfo.getDistance(this.reference, b);
return d1 - d2;
}
}
private boolean isValidHuman(StandardEntity entity) {
if (entity == null)
return false;
if (!(entity instanceof Human))
return false;
Human target = (Human) entity;
if (!target.isHPDefined() || target.getHP() == 0)
return false;
if (!target.isPositionDefined())
return false;
if (!target.isDamageDefined() || target.getDamage() == 0)
return false;
if (!target.isBuriednessDefined())
return false;
StandardEntity position = worldInfo.getPosition(target);
if (position == null)
return false;
StandardEntityURN positionURN = position.getStandardURN();
if (positionURN == REFUGE || positionURN == AMBULANCE_TEAM)
return false;
return true;
}
// cost計算
private int CalculationCost(EntityID target){
double targetDistance = this.pathplanning.getDistance(this.agentInfo.getID(), target);
return (int)(Math.ceil(targetDistance / 40000));
}
}
\ 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