/*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); }
/** @param args */ public static void main(String[] args) { Config config = ConfigUtils.createConfig(); config.controler().setLastIteration(0); config.controler().setOverwriteFileSetting(OverwriteFileSetting.deleteDirectoryIfExists); Collection<String> sf = new ArrayList<>(); sf.add("otfvis"); config.controler().setSnapshotFormat(sf); config.controler().setWriteSnapshotsInterval(1); config.qsim().setStartTime(0); config.qsim().setSimStarttimeInterpretation(StarttimeInterpretation.onlyUseStarttime); config.qsim().setEndTime(10. * 3600.); config.qsim().setSimEndtimeInterpretation(EndtimeInterpretation.onlyUseEndtime); config.qsim().setNumberOfThreads(1); config.qsim().setSnapshotPeriod(1); OTFVisConfigGroup otfconf = ConfigUtils.addOrGetModule(config, OTFVisConfigGroup.GROUP_NAME, OTFVisConfigGroup.class); otfconf.setColoringScheme(ColoringScheme.byId); Scenario scenario = ScenarioUtils.createScenario(config); Network net = scenario.getNetwork(); NetworkFactory nf = net.getFactory(); Node node1 = nf.createNode(Id.createNodeId(1), new Coord(0., 0.)); net.addNode(node1); Node node2 = nf.createNode(Id.createNodeId(2), new Coord(LEN * 7.5, 0.)); net.addNode(node2); Node node3 = nf.createNode(Id.createNodeId(3), new Coord(0., -LEN * 7.5)); net.addNode(node3); Node node4 = nf.createNode(Id.createNodeId(4), new Coord(0., LEN * 7.5)); net.addNode(node4); Link link = nf.createLink(Id.createLinkId("1-2"), node1, node2); link.setLength(LEN * 7.5); net.addLink(link); // Link link2 = nf.createLink(Id.createLinkId("1-3"), node1, node3) ; // link.setLength( LEN*7.5 ); // net.addLink( link2 ) ; Controler controler = new Controler(scenario); controler.addOverridingModule( new AbstractModule() { @Override public void install() { bind(QNetworkFactory.class).to(MyQNetworkFactory.class); } }); controler.addOverridingModule(new OTFVisLiveModule()); controler.addOverridingModule(new OTFVisFileWriterModule()); controler.run(); }
public void add() { List<Link> links = ((BusLaneAdderPanel) layersPanels.get(PanelIds.ONE)).getLinks(); Node prevNode = links.get(0).getFromNode(); for (Link link : links) if (!link.getAllowedModes().contains("bus")) { exitSave(); JOptionPane.showMessageDialog(this, "Wrong path, network saved"); } for (int i = 0; i < links.size(); i++) { Link link = links.get(i); Node node = null; if (link.getNumberOfLanes() == 1) { Set<String> modes = new HashSet<String>(); modes.add("bus"); link.setAllowedModes(modes); node = link.getToNode(); } else { Node oldNode = link.getToNode(); if (i == links.size() - 1 || oldNode.getInLinks().size() + oldNode.getOutLinks().size() > 2) node = oldNode; else { node = network .getFactory() .createNode( Id.createNodeId("fl" + oldNode.getId().toString()), oldNode.getCoord()); network.addNode(node); } LinkImpl newLink = (LinkImpl) network .getFactory() .createLink(Id.createLinkId("fl" + link.getId().toString()), prevNode, node); Set<String> modes = new HashSet<String>(); modes.add("car"); newLink.setAllowedModes(modes); newLink.setCapacity(link.getCapacity()); newLink.setFreespeed(link.getFreespeed()); newLink.setLength(link.getLength()); newLink.setNumberOfLanes(link.getNumberOfLanes() - 1); newLink.setOrigId(((LinkImpl) link).getOrigId()); newLink.setType(((LinkImpl) link).getType()); network.addLink(newLink); Set<String> modes2 = new HashSet<String>(); modes2.add("bus"); link.setAllowedModes(modes2); link.setCapacity(900); link.setNumberOfLanes(1); } prevNode = node; } ((BusLaneAdderPanel) layersPanels.get(PanelIds.ONE)).clearSelection(); }
public void addLinkNetwork(Node fromNode, Node toNode) { Id<Link> linkId = Id.create(network.getLinks().size() * 2, Link.class); network.addLink(network.getFactory().createLink(linkId, fromNode, toNode)); try { PrintWriter writer = new PrintWriter(new FileWriter(RoutesPathsGenerator.NEW_NETWORK_LINKS_FILE, true)); writer.println(linkId); writer.println(fromNode.getId()); writer.println(toNode.getId()); writer.close(); } catch (IOException e) { e.printStackTrace(); } }
private static Network filterNetwork(Network network) { CoordinateTransformation transform = TransformationFactory.getCoordinateTransformation( TransformationFactory.CH1903_LV03_GT, TransformationFactory.DHDN_GK4); // CoordinateTransformation transform = // TransformationFactory.getCoordinateTransformation(TransformationFactory.CH1903_LV03_GT, // "WGS84_UTM32T"); Network net = NetworkUtils.createNetwork(); RoadPricingSchemeImpl rps = new RoadPricingSchemeImpl(); RoadPricingReaderXMLv1 rpr = new RoadPricingReaderXMLv1(rps); rpr.parse(linksToFilter); Set<Id<Link>> linkList = rps.getTolledLinkIds(); for (Link link : network.getLinks().values()) { Id linkId = link.getId(); if (linkList.contains(linkId)) { Id fromId = link.getFromNode().getId(); Id toId = link.getToNode().getId(); Coord fromNodeCoord = link.getFromNode().getCoord(); Coord toNodeCoord = link.getToNode().getCoord(); Coord fromNodeTransformed = transform.transform(fromNodeCoord); Coord toNodeTransformed = transform.transform(toNodeCoord); // Node newFromNode = net.getFactory().createNode(fromId, fromNodeCoord); // Node newToNode = net.getFactory().createNode(toId, toNodeCoord); Node newFromNode = net.getFactory().createNode(fromId, fromNodeTransformed); Node newToNode = net.getFactory().createNode(toId, toNodeTransformed); if (!net.getNodes().containsKey(fromId)) { net.addNode(newFromNode); } if (!net.getNodes().containsKey(toId)) { net.addNode(newToNode); } Link ll = net.getFactory().createLink(link.getId(), newFromNode, newToNode); net.addLink(ll); } } return net; }
public static void main(String[] args) { roadTypeAndHBEFARoadType = new TreeMap<String, String[]>(); roadTypeAndHBEFARoadType.put("motorway", new String[] {"1", "RUR/MW/120"}); roadTypeAndHBEFARoadType.put("motorway_link", new String[] {"2", "URB/MW-Nat./80"}); roadTypeAndHBEFARoadType.put("trunk", new String[] {"3", "URB/MW-City/80"}); roadTypeAndHBEFARoadType.put("trunk_link", new String[] {"4", "URB/Trunk-City/50"}); roadTypeAndHBEFARoadType.put("primary", new String[] {"5", "URB/Trunk-Nat./80"}); roadTypeAndHBEFARoadType.put("primary_link", new String[] {"6", "RUR/Distr-sin./60"}); roadTypeAndHBEFARoadType.put("secondary", new String[] {"7", "URB/Distr/60"}); roadTypeAndHBEFARoadType.put("tertiary", new String[] {"8", "URB/Local/50"}); roadTypeAndHBEFARoadType.put("unclassified", new String[] {"9", "URB/Access/50"}); roadTypeAndHBEFARoadType.put("residential", new String[] {"10", "URB/Access/30"}); Scenario scenario = (MutableScenario) ScenarioUtils.createScenario(ConfigUtils.createConfig()); final Network network = scenario.getNetwork(); new MatsimNetworkReader(scenario.getNetwork()) .readFile("./input/baseCase/SiouxFalls_network_fromOSM.xml"); writer = IOUtils.getBufferedWriter("./input/emissionFiles/SiouxFalls_roadTypeMapping.txt"); for (Link link : network.getLinks().values()) { String linkIdentifier = link.getId().toString().split("___")[1]; if (roadTypeAndHBEFARoadType.containsKey(linkIdentifier)) { ((LinkImpl) link).setType(roadTypeAndHBEFARoadType.get(linkIdentifier)[0]); } else { throw new RuntimeException("Road Category " + linkIdentifier + " is not defined."); } link.setCapacity(link.getCapacity()); network.addLink( link); // This will give warning for duplicat of link in network which can be ignored as // same link is added with type of linkImpl. } new NetworkWriter(network).write("./input/baseCase/SiouxFalls_networkWithRoadType.xml.gz"); writeRoadTypeMappingFile(); }
/* (non-Javadoc) * @see org.matsim.core.api.internal.NetworkRunnable#run(org.matsim.api.core.v01.network.Network) */ @Override public void run(Network network) { Queue<Node> pendingNodes = new LinkedList<Node>(network.getNodes().values()); double[] env = NetworkUtils.getBoundingBox(pendingNodes); QuadTree<Node> quadTree = new QuadTree<Node>(env[0], env[1], env[2], env[3]); for (Node node : pendingNodes) { quadTree.put(node.getCoord().getX(), node.getCoord().getY(), node); } long linkIdCounter = 100000000000L; while (!pendingNodes.isEmpty()) { Node node = pendingNodes.poll(); double radius = 30; double minx = node.getCoord().getX() - radius; double miny = node.getCoord().getY() - radius; double maxx = node.getCoord().getX() + radius; double maxy = node.getCoord().getY() + radius; Set<Node> intersectionNodes = new HashSet<Node>(20); quadTree.getRectangle(minx, miny, maxx, maxy, intersectionNodes); if (intersectionNodes.size() > 1) { Set<Node> sourceNodes = new HashSet<Node>(); Map<Node, Set<Link>> inLinks = new HashMap<Node, Set<Link>>(); Set<Node> targetNodes = new HashSet<Node>(); Map<Node, Set<Link>> outLinks = new HashMap<Node, Set<Link>>(); for (Node intersectionNode : intersectionNodes) { for (Link link : intersectionNode.getInLinks().values()) { Node fromNode = link.getFromNode(); double x = fromNode.getCoord().getX(); double y = fromNode.getCoord().getY(); if (!(x > minx && y > miny && x < maxx && y < maxy)) { sourceNodes.add(fromNode); Set<Link> links = inLinks.get(fromNode); if (links == null) { links = new HashSet<Link>(); inLinks.put(fromNode, links); } links.add(link); } } for (Link link : intersectionNode.getOutLinks().values()) { Node toNode = link.getToNode(); double x = toNode.getCoord().getX(); double y = toNode.getCoord().getY(); if (!(x > minx && y > miny && x < maxx && y < maxy)) { targetNodes.add(toNode); Set<Link> links = outLinks.get(toNode); if (links == null) { links = new HashSet<Link>(); outLinks.put(toNode, links); } links.add(link); } } } for (Node intersectionNode : intersectionNodes) { network.removeNode(intersectionNode.getId()); quadTree.remove( intersectionNode.getCoord().getX(), intersectionNode.getCoord().getY(), intersectionNode); pendingNodes.remove(intersectionNode); } NetworkFactory factory = network.getFactory(); Node centerNode = factory.createNode(node.getId(), centerOfMass(intersectionNodes)); network.addNode(centerNode); quadTree.put(centerNode.getCoord().getX(), centerNode.getCoord().getY(), centerNode); for (Node source : sourceNodes) { Link newLink = factory.createLink(Id.create(linkIdCounter++, Link.class), source, centerNode); network.addLink(newLink); Set<Link> origLinks = inLinks.get(source); assignProps(origLinks, newLink); } for (Node target : targetNodes) { Link newLink = factory.createLink(Id.create(linkIdCounter++, Link.class), centerNode, target); network.addLink(newLink); Set<Link> origLinks = outLinks.get(target); assignProps(origLinks, newLink); } } } }
private static void testRestrictedNetwork(final Config config) throws Exception { // create a simple scenario, with two parallel links, // a long one for cars, a short one for pt. final Scenario scenario = ScenarioUtils.createScenario(config); Network net = scenario.getNetwork(); Node n1 = net.getFactory().createNode(Id.create(1, Node.class), new Coord((double) 0, (double) 0)); Node n2 = net.getFactory().createNode(Id.create(2, Node.class), new Coord((double) 0, (double) 0)); Node n3 = net.getFactory().createNode(Id.create(3, Node.class), new Coord((double) 0, (double) 0)); Node n4 = net.getFactory().createNode(Id.create(4, Node.class), new Coord((double) 0, (double) 0)); Link l1 = net.getFactory().createLink(Id.create("l1", Link.class), n1, n2); Link l2c = net.getFactory().createLink(Id.create("l2c", Link.class), n2, n3); Link l2pt = net.getFactory().createLink(Id.create("l2pt", Link.class), n2, n3); Link l3 = net.getFactory().createLink(Id.create("l3", Link.class), n3, n4); l2c.setAllowedModes(Collections.singleton(TransportMode.car)); l2c.setLength(1000); l2pt.setAllowedModes(Collections.singleton(TransportMode.pt)); l2pt.setLength(10); net.addNode(n1); net.addNode(n2); net.addNode(n3); net.addNode(n4); net.addLink(l1); net.addLink(l2c); net.addLink(l2pt); net.addLink(l3); Injector injector = Injector.createInjector( scenario.getConfig(), new AbstractModule() { @Override public void install() { install( AbstractModule.override( Arrays.asList(new TripRouterModule()), new AbstractModule() { @Override public void install() { bind(Scenario.class).toInstance(scenario); addTravelTimeBinding("car") .toInstance( new FreespeedTravelTimeAndDisutility(config.planCalcScore())); addTravelDisutilityFactoryBinding("car") .toInstance(new OnlyTimeDependentTravelDisutilityFactory()); } })); } }); // create the factory, get a router, route. Provider<TripRouter> factory = injector.getProvider(TripRouter.class); TripRouter router = factory.get(); List<? extends PlanElement> trip = router.calcRoute( TransportMode.car, new LinkFacility(l1), new LinkFacility(l3), 0, PersonImpl.createPerson(Id.create("toto", Person.class))); Leg l = (Leg) trip.get(0); // actual test NetworkRoute r = (NetworkRoute) l.getRoute(); Assert.assertEquals("unexpected route length " + r.getLinkIds(), 1, r.getLinkIds().size()); Assert.assertEquals("unexpected link", l2c.getId(), r.getLinkIds().get(0)); }
/** Checks that routes are found when using a monomodal network (ie modes are not restricted) */ @Test public void testMonomodalNetwork() throws Exception { final Config config = ConfigUtils.createConfig(); final Scenario scenario = ScenarioUtils.createScenario(config); Network net = scenario.getNetwork(); Node n1 = net.getFactory().createNode(Id.create(1, Node.class), new Coord((double) 0, (double) 0)); Node n2 = net.getFactory().createNode(Id.create(2, Node.class), new Coord((double) 0, (double) 0)); Node n3 = net.getFactory().createNode(Id.create(3, Node.class), new Coord((double) 0, (double) 0)); Node n4 = net.getFactory().createNode(Id.create(4, Node.class), new Coord((double) 0, (double) 0)); Link l1 = net.getFactory().createLink(Id.create("l1", Link.class), n1, n2); Link l2long = net.getFactory().createLink(Id.create("l2long", Link.class), n2, n3); Link l2short = net.getFactory().createLink(Id.create("l2short", Link.class), n2, n3); Link l3 = net.getFactory().createLink(Id.create("l3", Link.class), n3, n4); l2long.setLength(1000); l2short.setLength(10); net.addNode(n1); net.addNode(n2); net.addNode(n3); net.addNode(n4); net.addLink(l1); net.addLink(l2long); net.addLink(l2short); net.addLink(l3); // create the factory, get a router, route. Injector injector = Injector.createInjector( scenario.getConfig(), new AbstractModule() { @Override public void install() { install( AbstractModule.override( Arrays.asList(new TripRouterModule()), new AbstractModule() { @Override public void install() { bind(Scenario.class).toInstance(scenario); addTravelTimeBinding("car") .toInstance( new FreespeedTravelTimeAndDisutility(config.planCalcScore())); addTravelDisutilityFactoryBinding("car") .toInstance(new OnlyTimeDependentTravelDisutilityFactory()); } })); } }); TripRouter router = injector.getInstance(TripRouter.class); List<? extends PlanElement> trip = router.calcRoute( TransportMode.car, new LinkFacility(l1), new LinkFacility(l3), 0, PersonImpl.createPerson(Id.create("toto", Person.class))); Leg l = (Leg) trip.get(0); // actual test NetworkRoute r = (NetworkRoute) l.getRoute(); Assert.assertEquals("unexpected route length " + r.getLinkIds(), 1, r.getLinkIds().size()); Assert.assertEquals("unexpected link", l2short.getId(), r.getLinkIds().get(0)); }
private static void createNetwork(Scenario sc) { Network net = sc.getNetwork(); NetworkFactory fac = net.getFactory(); double length = 60; 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)); Node n2 = fac.createNode(Id.createNodeId(id++), CoordUtils.createCoord(length, length)); Node n3 = fac.createNode(Id.createNodeId(id++), CoordUtils.createCoord(0, length)); net.addNode(n0); net.addNode(n1); 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 l0 = fac.createLink(Id.createLinkId(id++), n1, n2); net.addLink(l0); l0.setCapacity(width); l0.setLength(length); } { Link l0 = fac.createLink(Id.createLinkId(id++), n2, n3); net.addLink(l0); l0.setCapacity(width); l0.setLength(length); } { Link l0 = fac.createLink(Id.createLinkId(id++), n3, n0); net.addLink(l0); l0.setCapacity(width); l0.setLength(length); } { Link l0 = fac.createLink(Id.createLinkId(id++), n1, n0); net.addLink(l0); l0.setCapacity(width); l0.setLength(length); } { Link l0 = fac.createLink(Id.createLinkId(id++), n2, n1); 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); } { Link l0 = fac.createLink(Id.createLinkId(id++), n0, n3); net.addLink(l0); l0.setCapacity(width); l0.setLength(length); } }
public static void main(String[] args) throws IOException { List<Setting> settings = new ArrayList<>(); for (double w = 2.9; w < 3; w += 0.05) { settings.add(new Setting(w, 3, 3, 500, 0, w, w)); } for (double w = .4; w < 3; w += 0.2) { settings.add(new Setting(3, 3, w, 500, 0, w, w)); } for (double w = 0.8; w <= 3; w += 0.2) { settings.add(new Setting(w, 3, 3, 500, 0, w, w)); } AbstractCANetwork.EMIT_VIS_EVENTS = false; USE_SPH = true; for (int R = 12; R <= 12; R++) { CASingleLaneDensityEstimatorSPH.H = R; CAMultiLaneDensityEstimatorSPH.H = R; CASingleLaneDensityEstimatorSPA.RANGE = R; CASingleLaneDensityEstimatorSPHII.H = R; CASingleLaneDensityEstimatorSPAII.RANGE = R; try { bw = new BufferedWriter( new FileWriter(new File("/Users/laemmel/devel/bipedca/ant/uni" + R))); } catch (IOException e) { e.printStackTrace(); } for (Setting s : settings) { System.out.println(s); Config c = ConfigUtils.createConfig(); c.global().setCoordinateSystem("EPSG:3395"); Scenario sc = ScenarioUtils.createScenario(c); // VIS only Sim2DConfig conf2d = Sim2DConfigUtils.createConfig(); Sim2DScenario sc2d = Sim2DScenarioUtils.createSim2dScenario(conf2d); sc.addScenarioElement(Sim2DScenario.ELEMENT_NAME, sc2d); Network net = sc.getNetwork(); ((NetworkImpl) net).setCapacityPeriod(1); NetworkFactory fac = net.getFactory(); int l = 8; int res = 20; Node n0 = fac.createNode(Id.createNodeId("0"), new CoordImpl(20 - res, 0)); Node n1 = fac.createNode(Id.createNodeId("1"), new CoordImpl(20, 0)); Node n2 = fac.createNode(Id.createNodeId("2"), new CoordImpl(24, 0)); Node n2ex = fac.createNode(Id.createNodeId("2ex"), new CoordImpl(24, 20)); Node n3 = fac.createNode(Id.createNodeId("3"), new CoordImpl(24 + l, 0)); Node n3ex = fac.createNode(Id.createNodeId("3ex"), new CoordImpl(24 + l, -20)); Node n4 = fac.createNode(Id.createNodeId("4"), new CoordImpl(24 + 4 + l, 0)); Node n5 = fac.createNode(Id.createNodeId("5"), new CoordImpl(24 + 4 + res + l, 0)); net.addNode(n5); net.addNode(n4); net.addNode(n3ex); net.addNode(n3); net.addNode(n2ex); net.addNode(n2); net.addNode(n1); net.addNode(n0); Link l0 = fac.createLink(Id.createLinkId("0"), n0, n1); Link l0rev = fac.createLink(Id.createLinkId("0rev"), n1, n0); Link l1 = fac.createLink(Id.createLinkId("1"), n1, n2); Link l1rev = fac.createLink(Id.createLinkId("1rev"), n2, n1); Link l2 = fac.createLink(Id.createLinkId("2"), n2, n3); Link l2rev = fac.createLink(Id.createLinkId("2rev"), n3, n2); Link l2ex = fac.createLink(Id.createLinkId("2ex"), n2, n2ex); Link l3 = fac.createLink(Id.createLinkId("3"), n3, n4); Link l3ex = fac.createLink(Id.createLinkId("3ex"), n3, n3ex); Link l3rev = fac.createLink(Id.createLinkId("3rev"), n4, n3); Link l4 = fac.createLink(Id.createLinkId("4"), n4, n5); Link l4rev = fac.createLink(Id.createLinkId("4rev"), n5, n4); l0.setLength(res); l1.setLength(4); l0rev.setLength(res); l1rev.setLength(4); l2rev.setLength(l); l2.setLength(l); l3rev.setLength(4); l3.setLength(4); l4rev.setLength(res); l4.setLength(res); l3ex.setLength(20); l2ex.setLength(20); net.addLink(l4); net.addLink(l4rev); net.addLink(l1); net.addLink(l0); net.addLink(l2); net.addLink(l2rev); net.addLink(l1rev); net.addLink(l0rev); net.addLink(l3rev); net.addLink(l3); net.addLink(l3ex); net.addLink(l2ex); double bl = s.bL; double bc = s.bCor; double br = s.bR; double bexR = s.bexR; double bexL = s.bexL; l0.setCapacity(5); l0rev.setCapacity(5); l1.setCapacity(bl); l1rev.setCapacity(bl); l2.setCapacity(bc); l2rev.setCapacity(bc); l3.setCapacity(br); l3rev.setCapacity(br); l4.setCapacity(5); l4rev.setCapacity(5); l3ex.setCapacity(bexR); l2ex.setCapacity(bexL); List<Link> linksLR = new ArrayList<Link>(); linksLR.add(l0); linksLR.add(l1); linksLR.add(l2); linksLR.add(l3ex); List<Link> linksRL = new ArrayList<Link>(); linksRL.add(l4rev); linksRL.add(l3rev); linksRL.add(l2rev); linksRL.add(l2ex); for (int i = 0; i < 1; i++) { for (double th = 1; th <= 1; th += 0.15) { runIt(net, linksLR, linksRL, sc, th, s); } // for (double th = .26; th <= .4; th += 0.01) { // runIt(net, linksLR, sc, th); // } // for (double th = .45; th < .5; th += 0.05) { // runIt(net, linksLR, sc, th); // } // for (double th = .5; th <= 1.; th += 0.1) { // runIt(net, linksLR, sc, th); // } } } bw.close(); } }
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++; } } }
public void testTransitRouteCopy() { Config config = super.loadConfig(null); config.scenario().setUseTransit(true); config.scenario().setUseVehicles(true); ScenarioImpl scenario = (ScenarioImpl) ScenarioUtils.createScenario(config); Id<Node> nodeId1 = Id.create("1", Node.class); Id<Node> nodeId2 = Id.create("2", Node.class); Id<Node> nodeId3 = Id.create("3", Node.class); Id<Link> linkId1 = Id.create("1", Link.class); Id<Link> linkId2 = Id.create("2", Link.class); // build network Network network = scenario.getNetwork(); NetworkFactory nBuilder = network.getFactory(); Node node1 = nBuilder.createNode(nodeId1, scenario.createCoord(0, 0)); Node node2 = nBuilder.createNode(nodeId2, scenario.createCoord(1000, 0)); Node node3 = nBuilder.createNode(nodeId3, scenario.createCoord(2000, 0)); network.addNode(node1); network.addNode(node2); network.addNode(node3); Link link1 = nBuilder.createLink(linkId1, node1, node2); Link link2 = nBuilder.createLink(linkId2, node2, node3); network.addLink(link1); network.addLink(link2); // build schedule TransitSchedule schedule = scenario.getTransitSchedule(); TransitScheduleFactory sBuilder = schedule.getFactory(); TransitStopFacility stopF1 = sBuilder.createTransitStopFacility( Id.create("1", TransitStopFacility.class), scenario.createCoord(1000.0, 0), false); TransitStopFacility stopF2 = sBuilder.createTransitStopFacility( Id.create("2", TransitStopFacility.class), scenario.createCoord(2000.0, 0), false); stopF1.setLinkId(link1.getId()); stopF2.setLinkId(link2.getId()); schedule.addStopFacility(stopF1); schedule.addStopFacility(stopF2); TransitLine tLine1 = sBuilder.createTransitLine(Id.create("1", TransitLine.class)); TransitRouteStop stop1 = sBuilder.createTransitRouteStop(stopF1, 0, 0); TransitRouteStop stop2 = sBuilder.createTransitRouteStop(stopF2, 100, 100); ArrayList<TransitRouteStop> stops = new ArrayList<TransitRouteStop>(); stops.add(stop1); stops.add(stop2); NetworkRoute netRoute = new LinkNetworkRouteImpl(link1.getId(), link2.getId()); netRoute.setLinkIds(link1.getId(), Collections.<Id<Link>>emptyList(), link2.getId()); TransitRoute tRoute1 = sBuilder.createTransitRoute(Id.create("1", TransitRoute.class), netRoute, stops, "bus"); tRoute1.addDeparture(sBuilder.createDeparture(Id.create("1", Departure.class), 7.0 * 3600)); tLine1.addRoute(tRoute1); schedule.addTransitLine(tLine1); // build vehicles new CreateVehiclesForSchedule(schedule, scenario.getVehicles()).run(); // build population Population population = scenario.getPopulation(); PopulationFactory pBuilder = population.getFactory(); Person person1 = pBuilder.createPerson(Id.create("1", Person.class)); Plan plan = pBuilder.createPlan(); Activity homeAct = pBuilder.createActivityFromLinkId("h", linkId1); homeAct.setEndTime(7.0 * 3600); plan.addActivity(homeAct); Leg leg = pBuilder.createLeg(TransportMode.pt); ExperimentalTransitRoute tRoute = new ExperimentalTransitRoute(stopF1, tLine1, tRoute1, stopF2); leg.setRoute(tRoute); plan.addLeg(leg); plan.addActivity(pBuilder.createActivityFromLinkId("w", linkId2)); person1.addPlan(plan); population.addPerson(person1); // prepare config config.controler().setFirstIteration(0); config.controler().setLastIteration(1); ActivityParams params = new ActivityParams("h"); params.setTypicalDuration(16.0 * 3600); config.planCalcScore().addActivityParams(params); params = new ActivityParams("w"); params.setTypicalDuration(8.0 * 3600); config.planCalcScore().addActivityParams(params); StrategySettings tam = new StrategySettings(Id.create(1, StrategySettings.class)); tam.setStrategyName("TimeAllocationMutator"); tam.setWeight(1.0); config.strategy().addStrategySettings(tam); // run Controler controler = new Controler(scenario); controler.getConfig().controler().setWriteEventsInterval(0); controler.setCreateGraphs(false); controler.run(); // checks assertEquals(1, population.getPersons().size()); assertEquals(2, person1.getPlans().size()); assertEquals( ExperimentalTransitRoute.class, ((Leg) person1.getPlans().get(0).getPlanElements().get(1)).getRoute().getClass()); assertEquals( ExperimentalTransitRoute.class, ((Leg) person1.getPlans().get(1).getPlanElements().get(1)).getRoute().getClass()); }
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; }