本文整理汇总了Python中population.Population.generations方法的典型用法代码示例。如果您正苦于以下问题:Python Population.generations方法的具体用法?Python Population.generations怎么用?Python Population.generations使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类population.Population
的用法示例。
在下文中一共展示了Population.generations方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from population import Population [as 别名]
# 或者: from population.Population import generations [as 别名]
else:
return i
class Mutable:
def __init__(self, number=None):
if(number==None):
self.number = randint(0, 10)
else:
self.number = number
def mutate(self):
rr = randint(-1,1)
return Mutable(nz(self.number + rr))
def __mul__(self, other):
return Mutable(max(self.number, other.number))
def generator():
return Mutable()
def fitness_function(a):
return a.number
p = Population(generator, 10, fitness_function)
p.generations(1000)
print("fitness:", fitness_function(p.population[0]))
示例2: go
# 需要导入模块: from population import Population [as 别名]
# 或者: from population.Population import generations [as 别名]
def go():
def is_power_of_two(i):
while i>1:
if i%2 == 1:
return False
i = i // 2
return True
def grade_answers(htm, steps):
tp = 0
fp = 0
tn = 0
fn = 0
for i in range(steps + 1):
res = htm.decide(i)
truth = is_power_of_two(i)
if truth == True:
if res == True:
tp += 1
elif res == False:
fp += 1
else:
fn += 1 #dubious?
else:
if res == True:
fn += 1
elif res == False:
tn += 1
else:
fp += 1 #dubious?
return f(tp, fp, fn, 0.5)
def f(true_positive, false_positive, false_negative, beta=1):
top = ((1 + beta ** 2) * true_positive)
bottom = ((1+beta**2)*true_positive + beta**2 * false_negative + false_positive)
if bottom == 0:
return 0
else:
return top/bottom
def fitness_function(mhltm, sample_size=33):
htm = mhltm.htm
trivial_cases = range(0,5)
for i in trivial_cases:
if is_power_of_two(i) != htm.decide(i):
return 0
# return 1 + 99 * (grade_answers(htm, sample_size)/len(htm.states))
return 1 + 99 * (grade_answers(htm, sample_size))
def generator():
return MutableHLTM(12, number_encoders.unary, 1)
p = Population(generator, 50, fitness_function)
p.generations(1000)
print(p.population[0].to_dot())
print("\n\n")
print("fitness_function(p.population[0])", fitness_function(p.population[0]))
print(fitness_function(p.population[0], 200))
for i in range(17):
print(i, p.population[0].htm.decide(i))