def multiply_potential(self,p1,p2): if p1.ids == p2.ids: newp = self.init_potential(p1.ids) newp.P = p1.P * p2.P return newp if len(p1.ids) >= len(p2.ids): pb = p1; ps = p2 else: pb = p2; ps = p1 assert(allin(ps.ids,pb.ids)) pt = deepcopy(pb) for npi,npv in np.ndenumerate(ps.P): idx = [] for v in pt.ids: if v in ps.ids: idx.append( npi[ps.ids.index(v)] ) else: idx.append( slice(None) ) idx = tuple(idx) pt.P[idx] *= npv pt.P = pt.P/np.sum(pt.P) return pt
def min_clique(self,G,ids): # find the minimum clique in G contains all id in ids candidates = [] for i in range(G.N): nids = G.V[i].ids if allin(ids,nids): candidates.append( (i,len(nids)) ) best = min(candidates,key=lambda x:x[1]) return best[0]
def get_elim_order(g,alg = 'min-fill',preserve=None): assert(not g.digraph) assert(alg in ['min-degree','min-fill']) unmarked = list(range(g.N)) edges = g.get_edges() elim_order = [] cliques = [] while len(unmarked) > 0: cost = [0 for x in unmarked] for i,v in enumerate(unmarked): unmarked_neighbor = list(filter(lambda x: (v,x) in edges,unmarked)) if alg == 'min-degree': cost[i] = len(unmarked_neighbor) if alg == 'min-fill': cost[i] = len(unmarked_neighbor)*(len(unmarked_neighbor)-1)/2 for s,t in utils.halfprod(unmarked_neighbor): if (s,t) in edges: cost[i] -= 1 besti = None bestv = None if preserve == None: besti = cost.index(min(cost)) bestv = unmarked[besti] else: tmp = list(zip(unmarked,cost)) tmp.sort(key = lambda x:x[1]) for v,_ in tmp: children = preserve.find_children(v) marked = list(filter(lambda x:x not in unmarked ,list(range(g.N)))) if utils.allin(children,marked): bestv = v besti = unmarked.index(bestv) break elim_order.append(bestv) best_neighbor = list(filter(lambda x: (bestv,x) in edges,unmarked)) for s,t in utils.diffprod(best_neighbor): if (s,t) not in edges: edges.append( (s,t) ) best_neighbor.append(bestv) cliques.append(best_neighbor) unmarked.pop(besti) return elim_order,cliques
def find_small_clique(X): for i,xi in enumerate(X): for j,xj in enumerate(X): if i!=j and utils.allin(xi.ids,xj.ids): return (i,j) return None