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)
def __init__(self, name, domain, parents): Variable.__init__(self, name, domain) self.parents = parents self.all_vars = set(parents) | {self}
best_elt = elt self.values[index] = max_val self.decision_fun.values[index] = best_elt return max_val class Factor_DF(Factor_stored): """A decision function""" def __init__(self, dvar, vars, values): Factor_stored.__init__(self, vars, values) 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])
from utilities import Displayable 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)
fi = json.load(open("cardiologia.json")) var = fi['variables'] elenco_terapie = fi['therapies'] patients = json.load(open("osservazioni.json")) var_dict = dict() ### dizionario variabili con distrubuzioni e descrizione var_list = list() ### lista delle variabili da dare in pasto alla BN for v in var: var_dict[v] = Variable( v, var[v][1] ) ### costruisce una variabile da inserire nella BN dal dizionario variabili var_list.append(var_dict[v]) 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])
def __init__(self, name, domain=[False, True], index=1): Variable.__init__(self, name, domain) self.index = index self.previous = None
from probGraphicalModels import Belief_network from probVariables import Variable from probFactors import Prob from probMCMC import Gibbs_sampling boolean = [False, True] 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()