def test_ann(self):
        from pybrain.datasets.classification import ClassificationDataSet
        # below line can be replaced with the algorithm of choice e.g.
        # from pybrain.optimization.hillclimber import HillClimber
        from pybrain.optimization.populationbased.ga import GA
        from pybrain.tools.shortcuts import buildNetwork

        # create XOR dataset
        d = ClassificationDataSet(2)
        d.addSample([181, 80], [1])
        d.addSample([177, 70], [1])
        d.addSample([160, 60], [0])
        d.addSample([154, 54], [0])
        d.setField('class', [ [0.],[1.],[1.],[0.]])

        nn = buildNetwork(2, 3, 1)
        # d.evaluateModuleMSE takes nn as its first and only argument
        ga = GA(d.evaluateModuleMSE, nn, minimize=True)
        for i in range(100):
            nn = ga.learn(0)[0]

        print nn.activate([181, 80])
def runTrial(iters):
    kw = {'minimize':True,  
        'verbose':True, 'mutationProb':0.1, 'populationSize':150,
        'storeAllEvaluations':True, 'storeAllPopulations':True,
        'elitism':True, 'tournament':False}
    e = Experiment(**kw)
    # e.buildXor()
    e.buildParity()
    # e.buildIris()

    # trainer = bp(e.nn, dataset=trn_data, verbose=True)
    # trainer.trainEpochs(100)

    ga = GA(testOnTrainData, e.nn, **kw)
    # ga = GA(trn_data.evaluateModuleMSE, e.nn, **kw)
    # tmp_nn = e.nn.newSimilarInstance()
    perc_errs = []
    for i in range(iters):
        ga.learn(0)
        perc_errs.append(ga._bestFound()[1])
        fits = array(ga.fitnesses)
        print '\t\t\tmean', mean(fits), 'std', std(fits), 'max', max(fits), 'min', min(fits)
        for i, t in trn_data:
            o = ga._bestFound()[0].activate(i)
            if o != t:
                print i, o, t, '++' if o == t else '----'

        # ranked_pop = ga._allGenerations[i]
        # ranked_pop = array(sorted(ga._allGenerations[i], key=lambda x: x[1]))
        # for ind in range(len(ranked_pop[0])):
        #     tmp_nn.params[:] = array(ranked_pop[0][ind]).copy()
            # print ranked_pop[1][ind], testOnTrainData(tmp_nn), int(testOnTrainData(tmp_nn)) * '.'
            # astrocytes = associateAstrocyteLayers(tmp_nn)
            # trainNGA(tmp_nn, trn_data, astrocytes[0], astrocytes[1])

        # ga.currentpop = new_pop
    return perc_errs
def runTrial(ds, iters):
    kw = {'minimize':True,  
        'verbose':True, 'mutationProb':0.1, 'populationSize':150,
        'storeAllEvaluations':True, 'storeAllPopulations':True,
        'elitism':True, 'tournament':False}
    e = setUpExperiment(ds, **kw)
    ga = GA(testOnTrainData, e.nn, **kw)
    e.results['trn_per_err'] = []
    for i in range(iters):
        print ga.learn(0)
        e.results['trn_per_err'].append(ga._bestFound()[1])
        fits = array(ga.fitnesses)
        print '\t\t\tmean', mean(fits), 'std', std(fits), 'max', max(fits), 'min', min(fits)
        for i, t in trn_data:
            o = ga._bestFound()[0].activate(i)
            if where(o != t, True, False).any():
                print i, o, argmax(t), '----'
    e.results['tst_per_err'].append(testOnTestData(ga._bestFound()[0], e.tst_data))
    return e
from pybrain.datasets.classification import ClassificationDataSet
# below line can be replaced with the algorithm of choice e.g.
# from pybrain.optimization.hillclimber import HillClimber
from pybrain.optimization.populationbased.ga import GA
from pybrain.tools.shortcuts import buildNetwork

# create XOR dataset
d = ClassificationDataSet(2)
d.addSample([0., 0.], [0.])
d.addSample([0., 1.], [1.])
d.addSample([1., 0.], [1.])
d.addSample([1., 1.], [0.])
# d.setField('class', [ [0.],[1.],[1.],[0.]])

nn = buildNetwork(2, 3, 5, 9, 5, 3, 1)
# d.evaluateModuleMSE takes nn as its first and only argument
ga = GA(d.evaluateModuleMSE, nn, minimize=True)
for i in range(500):
    nn = ga.learn(0)[0]

print nn.activate([0,0])
print nn.activate([1,0])
print nn.activate([0,1])
print nn.activate([1,1])