Esempio n. 1
0
 def get_transition_graph(self):
     g = gum.DiGraph()
     for i in range(self.__len__()):
         g.addNode()  #on ajoute autant de noeud qu'il n y a d'états
     mat = self.get_transition_matrix()
     for i in range(len(mat)):
         l = mat[i]  #une ligne de la matrice
         for j in range(len(l)):
             p = l[j]  #on recupere la proba correspondante dans la matrice
             if (p != 0):
                 g.addArc(
                     i, j
                 )  #si la proba n'est pas null on ajoute un arc entre les deux etats
     return g
Esempio n. 2
0
File: CdM.py Progetto: totogs/3I005
    def get_transition_graph(self):
        #creer un graph oriente
        g = gum.DiGraph()
        #creer autant de noeuds qu'il y a d'etats
        for i in range(len(self.get_states())):
            # g.addNodeWithId(i+1)
            g.addNode()

        #recup la matrice de transitions
        matrix = self.get_transition_matrix()
        #ajouter les transitions au graph
        for i in range(len(matrix)):
            for j in range(len(matrix[i])):
                c = matrix[i][j]
                if (c > 0):
                    g.addArc(i, j)

        return g
Esempio n. 3
0
    def testConstructorFromDG(self):
        dg = gum.DiGraph()

        dg.addNodes(4)

        dg.addArc(0, 2)
        dg.addArc(1, 2)
        dg.addArc(2, 3)

        mixed_graph = gum.MixedGraph()

        mixed_graph.addNodes(4)

        mixed_graph.addArc(0, 2)
        mixed_graph.addArc(1, 2)
        mixed_graph.addArc(2, 3)

        mg = gum.MixedGraph(dg)

        self.assertEqual(mixed_graph, mg)
Esempio n. 4
0
def backdoor_generator(bn: "pyAgrum.BayesNet",
                       cause: NodeId,
                       effect: NodeId,
                       not_bd: NodeSet = None):
    """
  Generates backdoor sets for the pair of nodes `(cause, effect)` in the graph `bn` excluding the nodes in the set
  `not_bd` (optional)

  Parameters
  ----------
  bn: pyAgrum.BayesNet
  cause: int
  effect: int
  not_bd: Set[int] default=None

  Yields
  -------
  List[int]
    the different backdoors
  """
    if len(bn.parents(cause)) == 0:  # no parent of cause, no backdoor
        return
    if isParent(effect, cause, bn):  # causalDagFromBN(bn)):
        return

    if not_bd is None:
        not_bd = set()

    # simplifying the graph
    interest = {cause, effect}
    G = dSep_reduce(bn, interest)

    # removing the non connected in G without descendants
    # GG is a trash graph just to find the disjointed nodes in G
    GG = pyAgrum.DiGraph(G)
    for i in descendants(bn, cause, set()):
        GG.eraseNode(i)

    # we only keep interesting connex components
    for nodes in GG.connectedComponents().values():
        if nodes.isdisjoint(interest):
            for n in nodes:
                G.eraseNode(n)

    possible = set(
        G.nodes()) - (descendants(bn, cause, set()) | interest | not_bd)
    if len(possible) == 0:
        return

    backdoors = set()

    for i in range(len(possible)):
        for subset in it.combinations(possible, i + 1):
            sub = frozenset(subset)
            worth_testing = True
            for s in backdoors:
                if s <= sub:
                    worth_testing = False
            if worth_testing and isDSep_parents(G, {cause}, {effect}, sub):
                backdoors.add(sub)
                yield list(subset)
Esempio n. 5
0
 def testAddNodes(self):
     self._testAddNodes(gum.DiGraph())
     self._testAddNodes(gum.UndiGraph())
     self._testAddNodes(gum.MixedGraph())
     self._testAddNodes(gum.DAG())