Skip to content

faq 110467206

Billy Charlton edited this page Sep 5, 2018 · 2 revisions

TravelCost

by Gregory Macfarlane on 2017-07-05 17:07:16


I'd like to get the shortest distance from two coordinates along a network. But for now I'm just trying to use Node ID's. I pulled some code from http://www.matsim.org/apidocs/core/0.3.0/org/matsim/core/router/Dijkstra.html to build this method. Unfortunately I can't find any documentation on how to specify the `costFunction` argument.


public static LeastCostPathCalculator.Path getShortestPath(
   Network network, Node toNode, Node fromNode){

   PreProcessDijkstra preProcessData = new PreProcessDijkstra();
   preProcessData.run(network);
   TravelCost costFunction = ; //how to specify costFunction
   LeastCostPathCalculator routingAlgo = new Dijkstra(network, costFunction, preProcessData);

   LeastCostPathCalculator.Path path = routingAlgo.calcLeastCostPath(
     toNode, fromNode, 3600 * 4, null, null); 
     // distance shouldn't matter because it's not time-dependent
   return(path);
 }

The only field I want to minimize is `Length` or distance. I need this information in order to create the demand, so I don't have a full simulation running. That makes the iteration end method Rolf implemented in Silo a bit heavy-handed. Additionally, I don't really want to specify my own router like in the shortest path programming tutorial.

Really, all I want is to give a list of coordinate pairs and return the links along the shortest path between each pair. Is there already something written that can do this easily? 

While I have you, is there a way to print the list of links in the shortest path as a character string, for instance?




Comments: 1


Re: TravelCost

by Marcel Rieser on 2017-07-05 20:03:22

You linked to the API-documentation from MATSim 0.3.0 which is about 6 years old! The current release is MATSim 0.9.0. Which version of MATSim are you actually using?

In MATSim 0.9.0, the class TravelCost no longer exists. It was replaced a while ago by TravelDisutility. This is a simple interface you could implement with your own disutility (=cost) calculation. If you only want to use the distance for routing, simply return the link length as disutility, like this:

public class DistanceOnlyTravelDisutility implements TravelDisutility {
   @Override
   public double getLinkTravelDisutility(final Link link, final double time, final Person person, final Vehicle vehicle) {
     return link.getLength();
   }

   @Override
   public double getLinkMinimumTravelDisutility(Link link) {
     return link.getLength();
   }
 }


 


On a side note: using AStarLandmarks instead of Dijkstra should be quite a lot faster while giving you still the same, correct results.

Clone this wiki locally