def LogFactorMaxProduct(F1, F2, variableList): """ Computes the max-product of two factors specified in log-scale """ scope = list( set(F1.scope).union(set(F2.scope)).difference(set(variableList))) eDim = 1 for var in variableList: eDim = eDim * var.num_states F = Factor(scope) j, k = 0, 0 assignment = [0 for l in range(len(variableList) + len(scope))] for i in range(F.dimension): for _ in range(eDim): F.values[i] = max(F.values[i], F1.values[j] + F2.values[k]) for l in range(len(variableList) + len(scope)): assignment[l] = assignment[l] + 1 if l < len(variableList): var = variableList[l] else: var = scope[l - len(variableList)] if assignment[l] == var.num_states: assignment[l] = 0 j = j - (var.num_states - 1) * F1.getStride(var) k = k - (var.num_states - 1) * F2.getStride(var) else: j = j + F1.getStride(var) k = k + F2.getStride(var) break return F
def parse(self): self.factor = Factor() self.factor.parse() if TokList.checkTok('MULT'): TokList.nextToken() self.term = Term() self.term.parse()
def main(args): print "[Solve ID by local search]" N, M, K = int(args[1]), int(args[2]), int(args[3]) from Factor import Factor # sample network ChanceVars, DecVars, CPT, Strategy, Utility = sampleChainID(N, M) #print "loading model from file...", #stime = time.clock() #Variables, Factors = read_network_from_file(args[1], useLog=logScale) #etime = time.clock() - stime #print "done: %d variables, %d factors. \033[91m[%gs]\033[0m" % (len(Variables), len(Factors), etime) run(ChanceVars, DecVars, CPT, Strategy, Utility, K, False, 100, True) sol = len(DecVars) * [None] for n in range(len(DecVars)): sol[n] = Factor([DecVars[n]], defaultValue=1.0 / M) kPUvalue, kPUit, kPUtime, KPUsize = run(ChanceVars, DecVars, CPT, sol, Utility, K, False, 100, True) sol2 = len(DecVars) * [None] for n in range(len(DecVars)): sol2[n] = Factor([DecVars[n]], defaultValue=1.0 / M) kPUvalue2, kPUit2, kPUtime2, KPUsize2 = run(ChanceVars, DecVars, CPT, sol2, Utility, K, False, 100, False) print print "METHOD \tTIME(s) \tMAX SIZE \tVALUE" print "Pruned \t%10s\t%10s\t%g" % (str(kPUtime), str(KPUsize), kPUvalue) print "Exhaustive\t%10s\t%10s\t%g" % (str(kPUtime2), str(KPUsize2), kPUvalue2)
def LogFactorMaxProduct(F1,F2,variableList): """ Computes the max-product of two factors specified in log-scale """ scope = list(set(F1.scope).union(set(F2.scope)).difference(set(variableList))) eDim = 1 for var in variableList: eDim = eDim*var.num_states F = Factor(scope) j,k = 0,0 assignment = [0 for l in range(len(variableList)+len(scope))] for i in range(F.dimension): for _ in range(eDim): F.values[i] = max(F.values[i], F1.values[j]+F2.values[k]) for l in range(len(variableList)+len(scope)): assignment[l] = assignment[l] + 1 if l < len(variableList): var = variableList[l] else: var = scope[l-len(variableList)] if assignment[l] == var.num_states: assignment[l] = 0 j = j - (var.num_states-1)*F1.getStride(var) k = k - (var.num_states-1)*F2.getStride(var) else: j = j + F1.getStride(var) k = k + F2.getStride(var) break return F
def __init__(self, functions, digitos=3): self.agregar = gtk.VBox() self.agregar.show() self.principal = gtk.VBox() self.principal.show() self.agregar.pack_start(self.principal, False, False) self.llevo = Factor(digitos, functions, -2) self.principal.pack_start(self.llevo.agregar, False, False) self.factores = Factores(2, digitos, functions, False) self.principal.pack_start(self.factores.agregar, False, False) self.igual = gtk.HBox() self.igual.show() self.linea = self.getLineaHor(500) self.igual.pack_end(self.linea, False, False) self.signo = gtk.Image() self.signo.set_from_file("./images/-.gif") self.signo.show() self.igual.pack_start(self.signo, False, False) self.principal.pack_start(self.igual, False, False) self.resultado = Factor(digitos + 1, functions, -1) self.principal.pack_start(self.resultado.agregar, False, False) self.tipo = 1 self.extra = [self.llevo, self.resultado]
def LogFactorSumProduct(F1,F2,variableList): """ Computes the sum-product of two factors specified in log-scale """ scope = list(set(F1.scope).union(set(F2.scope)).difference(set(variableList))) eDim = 1 for var in variableList: eDim = eDim*var.num_states F = Factor(scope, defaultValue=0.0) j,k = 0,0 c1 = max(F1.values) c2 = max(F2.values) c = max(c1,c2) assignment = [0 for l in range(len(variableList)+len(scope))] for i in range(F.dimension): for _ in range(eDim): F.values[i] += exp(F1.values[j]+F2.values[k]-c) for l in range(len(variableList)+len(scope)): assignment[l] = assignment[l] + 1 if l < len(variableList): var = variableList[l] else: var = scope[l-len(variableList)] if assignment[l] == var.num_states: assignment[l] = 0 j = j - (var.num_states-1)*F1.getStride(var) k = k - (var.num_states-1)*F2.getStride(var) else: j = j + F1.getStride(var) k = k + F2.getStride(var) break for k in range(F.dimension): try: F.values[k] = log(F.values[k]) + c except ValueError: F.values[k] = LOGZERO return F
class Suma(Operacion): def __init__(self, functions, factores=3, digitos=3, correrIDs=0): self.agregar = Gtk.HBox() self.agregar.show() self.principal = Gtk.VBox(False, 0) self.principal.show() self.agregar.pack_end(self.principal, True, True, 0) self.llevo = Factor(digitos, functions, -2) self.principal.pack_start(self.llevo.agregar, False, False, 0) self.factores = Factores(factores, digitos, functions, True, correrIDs=correrIDs) self.principal.pack_start(self.factores.agregar, False, False, 0) self.igual = Gtk.HBox() self.igual.show() self.linea = self.getLineaHor(500) self.igual.pack_end(self.linea, False, False, 0) self.signo = Gtk.Image() self.signo.set_from_file("./images/+.gif") self.signo.show() self.igual.pack_start(self.signo, True, True, 0) self.principal.pack_start(self.igual, False, True, 10) self.resultado = Factor(digitos + 1, functions, -1) self.principal.pack_start(self.resultado.agregar, False, False, 0) self.tipo = 0 self.extra = [self.llevo, self.resultado] def comprobar(self): if (self.resultado.getValor() == self.factores.sumarTodos()): return True else: return False def setZoom(self, valor): Operacion.setZoom(self, valor) if valor == 1: self.llevo.setSize("ss") self.llevo.digitos.agregar.set_spacing(0) elif valor == 2: self.llevo.setSize("ss") self.llevo.digitos.agregar.set_spacing(40) elif valor == 3: self.llevo.setSize("s") self.llevo.digitos.agregar.set_spacing(40) elif valor == 4: self.llevo.setSize("m") self.llevo.digitos.agregar.set_spacing(40)
def __init__(self, functions, digitos=2): self.agregar = gtk.VBox() self.agregar.show() self.principal = self.agregar arriba = gtk.HBox() arriba.show() self.agregar.pack_start(arriba, False, False) abajo = gtk.HBox() abajo.show() self.agregar.pack_start(abajo, False, False) self.dividendo = Factor(digitos, functions, 0) self.divisor = Factor(digitos, functions, 1) self.linea2 = self.getLineaVer(100) self.igual = gtk.HBox() self.igual.show() self.linea = self.getLineaHor(250) self.igual.pack_end(self.linea) arriba.pack_end(self.dividendo.agregar, False, False) arriba.pack_end(self.linea2, False, False) arriba.pack_end(self.divisor.agregar, False, False) self.cociente = Factor(digitos, functions, -1, inverso=True) self.restos = Factores(digitos, digitos, functions, True, correrIDs=2, inverso=True) der = gtk.VBox() der.show() der.pack_start(self.igual, False, False) der.pack_start(self.cociente.agregar, False, False) abajo.pack_end(der, False, False) abajo.pack_end(self.restos.agregar, False, False) self.factores = self.restos self.factores.factores.insert(0, self.divisor) self.factores.factores.insert(0, self.dividendo) self.resultado = self.cociente self.tipo = 3 self.extra = [ self.resultado, ]
def buildVariableFactor(self, vName, fName): incoming_messages = [] for fNameNeighbor in self.nodeVariables[vName].getNodeFactorNames(): if fNameNeighbor != fName: msg = self.msgFactorVariable(fNameNeighbor, vName) incoming_messages.append(msg) if not incoming_messages: incoming_messages.append( Factor([vName], np.array([1.] * self.nodeVariables[vName].getRank()))) joined = Factor.joint(incoming_messages) #normalized =joined.normalize() return joined
def __init__(self, nfactors, T=1.0): super(ThreeRegularFactorGraph, self).__init__() factorNvariables = {i: [] for i in range(nfactors)} for i in range(nfactors): if i == 0: j = nfactors - 1 else: j = i - 1 self.add_variable(ijth_variable_name(i, j)) factorNvariables[i].append(ijth_variable_name(i, j)) factorNvariables[j].append(ijth_variable_name(i, j)) if i < nfactors / 2: _, j = divmod(i + (nfactors / 2), nfactors) j = int(j) self.add_variable(ijth_variable_name(i, j)) factorNvariables[i].append(ijth_variable_name(i, j)) factorNvariables[j].append(ijth_variable_name(i, j)) for i in range(nfactors): cardinality = [2 for j in range(len(factorNvariables[i]))] factor_value = np.exp(np.random.normal(0.0, T, cardinality)) self.add_factor( Factor(ith_factor_name(i), factorNvariables[i], cardinality, factor_value))
def sumout(self, f, var): ''' Return new factor as marginalization of f on var Args: f: Factor() object var: variable in Markov Network ''' tableSize = f.getTableSize() // self.cardinalities[var] newTable = [0] * tableSize newScope = f.getScope() newScope.remove(var) s = f.getStride(var) seen = set() assign = 0 j = 0 for i in range(tableSize): while True: while j in seen: j += 1 seen.add(j) newTable[i] += f.getTable()[j] assign += 1 if assign == self.cardinalities[var]: assign = 0 j -= (self.cardinalities[var] - 1) * s break else: j += s return Factor(newScope, newTable, self.cardinalities)
def FactorSumProduct(F1, F2, variableList): """ Computes the sum-product of two factors w.r.t. the variables in variableList""" scope = list( set(F1.scope).union(set(F2.scope)).difference(set(variableList))) eDim = 1 for var in variableList: eDim = eDim * var.num_states F = Factor(scope, defaultValue=0.0) j, k = 0, 0 assignment = [0 for l in range(len(variableList) + len(scope))] for i in range(F.dimension): for _ in range(eDim): F.values[i] += F1.values[j] * F2.values[k] for l in range(len(variableList) + len(scope)): assignment[l] = assignment[l] + 1 if l < len(variableList): var = variableList[l] else: var = scope[l - len(variableList)] if assignment[l] == var.num_states: assignment[l] = 0 j = j - (var.num_states - 1) * F1.getStride(var) k = k - (var.num_states - 1) * F2.getStride(var) else: j = j + F1.getStride(var) k = k + F2.getStride(var) break return F
def instantiate(self, evidFile): ''' Instantiate evidence. Reduce CPTs and factors ''' with open(evidFile) as fname: lst = list(map(int, fname.readline().split())) for i in range(1, len(lst), 2): self.evid[lst[i]] = lst[i + 1] for i in range(len(self.factors)): for v in self.evid: if self.factors[i].contains(v): s = self.factors[i].getStride(v) tmpScope = self.factors[i].getScope() tmpScope.remove(v) tmpTable = [] for j in range(self.evid[v] * s, self.factors[i].getTableSize(), s * self.cardinalities[v]): for k in range(j, j + s): tmpTable.append(self.factors[i].getTable()[k]) self.factors[i] = Factor(tmpScope, tmpTable, self.cardinalities)
def read(self, inputFile): ''' Read input file and populate information for the graphical model Args: inputFile: input UAI file ''' with open(inputFile) as fname: fname.readline() numVars = int(fname.readline()) for i in range(numVars): self.variables.append(i) self.cardinalities = list(map(int, fname.readline().split())) self.numCliques = int(fname.readline()) cliques = [] for i in range(self.numCliques): cliques.append(list(map(int, fname.readline().split()))[1:]) tables = [] for i in range(self.numCliques): while (len(fname.readline()) <= 1): continue # fname.readline() tables.append(list(map(float, fname.readline().split()))) for i in range(self.numCliques): self.factors.append( Factor(cliques[i], tables[i], self.cardinalities))
def product(self, f1, f2): ''' Return product of 2 factors. Code reference from Box 10.A, pages 358-361 in Koller and Friedman Args: f1, f2: Factor() objects ''' j, k = 0, 0 assignment = {} tableSize = f1.getTableSize() for var in f1.getScope(): assignment[var] = 0 for var in f2.getScope(): if not f1.contains(var): tableSize *= self.cardinalities[var] assignment[var] = 0 newTable = [0] * tableSize for i in range(tableSize): newTable[i] = f1.getTable()[j] * f2.getTable()[k] for l in reversed(sorted(assignment.keys())): assignment[l] += 1 if assignment[l] == self.cardinalities[l]: assignment[l] = 0 j -= (self.cardinalities[l] - 1) * f1.getStride(l) k -= (self.cardinalities[l] - 1) * f2.getStride(l) else: j += f1.getStride(l) k += f2.getStride(l) break return Factor(list(sorted(assignment.keys())), newTable, self.cardinalities)
def belief(self, vName): self.cleanMessages() incoming_messages = [] for fName in self.nodeVariables[vName].getNodeFactorNames(): incoming_messages.append(self.msgFactorVariable(fName, vName)) joined = Factor.joint(incoming_messages) #normalized =joined.normalize() return joined
def agregarUno(self, functions = None): if functions == None: functions = self.defaultListeners if self.unoMenos: self.digitos -= 1 if(self.hided == 0): este = Factor(self.digitos, functions, self.count, self.inverso) if len(self.factores) != 0: este.MasListeners = self.factores[0].MasListeners este.MenosListeners = self.factores[0].MenosListeners self.count += 1 self.factores.append(este) self.principal.pack_start(este.agregar, False, False, 0) else: self.factores[self.count - self.hided - self.correrIDs].agregar.show() self.hided -= 1
def FactorProduct(F1, F2): """ Computes the product of two factors """ scope = list(set(F1.scope).union(set(F2.scope))) F = Factor(scope) j, k = 0, 0 assignment = [0 for l in range(len(scope))] for i in range(F.dimension): F.values[i] = F1.values[j] * F2.values[k] for l in range(len(scope)): assignment[l] = assignment[l] + 1 if assignment[l] == scope[l].num_states: assignment[l] = 0 j = j - (scope[l].num_states - 1) * F1.getStride(scope[l]) k = k - (scope[l].num_states - 1) * F2.getStride(scope[l]) else: j = j + F1.getStride(scope[l]) k = k + F2.getStride(scope[l]) break return F
def FactorProduct(F1,F2): """ Computes the product of two factors """ scope = list(set(F1.scope).union(set(F2.scope))) F = Factor(scope) j,k = 0,0 assignment = [0 for l in range(len(scope))] for i in range(F.dimension): F.values[i] = F1.values[j] * F2.values[k] for l in range(len(scope)): assignment[l] = assignment[l] + 1 if assignment[l] == scope[l].num_states: assignment[l] = 0 j = j - (scope[l].num_states-1)*F1.getStride(scope[l]) k = k - (scope[l].num_states-1)*F2.getStride(scope[l]) else: j = j + F1.getStride(scope[l]) k = k + F2.getStride(scope[l]) break return F
class Resta(Operacion): def __init__(self, functions, digitos=3): self.agregar = gtk.VBox() self.agregar.show() self.principal = gtk.VBox() self.principal.show() self.agregar.pack_start(self.principal, False, False) self.llevo = Factor(digitos, functions, -2) self.principal.pack_start(self.llevo.agregar, False, False) self.factores = Factores(2, digitos, functions, False) self.principal.pack_start(self.factores.agregar, False, False) self.igual = gtk.HBox() self.igual.show() self.linea = self.getLineaHor(500) self.igual.pack_end(self.linea, False, False) self.signo = gtk.Image() self.signo.set_from_file("./images/-.gif") self.signo.show() self.igual.pack_start(self.signo, False, False) self.principal.pack_start(self.igual, False, False) self.resultado = Factor(digitos + 1, functions, -1) self.principal.pack_start(self.resultado.agregar, False, False) self.tipo = 1 self.extra = [self.llevo, self.resultado] def comprobar(self): pass def setZoom(self, valor): Operacion.setZoom(self, valor) if valor == 1: self.llevo.setSize("ss") self.llevo.digitos.agregar.set_spacing(0) elif valor == 2: self.llevo.setSize("ss") self.llevo.digitos.agregar.set_spacing(40) elif valor == 3: self.llevo.setSize("s") self.llevo.digitos.agregar.set_spacing(40) elif valor == 4: self.llevo.setSize("m") self.llevo.digitos.agregar.set_spacing(40)
def buildMaxFactorVariable(self, fName, vName): incoming_messages = [self.nodeFactors[fName].getFactor()] marginalization_variables = [] for vNameMeighbor in self.nodeFactors[fName].getVariableNames(): if vNameMeighbor != vName: msg = self.msgMaxVariableFactor(vNameMeighbor, fName) incoming_messages.append(msg) marginalization_variables.append(vNameMeighbor) joined = Factor.joint(incoming_messages) maximized = joined.maximize(marginalization_variables) #normalized =maximized.normalize() return maximized
def main() -> None: if len(sys.argv) <= 1: print( 'Usage: python main.py [1 or 2 for 2b part 1 and 2 respectively]') exit(-1) sim_to_run = sys.argv[1] factors = [ Factor(['Trav'], [], [0.05, 0.95]), Factor(['Fraud'], ['Trav'], [0.01, 0.99, 0.004, 0.996]), Factor(['OC'], [], [0.7, 0.3]), Factor(['CRP'], ['OC'], [0.1, 0.9, 0.001, 0.999]), Factor(['FP'], ['Trav', 'Fraud'], [0.9, 0.1, 0.9, 0.1, 0.1, 0.9, 0.01, 0.99]), Factor(['IP'], ['OC', 'Fraud'], [0.02, 0.98, 0.01, 0.99, 0.011, 0.989, 0.001, 0.999]) ] if sim_to_run == '1': inference(factors, ['Fraud'], ['Trav', 'FP', 'IP', 'OC', 'CRP'], []) else: inference(factors, ['Fraud'], ['Trav', 'OC'], [('FP', Sign.POSITIVE), ('IP', Sign.NEGATIVE), ('CRP', Sign.POSITIVE)])
def main(args): print "[Solve ID by local search]" N, M, K, T = int(args[1]), int(args[2]), int(args[3]), int(args[4]) from Factor import Factor # sample network ChanceVars, DecVars, CPT, Strategy, Utility = sampleChainID(N, M) print "SPU" print sys.stdout.flush() SPUvalue, SPUit, SPUtime, SPUsize = run(ChanceVars, DecVars, CPT, Strategy, Utility, 1, False, 4 * N, False) print "Exact" print sys.stdout.flush() sol = len(DecVars) * [None] for n in range(len(DecVars)): sol[n] = Factor([DecVars[n]], defaultValue=1.0 / M) kPUvalue, kPUit, kPUtime, kPUsize = run(ChanceVars, DecVars, CPT, sol, Utility, K, True, 2 * N, False) print "Approximate" print sys.stdout.flush() sol2 = len(DecVars) * [None] for n in range(len(DecVars)): sol2[n] = Factor([DecVars[n]], defaultValue=1.0 / M) akPUvalue, akPUit, akPUtime, akPUsize = run(ChanceVars, DecVars, CPT, sol2, Utility, K, True, 2 * N, True, T) print print "METHOD \tTIME(s) \tMAX SIZE \tVALUE" print "SPU \t%10s\t%10s\t%g" % (str(SPUtime), str(SPUsize), SPUvalue) print "Exact \t%10s\t%10s\t%g" % (str(kPUtime), str(kPUsize), kPUvalue) print "Approximate\t%10s\t%10s\t%g" % (str(akPUtime), str(akPUsize), akPUvalue)
def FactorMaxBProduct(FactorList, variableList): """ Computes pairwise max-product of list of factors """ F = FactorList.pop() if len(FactorList) == 0: Fp = Factor([], defaultValue=1.0) return FactorMaxProduct(F, Fp, variableList) while len(FactorList) > 1: Fp = FactorList.pop() F = FactorProduct(F, Fp) Fp = FactorList.pop() return FactorMaxProduct(F, Fp, variableList)
def LogFactorSumBProduct(FactorList, variableList): """ Computes pairwise sum-product of list of factors specified in log-scale """ F = FactorList.pop() if len(FactorList) == 0: Fp = Factor([], defaultValue=0.0) return LogFactorSumProduct(F, Fp, variableList) while len(FactorList) > 1: Fp = FactorList.pop() F = LogFactorProduct(F, Fp) Fp = FactorList.pop() return LogFactorSumProduct(F, Fp, variableList)
def buildFactorVariable(self, fName, vName): incoming_messages = [self.nodeFactors[fName].getFactor()] marginalization_variables = [] for vNameMeighbor in self.nodeFactors[fName].getVariableNames(): if vNameMeighbor != vName: msg = self.msgVariableFactor(vNameMeighbor, fName) incoming_messages.append(msg) marginalization_variables.append(vNameMeighbor) joined = Factor.joint(incoming_messages) #This doesn't seem to work for evicences :-( reduced = self.reduce(joined, marginalization_variables + [vName]) marginalized = reduced.marginalize(marginalization_variables) #normalized =marginalized.normalize() return marginalized
def sampleChainID(N,M): """ Sample a chain ID with N chance nodes and variable cardinalities M """ from random import random ChanceVars = [ Variable('C'+str(i),M) for i in range(N) ] DecVars = [ Variable('D'+str(i),M) for i in range(N) ] CPT = N*[ None ] DecCPT = N*[ None ] CPT[0] = Factor( [ChanceVars[0],DecVars[0]] ) # P(C[0]|D[0]) for offset in range(M): # P(C[i]|C[i-1]=0,D[i]=0) CPT[0].values[M*offset:M*(offset+1)] = Dirichlet(M,M) for i in range(1,N): CPT[i] = Factor( [ChanceVars[i],ChanceVars[i-1],DecVars[i]] ) # P(C[i]|C[i-1],D[i]) offset = 0 for offset in range(M*M): # P(C[i]|C[i-1]=0,D[i]=0) CPT[i].values[M*offset:M*(offset+1)] = Dirichlet(M,M) offset+=1 for i in range(N): DecCPT[i] = Factor([DecVars[i]], defaultValue=1.0/M) U = Factor([ ChanceVars[N-1] ]) for i in range(M): U.values[i] = random() return ChanceVars, DecVars, CPT, DecCPT, U
def __init__(self, functions, factores=3, digitos=3, correrIDs=0): self.agregar = Gtk.HBox() self.agregar.show() self.principal = Gtk.VBox(False, 0) self.principal.show() self.agregar.pack_end(self.principal, True, True, 0) self.llevo = Factor(digitos, functions, -2) self.principal.pack_start(self.llevo.agregar, False, False, 0) self.factores = Factores(factores, digitos, functions, True, correrIDs=correrIDs) self.principal.pack_start(self.factores.agregar, False, False, 0) self.igual = Gtk.HBox() self.igual.show() self.linea = self.getLineaHor(500) self.igual.pack_end(self.linea, False, False, 0) self.signo = Gtk.Image() self.signo.set_from_file("./images/+.gif") self.signo.show() self.igual.pack_start(self.signo, True, True, 0) self.principal.pack_start(self.igual, False, True, 10) self.resultado = Factor(digitos + 1, functions, -1) self.principal.pack_start(self.resultado.agregar, False, False, 0) self.tipo = 0 self.extra = [self.llevo, self.resultado]
def LogFactorSumProduct(F1, F2, variableList): """ Computes the sum-product of two factors specified in log-scale """ scope = list( set(F1.scope).union(set(F2.scope)).difference(set(variableList))) eDim = 1 for var in variableList: eDim = eDim * var.num_states F = Factor(scope, defaultValue=0.0) j, k = 0, 0 c1 = max(F1.values) c2 = max(F2.values) c = max(c1, c2) assignment = [0 for l in range(len(variableList) + len(scope))] for i in range(F.dimension): for _ in range(eDim): F.values[i] += exp(F1.values[j] + F2.values[k] - c) for l in range(len(variableList) + len(scope)): assignment[l] = assignment[l] + 1 if l < len(variableList): var = variableList[l] else: var = scope[l - len(variableList)] if assignment[l] == var.num_states: assignment[l] = 0 j = j - (var.num_states - 1) * F1.getStride(var) k = k - (var.num_states - 1) * F2.getStride(var) else: j = j + F1.getStride(var) k = k + F2.getStride(var) break for k in range(F.dimension): try: F.values[k] = log(F.values[k]) + c except ValueError: F.values[k] = LOGZERO return F
def __init__(self, nfactors): super(CompleteFactorGraph, self).__init__() factorNvariables = {i: [] for i in range(nfactors)} for i in range(nfactors): for j in range(i + 1, nfactors): self.add_variable(ijth_variable_name(i, j)) factorNvariables[i].append(ijth_variable_name(i, j)) factorNvariables[j].append(ijth_variable_name(i, j)) for i in range(nfactors): cardinality = [2 for j in range(nfactors - 1)] factor_value = np.random.random(cardinality) self.add_factor( Factor(ith_factor_name(i), factorNvariables[i], cardinality, factor_value))
def var_elimination(factor_list: List[Factor], query_variables: List[str], ordered_list_of_hidden_variables: List[str], evidence_list: List[Tuple[str, Sign]]): """ Calculate P(factor_list | evidence_list) using variable elimination, after all of the evidence is observed :param factor_list: List of the factors we'll use :param query_variables: We want to find this variables' probability :param ordered_list_of_hidden_variables: The order to sumout variables not in query_variables or evidence_list :param evidence_list: List of variables that we know the values of """ for var in ordered_list_of_hidden_variables: # find out which factors contain var, remove them from factor_list factors_to_multiply = [] for factor in factor_list[:]: if var in factor.variables: factor_list.remove(factor) factors_to_multiply.append(factor) # multiply all the removed factors together, then sumout var joined_factor = factors_to_multiply[0] for i in range(1, len(factors_to_multiply)): joined_factor = Factor.multiply(joined_factor, factors_to_multiply[i]) joined_factor.sumout(var) if len(joined_factor.solution_variables) > 0: factor_list.append(joined_factor) print('After eliminating {0}:'.format(var)) print_all_factors(factor_list) # multiply remaining factors together joined_factor = factor_list[0] for i in range(1, len(factor_list)): joined_factor = Factor.multiply(joined_factor, factor_list[i]) joined_factor.normalize() final_factor = Factor(query_variables, [], joined_factor.table[:, -1], [], evidence_list) print('Solution:') final_factor.print_representation() final_factor.print_factor()
def inference_by_enumeration(factor_list: List[Factor], ordered_list_of_hidden_variables: List[str]): """ Calculate P(factor_list | evidence_list) using inference by enumeration, after all of the evidence is observed This function is unused when doing inference :param factor_list: List of the factors we'll use :param ordered_list_of_hidden_variables: The order to sumout variables not in query_variables or evidence_list """ joined_factor = factor_list[0] for i in range(1, len(factor_list)): joined_factor = Factor.multiply(joined_factor, factor_list[i]) for var in ordered_list_of_hidden_variables: joined_factor.sumout(var) joined_factor.normalize() joined_factor.print_factor()
def PohligHellman(g, a, p): # Factor p-1 into q1, .. qi Q = [] # small primes Q = Factor(p - 1) # Find Ni = (p - 1) / qi N = [] for i in range(0, len(Q)): N.append((p - 1) / Q[i]) # Compute g^(Ni * x) = h^(Ni) mod p X = [] for i in range(0, len(Q)): X.append(findExp((g**N[i]) % p, (a**N[i]) % p, p)) # Use CRT to find x x = ChineseRemainderTheorem(X, Q) return x
def main(args): logScale = False # Use log-scale #print "[Compute all marginals by Variable Elimination]" start = time.clock() print "[Covnert BN into LIMID]" print "loading model from file", args[1], "...", sys.stdout.flush() stime = time.clock() Variables, Factors = read_network_from_file(args[1], useLog=logScale) etime = time.clock() - stime print "done: %d variables, %d factors. \033[91m[%gs]\033[0m" % (len(Variables), len(Factors), etime) pa = {} ch = {} root = set() leaf = set() for v in Variables: ch[v] = set() pa[v] = [] for f in Factors: pa[f.scope[-1]] = f.scope[:-1] for v in f.scope[:-1]: ch[v].add(f.scope[-1]) for v in Variables: if len(ch[v]) == 0: leaf.add(v) if len(pa[v]) == 0: root.add(v) print '#Leaves:', len(leaf) print '#Root:', len(root) print "building domain graph...", sys.stdout.flush() stime = time.clock() dGraph = BuildDomainGraph(Factors, Variables) etime = time.clock() - stime print "done. \033[91m[%gs]\033[0m" % etime print "computing MMD lower bound on treewidth...", sys.stdout.flush() stime = time.clock() mmd = ComputeLowerBound(dGraph) etime = time.clock() - stime print "done. \033[91m[%gs]\033[0m" % etime print "applying safe reduction rules...", sys.stdout.flush() stime = time.clock() Prefix, low = FindPrefix(dGraph, low=mmd) etime = time.clock() - stime print "done. \033[91m[%gs]\033[0m" % etime print "Optimal Prefix Length:", len(Prefix) print "computing min-fill ordering...", sys.stdout.flush() stime = time.clock() #OrderedVariables, tw = FindOrder(dGraph) OrderedVariables, tw = FindOrder(dGraph, Prefix=Prefix, treewidth=low) etime = time.clock() - stime print "done. \033[91m[%gs]\033[0m" % etime print "Min-Fill Elimination order:", for var in OrderedVariables: print var.label, print print "Treewidth:", tw dGraph, Prefix = None, None # dump graph prev = None i = 0 for v in reversed(OrderedVariables): if v in leaf: i += 1 #print i, ':', v.label w = Variable("W"+v.label[1:],2) f = Factor([v,w]) util = [ random() for _ in range(v.num_states) ] f.values = util + [1-u for u in util] #f.printOut() Variables.append(w) Factors.append(f) o = Variable("O"+v.label[1:],2) Variables.append(o) if prev == None: g = Factor([w,o]) g.values = [1.0, 0.0, 0.0, 1.0] Factors.append(g) else: g = Factor([w,prev,o]) g.values = [1.0, 1.0-1.0/i, 1.0/i, 0.0, 0.0, 1.0/i, 1.0-1.0/i, 1.0] Factors.append(g) #g.printOut() prev = o #print V = Variable("V",1) U = Factor([prev,V]) U.values = [float(i), 0.0] #U.printOut() #Factors.append(u) print # TO-DO: save to file in LIMID format and pickle dump out = open( args[2], "w" ) out.write("LIMID\n" + str(len(Variables)-len(root)) + " " + str(len(root)) + " 1\n") v_id = {} i = 0 for var in Variables: if var not in root: v_id[var.id] = i out.write(str(var.cardinality) + " ") print var.label, v_id[var.id] i += 1 out.write("\n") for var in root: v_id[var.id] = i out.write(str(var.cardinality) + " ") print var.label, v_id[var.id] i += 1 out.write("\n") for j,var in enumerate(Variables): if var not in root: out.write(str(len(Factors[j].scope)-1) + " ") for pa in reversed(Factors[j].scope[:-1]): out.write(str(v_id[pa.id]) + " ") out.write("\n") for var in root: out.write("0\n") out.write("1 " + str(v_id[U.scope[0].id])+ "\n") for i,var in enumerate(Variables): if var not in root: out.write( str(len(Factors[i].values)) + "\n") s = Factors[i].scope c = len(s)*[0] for _ in range(Factors[i].dimension): out.write(" "+ str(Factors[i].getValue(c))) for l in reversed(range(len(c))): c[l] += 1 if c[l] == s[l].num_states: c[l] = 0 else: break out.write(" \n") out.write( str(len(U.values)) + "\n") for val in U.values: out.write( " " + str(val)) out.write(" \n") out.close() ## print "running variable elimination..." ## stime = time.clock() ## Z, tw, wtw, mem = VariableElimination(Factors, OrderedVariables) ## etime = time.clock() - stime ## print "done. \033[91m[%gs]\033[0m" % etime ## Z.printOut() print "Elapsed time: \033[91m%gs\033[0m" % (time.clock()-start)