예제 #1
0
    def mssg(self, from_v, to_w, isMax=False):
        # collect all mssg arriving at v
        mess = []
        neighbors = self.adj[from_v]
        for n in neighbors:
            if n!=to_w:
                pos = self.adj[n].index(from_v)
                msg = self.delta[n][pos]
                mess.append(msg)

        # take the the initial Psi (and log if needed)
        d = copy.copy(self.factors[from_v])
        if isMax==True:
            d.values = np.log(d.values)

        # multiply/sum by incoming messages
        for ms in mess:
            if isMax==True:
                d = FactorOperations.sum(d, ms, False)
            else:
                d = FactorOperations.multiply(d, ms, True)

        # marginalized to setsep vars
        for n in d.variables:
            if n not in (self.box[from_v] & self.box[to_w]):
                if isMax==True:
                    d = FactorOperations.max_marginalize(d, n)
                else:
                    d = FactorOperations.marginalize(d, n)
        return d
예제 #2
0
 def calibrate(self, isMax=False):
     self.beta = [None]*self.V
     # compute messages
     for e in self.computePath():
         from_v, to_w = e
         pos_to = self.adj[from_v].index(to_w)
         self.delta[from_v][pos_to] = self.mssg(from_v, to_w, isMax)
     
     # compute the beliefs
     for v in range(self.V):
         belief = copy.copy(self.factors[v])
         if isMax==True:
             belief.values = np.log(belief.values)
         for w in self.adj[v]:
             pos = self.adj[w].index(v)
             delta = self.delta[w][pos]
             if isMax==True:
                 belief = FactorOperations.sum(belief, delta, False)
             else:
                 belief = FactorOperations.multiply(belief, delta, False)
         self.beta[v] = belief