Java Code Examples for org.matsim.api.core.v01.Id#createLinkId()

The following examples show how to use org.matsim.api.core.v01.Id#createLinkId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PTMapperTools.java    From pt2matsim with GNU General Public License v2.0 5 votes vote down vote up
public static Id<Link> createArtificialLinkId(LinkCandidate fromLinkCandidate, LinkCandidate toLinkCandidate) {
	if(fromLinkCandidate.isLoopLink()) {
		return Id.createLinkId(PublicTransitMappingStrings.PREFIX_ARTIFICIAL + fromLinkCandidate.getStop().getStopFacility().getId() + "_" + toLinkCandidate.getLink().getId());
	} else if(toLinkCandidate.isLoopLink()) {
		return Id.createLinkId(PublicTransitMappingStrings.PREFIX_ARTIFICIAL + fromLinkCandidate.getLink().getId() + "_" + toLinkCandidate.getStop().getStopFacility().getId());
	} else {
		return Id.createLinkId(PublicTransitMappingStrings.PREFIX_ARTIFICIAL + fromLinkCandidate.getLink().getId() + "_" + toLinkCandidate.getLink().getId());
	}
}
 
Example 2
Source File: BasicScheduleEditor.java    From pt2matsim with GNU General Public License v2.0 5 votes vote down vote up
/**
 * "Refreshes" the transit route by routing between all referenced links
 * of the stop facilities.
 */
@Override
public void refreshTransitRoute(TransitLine transitLine, TransitRoute transitRoute) {
	List<TransitRouteStop> routeStops = transitRoute.getStops();
	List<Id<Link>> linkSequence = new ArrayList<>();
	linkSequence.add(routeStops.get(0).getStopFacility().getLinkId());

	// route
	for(int i = 0; i < routeStops.size() - 1; i++) {
		if(routeStops.get(i).getStopFacility().getLinkId() == null) {
			throw new IllegalArgumentException("stop facility " + routeStops.get(i).getStopFacility().getName() + " (" + routeStops.get(i).getStopFacility().getId() + " not referenced!");
		}
		if(routeStops.get(i + 1).getStopFacility().getLinkId() == null) {
			throw new IllegalArgumentException("stop facility " + routeStops.get(i - 1).getStopFacility().getName() + " (" + routeStops.get(i + 1).getStopFacility().getId() + " not referenced!");
		}

		Id<Link> currentLinkId = Id.createLinkId(routeStops.get(i).getStopFacility().getLinkId().toString());

		Link currentLink = network.getLinks().get(currentLinkId);
		Link nextLink = network.getLinks().get(routeStops.get(i + 1).getStopFacility().getLinkId());

		List<Id<Link>> path = PTMapperTools.getLinkIdsFromPath(routers.calcLeastCostPath(currentLink.getToNode().getId(), nextLink.getFromNode().getId(), transitLine, transitRoute));

		if(path != null)
			linkSequence.addAll(path);

		linkSequence.add(nextLink.getId());
	}

	// add link sequence to schedule
	transitRoute.setRoute(RouteUtils.createNetworkRoute(linkSequence, network));
}
 
Example 3
Source File: StandardMATSimScenarioTest.java    From amodeus with GNU General Public License v2.0 4 votes vote down vote up
private static void makeMultimodal(Scenario scenario) {
    // Add pt-links to the network to test a multimodal network as it appears in standard MATSim use cases

    Network network = scenario.getNetwork();
    NetworkFactory factory = network.getFactory();

    // Let's build a fast track through the scenario
    for (int i = 0; i < 9; i++) {
        Id<Link> ptFowardLinkId = Id.createLinkId(String.format("pt_fwd_%d:%d", i, i));
        Id<Link> ptBackwardLinkId = Id.createLinkId(String.format("pt_bck_%d:%d", i, i));
        Id<Node> fromNodeId = Id.createNodeId(String.format("%d:%d", i, i));
        Id<Node> toNodeId = Id.createNodeId(String.format("%d:%d", i + 1, i + 1));

        Link ptFowardLink = factory.createLink(ptFowardLinkId, network.getNodes().get(fromNodeId), network.getNodes().get(toNodeId));
        ptFowardLink.setFreespeed(100.0 * 1000.0 / 3600.0);
        ptFowardLink.setLength(1000.0);
        ptFowardLink.setAllowedModes(Collections.singleton("pt"));
        network.addLink(ptFowardLink);

        Link ptBackwardLink = factory.createLink(ptBackwardLinkId, network.getNodes().get(toNodeId), network.getNodes().get(fromNodeId));
        ptBackwardLink.setFreespeed(100.0 * 1000.0 / 3600.0);
        ptBackwardLink.setLength(1000.0);
        ptBackwardLink.setAllowedModes(Collections.singleton("pt"));
        network.addLink(ptBackwardLink);
    }

    // Also, a routed population may have "pt interaction" activities, which take place at links that are not part of the road network. Amodeus must be able
    // to
    // handle these cases.

    /* for (Person person : scenario.getPopulation().getPersons().values())
     * for (Plan plan : person.getPlans()) {
     * Activity trickyActivity = PopulationUtils.createActivityFromCoordAndLinkId("pt interaction", new Coord(5500.0, 5500.0), Id.createLinkId("pt_fwd_5:5"));
     * 
     * plan.getPlanElements().add(PopulationUtils.createLeg("walk"));
     * plan.getPlanElements().add(trickyActivity);
     * } */

    // TODO @sebhoerl Difficult to keep this in as handling of "interaction" activities become much smarter in MATSim now. We would need to
    // set up a much more realistic test scenario. There is one in the AV package, so we can use that one!

    for (Link link : network.getLinks().values()) {
        if (link.getAllowedModes().contains("car")) {
            link.setAllowedModes(new HashSet<>(Arrays.asList("car", AmodeusModeConfig.DEFAULT_MODE)));
        }
    }
}
 
Example 4
Source File: ArtificialScenarioCreator.java    From amodeus with GNU General Public License v2.0 4 votes vote down vote up
public ArtificialScenarioCreator(Config config) {
    network = (config == null) ? NetworkUtils.createNetwork() : NetworkUtils.createNetwork(config);

    Id<Node> nodeid1 = Id.createNodeId("node1");
    Coord coord1 = new Coord(100, 100);
    node1 = NetworkUtils.createNode(nodeid1, coord1);
    network.addNode(node1);

    Id<Node> nodeid2 = Id.createNodeId("node2");
    Coord coord2 = new Coord(100, 200);
    node2 = NetworkUtils.createNode(nodeid2, coord2);
    network.addNode(node2);

    Id<Node> nodeid3 = Id.createNodeId("node3");
    Coord coord3 = new Coord(200, 200);
    node3 = NetworkUtils.createNode(nodeid3, coord3);
    network.addNode(node3);

    Id<Node> nodeid4 = Id.createNodeId("node4");
    Coord coord4 = new Coord(200, 100);
    node4 = NetworkUtils.createNode(nodeid4, coord4);
    network.addNode(node4);

    Id<Node> depotId = Id.createNodeId("depot");
    Coord depotCoord = new Coord(100, 0);
    depot = NetworkUtils.createNode(depotId, depotCoord);
    network.addNode(depot);

    Id<Link> idUp = Id.createLinkId("linkUp");
    linkUp = NetworkUtils.createLink(idUp, node1, node2, network, length, freespeed, capacity, lanes);
    Id<Link> idRight = Id.createLinkId("linkRight");
    linkRight = NetworkUtils.createLink(idRight, node2, node3, network, length, freespeed, capacity, lanes);
    Id<Link> idDown = Id.createLinkId("linkDown");
    linkDown = NetworkUtils.createLink(idDown, node3, node4, network, length, freespeed, capacity, lanes);
    Id<Link> idLeft = Id.createLinkId("linkLeft");
    linkLeft = NetworkUtils.createLink(idLeft, node4, node1, network, length, freespeed, capacity, lanes);

    Id<Link> iddepotIn = Id.createLinkId("linkDepotIn");
    linkDepotIn = NetworkUtils.createLink(iddepotIn, node1, depot, network, length, freespeed, capacity, lanes);
    Id<Link> iddepotOut = Id.createLinkId("linkDepotOut");
    linkDepotOut = NetworkUtils.createLink(iddepotOut, depot, node1, network, length, freespeed, capacity, lanes);

    network.addLink(linkUp);
    network.addLink(linkRight);
    network.addLink(linkDown);
    network.addLink(linkLeft);
    network.addLink(linkDepotIn);
    network.addLink(linkDepotOut);

    avRequest1 = new AmodeusRequest(Id.create("p1", Request.class), null, linkUp, linkDown, 0.0, AmodeusModeConfig.DEFAULT_MODE, null);
    avRequest2 = new AmodeusRequest(Id.create("p2", Request.class), null, linkRight, linkLeft, 0.0, AmodeusModeConfig.DEFAULT_MODE, null);
    avRequest3 = new AmodeusRequest(Id.create("p3", Request.class), null, linkRight, linkUp, 0.0, AmodeusModeConfig.DEFAULT_MODE, null);
    avRequest4 = new AmodeusRequest(Id.create("p4", Request.class), null, linkRight, linkDown, 0.0, AmodeusModeConfig.DEFAULT_MODE, null);
    avRequest5 = new AmodeusRequest(Id.create("p5", Request.class), null, linkUp, linkRight, 0.0, AmodeusModeConfig.DEFAULT_MODE, null);
    avRequest6 = new AmodeusRequest(Id.create("p6", Request.class), null, linkUp, linkLeft, 0.0, AmodeusModeConfig.DEFAULT_MODE, null);
    avRequest7 = new AmodeusRequest(Id.create("p7", Request.class), null, linkRight, linkLeft, 0.0, AmodeusModeConfig.DEFAULT_MODE, null);
    avRequestDepotOut = new AmodeusRequest(Id.create("depotRequestOut", Request.class), null, linkDepotOut, linkDepotOut, 0.0, AmodeusModeConfig.DEFAULT_MODE, null);
    avRequestDepotIn = new AmodeusRequest(Id.create("depotRequestIn", Request.class), null, linkDepotIn, linkDepotIn, 0.0, AmodeusModeConfig.DEFAULT_MODE, null);

}
 
Example 5
Source File: PTMapperTools.java    From pt2matsim with GNU General Public License v2.0 4 votes vote down vote up
public static Id<Link> createArtificialLinkId(TransitStopFacility stopFacility) {
	return Id.createLinkId(PublicTransitMappingStrings.PREFIX_ARTIFICIAL + stopFacility.getId());
}
 
Example 6
Source File: ScheduleTools.java    From pt2matsim with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Generates link sequences (network route) for all transit routes in
 * the schedule, modifies the schedule. All stopFacilities used by a
 * route must have a link referenced.
 *
 * @param schedule where transitRoutes should be routed
 * @param network  the network where the routes should be routed
 * @param routers  schedule routers class defining the Router for each transit route.
 */
public static void routeSchedule(TransitSchedule schedule, Network network, ScheduleRouters routers) {
	Counter counterRoute = new Counter("route # ");

	log.info("Routing all routes with referenced links...");

	if(routers == null) {
		log.error("No routers given, routing cannot be completed!");
		return;
	}

	for(TransitLine transitLine : schedule.getTransitLines().values()) {
		for(TransitRoute transitRoute : transitLine.getRoutes().values()) {
			if(transitRoute.getStops().size() > 0) {
				counterRoute.incCounter();

				List<TransitRouteStop> routeStops = transitRoute.getStops();
				List<Id<Link>> linkIdSequence = new LinkedList<>();
				linkIdSequence.add(routeStops.get(0).getStopFacility().getLinkId());

				// route
				for(int i = 0; i < routeStops.size() - 1; i++) {
					if(routeStops.get(i).getStopFacility().getLinkId() == null) {
						log.warn("stop facility " + routeStops.get(i).getStopFacility().getName() + " (" + routeStops.get(i).getStopFacility().getId() + ") not referenced!");
						linkIdSequence = null;
						break;
					}
					if(routeStops.get(i + 1).getStopFacility().getLinkId() == null) {
						log.warn("stop facility " + routeStops.get(i - 1).getStopFacility().getName() + " (" + routeStops.get(i + 1).getStopFacility().getId() + " not referenced!");
						linkIdSequence = null;
						break;
					}

					Id<Link> currentLinkId = Id.createLinkId(routeStops.get(i).getStopFacility().getLinkId().toString());
					Link currentLink = network.getLinks().get(currentLinkId);
					Link nextLink = network.getLinks().get(routeStops.get(i + 1).getStopFacility().getLinkId());

					LeastCostPathCalculator.Path leastCostPath = routers.calcLeastCostPath(currentLink.getToNode().getId(), nextLink.getFromNode().getId(), transitLine, transitRoute);


					List<Id<Link>> path = null;
					if(leastCostPath != null) {
						path = PTMapperTools.getLinkIdsFromPath(leastCostPath);
					}

					if(path != null)
						linkIdSequence.addAll(path);

					linkIdSequence.add(nextLink.getId());
				} // -for stops

				// add link sequence to schedule
				if(linkIdSequence != null) {
					transitRoute.setRoute(RouteUtils.createNetworkRoute(linkIdSequence, network));
				}
			} else {
				log.warn("Route " + transitRoute.getId() + " on line " + transitLine.getId() + " has no stop sequence");
			}
		} // -route
	} // -line
	log.info("Routing all routes with referenced links... done");
}