Commit 65d59dbe authored by Benedikt Kleinmeier's avatar Benedikt Kleinmeier
Browse files

Started implementing "SelfCatThreatModel" and added "radius" to "Bang"

Note: "SelfCatThreatModel" uses OSM as locomotion model.

VadereSimulator/src/org/vadere/simulator/models/osm/OptimalStepsModel.java:
- Extracted new method static method "StepCircleOptimizer.create()" based on "OptimalStepsModel.createStepCircleOptimizer()".
parent 039a59e0
Pipeline #200760 passed with stages
in 120 minutes and 13 seconds
......@@ -10,22 +10,12 @@ import org.vadere.simulator.models.bmm.BiomechanicsModel;
import org.vadere.simulator.models.gnm.GradientNavigationModel;
import org.vadere.simulator.models.osm.OptimalStepsModel;
import org.vadere.simulator.models.ovm.OptimalVelocityModel;
import org.vadere.simulator.models.psychology.selfcategorization.SelfCatThreatModel;
import org.vadere.simulator.models.reynolds.ReynoldsSteeringModel;
import org.vadere.simulator.models.sfm.SocialForceModel;
import org.vadere.state.attributes.Attributes;
import org.vadere.state.attributes.ModelDefinition;
import org.vadere.state.attributes.models.AttributesBHM;
import org.vadere.state.attributes.models.AttributesBMM;
import org.vadere.state.attributes.models.AttributesFloorField;
import org.vadere.state.attributes.models.AttributesGNM;
import org.vadere.state.attributes.models.AttributesOSM;
import org.vadere.state.attributes.models.AttributesOVM;
import org.vadere.state.attributes.models.AttributesPotentialCompact;
import org.vadere.state.attributes.models.AttributesPotentialCompactSoftshell;
import org.vadere.state.attributes.models.AttributesPotentialGNM;
import org.vadere.state.attributes.models.AttributesPotentialSFM;
import org.vadere.state.attributes.models.AttributesReynolds;
import org.vadere.state.attributes.models.AttributesSFM;
import org.vadere.state.attributes.models.*;
/**
* Singleton for model presets.
......@@ -88,6 +78,13 @@ public class ModelPresets {
list.add(AttributesBHM.class);
registerModelPreset(BiomechanicsModel.class, list);
// SelfCatThreat Model
list.clear();
list.add(AttributesSelfCatThreat.class);
list.add(AttributesPotentialCompactSoftshell.class);
list.add(AttributesFloorField.class);
registerModelPreset(SelfCatThreatModel.class, list);
// list.clear();
// list.add(...);
// registerModelPreset(MyModel.class, list);
......
......@@ -5,46 +5,29 @@ import org.vadere.annotation.factories.models.ModelClass;
import org.vadere.simulator.control.factory.GroupSourceControllerFactory;
import org.vadere.simulator.control.factory.SingleSourceControllerFactory;
import org.vadere.simulator.control.factory.SourceControllerFactory;
import org.vadere.simulator.models.MainModel;
import org.vadere.simulator.models.Model;
import org.vadere.simulator.models.SpeedAdjuster;
import org.vadere.simulator.models.StepSizeAdjuster;
import org.vadere.simulator.models.SubModelBuilder;
import org.vadere.simulator.models.*;
import org.vadere.simulator.models.groups.cgm.CentroidGroupModel;
import org.vadere.simulator.models.groups.cgm.CentroidGroupPotential;
import org.vadere.simulator.models.groups.cgm.CentroidGroupSpeedAdjuster;
import org.vadere.simulator.models.osm.optimization.ParticleSwarmOptimizer;
import org.vadere.simulator.models.osm.optimization.PatternSearchOptimizer;
import org.vadere.simulator.models.osm.optimization.StepCircleOptimizer;
import org.vadere.simulator.models.osm.optimization.StepCircleOptimizerBrent;
import org.vadere.simulator.models.osm.optimization.StepCircleOptimizerCircleNelderMead;
import org.vadere.simulator.models.osm.optimization.StepCircleOptimizerDiscrete;
import org.vadere.simulator.models.osm.optimization.StepCircleOptimizerEvolStrat;
import org.vadere.simulator.models.osm.optimization.StepCircleOptimizerGradient;
import org.vadere.simulator.models.osm.optimization.StepCircleOptimizerNelderMead;
import org.vadere.simulator.models.osm.optimization.StepCircleOptimizerPowell;
import org.vadere.simulator.models.osm.updateScheme.UpdateSchemeOSM;
import org.vadere.simulator.models.potential.PotentialFieldModel;
import org.vadere.simulator.models.potential.fields.IPotentialFieldTarget;
import org.vadere.simulator.models.potential.fields.IPotentialFieldTargetGrid;
import org.vadere.simulator.models.potential.fields.PotentialFieldAgent;
import org.vadere.simulator.models.potential.fields.PotentialFieldObstacle;
import org.vadere.simulator.utils.cache.ScenarioCache;
import org.vadere.state.attributes.Attributes;
import org.vadere.state.attributes.models.AttributesOSM;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.scenario.*;
import org.vadere.state.types.OptimizationType;
import org.vadere.state.scenario.DynamicElement;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.scenario.Topography;
import org.vadere.state.types.UpdateType;
import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VShape;
import org.vadere.util.logging.Logger;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
......@@ -117,7 +100,7 @@ public class OptimalStepsModel implements MainModel, PotentialFieldModel {
this.speedAdjusters.add(new CentroidGroupSpeedAdjuster(centroidGroupModel));
}
this.stepCircleOptimizer = createStepCircleOptimizer(
this.stepCircleOptimizer = StepCircleOptimizer.create(
attributesOSM, random, topography, iPotentialTargetGrid);
// TODO implement a step speed adjuster for this!
......@@ -224,54 +207,6 @@ public class OptimalStepsModel implements MainModel, PotentialFieldModel {
}
}
private StepCircleOptimizer createStepCircleOptimizer(
AttributesOSM attributesOSM, Random random, Topography topography,
IPotentialFieldTargetGrid potentialFieldTarget) {
StepCircleOptimizer result;
double movementThreshold = attributesOSM.getMovementThreshold();
OptimizationType type = attributesOSM.getOptimizationType();
if (type == null) {
type = OptimizationType.DISCRETE;
}
switch (type) {
case BRENT:
result = new StepCircleOptimizerBrent(random);
break;
case EVOLUTION_STRATEGY:
result = new StepCircleOptimizerEvolStrat();
break;
case NELDER_MEAD:
result = new StepCircleOptimizerNelderMead(random);
break;
case NELDER_MEAD_CIRCLE:
result = new StepCircleOptimizerCircleNelderMead(random, attributesOSM);
break;
case POWELL:
result = new StepCircleOptimizerPowell(random);
break;
case PSO:
result = new ParticleSwarmOptimizer(movementThreshold, random);
break;
case PATTERN_SEARCH:
result = new PatternSearchOptimizer(movementThreshold, attributesOSM, random);
break;
case GRADIENT:
result = new StepCircleOptimizerGradient(topography,
potentialFieldTarget, attributesOSM);
break;
case DISCRETE:
case NONE:
default:
result = new StepCircleOptimizerDiscrete(movementThreshold, random);
break;
}
return result;
}
@Override
public void preLoop(final double simTimeInSec) {
this.lastSimTimeInSec = simTimeInSec;
......@@ -289,8 +224,8 @@ public class OptimalStepsModel implements MainModel, PotentialFieldModel {
lastSimTimeInSec = simTimeInSec;
}
/*
* At the moment all pedestrians also the initalPedestrians get this.attributesPedestrain!!!
/**
* At the moment, all pedestrians inherit position from "this.attributesPedestian"!
*/
@Override
public <T extends DynamicElement> PedestrianOSM createElement(VPoint position, int id, Class<T> type) {
......
......@@ -63,7 +63,7 @@ public class PedestrianOSM extends Pedestrian {
private StairStepOptimizer stairStepOptimizer;
@SuppressWarnings("unchecked")
PedestrianOSM(AttributesOSM attributesOSM,
public PedestrianOSM(AttributesOSM attributesOSM,
AttributesAgent attributesPedestrian, Topography topography,
Random random, IPotentialFieldTarget potentialFieldTarget,
PotentialFieldObstacle potentialFieldObstacle,
......
......@@ -2,8 +2,13 @@ package org.vadere.simulator.models.osm.optimization;
import java.awt.Shape;
import java.util.ArrayList;
import java.util.Random;
import org.vadere.simulator.models.osm.PedestrianOSM;
import org.vadere.simulator.models.potential.fields.IPotentialFieldTargetGrid;
import org.vadere.state.attributes.models.AttributesOSM;
import org.vadere.state.scenario.Topography;
import org.vadere.state.types.OptimizationType;
import org.vadere.util.config.VadereConfig;
import org.vadere.util.geometry.shapes.VPoint;
......@@ -76,4 +81,53 @@ public abstract class StepCircleOptimizer {
public void clearMetricValues(){
this.currentMetricValues = new ArrayList<>();
}
public static StepCircleOptimizer create(
AttributesOSM attributesOSM, Random random, Topography topography,
IPotentialFieldTargetGrid potentialFieldTarget) {
StepCircleOptimizer result;
double movementThreshold = attributesOSM.getMovementThreshold();
OptimizationType type = attributesOSM.getOptimizationType();
if (type == null) {
type = OptimizationType.DISCRETE;
}
switch (type) {
case BRENT:
result = new StepCircleOptimizerBrent(random);
break;
case EVOLUTION_STRATEGY:
result = new StepCircleOptimizerEvolStrat();
break;
case NELDER_MEAD:
result = new StepCircleOptimizerNelderMead(random);
break;
case NELDER_MEAD_CIRCLE:
result = new StepCircleOptimizerCircleNelderMead(random, attributesOSM);
break;
case POWELL:
result = new StepCircleOptimizerPowell(random);
break;
case PSO:
result = new ParticleSwarmOptimizer(movementThreshold, random);
break;
case PATTERN_SEARCH:
result = new PatternSearchOptimizer(movementThreshold, attributesOSM, random);
break;
case GRADIENT:
result = new StepCircleOptimizerGradient(topography,
potentialFieldTarget, attributesOSM);
break;
case DISCRETE:
case NONE:
default:
result = new StepCircleOptimizerDiscrete(movementThreshold, random);
break;
}
return result;
}
}
package org.vadere.simulator.models.psychology.selfcategorization;
import org.vadere.simulator.models.SpeedAdjuster;
import org.vadere.simulator.models.osm.PedestrianOSM;
import org.vadere.simulator.models.osm.optimization.StepCircleOptimizer;
import org.vadere.simulator.models.potential.fields.IPotentialFieldTarget;
import org.vadere.simulator.models.potential.fields.PotentialFieldAgent;
import org.vadere.simulator.models.potential.fields.PotentialFieldObstacle;
import org.vadere.state.attributes.models.AttributesOSM;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.scenario.Topography;
import java.util.List;
import java.util.Random;
/**
* Use OSM as locomotion model. Therefore, extend "PedestrianOSM" maybe to inherit "updatePosition" and so on.
*/
public class PedestrianSelfCatThreat extends PedestrianOSM {
public PedestrianSelfCatThreat(AttributesOSM attributesOSM, AttributesAgent attributesPedestrian, Topography topography, Random random, IPotentialFieldTarget potentialFieldTarget, PotentialFieldObstacle potentialFieldObstacle, PotentialFieldAgent potentialFieldPedestrian, List<SpeedAdjuster> speedAdjusters, StepCircleOptimizer stepCircleOptimizer) {
super(attributesOSM, attributesPedestrian, topography, random, potentialFieldTarget, potentialFieldObstacle, potentialFieldPedestrian, speedAdjusters, stepCircleOptimizer);
}
}
package org.vadere.simulator.models.psychology.selfcategorization;
import org.jetbrains.annotations.NotNull;
import org.vadere.annotation.factories.models.ModelClass;
import org.vadere.simulator.models.MainModel;
import org.vadere.simulator.models.Model;
import org.vadere.simulator.models.SpeedAdjuster;
import org.vadere.simulator.models.osm.PedestrianOSM;
import org.vadere.simulator.models.osm.optimization.StepCircleOptimizer;
import org.vadere.simulator.models.osm.updateScheme.UpdateSchemeOSM;
import org.vadere.simulator.models.potential.fields.IPotentialFieldTarget;
import org.vadere.simulator.models.potential.fields.IPotentialFieldTargetGrid;
import org.vadere.simulator.models.potential.fields.PotentialFieldAgent;
import org.vadere.simulator.models.potential.fields.PotentialFieldObstacle;
import org.vadere.state.attributes.Attributes;
import org.vadere.state.attributes.models.AttributesOSM;
import org.vadere.state.attributes.models.AttributesSelfCatThreat;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.psychology.perception.types.Bang;
import org.vadere.state.scenario.DynamicElement;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.scenario.Topography;
import org.vadere.state.types.UpdateType;
import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VShape;
import org.vadere.util.logging.Logger;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
/**
* This class models how agents react to a perceived threat (a loud bang) while considering the
* self-categorization theory ("reicher-2010").
*
* Please, note:
* <ul>
* <li>A {@link Bang} has a loudness and a radius.</li>
* <li>We use the self-categorization theory to divide the agents into in- and out-group members.
* In-group members imitate the behavior of other in-group members. Out-group members ignore the
* behavior of in-group members.</li>
* </ul>
*
* Implement following behavior:
*
* <ol>
* <li>If agent A is within the bang radius, escape (i.e., first maximize distance to the bang and then
* go to a safe zone).</li>
* <li>If agent A is outside bang radius, check if "searchRadius" contains an escaping in-group member.
* If yes, escape also. Otherwise, go to original target.</li>
* <li>Out-group members use the locomotion layer to go to a target (i.e., keep their original behavior).</li>
* </ol>
*/
@ModelClass(isMainModel = true)
public class SelfCatThreatModel implements MainModel {
// Static Variables
private final static Logger logger = Logger.getLogger(SelfCatThreatModel.class);
// Variables
private AttributesSelfCatThreat attributesSelfCatThreat;
private AttributesOSM attributesLocomotion;
AttributesAgent attributesPedestrian;
private Topography topography;
private Random random;
private IPotentialFieldTarget potentialFieldTarget;
private PotentialFieldObstacle potentialFieldObstacle;
private PotentialFieldAgent potentialFieldPedestrian;
private StepCircleOptimizer stepCircleOptimizer;
private UpdateSchemeOSM updateSchemeOSM;
// These models are updated in actual simulation loop.
private List<Model> models = new LinkedList<>();
private double lastSimTimeInSec;
@Override
public List<Model> getSubmodels() {
return models;
}
@Override
public <T extends DynamicElement> DynamicElement createElement(VPoint position, int id, Class<T> type) {
if (!Pedestrian.class.isAssignableFrom(type))
throw new IllegalArgumentException("cannot initialize " + type.getCanonicalName());
AttributesAgent pedAttributes = new AttributesAgent(
this.attributesPedestrian, registerDynamicElementId(topography, id));
PedestrianSelfCatThreat pedestrian = createElement(position, pedAttributes);
return pedestrian;
}
private PedestrianSelfCatThreat createElement(VPoint position, @NotNull final AttributesAgent attributesAgent) {
List<SpeedAdjuster> noSpeedAdjusters = new LinkedList<>();
PedestrianSelfCatThreat pedestrian = new PedestrianSelfCatThreat(attributesLocomotion,
attributesAgent, topography, random, potentialFieldTarget,
potentialFieldObstacle.copy(), potentialFieldPedestrian,
noSpeedAdjusters, stepCircleOptimizer.clone());
pedestrian.setPosition(position);
return pedestrian;
}
@Override
public VShape getDynamicElementRequiredPlace(@NotNull VPoint position) {
return createElement(position, new AttributesAgent(attributesPedestrian, -1)).getShape();
}
@Override
public void initialize(List<Attributes> attributesList, Topography topography, AttributesAgent attributesPedestrian, Random random) {
logger.debug("initialize " + getClass().getSimpleName());
this.attributesSelfCatThreat = Model.findAttributes(attributesList, AttributesSelfCatThreat.class);
this.attributesLocomotion = attributesSelfCatThreat.getAttributesLocomotion();
this.topography = topography;
this.random = random;
this.attributesPedestrian = attributesPedestrian;
initializeLocomotionLayer(attributesList, topography, attributesPedestrian, random);
this.topography.addElementAddedListener(Pedestrian.class, updateSchemeOSM);
this.topography.addElementRemovedListener(Pedestrian.class, updateSchemeOSM);
models.add(potentialFieldTarget);
models.add(this);
}
private void initializeLocomotionLayer(List<Attributes> attributesList, Topography topography, AttributesAgent attributesPedestrian, Random random) {
IPotentialFieldTargetGrid potentialTargetGrid = IPotentialFieldTargetGrid.createPotentialField(
attributesList, topography, attributesPedestrian, attributesLocomotion.getTargetPotentialModel());
this.potentialFieldTarget = potentialTargetGrid;
this.potentialFieldObstacle = PotentialFieldObstacle.createPotentialField(
attributesList, topography, attributesPedestrian, random, attributesLocomotion.getObstaclePotentialModel());
this.potentialFieldPedestrian = PotentialFieldAgent.createPotentialField(
attributesList, topography, attributesPedestrian, random, attributesLocomotion.getPedestrianPotentialModel());
this.stepCircleOptimizer = StepCircleOptimizer.create(
attributesLocomotion, random, topography, potentialTargetGrid);
if (attributesPedestrian.isDensityDependentSpeed()) {
throw new UnsupportedOperationException("densityDependentSpeed not yet implemented.");
// this.speedAdjusters.add(new SpeedAdjusterWeidmann());
}
if (attributesLocomotion.getUpdateType() == UpdateType.PARALLEL) {
throw new IllegalArgumentException("\"UpdateType.PARALLEL\" not supported!");
}
this.updateSchemeOSM = UpdateSchemeOSM.create(attributesLocomotion.getUpdateType(), topography, random);
}
@Override
public void preLoop(final double simTimeInSec) {
this.lastSimTimeInSec = simTimeInSec;
}
@Override
public void postLoop(final double simTimeInSec) {
updateSchemeOSM.shutdown();
}
@Override
public void update(final double simTimeInSec) {
double timeStepInSec = simTimeInSec - this.lastSimTimeInSec;
// TODO: Copy "UpdateSchemeEventDriven" as default locomotion model
// and implement specific behavior there by using behavior repertoire from "OSMBehaviorController":
updateSchemeOSM.update(timeStepInSec, simTimeInSec);
lastSimTimeInSec = simTimeInSec;
}
}
package org.vadere.state.attributes.models;
import org.vadere.state.attributes.Attributes;
public class AttributesSelfCatThreat extends Attributes {
AttributesOSM attributesLocomotion = new AttributesOSM();
public AttributesOSM getAttributesLocomotion() {
return attributesLocomotion;
}
}
......@@ -11,6 +11,7 @@ public class Bang extends Stimulus {
// Member Variables
private int originAsTargetId = -1;
private double loudness = 1;
private double radius = 5;
// Constructors
// Default constructor required for JSON de-/serialization.
......@@ -31,15 +32,18 @@ public class Bang extends Stimulus {
this.originAsTargetId = other.getOriginAsTargetId();
this.loudness = other.getLoudness();
this.radius = other.getRadius();
}
// Getter
public int getOriginAsTargetId() { return originAsTargetId; }
public double getLoudness() { return loudness; }
public double getRadius() { return radius; }
// Setter
public void setOriginAsTargetId(int originAsTargetId) { this.originAsTargetId = originAsTargetId; }
public void setLoudness(double loudness) { this.loudness = loudness; }
public void setRadius(double radius) { this.radius = radius; }
@Override
public Bang clone() {
......
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