当前位置: 首页>>代码示例>>Python>>正文


Python Population.generations方法代码示例

本文整理汇总了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]))
开发者ID:kuba-orlik,项目名称:turing-machine-learning,代码行数:32,代码来源:ws_test.py

示例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))
开发者ID:kuba-orlik,项目名称:turing-machine-learning,代码行数:69,代码来源:test3.py


注:本文中的population.Population.generations方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。