Commit a4a8e223 authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

Merge branch 'stsc_SourcnegExpDist' into 'master'

Stsc sourcneg exp dist

See merge request !122
parents b9c8c5b9 ab6c6906
Pipeline #225561 passed with stages
in 129 minutes and 6 seconds
package org.vadere.simulator.projects.dataprocessing.datakey;
import org.jetbrains.annotations.NotNull;
import org.vadere.simulator.projects.dataprocessing.outputfile.TimeGridOutputFile;
import java.util.Objects;
@OutputFileMap(outputFileClass = TimeGridOutputFile.class)
public class TimeGridKey implements DataKey<TimeGridKey> {
private final int timestep;
private final double x;
private final double y;
private final double size;
public TimeGridKey(int timestep, double x, double y, double size) {
this.timestep = timestep;
this.x = x;
this.y = y;
this.size = size;
}
public int getTimestep() {
return timestep;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getSize() {
return size;
}
public static String[] getHeaders() {
return new String[] { TimestepKey.getHeader(), "x", "y", "size"};
}
public String[] toStrings(){
return new String[]{Integer.toString(timestep), Double.toString(x), Double.toString(y), Double.toString(size)};
}
@Override
public int compareTo(@NotNull TimeGridKey other) {
int ret;
if ((ret = Integer.compare(timestep, other.timestep))==0){
if ((ret = Double.compare(x, other.x)) == 0){
if ((ret = Double.compare(y, other.y)) == 0){
if ((ret = Double.compare(size, other.size)) == 0){
return 0;
}
return ret;
}
return ret;
}
return ret;
}
return ret;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TimeGridKey that = (TimeGridKey) o;
return timestep == that.timestep &&
Double.compare(that.x, x) == 0 &&
Double.compare(that.y, y) == 0 &&
Double.compare(that.size, size) == 0;
}
@Override
public int hashCode() {
return Objects.hash(timestep, x, y, size);
}
}
package org.vadere.simulator.projects.dataprocessing.outputfile;
import org.vadere.annotation.factories.outputfiles.OutputFileClass;
import org.vadere.simulator.projects.dataprocessing.datakey.TimeGridKey;
@OutputFileClass(dataKeyMapping = TimeGridKey.class)
public class TimeGridOutputFile extends OutputFile<TimeGridKey> {
public TimeGridOutputFile(String... dataIndices) {
super(TimeGridKey.getHeaders());
}
public String[] toStrings(TimeGridKey key) {
return key.toStrings();
}
}
package org.vadere.simulator.projects.dataprocessing.processor;
import org.vadere.annotation.factories.dataprocessors.DataProcessorClass;
import org.vadere.simulator.control.simulation.SimulationState;
import org.vadere.simulator.projects.dataprocessing.ProcessorManager;
import org.vadere.simulator.projects.dataprocessing.datakey.TimeGridKey;
import org.vadere.state.attributes.processor.AttributesAreaDensityGridCountingProcessor;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.util.geometry.LinkedCellsGrid;
@DataProcessorClass(label = "AreaDensityGridCountingProcessor")
public class AreaDensityGridCountingProcessor extends DataProcessor<TimeGridKey, Integer> {
public AreaDensityGridCountingProcessor() {
super("gridCount");
setAttributes(new AttributesAreaDensityGridCountingProcessor());
}
@Override
protected void doUpdate(SimulationState state) {
int step = state.getStep();
AttributesAreaDensityGridCountingProcessor attr = (AttributesAreaDensityGridCountingProcessor)getAttributes();
LinkedCellsGrid<Pedestrian> cellsElements = new LinkedCellsGrid<Pedestrian>(
state.getTopography().getBounds().x,
state.getTopography().getBounds().y,
state.getTopography().getBounds().width,
state.getTopography().getBounds().height,
attr.getCellSize());
state.getTopography().getPedestrianDynamicElements().getElements().forEach(cellsElements::addObject);
int[][] count = cellsElements.getCellObjectCount();
int pedCount;
for (int r = 0; r < count.length; r++) {
for (int c = 0; c < count[r].length; c++) {
pedCount = count[r][c];
this.putValue(new TimeGridKey(step,r*attr.getCellSize(),c*attr.getCellSize(), attr.getCellSize()), pedCount);
}
}
}
@Override
public void init(ProcessorManager manager) {
super.init(manager);
}
}
......@@ -172,7 +172,7 @@ public class GroupSourceControllerTest extends TestSourceControllerUsingConstant
double endTime = 10.0;
SourceTestAttributesBuilder builder = new SourceTestAttributesBuilder()
.setStartTime(startTime).setEndTime(endTime)
.setSpawnIntervalForConstantDistribution(10)
.setDistributionParams(10)
.setSourceDim(5.0, 5.0)
.setGroupSizeDistribution(0.0, 0.5, 0.5)
.setGroupSizeDistributionMock(2, 3, 2, 3);
......@@ -194,7 +194,7 @@ public class GroupSourceControllerTest extends TestSourceControllerUsingConstant
double endTime = 10.0;
SourceTestAttributesBuilder builder = new SourceTestAttributesBuilder()
.setStartTime(0).setEndTime(endTime)
.setSpawnIntervalForConstantDistribution(5)
.setDistributionParams(5)
.setSourceDim(5.0, 5.0)
.setGroupSizeDistribution(0.0, 0.5, 0.5)
.setGroupSizeDistributionMock(2, 3, 2, 3);
......@@ -214,7 +214,7 @@ public class GroupSourceControllerTest extends TestSourceControllerUsingConstant
double endTime = 1.0;
SourceTestAttributesBuilder builder = new SourceTestAttributesBuilder()
.setStartTime(0).setEndTime(endTime)
.setSpawnIntervalForConstantDistribution(0.1)
.setDistributionParams(0.1)
.setSourceDim(5.0, 5.0)
.setGroupSizeDistribution(0.0, 0.5, 0.5)
.setGroupSizeDistributionMock(2, 3, 2, 3, 2, 2, 3, 3, 3, 2, 2, 3, 3, 3, 3);
......@@ -325,7 +325,7 @@ public class GroupSourceControllerTest extends TestSourceControllerUsingConstant
public void testSpawnRateGreaterThanUpdateRate() {
SourceTestAttributesBuilder builder = new SourceTestAttributesBuilder()
.setStartTime(0).setEndTime(1)
.setSpawnIntervalForConstantDistribution(0.3)
.setDistributionParams(0.3)
.setSourceDim(5.0, 5.0)
.setGroupSizeDistribution(0.0, 0.0, 0.25, 0.75)
.setGroupSizeDistributionMock(4, 3, 4, 4);
......
package org.vadere.simulator.control;
import org.jetbrains.annotations.NotNull;
import org.junit.Before;
import org.vadere.simulator.control.factory.SingleSourceControllerFactory;
import org.vadere.simulator.control.factory.SourceControllerFactory;
import org.vadere.simulator.control.scenarioelements.SourceController;
import org.vadere.simulator.models.DynamicElementFactory;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.attributes.scenario.AttributesSource;
import org.vadere.state.attributes.scenario.SourceTestAttributesBuilder;
import org.vadere.state.scenario.DynamicElement;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.scenario.Source;
import org.vadere.state.scenario.Topography;
import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VShape;
import java.io.IOException;
import java.util.*;
import static org.junit.Assert.assertEquals;
public class TestSourceController {
ArrayList<TestSourceControllerUsingConstantSpawnRate.SourceTestData> sourceTestData;
@Before
public void init() {
sourceTestData = new ArrayList<>();
}
public SourceControllerFactory getSourceControllerFactory(SourceTestData d) {
return new SingleSourceControllerFactory();
}
protected SourceTestData first() {
return sourceTestData.get(0);
}
protected SourceTestData second() {
return sourceTestData.get(1);
}
protected int countPedestrians(int source) {
return sourceTestData.get(source).topography.getElements(Pedestrian.class).size();
}
protected int countPedestriansAndRemove(int source) {
int ret = sourceTestData.get(source).topography.getElements(Pedestrian.class).size();
sourceTestData.get(source).topography.getPedestrianDynamicElements().clear();
return ret;
}
protected void pedestrianCountEquals(int expected) {
assertEquals(expected, countPedestrians(0));
}
public void initialize(SourceTestAttributesBuilder builder) throws IOException {
SourceTestData d = new SourceTestData();
d.attributesSource = builder.getResult();
d.attributesPedestrian = new AttributesAgent();
d.random = new Random(builder.getRandomSeed());
d.source = new Source(d.attributesSource);
d.pedestrianFactory = new DynamicElementFactory() {
private int pedestrianIdCounter = 0;
@Override
public <T extends DynamicElement> DynamicElement createElement(VPoint position, int id, Class<T> type) {
AttributesAgent att = new AttributesAgent(
d.attributesPedestrian, registerDynamicElementId(null, id));
Pedestrian ped = new Pedestrian(att, d.random);
ped.setPosition(position);
return ped;
}
@Override
public int registerDynamicElementId(Topography topography, int id) {
return id > 0 ? id : ++pedestrianIdCounter;
}
@Override
public int getNewDynamicElementId(Topography topography) {
return registerDynamicElementId(topography, AttributesAgent.ID_NOT_SET);
}
@Override
public VShape getDynamicElementRequiredPlace(@NotNull VPoint position) {
return createElement(position, AttributesAgent.ID_NOT_SET, Pedestrian.class).getShape();
}
};
d.sourceControllerFactory = getSourceControllerFactory(d);
d.sourceController = d.sourceControllerFactory.create(d.topography, d.source,
d.pedestrianFactory, d.attributesPedestrian, d.random);
sourceTestData.add(d);
}
class SourceTestData {
public Random random;
public AttributesAgent attributesPedestrian;
public DynamicElementFactory pedestrianFactory;
public Source source;
public Topography topography = new Topography();
public SourceController sourceController;
public AttributesSource attributesSource;
public SourceControllerFactory sourceControllerFactory;
public long randomSeed = 0;
}
}
package org.vadere.simulator.control;
import org.jetbrains.annotations.NotNull;
import org.junit.Before;
import org.junit.Test;
import org.vadere.simulator.control.factory.SingleSourceControllerFactory;
import org.vadere.simulator.control.factory.SourceControllerFactory;
import org.vadere.simulator.control.scenarioelements.SourceController;
import org.vadere.simulator.models.DynamicElementFactory;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.attributes.scenario.AttributesSource;
import org.vadere.state.attributes.scenario.SourceTestAttributesBuilder;
import org.vadere.state.scenario.DynamicElement;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.scenario.Source;
import org.vadere.state.scenario.Topography;
import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VRectangle;
import org.vadere.util.geometry.shapes.VShape;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import static org.junit.Assert.assertEquals;
public class TestSourceControllerUsingConstantSpawnRate {
public class TestSourceControllerUsingConstantSpawnRate extends TestSourceController {
ArrayList<SourceTestData> sourceTestData;
protected SourceTestData first() {
return sourceTestData.get(0);
}
protected SourceTestData second() {
return sourceTestData.get(1);
}
@Before
public void init() {
sourceTestData = new ArrayList<>();
}
public SourceControllerFactory getSourceControllerFactory(SourceTestData d) {
return new SingleSourceControllerFactory();
}
public void initialize(SourceTestAttributesBuilder builder) throws IOException{
SourceTestData d = new SourceTestData();
d.attributesSource = builder.getResult();
d.attributesPedestrian = new AttributesAgent();
d.random = new Random(builder.getRandomSeed());
d.source = new Source(d.attributesSource);
d.pedestrianFactory = new DynamicElementFactory() {
private int pedestrianIdCounter = 0;
@Override
public <T extends DynamicElement> DynamicElement createElement(VPoint position, int id, Class<T> type) {
AttributesAgent att = new AttributesAgent(
d.attributesPedestrian, registerDynamicElementId(null, id));
Pedestrian ped = new Pedestrian(att, d.random);
ped.setPosition(position);
return ped;
}
@Override
public int registerDynamicElementId(Topography topography, int id) {
return id > 0 ? id : ++pedestrianIdCounter;
}
@Override
public int getNewDynamicElementId(Topography topography) {
return registerDynamicElementId(topography, AttributesAgent.ID_NOT_SET);
}
@Override
public VShape getDynamicElementRequiredPlace(@NotNull VPoint position) {
return createElement(position, AttributesAgent.ID_NOT_SET, Pedestrian.class).getShape();
}
};
d.sourceControllerFactory = getSourceControllerFactory(d);
d.sourceController = d.sourceControllerFactory.create(d.topography, d.source,
d.pedestrianFactory, d.attributesPedestrian, d.random);
sourceTestData.add(d);
}
/**
* Test method for {@link SourceController#update(double)}.
......@@ -122,7 +44,7 @@ public class TestSourceControllerUsingConstantSpawnRate {
double endTime = 10.0;
SourceTestAttributesBuilder builder = new SourceTestAttributesBuilder()
.setStartTime(startTime).setEndTime(endTime)
.setSpawnIntervalForConstantDistribution(10);
.setDistributionParams(10);
initialize(builder);
first().sourceController.update(startTime);
......@@ -143,7 +65,7 @@ public class TestSourceControllerUsingConstantSpawnRate {
double endTime = 10.0;
SourceTestAttributesBuilder builder = new SourceTestAttributesBuilder()
.setStartTime(0).setEndTime(endTime)
.setSpawnIntervalForConstantDistribution(5);
.setDistributionParams(5);
initialize(builder);
for (double simTimeInSec = 0; simTimeInSec < endTime * 2; simTimeInSec += 1.0) {
......@@ -162,7 +84,7 @@ public class TestSourceControllerUsingConstantSpawnRate {
double endTime = 1.0;
SourceTestAttributesBuilder builder = new SourceTestAttributesBuilder()
.setStartTime(0).setEndTime(endTime)
.setSpawnIntervalForConstantDistribution(0.1);
.setDistributionParams(0.1);
initialize(builder);
for (double simTimeInSec = 0; simTimeInSec < endTime * 2; simTimeInSec += 1.0) {
......@@ -208,13 +130,7 @@ public class TestSourceControllerUsingConstantSpawnRate {
assertEquals("wrong pedestrian number.", 100, countPedestrians(0));
}
protected int countPedestrians(int source) {
return sourceTestData.get(source).topography.getElements(Pedestrian.class).size();
}
protected void pedestrianCountEquals(int expected) {
assertEquals(expected, countPedestrians(0));
}
protected void doUpdates(int source, int number, double startTime, double endTimeExclusive) {
double timeStep = (endTimeExclusive - startTime) / number;
......@@ -238,15 +154,4 @@ public class TestSourceControllerUsingConstantSpawnRate {
}
}
class SourceTestData {
public Random random;
public AttributesAgent attributesPedestrian;
public DynamicElementFactory pedestrianFactory;
public Source source;
public Topography topography = new Topography();
public SourceController sourceController;
public AttributesSource attributesSource;
public SourceControllerFactory sourceControllerFactory;
public long randomSeed = 0;
}
}
......@@ -90,7 +90,7 @@ public class TestSourceControllerUsingDistributions extends TestSourceController
public void testSpawnRateGreaterThanUpdateRate() throws IOException {
SourceTestAttributesBuilder builder = new SourceTestAttributesBuilder()
.setStartTime(0).setEndTime(1)
.setSpawnIntervalForConstantDistribution(0.3);
.setDistributionParams(0.3);
initialize(builder);
// per update only one "spawn action" is performed.
......
package org.vadere.simulator.control;
import org.junit.Test;
import org.vadere.simulator.control.scenarioelements.SourceController;
import org.vadere.state.attributes.scenario.SourceTestAttributesBuilder;
import org.vadere.state.scenario.NegativeExponentialDistribution;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
public class TestSourceControllerUsingNegativeExponentialSpawnRate extends TestSourceController {
@Test
public void testSpawnTimeDist() throws IOException{
int timeSteps = 10000;
double meanIterArrival = 0.35;
double totalTime = timeSteps* 0.4*meanIterArrival/0.2;
SourceTestAttributesBuilder builder =
new SourceTestAttributesBuilder()
.setRandomSeed(55)
.setStartTime(0.0)
.setEndTime(totalTime)
.setDistributionClass(NegativeExponentialDistribution.class)
.setDistributionParams(meanIterArrival);
initialize(builder);
SourceController s = first().sourceController;
double sumPeds = 0;
for (double t = 0; t < totalTime; t+=0.4) {
s.update(t);
double ped = countPedestriansAndRemove(0);
sumPeds += ped;
}
double arrivalRatePerSec = sumPeds / totalTime;
assertEquals(meanIterArrival, 1/arrivalRatePerSec,0.1);
}
}
package org.vadere.state.attributes.processor;
public class AttributesAreaDensityGridCountingProcessor extends AttributesProcessor {
double cellSize = 5.0;
public double getCellSize() {
return cellSize;
}
public void setCellSize(double cellSize) {
checkSealed();
this.cellSize = cellSize;
}
}
......@@ -60,8 +60,8 @@ public class SourceTestAttributesBuilder {
return this;
}
public SourceTestAttributesBuilder setSpawnIntervalForConstantDistribution(double spawnDelay) {
this.distributionParams = new double[]{spawnDelay};
public SourceTestAttributesBuilder setDistributionParams(double parameter) {
this.distributionParams = new double[]{parameter};
return this;
}
......
package org.vadere.state.scenario;
import org.apache.commons.math3.distribution.ExponentialDistribution;
import org.apache.commons.math3.random.RandomGenerator;
public class NegativeExponentialDistribution extends ExponentialDistribution {
/**
*
* @param mean inter-arrival time
*/
public NegativeExponentialDistribution(RandomGenerator rng, double mean) {
super(rng, mean);
}
}
......@@ -202,6 +202,16 @@ public class LinkedCellsGrid<T extends PointPositioned> implements Iterable<T> {
return new int[] {iX, iY};
}
public int[][] getCellObjectCount(){
int[][] count = new int[this.gridSize[0]][this.gridSize[1]];
for (int r = 0; r < grid.length; r++) {