Class WeightedOperatorSelector<T>
java.lang.Object
com.graphhopper.jsprit.core.algorithm.selector.WeightedOperatorSelector<T>
- Type Parameters:
T- the type of operator
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordEntry holding an operator with its weight. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new selector with default random.WeightedOperatorSelector(Random random) Creates a new selector with the specified random. -
Method Summary
Modifier and TypeMethodDescriptionAdds an operator with the specified weight.Adds an operator with the specified weight and name.voidclear()Clears all registered operators.Returns an unmodifiable list of all entries.Returns the entry that was selected in the last call toselect().Returns the name of the operator selected in the last call toselect().doublegetProbability(int index) Returns the selection probability for an operator at the given index.doubleReturns the total weight of all operators.booleanisEmpty()Returns true if no operators have been registered.select()Selects a random operator based on weights.voidSets the random number generator.intsize()Returns the number of operators registered.toString()
-
Constructor Details
-
WeightedOperatorSelector
public WeightedOperatorSelector()Creates a new selector with default random. -
WeightedOperatorSelector
Creates a new selector with the specified random.- Parameters:
random- the random number generator to use
-
-
Method Details
-
setRandom
Sets the random number generator.- Parameters:
random- the random number generator
-
add
Adds an operator with the specified weight.- Parameters:
operator- the operator to addweight- the selection weight (must be positive)- Returns:
- this selector for chaining
-
add
Adds an operator with the specified weight and name.- Parameters:
operator- the operator to addweight- the selection weight (must be positive)name- optional name for identification/debugging- Returns:
- this selector for chaining
-
select
Selects a random operator based on weights.- Returns:
- the selected operator
- Throws:
IllegalStateException- if no operators have been added
-
getLastSelected
Returns the entry that was selected in the last call toselect().- Returns:
- the last selected entry, or null if select() hasn't been called
-
getLastSelectedName
Returns the name of the operator selected in the last call toselect().- 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
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
-