Class WeightedOperatorSelector<T>

java.lang.Object
com.graphhopper.jsprit.core.algorithm.selector.WeightedOperatorSelector<T>
Type Parameters:
T - the type of operator

public class WeightedOperatorSelector<T> extends Object
Selects operators based on their weights using weighted random selection.

This class provides a simple way to manage multiple operators with different probabilities of being selected. Operators with higher weights are selected more frequently.

Thread-safety: This class is NOT thread-safe. If used from multiple threads, external synchronization is required.

  • Constructor Details

    • WeightedOperatorSelector

      public WeightedOperatorSelector()
      Creates a new selector with default random.
    • WeightedOperatorSelector

      public WeightedOperatorSelector(Random random)
      Creates a new selector with the specified random.
      Parameters:
      random - the random number generator to use
  • Method Details

    • setRandom

      public void setRandom(Random random)
      Sets the random number generator.
      Parameters:
      random - the random number generator
    • add

      public WeightedOperatorSelector<T> add(T operator, double weight)
      Adds an operator with the specified weight.
      Parameters:
      operator - the operator to add
      weight - the selection weight (must be positive)
      Returns:
      this selector for chaining
    • add

      public WeightedOperatorSelector<T> add(T operator, double weight, String name)
      Adds an operator with the specified weight and name.
      Parameters:
      operator - the operator to add
      weight - the selection weight (must be positive)
      name - optional name for identification/debugging
      Returns:
      this selector for chaining
    • select

      public T select()
      Selects a random operator based on weights.
      Returns:
      the selected operator
      Throws:
      IllegalStateException - if no operators have been added
    • getLastSelected

      public WeightedOperatorSelector.Entry<T> getLastSelected()
      Returns the entry that was selected in the last call to select().
      Returns:
      the last selected entry, or null if select() hasn't been called
    • getLastSelectedName

      public String getLastSelectedName()
      Returns the name of the operator selected in the last call to select().
      Returns:
      the name of the last selected operator, or "unknown" if no name was set
    • size

      public int size()
      Returns the number of operators registered.
    • isEmpty

      public boolean isEmpty()
      Returns true if no operators have been registered.
    • getTotalWeight

      public double getTotalWeight()
      Returns the total weight of all operators.
    • getEntries

      public List<WeightedOperatorSelector.Entry<T>> getEntries()
      Returns an unmodifiable list of all entries.
    • getProbability

      public double getProbability(int index)
      Returns the selection probability for an operator at the given index.
      Parameters:
      index - the index of the operator
      Returns:
      the probability (0.0 to 1.0) of selecting this operator
    • clear

      public void clear()
      Clears all registered operators.
    • toString

      public String toString()
      Overrides:
      toString in class Object