def do_mate(world, ra, rb): """ :param world: :param ra: :param rb: :return: """ mate = None num_attempts = 0 while num_attempts < 10: # Attempt reproduction mate = yield From(world.attempt_mate(ra, rb)) if mate: break num_attempts += 1 # logger.debug("Mates selected, highlighting points...") new_pos = pick_position() new_pos.z = insert_z # hls = yield From(create_highlights( # world, ra.last_position, rb.last_position, new_pos)) # yield From(trollius.sleep(2)) if mate: logger.debug("Inserting child...") child, bbox = mate pose = Pose(position=new_pos) spawn_robot(world, tree=child, pose=pose, parents=[ra, rb]) else: logger.debug("Could not mate")
def cleanup(world, max_bots=10, remove_from=5): """ Removes the slowest of the oldest `remove_from` robots from the world if there are more than `max_bots` :param world: :type world: World :return: """ if len(world.robots) <= max_bots: return logger.debug("Automatically removing the slowest of the oldest %d robots..." % remove_from) # Get the oldest `num` robots robots = sorted(world.robot_list(), key=lambda r: r.age(), reverse=True)[:remove_from] # Get the slowest robot slowest = sorted(robots, key=lambda r: r.velocity())[0] fut, hl = yield From(world.add_highlight(slowest.last_position, (50, 50, 50, 50))) yield From(fut) yield From(trollius.sleep(1)) # Delete it from the world fut = yield From(world.delete_robot(slowest)) yield From(trollius.sleep(1)) yield From(remove_highlights(world, [hl])) yield From(fut)
def build_arena(self): """ Initializes the arena by building square arena wall blocks. :return: Future that resolves when arena building is done. """ logger.debug("Building the arena...") n = self.conf.num_wall_segments r = self.conf.world_diameter * 0.5 frac = 2 * math.pi / n points = [Vector3(r * math.cos(i * frac), r * math.sin(i * frac), 0) for i in range(n)] fut = yield From(self.build_walls(points)) raise Return(fut)
def build_arena(self): """ Initializes the arena by building square arena wall blocks. :return: Future that resolves when arena building is done. """ logger.debug("Building the arena...") n = self.conf.num_wall_segments futs = [] r = self.conf.world_diameter * 0.5 frac = 2 * math.pi / n points = [Vector3(r * math.cos(i * frac), r * math.sin(i * frac), 0) for i in range(n)] fut = yield From(self.build_walls(points)) futs.append(fut) raise Return(multi_future(futs))
def automatic_mode(args, world, state): """ :param args: :param world: :return: """ logger.debug("Simulating (make sure the world is running)...") yield From(sleep_sim_time(world, 5 if args.fast else 15, state)) if not state[0]: logger.debug("Selecting robots to reproduce...") robots = world.robot_list() ra, rb = random.sample(robots, 2) yield From(do_mate(world, ra, rb))
def do_mate(world, ra, rb): """ :param world: :param ra: :param rb: :return: """ mate = None while True: # Attempt reproduction mate = yield From(world.attempt_mate(ra, rb)) if mate: break logger.debug("Mates selected, highlighting points...") new_pos = get_insert_position(world.conf, ra, rb, world) new_pos.z = insert_z hls = yield From(create_highlights( world, ra.last_position, rb.last_position, new_pos)) yield From(trollius.sleep(2)) logger.debug("Inserting child...") child, bbox = mate pose = Pose(position=new_pos) future = yield From(world.insert_robot(child, pose, parents=[ra, rb])) yield From(future) yield From(sleep_sim_time(world, 1.8)) logger.debug("Removing highlights...") yield From(remove_highlights(world, hls))
# ToL from tol.config import Config, constants from tol.manage import World from tol.logging import logger, output_console # Output logs to console output_console() # Set command line seed if supplied, otherwise choose a random number if len(sys.argv) > 1: seed = int(sys.argv[1]) else: seed = random.randint(0, 1000000) random.seed(seed) logger.debug("Seed: %d" % seed) def get_circle_poses(n, radius): """ :param n: :param radius: :return: """ shift = 2.0 * math.pi / n poses = [] for i in xrange(n): angle = i * shift x, y = radius * math.cos(angle), radius * math.sin(angle) starting_rotation = Quaternion.from_angle_axis(math.pi + angle, Vector3(0, 0, 1))