예제 #1
0
def genetic_algorithm(make_hypothesis, data, mutate, crossover, population_size=100, generations=100000):

    population = [make_hypothesis() for _ in xrange(population_size)]
    for h in population:
        h.compute_posterior(data)

    for g in xrange(generations):

        nextpopulation = []

        while len(nextpopulation) < population_size:
            # sample proportional to fitness
            mom = weighted_sample(population, probs=[v.posterior_score for v in population], log=True)
            dad = weighted_sample(population, probs=[v.posterior_score for v in population], log=True)

            try:
                kid = mutate(crossover(mom, dad))
            except (ProposalFailedException, NodeSamplingException):
                continue

            kid.compute_posterior(data)
            yield kid

            nextpopulation.append(kid)

            # # if MH_acceptance(population[i].posterior_score, kid.posterior_score, 0.0):
            # if kid.posterior_score > population[i].posterior_score:
            #     population[i] = kid
            #     yield kid
        population = nextpopulation
예제 #2
0
def genetic_algorithm(make_hypothesis,
                      data,
                      mutate,
                      crossover,
                      population_size=100,
                      generations=100000):

    population = [make_hypothesis() for _ in xrange(population_size)]
    for h in population:
        h.compute_posterior(data)

    for g in xrange(generations):

        nextpopulation = []

        while len(nextpopulation) < population_size:
            # sample proportional to fitness
            mom = weighted_sample(
                population,
                probs=[v.posterior_score for v in population],
                log=True)
            dad = weighted_sample(
                population,
                probs=[v.posterior_score for v in population],
                log=True)

            try:
                kid = mutate(crossover(mom, dad))
            except (ProposalFailedException, NodeSamplingException):
                continue

            kid.compute_posterior(data)
            yield kid

            nextpopulation.append(kid)

            # # if MH_acceptance(population[i].posterior_score, kid.posterior_score, 0.0):
            # if kid.posterior_score > population[i].posterior_score:
            #     population[i] = kid
            #     yield kid
        population = nextpopulation
예제 #3
0
파일: Demo.py 프로젝트: TerryLew/BinLOTlib
if __name__ == "__main__":

    from LOTlib import break_ctrlc
    from LOTlib.Examples.Number.Model import make_hypothesis, make_data
    from LOTlib.Inference.Samplers.MetropolisHastings import MHSampler

    from LOTlib.MCMCSummary import *

    for h in break_ctrlc(
            Print(PosteriorTrace(MHSampler(make_hypothesis(),
                                           make_data(100))))):
        pass

    # pt = PosteriorTrace(plot_every=1000, window=False)
    #
    # for h in break_ctrlc(pt(MHSampler(make_hypothesis(), make_data(100)))):
    #     print h
예제 #4
0
    def __enter__(self):
        for a in self.outputs:
            a.__enter__()

    def __exit__(self, t, value, traceback):
        """ I defaultly call all of my children's exits """
        for a in self.outputs:
            a.__exit__(t, value, traceback)
        return False


if __name__ == "__main__":

    from LOTlib import break_ctrlc
    from LOTlib.Examples.Number.Model import make_hypothesis, make_data
    from LOTlib.Inference.Samplers.MetropolisHastings import MHSampler
    from LOTlib.SampleStream import *

    sampler = break_ctrlc(MHSampler(make_hypothesis(), make_data()))

    for h in SampleStream(sampler) >> Tee(Skip(2) >> Unique() >> Print(), PosteriorTrace()) >> Z():
        pass







예제 #5
0
파일: Demo.py 프로젝트: joshrule/LOTlib
if __name__ == "__main__":

    from LOTlib import break_ctrlc
    from LOTlib.Examples.Number.Model import make_hypothesis, make_data
    from LOTlib.Inference.Samplers.MetropolisHastings import MHSampler

    from LOTlib.MCMCSummary import *

    for h in break_ctrlc(Print(PosteriorTrace(MHSampler(make_hypothesis(), make_data(100))))):
        pass

    # pt = PosteriorTrace(plot_every=1000, window=False)
    #
    # for h in break_ctrlc(pt(MHSampler(make_hypothesis(), make_data(100)))):
    #     print h