private void doRandomChoice(double now) { // When do I want to stop the current activity? endTime = now + MatsimRandom.getRandom().nextInt(100);// 1% chance that endTime == now } }
/** Resets the random number generator with the given seed. * * @param seed The seed used to draw random numbers. */ public static void reset(final long seed) { lastUsedSeed = seed; internalCounter = 0; getRandom().setSeed(seed); // prepareRNG(random); } public static Random getRandom() {
static <E> E chooseRandomElement(Set<E> set) { int randomIndex = MatsimRandom.getRandom().nextInt(set.size()); return Iterators.get(set.iterator(), randomIndex); } }
private void resetRandomNumbers(long seed, int iteration) { MatsimRandom.reset(seed + iteration); MatsimRandom.getRandom().nextDouble(); // draw one because of strange // "not-randomness" is the first // draw... // Fixme [kn] this should really be ten thousand draws instead of just // one }
private void drawOutputSample() { this.outputSamplePlans = new ArrayList<Plan>(); for (Person pers : PopulationUtils.getSortedPersons(this.population).values()) { if (MatsimRandom.getRandom().nextDouble() <= this.outputSample) { this.outputSamplePlans.add(pers.getSelectedPlan()); } } }
private final void writePerson(final Person person) { try { if ((this.write_person_fraction < 1.0) && (MatsimRandom.getRandom().nextDouble() >= this.write_person_fraction)) { return; } this.handler.writePerson(person, this.writer); counter.incCounter(); } catch (IOException e) { throw new UncheckedIOException(e); } }
private void route(CarrierPlan carrierPlan) { for(ScheduledTour tour : carrierPlan.getScheduledTours()){ if(MatsimRandom.getRandom().nextDouble() < probability){ new TimeAndSpaceTourRouter(router, network, travelTime).route(tour); } } }
/** * Test that MatsimRandom returns different values. */ public void testRandomness() { final double value1 = MatsimRandom.getRandom().nextDouble(); final double value2 = MatsimRandom.getRandom().nextDouble(); final double value3 = MatsimRandom.getRandom().nextDouble(); assertTrue(Math.abs(value1 - value2) > EPSILON); assertTrue(Math.abs(value1 - value3) > EPSILON); assertTrue(Math.abs(value2 - value3) > EPSILON); }
public final void writePerson(final Person person) { try { if ((this.write_person_fraction < 1.0) && (MatsimRandom.getRandom().nextDouble() >= this.write_person_fraction)) { return; } this.handler.writePerson(person, matsimWriter.getWriter()); counter.incCounter(); } catch (IOException e) { throw new UncheckedIOException(e); } }
public void testHandleEmptyPlan() { ChooseRandomLegMode algo = new ChooseRandomLegMode(new String[] {TransportMode.car, TransportMode.pt, TransportMode.walk}, MatsimRandom.getRandom(), false); Plan plan = PopulationUtils.createPlan(PopulationUtils.getFactory().createPerson(Id.create(1, Person.class))); algo.run(plan); // no specific assert, but there should also be no NullPointerException or similar stuff that could theoretically happen }
@Test public void testHandleEmptyPlan() { ChooseRandomSingleLegMode algo = new ChooseRandomSingleLegMode(new String[] {TransportMode.car, TransportMode.pt, TransportMode.walk}, MatsimRandom.getRandom(),false); Plan plan = PopulationUtils.createPlan(PopulationUtils.getFactory().createPerson(Id.create(1, Person.class))); algo.run(plan); // no specific assert, but there should also be no NullPointerException or similar stuff that could theoretically happen }
@Test public void testHandleEmptyPlan() { String[] modes = new String[] {TransportMode.car, TransportMode.pt, TransportMode.walk}; ChooseRandomLegModeForSubtour algo = new ChooseRandomLegModeForSubtour( EmptyStageActivityTypes.INSTANCE , new MainModeIdentifierImpl() , new AllowTheseModesForEveryone(modes), modes, CHAIN_BASED_MODES, MatsimRandom.getRandom(), SubtourModeChoice.Behavior.fromSpecifiedModesToSpecifiedModes, probaForRandomSingleTripMode); Plan plan = PopulationUtils.createPlan(null); algo.run(plan); // no specific assert, but there should also be no NullPointerException or similar stuff that could theoretically happen }
public void testHandlePlanWithoutLeg() { ChooseRandomLegMode algo = new ChooseRandomLegMode(new String[] {TransportMode.car, TransportMode.pt, TransportMode.walk}, MatsimRandom.getRandom(), false); Plan plan = PopulationUtils.createPlan(PopulationUtils.getFactory().createPerson(Id.create(1, Person.class))); PopulationUtils.createAndAddActivityFromCoord(plan, "home", new Coord(0, 0)); algo.run(plan); // no specific assert, but there should also be no NullPointerException or similar stuff that could theoretically happen }
@Test public void testHandlePlanWithoutLeg() { ChooseRandomSingleLegMode algo = new ChooseRandomSingleLegMode(new String[] {TransportMode.car, TransportMode.pt, TransportMode.walk}, MatsimRandom.getRandom(),false); Plan plan = PopulationUtils.createPlan(PopulationUtils.getFactory().createPerson(Id.create(1, Person.class))); PopulationUtils.createAndAddActivityFromCoord(plan, "home", new Coord(0, 0)); algo.run(plan); // no specific assert, but there should also be no NullPointerException or similar stuff that could theoretically happen }
@Test public void testHandlePlanWithoutLeg() { String[] modes = new String[] {TransportMode.car, TransportMode.pt, TransportMode.walk}; ChooseRandomLegModeForSubtour algo = new ChooseRandomLegModeForSubtour( EmptyStageActivityTypes.INSTANCE , new MainModeIdentifierImpl() ,new AllowTheseModesForEveryone(modes), modes, CHAIN_BASED_MODES, MatsimRandom.getRandom(), SubtourModeChoice.Behavior.fromSpecifiedModesToSpecifiedModes, probaForRandomSingleTripMode); Plan plan = PopulationUtils.createPlan(null); PopulationUtils.createAndAddActivityFromCoord(plan, "home", new Coord((double) 0, (double) 0)); algo.run(plan); // no specific assert, but there should also be no NullPointerException or similar stuff that could theoretically happen }
public void testIgnoreCarAvailability_Never_noChoice() { ChooseRandomLegMode algo = new ChooseRandomLegMode(new String[] {TransportMode.car, TransportMode.pt}, MatsimRandom.getRandom(), false); algo.setIgnoreCarAvailability(false); Person person = PopulationUtils.getFactory().createPerson(Id.create(1, Person.class)); PersonUtils.setCarAvail(person, "never"); Plan plan = PopulationUtils.createPlan(person); PopulationUtils.createAndAddActivityFromCoord(plan, "home", new Coord((double) 0, (double) 0)); PopulationUtils.createAndAddLeg( plan, TransportMode.pt ); PopulationUtils.createAndAddActivityFromCoord(plan, "work", new Coord((double) 0, (double) 0)); algo.run(plan); assertEquals("unexpected leg mode in leg 1.", TransportMode.pt, ((Leg) plan.getPlanElements().get(1)).getMode()); }
@Test public void testIgnoreCarAvailability_Never_noChoice() { ChooseRandomSingleLegMode algo = new ChooseRandomSingleLegMode(new String[] {TransportMode.car, TransportMode.pt}, MatsimRandom.getRandom(),false); algo.setIgnoreCarAvailability(false); Person person = PopulationUtils.getFactory().createPerson(Id.create(1, Person.class)); PersonUtils.setCarAvail(person, "never"); Plan plan = PopulationUtils.createPlan(person); PopulationUtils.createAndAddActivityFromCoord(plan, "home", new Coord((double) 0, (double) 0)); PopulationUtils.createAndAddLeg( plan, TransportMode.pt ); PopulationUtils.createAndAddActivityFromCoord(plan, "work", new Coord((double) 0, (double) 0)); algo.run(plan); assertEquals("unexpected leg mode in leg 1.", TransportMode.pt, ((Leg) plan.getPlanElements().get(1)).getMode()); }
public void testRandomChoiceWithinListedModesOnlyWorks() { ChooseRandomLegMode algo = new ChooseRandomLegMode(new String[] {TransportMode.pt, TransportMode.walk}, MatsimRandom.getRandom(), true); Plan plan = PopulationUtils.createPlan(PopulationUtils.getFactory().createPerson(Id.create(1, Person.class))); PopulationUtils.createAndAddActivityFromCoord(plan, "home", new Coord(0, 0)); Leg leg = PopulationUtils.createAndAddLeg( plan, TransportMode.car ); PopulationUtils.createAndAddActivityFromCoord(plan, "work", new Coord((double) 0, (double) 0)); String previousMode = leg.getMode(); algo.run(plan); assertSame("leg mode must have changed.", previousMode, leg.getMode()); }
@Test public void testRandomChoiceWithListedModesOnlyAndDifferentFromMode() { ChooseRandomSingleLegMode algo = new ChooseRandomSingleLegMode(new String[]{TransportMode.pt, TransportMode.walk}, MatsimRandom.getRandom(), true); Plan plan = PopulationUtils.createPlan(PopulationUtils.getFactory().createPerson(Id.create(1, Person.class))); PopulationUtils.createAndAddActivityFromCoord(plan, "home", new Coord((double) 0, (double) 0)); Leg leg = PopulationUtils.createAndAddLeg(plan, TransportMode.car); PopulationUtils.createAndAddActivityFromCoord(plan, "work", new Coord((double) 0, (double) 0)); algo.run(plan); String mode = leg.getMode(); Assert.assertSame(TransportMode.car, mode); } @Test