---
name: "mlpack_neighbor_RASearchRules"
section: "3"
platform: "linux"
package: "mlpack-doc"
version: "1.0.10-1"
release: "Debian 8 (Jessie)"
platforms: ["linux"]
url: "https://new.carta.tech/mlpack_neighbor_RASearchRules"
---

# mlpack\_neighbor\_RASearchRules(3)

## NAME

mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \> -

## SYNOPSIS

### Public Types

typedef \
 **neighbor::NeighborSearchTraversalInfo**\
 \< TreeType \> **TraversalInfoType**"\

### Public Member Functions

**RASearchRules** (const arma::mat &**referenceSet**, const arma::mat &**querySet**, arma::Mat\< size\_t \> &**neighbors**, arma::mat &**distances**, MetricType &**metric**, const double tau=5, const double **alpha**=0.95, const bool naive=false, const bool **sampleAtLeaves**=false, const bool **firstLeafExact**=false, const size\_t **singleSampleLimit**=20)\
 double **BaseCase** (const size\_t queryIndex, const size\_t referenceIndex)\
 size\_t **NumDistComputations** ()\
 size\_t **NumEffectiveSamples** ()\
 double **Rescore** (const size\_t queryIndex, TreeType &referenceNode, const double oldScore)\
 *Re-evaluate the score for recursion order.*  double **Rescore** (TreeType &queryNode, TreeType &referenceNode, const double oldScore)\
 *Re-evaluate the score for recursion order.*  double **Score** (const size\_t queryIndex, TreeType &referenceNode)\
 *Get the score for recursion order.*  double **Score** (const size\_t queryIndex, TreeType &referenceNode, const double baseCaseResult)\
 *Get the score for recursion order.*  double **Score** (TreeType &queryNode, TreeType &referenceNode)\
 *Get the score for recursion order.*  double **Score** (TreeType &queryNode, TreeType &referenceNode, const double baseCaseResult)\
 *Get the score for recursion order, passing the base case result (in the situation where it may be needed to calculate the recursion order).*  const **TraversalInfoType** & **TraversalInfo** () const \
 **TraversalInfoType** & **TraversalInfo** ()\

### Private Member Functions

void **InsertNeighbor** (const size\_t queryIndex, const size\_t pos, const size\_t neighbor, const double distance)\
 *Insert a point into the neighbors and distances matrices; this is a helper function.*  size\_t **MinimumSamplesReqd** (const size\_t n, const size\_t k, const double tau, const double **alpha**) const \
 *Compute the minimum number of samples required to guarantee the given rank-approximation and success probability.*  void **ObtainDistinctSamples** (const size\_t numSamples, const size\_t rangeUpperBound, arma::uvec &distinctSamples) const \
 *Pick up desired number of samples (with replacement) from a given range of integers so that only the distinct samples are returned from the range \[0 - specified upper bound)*  double **Score** (const size\_t queryIndex, TreeType &referenceNode, const double distance, const double bestDistance)\
 *Perform actual scoring for single-tree case.*  double **Score** (TreeType &queryNode, TreeType &referenceNode, const double distance, const double bestDistance)\
 *Perform actual scoring for dual-tree case.*  double **SuccessProbability** (const size\_t n, const size\_t k, const size\_t m, const size\_t t) const \
 *Compute the success probability of obtaining 'k'-neighbors from a set of size 'n' within the top 't' neighbors if 'm' samples are made.*

### Private Attributes

arma::mat & **distances**\
 *The matrix the resultant neighbor distances should be stored in.*  bool **firstLeafExact**\
 *Whether to do exact computation on the first leaf before any sampling.*  MetricType & **metric**\
 *The instantiated metric.*  arma::Mat\< size\_t \> & **neighbors**\
 *The matrix the resultant neighbor indices should be stored in.*  size\_t **numDistComputations**\
 arma::Col\< size\_t \> **numSamplesMade**\
 *The number of samples made for every query.*  size\_t **numSamplesReqd**\
 *The minimum number of samples required per query.*  const arma::mat & **querySet**\
 *The query set.*  const arma::mat & **referenceSet**\
 *The reference set.*  bool **sampleAtLeaves**\
 *Whether to sample at leaves or just use all of it.*  double **samplingRatio**\
 *The sampling ratio.*  size\_t **singleSampleLimit**\
 *The limit on the largest node that can be approximated by sampling.*  **TraversalInfoType** **traversalInfo**\

### Friends

class **RASearch\< SortPolicy, MetricType, TreeType \>**\

## Detailed Description

### template\<typename SortPolicy, typename MetricType, typename TreeType\>class mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>

Definition at line 34 of file ra\_search\_rules.hpp.

## Member Typedef Documentation

### template\<typename SortPolicy , typename MetricType , typename TreeType \> typedef neighbor::NeighborSearchTraversalInfo\<TreeType\> mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::TraversalInfoType

Definition at line 205 of file ra\_search\_rules.hpp.

## Constructor & Destructor Documentation

### template\<typename SortPolicy , typename MetricType , typename TreeType \> mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::RASearchRules (const arma::mat &referenceSet, const arma::mat &querySet, arma::Mat\< size\_t \> &neighbors, arma::mat &distances, MetricType &metric, const doubletau = `5`, const doublealpha = `0.95`, const boolnaive = `false`, const boolsampleAtLeaves = `false`, const boolfirstLeafExact = `false`, const size\_tsingleSampleLimit = `20`)

## Member Function Documentation

### template\<typename SortPolicy , typename MetricType , typename TreeType \> double mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::BaseCase (const size\_tqueryIndex, const size\_treferenceIndex)

### template\<typename SortPolicy , typename MetricType , typename TreeType \> void mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::InsertNeighbor (const size\_tqueryIndex, const size\_tpos, const size\_tneighbor, const doubledistance)` [private]`

Insert a point into the neighbors and distances matrices; this is a helper function.

**Parameters:**

*queryIndex* Index of point whose neighbors we are inserting into. \
 *pos* Position in list to insert into. \
 *neighbor* Index of reference point which is being inserted. \
 *distance* Distance from query point to reference point.

### template\<typename SortPolicy , typename MetricType , typename TreeType \> size\_t mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::MinimumSamplesReqd (const size\_tn, const size\_tk, const doubletau, const doublealpha) const` [private]`

Compute the minimum number of samples required to guarantee the given rank-approximation and success probability.

**Parameters:**

*n* Size of the set to be sampled from. \
 *k* The number of neighbors required within the rank-approximation. \
 *tau* The rank-approximation in percentile of the data. \
 *alpha* The success probability desired.

### template\<typename SortPolicy , typename MetricType , typename TreeType \> size\_t mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::NumDistComputations ()` [inline]`

Definition at line 196 of file ra\_search\_rules.hpp.

References mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::numDistComputations.

### template\<typename SortPolicy , typename MetricType , typename TreeType \> size\_t mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::NumEffectiveSamples ()` [inline]`

Definition at line 197 of file ra\_search\_rules.hpp.

References mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::numSamplesMade.

### template\<typename SortPolicy , typename MetricType , typename TreeType \> void mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::ObtainDistinctSamples (const size\_tnumSamples, const size\_trangeUpperBound, arma::uvec &distinctSamples) const` [private]`

Pick up desired number of samples (with replacement) from a given range of integers so that only the distinct samples are returned from the range \[0 - specified upper bound)

**Parameters:**

*numSamples* Number of random samples. \
 *rangeUpperBound* The upper bound on the range of integers. \
 *distinctSamples* The list of the distinct samples.

### template\<typename SortPolicy , typename MetricType , typename TreeType \> double mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::Rescore (const size\_tqueryIndex, TreeType &referenceNode, const doubleoldScore)

Re-evaluate the score for recursion order. A low score indicates priority for recursion, while DBL\_MAX indicates that the node should not be recursed into at all (it should be pruned). This is used when the score has already been calculated, but another recursion may have modified the bounds for pruning. So the old score is checked against the new pruning bound.

For rank-approximation, it also checks if the number of samples left for a query to satisfy the rank constraint is small enough at this point of the algorithm, then this node is approximated by sampling and given a new score of 'DBL\_MAX'.

**Parameters:**

*queryIndex* Index of query point. \
 *referenceNode* Candidate node to be recursed into. \
 *oldScore* Old score produced by **Score()** (or **Rescore()**).

### template\<typename SortPolicy , typename MetricType , typename TreeType \> double mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::Rescore (TreeType &queryNode, TreeType &referenceNode, const doubleoldScore)

Re-evaluate the score for recursion order. A low score indicates priority for recursion, while DBL\_MAX indicates that the node should not be recursed into at all (it should be pruned). This is used when the score has already been calculated, but another recursion may have modified the bounds for pruning. So the old score is checked against the new pruning bound.

For the rank-approximation, we check if the referenceNode can be approximated by sampling. If it can be, enough samples are made for every query in the queryNode. No further query-tree traversal is performed.

The 'NumSamplesMade' query stat is propagated up the tree. And then if pruning occurs (by distance or by sampling), the 'NumSamplesMade' stat is not propagated down the tree. If no pruning occurs, the stat is propagated down the tree.

**Parameters:**

*queryNode* Candidate query node to recurse into. \
 *referenceNode* Candidate reference node to recurse into. \
 *oldScore* Old score produced by Socre() (or **Rescore()**).

### template\<typename SortPolicy , typename MetricType , typename TreeType \> double mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::Score (const size\_tqueryIndex, TreeType &referenceNode)

Get the score for recursion order. A low score indicates priority for recursion, while DBL\_MAX indicates that the node should not be recursed into at all (it should be pruned).

For rank-approximation, the scoring function first checks if pruning by distance is possible. If yes, then the node is given the score of 'DBL\_MAX' and the expected number of samples from that node are added to the number of samples made for the query.

If no, then the function tries to see if the node can be pruned by approximation. If number of samples required from this node is small enough, then that number of samples are acquired from this node and the score is set to be 'DBL\_MAX'.

If the pruning by approximation is not possible either, the algorithm continues with the usual tree-traversal.

**Parameters:**

*queryIndex* Index of query point. \
 *referenceNode* Candidate node to be recursed into.

### template\<typename SortPolicy , typename MetricType , typename TreeType \> double mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::Score (const size\_tqueryIndex, TreeType &referenceNode, const doublebaseCaseResult)

Get the score for recursion order. A low score indicates priority for recursion, while DBL\_MAX indicates that the node should not be recursed into at all (it should be pruned).

For rank-approximation, the scoring function first checks if pruning by distance is possible. If yes, then the node is given the score of 'DBL\_MAX' and the expected number of samples from that node are added to the number of samples made for the query.

If no, then the function tries to see if the node can be pruned by approximation. If number of samples required from this node is small enough, then that number of samples are acquired from this node and the score is set to be 'DBL\_MAX'.

If the pruning by approximation is not possible either, the algorithm continues with the usual tree-traversal.

**Parameters:**

*queryIndex* Index of query point. \
 *referenceNode* Candidate node to be recursed into. \
 *baseCaseResult* Result of BaseCase(queryIndex, referenceNode).

### template\<typename SortPolicy , typename MetricType , typename TreeType \> double mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::Score (TreeType &queryNode, TreeType &referenceNode)

Get the score for recursion order. A low score indicates priority for recursionm while DBL\_MAX indicates that the node should not be recursed into at all (it should be pruned).

For the rank-approximation, we check if the referenceNode can be approximated by sampling. If it can be, enough samples are made for every query in the queryNode. No further query-tree traversal is performed.

The 'NumSamplesMade' query stat is propagated up the tree. And then if pruning occurs (by distance or by sampling), the 'NumSamplesMade' stat is not propagated down the tree. If no pruning occurs, the stat is propagated down the tree.

**Parameters:**

*queryNode* Candidate query node to recurse into. \
 *referenceNode* Candidate reference node to recurse into.

### template\<typename SortPolicy , typename MetricType , typename TreeType \> double mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::Score (TreeType &queryNode, TreeType &referenceNode, const doublebaseCaseResult)

Get the score for recursion order, passing the base case result (in the situation where it may be needed to calculate the recursion order). A low score indicates priority for recursion, while DBL\_MAX indicates that the node should not be recursed into at all (it should be pruned).

For the rank-approximation, we check if the referenceNode can be approximated by sampling. If it can be, enough samples are made for every query in the queryNode. No further query-tree traversal is performed.

The 'NumSamplesMade' query stat is propagated up the tree. And then if pruning occurs (by distance or by sampling), the 'NumSamplesMade' stat is not propagated down the tree. If no pruning occurs, the stat is propagated down the tree.

**Parameters:**

*queryNode* Candidate query node to recurse into. \
 *referenceNode* Candidate reference node to recurse into. \
 *baseCaseResult* Result of BaseCase(queryIndex, referenceNode).

### template\<typename SortPolicy , typename MetricType , typename TreeType \> double mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::Score (const size\_tqueryIndex, TreeType &referenceNode, const doubledistance, const doublebestDistance)` [private]`

Perform actual scoring for single-tree case.

### template\<typename SortPolicy , typename MetricType , typename TreeType \> double mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::Score (TreeType &queryNode, TreeType &referenceNode, const doubledistance, const doublebestDistance)` [private]`

Perform actual scoring for dual-tree case.

### template\<typename SortPolicy , typename MetricType , typename TreeType \> double mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::SuccessProbability (const size\_tn, const size\_tk, const size\_tm, const size\_tt) const` [private]`

Compute the success probability of obtaining 'k'-neighbors from a set of size 'n' within the top 't' neighbors if 'm' samples are made.

**Parameters:**

*n* Size of the set being sampled from. \
 *k* The number of neighbors required within the rank-approximation. \
 *m* The number of random samples. \
 *t* The desired rank-approximation.

### template\<typename SortPolicy , typename MetricType , typename TreeType \> const TraversalInfoType& mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::TraversalInfo () const` [inline]`

Definition at line 207 of file ra\_search\_rules.hpp.

References mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::traversalInfo.

### template\<typename SortPolicy , typename MetricType , typename TreeType \> TraversalInfoType& mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::TraversalInfo ()` [inline]`

Definition at line 208 of file ra\_search\_rules.hpp.

References mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::traversalInfo.

## Friends And Related Function Documentation

### template\<typename SortPolicy , typename MetricType , typename TreeType \> friend class RASearch\< SortPolicy, MetricType, TreeType \>` [friend]`

Definition at line 323 of file ra\_search\_rules.hpp.

## Member Data Documentation

### template\<typename SortPolicy , typename MetricType , typename TreeType \> arma::mat& mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::distances` [private]`

The matrix the resultant neighbor distances should be stored in.

Definition at line 221 of file ra\_search\_rules.hpp.

### template\<typename SortPolicy , typename MetricType , typename TreeType \> bool mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::firstLeafExact` [private]`

Whether to do exact computation on the first leaf before any sampling.

Definition at line 230 of file ra\_search\_rules.hpp.

### template\<typename SortPolicy , typename MetricType , typename TreeType \> MetricType& mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::metric` [private]`

The instantiated metric.

Definition at line 224 of file ra\_search\_rules.hpp.

### template\<typename SortPolicy , typename MetricType , typename TreeType \> arma::Mat\<size\_t\>& mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::neighbors` [private]`

The matrix the resultant neighbor indices should be stored in.

Definition at line 218 of file ra\_search\_rules.hpp.

### template\<typename SortPolicy , typename MetricType , typename TreeType \> size\_t mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::numDistComputations` [private]`

Definition at line 245 of file ra\_search\_rules.hpp.

Referenced by mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::NumDistComputations().

### template\<typename SortPolicy , typename MetricType , typename TreeType \> arma::Col\<size\_t\> mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::numSamplesMade` [private]`

The number of samples made for every query.

Definition at line 239 of file ra\_search\_rules.hpp.

Referenced by mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::NumEffectiveSamples().

### template\<typename SortPolicy , typename MetricType , typename TreeType \> size\_t mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::numSamplesReqd` [private]`

The minimum number of samples required per query.

Definition at line 236 of file ra\_search\_rules.hpp.

### template\<typename SortPolicy , typename MetricType , typename TreeType \> const arma::mat& mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::querySet` [private]`

The query set.

Definition at line 215 of file ra\_search\_rules.hpp.

### template\<typename SortPolicy , typename MetricType , typename TreeType \> const arma::mat& mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::referenceSet` [private]`

The reference set.

Definition at line 212 of file ra\_search\_rules.hpp.

### template\<typename SortPolicy , typename MetricType , typename TreeType \> bool mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::sampleAtLeaves` [private]`

Whether to sample at leaves or just use all of it.

Definition at line 227 of file ra\_search\_rules.hpp.

### template\<typename SortPolicy , typename MetricType , typename TreeType \> double mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::samplingRatio` [private]`

The sampling ratio.

Definition at line 242 of file ra\_search\_rules.hpp.

### template\<typename SortPolicy , typename MetricType , typename TreeType \> size\_t mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::singleSampleLimit` [private]`

The limit on the largest node that can be approximated by sampling.

Definition at line 233 of file ra\_search\_rules.hpp.

### template\<typename SortPolicy , typename MetricType , typename TreeType \> TraversalInfoType mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::traversalInfo` [private]`

Definition at line 247 of file ra\_search\_rules.hpp.

Referenced by mlpack::neighbor::RASearchRules\< SortPolicy, MetricType, TreeType \>::TraversalInfo().

## Author

Generated automatically by Doxygen for MLPACK from the source code.
