ORSPriorityWeighting.java

  1. /*  This file is part of Openrouteservice.
  2.  *
  3.  *  Openrouteservice is free software; you can redistribute it and/or modify it under the terms of the
  4.  *  GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1
  5.  *  of the License, or (at your option) any later version.

  6.  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  7.  *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8.  *  See the GNU Lesser General Public License for more details.

  9.  *  You should have received a copy of the GNU Lesser General Public License along with this library;
  10.  *  if not, see <https://www.gnu.org/licenses/>.
  11.  */
  12. package org.heigit.ors.routing.graphhopper.extensions.weighting;

  13. import com.graphhopper.routing.ev.DecimalEncodedValue;
  14. import com.graphhopper.routing.util.FlagEncoder;
  15. import com.graphhopper.routing.weighting.TurnCostProvider;
  16. import com.graphhopper.util.EdgeIteratorState;
  17. import com.graphhopper.util.PMap;
  18. import org.heigit.ors.routing.graphhopper.extensions.flagencoders.FlagEncoderKeys;
  19. import org.heigit.ors.routing.graphhopper.extensions.util.PriorityCode;

  20. import static com.graphhopper.routing.util.EncodingManager.getKey;

  21. public class ORSPriorityWeighting extends ORSFastestWeighting {
  22.     private static final double PRIORITY_BEST = PriorityCode.BEST.getValue();
  23.     private static final double PRIORITY_UNCHANGED = PriorityCode.UNCHANGED.getValue();
  24.     private final DecimalEncodedValue priorityEncoder;

  25.     public ORSPriorityWeighting(FlagEncoder encoder, PMap map, TurnCostProvider tcp) {
  26.         super(encoder, map, tcp);
  27.         priorityEncoder = encoder.getDecimalEncodedValue(getKey(encoder, FlagEncoderKeys.PRIORITY_KEY));
  28.     }

  29.     @Override
  30.     public double calcEdgeWeight(EdgeIteratorState edgeState, boolean reverse, long edgeEnterTime) {
  31.         double weight = super.calcEdgeWeight(edgeState, reverse, edgeEnterTime);
  32.         if (Double.isInfinite(weight))
  33.             return Double.POSITIVE_INFINITY;

  34.         double priority = priorityEncoder.getDecimal(reverse, edgeState.getFlags());

  35.         double normalizedPriority = priority * PRIORITY_BEST - PRIORITY_UNCHANGED;

  36.         double factor = Math.pow(2, normalizedPriority / (PRIORITY_UNCHANGED - PRIORITY_BEST));

  37.         return weight * factor;
  38.     }

  39.     @Override
  40.     public boolean equals(Object obj) {
  41.         if (obj == null)
  42.             return false;
  43.         if (getClass() != obj.getClass())
  44.             return false;
  45.         final ORSPriorityWeighting other = (ORSPriorityWeighting) obj;
  46.         return toString().equals(other.toString());
  47.     }

  48.     @Override
  49.     public int hashCode() {
  50.         return ("ORSPriorityWeighting" + this).hashCode();
  51.     }

  52.     @Override
  53.     public String getName() {
  54.         return "recommended";
  55.     }
  56. }