Commit 8c3c2b65 authored by k20066's avatar k20066
Browse files

add hungarian module

parent f6734e74
No preview for this file type
......@@ -30,3 +30,5 @@ bin
# Project
precomp_data
test-results
.DS_Store
\ No newline at end of file
#!/bin/sh
./gradlew clean
#!/bin/sh
cd `dirname $0`
rm -rf build
mkdir build
chmod +x gradlew
./gradlew build
## DefaultTacticsAmbulanceTeam
DefaultTacticsAmbulanceTeam.HumanDetector : sample_team.module.complex.SampleHumanDetector
DefaultTacticsAmbulanceTeam.HumanDetector : autumn_2023.module.complex.CentralizedControlATHumanDetector
DefaultTacticsAmbulanceTeam.Search : sample_team.module.complex.SampleSearch
DefaultTacticsAmbulanceTeam.ExtActionTransport : adf.impl.extaction.DefaultExtActionTransport
DefaultTacticsAmbulanceTeam.ExtActionMove : adf.impl.extaction.DefaultExtActionMove
DefaultTacticsAmbulanceTeam.CommandExecutorAmbulance : adf.impl.centralized.DefaultCommandExecutorAmbulance
#DefaultTacticsAmbulanceTeam.CommandExecutorAmbulance : adf.impl.centralized.DefaultCommandExecutorAmbulance
DefaultTacticsAmbulanceTeam.CommandExecutorAmbulance : autumn_2023.centralized.CentralizedControlCommandExecutorA
DefaultTacticsAmbulanceTeam.CommandExecutorScout : adf.impl.centralized.DefaultCommandExecutorScout
## DefaultTacticsFireBrigade
DefaultTacticsFireBrigade.HumanDetector : sample_team.module.complex.SampleHumanDetector
DefaultTacticsFireBrigade.HumanDetector : autumn_2023.module.complex.CentralizedControlFBHumanDetector
DefaultTacticsFireBrigade.Search : sample_team.module.complex.SampleSearch
DefaultTacticsFireBrigade.ExtActionFireRescue : adf.impl.extaction.DefaultExtActionFireRescue
DefaultTacticsFireBrigade.ExtActionFireRescue : autumn_2023.extaction.ComplimentTacticsExtActionFireRescue
DefaultTacticsFireBrigade.ExtActionMove : adf.impl.extaction.DefaultExtActionMove
DefaultTacticsFireBrigade.CommandExecutorFire : adf.impl.centralized.DefaultCommandExecutorFire
DefaultTacticsFireBrigade.CommandExecutorFire : autumn_2023.centralized.CentralizedControlCommandExecutorF
DefaultTacticsFireBrigade.CommandExecutorScout : adf.impl.centralized.DefaultCommandExecutorScout
## DefaultTacticsPoliceForce
......@@ -23,12 +24,15 @@ DefaultTacticsPoliceForce.CommandExecutorPolice : adf.impl.centralized.DefaultCo
DefaultTacticsPoliceForce.CommandExecutorScout : adf.impl.centralized.DefaultCommandExecutorScoutPolice
## DefaultTacticsAmbulanceCentre
DefaultTacticsAmbulanceCentre.TargetAllocator : sample_team.module.complex.SampleAmbulanceTargetAllocator
DefaultTacticsAmbulanceCentre.CommandPicker : adf.impl.centralized.DefaultCommandPickerAmbulance
#DefaultTacticsAmbulanceCentre.TargetAllocator : sample_team.module.complex.SampleAmbulanceTargetAllocator
#DefaultTacticsAmbulanceCentre.CommandPicker : adf.impl.centralized.DefaultCommandPickerAmbulance
DefaultTacticsAmbulanceCentre.TargetAllocator : autumn_2023.module.complex.CentralizedControlATAllocator
DefaultTacticsAmbulanceCentre.CommandPicker : autumn_2023.centralized.CentralizedControlCommandPickerA
## DefaultTacticsFireStation
DefaultTacticsFireStation.TargetAllocator : sample_team.module.complex.SampleFireTargetAllocator
#DefaultTacticsFireStation.TargetAllocator : sample_team.module.complex.SampleFireTargetAllocator
DefaultTacticsFireStation.CommandPicker : adf.impl.centralized.DefaultCommandPickerFire
TacticsFireStation.TargetAllocator : autumn_2023.module.complex.CentralizedControlFBAllocator
## DefaultTacticsPoliceOffice
DefaultTacticsPoliceOffice.TargetAllocator : sample_team.module.complex.SamplePoliceTargetAllocator
......@@ -75,8 +79,8 @@ DefaultCommandExecutorAmbulance.ExtActionMove : adf.impl.extaction.DefaultExtAct
## DefaultCommandExecutorFire
DefaultCommandExecutorFire.PathPlanning : adf.impl.module.algorithm.DijkstraPathPlanning
DefaultCommandExecutorFire.EtxActionFireRescue : adf.impl.extaction.DefaultExtActionFireRescue
DefaultCommandExecutorFire.EtxActionFireFighting : adf.impl.extaction.DefaultExtActionFireFighting
DefaultCommandExecutorFire.ExtActionFireRescue : autumn_2023.extaction.ComplimentTacticsExtActionFireRescue
DefaultCommandExecutorFire.ExtActionFireFighting : adf.impl.extaction.DefaultExtActionFireFighting
DefaultCommandExecutorFire.ExtActionMove : adf.impl.extaction.DefaultExtActionMove
## DefaultCommandExecutorPolice
......@@ -92,10 +96,10 @@ DefaultCommandExecutorScoutPolice.PathPlanning : adf.impl.module.algorithm.Dijks
DefaultCommandExecutorScoutPolice.ExtActionClear : adf.impl.extaction.DefaultExtActionClear
## MessageManager
MessageManager.PlatoonChannelSubscriber : adf.impl.module.comm.DefaultChannelSubscriber
MessageManager.CenterChannelSubscriber : adf.impl.module.comm.DefaultChannelSubscriber
MessageManager.PlatoonMessageCoordinator : adf.impl.module.comm.DefaultMessageCoordinator
MessageManager.CenterMessageCoordinator : adf.impl.module.comm.DefaultMessageCoordinator
MessageManager.PlatoonChannelSubscriber : autumn_2023.module.comm.DefaultChannelSubscriber
MessageManager.CenterChannelSubscriber : autumn_2023.module.comm.DefaultChannelSubscriber
MessageManager.PlatoonMessageCoordinator : autumn_2023.module.comm.CentralizedControlMessageCoordinator
MessageManager.CenterMessageCoordinator : autumn_2023.module.comm.CentralizedControlMessageCoordinator
## VisualDebug
VisualDebug : true
\ No newline at end of file
No preview for this file type
#!/bin/sh
LOADER="adf.impl.DefaultLoader"
PARAMS=$*
cd `dirname $0`
if [ ! -z "$1" ]; then
./gradlew launch --args="${LOADER} ${PARAMS} -pre 1"
else
echo "Options:"
echo "-tn Team name"
echo "-t [FB],[FS],[PF],[PO],[AT],[AC] Number of agents"
echo "-fb [FB] Number of FireBrigade"
echo "-fs [FS] Number of FireStation"
echo "-pf [PF] Number of PoliceForce"
echo "-po [PO] Number of PoliceOffice"
echo "-at [AT] Number of AmbulanceTeam"
echo "-ac [AC] Number of AmbulanceCentre"
echo "-s [HOST]:[PORT] RCRS server host and port"
echo "-h [HOST] RCRS server host (port:27931)"
echo "-pre [0|1] Precompute flag"
echo "-d [0|1] Debug flag"
echo "-dev [0|1] Development mode"
echo "-mc [FILE] ModuleConfig file name"
echo "-md [JSON] ModuleConfig JSON"
echo "-df [FILE] DevelopData JSON file"
echo "-dd [JSON] DevelopData JSON"
echo "-all [alias] -t -1,-1,-1,-1,-1,-1"
echo "-allp [alias] -t 1,0,1,0,1,0,"
echo "-local [alias] -h localhost"
echo "-precompute [alias] -pre 1"
echo "-debug [alias] -d 1"
echo "-develop [alias] -dev 1"
fi
......@@ -25,7 +25,7 @@ import rescuecore2.standard.entities.Human;
import rescuecore2.standard.entities.StandardEntity;
import rescuecore2.worldmodel.EntityID;
public class HungarianCommandExecutorAmbulance
public class CentralizedControlCommandExecutorA
extends CommandExecutor<CommandAmbulance> {
private static final int ACTION_UNKNOWN = -1;
......@@ -45,7 +45,7 @@ public class HungarianCommandExecutorAmbulance
private EntityID target;
private EntityID commanderID;
public HungarianCommandExecutorAmbulance(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) {
public CentralizedControlCommandExecutorA(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) {
super(ai, wi, si, moduleManager, developData);
this.commandType = ACTION_UNKNOWN;
switch (scenarioInfo.getMode()) {
......@@ -149,6 +149,7 @@ public class HungarianCommandExecutorAmbulance
@Override
public CommandExecutor calc() {
this.result = null;
//System.out.println("time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" excutor");
switch (this.commandType) {
case ACTION_REST:
EntityID position = this.agentInfo.getPosition();
......
package autumn_2023.centralized;
import static rescuecore2.standard.entities.StandardEntityURN.CIVILIAN;
import static rescuecore2.standard.entities.StandardEntityURN.REFUGE;
import adf.core.agent.action.Action;
import adf.core.agent.action.common.ActionMove;
import adf.core.agent.action.common.ActionRest;
import adf.core.agent.communication.MessageManager;
import adf.core.agent.communication.standard.bundle.centralized.CommandFire;
import adf.core.agent.communication.standard.bundle.centralized.MessageReport;
import adf.core.agent.develop.DevelopData;
import adf.core.agent.info.AgentInfo;
import adf.core.agent.info.ScenarioInfo;
import adf.core.agent.info.WorldInfo;
import adf.core.agent.module.ModuleManager;
import adf.core.agent.precompute.PrecomputeData;
import adf.core.component.centralized.CommandExecutor;
import adf.core.component.extaction.ExtAction;
import adf.core.component.module.algorithm.PathPlanning;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import rescuecore2.standard.entities.Area;
import rescuecore2.standard.entities.Human;
import rescuecore2.standard.entities.StandardEntity;
import rescuecore2.worldmodel.EntityID;
public class CentralizedControlCommandExecutorF extends CommandExecutor<CommandFire> {
private static final int ACTION_UNKNOWN = -1;
private static final int ACTION_REST = CommandFire.ACTION_REST;
private static final int ACTION_MOVE = CommandFire.ACTION_MOVE;
private static final int ACTION_RESCUE = CommandFire.ACTION_RESCUE;
private static final int ACTION_AUTONOMY = CommandFire.ACTION_AUTONOMY;
private PathPlanning pathPlanning;
private ExtAction actionFireRescue;
private ExtAction actionExtMove;
private int commandType;
private EntityID target;
private EntityID commanderID;
private boolean commandCompleted;
public CentralizedControlCommandExecutorF(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) {
super(ai, wi, si, moduleManager, developData);
this.commandType = ACTION_UNKNOWN;
this.target = null;
this.commanderID = null;
switch (scenarioInfo.getMode()) {
case PRECOMPUTATION_PHASE:
case PRECOMPUTED:
case NON_PRECOMPUTE:
this.pathPlanning = moduleManager.getModule(
"DefaultCommandExecutorFire.PathPlanning",
"adf.impl.module.algorithm.DijkstraPathPlanning");
this.actionFireRescue = moduleManager.getExtAction(
"DefaultCommandExecutorFire.ExtActionFireRescue",
"adf.impl.extaction.DefaultExtActionFireRescue");
this.actionExtMove = moduleManager.getExtAction(
"DefaultCommandExecutorFire.ExtActionMove",
"adf.impl.extaction.DefaultExtActionMove");
break;
}
}
@Override
public CommandExecutor setCommand(CommandFire command) {
EntityID agentID = this.agentInfo.getID();
if(!this.commandCompleted) return this;
if (command.isToIDDefined() && Objects.requireNonNull(command.getToID())
.getValue() == agentID.getValue()) {
this.commandType = command.getAction();
this.target = command.getTargetID();
this.commanderID = command.getSenderID();
//System.out.println("[FireBrigade] recievecommand time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" tagID:"+this.target+" commandType:"+this.commandType);
}
return this;
}
@Override
public CommandExecutor updateInfo(MessageManager messageManager) {
super.updateInfo(messageManager);
if (this.getCountUpdateInfo() >= 2) {
return this;
}
this.pathPlanning.updateInfo(messageManager);
this.actionFireRescue.updateInfo(messageManager);
this.actionExtMove.updateInfo(messageManager);
this.commandCompleted = this.isCommandCompleted(this.commandType);
//System.out.println("[FireBrigade] isCommandCompleted ["+this.commandCompleted+"] time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" tagID:"+this.target+" commandType:"+this.commandType);
if (this.commandCompleted) {
if (this.commandType != ACTION_UNKNOWN) {
messageManager
.addMessage(new MessageReport(true, true, false, this.target));
this.commandType = ACTION_UNKNOWN;
this.target = null;
this.commanderID = null;
}
}
return this;
}
@Override
public CommandExecutor precompute(PrecomputeData precomputeData) {
super.precompute(precomputeData);
if (this.getCountPrecompute() >= 2) {
return this;
}
this.pathPlanning.precompute(precomputeData);
this.actionFireRescue.precompute(precomputeData);
this.actionExtMove.precompute(precomputeData);
return this;
}
@Override
public CommandExecutor resume(PrecomputeData precomputeData) {
super.resume(precomputeData);
if (this.getCountResume() >= 2) {
return this;
}
this.pathPlanning.resume(precomputeData);
this.actionFireRescue.resume(precomputeData);
this.actionExtMove.resume(precomputeData);
return this;
}
@Override
public CommandExecutor preparate() {
super.preparate();
if (this.getCountPreparate() >= 2) {
return this;
}
this.pathPlanning.preparate();
this.actionFireRescue.preparate();
this.actionExtMove.preparate();
return this;
}
@Override
public CommandExecutor calc() {
this.result = null;
switch (this.commandType) {
case ACTION_REST:
EntityID position = this.agentInfo.getPosition();
if (this.target == null) {
Collection<
EntityID> refuges = this.worldInfo.getEntityIDsOfType(REFUGE);
if (refuges.contains(position)) {
this.result = new ActionRest();
} else {
this.pathPlanning.setFrom(position);
this.pathPlanning.setDestination(refuges);
List<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) {
System.out.println("[FireBrigade] excutor notarget time:"+this.agentInfo.getTime()+" id:"+this.commanderID+" tagID:"+this.target+" commandType:"+this.commandType);
return this;
}
StandardEntity targetEntity = this.worldInfo.getEntity(this.target);
if (targetEntity instanceof Area) {
this.result = this.actionExtMove.setTarget(this.target).calc()
.getAction();
} else if (targetEntity instanceof Human) {
this.result = this.actionFireRescue.setTarget(this.target).calc()
.getAction();
System.out.println("[FireBrigade] excutor "+this.result+" time:"+this.agentInfo.getTime()+" id:"+this.commanderID+" tagID:"+this.target+" commandType:"+this.commandType);
}
}
return this;
}
/*private Action computeCommand(int commandType, EntityID target, EntityID commanderID){
Action action = null;
switch (commandType) {
case ACTION_REST:
EntityID position = this.agentInfo.getPosition();
if (target == null) {
Collection<
EntityID> refuges = this.worldInfo.getEntityIDsOfType(REFUGE);
if (refuges.contains(position)) {
action = new ActionRest();
} else {
this.pathPlanning.setFrom(position);
this.pathPlanning.setDestination(refuges);
List<EntityID> path = this.pathPlanning.calc().getResult();
if (path != null && path.size() > 0) {
action = new ActionMove(path);
} else {
action = new ActionRest();
}
}
return action;
}
if (position.getValue() != target.getValue()) {
List<EntityID> path = this.pathPlanning.getResult(position,
target);
if (path != null && path.size() > 0) {
action = new ActionMove(path);
return action;
}
}
action = new ActionRest();
return action;
case ACTION_MOVE:
if (target != null) {
action = this.actionExtMove.setTarget(target).calc()
.getAction();
}
return action;
case ACTION_RESCUE:
if (target != null) {
action = this.actionFireRescue.setTarget(target).calc()
.getAction();
}
return action;
case ACTION_AUTONOMY:
if (target == null) {
return action;
}
StandardEntity targetEntity = this.worldInfo.getEntity(target);
if (targetEntity instanceof Area) {
action = this.actionExtMove.setTarget(target).calc()
.getAction();
} else if (targetEntity instanceof Human) {
action = this.actionFireRescue.setTarget(target).calc()
.getAction();
}
}
return action;
}*/
private boolean isCommandCompleted(int commandType) {
Human agent = (Human) this.agentInfo.me();
switch (commandType) {
case ACTION_REST:
if (this.target == null) {
return (agent.getDamage() == 0);
}
if (Objects.requireNonNull(this.worldInfo.getEntity(this.target))
.getStandardURN() == REFUGE) {
if (agent.getPosition().getValue() == this.target.getValue()) {
return (agent.getDamage() == 0);
}
}
return false;
case ACTION_MOVE:
return this.target == null || this.agentInfo.getPosition()
.getValue() == this.target.getValue();
case ACTION_RESCUE:
if (this.target == null) {
return true;
}
Human human = (Human) Objects
.requireNonNull(this.worldInfo.getEntity(this.target));
return human.isBuriednessDefined() && human.getBuriedness() == 0
|| (human.isHPDefined() && human.getHP() == 0);
case ACTION_AUTONOMY:
if (this.target != null) {
StandardEntity targetEntity = this.worldInfo.getEntity(this.target);
if (targetEntity instanceof Area) {
commandType = ACTION_MOVE;
return this.isCommandCompleted(commandType);
} else if (targetEntity instanceof Human) {
//commandType = ACTION_MOVE;
//if(!this.isCommandCompleted(commandType)) return false;
Human h = (Human) targetEntity;
EntityID position = h.getPosition();
/*if (this.agentInfo.getPosition().getValue() != position.getValue()) {
return false;
}*/
commandType = ACTION_RESCUE;
/*if ((h.isHPDefined() && h.getHP() == 0)) {
return true;
}
//commandType = h.getStandardURN() == CIVILIAN ? ACTION_RESCUE : null;
if (h.getStandardURN() == CIVILIAN) {
commandType = ACTION_RESCUE;
}*/
return this.isCommandCompleted(commandType);
}
}
return true;
}
return true;
}
}
\ No newline at end of file
package autumn_2023.centralized;
import adf.core.agent.communication.standard.bundle.centralized.CommandAmbulance;
import adf.core.agent.communication.standard.bundle.centralized.CommandScout;
import adf.core.agent.develop.DevelopData;
import adf.core.agent.info.AgentInfo;
import adf.core.agent.info.ScenarioInfo;
import adf.core.agent.info.WorldInfo;
import adf.core.agent.module.ModuleManager;
import adf.core.component.centralized.CommandPicker;
import adf.core.component.communication.CommunicationMessage;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import rescuecore2.standard.entities.Area;
import rescuecore2.standard.entities.Human;
import rescuecore2.standard.entities.StandardEntity;
import rescuecore2.standard.entities.StandardEntityURN;
import rescuecore2.worldmodel.EntityID;
public class CentralizedControlCommandPickerA extends CommandPicker {
private int scoutDistance;
private Collection<CommunicationMessage> messages;
private Map<EntityID, EntityID> allocationData;
public CentralizedControlCommandPickerA(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) {
super(ai, wi, si, moduleManager, developData);
this.messages = new ArrayList<>();
this.allocationData = null;
this.scoutDistance = developData.getInteger(
"adf.impl.centralized.DefaultCommandPickerAmbulance.scoutDistance",
40000);
}
@Override
public CommandPicker
setAllocatorResult(Map<EntityID, EntityID> allocationData) {
this.allocationData = allocationData;
return this;
}
@Override
public CommandPicker calc() {
//System.out.println("time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" picker");
this.messages.clear();
if (this.allocationData == null) {
return this;
}
for (EntityID agentID : this.allocationData.keySet()) {
StandardEntity agent = this.worldInfo.getEntity(agentID);
if (agent != null
&& agent.getStandardURN() == StandardEntityURN.AMBULANCE_TEAM) {
StandardEntity target = this.worldInfo
.getEntity(this.allocationData.get(agentID));
if (target != null) {
if (target instanceof Human) {
CommandAmbulance command = new CommandAmbulance(true, agentID,
target.getID(), CommandAmbulance.ACTION_AUTONOMY);
this.messages.add(command);
} else if (target instanceof Area) {
CommandScout command = new CommandScout(true, agentID,
target.getID(), this.scoutDistance);
this.messages.add(command);
}
}
}
}
return this;
}
@Override
public Collection<CommunicationMessage> getResult() {
return this.messages;
}
}
\ No newline at end of file
package autumn_2023.extaction;
import static rescuecore2.standard.entities.StandardEntityURN.BLOCKADE;
import adf.core.agent.action.Action;
import adf.core.agent.action.ambulance.ActionRescue;
import adf.core.agent.action.common.ActionMove;
import adf.core.agent.communication.MessageManager;
import adf.core.agent.communication.standard.bundle.information.MessageFireBrigade;
import adf.core.agent.develop.DevelopData;
import adf.core.agent.info.AgentInfo;
import adf.core.agent.info.ScenarioInfo;
import adf.core.agent.info.WorldInfo;
import adf.core.agent.module.ModuleManager;
import adf.core.agent.precompute.PrecomputeData;
import adf.core.component.extaction.ExtAction;
import adf.core.component.module.algorithm.PathPlanning;
import java.util.ArrayList;
import java.util.List;
import rescuecore2.config.NoSuchConfigOptionException;
import rescuecore2.standard.entities.Area;
import rescuecore2.standard.entities.Blockade;
import rescuecore2.standard.entities.FireBrigade;
import rescuecore2.standard.entities.Human;
import rescuecore2.standard.entities.StandardEntity;
import rescuecore2.worldmodel.EntityID;
public class ComplimentTacticsExtActionFireRescue extends ExtAction {
private MessageManager mm;
private PathPlanning pathPlanning;
private int thresholdRest;
private int kernelTime;
private EntityID target;
public ComplimentTacticsExtActionFireRescue(AgentInfo agentInfo, WorldInfo worldInfo, ScenarioInfo scenarioInfo, ModuleManager moduleManager, DevelopData developData) {
super(agentInfo, worldInfo, scenarioInfo, moduleManager, developData);
this.target = null;
this.thresholdRest = developData
.getInteger("adf.impl.extaction.DefaultExtActionFireRescue.rest", 100);
switch (scenarioInfo.getMode()) {
case PRECOMPUTATION_PHASE:
case PRECOMPUTED:
case NON_PRECOMPUTE:
this.pathPlanning = moduleManager.getModule(
"DefaultExtActionFireRescue.PathPlanning",
"adf.impl.module.algorithm.DijkstraPathPlanning");
break;
}
}
public ExtAction precompute(PrecomputeData precomputeData) {
super.precompute(precomputeData);
if (this.getCountPrecompute() >= 2) {
return this;
}
this.pathPlanning.precompute(precomputeData);
try {
this.kernelTime = this.scenarioInfo.getKernelTimesteps();
} catch (NoSuchConfigOptionException e) {
this.kernelTime = -1;
}
return this;
}
public ExtAction resume(PrecomputeData precomputeData) {
super.resume(precomputeData);
if (this.getCountResume() >= 2) {
return this;
}
this.pathPlanning.resume(precomputeData);
try {
this.kernelTime = this.scenarioInfo.getKernelTimesteps();
} catch (NoSuchConfigOptionException e) {
this.kernelTime = -1;
}
return this;
}
public ExtAction preparate() {
super.preparate();
if (this.getCountPreparate() >= 2) {
return this;
}
this.pathPlanning.preparate();
try {
this.kernelTime = this.scenarioInfo.getKernelTimesteps();
} catch (NoSuchConfigOptionException e) {
this.kernelTime = -1;
}
return this;
}
public ExtAction updateInfo(MessageManager messageManager) {
super.updateInfo(messageManager);
if (this.getCountUpdateInfo() >= 2) {
return this;
}
this.mm = messageManager;
this.pathPlanning.updateInfo(messageManager);
return this;
}
@Override
public ExtAction setTarget(EntityID target) {
this.target = null;
if (target != null) {
StandardEntity entity = this.worldInfo.getEntity(target);
if (entity instanceof Human || entity instanceof Area) {
this.target = target;
return this;
}
}
return this;
}
@Override
public ExtAction calc() {
this.result = null;
FireBrigade agent = (FireBrigade) this.agentInfo.me();
if (this.needRest(agent)) {
EntityID areaID = this.convertArea(this.target);
ArrayList<EntityID> targets = new ArrayList<>();
if (areaID != null) {
targets.add(areaID);
}
}
if (this.target != null) {
this.result = this.calcRescue(agent, this.pathPlanning, this.target);
}
if(this.result != null){
Class<? extends Action> actionClass = this.result.getClass();
if (actionClass == ActionRescue.class) {
//System.out.println("[FireRescue time:"+this.agentInfo.getTime()+" myid:"+this.agentInfo.getID());
this.mm.addMessage(new MessageFireBrigade(true, agent, MessageFireBrigade.ACTION_RESCUE, this.target));
}
}
return this;
}
private Action calcRescue(FireBrigade agent, PathPlanning pathPlanning,
EntityID targetID) {
StandardEntity targetEntity = this.worldInfo.getEntity(targetID);
if (targetEntity == null) {
return null;
}
EntityID agentPosition = agent.getPosition();
if (targetEntity instanceof Human) {
Human human = (Human) targetEntity;
if (!human.isPositionDefined()) {
return null;
}
if (human.isHPDefined() && human.getHP() == 0) {
return null;
}
EntityID targetPosition = human.getPosition();
if (agentPosition.getValue() == targetPosition.getValue()) {
if (human.isBuriednessDefined() && human.getBuriedness() > 0) {
return new ActionRescue(human);
}
} else {
List<EntityID> path = pathPlanning.getResult(agentPosition,
targetPosition);
if (path != null && path.size() > 0) {
return new ActionMove(path);
}
}
return null;
}
if (targetEntity.getStandardURN() == BLOCKADE) {
Blockade blockade = (Blockade) targetEntity;
if (blockade.isPositionDefined()) {
targetEntity = this.worldInfo.getEntity(blockade.getPosition());
}
}
if (targetEntity instanceof Area) {
List<EntityID> path = pathPlanning.getResult(agentPosition,
targetEntity.getID());
if (path != null && path.size() > 0) {
return new ActionMove(path);
}
}
return null;
}
private boolean needRest(Human agent) {
int hp = agent.getHP();
int damage = agent.getDamage();
if (hp == 0 || damage == 0) {
return false;
}
int activeTime = (hp / damage) + ((hp % damage) != 0 ? 1 : 0);
if (this.kernelTime == -1) {
try {
this.kernelTime = this.scenarioInfo.getKernelTimesteps();
} catch (NoSuchConfigOptionException e) {
this.kernelTime = -1;
}
}
return damage >= this.thresholdRest
|| (activeTime + this.agentInfo.getTime()) < this.kernelTime;
}
private EntityID convertArea(EntityID targetID) {
StandardEntity entity = this.worldInfo.getEntity(targetID);
if (entity == null) {
return null;
}
if (entity instanceof Human) {
Human human = (Human) entity;
if (human.isPositionDefined()) {
EntityID position = human.getPosition();
if (this.worldInfo.getEntity(position) instanceof Area) {
return position;
}
}
} else if (entity instanceof Area) {
return targetID;
} else if (entity.getStandardURN() == BLOCKADE) {
Blockade blockade = (Blockade) entity;
if (blockade.isPositionDefined()) {
return blockade.getPosition();
}
}
return null;
}
}
\ No newline at end of file
package autumn_2023.module.comm;
import adf.core.agent.communication.MessageManager;
import adf.core.agent.communication.standard.bundle.StandardMessage;
import adf.core.agent.communication.standard.bundle.StandardMessagePriority;
import adf.core.agent.communication.standard.bundle.centralized.CommandAmbulance;
import adf.core.agent.communication.standard.bundle.centralized.CommandFire;
import adf.core.agent.communication.standard.bundle.centralized.CommandPolice;
import adf.core.agent.communication.standard.bundle.centralized.CommandScout;
import adf.core.agent.communication.standard.bundle.centralized.MessageReport;
import adf.core.agent.communication.standard.bundle.information.MessageAmbulanceTeam;
import adf.core.agent.communication.standard.bundle.information.MessageBuilding;
import adf.core.agent.communication.standard.bundle.information.MessageCivilian;
import adf.core.agent.communication.standard.bundle.information.MessageFireBrigade;
import adf.core.agent.communication.standard.bundle.information.MessagePoliceForce;
import adf.core.agent.communication.standard.bundle.information.MessageRoad;
import adf.core.agent.info.AgentInfo;
import adf.core.agent.info.ScenarioInfo;
import adf.core.agent.info.WorldInfo;
import adf.core.component.communication.CommunicationMessage;
import adf.core.component.communication.MessageCoordinator;
import java.util.ArrayList;
import java.util.List;
import rescuecore2.standard.entities.StandardEntityURN;
public class CentralizedControlMessageCoordinator extends MessageCoordinator {
@Override
public void coordinate(AgentInfo agentInfo, WorldInfo worldInfo,
ScenarioInfo scenarioInfo, MessageManager messageManager,
ArrayList<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) {
fireBrigadeMessages.add(msg);
ambulanceMessages.add(msg);
} else if (msg instanceof MessageRoad) {
fireBrigadeMessages.add(msg);
ambulanceMessages.add(msg);
policeMessages.add(msg);
} else if (msg instanceof CommandAmbulance) {
ambulanceMessages.add(msg);
} else if (msg instanceof CommandFire) {
fireBrigadeMessages.add(msg);
} else if (msg instanceof CommandPolice) {
policeMessages.add(msg);
} else if (msg instanceof CommandScout) {
if (agentType == StandardEntityURN.FIRE_STATION) {
fireBrigadeMessages.add(msg);
} else if (agentType == StandardEntityURN.POLICE_OFFICE) {
policeMessages.add(msg);
} else if (agentType == StandardEntityURN.AMBULANCE_CENTRE) {
ambulanceMessages.add(msg);
}
} else if (msg instanceof MessageReport) {
if (agentType == StandardEntityURN.FIRE_BRIGADE) {
fireBrigadeMessages.add(msg);
} else if (agentType == StandardEntityURN.POLICE_FORCE) {
policeMessages.add(msg);
} else if (agentType == StandardEntityURN.AMBULANCE_TEAM) {
ambulanceMessages.add(msg);
}
} else if (msg instanceof MessageFireBrigade) {
fireBrigadeMessages.add(msg);
ambulanceMessages.add(msg);
policeMessages.add(msg);
} else if (msg instanceof MessagePoliceForce) {
ambulanceMessages.add(msg);
policeMessages.add(msg);
} else if (msg instanceof MessageAmbulanceTeam) {
ambulanceMessages.add(msg);
policeMessages.add(msg);
}
}
}
if (scenarioInfo.getCommsChannelsCount() > 1) {
// send radio messages if there are more than one communication channel
int[] channelSize = new int[scenarioInfo.getCommsChannelsCount() - 1];
setSendMessages(scenarioInfo, StandardEntityURN.POLICE_FORCE, agentInfo,
worldInfo, policeMessages, channelSendMessageList, channelSize);
setSendMessages(scenarioInfo, StandardEntityURN.AMBULANCE_TEAM, agentInfo,
worldInfo, ambulanceMessages, channelSendMessageList, channelSize);
setSendMessages(scenarioInfo, StandardEntityURN.FIRE_BRIGADE, agentInfo,
worldInfo, fireBrigadeMessages, channelSendMessageList, channelSize);
}
ArrayList<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] = DefaultChannelSubscriber.getChannelNumber(agentType, i,
numChannels);
}
String a = "isPlatoon:"+isPlatoon;
for(int i = 0;i < channels.length; i++){
a += " channels["+i+"]:"+channels[i];
}
//System.out.println("time:"+agentInfo.getTime()+" myid:"+agentInfo.getID()+a);
return channels;
}
protected boolean isPlatoonAgent(AgentInfo agentInfo, WorldInfo worldInfo) {
StandardEntityURN agentType = getAgentType(agentInfo, worldInfo);
if (agentType == StandardEntityURN.FIRE_BRIGADE
|| agentType == StandardEntityURN.POLICE_FORCE
|| agentType == StandardEntityURN.AMBULANCE_TEAM) {
return true;
}
return false;
}
protected StandardEntityURN getAgentType(AgentInfo agentInfo,
WorldInfo worldInfo) {
StandardEntityURN agentType = worldInfo.getEntity(agentInfo.getID())
.getStandardURN();
return agentType;
}
protected void setSendMessages(ScenarioInfo scenarioInfo,
StandardEntityURN agentType, AgentInfo agentInfo, WorldInfo worldInfo,
List<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);
}
}
}
}
}
\ 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 DefaultChannelSubscriber extends ChannelSubscriber {
@Override
public void subscribe(AgentInfo agentInfo, WorldInfo worldInfo,
ScenarioInfo scenarioInfo, MessageManager messageManager) {
// subscribe only once at the beginning
if (agentInfo.getTime() == 1) {
int numChannels = scenarioInfo.getCommsChannelsCount() - 1; // 0th channel
// is the
// voice
// channel
int maxChannelCount = 0;
boolean isPlatoon = isPlatoonAgent(agentInfo, worldInfo);
if (isPlatoon) {
maxChannelCount = scenarioInfo.getCommsChannelsMaxPlatoon();
} else {
maxChannelCount = scenarioInfo.getCommsChannelsMaxOffice();
}
StandardEntityURN agentType = getAgentType(agentInfo, worldInfo);
int[] channels = new int[maxChannelCount];
for (int i = 0; i < maxChannelCount; i++) {
channels[i] = getChannelNumber(agentType, i, numChannels);
}
messageManager.subscribeToChannels(channels);
}
}
protected boolean isPlatoonAgent(AgentInfo agentInfo, WorldInfo worldInfo) {
StandardEntityURN agentType = getAgentType(agentInfo, worldInfo);
if (agentType == StandardEntityURN.FIRE_BRIGADE
|| agentType == StandardEntityURN.POLICE_FORCE
|| agentType == StandardEntityURN.AMBULANCE_TEAM) {
return true;
}
return false;
}
protected StandardEntityURN getAgentType(AgentInfo agentInfo,
WorldInfo worldInfo) {
StandardEntityURN agentType = worldInfo.getEntity(agentInfo.getID())
.getStandardURN();
return agentType;
}
public static int getChannelNumber(StandardEntityURN agentType,
int channelIndex, int numChannels) {
int agentIndex = 0;
if (agentType == StandardEntityURN.FIRE_BRIGADE
|| agentType == StandardEntityURN.FIRE_STATION) {
agentIndex = 1;
} else if (agentType == StandardEntityURN.POLICE_FORCE
|| agentType == StandardEntityURN.POLICE_OFFICE) {
agentIndex = 2;
} else if (agentType == StandardEntityURN.AMBULANCE_TEAM
|| agentType == StandardEntityURN.AMBULANCE_CENTRE) {
agentIndex = 3;
}
int index = (3 * channelIndex) + agentIndex;
if ((index % numChannels) == 0) {
index = numChannels;
} else {
index = index % numChannels;
}
return index;
}
public static void main(String[] args) {
int numChannels = 6;
int maxChannels = 2;
for (int i = 0; i < maxChannels; i++) {
System.out.println("FIREBRIGADE-" + i + ":"
+ getChannelNumber(StandardEntityURN.FIRE_BRIGADE, i, numChannels));
}
for (int i = 0; i < maxChannels; i++) {
System.out.println("POLICE-" + i + ":"
+ getChannelNumber(StandardEntityURN.POLICE_OFFICE, i, numChannels));
}
for (int i = 0; i < maxChannels; i++) {
System.out.println("AMB-" + i + ":" + getChannelNumber(
StandardEntityURN.AMBULANCE_CENTRE, i, numChannels));
}
}
}
\ No newline at end of file
package autumn_2023.module.complex;
import static rescuecore2.standard.entities.StandardEntityURN.REFUGE;
import adf.core.agent.communication.MessageManager;
import adf.core.agent.communication.standard.bundle.MessageUtil;
import adf.core.agent.communication.standard.bundle.centralized.CommandAmbulance;
import adf.core.agent.communication.standard.bundle.centralized.MessageReport;
import adf.core.agent.communication.standard.bundle.information.MessageAmbulanceTeam;
import adf.core.agent.communication.standard.bundle.information.MessageCivilian;
import adf.core.agent.communication.standard.bundle.information.MessageFireBrigade;
import adf.core.agent.communication.standard.bundle.information.MessagePoliceForce;
import adf.core.agent.develop.DevelopData;
import adf.core.agent.info.AgentInfo;
import adf.core.agent.info.ScenarioInfo;
import adf.core.agent.info.WorldInfo;
import adf.core.agent.module.ModuleManager;
import adf.core.agent.precompute.PrecomputeData;
import adf.core.component.communication.CommunicationMessage;
import adf.core.component.module.complex.AmbulanceTargetAllocator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import rescuecore2.standard.entities.AmbulanceTeam;
import rescuecore2.standard.entities.Area;
import rescuecore2.standard.entities.Human;
import rescuecore2.standard.entities.StandardEntity;
import rescuecore2.standard.entities.StandardEntityURN;
import rescuecore2.worldmodel.EntityID;
public class CentralizedControlATAllocator extends AmbulanceTargetAllocator {
private Collection<EntityID> priorityHumans;
private Collection<EntityID> targetHumans;
private Map<EntityID, AmbulanceTeamInfo> ambulanceTeamInfoMap;
public CentralizedControlATAllocator(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) {
super(ai, wi, si, moduleManager, developData);
this.priorityHumans = new HashSet<>();
this.targetHumans = new HashSet<>();
this.ambulanceTeamInfoMap = new HashMap<>();
}
@Override
public AmbulanceTargetAllocator resume(PrecomputeData precomputeData) {
super.resume(precomputeData);
if (this.getCountResume() >= 2) {
return this;
}
for (EntityID id : this.worldInfo
.getEntityIDsOfType(StandardEntityURN.AMBULANCE_TEAM)) {
this.ambulanceTeamInfoMap.put(id, new AmbulanceTeamInfo(id));
}
return this;
}
@Override
public AmbulanceTargetAllocator preparate() {
super.preparate();
if (this.getCountPrecompute() >= 2) {
return this;
}
for (EntityID id : this.worldInfo
.getEntityIDsOfType(StandardEntityURN.AMBULANCE_TEAM)) {
this.ambulanceTeamInfoMap.put(id, new AmbulanceTeamInfo(id));
}
return this;
}
@Override
public Map<EntityID, EntityID> getResult() {
return this.convert(this.ambulanceTeamInfoMap);
}
@Override
public AmbulanceTargetAllocator calc() {
//System.out.println("time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" allocator");
List<StandardEntity> agents = this
.getActionAgents(this.ambulanceTeamInfoMap);
Collection<EntityID> removes = new ArrayList<>();
int currentTime = this.agentInfo.getTime();
for (EntityID target : this.priorityHumans) {
if (agents.size() > 0) {
StandardEntity targetEntity = this.worldInfo.getEntity(target);
if (targetEntity != null && targetEntity instanceof Human
&& ((Human) targetEntity).isPositionDefined()) {
agents.sort(new DistanceSorter(this.worldInfo, targetEntity));
StandardEntity result = agents.get(0);
agents.remove(0);
AmbulanceTeamInfo info = this.ambulanceTeamInfoMap
.get(result.getID());
if (info != null) {
info.canNewAction = false;
info.target = target;
info.commandTime = currentTime;
this.ambulanceTeamInfoMap.put(result.getID(), info);
removes.add(target);
}
}
}
}
this.priorityHumans.removeAll(removes);
removes.clear();
for (EntityID target : this.targetHumans) {
if (agents.size() > 0) {
StandardEntity targetEntity = this.worldInfo.getEntity(target);
if (targetEntity != null && targetEntity instanceof Human
&& ((Human) targetEntity).isPositionDefined()) {
agents.sort(new DistanceSorter(this.worldInfo, targetEntity));
StandardEntity result = agents.get(0);
agents.remove(0);
AmbulanceTeamInfo info = this.ambulanceTeamInfoMap
.get(result.getID());
if (info != null) {
info.canNewAction = false;
info.target = target;
info.commandTime = currentTime;
this.ambulanceTeamInfoMap.put(result.getID(), info);
removes.add(target);
}
}
}
}
this.targetHumans.removeAll(removes);
return this;
}
@Override
public AmbulanceTargetAllocator updateInfo(MessageManager messageManager) {
super.updateInfo(messageManager);
if (this.getCountUpdateInfo() >= 2) {
return this;
}
int currentTime = this.agentInfo.getTime();
for (CommunicationMessage message : messageManager
.getReceivedMessageList()) {
Class<? extends CommunicationMessage> messageClass = message.getClass();
if (messageClass == MessageCivilian.class) {
MessageCivilian mc = (MessageCivilian) message;
MessageUtil.reflectMessage(this.worldInfo, mc);
if (mc.isBuriednessDefined() && mc.getBuriedness() > 0) {
this.targetHumans.add(mc.getAgentID());
} else {
this.priorityHumans.remove(mc.getAgentID());
this.targetHumans.remove(mc.getAgentID());
}
} else if (messageClass == MessageFireBrigade.class) {
MessageFireBrigade mfb = (MessageFireBrigade) message;
MessageUtil.reflectMessage(this.worldInfo, mfb);
if (mfb.isBuriednessDefined() && mfb.getBuriedness() > 0) {
this.priorityHumans.add(mfb.getAgentID());
} else {
this.priorityHumans.remove(mfb.getAgentID());
this.targetHumans.remove(mfb.getAgentID());
}
} else if (messageClass == MessagePoliceForce.class) {
MessagePoliceForce mpf = (MessagePoliceForce) message;
MessageUtil.reflectMessage(this.worldInfo, mpf);
if (mpf.isBuriednessDefined() && mpf.getBuriedness() > 0) {
this.priorityHumans.add(mpf.getAgentID());
} else {
this.priorityHumans.remove(mpf.getAgentID());
this.targetHumans.remove(mpf.getAgentID());
}
}
}
for (CommunicationMessage message : messageManager
.getReceivedMessageList(MessageAmbulanceTeam.class)) {
MessageAmbulanceTeam mat = (MessageAmbulanceTeam) message;
MessageUtil.reflectMessage(this.worldInfo, mat);
if (mat.isBuriednessDefined() && mat.getBuriedness() > 0) {
this.priorityHumans.add(mat.getAgentID());
} else {
this.priorityHumans.remove(mat.getAgentID());
this.targetHumans.remove(mat.getAgentID());
}
AmbulanceTeamInfo info = this.ambulanceTeamInfoMap.get(mat.getAgentID());
if (info == null) {
info = new AmbulanceTeamInfo(mat.getAgentID());
}
if (currentTime >= info.commandTime + 2) {
this.ambulanceTeamInfoMap.put(mat.getAgentID(), this.update(info, mat));
}
}
for (CommunicationMessage message : messageManager
.getReceivedMessageList(CommandAmbulance.class)) {
CommandAmbulance command = (CommandAmbulance) message;
if (command.getAction() == CommandAmbulance.ACTION_RESCUE
&& command.isBroadcast()) {
this.priorityHumans.add(command.getTargetID());
this.targetHumans.add(command.getTargetID());
} else if (command.getAction() == CommandAmbulance.ACTION_LOAD
&& command.isBroadcast()) {
this.priorityHumans.add(command.getTargetID());
this.targetHumans.add(command.getTargetID());
}
}
for (CommunicationMessage message : messageManager
.getReceivedMessageList(MessageReport.class)) {
MessageReport report = (MessageReport) message;
AmbulanceTeamInfo info = this.ambulanceTeamInfoMap
.get(report.getSenderID());
if (info != null && report.isDone()) {
info.canNewAction = true;
this.priorityHumans.remove(info.target);
this.targetHumans.remove(info.target);
info.target = null;
this.ambulanceTeamInfoMap.put(info.agentID, info);
}
}
return this;
}
private Map<EntityID, EntityID>
convert(Map<EntityID, AmbulanceTeamInfo> map) {
Map<EntityID, EntityID> result = new HashMap<>();
for (EntityID id : map.keySet()) {
AmbulanceTeamInfo info = map.get(id);
if (info != null && info.target != null) {
result.put(id, info.target);
}
}
return result;
}
private List<StandardEntity>
getActionAgents(Map<EntityID, AmbulanceTeamInfo> map) {
List<StandardEntity> result = new ArrayList<>();
for (StandardEntity entity : this.worldInfo
.getEntitiesOfType(StandardEntityURN.POLICE_FORCE)) {
AmbulanceTeamInfo info = map.get(entity.getID());
if (info != null && info.canNewAction
&& ((AmbulanceTeam) entity).isPositionDefined()) {
result.add(entity);
}
}
return result;
}
private AmbulanceTeamInfo update(AmbulanceTeamInfo info,
MessageAmbulanceTeam message) {
if (message.isBuriednessDefined() && message.getBuriedness() > 0) {
info.canNewAction = false;
if (info.target != null) {
this.targetHumans.add(info.target);
info.target = null;
}
return info;
}
if (message.getAction() == MessageAmbulanceTeam.ACTION_REST) {
info.canNewAction = true;
if (info.target != null) {
this.targetHumans.add(info.target);
info.target = null;
}
} else if (message.getAction() == MessageAmbulanceTeam.ACTION_MOVE) {
if (message.getTargetID() != null) {
StandardEntity entity = this.worldInfo.getEntity(message.getTargetID());
if (entity != null) {
if (entity instanceof Area) {
if (entity.getStandardURN() == REFUGE) {
info.canNewAction = false;
return info;
}
StandardEntity targetEntity = this.worldInfo.getEntity(info.target);
if (targetEntity != null) {
if (targetEntity instanceof Human) {
targetEntity = this.worldInfo.getPosition((Human) targetEntity);
if (targetEntity == null) {
this.priorityHumans.remove(info.target);
this.targetHumans.remove(info.target);
info.canNewAction = true;
info.target = null;
return info;
}
}
if (targetEntity.getID().getValue() == entity.getID()
.getValue()) {
info.canNewAction = false;
} else {
info.canNewAction = true;
if (info.target != null) {
this.targetHumans.add(info.target);
info.target = null;
}
}
} else {
info.canNewAction = true;
info.target = null;
}
return info;
} else if (entity instanceof Human) {
if (entity.getID().getValue() == info.target.getValue()) {
info.canNewAction = false;
} else {
info.canNewAction = true;
this.targetHumans.add(info.target);
this.targetHumans.add(entity.getID());
info.target = null;
}
return info;
}
}
}
info.canNewAction = true;
if (info.target != null) {
this.targetHumans.add(info.target);
info.target = null;
}
} else if (message.getAction() == MessageAmbulanceTeam.ACTION_RESCUE) {
info.canNewAction = true;
if (info.target != null) {
this.targetHumans.add(info.target);
info.target = null;
}
} else if (message.getAction() == MessageAmbulanceTeam.ACTION_LOAD) {
info.canNewAction = false;
} else if (message.getAction() == MessageAmbulanceTeam.ACTION_UNLOAD) {
info.canNewAction = true;
this.priorityHumans.remove(info.target);
this.targetHumans.remove(info.target);
info.target = null;
}
return info;
}
private class AmbulanceTeamInfo {
EntityID agentID;
EntityID target;
boolean canNewAction;
int commandTime;
AmbulanceTeamInfo(EntityID id) {
agentID = id;
target = null;
canNewAction = true;
commandTime = -1;
}
}
private class DistanceSorter implements Comparator<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;
}
}
}
\ 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.action.Action;
import adf.core.agent.communication.MessageManager;
import adf.core.agent.communication.standard.bundle.StandardMessageBundle;
import adf.core.agent.communication.standard.bundle.StandardMessagePriority;
import adf.core.agent.communication.standard.bundle.information.MessageBuilding;
import adf.core.agent.communication.standard.bundle.information.MessageCivilian;
import adf.core.agent.develop.DevelopData;
import adf.core.agent.info.AgentInfo;
import adf.core.agent.info.ScenarioInfo;
import adf.core.agent.info.WorldInfo;
import adf.core.agent.module.ModuleManager;
import adf.core.component.communication.CommunicationMessage;
import adf.core.component.module.algorithm.Clustering;
import adf.core.component.module.complex.HumanDetector;
import adf.core.debug.DefaultLogger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.log4j.Logger;
import rescuecore2.standard.entities.Building;
import rescuecore2.standard.entities.Civilian;
import rescuecore2.standard.entities.Human;
import rescuecore2.standard.entities.StandardEntity;
import rescuecore2.standard.entities.StandardEntityURN;
import rescuecore2.worldmodel.EntityID;
public class CentralizedControlATHumanDetector extends HumanDetector {
private Clustering clustering;
private EntityID result;
private Logger logger;
public CentralizedControlATHumanDetector(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) {
super(ai, wi, si, moduleManager, developData);
logger = DefaultLogger.getLogger(agentInfo.me());
this.clustering = moduleManager.getModule("SampleHumanDetector.Clustering",
"adf.impl.module.algorithm.KMeansClustering");
registerModule(this.clustering);
}
@Override
public HumanDetector updateInfo(MessageManager messageManager) {
logger.debug("Time:" + agentInfo.getTime());
super.updateInfo(messageManager);
int time = this.agentInfo.getTime();
Action action = this.agentInfo.getExecutedAction(time - 1);
//System.out.println("time:"+time+" myid:"+this.agentInfo.getID()+" -1step action:"+action);
int[] chan = messageManager.getChannels();
String str = "";
for(int i=0; i<chan.length; i++){
str+=" getChannels["+i+"]:"+chan[i]+" chan_len:"+chan.length;
}
//System.out.println("time:"+time+" myid:"+this.agentInfo.getID()+" channelCount:"+scenarioInfo.getCommsChannelsCount()+str);
//List<CommunicationMessage> mes = messageManager.getReceivedMessageList(MessageCivilian.class);
Set<EntityID> changes = this.worldInfo.getChanged().getChangedEntities();
for(EntityID id : changes){
StandardEntity entity = this.worldInfo.getEntity(id);
if(!(entity instanceof Civilian)) continue;
Civilian civ = (Civilian) entity;
if(!this.isNeedRescueHuman(civ)){
continue;
}
StandardMessagePriority level = StandardMessagePriority.HIGH;
//MessageCivilian civ_mes = new MessageDamageCivilian(true, level, civ);
MessageCivilian civ_mes = new MessageCivilian(true, level, civ);
//Building building = (Building) this.worldInfo.getPosition(id);
//System.out.println("time:"+time+" myid:"+this.agentInfo.getID()+" civ:"+civ.getID()+" level:"+civ_mes.getSendingPriority());
//messageManager.addMessage(building_mes,true);
messageManager.addMessage(civ_mes,true);
}
return this;
}
@Override
public HumanDetector calc() {
Human transportHuman = this.agentInfo.someoneOnBoard();
if (transportHuman != null) {
logger.debug("someoneOnBoard:" + transportHuman);
this.result = transportHuman.getID();
return this;
}
if (this.result != null) {
Human target = (Human) this.worldInfo.getEntity(this.result);
if (!isValidHuman(target)) {
logger.debug("Invalid Human:" + target + " ==>reset target");
this.result = null;
}
}
if (this.result == null) {
this.result = calcTarget();
}
return this;
}
private EntityID calcTarget() {
List<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;
}
private boolean isNeedRescueHuman(StandardEntity entity){
if (entity == null)
return false;
if (!(entity instanceof Human))
return false;
Human target = (Human) entity;
if (!target.isHPDefined() || target.getHP() == 0)
return false;
if (!target.isPositionDefined())
return false;
if (!target.isBuriednessDefined() || target.getBuriedness() == 0)
return false;
StandardEntity position = worldInfo.getPosition(target);
if (position == null)
return false;
StandardEntityURN positionURN = position.getStandardURN();
if (positionURN == REFUGE || positionURN == AMBULANCE_TEAM)
return false;
return true;
}
}
\ No newline at end of file
package autumn_2023.module.complex;
import autumn_2023.module.algorithm.Hungarian;
import adf.core.agent.communication.MessageManager;
import adf.core.agent.communication.standard.bundle.MessageUtil;
import adf.core.agent.communication.standard.bundle.StandardMessageBundle;
import adf.core.agent.communication.standard.bundle.StandardMessagePriority;
import adf.core.agent.communication.standard.bundle.centralized.MessageReport;
import adf.core.agent.communication.standard.bundle.information.MessageBuilding;
import adf.core.agent.communication.standard.bundle.information.MessageCivilian;
import adf.core.agent.communication.standard.bundle.information.MessageFireBrigade;
import adf.core.agent.develop.DevelopData;
import adf.core.agent.info.AgentInfo;
import adf.core.agent.info.ScenarioInfo;
import adf.core.agent.info.WorldInfo;
import adf.core.agent.module.ModuleManager;
import adf.core.agent.precompute.PrecomputeData;
import adf.core.component.communication.CommunicationMessage;
import adf.core.component.module.complex.FireTargetAllocator;
import static rescuecore2.standard.entities.StandardEntityURN.AMBULANCE_TEAM;
import static rescuecore2.standard.entities.StandardEntityURN.REFUGE;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import rescuecore2.standard.entities.Building;
import rescuecore2.standard.entities.FireBrigade;
import rescuecore2.standard.entities.Human;
import rescuecore2.standard.entities.StandardEntity;
import rescuecore2.standard.entities.StandardEntityURN;
import rescuecore2.worldmodel.EntityID;
public class CentralizedControlFBAllocator extends FireTargetAllocator {
private Map<EntityID, FireBrigadeInfo> fireBrigadeInfoMap;
private Set<EntityID> taskSet;
private Map<EntityID, EntityID> allocateTaskMap;
private Set<EntityID> finishEntityIDs;
public CentralizedControlFBAllocator(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) {
super(ai, wi, si, moduleManager, developData);
this.fireBrigadeInfoMap = new HashMap<>();
this.taskSet = new HashSet<>();
this.allocateTaskMap = new HashMap<>();
this.finishEntityIDs = new HashSet<>();
}
@Override
public FireTargetAllocator resume(PrecomputeData precomputeData) {
super.resume(precomputeData);
if (this.getCountResume() >= 2) {
return this;
}
for (EntityID id : this.worldInfo
.getEntityIDsOfType(StandardEntityURN.FIRE_BRIGADE)) {
this.fireBrigadeInfoMap.put(id, new FireBrigadeInfo(id));
}
return this;
}
@Override
public FireTargetAllocator preparate() {
super.preparate();
if (this.getCountPrecompute() >= 2) {
return this;
}
for (EntityID id : this.worldInfo
.getEntityIDsOfType(StandardEntityURN.FIRE_BRIGADE)) {
this.fireBrigadeInfoMap.put(id, new FireBrigadeInfo(id));
}
return this;
}
@Override
public Map<EntityID, EntityID> getResult() {
return this.convert(this.fireBrigadeInfoMap);
}
private Map<EntityID, EntityID> convert(Map<EntityID, FireBrigadeInfo> map) {
Map<EntityID, EntityID> result = new HashMap<>();
for (EntityID id : map.keySet()) {
FireBrigadeInfo info = map.get(id);
if (info != null && info.target != null) {
result.put(id, info.target);
}
}
return result;
}
@Override
public FireTargetAllocator calc() {
if(this.agentInfo.getTime() < scenarioInfo.getKernelAgentsIgnoreuntil()) return this;
if(this.fireBrigadeInfoMap.keySet().size()==0||this.taskSet.size()==0) return this;
int row = this.fireBrigadeInfoMap.keySet().size();
int col = this.taskSet.size();
boolean reverse = false;
List<EntityID> rowlist = new ArrayList<>(this.fireBrigadeInfoMap.keySet());
List<EntityID> collist = new ArrayList<>(this.taskSet);
if(row > col){
reverse = true;
int tmp = row;
row = col;
col = tmp;
List<EntityID> tmplist = rowlist;
rowlist = collist;
collist = tmplist;
}
int[][] costs = new int[row][col];
for(int i = 0; i<row; i++){
EntityID rowid = rowlist.get(i);
StandardEntity rowposi = this.worldInfo.getPosition(rowid);
for(int j = 0; j<col; j++){
EntityID colid = collist.get(j);
int distance = this.worldInfo.getDistance(rowposi, this.worldInfo.getPosition(colid));
costs[i][j] = distance;
}
}
int[] allocate = Hungarian.execute(costs);
if(reverse){
int tmp = row;
row = col;
col = tmp;
List<EntityID> tmplist = rowlist;
rowlist = collist;
collist = tmplist;
int[] reverse_list = new int[row];
for(int i = 0; i<row; i++) reverse_list[i] = -1;
for(int i = 0; i<col; i++){
reverse_list[allocate[i]] = i;
}
allocate = reverse_list;
}
String str = " allocate";
for(int i = 0; i<row; i++){
if(allocate[i] == -1) continue;
str += "\n["+i+"]:"+allocate[i]+" fbID:"+rowlist.get(i)+" tagID:"+collist.get(allocate[i]);
this.fireBrigadeInfoMap.get(rowlist.get(i)).target = collist.get(allocate[i]);
}
System.out.println("[FBCenter] time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+str);
return this;
}
@Override
public FireTargetAllocator updateInfo(MessageManager messageManager) {
super.updateInfo(messageManager);
/*List<CommunicationMessage> mescivlist = messageManager.getReceivedMessageList(MessageCivilian.class);
for(CommunicationMessage mes : mescivlist){
MessageCivilian mesciv = (MessageCivilian) mes;
StandardMessagePriority level = mesciv.getSendingPriority();
//if(level != StandardMessagePriority.HIGH) continue;
MessageUtil.reflectMessage(this.worldInfo, mesciv);
//boolean mdc = (mesciv instanceof MessageDamageCivilian);
//if(!(mesciv instanceof MessageDamageCivilian)) continue;
EntityID civID = mesciv.getAgentID();
StandardEntity civ = this.worldInfo.getEntity(civID);
if(!this.isNeedRescueHuman(civ)) continue;
if(finishEntityIDs.contains(civID)) continue;
EntityID buildingID = mesciv.getPosition();
//int time = this.agentInfo.getTime();
//System.out.println("[FBCenter] time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" civID:"+civID+" buildingID:"+buildingID+" size:"+mesciv.getByteArraySize()+" mdc:"+mdc);
System.out.println("[FBCenter] time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" civID:"+civID+" buildingID:"+buildingID+" size:"+mesciv.getByteArraySize()+" isNeedRescueHuman:"+this.isNeedRescueHuman(civ));
this.taskSet.add(civID);
}*/
Set<EntityID> recievetasks = messageManager.getReceivedMessageList(MessageCivilian.class).stream()
.map(e -> {
MessageCivilian mesciv = (MessageCivilian) e;
MessageUtil.reflectMessage(this.worldInfo, mesciv);
return(mesciv);
})
.map(MessageCivilian::getAgentID)
.filter(e -> this.isNeedRescueHuman(this.worldInfo.getEntity(e)))
.filter(e -> !(finishEntityIDs.contains(e)))
.collect(Collectors.toSet());
this.taskSet.addAll(recievetasks);
List<CommunicationMessage> mesfblist = messageManager.getReceivedMessageList(MessageFireBrigade.class);
for(CommunicationMessage mes : mesfblist){
MessageFireBrigade mesfb = (MessageFireBrigade) mes;
MessageUtil.reflectMessage(this.worldInfo, mesfb);
EntityID fbID = mesfb.getAgentID();
System.out.println("[FBCenter] time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" fbID:"+fbID+" posi:"+mesfb.getPosition()+" Action:"+mesfb.getAction()+" level:"+mesfb.getSendingPriority()+" size:"+mesfb.getByteArraySize());
FireBrigadeInfo fbi = this.fireBrigadeInfoMap.get(fbID);
fbi.setInfo(mesfb.getTargetID(), mesfb.getPosition(), mesfb.getBuriedness(),
(mesfb.getAction() == MessageFireBrigade.ACTION_RESCUE) ? false : true, this.agentInfo.getTime());
}
List<CommunicationMessage> reportlist = messageManager.getReceivedMessageList(MessageReport.class);
for(CommunicationMessage mes : reportlist){
MessageReport report = (MessageReport) mes;
EntityID targetID = report.getFromID();
//System.out.println("[FBCenter] report time:"+this.agentInfo.getTime()+" id:"+this.agentInfo.getID()+" fbID:"+senderID+" tagID:"+targetID);
boolean done = report.isDone();
if(done) {
this.finishEntityIDs.add(targetID);
this.taskSet.remove(targetID);
}
}
return this;
}
private boolean isNeedRescueHuman(StandardEntity entity){
if (entity == null)
return false;
if (!(entity instanceof Human))
return false;
Human target = (Human) entity;
if (!target.isHPDefined() || target.getHP() == 0)
return false;
if (!target.isPositionDefined())
return false;
if (!target.isBuriednessDefined() || target.getBuriedness() == 0)
return false;
StandardEntity position = worldInfo.getPosition(target);
if (position == null)
return false;
StandardEntityURN positionURN = position.getStandardURN();
if (positionURN == REFUGE || positionURN == AMBULANCE_TEAM)
return false;
return true;
}
private class FireBrigadeInfo {
EntityID agentID;
EntityID prevtarget;
EntityID position;
int buriedness;
EntityID target;
boolean canNewAction;
int commandTime;
FireBrigadeInfo(EntityID id) {
agentID = id;
prevtarget = null;
position = null;
buriedness = -1;
target = null;
canNewAction = true;
commandTime = -1;
}
public void setInfo(EntityID prevtarget, EntityID position, int buriedness, boolean canNewAction, int commandTime){
this.prevtarget = prevtarget;
this.position = position;
this.buriedness = buriedness;
this.canNewAction = canNewAction;
this.commandTime = commandTime;
}
}
}
\ No newline at end of file
package autumn_2023.module.complex;
import static rescuecore2.standard.entities.StandardEntityURN.AMBULANCE_TEAM;
import static rescuecore2.standard.entities.StandardEntityURN.CIVILIAN;
import static rescuecore2.standard.entities.StandardEntityURN.REFUGE;
import adf.core.agent.action.Action;
import adf.core.agent.communication.MessageManager;
import adf.core.agent.communication.standard.bundle.StandardMessageBundle;
import adf.core.agent.communication.standard.bundle.StandardMessagePriority;
import adf.core.agent.communication.standard.bundle.information.MessageBuilding;
import adf.core.agent.communication.standard.bundle.information.MessageCivilian;
import adf.core.agent.communication.standard.bundle.information.MessageFireBrigade;
import adf.core.agent.develop.DevelopData;
import adf.core.agent.info.AgentInfo;
import adf.core.agent.info.ScenarioInfo;
import adf.core.agent.info.WorldInfo;
import adf.core.agent.module.ModuleManager;
import adf.core.component.module.algorithm.Clustering;
import adf.core.component.module.complex.HumanDetector;
import adf.core.debug.DefaultLogger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.log4j.Logger;
import rescuecore2.standard.entities.Building;
import rescuecore2.standard.entities.Civilian;
import rescuecore2.standard.entities.FireBrigade;
import rescuecore2.standard.entities.Human;
import rescuecore2.standard.entities.StandardEntity;
import rescuecore2.standard.entities.StandardEntityURN;
import rescuecore2.worldmodel.EntityID;
public class CentralizedControlFBHumanDetector extends HumanDetector {
private Clustering clustering;
private EntityID result;
private Logger logger;
private MessageManager mm;
public CentralizedControlFBHumanDetector(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) {
super(ai, wi, si, moduleManager, developData);
logger = DefaultLogger.getLogger(agentInfo.me());
this.clustering = moduleManager.getModule("SampleHumanDetector.Clustering",
"adf.impl.module.algorithm.KMeansClustering");
registerModule(this.clustering);
}
@Override
public HumanDetector updateInfo(MessageManager messageManager) {
logger.debug("Time:" + agentInfo.getTime());
super.updateInfo(messageManager);
this.mm = messageManager;
int time = this.agentInfo.getTime();
int[] chan = messageManager.getChannels();
String str = "";
for(int i=0; i<chan.length; i++){
str+=" getChannels["+i+"]:"+chan[i]+" chan_len:"+chan.length;
}
//System.out.println("time:"+time+" myid:"+this.agentInfo.getID()+" channelCount:"+scenarioInfo.getCommsChannelsCount()+str);
Set<EntityID> changes = this.worldInfo.getChanged().getChangedEntities();
for(EntityID id : changes){
StandardEntity entity = this.worldInfo.getEntity(id);
if(!(entity instanceof Civilian)) continue;
Civilian civ = (Civilian) entity;
if(!this.isNeedRescueHuman(civ)){
continue;
}
StandardMessagePriority level = StandardMessagePriority.HIGH;
MessageCivilian civ_mes = new MessageCivilian(true, level, civ);
//System.out.println("time:"+time+" myid:"+this.agentInfo.getID()+" civ:"+civ.getID()+" level:"+civ_mes.getSendingPriority());
messageManager.addMessage(civ_mes,true);
}
return this;
}
@Override
public HumanDetector calc() {
if(this.agentInfo.getTime() < scenarioInfo.getKernelAgentsIgnoreuntil()) return this;
this.result = null;
return this;
}
private EntityID calcTarget() {
List<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;
}
private boolean isNeedRescueHuman(StandardEntity entity){
if (entity == null)
return false;
if (!(entity instanceof Human))
return false;
Human target = (Human) entity;
if (!target.isHPDefined() || target.getHP() == 0)
return false;
if (!target.isPositionDefined())
return false;
if (!target.isBuriednessDefined() || target.getBuriedness() == 0)
return false;
StandardEntity position = worldInfo.getPosition(target);
if (position == null)
return false;
StandardEntityURN positionURN = position.getStandardURN();
if (positionURN == REFUGE || positionURN == AMBULANCE_TEAM)
return false;
return true;
}
}
\ No newline at end of file
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
#!/bin/sh
LOADER="adf.impl.DefaultLoader"
PARAMS=$*
cd `dirname $0`
if [ ! -z "$1" ]; then
./gradlew launch --args="${LOADER} ${PARAMS} -pre 0"
else
echo "Options:"
echo "-tn Team name"
echo "-t [FB],[FS],[PF],[PO],[AT],[AC] Number of agents"
echo "-fb [FB] Number of FireBrigade"
echo "-fs [FS] Number of FireStation"
echo "-pf [PF] Number of PoliceForce"
echo "-po [PO] Number of PoliceOffice"
echo "-at [AT] Number of AmbulanceTeam"
echo "-ac [AC] Number of AmbulanceCentre"
echo "-s [HOST]:[PORT] RCRS server host and port"
echo "-h [HOST] RCRS server host (port:27931)"
echo "-pre [0|1] Precompute flag"
echo "-d [0|1] Debug flag"
echo "-dev [0|1] Development mode"
echo "-mc [FILE] ModuleConfig file name"
echo "-md [JSON] ModuleConfig JSON"
echo "-df [FILE] DevelopData JSON file"
echo "-dd [JSON] DevelopData JSON"
echo "-all [alias] -t -1,-1,-1,-1,-1,-1"
echo "-allp [alias] -t 1,0,1,0,1,0,"
echo "-local [alias] -h localhost"
echo "-precompute [alias] -pre 1"
echo "-debug [alias] -d 1"
echo "-develop [alias] -dev 1"
fi
#!/bin/sh
TEAM=ait
mkdir ../${TEAM}
cp -r src ../${TEAM}
cp -r lib ../${TEAM}
mv ../${TEAM}/lib ../${TEAM}/library
cp -r config ../${TEAM}
cp -r data ../${TEAM}
cd ../${TEAM}
tar cvzf ait.tgz *
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