void BuildBPLayerNode <T>(List <T> sourceLayer, List <BPUnit <T> > targetLayer) where T : Factorable, ILayerNode { foreach (T baseUnit in sourceLayer) { BPUnit <T> unit = new BPUnit <T>(this, baseUnit); targetLayer.Add(unit); Base2BPMapping.Add(baseUnit, unit); } }
void BuildBPLayerNeighbor <T>(List <BPUnit <T> > layer) where T : Factorable, ILayerNode { foreach (BPUnit <T> unit in layer) { foreach (T neighBase in unit.BaseUnit.GetAdjacent()) { BPUnit <T> neigh = Base2BPMapping[neighBase] as BPUnit <T>; unit.Neighbors.Add(neigh); unit.Messages[neigh] = new Dictionary <short, double>(); } } }
double SendMessageTo(BPUnit <T> unit, Dictionary <short, double> message) { Dictionary <short, double> lastMsg; if (unit.Messages.ContainsKey(this)) { lastMsg = unit.Messages[this]; } else { lastMsg = new Dictionary <short, double>(); } unit.Messages[this] = message; if (lastMsg.Count != message.Count) { return(1.0); } return(Enumerable.Zip(lastMsg, message, (first, second) => Math.Abs(first.Value - second.Value)) .Max()); }