All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentripplanner.astar.strategy.MaxCountTerminationStrategy Maven / Gradle / Ivy

The newest version!
package org.opentripplanner.astar.strategy;

import java.util.function.Predicate;
import org.opentripplanner.astar.spi.AStarState;
import org.opentripplanner.astar.spi.SearchTerminationStrategy;

/**
 * This termination strategy is used to terminate an a-star search after a number of states matching
 * some criteria has been found. For example it can be used to limit a search to a maximum number of
 * stops.
 */
public class MaxCountTerminationStrategy>
  implements SearchTerminationStrategy {

  private final int maxCount;
  private final Predicate shouldIncreaseCount;
  private int count;

  /**
   * @param maxCount Terminate the search after this many matching states have been reached.
   * @param shouldIncreaseCount A predicate to check if a state should increase the count or not.
   */
  public MaxCountTerminationStrategy(int maxCount, Predicate shouldIncreaseCount) {
    this.maxCount = maxCount;
    this.shouldIncreaseCount = shouldIncreaseCount;
    this.count = 0;
  }

  @Override
  public boolean shouldSearchTerminate(State current) {
    if (shouldIncreaseCount.test(current)) {
      count++;
    }
    return count >= maxCount;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy