Commit 758330c4 authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

add NegativeExponentialDistribution wrapper for pedestrian spawning

parent 488d9311
Pipeline #222647 passed with stages
in 132 minutes and 26 seconds
......@@ -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);
}
}
......@@ -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);
}
}
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