Example #1
0
class Boa(PopulationDistribution):
    """The Bayesian Optimization Algorithm of Martin Pelikan. """
    config = Config(
        variableGenerator=BinaryVariable,
        branchFactor=3,
        structureGenerator=GreedyStructureSearch(
            3, BayesianInformationCriterion()),
        randomizer=lambda net: TernaryString(0L, -1L, net.numVariables),
        sampler=DAGSampler(),
        data=None,
        truncate=0.60,  # percentage of the population to keep
        space=BinaryReal(realDim=5),
        history=SortedMarkovHistory)

    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 compatible(self, history):
        return hasattr(history, "lastPopulation") and history.sorted

    def sample(self):
        x = self.network.__call__()
        cnt = 0
        while not self.config.space.in_bounds(x):
            if cnt > 10000:
                raise ValueError(
                    "Rejection sampling failed after 10,000 attempts in BOA")
            x = self.network.__call__()
            cnt += 1
        return x

    def batch(self, num):
        return [self.sample() for i in xrange(num)]

    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)
Example #2
0
class Boa(PopulationDistribution):
   """The Bayesian Optimization Algorithm of Martin Pelikan. """
   config = Config(variableGenerator=BinaryVariable,
                   branchFactor=3,
                   structureGenerator=GreedyStructureSearch(3, BayesianInformationCriterion()),
                   randomizer = lambda net: TernaryString(0L, -1L, net.numVariables),
                   sampler = DAGSampler(),
                   data = None,
                   truncate = 0.60, # percentage of the population to keep
                   space = BinaryReal(realDim=5),
                   history=SortedMarkovHistory)

   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 compatible(self, history):
      return hasattr(history, "lastPopulation") and history.sorted

   def sample(self):
      x = self.network.__call__()
      cnt = 0
      while not self.config.space.in_bounds(x):
         if cnt > 10000:
            raise ValueError("Rejection sampling failed after 10,000 attempts in BOA")
         x = self.network.__call__()
         cnt += 1
      return x

   def batch(self, num):
      return [self.sample() for i in xrange(num)]

   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)