예제 #1
0
    def __init__(self, filename, fileosservazioni):

        fi = json.load(open(filename))
        self.var = fi['variables']
        self.elenco_terapie = fi['therapies']
        self.collisioni = fi['false']
        self.patients = json.load(open(fileosservazioni))

        self.var_dict = dict(
        )  ### dizionario variabili con distrubuzioni e descrizione
        self.prob_dict = dict()  ### dizionario probability table

        var_list = list()  ### lista delle variabili da dare in pasto alla BN
        prob_list = list()  ### lista probability table da inseriore nella BN

        for v in self.var:
            self.var_dict[v] = Variable(
                v, self.var[v][1]
            )  ### costruisce una variabile da inserire nella BN dal dizionario variabili
            var_list.append(self.var_dict[v])

        for v in self.var:
            nod = self.var_dict[v]
            gen = list()

            lis = self.var[v][3]  ### lista genitori della singola variabile

            for l in lis:
                gen.append(self.var_dict[l])

            self.prob_dict[v] = Prob(
                nod, gen, self.var[v][4]
            )  ### costruzione probability table: variabile, genitore, distribuzioni di probabilità

            prob_list.append(
                self.prob_dict[v]
            )  ### append in lista probability table da inseriore in BN
            print(self.prob_dict[v])

        self.bn = Belief_network(var_list, prob_list)
예제 #2
0
        self.dvar = dvar
        self.name = str(dvar)  # Used in printing


boolean = [False, True]
Al = Variable("Alarm", boolean)
Fi = Variable("Fire", boolean)
Le = Variable("Leaving", boolean)
Re = Variable("Report", boolean)
Sm = Variable("Smoke", boolean)
Ta = Variable("Tamper", boolean)
SS = Variable("See Sm", boolean)
CS = DecisionVariable("Ch Sm", boolean, {Re})
Call = DecisionVariable("Call", boolean, {SS, CS, Re})

f_ta = Prob(Ta, [], [0.98, 0.02])
f_fi = Prob(Fi, [], [0.99, 0.01])
f_sm = Prob(Sm, [Fi], [0.99, 0.01, 0.1, 0.9])
f_al = Prob(Al, [Fi, Ta], [0.9999, 0.0001, 0.15, 0.85, 0.01, 0.99, 0.5, 0.5])
f_lv = Prob(Le, [Al], [0.999, 0.001, 0.12, 0.88])
f_re = Prob(Re, [Le], [0.99, 0.01, 0.25, 0.75])
f_ss = Prob(SS, [CS, Sm], [1, 0, 1, 0, 1, 0, 0, 1])

ut = Utility([CS, Fi, Call], [0, -200, -5000, -200, -20, -220, -5020, -220])

dnf = DecisionNetwork([Ta, Fi, Al, Le, Sm, Call, SS, CS, Re],
                      [f_ta, f_fi, f_sm, f_al, f_lv, f_re, f_ss, ut])
# v,p = VE_DN(dnf).optimize()
# for df in p: print(df,"\n")

grades = ["A", "B", "C", "F"]
class Inference_method(Displayable):
    """The abstract class of graphical model inference methods"""
    def query(self, qvar, obs={}):
        raise NotImplementedError("Inference_method query")  # abstract method


from probVariables import Variable
from probFactors import Prob

boolean = [False, True]
A = Variable("A", boolean)
B = Variable("B", boolean)
C = Variable("C", boolean)

f_a = Prob(A, [], [0.4, 0.6])
f_b = Prob(B, [A], [0.9, 0.1, 0.2, 0.8])
f_c = Prob(C, [B], [0.5, 0.5, 0.3, 0.7])

bn1 = Belief_network([A, B, C], [f_a, f_b, f_c])

# Bayesian network report of leaving example from
# Poole and Mackworth, Artificial Intelligence, 2010 http://artint.info
# This is Example 6.10 (page 236) shown in Figure 6.1

Al = Variable("Alarm", boolean)
Fi = Variable("Fire", boolean)
Le = Variable("Leaving", boolean)
Re = Variable("Report", boolean)
Sm = Variable("Smoke", boolean)
Ta = Variable("Tamper", boolean)
예제 #4
0
prob_dict = dict()  ### dizionario probability table
prob_list = list()  ### lista probability table da inseriore nella BN

for v in var:
    nod = var_dict[v]

    gen = list()

    lis = var[v][3]  ### lista genitori della singola variabile

    for l in lis:
        gen.append(var_dict[l])

    prob_dict[v] = Prob(
        nod, gen, var[v][4]
    )  ### costruzione probability table: variabile, genitore, distribuzioni di probabilità

    prob_list.append(
        prob_dict[v])  ### append in lista probability table da inseriore in BN

    print(prob_dict[v])

bn = Belief_network(var_list, prob_list)

sampling = Gibbs_sampling(bn)  ### sampling di Gibbs

##### costruita base ^^^^^^

lista_patologie = list()  ### lista patologia da estrarre nella BN
예제 #5
0
파일: probDBN.py 프로젝트: libphy/AISpace2
    def __init__(self, vars1, transition_factors=None, init_factors=None):
        self.vars1 = vars1
        self.vars0 = [v.previous for v in vars1]
        self.transition_factors = transition_factors
        self.init_factors = init_factors
        self.var_index = {}  # var_index[v] is the index of variable v
        for i, v in enumerate(vars1):
            self.var_index[v] = i


A0, A1 = variable_pair("A")
B0, B1 = variable_pair("B")
C0, C1 = variable_pair("C")

# dynamics
pc = Prob(C1, [B1, C0], [0.03, 0.97, 0.38, 0.62, 0.23, 0.77, 0.78, 0.22])
pb = Prob(B1, [A0, A1], [0.5, 0.5, 0.77, 0.23, 0.4, 0.6, 0.83, 0.17])
pa = Prob(A1, [A0, B0], [0.1, 0.9, 0.65, 0.35, 0.3, 0.7, 0.8, 0.2])

# initial distribution
pa0 = Prob(A1, [], [0.9, 0.1])
pb0 = Prob(B1, [A1], [0.3, 0.7, 0.8, 0.2])
pc0 = Prob(C1, [], [0.2, 0.8])

dbn1 = DBN([A1, B1, C1], [pa, pb, pc], [pa0, pb0, pc0])

Pos_0, Pos_1 = variable_pair("Position", domain=[0, 1, 2, 3])
Mic1_0, Mic1_1 = variable_pair("Mic1")
Mic2_0, Mic2_1 = variable_pair("Mic2")
Mic3_0, Mic3_1 = variable_pair("Mic3")
예제 #6
0
nodi = dict()

temp = Variable('infarto', boolean)

nodi['infarto'] = ('infarto del miocardio', 'patologia', temp, (temp, [],
                                                                [0.2, 0.8]))

temp = Variable('dolore', boolean)

nodi['dolore'] = ('dolore al petto', 'sintomo', temp,
                  (temp, [nodi['infarto'][2]], [0.2, 0.8, 0.8, 0.2]))

propertytable = dict()

propertytable['infarto'] = Prob(nodi['infarto'][2], [], nodi['infarto'][3][2])
propertytable['dolore'] = Prob(nodi['dolore'][2], [nodi['infarto'][2]],
                               nodi['dolore'][3][2])

lista_nodi = list()

for n in nodi:
    lista_nodi.append(nodi[n][2])

lista_prop = list()

for p in propertytable:
    lista_prop.append(propertytable[p])

bn = Belief_network(lista_nodi, lista_prop)