def _eliminate_edges(self, Sep):
     num_nodes = len(self._values_dict.keys())
     graph = complete_graph(num_nodes, Graph())
     self._graph = GraphUtils.rename_nodes(graph, self._node_names)
     n = 0
     max_allowed_degree = settings.networks_settings['genome']['max_allowed_degree']
     while n <= 3:
         print '--------------------------------------------------------'
         #  We repeat the iterations unless each node X has
         #  less than or equal to n neighbors
         for X in self._graph:
             for Y in self._graph:
                 if X != Y and GraphUtils.is_degree_greater(self._graph, max_allowed_degree) \
                 and is_connected(self._graph):
                     #  all the neighbors of X excluding Y
                     neighbors = self._graph.neighbors(X)
                     if Y in neighbors:
                         neighbors.remove(Y)
                         #  We only consider X,Y if #neighbors of X excluding Y are more than
                         #  or equal to n
                         if len(neighbors) >= n:
                             #  Combinations of all the adjacent nodes of X excluding Y
                             #  each subset in the observed_sets has cardinality 'n'
                             observed_subsets = combinations(neighbors, n)
                             for S in observed_subsets:
                                 #  We only consider the subsets which have exactly
                                 S = [s for s in sorted(S)]
                                 are_deseparated = self._are_dseparated(X, Y, S, n)
                                 if are_deseparated:
                                     if self._graph.has_edge(X, Y):
                                         self._graph.remove_edge(X, Y)
                                         print 'Removed', X, '-', Y
                                         Sep[X + ',' + Y] = S
                                         Sep[Y + ',' + X] = S
         n += 1
 def test():
     generator = RandomBNGenerator('cancer', 3, max_parents = 2);
     bns = generator.get_bayesian_networks()
     for bn in bns:
         print bn.nodes()
         print GraphUtils.has_cycle(bn)
         draw(bn)
         plt.show()
Beispiel #3
0
def main():
    # Creating a random unidirectional graph
    graph = GraphUtils.generate_graph(10, 25)

    # Creating a list of random colors
    colors = GraphUtils.generate_colors(10)

    # Execute the 'FirsFit' heuristic on the generated graph
    graph_color = FirstFit.execute(graph, colors)

    # Displaying the graph inside file
    GraphUtils.display_graph(graph, graph_color, "FirstFit")
    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
    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_feasible_local_solutions(self, bayesian_network, undirected_graph, edge):
        local_solutions_action_pairs = []
        #  Calculate all possible local solutions by applying
        #  all the possible actions.
        temp_bn = deepcopy(bayesian_network)
        temp_graph = deepcopy(undirected_graph)
        for action in self._actions_list:
            #  print action + 'ing', edge, ' in ', bayesian_network.edges()
            is_feasible = GraphUtils.apply_action(temp_bn, temp_graph, (edge), action, 2)

            if not is_feasible:
                #  If the action was not feasible  then try again
                #  print 'Infeasible action.. trying with different action'
                continue

            if self._tabu_list_contains(temp_bn):
                #  If generated solution is already in the tabu list then try again
                #  print 'Solution already in tabu list trying again'
                continue
            #  print 'Got ', temp_bn.edges()
            local_solutions_action_pairs.append((temp_bn, action))
            temp_bn = deepcopy(bayesian_network)
            temp_graph = deepcopy(undirected_graph)
        return local_solutions_action_pairs
 def get_skeleton(self):
     self._graph = GraphUtils.convert_to_directed(self._graph)
     self._graph.name = self._network_name
     return self._graph