Esempio n. 1
0
 public static void main(String[] args) {
   Scenario scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig());
   String basedir = "C:/Users/Joschka/Documents/shared-svn/projects/vw_rufbus/scenario/input/";
   new MatsimNetworkReader(scenario.getNetwork()).readFile(basedir + "network.xml");
   for (Link link : scenario.getNetwork().getLinks().values()) {
     double speed = link.getFreespeed();
     if (speed < 10) link.setFreespeed(0.75 * speed);
     else if (speed < 20) link.setFreespeed(0.7 * speed);
     else if (speed < 30) {
       if (link.getNumberOfLanes() < 2) link.setFreespeed(0.7 * speed);
       else link.setFreespeed(0.75 * speed);
     } else link.setFreespeed(0.7 * speed);
   }
   new NetworkWriter(scenario.getNetwork()).write(basedir + "networks.xml");
 }
  /*package*/ void addConnectorLinks() {

    QuadTree<Node> quadTree = getNodesQuadTree();
    NetworkFactory factory = network.getFactory();

    /*
     * Try to use the centroid of the polygon. If that does not lie within
     * the polygon, use an interior point.
     */
    for (Entry<Integer, SimpleFeature> entry : zonesMap.entrySet()) {
      int zoneId = entry.getKey();
      SimpleFeature zone = entry.getValue();

      if (SpecialZones.skipZone(zone)) continue;

      Geometry polygon = (Geometry) zone.getDefaultGeometry();
      Point point = polygon.getCentroid();
      if (!polygon.contains(point)) point = polygon.getInteriorPoint();

      /*
       * Convert coordinate from WGS84 to EPSG:28992 (Netherlands Projection)
       */
      Coord wgs84Coord = new Coord(point.getCoordinate().x, point.getCoordinate().y);
      Coord nodeCoord = ct.transform(wgs84Coord);

      Id<Node> nodeId = Id.create(String.valueOf(zoneId), Node.class);
      network.addNode(factory.createNode(nodeId, nodeCoord));

      Node networkConnectorNode = quadTree.getClosest(nodeCoord.getX(), nodeCoord.getY());
      Id<Node> networkConnectorNodeId = networkConnectorNode.getId();

      double length = CoordUtils.calcDistance(nodeCoord, networkConnectorNode.getCoord());

      Id<Link> linkFromId = Id.create(String.valueOf(zoneId) + "from", Link.class);
      Link fromLink = factory.createLink(linkFromId, nodeId, networkConnectorNodeId);
      fromLink.setLength(length);
      fromLink.setCapacity(this.connectorCapacity);
      fromLink.setFreespeed(this.connectorLinkFreeSpeed);
      network.addLink(fromLink);

      Id<Link> linkToId = Id.create(String.valueOf(zoneId) + "to", Link.class);
      Link toLink = factory.createLink(linkToId, networkConnectorNodeId, nodeId);
      toLink.setLength(length);
      toLink.setCapacity(this.connectorCapacity);
      toLink.setFreespeed(this.connectorLinkFreeSpeed);
      network.addLink(toLink);
    }
  }
  private void createAndAddLinks(
      Id<Link> linkId, Id<Link> backLinkId, Id<Node> fromNodeId, Id<Node> toNodeId, Network net) {

    NetworkFactory fac = net.getFactory();

    Link l = fac.createLink(linkId, net.getNodes().get(fromNodeId), net.getNodes().get(toNodeId));
    l.setCapacity(capacity);
    l.setFreespeed(fs);
    l.setLength(linkLength);
    net.addLink(l);

    l = fac.createLink(backLinkId, net.getNodes().get(toNodeId), net.getNodes().get(fromNodeId));
    l.setCapacity(capacity);
    l.setFreespeed(fs);
    l.setLength(linkLength);
    net.addLink(l);
  }
  private final void convertPseudoNetwork() {
    List<Id> nodeIds = new ArrayList<Id>(scenario.getNetwork().getNodes().keySet());
    for (Id id : nodeIds) {
      scenario.getNetwork().removeNode(id);
    }
    new CreatePseudoNetwork(scenario.getTransitSchedule(), scenario.getNetwork(), "")
        .createNetwork();

    for (Link l : scenario.getNetwork().getLinks().values()) {
      l.setCapacity(WagonSimConstants.DEFAULT_CAPACITY);
      l.setFreespeed(WagonSimConstants.DEFAULT_FREESPEED);
    }
  }
 private Link getNewLink(Node fromNode, Node toNode) {
   Link link =
       networkFactory.createLink(
           Id.create(prefix + linkIdCounter++, Link.class), fromNode, toNode);
   if (fromNode == toNode) {
     link.setLength(50);
   } else {
     link.setLength(CoordUtils.calcDistance(fromNode.getCoord(), toNode.getCoord()));
   }
   link.setFreespeed(80.0 / 3.6);
   link.setCapacity(10000);
   link.setNumberOfLanes(1);
   link.setAllowedModes(this.transitModes);
   return link;
 }
  private void assignProps(Collection<Link> links, Link link) {
    double capacity = 0;
    double freespeed = 0;
    double lanes = 0;
    for (Link origLink : links) {
      capacity += origLink.getCapacity();
      freespeed = Math.max(freespeed, origLink.getFreespeed());
      lanes += origLink.getNumberOfLanes();
    }

    link.setCapacity(capacity);
    link.setFreespeed(freespeed);
    link.setNumberOfLanes(lanes);
    link.setLength(
        NetworkUtils.getEuclideanDistance(
            link.getFromNode().getCoord(), link.getToNode().getCoord()));
  }
Esempio n. 7
0
  /**
   * Tests that a custom scoring function factory doesn't get overwritten in the initialization
   * process of the Controler.
   *
   * @author mrieser
   */
  @Test
  public void testSetScoringFunctionFactory() {
    final Config config = this.utils.loadConfig(null);
    config.controler().setLastIteration(0);

    ScenarioImpl scenario = (ScenarioImpl) ScenarioUtils.createScenario(config);
    // create a very simple network with one link only and an empty population
    Network network = scenario.getNetwork();
    Node node1 = network.getFactory().createNode(Id.create(1, Node.class), new Coord(0, 0));
    Node node2 = network.getFactory().createNode(Id.create(2, Node.class), new Coord(100, 0));
    network.addNode(node1);
    network.addNode(node2);
    Link link = network.getFactory().createLink(Id.create(1, Link.class), node1, node2);
    link.setLength(100);
    link.setFreespeed(1);
    link.setCapacity(3600.0);
    link.setNumberOfLanes(1);

    final Controler controler = new Controler(scenario);
    controler.getConfig().controler().setCreateGraphs(false);
    controler.getConfig().controler().setWriteEventsInterval(0);
    controler.setScoringFunctionFactory(new DummyScoringFunctionFactory());

    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();

    assertTrue(
        "Custom ScoringFunctionFactory was not set.",
        controler.getScoringFunctionFactory() instanceof DummyScoringFunctionFactory);
  }
 public static void changePropertiesGroupLinksCSVFile(Network network, String fileName)
     throws IOException {
   BufferedReader reader = new BufferedReader(new FileReader(new File(fileName)));
   reader.readLine();
   String line = reader.readLine();
   while (line != null) {
     String[] parts = line.split(CSV_SEPARATOR);
     double[] values = new double[parts.length];
     for (int i = 0; i < parts.length; i++) values[i] = Double.parseDouble(parts[i]);
     for (Link link : network.getLinks().values())
       if (isLinkOfType(link, values[0], values[1], values[2])) {
         link.setFreespeed(values[3]);
         link.setCapacity(values[4]);
         link.setNumberOfLanes(values[5]);
       }
     line = reader.readLine();
   }
   reader.close();
 }
  @Before
  public void setUp() throws Exception {

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

    this.population = scenario.getPopulation();
    Person person = PopulationUtils.getFactory().createPerson(DEFAULT_PERSON_ID);
    this.population.addPerson(person);
    Plan plan = PersonUtils.createAndAddPlan(person, true);
    PopulationUtils.createAndAddActivityFromCoord(plan, "act1", new Coord(100.0, 100.0));
    PopulationUtils.createAndAddLeg(plan, "undefined");
    PopulationUtils.createAndAddActivityFromCoord(plan, "act2", new Coord(200.0, 200.0));
    PopulationUtils.createAndAddLeg(plan, "undefined");
    PopulationUtils.createAndAddActivityFromCoord(plan, "act3", new Coord(200.0, 200.0));
    PopulationUtils.createAndAddLeg(plan, "undefined");
    PopulationUtils.createAndAddActivityFromCoord(plan, "act4", new Coord(200.0, 200.0));
    PopulationUtils.createAndAddLeg(plan, "undefined");
    PopulationUtils.createAndAddActivityFromCoord(plan, "act5", new Coord(200.0, 200.0));
    plan.setScore(12.);

    this.network = scenario.getNetwork();
    Node fromNode =
        this.network
            .getFactory()
            .createNode(Id.create("123456", Node.class), new Coord(100.0, 100.0));
    this.network.addNode(fromNode);
    Node toNode =
        this.network
            .getFactory()
            .createNode(Id.create("789012", Node.class), new Coord(200.0, 200.0));
    this.network.addNode(toNode);
    Link link = this.network.getFactory().createLink(DEFAULT_LINK_ID, fromNode, toNode);
    link.setLength(Math.sqrt(20000.0));
    link.setFreespeed(13.333);
    link.setCapacity(2000);
    link.setNumberOfLanes(1);
    this.network.addLink(link);
  }
  private void createLink(
      final Network network,
      final OsmWay way,
      final OsmNode fromNode,
      final OsmNode toNode,
      final double length) {
    double nofLanes;
    double laneCapacity;
    double freespeed;
    double freespeedFactor;
    boolean oneway;
    boolean onewayReverse = false;

    // load defaults
    String highway = way.tags.get(TAG_HIGHWAY);
    String railway = way.tags.get(TAG_RAILWAY);
    String ptway = way.tags.get(TAG_PT_WAYS);
    OsmHighwayDefaults defaults;
    if (highway != null) {
      defaults = this.highwayDefaults.get(highway);
      if (defaults == null) {
        this.unknownHighways.add(highway);
        return;
      }
    } else if (railway != null) {
      defaults = this.railwayDefaults.get(railway);
      if (defaults == null) {
        this.unknownRailways.add(railway);
        return;
      }
    } else if (ptway != null) {
      defaults = this.ptDefaults.get(ptway);
      if (defaults == null) {
        this.unknownPTs.add(ptway);
        return;
      }
    } else {
      this.unknownWays.add(way.tags.values().toString());
      return;
    }
    nofLanes = defaults.lanes;
    laneCapacity = defaults.laneCapacity;
    freespeed = defaults.freespeed;
    freespeedFactor = defaults.freespeedFactor;
    oneway = defaults.oneway;

    // check if there are tags that overwrite defaults
    // - check tag "junction"
    if ("roundabout".equals(way.tags.get(TAG_JUNCTION))) {
      // if "junction" is not set in tags, get() returns null and equals() evaluates to false
      oneway = true;
    }
    // - check tag "oneway"
    String onewayTag = way.tags.get(TAG_ONEWAY);
    if (onewayTag != null) {
      if ("yes".equals(onewayTag)) {
        oneway = true;
      } else if ("true".equals(onewayTag)) {
        oneway = true;
      } else if ("1".equals(onewayTag)) {
        oneway = true;
      } else if ("-1".equals(onewayTag)) {
        onewayReverse = true;
        oneway = false;
      } else if ("no".equals(onewayTag)) {
        oneway = false; // may be used to overwrite defaults
      }
    }
    // - check tag "oneway" with trunks, primary and secondary roads
    // 		(if they are marked as such, the default number of lanes should be two instead of one)
    if (highway != null) {
      if (highway.equalsIgnoreCase("trunk")
          || highway.equalsIgnoreCase("primary")
          || highway.equalsIgnoreCase("secondary")) {
        if (oneway && nofLanes == 1.0) {
          nofLanes = 2.0;
        }
      }
    }
    // - ckeck tag "maxspeed"
    String maxspeedTag = way.tags.get(TAG_MAXSPEED);
    if (maxspeedTag != null) {
      try {
        freespeed = Double.parseDouble(maxspeedTag) / 3.6; // convert km/h to m/s
      } catch (NumberFormatException e) {
        if (!this.unknownMaxspeedTags.contains(maxspeedTag)) {
          this.unknownMaxspeedTags.add(maxspeedTag);
          log.warn("Could not parse maxspeed tag:" + e.getMessage() + ". Ignoring it.");
        }
      }
    }
    // - check tag "lanes"
    String lanesTag = way.tags.get(TAG_LANES);
    if (lanesTag != null) {
      try {
        double tmp = Double.parseDouble(lanesTag);
        if (tmp > 0) {
          nofLanes = tmp;
        }
      } catch (Exception e) {
        if (!this.unknownLanesTags.contains(lanesTag)) {
          this.unknownLanesTags.add(lanesTag);
          log.warn("Could not parse lanes tag:" + e.getMessage() + ". Ignoring it.");
        }
      }
    }

    // define the links' capacity and freespeed
    double capacity = nofLanes * laneCapacity;
    if (this.scaleMaxSpeed) {
      freespeed = freespeed * freespeedFactor;
    }

    // define modes allowed on link(s)
    //	basic type:
    Set<String> modes = new HashSet<String>();
    if (highway != null) {
      modes.add("car");
    }
    if (railway != null) {
      modes.add(railway);
    }
    if (ptway != null) {
      modes.add(ptway);
    }
    if (modes.isEmpty()) {
      modes.add("unknownStreetType");
    }
    //	public transport:
    for (OsmRelation relation : this.relations.values()) {
      for (OsmParser.OsmRelationMember member : relation.members) {
        if ((member.type == OsmParser.OsmRelationMemberType.WAY) && (member.refId == way.id)) {
          String mode = relation.tags.get("name");
          // mark that it is a link used by any pt:
          if (mode == null) {
            break;
          } else {
            modes.add("pt");
          }
          if (mode.indexOf(":") > 0
              && mode.indexOf(" ") > 0
              && mode.indexOf(" ") < mode.indexOf(":")) {
            modes.add(mode.toLowerCase().substring(mode.indexOf(" "), mode.indexOf(":")).trim());
          }
          // modes.add(relation.tags.get("name"));
          break;
        }
      }
    }

    // only create link, if both nodes were found, node could be null, since nodes outside a layer
    // were dropped
    Id<Node> fromId = Id.create(fromNode.id, Node.class);
    Id<Node> toId = Id.create(toNode.id, Node.class);
    if (network.getNodes().get(fromId) != null && network.getNodes().get(toId) != null) {
      String origId = Long.toString(way.id);

      if (!onewayReverse) {
        Link l =
            network
                .getFactory()
                .createLink(
                    Id.create(this.id, Link.class),
                    network.getNodes().get(fromId),
                    network.getNodes().get(toId));
        l.setLength(length);
        l.setFreespeed(freespeed);
        l.setCapacity(capacity);
        l.setNumberOfLanes(nofLanes);
        l.setAllowedModes(modes);
        if (l instanceof Link) {
          final String id1 = origId;
          NetworkUtils.setOrigId(((Link) l), id1);
        }
        network.addLink(l);
        this.id++;
      }
      if (!oneway) {
        Link l =
            network
                .getFactory()
                .createLink(
                    Id.create(this.id, Link.class),
                    network.getNodes().get(toId),
                    network.getNodes().get(fromId));
        l.setLength(length);
        l.setFreespeed(freespeed);
        l.setCapacity(capacity);
        l.setNumberOfLanes(nofLanes);
        l.setAllowedModes(modes);
        if (l instanceof Link) {
          final String id1 = origId;
          NetworkUtils.setOrigId(((Link) l), id1);
        }
        network.addLink(l);
        this.id++;
      }
    }
  }
Esempio n. 11
0
  private static int createNetwork(Scenario sc) {
    Network net = sc.getNetwork();
    NetworkFactory fac = net.getFactory();

    double length = 30;
    double width = 20;

    int id = 0;
    Node n0 = fac.createNode(Id.createNodeId(id++), CoordUtils.createCoord(0, 0));
    Node n1 = fac.createNode(Id.createNodeId(id++), CoordUtils.createCoord(length, 0));
    net.addNode(n0);
    net.addNode(n1);

    List<Node> someNodes = new ArrayList<>();
    for (double y = -30; y <= 30; y += 10) {
      Node n = fac.createNode(Id.createNodeId(id++), CoordUtils.createCoord(length * 3, y));
      net.addNode(n);
      someNodes.add(n);
    }
    Node n2 = fac.createNode(Id.createNodeId(id++), CoordUtils.createCoord(length * 4, 0));
    Node n3 = fac.createNode(Id.createNodeId(id++), CoordUtils.createCoord(length * 5, 0));
    net.addNode(n2);
    net.addNode(n3);
    id = 0;
    {
      Link l0 = fac.createLink(Id.createLinkId(id++), n0, n1);
      net.addLink(l0);
      l0.setCapacity(width);
      l0.setLength(length);
      Link l0Rev = fac.createLink(Id.createLinkId(id++), n1, n0);
      net.addLink(l0Rev);
      l0Rev.setCapacity(width);
      l0Rev.setLength(length);
    }
    int cnt = 0;
    for (Node n : someNodes) {
      {
        Link l0 = fac.createLink(Id.createLinkId(id++), n1, n);
        net.addLink(l0);
        l0.setCapacity(width / someNodes.size());
        l0.setLength(length * 2); // ?check!!
        if (cnt++ % 2 != 0) {
          Link l0Rev = fac.createLink(Id.createLinkId(id++), n, n1);
          net.addLink(l0Rev);
          l0Rev.setCapacity(width / someNodes.size());
          l0Rev.setLength(length * 2);
        }
      }

      {
        Link l0 = fac.createLink(Id.createLinkId(id++), n, n2);
        net.addLink(l0);
        l0.setCapacity(width / someNodes.size());
        l0.setLength(length); // ?check!!
        Link l0Rev = fac.createLink(Id.createLinkId(id++), n2, n);
        net.addLink(l0Rev);
        l0Rev.setCapacity(width / someNodes.size());
        l0Rev.setLength(length);
      }
    }
    int ret;
    {
      ret = id++;
      Link l0 = fac.createLink(Id.createLinkId(ret), n2, n3);
      net.addLink(l0);
      l0.setCapacity(width);
      l0.setLength(length);
    }
    {
      Link l0 = fac.createLink(Id.createLinkId(id++), n3, n2);
      net.addLink(l0);
      l0.setCapacity(width);
      l0.setLength(length);
    }

    Set<String> modes = new HashSet<>();
    modes.add("walkct");
    for (Link l : net.getLinks().values()) {
      l.setAllowedModes(modes);
      l.setFreespeed(20);
    }
    return ret;
  }