Commit 0fb09887 authored by Jakob Schöttl's avatar Jakob Schöttl
Browse files

Fix bug and change attribute type

parent 5cad3b08
......@@ -4,7 +4,6 @@ import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import org.apache.commons.math3.distribution.EnumeratedDistribution;
import org.apache.commons.math3.distribution.EnumeratedIntegerDistribution;
import org.apache.commons.math3.distribution.RealDistribution;
import org.apache.commons.math3.random.JDKRandomGenerator;
import org.apache.commons.math3.random.RandomGenerator;
......@@ -21,6 +20,7 @@ import org.vadere.state.attributes.models.AttributesSeating;
import org.vadere.state.attributes.models.seating.SeatFacingDirection;
import org.vadere.state.attributes.models.seating.SeatRelativePosition;
import org.vadere.state.attributes.models.seating.SeatSide;
import org.vadere.state.attributes.models.seating.model.SeatPosition;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.scenario.Agent;
import org.vadere.state.scenario.Pedestrian;
......@@ -41,8 +41,6 @@ import org.vadere.util.reflection.DynamicClassInstantiator;
*/
public class SeatingModel implements ActiveCallback, Model {
private static final int[] SEAT_INDEXES = {0, 1, 2, 3};
private final Logger log = Logger.getLogger(SeatingModel.class);
private AttributesSeating attributes;
......@@ -245,9 +243,9 @@ public class SeatingModel implements ActiveCallback, Model {
}
private Seat chooseSeat0(SeatGroup seatGroup) {
final double[] probabilities = attributes.getSeatChoice0();
final EnumeratedIntegerDistribution distribution = new EnumeratedIntegerDistribution(rng, SEAT_INDEXES, probabilities);
return seatGroup.getSeat(distribution.sample());
final EnumeratedDistribution<SeatPosition> distribution =
new EnumeratedDistribution<>(rng, attributes.getSeatChoice0());
return seatGroup.getSeatByPosition(distribution.sample());
}
private Seat chooseSeat1(final SeatGroup seatGroup) {
......
......@@ -2,6 +2,7 @@ package org.vadere.simulator.models.seating.trainmodel;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
......@@ -10,6 +11,7 @@ import java.util.stream.Stream;
import org.vadere.state.attributes.models.seating.SeatFacingDirection;
import org.vadere.state.attributes.models.seating.SeatRelativePosition;
import org.vadere.state.attributes.models.seating.SeatSide;
import org.vadere.state.attributes.models.seating.model.SeatPosition;
import org.vadere.state.scenario.Target;
public class SeatGroup {
......@@ -220,6 +222,26 @@ public class SeatGroup {
return getPersonCount() == SEATS_PER_SEAT_GROUP;
}
public Seat getSeatByPosition(SeatPosition seatPosition) {
return seats.get(getSeatIndexByPosition(seatPosition));
}
private int getSeatIndexByPosition(SeatPosition seatPosition) {
final Map<SeatPosition, Integer> seatPositionMapping = new HashMap<>();
if (isAtLeftSide()) {
seatPositionMapping.put(SeatPosition.WINDOW_BACKWARD, 0);
seatPositionMapping.put(SeatPosition.AISLE_BACKWARD, 1);
seatPositionMapping.put(SeatPosition.WINDOW_FORWARD, 2);
seatPositionMapping.put(SeatPosition.AISLE_FORWARD, 3);
} else {
seatPositionMapping.put(SeatPosition.AISLE_BACKWARD, 0);
seatPositionMapping.put(SeatPosition.WINDOW_BACKWARD, 1);
seatPositionMapping.put(SeatPosition.AISLE_FORWARD, 2);
seatPositionMapping.put(SeatPosition.WINDOW_FORWARD, 3);
}
return seatPositionMapping.get(seatPosition);
}
public boolean isAtLeftSide() {
return index % 2 == 0;
}
......
......@@ -14,6 +14,7 @@ import org.vadere.state.attributes.models.AttributesSeating;
import org.vadere.state.attributes.models.seating.SeatFacingDirection;
import org.vadere.state.attributes.models.seating.SeatRelativePosition;
import org.vadere.state.attributes.models.seating.SeatSide;
import org.vadere.state.attributes.models.seating.model.SeatPosition;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.util.data.TallySheet;
......@@ -46,14 +47,21 @@ public class TestChooseSeat {
final int nTrials = 1000;
TallySheet<Seat> tallySheet = runChooseSeat(nTrials);
final double[] probabilities = FractionProbabilityNormalization.normalize(new AttributesSeating().getSeatChoice0());
final Map<SeatPosition, Double> probabilities = FractionProbabilityNormalization.normalize(new AttributesSeating().getSeatChoice0());
for (int i = 0; i < tallySheet.getKeys().size(); i++) {
Seat s = seatGroup.getSeat(i);
assertEquals(probabilities[i], (double) tallySheet.getCount(s) / nTrials, 0.05);
assertEquals(probabilities.get(getSeatPosition(s)), (double) tallySheet.getCount(s) / nTrials, 0.05);
}
}
private SeatPosition getSeatPosition(Seat seat) {
for (SeatPosition pos : SeatPosition.values())
if (seatGroup.getSeatByPosition(pos) == seat)
return pos;
throw new IllegalArgumentException("cannot get position for seat");
}
@StatisticalTestCase
@Test
public void testChooseSeat1() {
......
......@@ -10,6 +10,7 @@ import org.vadere.state.attributes.models.seating.SeatFacingDirection;
import org.vadere.state.attributes.models.seating.SeatRelativePosition;
import org.vadere.state.attributes.models.seating.SeatSide;
import org.vadere.state.attributes.models.seating.ValueWithProbabilityFraction;
import org.vadere.state.attributes.models.seating.model.SeatPosition;
import org.vadere.state.scenario.Et423Geometry;
/**
......@@ -27,8 +28,7 @@ public class AttributesSeating extends Attributes {
*/
private List<ValueWithProbabilityFraction<Boolean>> seatGroupChoice;
/** Probabilities for seat indexes 0 to 3. */
private double[] seatChoice0;
private List<ValueWithProbabilityFraction<SeatPosition>> seatChoice0;
private List<ValueWithProbabilityFraction<SeatRelativePosition>> seatChoice1;
......@@ -44,7 +44,11 @@ public class AttributesSeating extends Attributes {
addFraction(seatGroupChoice, true, 64.0);
addFraction(seatGroupChoice, false, 11.0);
seatChoice0 = new double[] { 2, 1, 14, 5 };
seatChoice0 = new ArrayList<>(4);
addFraction(seatChoice0, SeatPosition.WINDOW_BACKWARD, 2);
addFraction(seatChoice0, SeatPosition.AISLE_BACKWARD, 1);
addFraction(seatChoice0, SeatPosition.WINDOW_FORWARD, 14);
addFraction(seatChoice0, SeatPosition.AISLE_FORWARD, 5);
seatChoice1 = new ArrayList<>(3);
addFraction(seatChoice1, SeatRelativePosition.DIAGONAL, 31);
......@@ -69,8 +73,8 @@ public class AttributesSeating extends Attributes {
return toPairListForEnumeratedDistribution(seatGroupChoice);
}
public double[] getSeatChoice0() {
return seatChoice0; // TODO fix names
public List<Pair<SeatPosition, Double>> getSeatChoice0() {
return toPairListForEnumeratedDistribution(seatChoice0);
}
public List<Pair<SeatRelativePosition, Double>> getSeatChoice1() {
......
package org.vadere.state.attributes.models.seating.model;
public enum SeatPosition {
WINDOW_FORWARD, WINDOW_BACKWARD, AISLE_FORWARD, AISLE_BACKWARD;
}
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