Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simei94 committed May 27, 2024
1 parent 83107f0 commit 4bea07f
Showing 1 changed file with 93 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,93 +99,102 @@ public void notifyIterationEnds(final IterationEndsEvent event) {
updateModesUsedPerPerson();


/*
* Looks through modesUsedPerPersonTrip at each person-trip. How many of those person trips have used each mode more than the
* predefined limits.
*/
int totalPersonTripCount = 0;
Map<Integer, Map<String, Double>> modeCountCurrentIteration = new TreeMap<>();
//Map<Limit, Map<Mode , TotalTripCount >>

for (Map<Integer, Map<String, Integer>> mapForPerson : modesUsedPerPersonTrip.values()) {
//Map<Trip # , Map<Mode , Count >>
for (Map<String, Integer> mapForPersonTrip : mapForPerson.values()) {
//Map<Mode , Count >
totalPersonTripCount++;
for (String mode : mapForPersonTrip.keySet()) {
Integer realCount = mapForPersonTrip.get(mode);
for (Integer limit : limits) {
Map<String, Double> modeCountMap = modeCountCurrentIteration.computeIfAbsent(limit, k -> new TreeMap<>());
Double modeCount = modeCountMap.computeIfAbsent(mode, k -> 0.);
if (realCount >= limit) {
modeCount++;
}
modeCountMap.put(mode, modeCount);
modeCountCurrentIteration.put(limit, modeCountMap);
}
}
}
}
// Calculates mcc share for each mode in current iteration, and updates modeCCHistory accordingly
for (Integer limit : limits) {
Map<String, Double> modeCnt = modeCountCurrentIteration.get(limit);
this.modes.addAll(modeCnt.keySet()); // potentially adds new modes to setthat just showed up in current iter
Map<String, Map<Integer, Double>> modeIterationShareMap = modeCCHistory.computeIfAbsent(limit, k -> new HashMap<>());
for (String mode : modes) {
Double cnt = modeCnt.get(mode);
double share = 0.;
if (cnt != null) {
share = cnt / totalPersonTripCount;
}

log.info("-- mode choice coverage (" + limit + "x) of mode " + mode + " = " + share);

Map<Integer, Double> iterationShareMap = modeIterationShareMap.get(mode);

// If this is the first iteration where the mode shows up, add zeros to all previous iterations in history
if (iterationShareMap == null) {
iterationShareMap = new TreeMap<>();
for (int iter = firstIteration; iter < event.getIteration(); iter++) {
iterationShareMap.put(iter, 0.0);
}
modeIterationShareMap.put(mode, iterationShareMap);
}

iterationShareMap.put(event.getIteration(), share);
}
}
// for testing purposes: if there are any trips, do analysis. If not, it is probably a test or a faulty / empty population. -sme0524
if (!modesUsedPerPersonTrip.isEmpty()) {
/*
* Looks through modesUsedPerPersonTrip at each person-trip. How many of those person trips have used each mode more than the
* predefined limits.
*/
int totalPersonTripCount = 0;
Map<Integer, Map<String, Double>> modeCountCurrentIteration = new TreeMap<>();
//Map<Limit, Map<Mode , TotalTripCount >>

for (Map<Integer, Map<String, Integer>> mapForPerson : modesUsedPerPersonTrip.values()) {
//Map<Trip # , Map<Mode , Count >>
for (Map<String, Integer> mapForPersonTrip : mapForPerson.values()) {
//Map<Mode , Count >
totalPersonTripCount++;
for (String mode : mapForPersonTrip.keySet()) {
Integer realCount = mapForPersonTrip.get(mode);
for (Integer limit : limits) {
Map<String, Double> modeCountMap = modeCountCurrentIteration.computeIfAbsent(limit, k -> new TreeMap<>());
Double modeCount = modeCountMap.computeIfAbsent(mode, k -> 0.);
if (realCount >= limit) {
modeCount++;
}
modeCountMap.put(mode, modeCount);
modeCountCurrentIteration.put(limit, modeCountMap);
}
}
}
}
// Calculates mcc share for each mode in current iteration, and updates modeCCHistory accordingly
for (Integer limit : limits) {
Map<String, Double> modeCnt = modeCountCurrentIteration.get(limit);
this.modes.addAll(modeCnt.keySet()); // potentially adds new modes to setthat just showed up in current iter
Map<String, Map<Integer, Double>> modeIterationShareMap = modeCCHistory.computeIfAbsent(limit, k -> new HashMap<>());
for (String mode : modes) {
Double cnt = modeCnt.get(mode);
double share = 0.;
if (cnt != null) {
share = cnt / totalPersonTripCount;
}

log.info("-- mode choice coverage (" + limit + "x) of mode " + mode + " = " + share);

Map<Integer, Double> iterationShareMap = modeIterationShareMap.get(mode);

// If this is the first iteration where the mode shows up, add zeros to all previous iterations in history
if (iterationShareMap == null) {
iterationShareMap = new TreeMap<>();
for (int iter = firstIteration; iter < event.getIteration(); iter++) {
iterationShareMap.put(iter, 0.0);
}
modeIterationShareMap.put(mode, iterationShareMap);
}

iterationShareMap.put(event.getIteration(), share);
}
}


// Print MCC Stats to output file
for (Integer limit : limits) {
Map<String, Map<Integer, Double>> modeIterationShareMap = modeCCHistory.get(limit);

BufferedWriter modeOut = IOUtils.getBufferedWriter(this.modeFileName + limit + "x.txt");
try {
modeOut.write("Iteration");
for (String mode : modes) {
modeOut.write("\t" + mode);
}
modeOut.write("\n");
for (int iter = firstIteration; iter <= event.getIteration(); iter++) {
modeOut.write(String.valueOf(iter));
for (String mode : modes) {
modeOut.write("\t" + modeIterationShareMap.get(mode).get(iter));
}
modeOut.write("\n");
}

modeOut.flush();
modeOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}

// Produce Graphs
if (this.createPNG && event.getIteration() > this.minIteration) {
produceGraphs();
}
} else {
log.warn("There are no trips conducted by the analyzed population. This should only be the case for tests. If you are running a simulation run, " +
" this should not happen. Check your population.");
}


// Print MCC Stats to output file
for (Integer limit : limits) {
Map<String, Map<Integer, Double>> modeIterationShareMap = modeCCHistory.get(limit);

BufferedWriter modeOut = IOUtils.getBufferedWriter(this.modeFileName + limit + "x.txt");
try {
modeOut.write("Iteration");
for (String mode : modes) {
modeOut.write("\t" + mode);
}
modeOut.write("\n");
for (int iter = firstIteration; iter <= event.getIteration(); iter++) {
modeOut.write(String.valueOf(iter));
for (String mode : modes) {
modeOut.write("\t" + modeIterationShareMap.get(mode).get(iter));
}
modeOut.write("\n");
}

modeOut.flush();
modeOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}

// Produce Graphs
if (this.createPNG && event.getIteration() > this.minIteration) {
produceGraphs();
}

}

Expand Down

0 comments on commit 4bea07f

Please sign in to comment.