MATSIM
AStarEuclidean.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * AStarEuclidean.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.router;
22 
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
28 import org.matsim.core.router.util.*;
31 
75 public class AStarEuclidean extends Dijkstra {
76  private static final Logger log = LogManager.getLogger( AStarEuclidean.class ) ;
77 
78  protected final double overdoFactor;
79 
80  private double minTravelCostPerLength;
81 
89  final TravelTime timeFunction) {
91  }
92 
103  final TravelTime timeFunction, final double overdoFactor) {
104  this(network, preProcessData, preProcessData.getCostFunction(),
106  }
107 
118  AStarEuclidean(final Network network,
119  final PreProcessEuclidean preProcessData,
120  final TravelDisutility costFunction, final TravelTime timeFunction, final double overdoFactor) {
121  super(network, costFunction, timeFunction, preProcessData);
122 
123  setMinTravelCostPerLength(preProcessData.getMinTravelCostPerLength());
124 
125  this.overdoFactor = overdoFactor;
126  }
127 
142  @Override
143  protected void initFromNode(final Node fromNode, final Node toNode, final double startTime, final RouterPriorityQueue<Node> pendingNodes) {
144  AStarNodeData data = getData(fromNode);
145  visitNode(fromNode, data, pendingNodes, startTime, 0, null);
147  }
148 
149  @Override
150  protected boolean addToPendingNodes(final Link l, final Node n, final RouterPriorityQueue<Node> pendingNodes,
151  final double currTime, final double currCost, final Node toNode) {
152 
153  final double travelTime = this.timeFunction.getLinkTravelTime(l, currTime, this.person, this.vehicle);
154  final double travelCost = this.costFunction.getLinkTravelDisutility(l, currTime, this.person, this.vehicle);
155  final AStarNodeData data = getData(n);
156  if (!data.isVisited(getIterationId())) {
157  double remainingTravelCost = estimateRemainingTravelCost(n, toNode);
158  visitNode(n, data, pendingNodes, currTime + travelTime, currCost + travelCost, remainingTravelCost, l);
159  return true;
160  }
161 
162  final double nCost = data.getCost();
163  final double totalCost = currCost + travelCost;
164  if (totalCost < nCost) {
165  revisitNode(n, data, pendingNodes, currTime + travelTime, totalCost, l);
166  return true;
167  } else if (totalCost == nCost) {
168  // Special case: a node can be reached from two links with exactly the same costs.
169  // Decide based on the linkId which one to take... just have to common criteria to be deterministic.
170 
171  if ( totalCost==0. ) {
172  log.warn( "finding totalCost=" + totalCost + "; this will often (or always?) lead to a null " +
173  "pointer exception later. In my own case, it was related to a network " +
174  "having freespeed infinity at places. linkId=" + l.getId()+ ". kai, jan'18") ;
175  }
176 
177  if (data.getPrevLink().getId().compareTo(l.getId()) > 0) {
178  revisitNode(n, data, pendingNodes, currTime + travelTime, totalCost, l);
179  return true;
180  }
181  }
182  return false;
183  }
184 
197  private void visitNode(final Node n, final AStarNodeData data, final RouterPriorityQueue<Node> pendingNodes,
198  final double time, final double cost, final double expectedRemainingCost, final Link outLink) {
199  data.setExpectedRemainingCost(expectedRemainingCost);
200  super.visitNode(n, data, pendingNodes, time, cost, outLink);
201  }
202 
206  public double getOverdoFactor() {
207  return this.overdoFactor;
208  }
209 
217  protected double estimateRemainingTravelCost(final Node fromNode, final Node toNode) {
218  double dist = CoordUtils.calcEuclideanDistance(fromNode.getCoord(), toNode.getCoord()) * getMinTravelCostPerLength();
219  return dist * this.overdoFactor;
220  }
221 
228  @Override
229  protected AStarNodeData getData(final Node n) {
230  return (AStarNodeData) super.getData(n);
231  }
232 
233  @Override
235  return new AStarNodeData();
236  }
237 
244  /*package*/ void setMinTravelCostPerLength(final double minTravelCostPerLength) {
245  this.minTravelCostPerLength = minTravelCostPerLength;
246  }
247 
253  public final double getMinTravelCostPerLength() {
254  return this.minTravelCostPerLength;
255  }
256 
261  @Override
262  protected double getPriority(final DijkstraNodeData data) {
263  return ((AStarNodeData) data).getExpectedCost();
264  }
265 }
static double calcEuclideanDistance(Coord coord, Coord other)
void setExpectedRemainingCost(final double expectedCost)
double getLinkTravelTime(Link link, double time, Person person, Vehicle vehicle)
void revisitNode(final Node n, final DijkstraNodeData data, final RouterPriorityQueue< Node > pendingNodes, final double time, final double cost, final Link outLink)
Definition: Dijkstra.java:530
double getLinkTravelDisutility(final Link link, final double time, final Person person, final Vehicle vehicle)
boolean addToPendingNodes(final Link l, final Node n, final RouterPriorityQueue< Node > pendingNodes, final double currTime, final double currCost, final Node toNode)
final PreProcessDijkstra preProcessData
Definition: Dijkstra.java:127
final TravelDisutility costFunction
Definition: Dijkstra.java:98
void initFromNode(final Node fromNode, final Node toNode, final double startTime, final RouterPriorityQueue< Node > pendingNodes)
final TravelTime timeFunction
Definition: Dijkstra.java:103
double estimateRemainingTravelCost(final Node fromNode, final Node toNode)
void visitNode(final Node n, final AStarNodeData data, final RouterPriorityQueue< Node > pendingNodes, final double time, final double cost, final double expectedRemainingCost, final Link outLink)
double getPriority(final DijkstraNodeData data)
AStarNodeData getData(final Node n)