Exemplo n.º 1
0
  /**
   * TODO: write test.
   *
   * @param persons
   * @param outputPlansFileName
   * @param network
   */
  public static void writePersons(
      Collection<? extends Person> persons, String outputPlansFileName, Network network) {
    PopulationWriter popWriter =
        new PopulationWriter(
            PopulationUtils.createPopulation(
                ((MutableScenario) null).getConfig(), ((MutableScenario) null).getNetwork()),
            network);
    popWriter.writeStartPlans(outputPlansFileName);

    for (Person person : persons) {
      popWriter.writePerson(person);
    }

    popWriter.writeEndPlans();
  }
Exemplo n.º 2
0
  public void createDilutedPlans(
      final Coord center, final double radius, final String fromFile, final String toFile) {
    final Map<Id<Link>, Link> areaOfInterest = new HashMap<>();

    Network network = this.scenario.getNetwork();

    log.info("extracting aoi:");
    log.info("  center: " + center.getX() + " / " + center.getY());
    log.info("  radius: " + radius);
    for (Link link : network.getLinks().values()) {
      final Node from = link.getFromNode();
      final Node to = link.getToNode();
      if ((CoordUtils.calcDistance(from.getCoord(), center) <= radius)
          || (CoordUtils.calcDistance(to.getCoord(), center) <= radius)) {
        System.out.println("    link " + link.getId().toString());
        areaOfInterest.put(link.getId(), link);
      }
    }
    log.info("  # links in aoi: " + areaOfInterest.size());

    log.info("creating diluted dpopulation:");
    log.info("  input-file:  " + fromFile);
    log.info("  output-file: " + toFile);
    PopulationImpl pop =
        (PopulationImpl)
            ((MutableScenario) ScenarioUtils.createScenario(ConfigUtils.createConfig()))
                .getPopulation();
    pop.setIsStreaming(true);

    PopulationWriter writer = new PopulationWriter(pop, this.scenario.getNetwork());
    writer.startStreaming(toFile);

    final PersonIntersectAreaFilter filter =
        new PersonIntersectAreaFilter(writer, areaOfInterest, network);
    filter.setAlternativeAOI(center, radius);
    pop.addAlgorithm(filter);

    new MatsimPopulationReader(new PseudoScenario(this.scenario, pop)).readFile(fromFile);

    writer.closeStreaming();

    pop.printPlansCount();
    log.info("persons in output: " + filter.getCount());
  }
Exemplo n.º 3
0
  public void createSamplePopulation(
      final String fromFile, final String toFile, final double percentage) {
    PopulationImpl pop =
        (PopulationImpl)
            ((MutableScenario) ScenarioUtils.createScenario(ConfigUtils.createConfig()))
                .getPopulation();
    pop.setIsStreaming(true);
    final PopulationWriter plansWriter =
        new PopulationWriter(pop, this.scenario.getNetwork(), percentage);
    plansWriter.startStreaming(toFile);
    pop.addAlgorithm(plansWriter);
    PopulationReader plansReader =
        new MatsimPopulationReader(new PseudoScenario(this.scenario, pop));

    log.info("extracting sample from population:");
    log.info("  input-file:  " + fromFile);
    log.info("  output-file: " + toFile);
    log.info("  sample-size: " + percentage);
    plansReader.readFile(fromFile);
    pop.printPlansCount();
    plansWriter.closeStreaming();
  }
Exemplo n.º 4
0
  /**
   * @param args
   * @throws FactoryException
   */
  public static void main(String[] args) throws FactoryException {
    String popFile = args[0];
    String facFile = args[1];
    String netFile = args[2];
    int n = Integer.parseInt(args[3]);
    String outDir = args[4];

    Logger logger = Logger.getLogger(DemoScenario.class);

    MathTransform transform = CRS.findMathTransform(CRSUtils.getCRS(31467), CRSUtils.getCRS(3857));

    Config config = ConfigUtils.createConfig();
    Scenario scenario = ScenarioUtils.createScenario(config);
    /*
     * remove foreign persons and extract subsample
     */
    logger.info("Loading persons...");
    MatsimPopulationReader pReader = new MatsimPopulationReader(scenario);
    pReader.readFile(popFile);
    logger.info("Done.");

    logger.info("Removing foreign persons...");
    Set<Id<Person>> remove = new HashSet<>();
    for (Id<Person> id : scenario.getPopulation().getPersons().keySet()) {
      if (id.toString().startsWith("foreign")) {
        remove.add(id);
      }
    }

    int cnt = 0;
    for (Id<Person> id : remove) {
      if (scenario.getPopulation().getPersons().remove(id) != null) {
        cnt++;
      }
    }
    logger.info(String.format("Done. Removed %s foreign persons.", cnt));

    logger.info("Drawing population subsample...");
    List<Person> persons = new ArrayList<>(scenario.getPopulation().getPersons().values());
    Collections.shuffle(persons);
    Population population = PopulationUtils.createPopulation(config);
    cnt = 0;
    for (int i = 0; i < n; i++) {
      population.addPerson(persons.get(i));
    }
    logger.info("Done.");

    logger.info("Bluring activity end times...");
    Random random = new XORShiftRandom();
    for (Person person : population.getPersons().values()) {
      for (Plan plan : person.getPlans()) {
        for (int i = 0; i < plan.getPlanElements().size(); i += 2) {
          Activity act = (Activity) plan.getPlanElements().get(i);
          double endTim = act.getEndTime() - 15 * 60 + (random.nextDouble() * 30 * 60);
          act.setEndTime(endTim);
          double startTim = act.getStartTime() - 15 * 60 + (random.nextDouble() * 30 * 60);
          act.setStartTime(startTim);
        }
      }
    }
    logger.info("Done.");

    logger.info("Writing population...");
    PopulationWriter pWriter = new PopulationWriter(population);
    pWriter.write(String.format("%s/plans.xml.gz", outDir));
    logger.info("Done.");
    /*
     * filter only used facilities
     */
    logger.info("Loading facilities...");
    MatsimFacilitiesReader fReader = new MatsimFacilitiesReader(scenario);
    fReader.readFile(facFile);
    logger.info("Done.");

    logger.info("Removing unsused facilities...");
    Set<Id<ActivityFacility>> unused =
        new HashSet<>(scenario.getActivityFacilities().getFacilities().keySet());
    for (Person person : population.getPersons().values()) {
      for (Plan plan : person.getPlans()) {
        for (int i = 0; i < plan.getPlanElements().size(); i += 2) {
          Activity act = (Activity) plan.getPlanElements().get(i);
          unused.remove(act.getFacilityId());
        }
      }
    }
    logger.info("Done.");

    logger.info("Transforming facility coordinates...");
    for (ActivityFacility fac : scenario.getActivityFacilities().getFacilities().values()) {
      double[] points = new double[] {fac.getCoord().getX(), fac.getCoord().getY()};
      try {
        transform.transform(points, 0, points, 0, 1);
      } catch (TransformException e) {
        e.printStackTrace();
      }

      ((ActivityFacilityImpl) fac).setCoord(new Coord(points[0], points[1]));
    }
    logger.info("Done.");

    logger.info("Writing facilities...");
    FacilitiesWriter fWrtier = new FacilitiesWriter(scenario.getActivityFacilities());
    fWrtier.write(String.format("%s/facilities.xml.gz", outDir));
    logger.info("Done.");
    /*
     * clean network from foreign links
     */
    logger.info("Loading network...");
    MatsimNetworkReader nReader = new MatsimNetworkReader(scenario);
    nReader.readFile(netFile);
    logger.info("Done.");

    logger.info("Removing foreign links...");
    Set<Id<Link>> linksRemove = new HashSet<>();
    for (Id<Link> id : scenario.getNetwork().getLinks().keySet()) {
      if (id.toString().contains(".l")) {
        linksRemove.add(id);
      }
    }

    for (Id<Link> id : linksRemove) {
      scenario.getNetwork().removeLink(id);
    }
    logger.info("Done.");

    logger.info("Removing foreign nodes...");
    Set<Id<Node>> nodesRemove = new HashSet<>();
    for (Id<Node> id : scenario.getNetwork().getNodes().keySet()) {
      if (id.toString().contains(".n")) {
        nodesRemove.add(id);
      }
    }

    for (Id<Node> id : nodesRemove) {
      scenario.getNetwork().removeNode(id);
    }
    logger.info("Done.");

    logger.info("Transforming node coordinates...");
    for (Node node : scenario.getNetwork().getNodes().values()) {
      double[] points = new double[] {node.getCoord().getX(), node.getCoord().getY()};
      try {
        transform.transform(points, 0, points, 0, 1);
      } catch (TransformException e) {
        e.printStackTrace();
      }

      ((NodeImpl) node).setCoord(new Coord(points[0], points[1]));
    }
    logger.info("Done.");

    logger.info("Writing network...");
    NetworkWriter nWriter = new NetworkWriter(scenario.getNetwork());
    nWriter.write(String.format("%s/network.xml.gz", outDir));
    logger.info("Done.");
  }