def runTrial(iters): """ Fresh population of NN individuals are evalutated, paired with their fitness scores and ordered by fitness in descending order (fittest first) """ pop = createPop() pairedPop = pairPop(pop, verbose=False) rankedPop = sorted(pairedPop, key=itemgetter(-1), reverse=True) all_results = { 'train_mses_top': [], 'train_mses_avg': [], 'train_percents_avg': [], 'train_percents_top': [], 'test_mse': 0, 'test_percent': 0 } for iters in range(iters): # raw_input('==============NEXT==============') if not iters % 1: print 'Iteration'.rjust(120), iters newpopW = evolveNewPop(rankedPop) print '\n \nEVOLVE NEW POP' rankedPop, top_mse, avg_mse, top_per, avg_per = rankPop(newpopW, pop, verbose=False) print top_per all_results['train_mses_top'].append(top_mse) all_results['train_mses_avg'].append(avg_mse) all_results['train_percents_top'].append(top_per) all_results['train_percents_avg'].append(avg_per) test_mse, test_per = testFittestInd(rankedPop) all_results['test_mse'] = test_mse all_results['test_percent'] = test_per return all_results
def setUp(self): self.pop = createPop() self.pairedPop = pairPop(self.pop) self.rankedPop = sorted(self.pairedPop, key=itemgetter(-1), reverse=True) self.rankedWeights = [x[0] for x in self.rankedPop] self.fitnessScores = [x[-1] for x in self.rankedPop] self.newpopW = evolveNewPop(self.rankedPop)
def setUp(self): self.pop = createPop() paired = pairPop(self.pop, verbose=False) ranked = sorted(paired, key=itemgetter(-1), reverse=True) self.newpopW = evolveNewPop(ranked) for i in range(len(self.pop)): self.pop[i].assignWeights(self.newpopW[i])
def setUp(self): self.pop = createPop() self.pairedPop = pairPop(self.pop) self.rankedPop = sorted(self.pairedPop, key=itemgetter(-1), reverse=True) self.rankedWeights = [x[0] for x in self.rankedPop] self.fitnessScores = [x[-1] for x in self.rankedPop] self.newpopW = evolveNewPop(self.rankedPop)
def runTrial(): """ Fresh population of NN individuals are evalutated, paired with their fitness scores and ordered by fitness in descending order (fittest first) """ pop = createPop() pairedPop = pairPop(pop) rankedPop = sorted(pairedPop, key=itemgetter(-1), reverse=True) all_results = { 'train_mses_top' : [], 'train_mses_avg' : [], 'train_percents_avg' : [], 'train_percents_top' : [], 'test_mse' : 0, 'test_percent' : 0 } for iters in range(max_iterations): # raw_input('==============NEXT==============') if not iters % 1: print 'Iteration'.rjust(120), iters newpopW = evolveNewPop(rankedPop) print '\n \nEVOLVE NEW POP' rankedPop, top_mse, avg_mse, top_per, avg_per = rankPop(newpopW, pop, verbose=NN.pop_size) if iters > 0 and iters < 1000: newpopW = trainPP(rankedPop, pop, incr=0.25, decr=0.5) print '\n \nTRAIN PP' rankedPop, top_mse, avg_mse, top_per, avg_per = rankPop(newpopW, pop, verbose=NN.pop_size) all_results['train_mses_top'].append(top_mse) all_results['train_mses_avg'].append(avg_mse) all_results['train_percents_top'].append(top_per) all_results['train_percents_avg'].append(avg_per) test_mse, test_per = testFittestInd(rankedPop) all_results['test_mse'] = test_mse all_results['test_percent'] = test_per return all_results
def setUp(self): self.pop = createPop() paired = pairPop(self.pop, verbose=False) ranked = sorted(paired, key=itemgetter(-1), reverse=True) self.newpopW = evolveNewPop(ranked) for i in range(len(self.pop)): self.pop[i].assignWeights(self.newpopW[i])
def runTrial(): """ Fresh population of NN individuals are evalutated, paired with their fitness scores and ordered by fitness in descending order (fittest first) """ pop = createPop() pairedPop = pairPop(pop) rankedPop = sorted(pairedPop, key=itemgetter(-1), reverse=True) all_results = { 'train_mses_top' : [], 'train_mses_avg' : [], 'train_percents_avg' : [], 'train_percents_top' : [], 'test_mse' : 0, 'test_percent' : 0 } for iters in range(max_iterations): if not iters % 1: print 'Iteration'.rjust(150), iters newpopW = evolveNewPop(rankedPop) rankedPop, top_mse, avg_mse, top_per, avg_per = rankPop(newpopW, pop) all_results['train_mses_top'].append(top_mse) all_results['train_mses_avg'].append(avg_mse) all_results['train_percents_top'].append(top_per) all_results['train_percents_avg'].append(avg_per) test_mse, test_per = testFittestInd(rankedPop) all_results['test_mse'] = test_mse all_results['test_percent'] = test_per return all_results
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 setUp(self): self.pop = createPop() pairedPop = pairPop(self.pop) rankedPop = sorted(pairedPop, key=itemgetter(-1), reverse=True) self.newpopW = evolveNewPop(rankedPop)