/** * Handles each person of the given <code>population</code> with the specified <code>algorithm</code>, * using up to <code>numberOfThreads</code> threads to speed things up. Use this method only if the given * algorithm is thread-safe! Otherwise, use {@link #run(Population, int, PersonAlgorithmProvider)}. * * @param population * @param numberOfThreads * @param algorithm */ public static void run(final Population population, final int numberOfThreads, final PersonAlgorithm algorithm) { run(population, numberOfThreads, new PersonAlgorithmProvider() { @Override public PersonAlgorithm getPersonAlgorithm() { return algorithm; } }); }
ParallelPersonAlgorithmUtils.run(population, globalConfigGroup.getNumberOfThreads(), new ParallelPersonAlgorithmUtils.PersonAlgorithmProvider() { @Override
/** * Tests that the specified number of threads is allocated. * * @author mrieser */ @Test public void testNumberOfThreads() { Population population = ScenarioUtils.createScenario(ConfigUtils.createConfig()).getPopulation(); PersonAlgorithmTester algo = new PersonAlgorithmTester(); PersonAlgoProviderTester tester = new PersonAlgoProviderTester(algo); ParallelPersonAlgorithmUtils.run(population, 2, tester); Assert.assertEquals(2, tester.counter); PersonAlgoProviderTester tester2 = new PersonAlgoProviderTester(algo); ParallelPersonAlgorithmUtils.run(population, 4, tester2); Assert.assertEquals(4, tester2.counter); }
/** * Tests that all persons in the population are handled when using the threads. * * @author mrieser */ @Test public void testNofPersons() { MutableScenario scenario = (MutableScenario) ScenarioUtils.createScenario(ConfigUtils.createConfig()); Population population = scenario.getPopulation(); for (int i = 0; i < 100; i++) { Person person = PopulationUtils.getFactory().createPerson(Id.create(i, Person.class)); population.addPerson(person); } final PersonAlgorithmTester tester = new PersonAlgorithmTester(); ParallelPersonAlgorithmUtils.run(population, 2, tester); Assert.assertEquals(100, tester.personIds.size()); // test that all 100 different persons got handled, and not 1 person 100 times int sum = 0; int sumRef = 0; // build the sum of the personId's for (int i = 0, n = population.getPersons().size(); i < n; i++) { sumRef += i; sum += Integer.parseInt(population.getPersons().get(tester.personIds.get(i)).getId().toString()); } Assert.assertEquals(sumRef, sum); }
ParallelPersonAlgorithmUtils.run(population, globalConfigGroup.getNumberOfThreads(), new ParallelPersonAlgorithmUtils.PersonAlgorithmProvider() { @Override
@Test public void testCrashingAlgorithm() { try { MutableScenario scenario = (MutableScenario) ScenarioUtils.createScenario(ConfigUtils.createConfig()); Population population = scenario.getPopulation(); for (int i = 0; i < 10; i++) { Person person = PopulationUtils.getFactory().createPerson(Id.create(i, Person.class)); population.addPerson(person); } ParallelPersonAlgorithmUtils.run(population, 2, new AbstractPersonAlgorithm() { @Override public void run(Person person) { person.getPlans().get(0).setScore(null); // this will result in an IndexOutOfBoundsException } }); Assert.fail("Expected Exception, got none."); } catch (RuntimeException e) { Log.info("Catched expected exception.", e); } }
@Override public void notifyIterationStarts(IterationStartsEvent event) { PBox pBox = (PBox) operators ; final MatsimServices controler = event.getServices(); if(event.getIteration() == controler.getConfig().controler().getFirstIteration()){ log.info("This is the first iteration. All lines were added by notifyStartup event."); } else { pBox.notifyIterationStarts(event); removePreviousPTransitScheduleFromOriginalOne(event.getServices().getScenario().getTransitSchedule()); addPTransitScheduleToOriginalOne(event.getServices().getScenario().getTransitSchedule(), pBox.getpTransitSchedule()); removePreviousPVehiclesFromScenario(event.getServices().getScenario().getTransitVehicles()); addPVehiclesToOriginalOnes(event.getServices().getScenario().getTransitVehicles(), this.pVehiclesFactory.createVehicles(pBox.getpTransitSchedule())); event.getServices().getEvents().processEvent(new TransitScheduleChangedEvent(0.0)); if(this.agentsStuckHandler != null){ ParallelPersonAlgorithmUtils.run(controler.getScenario().getPopulation(), controler.getConfig().global().getNumberOfThreads(), new ParallelPersonAlgorithmUtils.PersonAlgorithmProvider() { @Override public AbstractPersonAlgorithm getPersonAlgorithm() { return stuckFactory.getReRouteStuck(new PlanRouter( controler.getTripRouterProvider().get(), controler.getScenario().getActivityFacilities() ), ((MutableScenario)controler.getScenario()), agentsStuckHandler.getAgentsStuck()); } }); } } this.dumpTransitScheduleAndVehicles(event.getServices(), event.getIteration()); }