Exemplo n.º 1
0
  /**
   * Used this routine for MeisterEtAl_Heureka_2008 paper, plot of number of deps, arrs by activity
   * type to visualize the time distribution from microcensus.
   */
  public static void analyseInitialTimes(Config config) {

    MutableScenario scenario = (MutableScenario) ScenarioUtils.createScenario(config);
    // initialize scenario with events from a given events file
    // - network
    logger.info("Reading network xml file...");
    new MatsimNetworkReader(scenario).readFile(scenario.getConfig().network().getInputFile());
    logger.info("Reading network xml file...done.");
    // - population
    PersonAlgorithm pa = new PersonAnalyseTimesByActivityType(TIME_BIN_SIZE);
    ArrayList<PersonAlgorithm> plansAlgos = new ArrayList<PersonAlgorithm>();
    plansAlgos.add(pa);

    PopulationImpl matsimAgentPopulation = (PopulationImpl) scenario.getPopulation();
    matsimAgentPopulation.setIsStreaming(true);
    PopulationReader plansReader = new MatsimPopulationReader(scenario);
    plansReader.readFile(scenario.getConfig().plans().getInputFile());
    matsimAgentPopulation.printPlansCount();
    int[][] numDeps = ((PersonAnalyseTimesByActivityType) pa).getNumDeps();
    MyRuns.writeAnArray(numDeps, "output/deptimes.txt");
    int[][] numArrs = ((PersonAnalyseTimesByActivityType) pa).getNumArrs();
    MyRuns.writeAnArray(numArrs, "output/arrtimes.txt");
    int[][] numTraveling = ((PersonAnalyseTimesByActivityType) pa).getNumTraveling();
    MyRuns.writeAnArray(numTraveling, "output/traveling.txt");
  }
Exemplo n.º 2
0
  public void analyzeModeChainFeasibility(Config config) {

    MutableScenario scenario =
        (MutableScenario) ScenarioUtils.createScenario(ConfigUtils.createConfig());

    // initialize scenario with events from a given events file
    // - network
    logger.info("Reading network xml file...");
    Network network = scenario.getNetwork();
    new MatsimNetworkReader(scenario).readFile(config.network().getInputFile());
    logger.info("Reading network xml file...done.");

    // - facilities
    logger.info("Reading facilities xml file...");
    ActivityFacilities facilities = scenario.getActivityFacilities();
    new MatsimFacilitiesReader(scenario).parse(config.facilities().getInputFile());
    logger.info("Reading facilities xml file...");

    // - population
    PersonAnalyzeModeChainFeasibility pa =
        new PersonAnalyzeModeChainFeasibility(facilities, network);
    ArrayList<PersonAlgorithm> plansAlgos = new ArrayList<PersonAlgorithm>();
    plansAlgos.add(pa);

    PopulationImpl matsimAgentPopulation = (PopulationImpl) scenario.getPopulation();
    matsimAgentPopulation.setIsStreaming(true);
    matsimAgentPopulation.addAlgorithm(pa);
    PopulationReader plansReader = new MatsimPopulationReader(scenario);
    plansReader.readFile(config.plans().getInputFile());

    logger.info("Number of selected plans which are infeasible: " + pa.getNumInfeasiblePlans());
  }
Exemplo n.º 3
0
  public static Population initMatsimAgentPopulation(
      final String inputFilename,
      final boolean isStreaming,
      final ArrayList<PersonAlgorithm> algos,
      MutableScenario scenario) {

    PopulationImpl population = (PopulationImpl) scenario.getPopulation();

    System.out.println("  reading plans xml file... ");
    population.setIsStreaming(isStreaming);

    if (isStreaming) {
      // add plans algos for streaming
      if (algos != null) {
        for (PersonAlgorithm algo : algos) {
          population.addAlgorithm(algo);
        }
      }
    }
    PopulationReader plansReader = new MatsimPopulationReader(scenario);
    plansReader.readFile(inputFilename);
    population.printPlansCount();
    System.out.println("  done.");

    return population;
  }
Exemplo n.º 4
0
  /*
   * Reads the population from the plans file.
   *
   * Note: use the other method with the same name, if this poses problems.
   */
  @Deprecated // use centralized infrastructure
  public static Scenario readScenario(String plansFile, String networkFile) {
    ScenarioImpl scenario = (ScenarioImpl) ScenarioUtils.createScenario(ConfigUtils.createConfig());

    new MatsimNetworkReader(scenario).readFile(networkFile);

    PopulationReader popReader = new MatsimPopulationReader(scenario);
    popReader.readFile(plansFile);

    return scenario;
  }
Exemplo n.º 5
0
  private void init() {

    String pathsFile = "./input/trb/valid/paths.txt";
    this.readInputFile(pathsFile);

    log.info("reading the facilities ...");
    new FacilitiesReaderMatsimV1(this.scenario).readFile(facilitiesfilePath);

    log.info("reading the network ...");
    new MatsimNetworkReader(this.scenario.getNetwork()).readFile(networkfilePath);

    log.info("  reading file " + plansfilePath);
    final PopulationReader plansReader = new MatsimPopulationReader(this.scenario);
    plansReader.readFile(plansfilePath);
  }
Exemplo n.º 6
0
  public static void main(String[] args) {

    String networkFile = "./bb_cl.xml.gz";
    String plansFile = "./baseplan";
    int numberOfAdditionalCopies = 9;
    double radiusOfPerimeter = 1000.0;

    Gbl.startMeasurement();

    ScenarioImpl sc = (ScenarioImpl) ScenarioUtils.createScenario(ConfigUtils.createConfig());

    Network net = sc.getNetwork();
    new MatsimNetworkReader(sc).readFile(networkFile);

    Population inPop = sc.getPopulation();
    PopulationReader popReader = new MatsimPopulationReader(sc);
    popReader.readFile(plansFile + ".xml.gz");

    DuplicatePlans dp = new DuplicatePlans(net, inPop, "tmp.xml.gz", numberOfAdditionalCopies);
    dp.run(inPop);
    dp.writeEndPlans();

    System.out.println("Dublicating plans finished");
    Gbl.printElapsedTime();

    inPop =
        ((ScenarioImpl) ScenarioUtils.createScenario(ConfigUtils.createConfig())).getPopulation();
    popReader = new MatsimPopulationReader(new SharedNetScenario(sc, inPop));
    popReader.readFile("tmp.xml.gz");

    ShuffleCoords shuffleCoords =
        new ShuffleCoords(
            net,
            inPop,
            plansFile + "_" + (numberOfAdditionalCopies + 1) + "x.xml.gz",
            radiusOfPerimeter,
            TransformationFactory.getCoordinateTransformation(
                TransformationFactory.DHDN_GK4, TransformationFactory.DHDN_GK4));
    shuffleCoords.setChangeHomeActsOnlyOnceTrue("home");
    shuffleCoords.run(inPop);
    shuffleCoords.writeEndPlans();

    (new File("tmp.xml.gz")).deleteOnExit();

    Gbl.printElapsedTime();
  }
Exemplo n.º 7
0
  public static void main(String[] args) {
    // TODO Auto-generated method stub

    MutableScenario scenario1 =
        (MutableScenario) ScenarioUtils.createScenario(ConfigUtils.createConfig());
    PopulationReader populationReader1 = new MatsimPopulationReader(scenario1);
    MatsimNetworkReader networkReader1 = new MatsimNetworkReader(scenario1);
    networkReader1.readFile(args[0]);
    populationReader1.readFile(args[1]);

    MutableScenario scenario2 =
        (MutableScenario) ScenarioUtils.createScenario(ConfigUtils.createConfig());
    PopulationReader populationReader2 = new MatsimPopulationReader(scenario2);
    MatsimNetworkReader networkReader2 = new MatsimNetworkReader(scenario2);
    networkReader2.readFile(args[0]);
    populationReader2.readFile(args[2]);

    int car = 0;
    int pt = 0;
    int walk = 0;
    int bike = 0;
    for (Person p : scenario2.getPopulation().getPersons().values()) {

      for (PlanElement pe :
          scenario1
              .getPopulation()
              .getPersons()
              .get(p.getId())
              .getSelectedPlan()
              .getPlanElements()) {

        if (pe instanceof Leg) {

          if (((Leg) pe).getMode().equals("car")) car++;
          else if (((Leg) pe).getMode().equals("pt")) pt++;
          else if (((Leg) pe).getMode().equals("walk")) walk++;
          else if (((Leg) pe).getMode().equals("bike")) bike++;
        }
      }
    }

    System.out.println(car + " " + pt + " " + bike + " " + walk);
  }
Exemplo n.º 8
0
  public void run(String plansFilePath, String networkFilePath) {
    MutableScenario scenario =
        (MutableScenario) ScenarioUtils.createScenario(ConfigUtils.createConfig());
    PopulationReader populationReader = new MatsimPopulationReader(scenario);
    MatsimNetworkReader networkReader = new MatsimNetworkReader(scenario.getNetwork());
    networkReader.readFile(networkFilePath);
    //	new FacilitiesReaderMatsimV1(scenario).readFile(facilitiesfilePath);
    populationReader.readFile(plansFilePath);

    int size = scenario.getPopulation().getPersons().values().size();
    Object[] arr = scenario.getPopulation().getPersons().values().toArray();

    for (int i = 1; i < size; i++) {
      if (i % 4 != 0) {
        scenario.getPopulation().getPersons().remove(((Person) arr[i]).getId());
      }
    }

    new PopulationWriter(scenario.getPopulation(), scenario.getNetwork())
        .writeFileV4("./plans_25perc.xml.gz");
  }
Exemplo n.º 9
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.º 10
0
  private void init(
      final String plansfilePath,
      final String networkfilePath,
      final String facilitiesfilePath,
      final String worldfilePath) {

    ScenarioImpl scenario = (ScenarioImpl) ScenarioUtils.createScenario(ConfigUtils.createConfig());

    this.network = scenario.getNetwork();
    new MatsimNetworkReader(scenario.getNetwork()).readFile(networkfilePath);
    log.info("network reading done");

    // this.facilities=new Facilities();
    this.facilities = scenario.getActivityFacilities();
    new FacilitiesReaderMatsimV1(scenario).readFile(facilitiesfilePath);
    log.info("facilities reading done");

    this.plans = scenario.getPopulation();
    final PopulationReader plansReader = new MatsimPopulationReader(scenario);
    plansReader.readFile(plansfilePath);
    log.info("plans reading done");
    log.info(this.plans.getPersons().size() + " persons");
  }
Exemplo n.º 11
0
  public void run(String plansFilePath, String networkFilePath) throws IOException {
    populationReader.readFile(plansFilePath);
    networkReader.readFile(networkFilePath);
    double distanceCar = 0.0;
    int countC = 0;
    double distanceBike = 0.0;
    int countB = 0;
    double distanceWalk = 0.0;
    int countW = 0;
    double distancePt = 0.0;
    int countPt = 0;
    int count = 0;
    Population pop = scenario.getPopulation();
    for (Person p : pop.getPersons().values()) {
      Leg previousLeg = null;
      for (PlanElement pe : p.getSelectedPlan().getPlanElements()) {

        if (pe instanceof Leg) {
          previousLeg = (Leg) pe;
          /*if (previousActivity.getType() == "shopgrocery") {
          	if (previousLeg.getMode() == "car") {
          		distanceCar += previousLeg.getTravelTime();
          		countC++;
          		count++;
          	}
          	else if (previousLeg.getMode() == "bike") {
          		distanceBike += previousLeg.getTravelTime();
          		countB++;
          		count++;
          	}
          	else if (previousLeg.getMode() == "walk") {
          		distanceWalk += previousLeg.getTravelTime();
          		countW++;
          		count++;
          	}
          	else if (previousLeg.getMode() == "pt") {
          		distancePt += previousLeg.getTravelTime();
          		countPt++;
          		count++;
          	}
          }*/

        } else if (pe instanceof Activity) {

          // if (((Activity) pe).getType().equals( "shopgrocery" )) {
          if (previousLeg != null) {
            if (previousLeg.getMode().equals("car")) {
              distanceCar += previousLeg.getTravelTime();
              countC++;
              count++;
            } else if (previousLeg.getMode().equals("bike")) {
              distanceBike += previousLeg.getTravelTime();
              countB++;
              count++;
            } else if (previousLeg.getMode().equals("walk")) {
              distanceWalk += previousLeg.getTravelTime();
              countW++;
              count++;
            } else if (previousLeg.getMode().equals("pt")) {
              distancePt += previousLeg.getTravelTime();
              countPt++;
              count++;
            }
          }
        }
      }
    }
    System.out.println((distanceCar + distanceBike + distanceWalk + distancePt) / count);
    System.out.println(distanceCar / (double) countC);
    System.out.println(distanceBike / (double) countB);
    System.out.println(distanceWalk / (double) countW);
    System.out.println(distancePt / (double) countPt);
  }
Exemplo n.º 12
0
  public static void main(String[] args) throws IOException {

    final BufferedReader readLink = IOUtils.getBufferedReader(args[0]);
    MutableScenario scenario =
        (MutableScenario) ScenarioUtils.createScenario(ConfigUtils.createConfig());
    PopulationReader populationReader = new MatsimPopulationReader(scenario);
    MatsimNetworkReader networkReader = new MatsimNetworkReader(scenario.getNetwork());
    Config config = scenario.getConfig();
    final BufferedWriter outLink = IOUtils.getBufferedWriter(args[3]);

    scenario.getConfig().getModule("qsim").addParam("mainMode", "cs_fix_gas");

    int count = 0;
    networkReader.readFile(args[1]);
    populationReader.readFile(args[2]);

    Network network = scenario.getNetwork();
    String s = readLink.readLine();
    s = readLink.readLine();
    String previous = null;
    int counterrb = 0;
    int counterff = 0;
    double income = 0.0;
    boolean ff = false;
    boolean rb = false;
    while (s != null) {
      Leg lastLeg = null;
      String[] arr = s.split("\t");

      double startTime = Double.parseDouble(arr[2]);
      double endTime = Double.parseDouble(arr[3]);
      Id<Person> personId = Id.create(arr[0], Person.class);

      if (previous == null || !previous.equals(arr[0])) {
        counterrb = 0;
        counterff = 0;
      }

      if (arr[1].contains("c")) {
        rb = true;
        ff = false;
      } else {
        rb = false;
        ff = true;
      }

      if (rb) {

        Person p = scenario.getPopulation().getPersons().get(personId);
        boolean cs = false;
        double distance = 0.0;
        int c = 0;
        for (PlanElement pe : p.getSelectedPlan().getPlanElements()) {

          if (pe instanceof Activity) {
            if (c == counterrb) {
              if (((Activity) pe).getType().equals("cs_interaction")
                  && ((Activity) pe).getLinkId().toString().equals(arr[4])
                  && !cs) {
                cs = true;
              } else if (((Activity) pe).getType().equals("cs_interaction")
                  && ((Activity) pe).getLinkId().toString().equals(arr[4])
                  && cs
                  && lastLeg.getMode().equals("cs_fix_gas")) {

                cs = false;
                counterrb += 2;
                // write the distance here
                outLink.write(arr[0]);
                outLink.write(" ");
                outLink.write(arr[1]);
                outLink.write(" ");
                outLink.write(arr[2]);
                outLink.write(" ");
                outLink.write(arr[3]);
                outLink.write(" ");
                outLink.write(Double.toString(distance));
                outLink.newLine();
                break;
              }
            }

          } else if (pe instanceof Leg) {
            lastLeg = (Leg) pe;
            if (c < counterrb) {
              if (((Leg) pe).getMode().equals("cs_walk")) {
                c++;
              }
            } else if (cs) {

              if (((Leg) pe).getMode().equals("cs_fix_gas")) {

                Route r = ((Leg) pe).getRoute();
                GenericRouteImpl rg = (GenericRouteImpl) r;
                String grr = rg.getRouteDescription();

                String[] arr1 = grr.split("\\s");

                for (int i = 0; i < arr1.length; i++) {

                  distance += network.getLinks().get(Id.create(arr1[i], Link.class)).getLength();
                }
              }
            }
          }
        }
      }
      // else {
      /*
      	Person p = scenario.getPopulation().getPersons().get(personId);
      	double distance = 0.0;

      	for(PlanElement pe:p.getSelectedPlan().getPlanElements()) {
      		int c = 0;

      		if (pe instanceof Leg) {
      				lastLeg = (Leg) pe;
      				if (c < counterff) {
      					if (((Leg) pe).getMode().equals("cs_flex_gas")) {
      						c++;

      					}
      				}
      				else if (((Leg) pe).getMode().equals("cs_flex_gas")) {

      						Route r = ((Leg) pe).getRoute();
      						GenericRouteImpl rg = (GenericRouteImpl) r;
      						String grr = rg.getRouteDescription();
      						System.out.println(grr);
      						if (grr == null) {
      							System.out.println("bla");
      							counterff++;
      							break;
      						}
      						String[] arr1 = grr.split("\\s");

      						for (int i = 0; i < arr1.length; i++) {

      							distance += network.getLinks().get(Id.create(arr1[i])).getLength();
      						}

      						//write here the distance
      						outLink.write(arr[0]);
      						outLink.write(" ");
      						outLink.write(arr[1]);
      						outLink.write(" ");
      						outLink.write(arr[2]);
      						outLink.write(" ");
      						outLink.write(arr[3]);
      						outLink.write(" ");
      						outLink.write(Double.toString(distance));
      						outLink.newLine();
      						counterff++;
      						break;

      			}

      		}
      	}

      }*/

      previous = arr[0];
      s = readLink.readLine();
    }
    outLink.flush();
    outLink.close();
    System.out.println(income);
    System.out.println(count);
  }
  public void run(String[] args) {

    ScenarioImpl scenario = (ScenarioImpl) ScenarioUtils.createScenario(ConfigUtils.createConfig());
    PopulationReader populationReader = new MatsimPopulationReader(scenario);
    MatsimNetworkReader networkReader = new MatsimNetworkReader(scenario);
    networkReader.readFile(args[0]);
    populationReader.readFile(args[1]);

    // randomly choose a person and run the simple model

    Object[] personArray = scenario.getPopulation().getPersons().values().toArray();

    int numberOfPersons = personArray.length;

    Map<Id, Person> addedMembers = new TreeMap<Id, Person>();

    int i = 0;

    Map<Id, Person> men = new TreeMap<Id, Person>();

    Map<Id, Person> women = new TreeMap<Id, Person>();

    // split the agents into men and women groups

    for (Person p : scenario.getPopulation().getPersons().values()) {

      if (PersonUtils.getSex(p).equals("m")) men.put(p.getId(), p);
      else women.put(p.getId(), p);
    }

    Object[] mArray = men.values().toArray();

    Object[] fArray = women.values().toArray();

    while (i < numMembers) {

      double randomDouble = MatsimRandom.getRandom().nextDouble();

      if (randomDouble < 0.8) {

        randomDouble = MatsimRandom.getRandom().nextDouble();

        if (randomDouble < ageShares[0]) {

          Person addP = findPerson(0, addedMembers, mArray);
        } else if (randomDouble < ageShares[1]) {
          Person addP = findPerson(1, addedMembers, mArray);

        } else if (randomDouble < ageShares[2]) {
          Person addP = findPerson(1, addedMembers, mArray);

        } else if (randomDouble < ageShares[3]) {
          Person addP = findPerson(1, addedMembers, mArray);

        } else {
          Person addP = findPerson(1, addedMembers, mArray);
        }

      } else {

        randomDouble = MatsimRandom.getRandom().nextDouble();

        if (randomDouble < ageShares[0]) {

          Person addP = findPerson(0, addedMembers, fArray);
        } else if (randomDouble < ageShares[1]) {
          Person addP = findPerson(1, addedMembers, fArray);

        } else if (randomDouble < ageShares[2]) {
          Person addP = findPerson(1, addedMembers, fArray);

        } else if (randomDouble < ageShares[3]) {
          Person addP = findPerson(1, addedMembers, fArray);

        } else {
          Person addP = findPerson(1, addedMembers, fArray);
        }
      }

      i++;
    }

    for (Person p : addedMembers.values()) {

      PersonUtils.addTravelcard(scenario.getPopulation().getPersons().get(p.getId()), "ffProgram");
    }

    new PopulationWriter(scenario.getPopulation(), scenario.getNetwork())
        .writeFileV4("./plans_ff_members.xml.gz");
  }