AIToolbox
A library that offers tools for AI problem solving.
AIToolbox::Factored::FactorGraph< FactorData > Class Template Reference

This class offers a minimal interface to manager a factor graph. More...

#include <AIToolbox/Factored/Utils/FactorGraph.hpp>

Classes

class  FactorNode
 

Public Types

using Variables = PartialKeys
 
using FactorList = std::list< FactorNode >
 
using FactorIt = typename FactorList::iterator
 
using CFactorIt = typename FactorList::const_iterator
 
using FactorItList = std::vector< FactorIt >
 
using value_type = FactorData
 
using iterator = FactorIt
 
using const_iterator = CFactorIt
 

Public Member Functions

 FactorGraph (size_t variables)
 Basic constructor. More...
 
 FactorGraph (const FactorGraph &other)
 Copy constructor. More...
 
FactorGraphoperator= (const FactorGraph &)=delete
 Deleted assignment operator. More...
 
void reset (size_t variables)
 This function re-initializes the graph from scratch as if it was just being built. More...
 
const FactorItListgetFactors (size_t variable) const
 This function returns all factors adjacent to the given variable. More...
 
const VariablesgetVariables (size_t variable) const
 This function returns all variables adjacent to a factor adjacent to the input variable. More...
 
const VariablesgetVariables (FactorIt factor) const
 This function returns all variables adjacent to the given factor. More...
 
const VariablesgetVariables (CFactorIt factor) const
 This function returns all variables adjacent to the given factor. More...
 
Variables getVariables (const FactorItList &factors) const
 This function returns all variables adjacent to any of the given factors. More...
 
FactorIt getFactor (const Variables &variables)
 This function returns an iterator to a factor adjacent to the given variables. More...
 
void erase (size_t variable)
 This function partially removes an variable from the graph. More...
 
size_t variableSize () const
 This function returns the number of variables still in the graph. More...
 
size_t factorSize () const
 This function returns the number of factors still in the graph. More...
 
FactorIt begin ()
 This function provides an editable iterator to the beginning of the internal factor list. More...
 
CFactorIt begin () const
 This function provides a const iterator to the beginning of the internal factor list. More...
 
CFactorIt cbegin () const
 This function provides a const iterator to the beginning of the internal factor list. More...
 
FactorIt end ()
 This function provides an editable interactor to the end of the internal factor list. More...
 
CFactorIt end () const
 This function provides a const interactor to the end of the internal factor list. More...
 
CFactorIt cend () const
 This function provides a const interactor to the end of the internal factor list. More...
 
size_t bestVariableToRemove (const Factors &F) const
 This function returns the variable which is the cheapest to remove with GenericVariableElimination. More...
 

Detailed Description

template<typename FactorData>
class AIToolbox::Factored::FactorGraph< FactorData >

This class offers a minimal interface to manager a factor graph.

This class allows to store arbitrary data into each factor, and to maintain adjacency lists between the factors and a given number of variables. The interface is intentionally very simple and tries to do very little, in order to allow clients to optimize their use of the graph as much as possible.

This class maintains a single FactorNode for any unique combination of variables. When multiple factors are needed, a single FactorNode containing a vector of data should suffice.

This class can allocate list nodes from an internal static pool; so it is probably not thread-safe.

Template Parameters
FactorDataThe class that is stored for each FactorNode.

Member Typedef Documentation

◆ CFactorIt

template<typename FactorData >
using AIToolbox::Factored::FactorGraph< FactorData >::CFactorIt = typename FactorList::const_iterator

◆ const_iterator

template<typename FactorData >
using AIToolbox::Factored::FactorGraph< FactorData >::const_iterator = CFactorIt

◆ FactorIt

template<typename FactorData >
using AIToolbox::Factored::FactorGraph< FactorData >::FactorIt = typename FactorList::iterator

◆ FactorItList

template<typename FactorData >
using AIToolbox::Factored::FactorGraph< FactorData >::FactorItList = std::vector<FactorIt>

◆ FactorList

template<typename FactorData >
using AIToolbox::Factored::FactorGraph< FactorData >::FactorList = std::list<FactorNode>

◆ iterator

template<typename FactorData >
using AIToolbox::Factored::FactorGraph< FactorData >::iterator = FactorIt

◆ value_type

template<typename FactorData >
using AIToolbox::Factored::FactorGraph< FactorData >::value_type = FactorData

◆ Variables

template<typename FactorData >
using AIToolbox::Factored::FactorGraph< FactorData >::Variables = PartialKeys

Constructor & Destructor Documentation

◆ FactorGraph() [1/2]

template<typename FD >
AIToolbox::Factored::FactorGraph< FD >::FactorGraph ( size_t  variables)

Basic constructor.

This constructor initializes the variable adjacency list with the given number of variables. Variables in this class cannot be added, only removed.

Parameters
variablesThe number of variables with which to start the graph.

◆ FactorGraph() [2/2]

template<typename FD >
AIToolbox::Factored::FactorGraph< FD >::FactorGraph ( const FactorGraph< FactorData > &  other)

Copy constructor.

This constructor simply copies the graph, but also makes sure that the internal stored iterators point to the correct containers.

We do not check the internal FactorData, so make sure it doesn't have pointers or iterators inside!

Parameters
otherThe graph to copy.

Member Function Documentation

◆ begin() [1/2]

template<typename FD >
FactorGraph< FD >::CFactorIt AIToolbox::Factored::FactorGraph< FD >::begin

This function provides an editable iterator to the beginning of the internal factor list.

This function is used in order to allow editing of all the factors.

Returns
An iterator to the beginning of the internal factor range.

◆ begin() [2/2]

template<typename FactorData >
CFactorIt AIToolbox::Factored::FactorGraph< FactorData >::begin ( ) const

This function provides a const iterator to the beginning of the internal factor list.

Returns
A const iterator to the beginning of the internal factor range.

◆ bestVariableToRemove()

template<typename FD >
size_t AIToolbox::Factored::FactorGraph< FD >::bestVariableToRemove ( const Factors F) const

This function returns the variable which is the cheapest to remove with GenericVariableElimination.

The choice is made heuristically, as computing the true best is an NP-Complete problem.

Parameters
FThe value space for each variable.
Returns
The best variable to eliminate.

◆ cbegin()

template<typename FD >
FactorGraph< FD >::CFactorIt AIToolbox::Factored::FactorGraph< FD >::cbegin

This function provides a const iterator to the beginning of the internal factor list.

Returns
A const iterator to the beginning of the internal factor range.

◆ cend()

template<typename FD >
FactorGraph< FD >::CFactorIt AIToolbox::Factored::FactorGraph< FD >::cend

This function provides a const interactor to the end of the internal factor list.

See also
factorsBegin() const
Returns
A const iterator to the end of the internal factor range.

◆ end() [1/2]

template<typename FD >
FactorGraph< FD >::CFactorIt AIToolbox::Factored::FactorGraph< FD >::end

This function provides an editable interactor to the end of the internal factor list.

See also
factorsBegin()
Returns
An iterator to the end of the internal factor range.

◆ end() [2/2]

template<typename FactorData >
CFactorIt AIToolbox::Factored::FactorGraph< FactorData >::end ( ) const

This function provides a const interactor to the end of the internal factor list.

See also
factorsBegin() const
Returns
A const iterator to the end of the internal factor range.

◆ erase()

template<typename FD >
void AIToolbox::Factored::FactorGraph< FD >::erase ( size_t  variable)

This function partially removes an variable from the graph.

This function removes the selected variable, and ALL factors associated with it.

Removing the same variable more than once does not do anything.

Parameters
variableThe variable to be removed.

◆ factorSize()

template<typename FD >
size_t AIToolbox::Factored::FactorGraph< FD >::factorSize

This function returns the number of factors still in the graph.

Returns
The number of factors still in the graph.

◆ getFactor()

template<typename FD >
FactorGraph< FD >::FactorIt AIToolbox::Factored::FactorGraph< FD >::getFactor ( const Variables variables)

This function returns an iterator to a factor adjacent to the given variables.

This function may return an iterator to an already existing factor, or if it didn't exist before, to a newly created one.

This means that it is safe to call this function multiple times with the same input, as only one factor will be created.

As factors are kept in a list, insertion is O(1).

Parameters
variablesThe variables the factor returned should be adjacent of.
Returns
An iterator to the desired factor.

◆ getFactors()

template<typename FD >
const FactorGraph< FD >::FactorItList & AIToolbox::Factored::FactorGraph< FD >::getFactors ( size_t  variable) const

This function returns all factors adjacent to the given variable.

Parameters
variableThe variable to look for.
Returns
A list of iterators pointing at the factors adjacent to the given variable.

◆ getVariables() [1/4]

template<typename FD >
const FactorGraph< FD >::Variables & AIToolbox::Factored::FactorGraph< FD >::getVariables ( CFactorIt  factor) const

This function returns all variables adjacent to the given factor.

Parameters
factorA const iterator to the factor to look for.
Returns
A vector of variables adjacent to the given factor.

◆ getVariables() [2/4]

template<typename FD >
FactorGraph< FD >::Variables AIToolbox::Factored::FactorGraph< FD >::getVariables ( const FactorItList factors) const

This function returns all variables adjacent to any of the given factors.

This function is equivalent to calling the getNeighbors(FactorIt) function multiple times, and merging the results to eliminate duplicates.

Parameters
factorsA list of iterators to the factors to look for.
Returns
A vector of variables adjacent to any of the given factors.

◆ getVariables() [3/4]

template<typename FD >
const FactorGraph< FD >::Variables & AIToolbox::Factored::FactorGraph< FD >::getVariables ( FactorIt  factor) const

This function returns all variables adjacent to the given factor.

Parameters
factorAn iterator to the factor to look for.
Returns
A vector of variables adjacent to the given factor.

◆ getVariables() [4/4]

template<typename FD >
const FactorGraph< FD >::Variables & AIToolbox::Factored::FactorGraph< FD >::getVariables ( size_t  variable) const

This function returns all variables adjacent to a factor adjacent to the input variable.

Parameters
variableThe variable to look for.
Returns
All other variables connected in some way to the input one.

◆ operator=()

template<typename FactorData >
FactorGraph& AIToolbox::Factored::FactorGraph< FactorData >::operator= ( const FactorGraph< FactorData > &  )
delete

Deleted assignment operator.

Since we already provide a copy constructor, this is redundant. The assignment operator provided by default would be broken, so we remove it.

◆ reset()

template<typename FD >
void AIToolbox::Factored::FactorGraph< FD >::reset ( size_t  variables)

This function re-initializes the graph from scratch as if it was just being built.

Parameters
variablesThe number of variables with which to start the graph.

◆ variableSize()

template<typename FD >
size_t AIToolbox::Factored::FactorGraph< FD >::variableSize

This function returns the number of variables still in the graph.

This function returns the number of active variables in the graph (that have not been explicitly eliminated via erase(size_t)).

Returns
The number of variables still in the graph.

The documentation for this class was generated from the following file: