Commit dab8187a authored by Benedikt Kleinmeier's avatar Benedikt Kleinmeier
Browse files

Refactored "ThreatCognitionModel", "OSMBehaviorController" and "ThreatMemory"

parent 54b088b8
......@@ -9,7 +9,6 @@ import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.scenario.Topography;
import org.vadere.util.geometry.shapes.VPoint;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
......@@ -50,29 +49,35 @@ public class ThreatCognitionModel implements ICognitionModel {
}
private void handleThreat(Pedestrian pedestrian, Stimulus stimulus) {
// TODO: Make code more readable.
if (isNewThreatForPedestrian(pedestrian, (Threat) stimulus)) {
pedestrian.getThreatMemory().setLatestThreatUnhandled(true);
}
// Current stimulus is a threat => store it and make clear that pedestrian is inside threat area.
pedestrian.getThreatMemory().add((Threat) stimulus);
pedestrian.setSelfCategory(SelfCategory.INSIDE_THREAT_AREA);
}
private boolean isNewThreatForPedestrian(Pedestrian pedestrian, Threat threat) {
boolean isNewThreat = false;
if (pedestrian.getThreatMemory().isEmpty()) {
pedestrian.getThreatMemory().setLatestThreatHandled(false);
isNewThreat = true;
} else {
// Check if pedestrian re-entered the same threat area.
Threat oldThreat = pedestrian.getThreatMemory().getLatestThreat();
Threat newThreat = (Threat) stimulus;
boolean isNewThreat = oldThreat.getOriginAsTargetId() != newThreat.getOriginAsTargetId();
if (isNewThreat) {
pedestrian.getThreatMemory().setLatestThreatHandled(false);
}
isNewThreat = oldThreat.getOriginAsTargetId() != threat.getOriginAsTargetId();
}
// Current stimulus is a threat => store it and make clear that pedestrian is inside threat area.
pedestrian.getThreatMemory().add((Threat) stimulus);
pedestrian.setSelfCategory(SelfCategory.INSIDE_THREAT_AREA);
return isNewThreat;
}
private void handleElapsedTime(Pedestrian pedestrian) {
if (pedestrian.getThreatMemory().getLatestThreat() != null) {
testIfInsideOrOutsideThreatArea(pedestrian);
Threat latestThreat = pedestrian.getThreatMemory().getLatestThreat();
if (latestThreat != null) {
testIfInsideOrOutsideThreatArea(pedestrian, latestThreat);
} else { // These agents did not perceive a threat but are aware of other threatened agents.
if (pedestrian.getGroupMembership() == GroupMembership.OUT_GROUP) {
......@@ -85,13 +90,11 @@ public class ThreatCognitionModel implements ICognitionModel {
}
}
private void testIfInsideOrOutsideThreatArea(Pedestrian pedestrian) {
Threat threat = pedestrian.getThreatMemory().getLatestThreat();
VPoint threatOrigin = topography.getTarget(threat.getOriginAsTargetId()).getShape().getCentroid();
private void testIfInsideOrOutsideThreatArea(Pedestrian pedestrian, Threat latestThreat) {
VPoint threatOrigin = topography.getTarget(latestThreat.getOriginAsTargetId()).getShape().getCentroid();
double distanceToThreat = threatOrigin.distance(pedestrian.getPosition());
boolean pedestrianIsInsideThreatArea = (distanceToThreat <= threat.getRadius());
boolean pedestrianIsInsideThreatArea = (distanceToThreat <= latestThreat.getRadius());
if (pedestrianIsInsideThreatArea) {
pedestrian.setSelfCategory(SelfCategory.INSIDE_THREAT_AREA);
......
......@@ -4,7 +4,6 @@ import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.vadere.simulator.models.potential.combinedPotentials.CombinedPotentialStrategy;
import org.vadere.simulator.models.potential.combinedPotentials.TargetAttractionStrategy;
import org.vadere.simulator.models.potential.combinedPotentials.TargetRepulsionStrategy;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.psychology.cognition.SelfCategory;
......@@ -120,8 +119,8 @@ public class OSMBehaviorController {
pedestrian.setTimeOfNextStep(pedestrian.getTimeOfNextStep() + timeStepInSec);
}
public void maximizeDistanceToThreatAndIncreaseSpeed(PedestrianOSM pedestrian, Topography topography) {
if (pedestrian.getThreatMemory().isLatestThreatHandled() == false) {
public void changeToTargetRepulsionStrategyAndIncreaseSpeed(PedestrianOSM pedestrian, Topography topography) {
if (pedestrian.getThreatMemory().isLatestThreatUnhandled()) {
Threat threat = pedestrian.getThreatMemory().getLatestThreat();
Target threatOrigin = topography.getTarget(threat.getOriginAsTargetId());
......@@ -135,7 +134,7 @@ public class OSMBehaviorController {
double escapeSpeed = pedestrian.getFreeFlowSpeed() * 2.0;
pedestrian.setFreeFlowSpeed(escapeSpeed);
pedestrian.getThreatMemory().setLatestThreatHandled(true);
pedestrian.getThreatMemory().setLatestThreatUnhandled(false);
}
}
......@@ -151,7 +150,7 @@ public class OSMBehaviorController {
if (pedestrian.getCombinedPotentialStrategy() instanceof TargetRepulsionStrategy) {
ScenarioElement searchPosition = (pedestrian.getSource() == null) ? pedestrian : pedestrian.getSource();
Target closestTarget = findClosestTarget(topography, searchPosition, (Threat) pedestrian.getThreatMemory().getLatestThreat());
Target closestTarget = findClosestTarget(topography, searchPosition, pedestrian.getThreatMemory().getLatestThreat());
assert closestTarget != null;
......
......@@ -63,7 +63,7 @@ public class UpdateSchemeEventDriven implements UpdateSchemeOSM {
osmBehaviorController.makeStepToTarget(pedestrian, topography);
}
} else if (selfCategory == SelfCategory.INSIDE_THREAT_AREA) {
osmBehaviorController.maximizeDistanceToThreatAndIncreaseSpeed(pedestrian, topography);
osmBehaviorController.changeToTargetRepulsionStrategyAndIncreaseSpeed(pedestrian, topography);
osmBehaviorController.makeStepToTarget(pedestrian, topography);
} else if (selfCategory == SelfCategory.OUTSIDE_THREAT_AREA) {
osmBehaviorController.changeTargetToSafeZone(pedestrian, topography);
......
......@@ -4,7 +4,6 @@ import org.jetbrains.annotations.NotNull;
import org.vadere.simulator.models.osm.OSMBehaviorController;
import org.vadere.simulator.models.osm.PedestrianOSM;
import org.vadere.state.psychology.cognition.SelfCategory;
import org.vadere.state.psychology.perception.types.*;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.scenario.Topography;
......@@ -60,7 +59,7 @@ public class UpdateSchemeSequential implements UpdateSchemeOSM {
stepForward(pedestrian, currentTimeInSec, timeStepInSec);
}
} else if (selfCategory == SelfCategory.INSIDE_THREAT_AREA) {
osmBehaviorController.maximizeDistanceToThreatAndIncreaseSpeed(pedestrian, topography);
osmBehaviorController.changeToTargetRepulsionStrategyAndIncreaseSpeed(pedestrian, topography);
stepForward(pedestrian, currentTimeInSec, timeStepInSec);
} else if (selfCategory == SelfCategory.OUTSIDE_THREAT_AREA) {
osmBehaviorController.changeTargetToSafeZone(pedestrian, topography);
......
......@@ -68,7 +68,7 @@ public class UpdateSchemeEventDriven implements DynamicElementAddListener, Dynam
osmBehaviorController.makeStepToTarget(pedestrian, topography);
}
} else if (selfCategory == SelfCategory.INSIDE_THREAT_AREA) {
osmBehaviorController.maximizeDistanceToThreatAndIncreaseSpeed(pedestrian, topography);
osmBehaviorController.changeToTargetRepulsionStrategyAndIncreaseSpeed(pedestrian, topography);
osmBehaviorController.makeStepToTarget(pedestrian, topography);
} else if (selfCategory == SelfCategory.OUTSIDE_THREAT_AREA) {
osmBehaviorController.changeTargetToSafeZone(pedestrian, topography);
......
......@@ -18,18 +18,21 @@ import java.util.List;
*
* Therefore, provide a {@link ThreatMemory} with a boolean flag included which
* indicates if latest {@link Threat} was already handled by the locomotion layer.
*
* Note: Maybe, this memory could also be implemented as a stack. I.e., cognition layer
* pushes {@link Threat}s to the stack while the locomotion layer pops off the {@link Threat}s.
* But, this is ot a real "memory"!
*/
public class ThreatMemory {
// Member Variables
List<Threat> allThreats;
// TODO: Rename to threatUnhandled.
boolean latestThreatHandled;
boolean latestThreatUnhandled;
// Constructors
public ThreatMemory() {
this.allThreats = new ArrayList<>();
this.latestThreatHandled = true;
this.latestThreatUnhandled = false;
}
public ThreatMemory(ThreatMemory other) {
......@@ -39,16 +42,16 @@ public class ThreatMemory {
other.getAllThreats().stream().forEach(threat -> this.allThreats.add(threat.clone()));
}
this.latestThreatHandled = other.isLatestThreatHandled();
this.latestThreatUnhandled = other.isLatestThreatUnhandled();
}
// Getter
public List<Threat> getAllThreats() { return allThreats; }
public boolean isLatestThreatHandled() { return latestThreatHandled; }
public boolean isLatestThreatUnhandled() { return latestThreatUnhandled; }
// Setter
public void setLatestThreatHandled(boolean latestThreatHandled) {
this.latestThreatHandled = latestThreatHandled;
public void setLatestThreatUnhandled(boolean latestThreatUnhandled) {
this.latestThreatUnhandled = latestThreatUnhandled;
}
// Methods
......
Markdown is supported
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