AIToolbox
A library that offers tools for AI problem solving.
AIToolbox::Factored Namespace Reference

Namespaces

 Bandit
 
 MDP
 

Classes

struct  BasisFunction
 This struct represents a basis function. More...
 
struct  BasisMatrix
 This struct represents a basis matrix. More...
 
class  CPSQueue
 This class is used as the priority queue for CooperativePrioritizedSweeping. More...
 
struct  DynamicDecisionNetwork
 This class represents a Dynamic Decision Network with factored actions. More...
 
class  DynamicDecisionNetworkGraph
 This class represents the structure of a dynamic decision network. More...
 
struct  FactoredMatrix2D
 This class represents a factored 2D matrix. More...
 
struct  FactoredVector
 This class represents a factored vector. More...
 
class  FactorGraph
 This class offers a minimal interface to manager a factor graph. More...
 
class  FasterTrie
 This class is a generally faster implementation of a Trie. More...
 
class  FilterMap
 This class is a container which uses PartialFactors as keys. More...
 
class  GenericVariableElimination
 This class represents the Variable Elimination algorithm. More...
 
class  PartialFactorsEnumerator
 This class enumerates all possible values for a PartialFactors. More...
 
class  PartialIndexEnumerator
 This class enumerates the indeces of all combinations where a value is fixed. More...
 
class  Trie
 This class organizes data ids as if in a trie. More...
 

Typedefs

using DDNGraph = DynamicDecisionNetworkGraph
 
using DDN = DynamicDecisionNetwork
 

Enumerations

enum  TagErrors {
  TagErrors::None, TagErrors::NoElements, TagErrors::TooManyElements, TagErrors::IdTooHigh,
  TagErrors::NotSorted, TagErrors::Duplicates
}
 This enum contains all possible errors in a tag. More...
 

Functions

template<typename Factor >
auto buildAdjacencyList (const Action &A, const FactorGraph< Factor > &graph)
 
template<typename Factor >
size_t APSP (const FactorGraph< Factor > &graph)
 This function solves the APSP problem for the provided graph. More...
 
template<typename Factor >
auto buildAdjacencyList (const FactorGraph< Factor > &graph)
 This function computes an adjacency list between the variables of the input graph. More...
 
BasisMatrix backProject (const DDN &ddn, const BasisFunction &bf)
 
FactoredMatrix2D backProject (const DDN &ddn, const FactoredVector &fv)
 
std::pair< TagErrors, size_t > checkTag (const Factors &space, const PartialKeys &tag)
 This function verifies whether a tag is correct w.r.t. a space. More...
 
PartialFactors removeFactor (const PartialFactors &pf, size_t f)
 This function removes the specified factor from the input PartialFactors. More...
 
template<typename Gen >
Factors makeRandomValue (const Factors &space, Gen &rnd)
 This function randomly generates a valid value inside the provided space. More...
 
bool match (const PartialFactors &lhs, const PartialFactors &rhs)
 This function returns whether the common factors in the inputs match in value. More...
 
bool match (const Factors &lhs, const PartialFactors &rhs)
 This function returns whether the common factors in the inputs match in value. More...
 
bool match (const PartialKeys &lhsK, const PartialValues &lhs, const PartialKeys &rhsK, const PartialValues &rhs)
 This function returns whether the common factors in the inputs match in value. More...
 
bool match (const PartialKeys &keys, const Factors &lhs, const Factors &rhs)
 This function checks whether the two input Factors match at the specified ids. More...
 
bool match (const std::vector< std::pair< size_t, size_t >> &matches, const Factors &lhs, const Factors &rhs)
 This function checks whether the two input Factors match at the specified ids. More...
 
void join (size_t S, PartialFactors *lhs, const PartialFactors &rhs)
 This function appends the rhs to the lhs, assuming the original Factor for lhs has S elements. More...
 
Factors join (const Factors &lhs, const Factors &rhs)
 This function creates a new Factor appending rhs to lhs. More...
 
PartialKeys join (size_t S, const PartialKeys &lhs, const PartialKeys &rhs)
 This function appends rhs to lhs, assuming the full Factor for lhs has S elements. More...
 
PartialFactors join (size_t S, const PartialFactors &lhs, const PartialFactors &rhs)
 This function appends rhs to lhs, assuming the full Factor for lhs has S elements. More...
 
void unsafe_join (PartialFactors *lhs, const PartialFactors &rhs)
 This function appends the rhs to the lhs. More...
 
PartialFactors merge (const PartialFactors &lhs, const PartialFactors &rhs)
 This function merges two PartialFactors together. More...
 
PartialValues merge (const PartialKeys &lhsk, const PartialValues &lhs, const PartialKeys &rhsk, const PartialValues &rhs)
 This function merges two PartialValues together, using two PartialKeys as guides. More...
 
PartialKeys merge (const PartialKeys &lhs, const PartialKeys &rhs, std::vector< std::pair< size_t, size_t >> *matches=nullptr)
 This function merges two PartialKeys together. More...
 
size_t factorSpace (const Factors &space)
 This function returns the multiplication of all elements of the input factor. More...
 
size_t factorSpacePartial (const PartialKeys &ids, const Factors &space)
 This function returns the multiplication of all elements of the input factor. More...
 
PartialFactors toPartialFactors (const Factors &f)
 This function converts Factors into the equivalent PartialFactors structure. More...
 
Factors toFactors (size_t F, const PartialFactors &pf)
 This function converts PartialFactors into the equivalent Factors structure. More...
 
Factors toFactors (const Factors &space, size_t id)
 This function converts an index into the equivalent Factors, within the specified factor space. More...
 
void toFactors (const Factors &space, size_t id, Factors *out)
 This function converts an index into the equivalent Factors, within the specified factor space. More...
 
PartialValues toFactorsPartial (const PartialKeys &ids, const Factors &space, size_t id)
 This function converts an index into the equivalent PartialValues of the input keys, within the specified factor space. More...
 
template<typename It >
void toFactorsPartial (It begin, const PartialKeys &ids, const Factors &space, size_t id)
 This function converts an index into the equivalent PartialValues of the input keys, within the specified factor space. More...
 
size_t toIndex (const Factors &space, const Factors &f)
 This function converts the input factor in the input space to an unique index. More...
 
size_t toIndex (const Factors &space, const PartialFactors &f)
 This function converts the input factor in the input space to an unique index. More...
 
size_t toIndexPartial (const PartialKeys &ids, const Factors &space, const Factors &f)
 This function converts the input factor in the input space to an unique index. More...
 
size_t toIndexPartial (const PartialKeys &ids, const Factors &space, const PartialFactors &pf)
 This function converts the input factor in the input space to an unique index. More...
 
size_t toIndexPartial (const Factors &space, const PartialFactors &f)
 This function converts the input factor in the input space to an unique index. More...
 
std::pair< size_t, size_t > toIndexPartialAndSkip (const PartialKeys &ids, const Factors &space, const Factors &f, size_t id)
 This function avoids computing indeces multiple times if only a single index is changing. More...
 

Typedef Documentation

◆ Action

◆ DDN

◆ DDNGraph

◆ Factors

using AIToolbox::Factored::Factors = typedef std::vector<size_t>

◆ PartialAction

◆ PartialFactors

◆ PartialKeys

using AIToolbox::Factored::PartialKeys = typedef std::vector<size_t>

◆ PartialState

◆ PartialValues

using AIToolbox::Factored::PartialValues = typedef std::vector<size_t>

◆ Rewards

◆ State

Enumeration Type Documentation

◆ TagErrors

This enum contains all possible errors in a tag.

  • None: No errors were found
  • NoElements: The tag does not have any element.
  • TooManyElements: The tag has more elements than its associated space.
  • IdTooHigh: The tag contains an id higher than the size of its space.
  • NotSorted: The tag contains an id out of order.
  • Duplicates: The tag contains a repeated id.
See also
checkTag(const Factors &, const PartialKeys &);
Enumerator
None 
NoElements 
TooManyElements 
IdTooHigh 
NotSorted 
Duplicates 

Function Documentation

◆ APSP()

template<typename Factor >
size_t AIToolbox::Factored::APSP ( const FactorGraph< Factor > &  graph)

This function solves the APSP problem for the provided graph.

This function computes the graph diameter; i.e. the shortest longest path between any two variable nodes. Factor nodes are treated as multi-edges for this purpose, so they do not count as actual nodes and they do not (directly) contribute to the diameter size.

This function can be used to compute the iteration parameter for MaxPlus, as the number of message iterations needed should be the same as the diameter of the graph.

Parameters
graphThe graph to compute the diameter for.
Returns
The diameter of the graph.

◆ backProject() [1/2]

BasisMatrix AIToolbox::Factored::backProject ( const DDN ddn,
const BasisFunction bf 
)

◆ backProject() [2/2]

FactoredMatrix2D AIToolbox::Factored::backProject ( const DDN ddn,
const FactoredVector fv 
)

◆ buildAdjacencyList() [1/2]

template<typename Factor >
auto AIToolbox::Factored::buildAdjacencyList ( const Action A,
const FactorGraph< Factor > &  graph 
)

◆ buildAdjacencyList() [2/2]

template<typename Factor >
auto AIToolbox::Factored::buildAdjacencyList ( const FactorGraph< Factor > &  graph)

This function computes an adjacency list between the variables of the input graph.

This function returns a vector with one element for each variable. Each variable's element is itself a vector containing the indeces of all neighbors of the variable. Two variables are neighbors if they are connected to at least one common factor.

Parameters
graphThe graph to compute the adjacency list for.
Returns
The adjacency list of the graph.

◆ checkTag()

std::pair<TagErrors, size_t> AIToolbox::Factored::checkTag ( const Factors space,
const PartialKeys tag 
)

This function verifies whether a tag is correct w.r.t. a space.

This function does a series of basic checks on the input tag, to see whether it was initialized correctly with respect to the input space.

This function is useful to do some checking on your models to make sure that there are no errors.

Parameters
spaceThe space associated with the tag.
tagThe tag to verify.
Returns
The first error encountered, and, if applicable, the position where the error was found.

◆ dot()

BasisFunction AIToolbox::Factored::dot ( const Factors space,
const BasisFunction lhs,
const BasisFunction rhs 
)

◆ factorSpace()

size_t AIToolbox::Factored::factorSpace ( const Factors space)

This function returns the multiplication of all elements of the input factor.

In case the factor space is too big to represent via a size_t, the maximum possible representable value is returned.

Parameters
spaceThe factored factor space.
Returns
The possible number of factors if representable, otherwise the max size_t.

◆ factorSpacePartial()

size_t AIToolbox::Factored::factorSpacePartial ( const PartialKeys ids,
const Factors space 
)

This function returns the multiplication of all elements of the input factor.

This function only takes into account the input ids.

In case the factor space is too big to represent via a size_t, the maximum possible representable value is returned.

See also
factorSpace
Parameters
idsThe indeces to consider.
spaceThe factored factor space.
Returns
The possible number of factors if representable, otherwise the max size_t.

◆ join() [1/4]

Factors AIToolbox::Factored::join ( const Factors lhs,
const Factors rhs 
)

This function creates a new Factor appending rhs to lhs.

Parameters
lhsThe left hand side.
rhsThe right hand side.
Returns
A new Factor containing all elements from lhs and rhs.

◆ join() [2/4]

PartialFactors AIToolbox::Factored::join ( size_t  S,
const PartialFactors lhs,
const PartialFactors rhs 
)

This function appends rhs to lhs, assuming the full Factor for lhs has S elements.

Parameters
SThe number of factors the full Factor for lhs has.
lhsThe left hand side.
rhsThe right hand side.
Returns
A new PartialFactors with all keys from rhs at the end and shifted by S.

◆ join() [3/4]

PartialKeys AIToolbox::Factored::join ( size_t  S,
const PartialKeys lhs,
const PartialKeys rhs 
)

This function appends rhs to lhs, assuming the full Factor for lhs has S elements.

Parameters
SThe number of factors the full Factor for lhs has.
lhsThe left hand side.
rhsThe right hand side.
Returns
The new joined PartialKeys.

◆ join() [4/4]

void AIToolbox::Factored::join ( size_t  S,
PartialFactors lhs,
const PartialFactors rhs 
)

This function appends the rhs to the lhs, assuming the original Factor for lhs has S elements.

Parameters
SThe number of factors the full Factor for lhs has.
lhsThe left hand side that gets extended in place.
rhsThe right hand side.

◆ makeRandomValue()

template<typename Gen >
Factors AIToolbox::Factored::makeRandomValue ( const Factors space,
Gen &  rnd 
)

This function randomly generates a valid value inside the provided space.

◆ match() [1/5]

bool AIToolbox::Factored::match ( const Factors lhs,
const PartialFactors rhs 
)

This function returns whether the common factors in the inputs match in value.

Parameters
lhsThe left hand side.
rhsThe right hand side.
Returns
True if all factors in common between the inputs match in value, false otherwise.

◆ match() [2/5]

bool AIToolbox::Factored::match ( const PartialFactors lhs,
const PartialFactors rhs 
)

This function returns whether the common factors in the inputs match in value.

Parameters
lhsThe left hand side.
rhsThe right hand side.
Returns
True if all factors in common between the inputs match in value, false otherwise.

◆ match() [3/5]

bool AIToolbox::Factored::match ( const PartialKeys keys,
const Factors lhs,
const Factors rhs 
)

This function checks whether the two input Factors match at the specified ids.

Parameters
keysThe ids to check.
lhsThe left hand side.
rhsThe right hand side.
Returns
Whether the two Factors match at the specified ids.

◆ match() [4/5]

bool AIToolbox::Factored::match ( const PartialKeys lhsK,
const PartialValues lhs,
const PartialKeys rhsK,
const PartialValues rhs 
)

This function returns whether the common factors in the inputs match in value.

This function is equivalent to match(const PartialFactors & lhs, const PartialFactors & rhs). It is provided to avoid having to construct two PartialFactors when not needed.

Parameters
lhsKThe keys of the lhs.
lhsThe values of the lhs.
rhsKThe keys of the rhs.
rhsThe values of the rhs.
Returns
True if all factors in common between the inputs match in value, false otherwise.

◆ match() [5/5]

bool AIToolbox::Factored::match ( const std::vector< std::pair< size_t, size_t >> &  matches,
const Factors lhs,
const Factors rhs 
)

This function checks whether the two input Factors match at the specified ids.

Each check is performed on a pair of ids: one for the left hand side, and its respective one for the right hand side.

See also
merge(const PartialKeys &, const PartialKeys &, std::vector<std::pair<size_t, size_t>> *)
Parameters
matchesThe id pairs to check.
lhsThe left hand side.
rhsThe right hand side.
Returns
Whether the two Factors match at the specified ids.

◆ merge() [1/3]

PartialFactors AIToolbox::Factored::merge ( const PartialFactors lhs,
const PartialFactors rhs 
)

This function merges two PartialFactors together.

This function assumes that all elements in the PartialFactors have different keys. If they have the same keys, the key is inserted once in the output, but its value is unspecified (it will be from one of the two inputs).

Parameters
lhsThe left hand side.
rhsThe right hand side.
Returns
A new PartialFactors containing all keys from both inputs and their respective values.

◆ merge() [2/3]

PartialKeys AIToolbox::Factored::merge ( const PartialKeys lhs,
const PartialKeys rhs,
std::vector< std::pair< size_t, size_t >> *  matches = nullptr 
)

This function merges two PartialKeys together.

This function merges two PartialKeys over the same range. Overlapping elements are merged together.

The function optionally returns a vector which specifies the indeces of the matches in the input. This may be useful to do checks before doing merges.

Parameters
lhsThe left hand side.
rhsThe right hand side.
matchesAn optional vector containing the matching indexes of the inputs
Returns
A new PartialKeys containing all unique keys from the inputs.

◆ merge() [3/3]

PartialValues AIToolbox::Factored::merge ( const PartialKeys lhsk,
const PartialValues lhs,
const PartialKeys rhsk,
const PartialValues rhs 
)

This function merges two PartialValues together, using two PartialKeys as guides.

This function is equivalent to merge(const PartialFactors&, const PartialFactors&), with the only difference that it does not merge the keys.

This function assumes that all elements in the PartialKeys are different. If there are matches, the corrisponding element is inserted once output, but its value is unspecified (it will be from one of the two input PartialValues).

Parameters
lhskThe left hand side keys.
lhsThe left hand side.
rhskThe right hand side keys.
rhsThe right hand side.
Returns
A new PartialValues containing all values from both inputs in the correct order.

◆ minus() [1/3]

BasisFunction AIToolbox::Factored::minus ( const Factors space,
const BasisFunction lhs,
const BasisFunction rhs 
)

◆ minus() [2/3]

FactoredVector AIToolbox::Factored::minus ( const Factors space,
FactoredVector  retval,
const BasisFunction rhs,
bool  clearZero = false 
)

◆ minus() [3/3]

FactoredVector AIToolbox::Factored::minus ( const Factors space,
FactoredVector  retval,
const FactoredVector rhs,
bool  clearZero = false 
)

◆ minusEqual() [1/2]

FactoredVector& AIToolbox::Factored::minusEqual ( const Factors space,
FactoredVector retval,
const BasisFunction basis,
bool  clearZero = false 
)

◆ minusEqual() [2/2]

FactoredVector& AIToolbox::Factored::minusEqual ( const Factors space,
FactoredVector retval,
const FactoredVector rhs,
bool  clearZero = false 
)

◆ minusEqualSubset()

BasisFunction& AIToolbox::Factored::minusEqualSubset ( const Factors space,
BasisFunction retval,
const BasisFunction rhs 
)

◆ minusSubset()

BasisFunction AIToolbox::Factored::minusSubset ( const Factors space,
BasisFunction  retval,
const BasisFunction rhs 
)

◆ operator*() [1/8]

FactoredMatrix2D AIToolbox::Factored::operator* ( const double  v,
FactoredMatrix2D  rhs 
)

◆ operator*() [2/8]

FactoredVector AIToolbox::Factored::operator* ( const double  v,
FactoredVector  rhs 
)

◆ operator*() [3/8]

FactoredMatrix2D AIToolbox::Factored::operator* ( const Vector w,
FactoredMatrix2D  rhs 
)

◆ operator*() [4/8]

FactoredVector AIToolbox::Factored::operator* ( const Vector w,
FactoredVector  rhs 
)

◆ operator*() [5/8]

FactoredMatrix2D AIToolbox::Factored::operator* ( FactoredMatrix2D  lhs,
const double  v 
)

◆ operator*() [6/8]

FactoredMatrix2D AIToolbox::Factored::operator* ( FactoredMatrix2D  lhs,
const Vector w 
)

◆ operator*() [7/8]

FactoredVector AIToolbox::Factored::operator* ( FactoredVector  lhs,
const double  v 
)

◆ operator*() [8/8]

FactoredVector AIToolbox::Factored::operator* ( FactoredVector  lhs,
const Vector w 
)

◆ plus() [1/4]

BasisFunction AIToolbox::Factored::plus ( const Factors space,
const BasisFunction lhs,
const BasisFunction rhs 
)

◆ plus() [2/4]

BasisMatrix AIToolbox::Factored::plus ( const Factors space,
const Factors actions,
const BasisMatrix lhs,
const BasisMatrix rhs 
)

◆ plus() [3/4]

FactoredVector AIToolbox::Factored::plus ( const Factors space,
FactoredVector  retval,
const BasisFunction rhs 
)

◆ plus() [4/4]

FactoredVector AIToolbox::Factored::plus ( const Factors space,
FactoredVector  retval,
const FactoredVector rhs 
)

◆ plusEqual() [1/8]

FactoredMatrix2D& AIToolbox::Factored::plusEqual ( const Factors space,
const Factors actions,
FactoredMatrix2D retval,
BasisMatrix &&  basis 
)

◆ plusEqual() [2/8]

FactoredMatrix2D& AIToolbox::Factored::plusEqual ( const Factors space,
const Factors actions,
FactoredMatrix2D retval,
const BasisMatrix basis 
)

◆ plusEqual() [3/8]

FactoredMatrix2D& AIToolbox::Factored::plusEqual ( const Factors space,
const Factors actions,
FactoredMatrix2D retval,
const FactoredMatrix2D rhs 
)

◆ plusEqual() [4/8]

FactoredMatrix2D& AIToolbox::Factored::plusEqual ( const Factors space,
const Factors actions,
FactoredMatrix2D retval,
FactoredMatrix2D &&  rhs 
)

◆ plusEqual() [5/8]

FactoredVector& AIToolbox::Factored::plusEqual ( const Factors space,
FactoredVector retval,
BasisFunction &&  basis 
)

◆ plusEqual() [6/8]

FactoredVector& AIToolbox::Factored::plusEqual ( const Factors space,
FactoredVector retval,
const BasisFunction basis 
)

◆ plusEqual() [7/8]

FactoredVector& AIToolbox::Factored::plusEqual ( const Factors space,
FactoredVector retval,
const FactoredVector rhs 
)

◆ plusEqual() [8/8]

FactoredVector& AIToolbox::Factored::plusEqual ( const Factors space,
FactoredVector retval,
FactoredVector &&  rhs 
)

◆ plusEqualSubset() [1/2]

BasisFunction& AIToolbox::Factored::plusEqualSubset ( const Factors space,
BasisFunction retval,
const BasisFunction rhs 
)

◆ plusEqualSubset() [2/2]

BasisMatrix& AIToolbox::Factored::plusEqualSubset ( const Factors space,
const Factors actions,
BasisMatrix retval,
const BasisMatrix rhs 
)

◆ plusSubset() [1/2]

BasisFunction AIToolbox::Factored::plusSubset ( const Factors space,
BasisFunction  retval,
const BasisFunction rhs 
)

◆ plusSubset() [2/2]

BasisMatrix AIToolbox::Factored::plusSubset ( const Factors space,
const Factors actions,
BasisMatrix  retval,
const BasisMatrix rhs 
)

◆ removeFactor()

PartialFactors AIToolbox::Factored::removeFactor ( const PartialFactors pf,
size_t  f 
)

This function removes the specified factor from the input PartialFactors.

Parameters
pfThe PartialFactors to modify.
fThe factor to be removed.
Returns
A new PartialFactors that does not contain the input factor.

◆ toFactors() [1/3]

Factors AIToolbox::Factored::toFactors ( const Factors space,
size_t  id 
)

This function converts an index into the equivalent Factors, within the specified factor space.

This function is the inverse of the toIndex(const Factors &, size_t) function.

The input id shall not cause the output to exceed the input space (i.e. the id will always be lower than factorSpace(space)).

Parameters
spaceThe factor space to consider.
idThe integer uniquely identifying the factor.
Returns
The id's equivalent Factors.

◆ toFactors() [2/3]

void AIToolbox::Factored::toFactors ( const Factors space,
size_t  id,
Factors out 
)

This function converts an index into the equivalent Factors, within the specified factor space.

This function is equivalent to toFactors(const Factors & space, size_t id), but does its work on the input Factors.

Note: this function does NOT check for nullptr.

Parameters
spaceThe factor space to consider.
idThe integer uniquely identifying the factor.
outThe id's equivalent Factors.

◆ toFactors() [3/3]

Factors AIToolbox::Factored::toFactors ( size_t  F,
const PartialFactors pf 
)

This function converts PartialFactors into the equivalent Factors structure.

If the PartialFactors are incomplete, the non-specified elements will be unspecified in the returned value as well. The PartialFactors will not contain a factor higher than what passed as input.

Parameters
FThe size of the Factors to be returned.
pfThe PartialFactors to be converted.
Returns
Factors containing all values of the input.

◆ toFactorsPartial() [1/2]

PartialValues AIToolbox::Factored::toFactorsPartial ( const PartialKeys ids,
const Factors space,
size_t  id 
)

This function converts an index into the equivalent PartialValues of the input keys, within the specified factor space.

This function is the inverse of the toIndexPartial(const PartialKeys &, const Factors &, const Factors &) function, but only generates the PartialValues for the keys (rather than a full Factors).

The input id shall not cause the output to exceed the input space (i.e. the id will always be lower than factorSpacePartial(ids, space)).

Parameters
idsThe indeces to consider.
spaceThe global factors space to consider.
idThe integer uniquely identifying the factor.
Returns
The id's equivalent PartialValues (same length as the input ids).

◆ toFactorsPartial() [2/2]

template<typename It >
void AIToolbox::Factored::toFactorsPartial ( It  begin,
const PartialKeys ids,
const Factors space,
size_t  id 
)

This function converts an index into the equivalent PartialValues of the input keys, within the specified factor space.

See also
PartialValues toFactorsPartial(const PartialKeys & ids, const Factors & space, size_t id);

This functioh outputs to a range beginning at begin, of the same size as the input ids.

Parameters
beginThe beginning of the range where to write the output.
idsThe indeces to consider.
spaceThe global factors space to consider.
idThe integer uniquely identifying the factor.

◆ toIndex() [1/2]

size_t AIToolbox::Factored::toIndex ( const Factors space,
const Factors f 
)

This function converts the input factor in the input space to an unique index.

This function returns an unique integer in range [0, factorSpace(space)).

The conversion guarantees that the output can be converted back to the same Factors via the toFactors functions, and that the relative ordering of the ids is the same as the one iterated by the PartialFactorsEnumerator.

In particular, iterating over factors is always done from the lowest id first. So for example in a space (2,3), the equivalency is:

(0,0) -> 0 (1,0) -> 1 (0,1) -> 2 (1,1) -> 3 (0,2) -> 4 (1,2) -> 5

This function does not perform bound checking. If the resulting number is too big to be representable via a size_t, the behavior is unspecified. This can happen only if factorSpace(space) exceeds the size of a size_t variable.

Parameters
spaceThe factor space to consider.
fThe input factor to convert.
Returns
An integer which uniquely identifies the factor in the factor space.

◆ toIndex() [2/2]

size_t AIToolbox::Factored::toIndex ( const Factors space,
const PartialFactors f 
)

This function converts the input factor in the input space to an unique index.

See also
toIndex(const Factors &, const Factors &)

All unspecified values are considered 0.

Parameters
spaceThe factor space to consider.
fThe input factor to convert.
Returns
An integer which uniquely identifies the factor in the factor space.

◆ toIndexPartial() [1/3]

size_t AIToolbox::Factored::toIndexPartial ( const Factors space,
const PartialFactors f 
)

This function converts the input factor in the input space to an unique index.

In this function only the ids mentioned in the PartialFactors are considered to be part of the space.

See also
toIndexPartial(const PartialKeys &, const Factors &, const Factors &);
Parameters
spaceThe factor space to consider.
fThe input factor to convert.
Returns
An integer which uniquely identifies the factor in the factor space for the factor's ids.

◆ toIndexPartial() [2/3]

size_t AIToolbox::Factored::toIndexPartial ( const PartialKeys ids,
const Factors space,
const Factors f 
)

This function converts the input factor in the input space to an unique index.

In this method ONLY the ids passed as input are considered. This function effectively considers only a subset of the input space and factor (which should still have the same length).

So if the ids are {1, 3}, the function will behave as if the factor space is two-dimensional, taking the values at ids 1 and 3 from the input space.

Then it will take the values at ids 1 and 3 from the input factor and use them to compute the equivalent number.

See also
toIndex(const Factors &, const Factors &)
Parameters
idsThe ids to consider in the input space and factor.
spaceThe factor space to consider.
fThe input factor to convert.
Returns
An integer which uniquely identifies the factor in the factor space for the specified ids.

◆ toIndexPartial() [3/3]

size_t AIToolbox::Factored::toIndexPartial ( const PartialKeys ids,
const Factors space,
const PartialFactors pf 
)

This function converts the input factor in the input space to an unique index.

In this method ONLY the ids passed as input are considered. This function effectively considers only a subset of the input space and partial factor. The partial factor MUST contain the ids passed as input!

So if the ids are {1, 3}, the function will behave as if the factor space is two-dimensional, taking the values at ids 1 and 3 from the input space.

Then it will take the values at keys 1 and 3 from the input partial factor and use them to compute the equivalent number.

See also
toIndex(const Factors &, const Factors &)
Parameters
idsThe ids to consider in the input space and factor.
spaceThe factor space to consider.
pfThe input factor to convert.
Returns
An integer which uniquely identifies the factor in the factor space for the specified ids.

◆ toIndexPartialAndSkip()

std::pair<size_t, size_t> AIToolbox::Factored::toIndexPartialAndSkip ( const PartialKeys ids,
const Factors space,
const Factors f,
size_t  id 
)

This function avoids computing indeces multiple times if only a single index is changing.

If you need to compute a sequence of indeces given that you are iterating over the values of a single key id (and keeping all the rest identical), you can use this function.

It returns the first valid index (the one where the input key id would have value 0, and all the rest of the input value is the same), and a skip value to find all the subsequent ones.

For example, suppose you have a space of

  • space = { 2, 3, 4 } and you are considering all keys, so that ids here is
  • ids = { 0, 1, 2 } Finally, you are interested in values in the form of
  • f = { 1, _, 3 } Note that the value in f at the underscore position is irrelevant as this function does not use it. As the underscore is at the second position, id = 1 here.

This function would output 19, 2.

19 is the index of { 1, 0, 3 }, which is the first value that matches the input f.

Then, skipping by 2, we'd have { 1, 1, 3 } and { 1, 2, 3 }. Note you have to only repeat the process space[id] times to avoid going out of bounds.

This is faster than calling toIndexPartial multiple times since we only go over the inputs once to do the multiplication, and the subsequent iteration is simply summing the skip value to the first index.

Parameters
idsThe ids to consider in the input space and factor.
spaceThe factor space to consider.
fThe input factor to convert.
idThe single id we are interested in iterating over.
Returns

◆ toPartialFactors()

PartialFactors AIToolbox::Factored::toPartialFactors ( const Factors f)

This function converts Factors into the equivalent PartialFactors structure.

Parameters
fThe Factors to be converted.
Returns
A PartialFactors structure equivalent to the input.

◆ unsafe_join()

void AIToolbox::Factored::unsafe_join ( PartialFactors lhs,
const PartialFactors rhs 
)

This function appends the rhs to the lhs.

This function may produce a non-valid PartialFactors. This is useful in case multiple joins must be done in successions, so that the process is more efficient.

Remember to call sort() at the end to make the output valid again.

Parameters
lhsThe left hand side that gets extended in place.
rhsThe right hand side.