def min_fill_in(self, eliminated): min_fill = self.node_number * self.node_number for v in self.adjacent: # for each not eliminated nodes if( v in eliminated): continue neighbour_set = self.adjacent[v].copy() neighbour_set.difference_update(eliminated) # all the not eliminated neighbors fill_in = 0 #print(v, neighbour_set) for neigh in neighbour_set: for other_next in neighbour_set: if(other_next == neigh): continue if(other_next not in self.adjacent[neigh]): fill_in += 1 #print(fill_in) if fill_in < min_fill: min_fill = fill_in node = v if min_fill == 0: break # eliminate the min fill in node neighbor = self.adjacent[node].copy() neighbor.difference_update(eliminated) neighbor.add(node) for pre_clique in self.cliques: if neighbor.intersection(pre_clique.nodes) == neighbor: #neighbor and itself are already provided #print(node, 'not add clique') return node, min_fill clique = Clique(len(neighbor), neighbor) self.add_clique(self.clique_number, clique) self.clique_number += 1 #print(node, 'add clique', neighbor) return node, min_fill
def fit(self, X): clusters = clique.run_clique(X, self.xsi, self.tau) clique.save_to_file(clusters, "../output/clique_output.txt") two_dim_clusters = filter(lambda c: len(c.dimensions) == 2, clusters) labels = np.full(X.shape[0], -1, dtype=np.intp) # assign label to values; lookup X[i] in results_dict for labl, c in enumerate(two_dim_clusters): for index in c.data_point_ids: # Dict miss means that the point is an outlier. # outliers are assigned a -1 label labels[index] = labl self.labels_ = labels return clusters
def add_pairwise(self,clique): new_card = clique.table.size self.variable_cardinality.append(new_card) #print("Z size", new_card) label = self.node_number Z = Clique(1,[label]) self.adjacent[self.node_number] = set() self.node_sharing_cliques[self.node_number] = set() self.node_number += 1 self.add_clique(self.clique_number, Z) table = clique.table.reshape(1,new_card) #print("table:",table[0,2]) #print("origin",clique.table[0,1,0]) #print(table) self.clique_number += 1 self.add_clique_table(self.clique_number -1, table) #print("Z", Z.nodes_list, Z.table) time = new_card for node in clique.nodes_list: labels = [node,label] Z_x= Clique(2,labels) #print(Z_x.nodes_list) self.add_clique(self.clique_number, Z_x) self.clique_number += 1 cardinality = self.variable_cardinality[node] table = np.zeros((cardinality,new_card)) time = time/cardinality y = 0 count = 0 for i in range(new_card): table[y,i] =1 count += 1 if count == time: count = 0; y = (y+1)%cardinality #print(table[2]) self.add_clique_table(self.clique_number -1, table)
def test(self): all_clique = Clique(self.node_number, range(self.node_number)) for clique in self.cliques: all_clique.times(clique, self.variable_cardinality) #print(all_clique.table) all_clique.sum() print('ground truth?') print(all_clique.table)
def main(): # Graph g = GUI() a = GraphGenerator.generate(0, 10, 40) g.addGraph(a, (255, 0, 0), (255, 255, 0)) # q, _ = Clique.findMaxBruteForceUp(a) # g.addGraph(q, (0, 0, 255), (0, 255, 255)) # q, _ = Clique.findMaxBruteForceDown(a) # g.addGraph(q, (0, 0, 255), (0, 255, 255)) q, _ = Clique.findMaxGreedy(a) g.addGraph(q, (0, 0, 255), (0, 255, 255)) g.run()
def compute_clique(self, seed): """ This function stores the logic of clique computation. It adds the point p that maximizes the average similarity to the current clique and does not belong to an already used sequence :param seed: :return: clique """ # Initialize cliques and update available indices clique = Clique(self.crops_dir, self.sim_matrix.shape[0]) clique.add_sample(seed, self.flipvals[seed, seed], self.relative_image_pathes[seed]) self.update_available_indices(clique, seed) idx_to_add = 0 # Add constraint for checking the freq of samples in cliques frq = np.max( [(self.sample_freq / self.sample_freq.sum()) - self.diff_prob, np.zeros(self.sample_freq.shape)], axis=0) idx_avail = np.where(clique.available_indices)[0] while (len(clique.samples) < self.num_samples_per_clique) and ( idx_to_add < idx_avail.shape[0]): idx_avail = np.where(clique.available_indices)[0] # FIXME: here we choose the point that has max similarity to some point in clique. We can get a line structure instead of a compact cluster. search_order = np.max( self.sim_matrix[clique.samples.reshape(-1, 1), idx_avail], axis=0).argsort()[::-1] # FIXME: BUG? idx_to_add must be always 0. Because on each step idx_avail will be changed. # FIXME: So now we don't get the best cliques p = idx_avail[search_order[idx_to_add]] if p in clique.samples: pass # Add constraint for freq samples if random from normal distribution is higher than the freq with which # sample p has been sampled then add it elif frq[p] < self.random_state.rand(): f = self.calculate_flip(clique, p) clique.add_sample(p, f, self.relative_image_pathes[p]) self.update_available_indices(clique, p) idx_to_add += 1 return clique
evidence = [None, None, None] root_idx = 2 tester_graph = Graph(node_list, ns, edge_u, edge_v, evidence, root_idx) tester_graph.print_adj_matrix() #define cliques phi_1 = np.array([0.9, 0.1]) phi_one = np.array([1, 1]) phi_12 = np.array([[0.9, 0.1], [0.2, 0.8]]) phi_23 = np.array([[0.9, 0.1], [0.7, 0.3]]) phi_2 = phi_one phi_3 = phi_one # all single nodes and all pairs of nodes connected by edge clique_nodes = [[0], [1], [2], [0, 1], [1, 2]] cliques = [] cliques.append(Clique(0, clique_nodes[0], np.array([2]), phi_1)) cliques.append(Clique(1, clique_nodes[1], np.array([2]), phi_2)) cliques.append(Clique(2, clique_nodes[2], np.array([2]), phi_3)) cliques.append(Clique(3, clique_nodes[3], np.array([2, 2]), phi_12)) cliques.append(Clique(4, clique_nodes[4], np.array([2, 2]), phi_23)) #model tester_model = Model(tester_graph, cliques, evidence) tester_model.print_model() #""Execute sum_product algorithm"" tester_model.sum_product() #Print out the marginal probability of each node. #marginal = net.marginal_nodes([C]) #print 'Probability it is cloudy: ', marginal.T[1]*100, '%' #marginal = net.marginal_nodes([S]) #print 'Probability the sprinkler is on: ', 0, '%' #Observed node #marginal = net.marginal_nodes([R])
def file_reader(file_name, exact_method=True): """Parse the graph structure and the look up table from the uai and evid file""" dir = os.path.dirname(os.path.realpath(__file__)) ### read the uai file with open(dir + '/uai/' + file_name) as f: line = f.readline() graph_type = line.strip('\n') #print (graph_type) node_number = int(f.readline()) #print (node_number) line = f.readline().strip(' \n').strip(' ') try: line2 = line.split(' ') variable_cardinality = list(map(int, line2)) except Exception as e: line2 = line.split(" ") variable_cardinality = list(map(int, line2)) #print (variable_cardinality) clique_number = int(f.readline()) #print (clique_number) graph = Graph(graph_type, node_number, variable_cardinality, clique_number) for i in range(clique_number): # start from 0 line = f.readline().strip('\n').strip(' ') #print(line) try: line = np.array(list(map(int, line.split('\t')))) except Exception as e: line = np.array(list(map(int, line.split(' ')))) # print(line) size = line[0] # print(size) nodes = list(line[1:]) # print(nodes) clique = Clique(size, nodes) graph.add_clique(i, clique) for i in range(clique_number): line = f.readline().strip('\n') if line == '': line = f.readline().strip('\n') table_size = int(line) # print(table_size) read = 0 psi = [] while read < table_size: line = f.readline().strip('\n').strip(' ').split(' ') read = read + len(line) psi.append(list(map(float, line))) graph.add_clique_table(i, psi) ### read the evidence file """ with open(dir + '/uai/' + file_name +'.evid') as evidence: line = evidence.readline().strip('\n').strip(' ') try: line = np.array(list(map(int,line.split('\t')))) except Exception as e: line = np.array(list(map(int,line.split(' ')))) evid_size = line[0] if evid_size != 0: evidence = {} for i in range(0, evid_size): evidence[line[2*i+1]] = line[2*i+2] graph.set_evidence(evidence) """ # test for the parse #print(graph.adjacent[1]) #for v in graph.node_sharing_cliques[1]: #print(v.table) #print(graph.evidence) # graph.triangulation() # graph.maxcliques() # # # if exact_method: start = time.clock() #print('test result') #graph.test() JT = graph.generate_JT() JT.traverse() elapsed = (time.clock() - start) print("Time used:", elapsed, 's') else: ####################### #Here to run the LBP ###################### start = time.clock() graph.pairwise() factorgraph = FactorGraph(graph) factorgraph.LBP(normalize=True) factorgraph.calculate_z() print("Time used", time.clock() - start, 's')