Commit 2641d902 authored by Jakob Schöttl's avatar Jakob Schöttl
Browse files

Add test and refactor

parent 83c5b73b
......@@ -2,16 +2,20 @@ package org.vadere.simulator.models.seating;
import static org.junit.Assert.*;
import java.util.Map;
import java.util.Random;
import org.junit.Before;
import org.junit.Test;
import org.vadere.simulator.models.seating.trainmodel.Seat;
import org.vadere.simulator.models.seating.trainmodel.SeatGroup;
import org.vadere.simulator.models.seating.trainmodel.TrainModel;
import org.vadere.state.attributes.models.AttributesSeating;
import org.vadere.state.attributes.models.seating.SeatRelativePosition;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.util.data.TallySheet;
import org.vadere.util.test.FractionProbabilityNormalization;
import org.vadere.util.test.StatisticalTestCase;
public class TestChooseSeat {
......@@ -30,7 +34,7 @@ public class TestChooseSeat {
@Test(expected=IllegalStateException.class)
public void testChooseSeatInFullSeatGroup() {
fillSeatGroup(seatGroup, true, true, true, true);
fillSeatGroup(seatGroup, 0, 1, 2, 3);
model.chooseSeat(seatGroup);
}
......@@ -38,10 +42,7 @@ public class TestChooseSeat {
@Test
public void testChooseSeat0() {
final int nTrials = 1000;
TallySheet<Seat> tallySheet = new TallySheet<>();
for (int i = 0; i < nTrials; i++) {
tallySheet.addOneTo(model.chooseSeat(seatGroup));
}
TallySheet<Seat> tallySheet = runChooseSeat(nTrials);
final double[] fractions = new AttributesSeating().getSeatChoice0();
double sum = 0;
......@@ -55,17 +56,37 @@ public class TestChooseSeat {
}
}
@StatisticalTestCase
@Test
public void testChooseSeat1() {
final int nTrials = 1000;
fillSeatGroup(seatGroup, 0);
TallySheet<Seat> tallySheet = runChooseSeat(nTrials);
final Seat diagonallyOppositeSeat = seatGroup.getSeat(3);
Map<SeatRelativePosition, Double> map = FractionProbabilityNormalization.normalize(new AttributesSeating().getSeatChoice1());
assertEquals(map.get(SeatRelativePosition.DIAGONAL),
(double) tallySheet.getCount(diagonallyOppositeSeat) / nTrials, 0.05);
}
private TallySheet<Seat> runChooseSeat(final int nTrials) {
TallySheet<Seat> tallySheet = new TallySheet<>();
for (int i = 0; i < nTrials; i++) {
tallySheet.addOneTo(model.chooseSeat(seatGroup));
}
return tallySheet;
}
private void clearSeatGroup(SeatGroup sg) {
for (int i = 0; i < 4; i++) {
sg.getSeat(i).setSittingPerson(null);
}
}
private void fillSeatGroup(SeatGroup seatGroup, boolean... seats) {
for (int i = 0; i < seats.length; i++) {
if (seats[i]) {
seatGroup.getSeat(i).setSittingPerson(new Pedestrian(new AttributesAgent(), new Random()));
}
private void fillSeatGroup(SeatGroup seatGroup, int... seatsWithPersons) {
for (int i : seatsWithPersons) {
seatGroup.getSeat(i).setSittingPerson(new Pedestrian(new AttributesAgent(), new Random()));
}
}
}
......@@ -44,7 +44,12 @@ public class AttributesSeating extends Attributes {
seatGroupChoice.add(new ValueWithProbabilityFraction<>(true, 64.0));
seatGroupChoice.add(new ValueWithProbabilityFraction<>(false, 11.0));
seatChoice0 = new double[] { 2, 1, 14, 5};
seatChoice0 = new double[] { 2, 1, 14, 5 };
seatChoice1 = new ArrayList<>(3);
seatChoice1.add(new ValueWithProbabilityFraction<>(SeatRelativePosition.DIAGONAL, 31));
seatChoice1.add(new ValueWithProbabilityFraction<>(SeatRelativePosition.ACROSS, 8));
seatChoice1.add(new ValueWithProbabilityFraction<>(SeatRelativePosition.NEXT, 3));
}
......
......@@ -25,5 +25,5 @@ public class ValueWithProbabilityFraction<T> {
public Pair<T, Double> toPair() {
return new Pair<>(value, fraction);
}
}
package org.vadere.util.test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.math3.util.Pair;
/**
* Helper class to normalize fractions to probabilities so that the sum of all
* probabilities is 1.
*/
public class FractionProbabilityNormalization {
public static <T> Map<T, Double> normalize(List<Pair<T, Double>> listWithFractions) {
double sum = 0;
for (Pair<T, Double> o : listWithFractions) {
sum += o.getValue();
}
Map<T, Double> map = new HashMap<>();
for (Pair<T, Double> o : listWithFractions) {
map.put(o.getKey(), o.getValue() / sum);
}
return map;
}
}
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