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


Python Brain.crossover方法代码示例

本文整理汇总了Python中brain.Brain.crossover方法的典型用法代码示例。如果您正苦于以下问题:Python Brain.crossover方法的具体用法?Python Brain.crossover怎么用?Python Brain.crossover使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在brain.Brain的用法示例。


在下文中一共展示了Brain.crossover方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from brain import Brain [as 别名]
# 或者: from brain.Brain import crossover [as 别名]
class Critter:
	def __init__(self, pos):
		"""
			Initializes a critter creating his brain, setting
			his energy and age using the settings functions
			and assigning it the given position

			Parameter pos is a tuple with the coordinates 
			of the critter in the world 
		"""
		self.brain  = Brain()
		self.energy = Critter_s.start_energy()
		self.age    = Critter_s.start_age()
		self.pos = pos

	def process(self, world, partners = None):
		"""
			Moves this critter, makes this critter learn and 
			returns list of children from the available partners that this
			critter decided to mate with

			Parameter world is a grid (environment.Grid) of the world
			Parameter partners is a list of critters available for mating
		"""
		sub = world.sub_grid(self.pos)

		inputs  = sub.values + [self.energy]		# list composed by what the critter sees plus its energy
		outputs = self.brain.process(inputs, 0)		# list of four values indicating the movement
		action  = parse_move(outputs)				# resulting movement vector obtained from the outputs 

		best = evaluate_move(sub, outputs)	# evaluate the best possible move
		self.brain.learn(inputs, best, 0)	# make the critter learn in case he took the wrong direction 

		# assign new position
		new_pos = [0, 0] 
		new_pos[0] = max(0, min(self.pos[0] + action[0], Environment_s.width -1))
		new_pos[1] = max(0, min(self.pos[1] + action[1], Environment_s.height -1))

		# decrease energy and increase age
		if new_pos != self.pos:
			self.energy -= Critter_s.move_cost
		else:
			self.energy -= Critter_s.stand_cost
		self.age += 1

		# move if the new position isn't occupied
		if world.access_value(new_pos[0], new_pos[1]) != Environment_s.grid_crit:
			self.pos = tuple(new_pos)
		#ToDo decide if stay, push or kill

		children = []
		if partners != None and self.age >= Critter_s.min_mate_age and self.energy >= Critter_s.mate_cost:				# if this critter is in condition to mate...
			for partner in partners:
				partner_constraints = partner.age >= Critter_s.min_mate_age2 and partner.energy >= Critter_s.mate_cost2

				inputs = [partner.energy, partner.age, self.energy]
				mate_decision = round(self.brain.process(inputs, 1)[0])

				if partner_constraints and mate_decision:																# ...the partner is in condition to mate,
					children.append(self.crossover(partner))															# and this critter wants to, then they mate 
		return children

	def crossover(self, partner):
		"""
			Returns the critters' child and decreases energy to
			this critter and the partner

			Parameter partner is the critter's partner
		"""
		self.energy    -= Critter_s.mate_cost
		partner.energy -= Critter_s.mate_cost2

		child = Critter(Critter_s.randpos())
		child.brain = self.brain.crossover(partner.brain)
		return child

	def copy(self):
		"""
			Returns a copy of this critter
		"""
		critter = Critter(self.pos)
		critter.brain  = self.brain.copy()
		critter.energy = self.energy
		critter.age    = self.age
		return critter

	def eat(self, food = Environment_s.def_food):
		"""
			Increases energy depending on the food type

			Parameter food is the food type, the constants are on the settings. Leave blank for deafult food
		"""
		self.energy += Critter_s.food_reward[food]

	def collide(self):
		"""
			Decreases energy on collision
		"""
		self.energy -= Critter_s.coll_cost
		# ToDo different kind of collisions 
#.........这里部分代码省略.........
开发者ID:nglgzz,项目名称:tale,代码行数:103,代码来源:critter.py


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