Skip to content

Commit

Permalink
improve randomness in pseudo scorer
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Dec 1, 2024
1 parent 0db7c42 commit f488192
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,15 @@ public IndividualPersonScoringParameters(Scenario scenario) {
this.globalAvgIncome = computeAvgIncome(scenario.getPopulation());
this.categories = Category.fromConfigParams(this.scoring.getScoringParameters());
this.cache = new IdMap<>(Person.class, scenario.getPopulation().getPersons().size());
this.rnd = ThreadLocal.withInitial(() -> new Context(scenario.getConfig().global().getRandomSeed()));

// Create uncorrelated seed from the global seed
SplittableRandom rnd = new SplittableRandom(scenario.getConfig().global().getRandomSeed());
for (int i = 0; i < rnd.nextInt(); i++) {
rnd.nextLong();
}

byte[] seed = Longs.toByteArray(rnd.nextLong());
this.rnd = ThreadLocal.withInitial(() -> new Context(seed));
}

static DistanceGroup[] calcDistanceGroups(List<Integer> dists, DoubleList distUtils) {
Expand Down Expand Up @@ -338,11 +346,10 @@ private void addDeltaParams(Context ctx, DistanceGroupModeUtilityParameters.Delt
*/
private record Context(NormalDistribution normal, TruncatedNormalDistribution tn, byte[] seed, RestorableUniformRandomProvider rnd) {

Context(long seed) {
Context(byte[] seed) {
this(NormalDistribution.of(0, 1),
TruncatedNormalDistribution.of(0, 1, 0, Double.POSITIVE_INFINITY),
// Feed seed into random number generator
Longs.toByteArray(new SplittableRandom(seed).nextLong()),
seed,
RandomSource.KISS.create());
}

Expand All @@ -361,6 +368,11 @@ void setSeed(Person p) {
System.arraycopy(person, 0, state, 8, Math.min(person.length, 12));

rnd.restoreState(new RandomProviderDefaultState(state));

// Warm up the generator
for (int i = 0; i < 100; i++) {
rnd.nextLong();
}
}
}
}
11 changes: 10 additions & 1 deletion src/main/java/org/matsim/run/scoring/PseudoRandomScorer.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;

import java.util.SplittableRandom;

/**
* Computes pseudo random errors (epsilons), which are frozen for certain choice situations, thus representing the unobserved heterogeneity.
*/
Expand All @@ -29,9 +31,16 @@ public final class PseudoRandomScorer {
@Inject
public PseudoRandomScorer(PseudoRandomTripError tripScore, Config config) {
this.tripScore = tripScore;
this.seed = config.global().getRandomSeed();
this.scale = ConfigUtils.addOrGetModule(config, AdvancedScoringConfigGroup.class).pseudoRamdomScale;
this.distribution = ConfigUtils.addOrGetModule(config, AdvancedScoringConfigGroup.class).pseudoRandomDistribution;

SplittableRandom rnd = new SplittableRandom(config.global().getRandomSeed());
for (int i = 0; i < WARMUP_ITERATIONS; i++) {
rnd.nextDouble();
}

// Create a random seed from the global one
this.seed = rnd.nextLong();
}

/**
Expand Down

0 comments on commit f488192

Please sign in to comment.