def perform_GHC(self): current_solution = self._bayesian_network self._best_score = current_score = self._get_score(current_solution) # draw(self._bayesian_network) # plt.show() print 'Initial score :', self._best_score undirected_graph = current_solution.to_undirected() change_count = 0 max_count = self._max_change_count print max_count while True: # Pick a random edge and decide the best action to be # applied on the edge random_edge = GraphUtils.get_random_edge(self._node_names) # print random_edge, ' is the edge selected' current_score, current_solution = \ self._get_best_local_solution(current_solution, undirected_graph, random_edge) undirected_graph = current_solution.to_undirected() if current_score > self._best_score: change_count = 0 # Update the new best solution self._best_solution = deepcopy(current_solution) self._best_score = current_score print '-----------', self._best_score , '------------------' else: change_count += 1 self._add_solution_to_tabu_list(current_solution) if change_count == max_count: break
def get_bayesian_networks(self): bayesian_network = GraphUtils.read_graph(self._network_name + '-' + str(self._max_parents)) bayesian_networks = [] if bayesian_network == None: # Generate a tree (graph) with required number of nodes. bayesian_network = self._generate_initial_graph() bayesian_network.name = self._network_name + '-' + str(self._max_parents) GraphUtils.write_graph(bayesian_network) # theoretical bound is infinity but this also does well num_iterations = 4 * self._num_nodes * self._num_nodes # Since connectedness is only defined for undirected graphs # so we have to keep a copy of the bayesian network # except that all the edges are undirected undirected_BN = bayesian_network.to_undirected() bayesian_network.name = self._network_name # Repeat for a large number of times. for i in xrange(self._num_BNs): count, i, j = 0, 0, 0; while count < num_iterations: edge = (i, j) = GraphUtils.get_random_edge(self._node_names) if bayesian_network.has_edge(*edge): # If (i,j) is in the graph, remove it GraphUtils.apply_action(bayesian_network, undirected_BN, edge, 'remove', self._max_parents) else: # If the edge (i,j) is not in the graph, add it. GraphUtils.apply_action(bayesian_network, undirected_BN, edge, 'add', self._max_parents) count += 1 bayesian_networks.append(deepcopy(bayesian_network)) # Return the obtained graph return bayesian_networks