def main (): """ Fresh population of NN individuals are evalutated, paired with their fitness scores and ordered by fitness in descending order (fittest first) """ # Rank first random population pop = createPop() pairedPop = pairPop(pop) # ordered by fitness in descending order rankedPop = sorted(pairedPop, key=itemgetter(-1), reverse=True) # tst.correctlyranked(rankedPop) # Keep iterating new pops until max_iterations iters = 0 tops, avgs = [], [] while iters != max_iterations: if iters%1 == 0: print 'Iteration'.rjust(150), iters newpopW = evolveNewPop(rankedPop) rankedPop, toperr, avgerr = rankPop(newpopW,pop) tops.append(toperr) avgs.append(avgerr) iters+=1 # test a NN with the fittest weights tester = NN () fittestWeights = [ x[0] for x in rankedPop ] tester.assignWeights(fittestWeights, 0) results, targets = tester.test(testpat) x = np.arange(0,150) title2 = 'Test after '+str(iters)+' iterations' plt.title(title2) plt.ylabel('Node output') plt.xlabel('Instances') plt.plot(results, 'xr', linewidth=1.5, label='Results') plt.plot(targets, 's', color='black', linewidth=3, label='Targets') plt.legend(loc='lower right') plt.figure(2) plt.subplot(121) plt.title('Top individual error evolution') plt.ylabel('Inverse error') plt.xlabel('Iterations') plt.plot( tops, '-g', linewidth=1) plt.subplot(122) plt.plot( avgs, '-g', linewidth=1) plt.title('Population average error evolution') plt.ylabel('Inverse error') plt.xlabel('Iterations') plt.show() print 'max_iterations',max_iterations,'\tpop_size', \ pop_size,'pop_size*0.15', \ int(pop_size*0.15),'\tmutation_rate', \ NN.mutation_rate,'crossover_rate', \ NN.crossover_rate,'ni, nh, no', NN.ni, NN.nh, NN.no
def main(): """ Fresh population of NN individuals are evalutated, paired with their fitness scores and ordered by fitness in descending order (fittest first) """ # Rank first random population pop = createPop() pairedPop = pairPop(pop) # ordered by fitness in descending order rankedPop = sorted(pairedPop, key=itemgetter(-1), reverse=True) # tst.correctlyranked(rankedPop) # Keep iterating new pops until max_iterations iters = 0 tops, avgs = [], [] while iters != max_iterations: if iters % 1 == 0: print 'Iteration'.rjust(150), iters newpopW = evolveNewPop(rankedPop) rankedPop, toperr, avgerr = rankPop(newpopW, pop) tops.append(toperr) avgs.append(avgerr) iters += 1 # test a NN with the fittest weights tester = NN() fittestWeights = rankedPop[0][0] tester.assignWeights(fittestWeights) results, targets = tester.test(testpat) title2 = 'Test after ' + str(iters) + ' iterations' plt.title(title2) plt.ylabel('Node output') plt.xlabel('Instances') plt.plot(results, 'xr', linewidth=1.5, label='Results') plt.plot(targets, 's', color='black', linewidth=3, label='Targets') plt.legend(loc='lower right') plt.figure(2) plt.subplot(121) plt.title('Top individual error evolution') plt.ylabel('Inverse error') plt.xlabel('Iterations') plt.plot(tops, '-g', linewidth=1) plt.subplot(122) plt.plot(avgs, '-g', linewidth=1) plt.title('Population average error evolution') plt.ylabel('Inverse error') plt.xlabel('Iterations') plt.show() print 'max_iterations',max_iterations,'\tpop_size', \ NN.pop_size,'pop_size*0.15', \ int(NN.pop_size*0.15),'\tmutation_rate', \ NN.mutation_rate,'crossover_rate', \ NN.crossover_rate,'ni, nh, no', NN.ni, NN.nh, NN.no
def testFittestInd(rankedPop): tester = NN () fittestWeights = rankedPop[0][0] tester.assignWeights(fittestWeights) count = 0 for i, t in tester.test_pat: o = tester.activate(i) print i, o, t, '++' if list(o) == list(t) else '----' count += 1 if list(o) == list(t) else -1 print float(count) / len(tester.test_pat) err, per = tester.sumErrors(test_data=True) print per return err, per
def testFittestInd(rankedPop): tester = NN() fittestWeights = rankedPop[0][0] tester.assignWeights(fittestWeights) count = 0 for i, t in tester.test_pat: o = tester.activate(i) print i, o, t, '++' if list(o) == list(t) else '----' count += 1 if list(o) == list(t) else -1 print float(count) / len(tester.test_pat) err, per = tester.sumErrors(test_data=True) print per return err, per
def testFittestInd(rankedPop): tester = NN() fittestWeights = rankedPop[0][0] tester.assignWeights(fittestWeights) right = 0 for i, t, c, l in tester.test_pat: o = tester.activate(i) print i, o, t, '++' if list(o) == list(t) else '----' right += 1 if list(o) == list(t) else 0 print right, len(tester.test_pat) print 100 * (float(right) / len(tester.test_pat)) err, per = tester.sumErrors(test_data=True) print per return err, per
def testFittestInd(rankedPop): tester = NN () fittestWeights = rankedPop[0][0] tester.assignWeights(fittestWeights) right = 0 for i, t, c, l in tester.test_pat: o = tester.activate(i) print i, o, t, '++' if list(o) == list(t) else '----' right += 1 if list(o) == list(t) else 0 print right, len(tester.test_pat) print 100 * (float(right) / len(tester.test_pat)) err, per = tester.sumErrors(test_data=True) print per return err, per
def testFittestInd(rankedPop): tester = NN () fittestWeights = rankedPop[0][0] tester.assignWeights(fittestWeights) return tester.sumErrors(test_data=True)
def testFittestInd(rankedPop): tester = NN() fittestWeights = rankedPop[0][0] tester.assignWeights(fittestWeights) return tester.sumErrors(test_data=True)