def __init__(self, **kwargs): super(Boa, self).__init__(**kwargs) self.network = BayesNet(numVariables=self.config.space.dim, **self.config.__properties__) self.network = StructureProposal(**self.config.__properties__).search( self.network) self.trained = False
def parse(self, fname): f = open(fname) totalLine = "" done = False for line in f: totalLine += line lefts = len(totalLine.split("(")) rights = len(totalLine.split(")")) if lefts == rights: self.processLine(totalLine) totalLine = "" categories = [[]] * self.index for name, idx in self.indexMap.iteritems(): categories[idx] = self.variables[name]['vals'] cfg = Config() cfg.numVariables = len(self.variables) cfg.variableGenerator = MultinomialVariableGenerator(categories) cfg.randomizer = MultinomialRandomizer() cfg.sampler = DAGSampler() cfg.structureGenerator = StructureProposal(cfg) net = BayesNet(cfg) for variable in net.variables: variable.tables = self.variables[self.revIndexMap[ variable.index]]['cpt'] #print names[variable.index], self.variables[self.revIndexMap[variable.index]]['parents'] variable.known = [ self.indexMap[parent] for parent in self.variables[ self.revIndexMap[variable.index]]['parents'] ] variable.known = sorted(variable.known) variable.parents = dict([(i, net.variables[i]) for i in variable.known]) net.dirty = True net.computeEdgeStatistics() """ for variable in net.variables: print "(var ", self.revIndexMap[variable.index], " (", " ".join(variable.categories[variable.index]), "))" for variable in net.variables: print "(parents ", self.revIndexMap[variable.index], " (", " ".join([self.revIndexMap[i] for i in variable.known]), ") " for key, val in variable.tables.iteritems(): if key == "": expanded = "" else: cfg = array([int(num) for num in key.split(",")]) expanded = " ".join(self.variables[self.revIndexMap[variable.known[k]]]['vals'][c-1] for k,c in enumerate(cfg)) total = val.sum() vals = " ".join([str(i) for i in val]) print "((", expanded, ") ", vals, (1. - total), ")" print ")" """ return net
def update(self, history, fitness): super(Boa, self).update(history, fitness) if history.lastPopulation() is None: return population = [x for x, s in history.lastPopulation()] selected = int(self.config.truncate * len(population)) self.network = BayesNet(numVariables=self.config.space.dim, **self.config.__properties__) self.network.config.data = None self.network = StructureProposal( **self.network.config.__properties__).search(self.network) self.network.config.data = population self.network.structureSearch(population) self.network.update(self.history.updates, population)
def __init__(self, space): self.space = space self.sampler = DAGSampler() if hasattr(space, 'dim'): self.numVariables = space.dim else: raise ValueError("Cannot determine the number of dimensions " "for BinaryBayesStructure space based on given " "space; expected space to have property dim " "indicating the number of required variables. ") super(BinaryBayesStructure, self).__init__(self.numVariables**2) self.config = Config(numVariables=self.numVariables, variableGenerator=self.variable, randomizer=self.randomize, sampler=self.sample) self.proposal = StructureProposal(**self.config.__properties__) self.config.structureGenerator = self.proposal