Пример #1
0
 def advance_generation(self):
     self.mark_oldno()
     self.sort()
     self.update_no()
     for dude in self:
         k,l=self.imap[dude.oldno]
         self.map[k,l]=dude.no
     self.update_imap()
     Population.advance_generation(self)
Пример #2
0
 def advance_generation(self):
     self.mark_oldno()
     self.sort()
     self.update_no()
     for dude in self:
         k, l = self.imap[dude.oldno]
         self.map[k, l] = dude.no
     self.update_imap()
     Population.advance_generation(self)
Пример #3
0
# saving initial generation characteristics
g_rec.append(parents.gg)
bs_rec.append(parents[0].score)
ws_rec.append(parents[-1].score)
T_rec.append(sa_T)
ms_rec.append(sa_mstep)

#-------------------------------------------------------------------------------
#--- part 3: the generational loop ---------------------------------------------
#-------------------------------------------------------------------------------

for g in range(G):

    # step A: creating and evaluating offspring
    offspring.advance_generation()
    for i, dude in enumerate(offspring):
        oldguy = parents[i]
        if i < elite_size:
            dude.copy_DNA_of(oldguy, copyscore=True)
        elif rand() < saga_ratio:
            dude.copy_DNA_of(oldguy)
            dude.mutate(sa_mprob, sd=sa_mstep)
            dude.evaluate()
            if dude < oldguy:
                pass  # accepting improvement
            elif rand() < exp(-(dude.score - oldguy.score) / sa_T):
                pass  # accepting improvement
            else:
                dude.copy_DNA_of(oldguy,
                                 copyscore=True)  # preferring parent DNA
Пример #4
0
        g_rec.append(g)
        s_rec.append(dude.score)
    ms_rec.append(mstep)
    successrate=np.sum(successes)/float(np.size(successes))
    if successrate > 1./5:      # 1/5th success rule is popular because reasonable for evolution strategies
        mstep*=1.05
    else:
        mstep/=1.05
    offspring.sort()
    for i in range(mu):
        parents[i].copy_DNA_of(offspring[i],copyscore=True)
        
    if mod(g+1,20)==0:
        parents[0].plot_yourself(plotpath)

    parents.advance_generation()    
    
print 'fitness of final population: '
print parents.get_scores()
print 'final mstep: ',mstep, 2*'\n'



#-------------------------------------------------------------------------------
#--- part 4: making a plot showing ---------------------------------------------
#--- a) fitness over time and --------------------------------------------------
#--- b) step size over time ----------------------------------------------------
#-------------------------------------------------------------------------------

N=len(s_rec)
g_rec=np.array(g_rec)-0.4+0.8*npr.rand(N) # a little random horizontal whiggeling for better visibility
Пример #5
0
# saving initial generation characteristics
g_rec.append(parents.gg)
bs_rec.append(parents[0].score)
ws_rec.append(parents[-1].score)
T_rec.append(sa_T)
ms_rec.append(sa_mstep)


#-------------------------------------------------------------------------------
#--- part 3: the generational loop ---------------------------------------------
#-------------------------------------------------------------------------------

for g in range(G):
    
    # step A: creating and evaluating offspring
    offspring.advance_generation()
    for i,dude in enumerate(offspring):
        oldguy=parents[i]
        if i < elite_size:
            dude.copy_DNA_of(oldguy,copyscore=True)
        elif rand() < saga_ratio:
            dude.copy_DNA_of(oldguy)
            dude.mutate(sa_mprob,sd=sa_mstep)
            dude.evaluate()
            if dude<oldguy:
                pass # accepting improvement
            elif rand() < exp(-(dude.score-oldguy.score)/sa_T):
                pass # accepting improvement
            else:
                dude.copy_DNA_of(oldguy,copyscore=True) # preferring parent DNA
        else:
Пример #6
0
        g_rec.append(g)
        s_rec.append(dude.score)
    ms_rec.append(mstep)
    successrate = np.sum(successes) / float(np.size(successes))
    if successrate > 1. / 5:  # 1/5th success rule is popular because reasonable for evolution strategies
        mstep *= 1.05
    else:
        mstep /= 1.05
    offspring.sort()
    for i in range(mu):
        parents[i].copy_DNA_of(offspring[i], copyscore=True)

    if mod(g + 1, 20) == 0:
        parents[0].plot_yourself(plotpath)

    parents.advance_generation()

print 'fitness of final population: '
print parents.get_scores()
print 'final mstep: ', mstep, 2 * '\n'

#-------------------------------------------------------------------------------
#--- part 4: making a plot showing ---------------------------------------------
#--- a) fitness over time and --------------------------------------------------
#--- b) step size over time ----------------------------------------------------
#-------------------------------------------------------------------------------

N = len(s_rec)
g_rec = np.array(g_rec) - 0.4 + 0.8 * npr.rand(
    N)  # a little random horizontal whiggeling for better visibility
l1 = plt.plot(g_rec, s_rec, 'bo', label='fitness')