# Class HeuristicBiasedStochasticSampling<T extends Copyable<T>>

java.lang.Object
org.cicirello.search.ss.HeuristicBiasedStochasticSampling<T>
Type Parameters:
`T` - The type of object under optimization.
All Implemented Interfaces:
`Splittable<TrackableSearch<T>>`, `Metaheuristic<T>`, `SimpleMetaheuristic<T>`, `TrackableSearch<T>`

public final class HeuristicBiasedStochasticSampling<T extends Copyable<T>> extends Object
Heuristic Biased Stochastic Sampling (HBSS) is a form of stochastic sampling search that uses a constructive heuristic to bias the random decisions. In HBSS, the search generates N random candidate solutions to the problem, using a problem-specific heuristic to rank the choices, and then biasing each random decision in favor of choices that are preferred by the heuristic (have ranks closer to 1). It evaluates each of the N candidate solutions with respect to the optimization problem's cost function, and returns the best of the N candidate solutions.

Although the HBSS algorithm itself is not restricted to permutation problems, the examples that follow in this documentation focus on permutations for illustrative purposes.

Let's consider an illustrative example. Consider a problem whose solution is to be represented with a permutation of length L of the integers {0, 1, ..., (L-1)}. For the sake of this example, let L=4. Thus, we are searching for a permutation of the integers {0, 1, 2, 3}. We will iteratively build up a partial permutation containing a subset of the elements into a complete permutation.

We begin with an empty partial permutation: p = []. The first iteration will select the first element. To do so, we use a heuristic to evaluate each option within the context of the problem. Let S be the set of elements not yet in the permutation, which in this case is initially S = {0, 1, 2, 3}. Let h(p, e) be a constructive heuristic that takes as input the current partial permutation p, and an element e from S under consideration for addition to p, and which produces a real value as output that increases as the importance of adding e to p increases. That is, higher values of the heuristic imply that the heuristic has a higher level of confidence that the element e should be added next to p. For the sake of this example, consider that the heuristic values are as follows: h([], 0) = 5, h([], 1) = 7, h([], 2) = 2, and h([], 3) = 1. The heuristic seems to favor element 1 the most, and has element 0 as its second choice, followed by element 2 as its third choice, and then element 3 is last. HBSS then ranks the choices. Let r(e) be the rank of element e. Thus, we have: r(0) = 2, r(1) = 1, r(2) = 3, and r(3) = 4.

Now, HBSS also uses a bias function b. We will compute b(r(e)) for each element e under consideration for addition to partial permutation p. A common form of the bias function is b(r(e)) = 1 / r(e)a for some exponent a. The reason for the fraction is that lower ranks imply apparently better choice, but we need the bias to be higher for apparently better choices. The greater your confidence is in the decision making ability of the heuristic, the greater the exponent a should be. We'll use a=2 for this example. So given our heuristic values from above, we have the following: b(r(0)) = 1/4 = 0.25, b(r(1)) = 1, b(r(2)) = 1/9 = 0.111, and b(r(3)) = 1/16 = 0.0625.

The element that is added to the partial permutation p is then determined randomly such that the probability P(e) of choosing element e is proportional to b(r(e)). Let's sum the b(r(e)) across the four elements. That sum is approximately 1.4235. In this example, P(0) = 0.25 / 1.4235 = 0.176. P(1) = 1 / 1.4235 = 0.702. P(2) = 0.111 / 1.4235 = 0.078. P(3) = 0.0625 / 1.4235 = 0.044. So with rather high probability, HBSS will end up beginning the permutation with element 1 in this example.

For the sake of the example, let's assume that element 1 was chosen above. We now have partial permutation p = , and the set of elements not yet added to p is S = {0, 2, 3}. We need to compute h(, e) for each of the elements e from S, determine their ranks r, and then compute b(r(e)). For most problems, the heuristic values would have changed, which might cause the ranks of the remaining elements also to change relative to each other. For example, if the problem was the traveling salesperson, then the heuristic might be in terms of the distance from the last city already in the partial permutation (favoring nearby cities). As you move from city to city, which cities are nearest will change. This is why one of the inputs to the heuristic must be the current partial permutation. So let's assume for the example that we recompute the heuristic and get the following values: h(, 0) = 1, h(, 2) = 4, and h(, 3) = 3. The corresponding ranks are: r(0) = 3, r(2) = 1, and r(3) = 2. When we compute the biases, we get: b(r(0)) = 1/9 = 0.111, b(r(2)) = 1, and b(r(3)) = 1/4 = 0.25. The sum of the biases is 1.361. The selection probabilities are thus: P(0) = 0.111 / 1.361 = 0.082; P(2) = 1 / 1.361 = 0.735; and P(3) = 0.25 / 1.361 = 0.184. There is much higher probability of selecting element 2, but for this example consider that element 3 was chosen instead. Thus, p is now p = [1, 3] and S = {0, 2}.

One final decision is needed in this example. Let h([1, 3], 0) = 5, and h([1, 3], 2) = 6, which means r(0) = 2 and r(2) = 1, and further that b(r(0)) = 1/4 = 0.25, and b(r(2)) = 1, which sum to 1.25. The selection probabilities are then: P(0) = 0.25 / 1.25 = 0.2, and P(2) = 1 / 1.25 = 0.8. Let's say that element 2 was chosen, so that now we have p = [0, 3, 2]. Since only one element remains, it is thus added as well to get p = [0, 3, 2, 1]. This permutation is then evaluated with the optimization problem's cost function, and the entire process repeated N times ultimately returning the best (lowest cost) of the N randomly sampled solutions.

To use this implementation of HBSS, you will need to implement a constructive heuristic for your problem using the `ConstructiveHeuristic` interface. The HeuristicBiasedStochasticSampling class also provides a variety of constructors enabling defining the bias function in different ways. The most basic uses the approach of the above example, allowing specifying the exponent, and the default is simply an exponent of 1. The most general allows you to specify any arbitrary bias function using the `HeuristicBiasedStochasticSampling.BiasFunction` interface. Make sure that you remember when implementing a bias for HBSS that lower values of rank imply the presumed better option.

Assuming that the length of the permutation is L, and that the runtime of the constructive heuristic is O(f(L)), the runtime to construct one permutation using HBSS is O(L2 f(L)). If the cost, f(L), to heuristically evaluate one permutation element is simply, O(1), constant time, then the cost to heuristically construct one permutation with HBSS is simply O(L2).

See the following publications for the original description of the HBSS algorithm:
Bresina, J.L. (1996). "Heuristic-Biased Stochastic Sampling." Proceedings of the Thirteenth National Conference on Artificial Intelligence, AAAI Press, pp. 271–278.

• ## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`static interface `
`HeuristicBiasedStochasticSampling.BiasFunction`
Implement this interface to implement the bias function used by HBSS.
• ## Constructor Summary

Constructors
Constructor
Description
`HeuristicBiasedStochasticSampling(ConstructiveHeuristic<T> heuristic)`
Constructs a HeuristicBiasedStochasticSampling search object.
```HeuristicBiasedStochasticSampling(ConstructiveHeuristic<T> heuristic, boolean exponentialBias)```
Constructs a HeuristicBiasedStochasticSampling search object.
```HeuristicBiasedStochasticSampling(ConstructiveHeuristic<T> heuristic, boolean exponentialBias, ProgressTracker<T> tracker)```
Constructs a HeuristicBiasedStochasticSampling search object.
```HeuristicBiasedStochasticSampling(ConstructiveHeuristic<T> heuristic, double exponent)```
Constructs a HeuristicBiasedStochasticSampling search object.
```HeuristicBiasedStochasticSampling(ConstructiveHeuristic<T> heuristic, double exponent, ProgressTracker<T> tracker)```
Constructs a HeuristicBiasedStochasticSampling search object.
```HeuristicBiasedStochasticSampling(ConstructiveHeuristic<T> heuristic, ProgressTracker<T> tracker)```
Constructs a HeuristicBiasedStochasticSampling search object.
```HeuristicBiasedStochasticSampling(ConstructiveHeuristic<T> heuristic, HeuristicBiasedStochasticSampling.BiasFunction bias)```
Constructs a HeuristicBiasedStochasticSampling search object.
```HeuristicBiasedStochasticSampling(ConstructiveHeuristic<T> heuristic, HeuristicBiasedStochasticSampling.BiasFunction bias, ProgressTracker<T> tracker)```
Constructs a HeuristicBiasedStochasticSampling search object.
• ## Method Summary

Modifier and Type
Method
Description
`final Problem<T>`
`getProblem()`
Gets a reference to the problem that this search is solving.
`final ProgressTracker<T>`
`getProgressTracker()`
Gets the `ProgressTracker` object that is in use for tracking search progress.
`final long`
`getTotalRunLength()`
Gets the total run length of the metaheuristic.
`final SolutionCostPair<T>`
`optimize()`
Executes a single run of a metaheuristic whose run length cannot be specified (e.g., a hill climber that terminates when it reaches a local optima, or a stochastic sampler that terminates when it constructs one solution, etc).
`final SolutionCostPair<T>`
`optimize(int numSamples)`
Generates multiple stochastic heuristic samples.
`final void`
`setProgressTracker(ProgressTracker<T> tracker)`
Sets the `ProgressTracker` object that is in use for tracking search progress.
`HeuristicBiasedStochasticSampling<T>`
`split()`
Generates a functionally identical copy of this object, for use in multithreaded implementations of search algorithms.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ## Constructor Details

• ### HeuristicBiasedStochasticSampling

public HeuristicBiasedStochasticSampling(ConstructiveHeuristic<T> heuristic)
Constructs a HeuristicBiasedStochasticSampling search object. A ProgressTracker is created for you. The default bias function returns 1/rank.
Parameters:
`heuristic` - The constructive heuristic.
Throws:
`NullPointerException` - if heuristic is null
• ### HeuristicBiasedStochasticSampling

public HeuristicBiasedStochasticSampling(ConstructiveHeuristic<T> heuristic, ProgressTracker<T> tracker)
Constructs a HeuristicBiasedStochasticSampling search object. The default bias function returns 1/rank.
Parameters:
`heuristic` - The constructive heuristic.
`tracker` - A ProgressTracker
Throws:
`NullPointerException` - if heuristic or tracker is null
• ### HeuristicBiasedStochasticSampling

public HeuristicBiasedStochasticSampling(ConstructiveHeuristic<T> heuristic, boolean exponentialBias)
Constructs a HeuristicBiasedStochasticSampling search object.
Parameters:
`heuristic` - The constructive heuristic.
`exponentialBias` - if true, the bias function evaluates as exp(-rank). if false, the bias function is the default of 1/rank.
Throws:
`NullPointerException` - if heuristic or tracker is null
• ### HeuristicBiasedStochasticSampling

public HeuristicBiasedStochasticSampling(ConstructiveHeuristic<T> heuristic, boolean exponentialBias, ProgressTracker<T> tracker)
Constructs a HeuristicBiasedStochasticSampling search object.
Parameters:
`heuristic` - The constructive heuristic.
`exponentialBias` - if true, the bias function evaluates as exp(-rank). if false, the bias function is the default of 1/rank.
`tracker` - A ProgressTracker
Throws:
`NullPointerException` - if heuristic or tracker is null
• ### HeuristicBiasedStochasticSampling

public HeuristicBiasedStochasticSampling(ConstructiveHeuristic<T> heuristic, double exponent)
Constructs a HeuristicBiasedStochasticSampling search object. A ProgressTracker is created for you.
Parameters:
`heuristic` - The constructive heuristic.
`exponent` - The bias function is defined as: bias(rank) = 1 / pow(rank, exponent).
Throws:
`NullPointerException` - if heuristic is null
• ### HeuristicBiasedStochasticSampling

public HeuristicBiasedStochasticSampling(ConstructiveHeuristic<T> heuristic, double exponent, ProgressTracker<T> tracker)
Constructs a HeuristicBiasedStochasticSampling search object.
Parameters:
`heuristic` - The constructive heuristic.
`exponent` - The bias function is defined as: bias(rank) = 1 / pow(rank, exponent)
`tracker` - A ProgressTracker
Throws:
`NullPointerException` - if heuristic or tracker is null
• ### HeuristicBiasedStochasticSampling

public HeuristicBiasedStochasticSampling(ConstructiveHeuristic<T> heuristic, HeuristicBiasedStochasticSampling.BiasFunction bias)
Constructs a HeuristicBiasedStochasticSampling search object. A ProgressTracker is created for you.
Parameters:
`heuristic` - The constructive heuristic.
`bias` - The bias function. If null, the default bias is used.
Throws:
`NullPointerException` - if heuristic is null
• ### HeuristicBiasedStochasticSampling

public HeuristicBiasedStochasticSampling(ConstructiveHeuristic<T> heuristic, HeuristicBiasedStochasticSampling.BiasFunction bias, ProgressTracker<T> tracker)
Constructs a HeuristicBiasedStochasticSampling search object.
Parameters:
`heuristic` - The constructive heuristic.
`bias` - The bias function. If null, then default bias is used.
`tracker` - A ProgressTracker
Throws:
`NullPointerException` - if heuristic or tracker is null
• ## Method Details

• ### split

public  split()
Description copied from interface: `Splittable`
Generates a functionally identical copy of this object, for use in multithreaded implementations of search algorithms. The state of the object that is returned may or may not be identical to that of the original. Thus, this is a distinct concept from the functionality of the `Copyable` interface. Classes that implement this interface must ensure that the object returned performs the same functionality, and that it does not share any state data that would be either unsafe or inefficient for concurrent access by multiple threads. The split method is allowed to simply return the this reference, provided that it is both safe and efficient for multiple threads to share a single copy of the Splittable object. The intention is to provide a multithreaded search with the capability to provide spawned threads with their own distinct search operators. Such multithreaded algorithms can call the split method for each thread it spawns to generate a functionally identical copy of the operator, but with independent state.
Specified by:
`split` in interface `Metaheuristic<T extends Copyable<T>>`
Specified by:
`split` in interface `SimpleMetaheuristic<T extends Copyable<T>>`
Specified by:
`split` in interface `Splittable<T extends Copyable<T>>`
Returns:
A functionally identical copy of the object, or a reference to this if it is both safe and efficient for multiple threads to share a single instance of this Splittable object.
• ### optimize

public final  optimize()
Description copied from interface: `SimpleMetaheuristic`
Executes a single run of a metaheuristic whose run length cannot be specified (e.g., a hill climber that terminates when it reaches a local optima, or a stochastic sampler that terminates when it constructs one solution, etc). If this method is called multiple times, each call is randomized in some algorithm dependent way (e.g., a hill climber begins at a new randomly generated starting solution), and reinitializes any control parameters that may have changed during the previous call to optimize to the start of run state.
Specified by:
`optimize` in interface `SimpleMetaheuristic<T extends Copyable<T>>`
Returns:
The current solution at the end of this run and its cost, which may or may not be the same as the solution contained in this metaheuristic's `ProgressTracker`, which contains the best of all runs. Returns null if the run did not execute, such as if the ProgressTracker already contains the theoretical best solution.
• ### optimize

public final  optimize(int numSamples)
Generates multiple stochastic heuristic samples. Returns the best solution of the set of samples.
Specified by:
`optimize` in interface `Metaheuristic<T extends Copyable<T>>`
Parameters:
`numSamples` - The number of samples to perform.
Returns:
The best solution of this set of samples, which may or may not be the same as the solution contained in this search's `ProgressTracker`, which contains the best of all runs across all calls to the various optimize methods. Returns null if no runs executed, such as if the ProgressTracker already contains the theoretical best solution.
• ### getProgressTracker

public final  getProgressTracker()
Description copied from interface: `TrackableSearch`
Gets the `ProgressTracker` object that is in use for tracking search progress. The object returned by this method contains the best solution found during the search (including across multiple concurrent runs if the search is multithreaded, or across multiple restarts if the run methods were called multiple times), as well as cost of that solution, among other information. See the `ProgressTracker` documentation for more information about the search data tracked by this object.
Specified by:
`getProgressTracker` in interface `TrackableSearch<T extends Copyable<T>>`
Returns:
the `ProgressTracker` in use by this metaheuristic.
• ### setProgressTracker

public final void setProgressTracker(ProgressTracker<T> tracker)
Description copied from interface: `TrackableSearch`
Sets the `ProgressTracker` object that is in use for tracking search progress. Any previously set ProgressTracker is replaced by this one.
Specified by:
`setProgressTracker` in interface `TrackableSearch<T extends Copyable<T>>`
Parameters:
`tracker` - The new ProgressTracker to set. The tracker must not be null. This method does nothing if tracker is null.
• ### getTotalRunLength

public final long getTotalRunLength()
Description copied from interface: `TrackableSearch`
Gets the total run length of the metaheuristic. This is the total run length across all calls to the search. This may differ from what may be expected based on run lengths. For example, the search terminates if it finds the theoretical best solution, and also immediately returns if a prior call found the theoretical best. In such cases, the total run length may be less than the requested run length.

The meaning of run length may vary from one metaheuristic to another. Therefore, implementing classes should provide fresh documentation rather than relying entirely on the interface documentation.

Specified by:
`getTotalRunLength` in interface `TrackableSearch<T extends Copyable<T>>`
Returns:
the total run length of the metaheuristic
• ### getProblem

public final Problem<T> getProblem()
Description copied from interface: `TrackableSearch`
Gets a reference to the problem that this search is solving.
Specified by:
`getProblem` in interface `TrackableSearch<T extends Copyable<T>>`
Returns:
a reference to the problem.