Commit 6e485769 authored by Christina's avatar Christina

first control model

parent 86471c0c
Pipeline #298301 failed with stages
in 2 minutes and 30 seconds
......@@ -70,7 +70,7 @@
},
"scenario" : {
"mainModel" : "org.vadere.simulator.models.osm.OptimalStepsModel",
"strategyModel" : "org.vadere.simulator.models.strategy.navigation.RouteChoiceThreeCorridors",
"strategyModel" : "RouteChoiceThreeCorridors",
"attributesModel" : {
"org.vadere.state.attributes.models.AttributesOSM" : {
"stepCircleResolution" : 4,
......@@ -195,9 +195,9 @@
} ],
"measurementAreas" : [ {
"shape" : {
"x" : 60.0,
"x" : 58.0,
"y" : 40.0,
"width" : 10.0,
"width" : 20.0,
"height" : 10.0,
"type" : "RECTANGLE"
},
......@@ -250,7 +250,7 @@
"height" : 5.0,
"type" : "RECTANGLE"
},
"waitingTime" : 0.0,
"waitingTime" : 20.0,
"waitingTimeYellowPhase" : 0.0,
"parallelWaiters" : 0,
"individualWaiting" : true,
......
......@@ -100,7 +100,7 @@ public class ModelPresets {
private void registerModelPreset(Class<? extends MainModel> mainModelClass,
List<Class<? extends Attributes>> attributesClasses) {
ModelDefinition definition = new ModelDefinition(mainModelClass.getName(), null);
ModelDefinition definition = new ModelDefinition(mainModelClass.getName(), "123" , null);
definition.createAndSetDefaultAttributes(attributesClasses);
modelDefinitionPresets.add(definition);
}
......
......@@ -180,6 +180,7 @@ public class TextView extends JPanel implements IJsonView {
case MODEL:
ModelDefinition modelDefinition = JsonConverter.deserializeModelDefinition(json);
currentScenario.getScenarioStore().setMainModel(modelDefinition.getMainModel());
currentScenario.getScenarioStore().setStrategyModel(modelDefinition.getStrategyModel());
currentScenario.setAttributesModel(modelDefinition.getAttributesList());
break;
case SIMULATION:
......
......@@ -47,9 +47,9 @@ public class TestProjectWriterAndReader {
attributes.add(new AttributesFloorField());
attributes.add(new AttributesPotentialOSM());
LinkedList<Scenario> tests = new LinkedList<>();
tests.add(new Scenario(new ScenarioStore(testName + "1", "", OptimalStepsModel.class.getName(), attributes, new AttributesSimulation(), new Topography())));
tests.add(new Scenario(new ScenarioStore(testName + "2", "", OptimalStepsModel.class.getName(), attributes, new AttributesSimulation(), new Topography())));
tests.add(new Scenario(new ScenarioStore(testName + "3", "", OptimalStepsModel.class.getName(), attributes, new AttributesSimulation(), new Topography())));
tests.add(new Scenario(new ScenarioStore(testName + "1", "", OptimalStepsModel.class.getName(), attributes, new AttributesSimulation(), new Topography(), strategyModel)));
tests.add(new Scenario(new ScenarioStore(testName + "2", "", OptimalStepsModel.class.getName(), attributes, new AttributesSimulation(), new Topography(), strategyModel)));
tests.add(new Scenario(new ScenarioStore(testName + "3", "", OptimalStepsModel.class.getName(), attributes, new AttributesSimulation(), new Topography(), strategyModel)));
testProject = new VadereProject(testProjectName, tests, Paths.get("."));
}
......
......@@ -17,9 +17,11 @@ import org.vadere.simulator.control.psychology.perception.models.IPerceptionMode
import org.vadere.simulator.control.psychology.perception.helpers.PerceptionModelBuilder;
import org.vadere.simulator.control.psychology.perception.StimulusController;
import org.vadere.simulator.control.scenarioelements.TargetChangerController;
import org.vadere.simulator.control.strategy.helpers.StrategyModelBuilder;
import org.vadere.simulator.models.MainModel;
import org.vadere.simulator.models.MainModelBuilder;
import org.vadere.simulator.models.potential.solver.EikonalSolverCacheProvider;
import org.vadere.simulator.control.strategy.models.IStrategyModel;
import org.vadere.simulator.projects.Domain;
import org.vadere.simulator.projects.RunnableFinishedListener;
import org.vadere.simulator.projects.Scenario;
......@@ -178,6 +180,8 @@ public class ScenarioRun implements Runnable {
IPerceptionModel perceptionModel = PerceptionModelBuilder.instantiateModel(scenarioStore);
ICognitionModel cognitionModel = CognitionModelBuilder.instantiateModel(scenarioStore);
IStrategyModel strategyModel = StrategyModelBuilder.instantiateModel(scenarioStore);
// ensure all elements have unique id before attributes are sealed
scenario.getTopography().generateUniqueIdIfNotSet();
......@@ -190,7 +194,7 @@ public class ScenarioRun implements Runnable {
passiveCallbacks, random,
processorManager, simulationResult,
remoteRunListeners, singleStepMode,
scenarioCache);
scenarioCache, strategyModel);
}
simulation.run();
......
......@@ -5,6 +5,7 @@ import org.vadere.simulator.control.psychology.cognition.models.ICognitionModel;
import org.vadere.simulator.control.psychology.perception.models.IPerceptionModel;
import org.vadere.simulator.control.psychology.perception.StimulusController;
import org.vadere.simulator.control.scenarioelements.*;
import org.vadere.simulator.control.strategy.models.IStrategyModel;
import org.vadere.simulator.models.DynamicElementFactory;
import org.vadere.simulator.models.MainModel;
import org.vadere.simulator.models.Model;
......@@ -17,6 +18,7 @@ import org.vadere.simulator.projects.Domain;
import org.vadere.simulator.projects.ScenarioStore;
import org.vadere.simulator.projects.SimulationResult;
import org.vadere.simulator.projects.dataprocessing.ProcessorManager;
import org.vadere.simulator.projects.dataprocessing.processor.DataProcessor;
import org.vadere.simulator.utils.cache.ScenarioCache;
import org.vadere.state.attributes.AttributesSimulation;
import org.vadere.state.attributes.scenario.AttributesAgent;
......@@ -79,6 +81,7 @@ public class Simulation {
private final MainModel mainModel;
private final IPerceptionModel perceptionModel;
private final ICognitionModel cognitionModel;
private final IStrategyModel strategyModel;
/** Hold the topography in an extra field for convenience. */
private final Topography topography;
......@@ -88,6 +91,7 @@ public class Simulation {
private final StimulusController stimulusController;
private final ScenarioCache scenarioCache;
public Simulation(MainModel mainModel, IPerceptionModel perceptionModel,
ICognitionModel cognitionModel, double startTimeInSec,
final String name, ScenarioStore scenarioStore,
......@@ -95,12 +99,14 @@ public class Simulation {
List<PassiveCallback> passiveCallbacks, Random random,
ProcessorManager processorManager, SimulationResult simulationResult,
List<RemoteRunListener> remoteRunListeners, boolean singleStepMode,
ScenarioCache scenarioCache) {
ScenarioCache scenarioCache, IStrategyModel strategyModel) {
this.name = name;
this.mainModel = mainModel;
this.perceptionModel = perceptionModel;
this.cognitionModel = cognitionModel;
this.strategyModel = strategyModel;
this.scenarioStore = scenarioStore;
this.attributesSimulation = scenarioStore.getAttributesSimulation();
this.attributesAgent = scenarioStore.getTopography().getAttributesPedestrian();
......@@ -371,8 +377,11 @@ public class Simulation {
}
private void updateCallbacks(double simTimeInSec) {
updateScenarioElements(simTimeInSec);
updateStrategyLayer(simTimeInSec);
updatePsychologyLayer(simTimeInSec);
updateLocomotionLayer(simTimeInSec);
......@@ -411,6 +420,20 @@ public class Simulation {
topographyController.update(simTimeInSec); //rebuild CellGrid
}
private void updateStrategyLayer(double simTimeInSec) {
Collection<Pedestrian> pedestrians = topography.getElements(Pedestrian.class);
if (simTimeInSec == startTimeInSec)
{
strategyModel.update(simTimeInSec, pedestrians, null);
}
strategyModel.update(simTimeInSec, pedestrians, processorManager);
}
private void updatePsychologyLayer(double simTimeInSec) {
Collection<Pedestrian> pedestrians = topography.getElements(Pedestrian.class);
......@@ -424,6 +447,8 @@ public class Simulation {
}
}
private void updateLocomotionLayer(double simTimeInSec) {
for (Model m : models) {
List<SourceController> stillSpawningSource = this.sourceControllers.stream().filter(s -> !s.isSourceFinished(simTimeInSec)).collect(Collectors.toList());
......
package org.vadere.simulator.control.strategy.helpers;
import org.vadere.simulator.control.strategy.models.IStrategyModel;
import org.vadere.simulator.projects.ScenarioStore;
import org.vadere.util.reflection.DynamicClassInstantiator;
public class StrategyModelBuilder {
public static final String JAVA_PACKAGE_SEPARATOR = ".";
public static IStrategyModel instantiateModel(ScenarioStore scenarioStore) {
String simpleClassName = scenarioStore.getStrategyModel();
String classSearchPath = "org.vadere.simulator.models.strategy";
String fullyQualifiedClassName = classSearchPath + JAVA_PACKAGE_SEPARATOR + simpleClassName;
DynamicClassInstantiator<IStrategyModel> instantiator = new DynamicClassInstantiator<>();
IStrategyModel strategyModel = instantiator.createObject(fullyQualifiedClassName);
return strategyModel;
}
}
package org.vadere.simulator.control.strategy.models;
import org.vadere.simulator.projects.dataprocessing.ProcessorManager;
import org.vadere.state.scenario.Pedestrian;
import java.util.Collection;
public interface IStrategyModel {
/*
* @param pedestrians The pedestrians to update
*/
void update(double simTimeInSec, Collection<Pedestrian> pedestrians, ProcessorManager processorManager);
}
package org.vadere.simulator.control.strategy.models.navigation;
import org.vadere.simulator.control.strategy.models.IStrategyModel;
public interface INavigationModel extends IStrategyModel {
}
......@@ -5,8 +5,10 @@ import org.vadere.state.attributes.Attributes;
import org.vadere.state.attributes.exceptions.AttributesMultiplyDefinedException;
import org.vadere.state.attributes.exceptions.AttributesNotFoundException;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.util.data.FindByClass;
import java.util.Collection;
import java.util.List;
import java.util.Random;
......@@ -24,7 +26,7 @@ public interface Model {
* list and creating sub models. It also sets attributes recursively for its sub models.
*/
void initialize(List<Attributes> attributesList, Domain domain,
AttributesAgent attributesPedestrian, Random random);
AttributesAgent attributesPedestrian, Random random);
void preLoop(final double simTimeInSec);
......
package org.vadere.simulator.models.strategy;
import org.apache.commons.math.distribution.DiscreteDistribution;
import org.vadere.simulator.control.strategy.models.navigation.INavigationModel;
import org.vadere.simulator.projects.dataprocessing.ProcessorManager;
import org.vadere.state.scenario.Pedestrian;
import java.util.*;
import java.util.stream.Collectors;
import static org.apache.tools.ant.taskdefs.rmic.DefaultRmicAdapter.rand;
public class RouteChoiceThreeCorridors implements INavigationModel {
@Override
public void update(double simTimeInSec, Collection<Pedestrian> pedestrians, ProcessorManager processorManager) {
if (simTimeInSec > 0.0) {
// get data from dataprocessors if necessary
LinkedList<Double> densities = new LinkedList<Double>();
densities.add( getDensityFromDataProcessor(5, processorManager) );
densities.add( getDensityFromDataProcessor(6, processorManager) );
densities.add( getDensityFromDataProcessor(7, processorManager) );
double density = getDensityFromDataProcessor(8, processorManager);
double maxDensity = 10.0;
double remainingCapacity;
LinkedList<Double> factors = new LinkedList<Double>();
LinkedList<Integer> factorsNorm = new LinkedList<Integer>();
double sum = 0;
if (density > maxDensity){
for (Double d : densities) {
remainingCapacity = maxDensity - d;
double fac = 0;
if (remainingCapacity > 0) {
fac = 1;
}
factors.add(fac);
sum += fac;
}
}
else{
factors.add(0.0);
factors.add(0.0);
factors.add(1.0);
sum = 1.0;
}
List<Pedestrian> newAgents = pedestrians.stream().filter(p-> p.getFootstepHistory().getFootSteps().size() == 0).collect(Collectors.toList());
int numberOfNewAgents = (int) newAgents.size();
int[] target = {2001,2002,2003};
int c = 0;
for (Double f : factors){
int n = (int) (f/sum*numberOfNewAgents);
for (int i = 0; i < n; i++){
factorsNorm.add( target[c] );
}
c += 1;
}
LinkedList<Integer> nextTargets = new LinkedList<Integer>();
c = 0;
for (Pedestrian pedestrian : newAgents) {
nextTargets.add(factorsNorm.get(c));
pedestrian.setTargets(nextTargets);
c+=1;
}
}
}
/* public void update(double simTimeInSec, Collection<Pedestrian> pedestrians, ProcessorManager processorManager) {
if (simTimeInSec > 0.0) {
// get data from dataprocessors if necessary
double density1 = getDensityFromDataProcessor(5, processorManager);
double density2 = getDensityFromDataProcessor(6, processorManager);
double density3 = getDensityFromDataProcessor(7, processorManager);
double density = getDensityFromDataProcessor(8, processorManager);
LinkedList<Integer> nextTargets = new LinkedList<Integer>();
int newTarget = 2003;
if (density > 10.0){
newTarget = 2002;}
if (density > 20.0){
newTarget = 2001;}
for (Pedestrian pedestrian : pedestrians) {
if (pedestrian.getFootstepHistory().size() == 0) {
nextTargets.add(newTarget);
pedestrian.setTargets(nextTargets);
}
}
}
}*/
private double getDensityFromDataProcessor(int processorId, ProcessorManager processorManager) {
double density = -1.0;
if (processorManager != null) {
TreeMap data = (TreeMap) processorManager.getProcessor(processorId).getData();
if (data.size() > 0) {
density = (double) (int) data.lastEntry().getValue();
}
}
return density;
}
}
package org.vadere.simulator.models.strategy;
import org.vadere.simulator.models.Model;
public interface StrategyModel extends Model {
}
package org.vadere.simulator.models.strategy.navigation;
import org.vadere.simulator.models.strategy.StrategyModel;
public interface NavigationModel extends StrategyModel {
}
package org.vadere.simulator.models.strategy.navigation;
import org.vadere.simulator.projects.Domain;
import org.vadere.state.attributes.Attributes;
import org.vadere.state.attributes.scenario.AttributesAgent;
import java.util.List;
import java.util.Random;
public class RouteChoiceThreeCorridors implements NavigationModel {
@Override
public void initialize(List<Attributes> attributesList, Domain domain, AttributesAgent attributesPedestrian, Random random) {
}
@Override
public void preLoop(double simTimeInSec) {
}
@Override
public void postLoop(double simTimeInSec) {
}
@Override
public void update(double simTimeInSec) {
}
}
......@@ -3,7 +3,6 @@ package org.vadere.simulator.projects;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.jetbrains.annotations.NotNull;
import org.vadere.simulator.models.strategy.StrategyModel;
import org.vadere.simulator.projects.io.JsonConverter;
import org.vadere.state.attributes.Attributes;
import org.vadere.state.attributes.AttributesPsychology;
......@@ -128,6 +127,10 @@ public class ScenarioStore {
this.mainModel = mainModel;
}
public void setStrategyModel(final String strategyModel) {
this.strategyModel = strategyModel;
}
public void setName(final String name) {
this.name = name;
}
......
......@@ -104,6 +104,11 @@ public abstract class DataProcessor<K extends DataKey<K>, V> {
return data.get(key);
}
public int getLastStep()
{
return this.lastStep;
}
protected void putValue(final K key, final V value) {
this.data.put(key, value);
}
......
......@@ -3,7 +3,6 @@ package org.vadere.simulator.projects.io;
import java.io.IOException;
import java.util.List;
import org.vadere.simulator.models.strategy.StrategyModel;
import org.vadere.util.version.Version;
import org.vadere.simulator.models.MainModel;
import org.vadere.simulator.projects.Scenario;
......@@ -41,7 +40,9 @@ public class JsonConverter {
@SuppressWarnings("unused")
MainModel dummyToProvokeClassCast = instantiator.createObject(mainModelString);
}
return new ModelDefinition(mainModelString, StateJsonConverter.deserializeAttributesListFromNode(node.get("attributesModel")));
String strategyModel = node.get("strategyModel").asText();
return new ModelDefinition(mainModelString, strategyModel, StateJsonConverter.deserializeAttributesListFromNode(node.get("attributesModel")));
}
public static Scenario deserializeScenarioRunManagerFromNode(JsonNode node) throws IOException, IllegalArgumentException {
......
package org.vadere.state.attributes;
public class AttributesStrategyModel {
private String strategyModel = null;
// Constructors
public AttributesStrategyModel() {
}
public AttributesStrategyModel(String strategyModel) {
this.strategyModel = strategyModel;
}
// Getter
public String getStrategyModel() {
return this.strategyModel;
}
public void setStrategyModel(String strategyModel) {
this.strategyModel = strategyModel;
}
}
......@@ -12,13 +12,17 @@ import org.vadere.util.reflection.DynamicClassInstantiator;
public class ModelDefinition {
private String mainModel;
private String strategyModel;
private List<Attributes> attributesModel;
public ModelDefinition(String mainModel, List<Attributes> attributesModel) {
public ModelDefinition(String mainModel, String strategyModel, List<Attributes> attributesModel) {
this.mainModel = mainModel;
this.strategyModel = strategyModel;
this.attributesModel = attributesModel;
}
public void createAndSetDefaultAttributes(List<Class<? extends Attributes>> attributesClasses) {
DynamicClassInstantiator<Attributes> instantiator = new DynamicClassInstantiator<>();
attributesModel = new ArrayList<>(attributesClasses.size());
......@@ -31,9 +35,17 @@ public class ModelDefinition {
return mainModel;
}
public String getStrategyModel() {
return strategyModel;
}
public void setMainModel(String mainModel) {
this.mainModel = mainModel;
}
public void setStrategyModel(String strategyModel) {
this.strategyModel = strategyModel;
}
public List<Attributes> getAttributesList() {
return attributesModel;
......
......@@ -147,6 +147,7 @@ public abstract class StateJsonConverter {
return deserializeTopographyFromNode(mapper.readTree(json));
}
public static Topography deserializeTopographyFromNode(JsonNode node) throws IllegalArgumentException {
TopographyStore store = mapper.convertValue(node, TopographyStore.class);
Topography topography = new Topography(store.attributes, store.attributesPedestrian, store.attributesCar);
......
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