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


Python World.advance方法代码示例

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


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

示例1: Scenario

# 需要导入模块: from world import World [as 别名]
# 或者: from world.World import advance [as 别名]
class Scenario(object):  # abstract
    __metaclass__ = ScenarioRegistrar
    world_size = (640, 480)  # Override this to customize world size
    walls = True

    def __init__(self, parameter, agent):
        self.parameter = parameter
        self.agent = agent
        self.world = World(self.world_size)
        self.world.init()
        self.init()

        if self.walls:
            self.add_walls()

        self.world.add_unit(agent)

    def __repr__(self):
        return "{0}({1})".format(self.__class__.__name__, self.parameter)

    def add_walls(self):
        """ Convenience method to add obstacles along all borders of the rendered world.

        Call this when setting up the world to prevent units from "escaping"
        """
        self.world.add_obstacle(obstacle.Line(Vec2d(0, 0), Vec2d(0, self.world_size[1])))
        self.world.add_obstacle(obstacle.Line(Vec2d(0, self.world_size[1]), Vec2d(self.world_size[0], self.world_size[1])))
        self.world.add_obstacle(obstacle.Line(Vec2d(self.world_size[0], self.world_size[1]), Vec2d(self.world_size[0], 0)))
        self.world.add_obstacle(obstacle.Line(Vec2d(self.world_size[0], 0), Vec2d(0, 0)))

    def update(self, dt):
        pass

    def run(self):
        overtime = -1
        agent_out_time = 0
        while 1:
            if self.agent.position.distance_to(self.agent.goal) <= self.agent.radius:
                ## for Crossing scenario only
                if (agent_out_time == 0):
                    self.world.remove_unit(self.agent)
                    agent_out_time = self.world._time
                overtime = overtime + 1
                if overtime == 0:
                    if(len(self.world.avg_groundspeed_list)):
                        print "Average Average Ground Speed:", sum(self.world.avg_groundspeed_list) / len(self.world.avg_groundspeed_list)
                        print "Agent Ground Speed:", self.agent.travel_length / agent_out_time
                    if(len(self.world.collision_list)):
                        print "Average Collisions:", sum(self.world.collision_list) / len(self.world.collision_list)
                        print "Agent Collisions:", self.agent.collisions
                    return True

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return False

            dt = self.world.advance()
            self.update(dt)
开发者ID:carson,项目名称:keiro,代码行数:60,代码来源:scenario.py

示例2: simulate

# 需要导入模块: from world import World [as 别名]
# 或者: from world.World import advance [as 别名]
def simulate(n, T, graph, source_node, terminal_node):
  """Run an ant-colony optimization simulation"""

  # Instantiate the world
  world = World(graph, source_node = source_node, terminal_node = terminal_node)
  
  # Create the ants
  for i in range(n):
    world.populate(
      Ant(world, world.SOURCE_NODE)
    )

  # Make the ants move until convergence
  for t in range(T):
    for i in range(n):
      world.ants[i].move()
    world.advance()

  return world
开发者ID:ChadFulton,项目名称:simple-aco,代码行数:21,代码来源:simulate.py

示例3: Simulation

# 需要导入模块: from world import World [as 别名]
# 或者: from world.World import advance [as 别名]
class Simulation():
	"""
	Controls the simulation
		- Runs the main loop
		- Detects mouse, keyboard and other events
		- Loads the necessary images
		- Controls the framerate of the simulation
	"""
	def __init__(self):
		self.clock = time.Clock()
		self.framerate = 60
		self.images = {}

		self.quit = False
		self.pause = False

		self.add_image("ant", "ant.png")
		self.add_image("grass", "grass.png")
		self.add_image("food", "food.png")
		self.add_image("home", "home.png")
		self.add_image("obstacle", "obstacle.png")
		self.add_image("home_scent", "home_scent.png")
		self.add_image("food_scent", "food_scent.png")
		self.add_image("cell", "cell.png")

		self.settings = {
		"no_of_ants": 150,
		"evaporation_rate": .03,
		"home_size": 10,
		"cell_size": 10
		}

		self.world = World(130, 70, self.images, self.settings)

	def add_image(self, name, path):
		"""
		Loads an image
		"""
		self.images[name] = image.load('images/' + path)		

	def run(self):
		"""
		Runs the main loop till the user quits
		"""
		while self.quit is False and self.pause is False:
			self.main_loop()

	def main_loop(self):
		"""
		Updates the simulation
			- Draws the world and update it
			- Handles all user events
			- Controls frame rate
		"""
		self.world.render()
		self.world.advance()

		self.handle_events()

		self.clock.tick(self.framerate)

	def handle_events(self):
		"""
		Handles all keyboard, mouse and events like QUIT, etc
		"""
		self.handle_keyboard_events()
		self.handle_mouse_events()
		self.handle_general_events() 

	def handle_keyboard_events(self):
		"""
		set quit as true if user presses ESCAPE key
		"""
		keys = key.get_pressed()
		if keys[K_q] or keys[K_ESCAPE]:
			self.quit = True

	def handle_general_events(self):
		"""
		set quit true if user clicks the close button
		"""
		for evt in event.get():
			if evt.type == QUIT:
				self.quit = True

	def handle_mouse_events(self):
		"""
		- Creates obstacles on left clicks
		- Removes obstacles on right clicks
		"""
		pressed = mouse.get_pressed()
		x, y = mouse.get_pos()
		size = self.settings["cell_size"]
		cell = self.world[(x/size, y/size)]
		if pressed[0]:
			cell.make_obstacle()
		elif pressed[2]:
			if cell.is_obstacle():
				cell.remove_obstacle()
开发者ID:nandakishoremmn,项目名称:ants,代码行数:101,代码来源:controller.py


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