Blame view

students/amartins/tarefas/tarefa2.py 2.32 KB
3e0f9b8a   Francisco Coelho   back to work?
1
2
#!env python
import pickle
505cdc43   Francisco Coelho   long due comeback
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from pgmpy.readwrite import BIFReader
import networkx as nx
import pydot
from networkx.drawing.nx_pydot import graphviz_layout
from bninput import *

def get_yes_probabilities(model):
    nodes = model.nodes()

    yes_probabilities = {}

    for node in nodes:
        cpd = model.get_cpds(node)
        state_names = cpd.state_names[node]
        yes_index = state_names.index("yes")
        yes_prob = cpd.values[yes_index]
        yes_probabilities[node] = yes_prob

    return yes_probabilities

3e0f9b8a   Francisco Coelho   back to work?
23
24
25
26
27
28
29
30
31
def compile_bn2asp(model):
    """_summary_

    Args:
        model (_type_): _description_

    Returns:
        _type_: _description_
    """
505cdc43   Francisco Coelho   long due comeback
32
    probabilities = get_yes_probabilities(model)
3e0f9b8a   Francisco Coelho   back to work?
33
    sf = ""
505cdc43   Francisco Coelho   long due comeback
34
35
36
37
    for node, yes_prob in probabilities.items():
        parents = model.get_parents(node)
        s = ""
        if len(parents) == 0:
3e0f9b8a   Francisco Coelho   back to work?
38
39
40
            s += f"%* {yes_prob} :: {node}. *% \n"
            s += f"{node} ; -{node}. \n\n"
            sf += s
505cdc43   Francisco Coelho   long due comeback
41
        elif len(parents) == 1:
353a8086   Francisco Coelho   alterações feitas...
42
            p = parents[0]
3e0f9b8a   Francisco Coelho   back to work?
43
44
45
46
47
            s += f"%* {yes_prob[0]} :: {node} :- {p}. *% \n"
            s += f"{node} ; -{node} :- {p}. \n"
            s += f"%* {yes_prob[1]} :: {node} :- -{p}. *% \n"
            s += f"{node} ; -{node} :- -{p}. \n\n"
            sf += s
505cdc43   Francisco Coelho   long due comeback
48
        else:
353a8086   Francisco Coelho   alterações feitas...
49
50
51
52
            yes1 = yes_prob[0]
            yes2 = yes_prob[1]
            p0 = parents[0]
            p1 = parents[1]
3e0f9b8a   Francisco Coelho   back to work?
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
            s += f"%* {yes1[0]} :: {node} :- {p0}, {p1}. *% \n"
            s += f"{node} ; -{node} :- {p0}, {p1}. \n"
            s += f"%* {yes2[0]} :: {node} :- -{p0}, {p1}. *% \n"
            s += f"{node} ; -{node} :- -{p0}, {p1}. \n"
            s += f"%* {yes1[1]} :: {node} :- {p0}, -{p1}. *% \n"
            s += f"{node} ; -{node} :- {p0}, -{p1}. \n"
            s += f"%* {yes2[1]} :: {node} :- -{p0}, -{p1}. *% \n"
            s += f"{node} ; -{node} :- -{p0}, -{p1}. \n"
            
            sf += s
    return sf

if __name__ == "__main__":
    # summary = summary_dag("/home/fc/sci/projetos/zugzwang/students/amartins/tarefas/asia2.bif")
    summary = pickle.load(open("asia2.pkl", "rb"))
    # 3.2776401042938232
    
    model = summary["bnmodel"]
    # 1.1920928955078125e-06
    
    sum = compile_bn2asp(model)
    # 0.00010275840759277344
    
    print(sum)
    # 5.269050598144531e-05
    
    # TOTAL DE TEMPO: 5.778 SEGUNDOS