The current implementation of ReactiveMP makes extensive use of global variables. Specifically:
x_tmin = EdgeGaussian("x_tmin")
g_t = TransitionGaussian("g_t")
x_t = EdgeGaussian("x_t")
f_t = LikelihoodGaussian("f_t")
y_t = EdgeDelta("y_t")
are all global variables that later need to be accessed inside different functions, for example inside act
. Moreover, an unconventional way of using the eval()
function is being used to obtain a reference to these to be able to modify them. A general trend in many programming languages, including Julia, is to avoid changing values of global variables inside function:
Another problem with the current implementation of ReactiveMP is that there are duplicate instances of the notions of "edges" and "nodes" in a factor graph: the ones contained inside the MetaGraph
object and the composite types defined by us, like x_tmin
, g_t
, etc.
One way to solve these two issues is to encapsulate all the relevant data of edges and nodes into the MetaGraph
object itself. We can do this by transferring all the fields defined inside the composite types into properties of the MetaGraph instance. One way to do this is by overloading the add_edge!
and add_vertex!
functions. Here is a small example that does this:
using LightGraphs, MetaGraphs, Distributions
import LightGraphs:
add_edge!
"""
Overloads the 'add_edge!' function by accepting an extra 'distribution'
argument that will be stored as a property of the edge.
"""
function add_edge!(
graph::LightGraphs.SimpleGraphs.AbstractSimpleGraph,
distribution::Normal{Float64},
src,
dst)
set_prop!(mg, Edge(src, dst), :type, distribution)
# TODO: add all other properties needed by a FFG edge
add_edge!(graph, src, dst)
end
# Call the overloaded add_edge!
mg = MetaGraph(path_digraph(5))
d = Normal(3, 5)
add_edge!(mg, d, 1, 2)
All the relevant data of the factor graph would be included inside the MetaGraph object which could be easily passed and modified by the different functions without having to rely on global variables. And there will be one instance of edges and nodes.