private void createDemand() {

    Population pop = sc.getPopulation();
    PopulationFactory fact = pop.getFactory();

    for (int i = 1; i <= 400; i++) {

      Person p = fact.createPerson(Id.createPersonId(i));
      Plan plan = fact.createPlan();
      p.addPlan(plan);
      Leg leg = fact.createLeg(TransportMode.car);
      Activity home;
      Activity work;

      if (i % 2 == 0) { // o --d1
        home = fact.createActivityFromCoord("o1", lo.getCoord());
        home.setEndTime(7 * 3600 + i);
        work = fact.createActivityFromCoord("d1", ld1.getCoord());
      } else /*if(i%2==0)*/ { // o --d2
        home = fact.createActivityFromCoord("o1", lo.getCoord());
        home.setEndTime(7 * 3600 + i);
        work = fact.createActivityFromCoord("d2", ld2.getCoord());
      }
      plan.addActivity(home);
      plan.addLeg(leg);
      plan.addActivity(work);
      pop.addPerson(p);
    }
    new PopulationWriter(pop).write(outputDir + "/input/input_plans.xml.gz");
  }
예제 #2
0
  private static void fillPlan(
      final Plan plan,
      final String mode,
      final Random r,
      final PopulationFactory pf,
      Coord homeCoord) {
    Activity h1 = pf.createActivityFromCoord("h", homeCoord);
    h1.setEndTime(7.0 * 3600 + r.nextDouble() * 3600.0);
    plan.addActivity(h1);

    Leg leg1 = pf.createLeg(mode);
    plan.addLeg(leg1);

    Coord workCoord;
    if (r.nextDouble() < 0.5) {
      workCoord =
          new Coord(
              (double) (int) (loc1X - 450 + 900.0 * r.nextDouble()),
              (double) (int) (loc1Y - 450 + 900.0 * r.nextDouble()));
    } else {
      workCoord =
          new Coord(
              (double) (int) (loc2X - 450 + 900.0 * r.nextDouble()),
              (double) (int) (loc2Y - 450 + 900.0 * r.nextDouble()));
    }
    Activity w = pf.createActivityFromCoord("w", workCoord);
    w.setEndTime(17.0 * 3600 + r.nextDouble() * 3600.0);
    plan.addActivity(w);

    Leg leg2 = pf.createLeg(mode);
    plan.addLeg(leg2);

    if (r.nextDouble() < 0.5) {
      // add shop activity
      Coord shopCoord;
      if (r.nextDouble() < 0.5) {
        shopCoord =
            new Coord(
                (double) (int) (loc1X - 450 + 900.0 * r.nextDouble()),
                (double) (int) (loc1Y - 450 + 900.0 * r.nextDouble()));
      } else {
        shopCoord =
            new Coord(
                (double) (int) (loc2X - 450 + 900.0 * r.nextDouble()),
                (double) (int) (loc2Y - 450 + 900.0 * r.nextDouble()));
      }
      Activity s = pf.createActivityFromCoord("s", shopCoord);
      s.setEndTime(w.getEndTime() + r.nextDouble() * 3600.0);
      plan.addActivity(s);

      Leg leg3 = pf.createLeg(mode);
      plan.addLeg(leg3);
    }

    Activity h2 = pf.createActivityFromCoord("h", homeCoord);
    plan.addActivity(h2);
  }
  private static Person createPerson(Scenario scenario, Coord home, Coord work, int i) {
    Random r = MatsimRandom.getRandom();
    PopulationFactory f = scenario.getPopulation().getFactory();
    Person p = f.createPerson(Id.createPersonId(i));
    Plan plan = f.createPlan();
    p.addPlan(plan);
    Activity h1 = f.createActivityFromCoord("home", home);
    h1.setEndTime(6 * 3600 + r.nextInt(4 * 3600));
    Leg leg = f.createLeg("taxibus");
    Activity w = f.createActivityFromCoord("work", work);
    w.setEndTime(12 * 3600 + r.nextInt(6 * 3600));
    Leg leg2 = f.createLeg("taxibus");
    Activity h2 = f.createActivityFromCoord("home", home);
    plan.addActivity(h1);
    plan.addLeg(leg);
    plan.addActivity(w);
    plan.addLeg(leg2);
    plan.addActivity(h2);

    return p;
  }
예제 #4
0
  public static void main(String[] args) {

    /*
     * We enter coordinates in the WGS84 reference system, but we want them to appear in the population file
     * projected to UTM33N, because we also generated the network in UTM33N.
     */
    CoordinateTransformation ct =
        TransformationFactory.getCoordinateTransformation(
            TransformationFactory.WGS84, TransformationFactory.WGS84_UTM33N);

    /*
     * First, create a new Config and a new Scenario.
     */
    Config config = ConfigUtils.createConfig();
    Scenario sc = ScenarioUtils.createScenario(config);

    /*
     * Pick the Network and the Population out of the Scenario for convenience.
     */
    Network network = sc.getNetwork();
    Population population = sc.getPopulation();

    /*
     * Pick the PopulationFactory out of the Population for convenience.
     * It contains methods to create new Population items.
     */
    PopulationFactory populationFactory = population.getFactory();

    /*
     * Create a Person designated "1" and add it to the Population.
     */

    long key = 1;
    for (long i = 1; i <= 5000; i++) {
      key = i;
      Person person = populationFactory.createPerson(Id.createPersonId(key));
      population.addPerson(person);

      /*
       * Create a Plan for the Person
       */
      Plan plan = populationFactory.createPlan();

      /*
       * Create a "home" Activity for the Person. In order to have the Person end its day at the same location,
       * we keep the home coordinates for later use (see below).
       * Note that we use the CoordinateTransformation created above.
       */
      Coord homeCoordinates = new Coord(686661.13571, 4827510.51845);
      Activity activity1 = populationFactory.createActivityFromCoord("home", homeCoordinates);
      activity1.setEndTime(
          21600
              + i * 0.72); // leave at 6 o'clock, one vehicle entering after other in a short while
      // so that there is no peak at one second
      // activity1.setEndTime(21600);
      plan.addActivity(activity1); // add the Activity to the Plan

      /*
       * Create a Leg. A Leg initially hasn't got many attributes. It just says that a car will be used.
       */
      plan.addLeg(populationFactory.createLeg("car"));

      /*
       * Create a "work" Activity, at a different location.
       */
      Activity activity2 =
          populationFactory.createActivityFromCoord("work", new Coord(689426.65361, 4826700.65288));
      activity2.setEndTime(57600); // leave at 4 p.m.
      plan.addActivity(activity2);
      System.out.println("Last Departure Time: " + claculateTime(activity1.getEndTime()));

      /*
       * Create another car Leg.
       */
      person.addPlan(plan);
    }

    for (long i = 1; i <= 5000; i++) {
      key = i + 5000;
      Person person = populationFactory.createPerson(Id.createPersonId(key));
      population.addPerson(person);
      Plan plan = populationFactory.createPlan();
      Coord homeCoordinates = new Coord(686661.13571, 4826063.88649);
      Activity activity1 = populationFactory.createActivityFromCoord("home", homeCoordinates);
      activity1.setEndTime(21600 + i * 1); // leave at 6 o'clock
      // activity1.setEndTime(21600);
      plan.addActivity(activity1); // add the Activity to the Plan
      plan.addLeg(populationFactory.createLeg("car"));
      Activity activity2 =
          populationFactory.createActivityFromCoord("work", new Coord(689426.65361, 4826700.65288));
      activity2.setEndTime(57600); // leave at 4 p.m.
      plan.addActivity(activity2);
      System.out.println("Last Departure Time: " + claculateTime(activity1.getEndTime()));
      person.addPlan(plan);
    }

    /*
     * Write the population (of 1 Person) to a file.
     */
    MatsimWriter popWriter =
        new org.matsim.api.core.v01.population.PopulationWriter(population, network);
    popWriter.write("H:\\Mike Work\\input\\population.xml");
  }
예제 #5
0
  /**
   * Tests that plans with missing act locations are completed (=xy2links and routed) before the
   * mobsim starts.
   *
   * @author mrieser
   */
  @Test
  public void testCalcMissingActLinks() {
    Config config = this.utils.loadConfig(null);
    Fixture f = new Fixture(config);

    /* Create a person with two plans, driving from link 1 to link 3, starting at 7am.  */
    Population population = f.scenario.getPopulation();
    PopulationFactory factory = population.getFactory();
    Person person1 = null;
    Activity act1a = null;
    Activity act1b = null;
    Activity act2a = null;
    Activity act2b = null;
    Leg leg1 = null;
    Leg leg2 = null;

    person1 = PersonImpl.createPerson(Id.create(1, Person.class));
    // --- plan 1 ---
    Plan plan1 = factory.createPlan();
    person1.addPlan(plan1);
    double x1 = -50.0;
    act1a = factory.createActivityFromCoord("h", new Coord(x1, 10.0));
    act1a.setEndTime(7.0 * 3600);
    plan1.addActivity(act1a);
    leg1 = factory.createLeg(TransportMode.car);
    plan1.addLeg(leg1);
    // DO NOT CREATE A ROUTE FOR THE LEG!!!
    double y1 = -10.0;
    act1b = factory.createActivityFromCoord("h", new Coord(1075.0, y1));
    plan1.addActivity(act1b);
    // --- plan 2 ---
    Plan plan2 = factory.createPlan();
    person1.addPlan(plan2);
    double x = -50.0;
    double y = -10.0;
    act2a = factory.createActivityFromCoord("h", new Coord(x, y));
    act2a.setEndTime(7.9 * 3600);
    plan2.addActivity(act2a);
    leg2 = factory.createLeg(TransportMode.car);
    plan2.addLeg(leg2);
    // DO NOT CREATE A ROUTE FOR THE LEG!!!
    act2b = factory.createActivityFromCoord("h", new Coord(1111.1, 10.0));
    plan2.addActivity(act2b);
    population.addPerson(person1);

    // Complete the configuration for our test case
    // - set scoring parameters
    ActivityParams actParams = new ActivityParams("h");
    actParams.setTypicalDuration(8 * 3600);
    actParams.setPriority(1.0);
    config.planCalcScore().addActivityParams(actParams);
    // - define iterations
    config.controler().setLastIteration(0);
    // - make sure we don't use threads, as they are not deterministic
    config.global().setNumberOfThreads(1);

    // Now run the simulation
    Controler controler = new Controler(f.scenario);
    controler.getConfig().controler().setCreateGraphs(false);
    controler.getConfig().controler().setWriteEventsInterval(0);
    controler.addOverridingModule(
        new AbstractModule() {
          @Override
          public void install() {
            bindMobsim()
                .toProvider(
                    new Provider<Mobsim>() {
                      @Override
                      public Mobsim get() {
                        return new FakeMobsim();
                      }
                    });
          }
        });
    controler.setDumpDataAtEnd(false);
    controler.run();
    /* if something goes wrong, there will be an exception we don't catch and the test fails,
     * otherwise, everything is fine. */

    // check that BOTH plans have their act-locations calculated
    assertEquals(f.link1.getId(), act1a.getLinkId());
    assertEquals(f.link3.getId(), act1b.getLinkId());
    assertEquals(f.link1.getId(), act2a.getLinkId());
    assertEquals(f.link3.getId(), act2b.getLinkId());

    // check that BOTH plans have a route set, even when we only run 1 iteration where only one of
    // them is used.
    // assertNotNull(leg1.getRoute());
    // assertNotNull(leg2.getRoute());
    // but do not assume that the leg will be the same instance...
    for (Plan plan : new Plan[] {plan1, plan2}) {
      assertEquals(
          "unexpected plan length in " + plan.getPlanElements(), 3, plan.getPlanElements().size());
      assertNotNull(
          "null route in plan " + plan.getPlanElements(),
          ((Leg) plan.getPlanElements().get(1)).getRoute());
    }
  }
예제 #6
0
  private void processDiary() {
    LOG.info("Processing diary...");
    PopulationFactory pf = this.sc.getPopulation().getFactory();

    Map<String, Integer> chainCount = new TreeMap<>();

    int noTrips = 0;
    int noFirstTrip = 0;
    int noZoneInfo = 0;
    int zoneInfo = 0;

    Counter counter = new Counter("  person # ");
    for (Id<Person> pId : this.tripMap.keySet()) {
      List<String[]> list = tripMap.get(pId);
      Map<String, String[]> map = new TreeMap<>();
      for (String[] sa : list) {
        String tripNumber = sa[23];
        if (!tripNumber.equals("")) {
          String tripId = String.format("%02d", Integer.parseInt(tripNumber));
          map.put(tripId, sa);
        }
      }

      if (map.size() == 0) {
        noTrips++;
      } else {
        /* Try and do some magic with the diary. */
        Plan plan = pf.createPlan();

        /* Check the first activity. */
        String chain = "";
        String[] sa = map.get("01");
        if (sa == null) {
          /* There is no first trip numbered '01'. */
          LOG.warn(pId.toString() + ": " + map.keySet().toString());
          noFirstTrip++;
        } else {
          chain += "h";
          String homeZone = sa[7];
          Coord homeCoord = sampleCoord(homeZone, "h");

          Activity firstHome = pf.createActivityFromCoord("h", homeCoord);
          double homeEnd = 0.0;
          try {
            homeEnd = Time.parseTime(sa[25]);
          } catch (NumberFormatException e) {
            LOG.error(" TIME: ===> " + pId.toString() + ": " + sa[25]);
          }
          firstHome.setEndTime(homeEnd);
          plan.addActivity(firstHome);
        }

        /* Parse the chain. */
        Iterator<String> it = map.keySet().iterator();

        while (it.hasNext()) {
          String trip = it.next();
          String[] tripSa = map.get(trip);
          String tripPurpose = tripSa[33];
          String matsimTrip =
              DiaryEnums.TripPurpose.parseFromCode(
                      tripPurpose.equals("") || tripPurpose.equals(" ")
                          ? 0
                          : Integer.parseInt(tripPurpose))
                  .getMatsimActivityCode();
          chain += "-" + matsimTrip;

          /* Add the trip to plan. */
          String recordedMode = tripSa[34];
          if (recordedMode.contains(";")) {
            LOG.error("Multiple modes for " + pId.toString() + ": " + recordedMode);
            throw new RuntimeException();
          }
          String matsimMode =
              DiaryEnums.ModeOfTravel.parseFromCode(
                      tripSa[34].equals("") || tripSa[34].equalsIgnoreCase(" ")
                          ? 0
                          : Integer.parseInt(tripSa[34]))
                  .getMatsimMode();
          Leg leg = pf.createLeg(matsimMode);
          double tripStartTime = 0.0;
          double tripEndTime = 0.0;
          try {
            tripStartTime = Time.parseTime(tripSa[25]);
          } catch (NumberFormatException e) {
            LOG.error(" TIME: ===> " + pId.toString() + ": " + tripSa[25]);
          }
          try {
            tripEndTime = Time.parseTime(tripSa[27]);
          } catch (NumberFormatException e) {
            LOG.error(" TIME: ===> " + pId.toString() + ": " + tripSa[27]);
          }
          leg.setDepartureTime(tripStartTime);
          leg.setTravelTime(tripEndTime - tripStartTime);
          plan.addLeg(leg);

          /* Add the destination activity. */
          if (tripSa[32].equals("") || tripSa[32].equals(" ")) {
            if (!matsimTrip.equalsIgnoreCase("h")) {
              zoneInfo++;
            } else {
              noZoneInfo++;
            }
          } else {
            zoneInfo++;
          }
          Coord actCoord = sampleCoord(tripSa[32], matsimTrip);
          Activity act = pf.createActivityFromCoord(matsimTrip, actCoord);
          act.setStartTime(tripEndTime);
          plan.addActivity(act);
        }

        /* Check and add chain. */
        if (!chainCount.containsKey(chain)) {
          chainCount.put(chain, 1);
        } else {
          int oldCount = chainCount.get(chain);
          chainCount.put(chain, oldCount + 1);
        }

        /* Finally, associate the plan with the person. */
        sc.getPopulation().getPersons().get(pId).addPlan(plan);
      }
      counter.incCounter();
    }
    counter.printCounter();

    LOG.info("          Number of persons with no trips: " + noTrips);
    LOG.info("    Number of persons with no first trips: " + noFirstTrip);
    LOG.info(" Number of destinations without zone info: " + noZoneInfo);
    LOG.info("    Number of destinations with zone info: " + zoneInfo);

    /* Report the activity chain types. */
    SortedSet<Entry<String, Integer>> set = entriesSortedByValues(chainCount);
    Iterator<Entry<String, Integer>> iter = set.iterator();
    while (iter.hasNext()) {
      Entry<String, Integer> entry = iter.next();
      LOG.info("  " + entry.getKey() + " (" + entry.getValue() + ")");
    }

    LOG.info("Done processing diary.");
  }