本文整理汇总了Python中population.Population.xrange_population方法的典型用法代码示例。如果您正苦于以下问题:Python Population.xrange_population方法的具体用法?Python Population.xrange_population怎么用?Python Population.xrange_population使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类population.Population
的用法示例。
在下文中一共展示了Population.xrange_population方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Genetic
# 需要导入模块: from population import Population [as 别名]
# 或者: from population.Population import xrange_population [as 别名]
class Genetic(object):
"""genetic layer"""
def __init__(self,epoch_num,pop_len,field_size=(20,15), delta = 0):
super(Genetic, self).__init__()
self.size = field_size
# self.field = Field(self)
self.pop_len = pop_len
self.population = Population(self.pop_len,8,4,delta = delta)
self.epoch_num = epoch_num
self.score_history = []
self.best_pers = None
self.best_ever = [-1,None]
self.time = None
self.bp_hist = []
############ ADD COEFFICIENTS!!!!
def del_info(self,field,d = 2):
field = field[d:-d]
field = [i[d:-d] for i in field]
return field
def add_coefficient(self,n,f):
COEFF[n]=f
def get_coefficient(self,n):
return COEFF[n]
# def start(self):
def start(self, f = None, analyze = 0):
self.time = time.time()
if DBG:
print '---------------------'
print 'New genetic'
print 'Length of population:',self.pop_len
print 'Number of epochs:',self.epoch_num
print 'Process: ' ,
self.score_history = []
for i in xrange(self.epoch_num):
fld = Field(self.size)
fld.generate()
pos = (random.randint(1,self.size[0]-1),random.randint(1,self.size[1]-1))# random start position
pop_score = []
for j in self.population.xrange_population():
pers,score = self.move_population(j,pos,copy.deepcopy(fld))
pop_score.append([j,pos,copy.deepcopy(fld),pers,score]) #neuronet,position,field,person,score
self.population.population[j] = score
best,nnb = MINUS_INFINITY,None
for j in pop_score[:]:
if j[-1]>best:
best,nnb = j[-1],j
if nnb is None:
print pop_score
raise Exception('Zero lifetime')
# print nnb.history[:]
self.score_history.append(best)
self.best_pers = nnb[:]
if analyze:
self.analyze(self.best_pers[:])
if self.best_ever[0]<best:
self.best_ever = [best,nnb[:]][:]
self.generate_new_population()
# if DBG: print `i+1` if not (i+1)%5 or i==0 else '.' ,
if DBG and not (i+1)%5 or i==0: print `i+1`
if f is not None:
if not f(i,self.best_ever):
break
print '\n'
if i < self.epoch_num:
print 'Stopped at epoch number:',i
self.epoch_num = i
print 'Score history last:',self.score_history[-1]
# print pop_score
self.save_best_neuronet()
self.save_last_field()
self.time = time.time() - self.time
print 'Time: ',self.time
print '---------------------'
return self.epoch_num
def do_up(a):
i = 0
yield a
while up(a):
yield a
i +=1
if i > 1000000: # CONSTANT! BAD
break
def up(self,a,i=0,i_max=2):
a[i] += AN_ST
if a[i]>AN_HG:
a[i] = AN_LOW
if (i<len(a)-1) and (i<i_max):
return self.up(a,i+1)
else:
return False
#.........这里部分代码省略.........