本文整理汇总了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)
示例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
示例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()