PreferencePriorityWeighting.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.FastestWeighting;
  16. import com.graphhopper.routing.weighting.TurnCostProvider;
  17. import com.graphhopper.util.EdgeIteratorState;
  18. import com.graphhopper.util.PMap;
  19. import org.heigit.ors.routing.graphhopper.extensions.flagencoders.FlagEncoderKeys;
  20. import org.heigit.ors.routing.graphhopper.extensions.util.PriorityCode;

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

  22. public class PreferencePriorityWeighting extends FastestWeighting {
  23.     private static final double THRESHOLD_VERY_BAD = PriorityCode.AVOID_IF_POSSIBLE.getValue() / (double) PriorityCode.BEST.getValue();
  24.     private static final double THRESHOLD_REACH_DEST = PriorityCode.REACH_DEST.getValue() / (double) PriorityCode.BEST.getValue();
  25.     private static final double THRESHOLD_PREFER = PriorityCode.PREFER.getValue() / (double) PriorityCode.BEST.getValue();
  26.     private static final double THRESHOLD_VERY_NICE = PriorityCode.VERY_NICE.getValue() / (double) PriorityCode.BEST.getValue();
  27.     private final DecimalEncodedValue priorityEncoder;

  28.     public PreferencePriorityWeighting(FlagEncoder encoder, PMap map) {
  29.         super(encoder, map);
  30.         priorityEncoder = encoder.getDecimalEncodedValue(getKey(encoder, FlagEncoderKeys.PRIORITY_KEY));
  31.     }

  32.     public PreferencePriorityWeighting(FlagEncoder encoder, PMap map, TurnCostProvider tcp) {
  33.         super(encoder, map, tcp);
  34.         priorityEncoder = encoder.getDecimalEncodedValue(getKey(encoder, FlagEncoderKeys.PRIORITY_KEY));
  35.     }

  36.     @Override
  37.     public double calcEdgeWeight(EdgeIteratorState edgeState, boolean reverse) {
  38.         double weight = super.calcEdgeWeight(edgeState, reverse);
  39.         if (Double.isInfinite(weight))
  40.             weight = 0.0;

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

  42.         if (priority <= THRESHOLD_REACH_DEST)
  43.             priority /= 1.5;
  44.         else if (priority <= THRESHOLD_VERY_BAD)
  45.             priority /= 1.25;
  46.         else if (priority == THRESHOLD_PREFER)
  47.             priority *= 1.5;
  48.         else if (priority >= THRESHOLD_VERY_NICE)
  49.             priority *= 2.2;

  50.         return weight / (0.5 + priority);
  51.     }

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

  56.     @Override
  57.     public boolean equals(Object obj) {
  58.         if (obj == null)
  59.             return false;
  60.         if (getClass() != obj.getClass())
  61.             return false;
  62.         final PreferencePriorityWeighting other = (PreferencePriorityWeighting) obj;
  63.         return toString().equals(other.toString());
  64.     }

  65.     @Override
  66.     public int hashCode() {
  67.         return ("PreferencePriorityWeighting" + this).hashCode();
  68.     }
  69. }