using System.Collections.Generic;
using System.Linq;
namespace Moserware.Skills.FactorGraphs
{
///
/// Helper class for computing the factor graph's normalization constant.
///
public class FactorList
{
private readonly List> _List = new List>();
public double LogNormalization
{
get
{
_List.ForEach(f => f.ResetMarginals());
double sumLogZ = 0.0;
for (int i = 0; i < _List.Count; i++)
{
Factor f = _List[i];
for (int j = 0; j < f.NumberOfMessages; j++)
{
sumLogZ += f.SendMessage(j);
}
}
double sumLogS = _List.Aggregate(0.0, (acc, fac) => acc + fac.LogNormalization);
return sumLogZ + sumLogS;
}
}
public int Count
{
get { return _List.Count; }
}
public Factor AddFactor(Factor factor)
{
_List.Add(factor);
return factor;
}
}
}