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

Extended unit tests in "OSMBehaviorControllerTest"

Also fixed incomplete initialization of mock objects
in method "createTwoTargetsAndTwoPedestrians()".
parent 3e131c34
......@@ -301,11 +301,13 @@ public class OSMBehaviorController {
makeStep(pedestrian1, topography, durationStep);
makeStep(pedestrian2, topography, durationStep);
// TODO The experiment showed that speed decreased (to half of free-flow velocity).
// Maybe, use "pedestrian.getDurationNextStep() * 2".
pedestrian1.setTimeOfNextStep(endTimeStep);
pedestrian2.setTimeOfNextStep(endTimeStep);
// TODO:
// "makeStep()" already invokes
// "pedestrian.setTimeCredit(pedestrian.getTimeCredit() - pedestrian.getDurationNextStep())"
// => Ask BZ if is it really necessary to call it twice (and subtract duration twice)?
pedestrian1.setTimeCredit(pedestrian1.getTimeCredit() - durationStep);
pedestrian2.setTimeCredit(pedestrian1.getTimeCredit());
}
......
package org.vadere.simulator.models.osm;
import org.junit.Test;
import org.vadere.simulator.models.SpeedAdjuster;
import org.vadere.simulator.models.potential.fields.IPotentialFieldTargetGrid;
import org.vadere.state.attributes.Attributes;
import org.vadere.state.attributes.models.AttributesFloorField;
import org.vadere.state.attributes.models.AttributesOSM;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.attributes.scenario.AttributesTarget;
import org.vadere.state.psychology.cognition.SelfCategory;
import org.vadere.state.scenario.Target;
import org.vadere.state.scenario.Topography;
import org.vadere.util.geometry.shapes.VCircle;
......@@ -18,7 +20,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
public class OSMBehaviorControllerTest {
......@@ -55,7 +57,7 @@ public class OSMBehaviorControllerTest {
);
}
private void createOppositeDirectionTopography() {
private void createOppositeDirectionVariation1Topography() {
createTwoTargetsAndTwoPedestrians(
new VPoint(-1, 0), // target1
new VPoint(2, 0), // target2
......@@ -64,6 +66,15 @@ public class OSMBehaviorControllerTest {
);
}
private void createOppositeDirectionVariation2Topography() {
createTwoTargetsAndTwoPedestrians(
new VPoint(-1, 0), // target1
new VPoint(2, 0), // target2
new VPoint(1, 0), // pedestrian1
new VPoint(0, 0) // pedestrian2
);
}
private void createTwoTargetsAndTwoPedestrians(VPoint target1Position, VPoint target2Position, VPoint pedestrian1Position, VPoint pedestrian2Position) {
List<Target> targets = createTwoTargets(target1Position, target2Position);
......@@ -74,6 +85,9 @@ public class OSMBehaviorControllerTest {
List<PedestrianOSM> pedestrians = createTwoPedestrianOSM(pedestrian1Position, pedestrian2Position, topography);
pedestrian1 = pedestrians.get(0);
pedestrian2 = pedestrians.get(1);
topography.addElement(pedestrian1);
topography.addElement(pedestrian2);
}
private List<Target> createTwoTargets(VPoint target1Position, VPoint target2Position) {
......@@ -93,7 +107,9 @@ public class OSMBehaviorControllerTest {
}
private List<PedestrianOSM> createTwoPedestrianOSM(VPoint pedestrian1Position, VPoint pedestrian2Position, Topography topography) {
AttributesAgent attributesAgent = new AttributesAgent();
// Create helper objects which are required by a PedestrianOSM:
AttributesAgent attributesAgent1 = new AttributesAgent(1);
AttributesAgent attributesAgent2 = new AttributesAgent(2);
AttributesOSM attributesOSM = new AttributesOSM();
int seed = 1;
......@@ -103,22 +119,29 @@ public class OSMBehaviorControllerTest {
topography,
new AttributesAgent(),
attributesOSM.getTargetPotentialModel());
// Force that target potential gets calculated so that the gradient can be used later on.
double simTimeInSec = 1;
potentialFieldTargetGrid.preLoop(simTimeInSec);
PedestrianOSM pedestrian1 = new PedestrianOSM(new AttributesOSM(), attributesAgent, topography, new Random(seed),
potentialFieldTargetGrid, null, null, null, null);
PedestrianOSM pedestrian2 = new PedestrianOSM(new AttributesOSM(), attributesAgent, topography, new Random(seed),
potentialFieldTargetGrid, null, null, null, null);
List<SpeedAdjuster> noSpeedAdjusters = new ArrayList<>();
// Create the actual PedestrianOSM
PedestrianOSM pedestrian1 = new PedestrianOSM(new AttributesOSM(), attributesAgent1, topography, new Random(seed),
potentialFieldTargetGrid, null, null, noSpeedAdjusters, null);
PedestrianOSM pedestrian2 = new PedestrianOSM(new AttributesOSM(), attributesAgent2, topography, new Random(seed),
potentialFieldTargetGrid, null, null, noSpeedAdjusters, null);
pedestrian1.setPosition(pedestrian1Position);
LinkedList<Integer> targetsPedestrian1 = new LinkedList<>();
targetsPedestrian1.add(topography.getTarget(1).getId());
pedestrian1.setTargets(targetsPedestrian1);
pedestrian1.setFreeFlowSpeed(1.1);
pedestrian2.setPosition(new VPoint(pedestrian2Position));
LinkedList<Integer> targetsPedestrian2 = new LinkedList<>();
targetsPedestrian2.add(topography.getTarget(2).getId());
pedestrian2.setTargets(targetsPedestrian2);
pedestrian2.setFreeFlowSpeed(1.2);
List<PedestrianOSM> pedestrians = new ArrayList<>();
pedestrians.add(pedestrian1);
......@@ -165,7 +188,7 @@ public class OSMBehaviorControllerTest {
@Test
public void calculateAngleBetweenTargetsReturnsPiIfPedestriansWalkInOppositeDirections() {
createOppositeDirectionTopography();
createOppositeDirectionVariation1Topography();
OSMBehaviorController controllerUnderTest = new OSMBehaviorController();
......@@ -352,4 +375,210 @@ public class OSMBehaviorControllerTest {
assertEquals(expectedAngle, actualAngle, ALLOWED_DOUBLE_TOLERANCE);
}
@Test
public void calculateAngleBetweenTargetGradientsReturnsZeroIfTargetsAreInSameDirection() {
createSameDirectionTopography();
OSMBehaviorController controllerUnderTest = new OSMBehaviorController();
// TODO: Clarify with BZ where this unexpected deviation comes from.
// This error sums up for larger angles and is problematic.
double expectedAngle = 0.043;
double actualAngle = controllerUnderTest.calculateAngleBetweenTargetGradients(pedestrian1, pedestrian2);
assertEquals(expectedAngle, actualAngle, ALLOWED_DOUBLE_TOLERANCE);
}
@Test
public void swapPedestriansSetsNewPositionForBothPedestrians() {
createSameDirectionTopography();
VPoint oldPositionPed1 = pedestrian1.getPosition().clone();
VPoint oldPositionPed2 = pedestrian2.getPosition().clone();
assertNotEquals(oldPositionPed1, oldPositionPed2);
OSMBehaviorController controllerUnderTest = new OSMBehaviorController();
double simTimeInSec = 1;
pedestrian1.setTimeOfNextStep(simTimeInSec);
pedestrian2.setTimeOfNextStep(simTimeInSec);
controllerUnderTest.swapPedestrians(pedestrian1, pedestrian2, topography);
assertEquals(oldPositionPed2, pedestrian1.getPosition());
assertEquals(oldPositionPed1, pedestrian2.getPosition());
}
@Test
public void swapPedestriansUsesStartTimeOfFirstPedestrianAndMaximumOfBothStepDurations() {
createSameDirectionTopography();
double timeOfNextStepPed1 = 1;
double timeOfNextStepPed2 = 1.5;
pedestrian1.setTimeOfNextStep(timeOfNextStepPed1);
pedestrian2.setTimeOfNextStep(timeOfNextStepPed2);
double maxStepDuration = Math.max(pedestrian1.getDurationNextStep(), pedestrian2.getDurationNextStep());
OSMBehaviorController controllerUnderTest = new OSMBehaviorController();
controllerUnderTest.swapPedestrians(pedestrian1, pedestrian2, topography);
double expectedTimeOfNextStep = timeOfNextStepPed1 + maxStepDuration;
assertEquals(expectedTimeOfNextStep, pedestrian1.getTimeOfNextStep(), ALLOWED_DOUBLE_TOLERANCE);
assertEquals(expectedTimeOfNextStep, pedestrian2.getTimeOfNextStep(), ALLOWED_DOUBLE_TOLERANCE);
}
@Test
public void swapPedestriansHandlesOSMTimeCreditParametersProperly() {
createSameDirectionTopography();
double timeOfNextStepPed1 = 1;
double timeOfNextStepPed2 = 1.5;
pedestrian1.setTimeOfNextStep(timeOfNextStepPed1);
pedestrian2.setTimeOfNextStep(timeOfNextStepPed2);
double timeCreditInSecPed1 = 1;
double timeCreditInSecPed2 = 1.5;
pedestrian1.setTimeCredit(timeCreditInSecPed1);
pedestrian2.setTimeCredit(timeCreditInSecPed2);
double maxStepDuration = Math.max(pedestrian1.getDurationNextStep(), pedestrian2.getDurationNextStep());
double stepDurationPed1 = pedestrian1.getDurationNextStep();
OSMBehaviorController controllerUnderTest = new OSMBehaviorController();
controllerUnderTest.swapPedestrians(pedestrian1, pedestrian2, topography);
// For event-driven update scheme "timeOfNextStep" must be set properly.
double expectedTimeOfNextStep = timeOfNextStepPed1 + maxStepDuration;
assertEquals(expectedTimeOfNextStep, pedestrian1.getTimeOfNextStep(), ALLOWED_DOUBLE_TOLERANCE);
assertEquals(expectedTimeOfNextStep, pedestrian2.getTimeOfNextStep(), ALLOWED_DOUBLE_TOLERANCE);
// For sequential update scheme "timeCredit" must be set properly.
double expectedTimeCredit = timeCreditInSecPed1 - (stepDurationPed1 + maxStepDuration);
assertEquals(expectedTimeCredit, pedestrian1.getTimeCredit(), ALLOWED_DOUBLE_TOLERANCE);
assertEquals(expectedTimeCredit, pedestrian2.getTimeCredit(), ALLOWED_DOUBLE_TOLERANCE);
}
@Test
public void findSwapCandidateReturnsNullIfGivenPedestrianHasNotTarget() {
createOppositeDirectionVariation2Topography();
LinkedList<Integer> noTargets = new LinkedList<>();
pedestrian1.setTargets(noTargets);
OSMBehaviorController controllerUnderTest = new OSMBehaviorController();
PedestrianOSM swapCandidate = controllerUnderTest.findSwapCandidate(pedestrian1, topography);
assertNull(swapCandidate);
}
@Test
public void findSwapCandidateReturnsNullIfNoPedestrianWithinSearchRadius() {
createOppositeDirectionVariation2Topography();
double searchRadius = 0.5;
pedestrian1.getAttributes().setSearchRadius(searchRadius);
OSMBehaviorController controllerUnderTest = new OSMBehaviorController();
PedestrianOSM swapCandidate = controllerUnderTest.findSwapCandidate(pedestrian1, topography);
assertNull(swapCandidate);
}
@Test
public void findSwapCandidateReturnsCandidateIfCandidateHasNotTarget() {
createOppositeDirectionVariation2Topography();
LinkedList<Integer> noTargets = new LinkedList<>();
pedestrian2.setTargets(noTargets);
double searchRadius = 1.5;
pedestrian1.getAttributes().setSearchRadius(searchRadius);
OSMBehaviorController controllerUnderTest = new OSMBehaviorController();
PedestrianOSM swapCandidate = controllerUnderTest.findSwapCandidate(pedestrian1, topography);
assertEquals(pedestrian2.getId(), swapCandidate.getId());
assertEquals(pedestrian2, swapCandidate);
}
@Test
public void findSwapCandidateReturnsNullIfCandidateIsNotCooperative() {
createOppositeDirectionVariation2Topography();
pedestrian2.setSelfCategory(SelfCategory.TARGET_ORIENTED);
double searchRadius = 1.5;
pedestrian1.getAttributes().setSearchRadius(searchRadius);
OSMBehaviorController controllerUnderTest = new OSMBehaviorController();
PedestrianOSM swapCandidate = controllerUnderTest.findSwapCandidate(pedestrian1, topography);
assertNull(swapCandidate);
}
@Test
public void findSwapCandidateReturnsNullIfGivenPedestrianAndCandidateShareSameWalkingDirection() {
createOppositeDirectionVariation2Topography();
pedestrian2.setSelfCategory(SelfCategory.COOPERATIVE);
pedestrian2.setTargets(pedestrian1.getTargets());
double searchRadius = 1.5;
pedestrian1.getAttributes().setSearchRadius(searchRadius);
OSMBehaviorController controllerUnderTest = new OSMBehaviorController();
PedestrianOSM swapCandidate = controllerUnderTest.findSwapCandidate(pedestrian1, topography);
assertNull(swapCandidate);
}
@Test
public void findSwapCandidateReturnsCandidateIfCandidateIsCooperativeAndHasDifferentWalkingDirection() {
createOppositeDirectionVariation2Topography();
pedestrian2.setSelfCategory(SelfCategory.COOPERATIVE);
double searchRadius = 1.5;
pedestrian1.getAttributes().setSearchRadius(searchRadius);
OSMBehaviorController controllerUnderTest = new OSMBehaviorController();
PedestrianOSM swapCandidate = controllerUnderTest.findSwapCandidate(pedestrian1, topography);
assertEquals(pedestrian2.getId(), swapCandidate.getId());
assertEquals(pedestrian2, swapCandidate);
}
@Test
public void waitSetsTimeOfNextStepForEventDrivenUpdateScheme() {
createSameDirectionTopography();
double currentSimTimeInSec = 1.0;
double timeOfNextStep = 0.5;
pedestrian1.setTimeOfNextStep(timeOfNextStep);
assertEquals(timeOfNextStep, pedestrian1.getTimeOfNextStep(), ALLOWED_DOUBLE_TOLERANCE);
OSMBehaviorController controllerUnderTest = new OSMBehaviorController();
controllerUnderTest.wait(pedestrian1, currentSimTimeInSec);
double expectedTimeOfNextStep = currentSimTimeInSec + timeOfNextStep;
assertEquals(expectedTimeOfNextStep, pedestrian1.getTimeOfNextStep(), ALLOWED_DOUBLE_TOLERANCE);
}
@Test
public void waitSetsTimeCreditToZeroForSequentialUpdateScheme() {
createSameDirectionTopography();
double timeCredit = 1.0;
pedestrian1.setTimeCredit(timeCredit);
assertEquals(timeCredit, pedestrian1.getTimeCredit(), ALLOWED_DOUBLE_TOLERANCE);
double currentSimTimeInSec = 1.0;
OSMBehaviorController controllerUnderTest = new OSMBehaviorController();
controllerUnderTest.wait(pedestrian1, currentSimTimeInSec);
double expectedTimeCredit = 0;
assertEquals(expectedTimeCredit, pedestrian1.getTimeCredit(), ALLOWED_DOUBLE_TOLERANCE);
}
}
\ No newline at end of file
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