示例#1
0
def run_server():
    conf = parser.parse_args()
    conf.enable_light_sensor = False
    conf.output_directory = None
    conf.max_lifetime = 999999
    conf.initial_age_mu = 500
    conf.initial_age_sigma = 500

    world = yield From(World.create(conf))
    yield From(world.pause(True))
    trees, bboxes = yield From(world.generate_population(40))
    insert_queue = zip(trees, bboxes)

    yield From(world.pause(False))
    for tree, bbox in insert_queue[:40]:
        fut = yield From(birth(world, tree, bbox, None))
        yield From(fut)
        yield From(sleep_sim_time(world, 1.0))

    print("Inserted all robots")
    sim_time_sec = 1.0

    while True:
        before = time.time()
        yield From(sleep_sim_time(world, sim_time_sec))
        after = time.time()

        diff = after - before
        print(sim_time_sec / diff)
示例#2
0
def run_server():
    conf = Config(
        proposal_threshold=0,
        output_directory='output'
    )

    world = yield From(World.create(conf))
    yield From(world.pause(True))

    wall_x = conf.arena_size[0] / 2.0
    wall_y = conf.arena_size[1] / 2.0
    wall_points = [Vector3(-wall_x, -wall_y, 0), Vector3(wall_x, -wall_y, 0),
                   Vector3(wall_x, wall_y, 0), Vector3(-wall_x, wall_y, 0)]

    future = yield From(world.build_walls(wall_points))
    yield From(future)

    grid_size = (1, 2)
    spacing = 3 * conf.mating_distance
    grid_x, grid_y = grid_size
    x_offset = -(grid_x - 1) * spacing * 0.5
    y_offset = -(grid_y - 1) * spacing * 0.5
    poses = [Pose(position=Vector3(x_offset + spacing * i, y_offset + spacing * j, 0))
             for i, j in itertools.product(range(grid_x), range(grid_y))]

    trees, bboxes = yield From(world.generate_population(len(poses)))
    for pose, bbox in itertools.izip(poses, bboxes):
        pose.position += Vector3(0, 0, bbox[2])

    fut = yield From(world.insert_population(trees, poses))
    yield From(fut)

    while True:
        yield From(trollius.sleep(0.1))
        yield From(world.perform_reproduction())
示例#3
0
def run_server():
    conf = parser.parse_args()
    conf.enable_light_sensor = False
    conf.output_directory = None
    conf.max_lifetime = 999999
    conf.initial_age_mu = 500
    conf.initial_age_sigma = 500

    world = yield From(World.create(conf))
    yield From(world.pause(True))
    trees, bboxes = yield From(world.generate_population(40))
    insert_queue = zip(trees, bboxes)

    yield From(world.pause(False))
    for tree, bbox in insert_queue[:40]:
        fut = yield From(birth(world, tree, bbox, None))
        yield From(fut)
        yield From(sleep_sim_time(world, 1.0))

    print("Inserted all robots")
    sim_time_sec = 1.0

    while True:
        before = time.time()
        yield From(sleep_sim_time(world, sim_time_sec))
        after = time.time()

        diff = after - before
        print(sim_time_sec / diff)
示例#4
0
def run(conf):
    """

    :param conf:
    :return:
    """
    conf.evaluation_time = 5.0
    conf.output_directory = 'output'
    world = yield From(World.create(conf))
    yield From(world.pause(True))

    for i in range(3):
        ta, _, _ = yield From(world.generate_valid_robot())
        tb, _, _ = yield From(world.generate_valid_robot())
        ra = yield From(wait_for(world.insert_robot(ta, pose=Pose(position=Vector3(0, 3*i, 0.5)))))
        rb = yield From(wait_for(world.insert_robot(tb, pose=Pose(position=Vector3(0, 3*i + 1, 0.5)))))

        while True:
            # Attempt reproduction
            mate = yield From(world.attempt_mate(ra, rb))

            if mate:
                break

        tree, bbox = mate
        yield From(wait_for(world.insert_robot(tree, pose=Pose(position=Vector3(0, 3*i + 2, 0.5)))))
示例#5
0
def run_server(conf):
    """

    :param args:
    :return:
    """
    conf.proposal_threshold = 0
    conf.output_directory = None
    conf.min_parts = 6
    conf.max_parts = 15
    conf.arena_size = (3, 3)

    interactive = [True]
    world = yield From(World.create(conf))
    yield From(world.pause(True))

    start_bots = conf.num_initial_bots
    poses = [Pose(position=pick_position(conf)) for _ in range(start_bots)]
    trees, bboxes = yield From(world.generate_population(len(poses)))
    fut = yield From(world.insert_population(trees, poses))
    yield From(fut)

    # List of reproduction requests
    reproduce = []

    # Request callback for the subscriber
    def callback(data):
        req = Request()
        req.ParseFromString(data)
        imode_value = None

        if req.request == "produce_offspring":
            reproduce.append(req.data.split("+++"))
            imode_value = True
        elif req.request == "enable_interaction":
            imode_value = True
        elif req.request == "disable_interaction":
            imode_value = False

        if imode_value is not None:
            interactive[0] = imode_value
            print("Interactive mode is now %s" % ("ON" if interactive[0] else "OFF"))

            if not interactive[0]:
                reproduce[:] = []

    subscriber = world.manager.subscribe(
        '/gazebo/default/request', 'gazebo.msgs.Request', callback)
    yield From(subscriber.wait_for_connection())

    while True:
        if interactive[0]:
            yield From(interactive_mode(world, reproduce))
        else:
            yield From(automatic_mode(conf, world, interactive))

        yield From(trollius.sleep(0.1))
        yield From(cleanup(world))
示例#6
0
def run_server():
    conf = parser.parse_args()
    conf.analyzer_address = None

    world = yield From(World.create(conf))
    yield From(world.pause(True))

    with open("/home/elte/mt/tol/scripts/starfish.yaml", "rb") as f:
        robot_yaml = f.read()

    body_spec = world.builder.body_builder.spec
    brain_spec = world.builder.brain_builder.spec
    bot = yaml_to_robot(body_spec, brain_spec, robot_yaml)

    fname = conf.output_directory+"/revolve_benchmark.csv"
    exists = os.path.exists(fname)
    if exists:
        f = open(fname, 'ab', buffering=1)
    else:
        f = open(fname, 'wb', buffering=1)

    output = csv.writer(f, delimiter=',')

    if not exists:
        output.writerow(['run', 'population_size', 'step_size',
                         'sim_time', 'real_time', 'factor'])

    n_bots = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
    sim_time = 5.0
    runs = 20

    yield From(world.pause(False))

    for n in n_bots:
        poses = get_poses(n)
        trees = [Tree.from_body_brain(bot.body, bot.brain, body_spec) for _ in range(n)]

        for i in range(runs):
            yield From(wait_for(world.insert_population(trees, poses)))

            while world.last_time is None:
                yield From(trollius.sleep(0.1))

            sim_before = world.last_time
            before = time.time()

            while float(world.last_time - sim_before) < sim_time:
                yield From(trollius.sleep(0.1))

            sim_diff = float(world.last_time - sim_before)
            diff = time.time() - before

            output.writerow((i, n, conf.world_step_size, sim_diff,
                             diff, sim_diff / diff))

            yield From(wait_for(world.delete_all_robots()))
            yield From(trollius.sleep(0.3))
def run():
    """
    The main coroutine, which is started below.
    """
    # Parse command line / file input arguments
    conf = parser.parse_args()

    # Adding brain configuration
    with open(conf.brain_conf_path, 'r') as f:
        s = f.read()
        brain_conf = ast.literal_eval(s)
    if conf.load_controller != "None":
        brain_conf["policy_load_path"] = conf.load_controller
    conf.brain_conf = brain_conf

    # This disables the analyzer; enable it if you want to generate valid robots
    # Can also do this using arguments of course, just pass an empty string
    # conf.analyzer_address = None

    with open("{}.yaml".format(conf.robot_name), 'r') as yamlfile:
        bot_yaml = yamlfile.read()

    # Create the world, this connects to the Gazebo world
    world = yield From(World.create(conf))

    # These are useful when working with YAML
    body_spec = world.builder.body_builder.spec
    brain_spec = world.builder.brain_builder.spec

    # Create a robot from YAML
    robot = yaml_to_robot(body_spec, brain_spec, bot_yaml)

    # Create a revolve.angle `Tree` representation from the robot, which
    # is what is used in the world manager.
    robot_tree = Tree.from_body_brain(robot.body, robot.brain, body_spec)

    # Insert the robot into the world. `insert_robot` resolves when the insert
    # request is sent, the future it returns resolves when the robot insert
    # is actually confirmed and a robot manager object has been created
    pose = Pose(position=Vector3(0, 0, 0.05))
    future = yield From(world.insert_robot(robot_tree, pose, "{}-{}".format(conf.robot_name, conf.experiment_round)))
    robot_manager = yield From(future)

    # I usually start the world paused, un-pause it here. Note that
    # pause again returns a future for when the request is sent,
    # that future in turn resolves when a response has been received.
    # This is the general convention for all message actions in the
    # world manager. `wait_for` saves the hassle of grabbing the
    # intermediary future in this case.
    yield From(wait_for(world.pause(True)))

    # Start a run loop to do some stuff
    while True:
        # Print robot fitness every second
        print("Robot fitness: %f" % robot_manager.fitness())
        yield From(trollius.sleep(1.0))
示例#8
0
def run_server():
    conf = Config(
        update_rate=25,
        proposal_threshold=0
    )

    world = yield From(World.create(conf))
    yield From(world.pause(True))
    yield From(yield_wait(world.build_arena()))

    n_bots = [1, 5, 20, 50]
    radius = 0.4 * conf.arena_size[0]
    n_repeats = 1
    sim_time = 5.0

    _state = [None, -1]

    # World update trigger
    def trigger(_):
        elapsed = float(world.last_time)
        if elapsed >= sim_time:
            _state[0] = time.time() - _state[0]
            _state[1] = elapsed

            # Remove trigger to prevent other incoming messages
            # from overwriting these values.
            world.remove_update_trigger(trigger)
        elif elapsed < 0:
            logger.error("Elapsed time < 0!")

    print("nbots\titer\tsimtime\twctime")
    for n in n_bots:
        poses = get_circle_poses(n, radius)

        for i in range(n_repeats):
            # Generate a starting population from the given poses
            yield From(yield_wait(world.generate_starting_population(poses)))
            yield From(trollius.sleep(0.5))
            _state[0] = time.time()
            _state[1] = -1
            world.add_update_trigger(trigger)
            yield From(world.pause(False))

            while _state[1] < 0:
                # Wait until the simulation time has passed the required
                # number of seconds.
                yield From(trollius.sleep(0.1))

            print("%d\t%d\t%f\t%f" % (n, i, _state[1], _state[0]))
            yield From(world.pause())
            yield From(yield_wait(world.delete_all_robots()))

            # Sleep to process all old messages
            yield From(trollius.sleep(0.5))
            yield From(world.reset())
示例#9
0
def run_server():
    conf = Config(update_rate=25, proposal_threshold=0)

    world = yield From(World.create(conf))
    yield From(world.pause(True))
    yield From(yield_wait(world.build_arena()))

    n_bots = [1, 5, 20, 50]
    radius = 0.4 * conf.arena_size[0]
    n_repeats = 1
    sim_time = 5.0

    _state = [None, -1]

    # World update trigger
    def trigger(_):
        elapsed = float(world.last_time)
        if elapsed >= sim_time:
            _state[0] = time.time() - _state[0]
            _state[1] = elapsed

            # Remove trigger to prevent other incoming messages
            # from overwriting these values.
            world.remove_update_trigger(trigger)
        elif elapsed < 0:
            logger.error("Elapsed time < 0!")

    print("nbots\titer\tsimtime\twctime")
    for n in n_bots:
        poses = get_circle_poses(n, radius)

        for i in range(n_repeats):
            # Generate a starting population from the given poses
            yield From(yield_wait(world.generate_starting_population(poses)))
            yield From(trollius.sleep(0.5))
            _state[0] = time.time()
            _state[1] = -1
            world.add_update_trigger(trigger)
            yield From(world.pause(False))

            while _state[1] < 0:
                # Wait until the simulation time has passed the required
                # number of seconds.
                yield From(trollius.sleep(0.1))

            print("%d\t%d\t%f\t%f" % (n, i, _state[1], _state[0]))
            yield From(world.pause())
            yield From(yield_wait(world.delete_all_robots()))

            # Sleep to process all old messages
            yield From(trollius.sleep(0.5))
            yield From(world.reset())
示例#10
0
def run_server(conf):
    """

    :param args:
    :return:
    """
 #   conf.proposal_threshold = 0
 #   conf.output_directory = None
    conf.min_parts = 1
    conf.max_parts = 3
    conf.arena_size = (3, 3)

 #   interactive = [True]

    world = yield From(World.create(conf))
    yield From(world.pause(True))

    start_bots = conf.num_initial_bots
    poses = [Pose(position=pick_position(conf)) for _ in range(start_bots)]

    trees, bboxes = yield From(world.generate_population(len(poses)))

    fut = yield From(world.insert_population(trees, poses))
    yield From(fut)

    # List of reproduction requests
    reproduce = []

    # Request callback for the subscriber
    def callback(data):
        req = Request()
        req.ParseFromString(data)
       

        if req.request == "produce_offspring":
            reproduce.append(req.data.split("+++"))


    subscriber = world.manager.subscribe(
        '/gazebo/default/request', 'gazebo.msgs.Request', callback)
    yield From(subscriber.wait_for_connection())

    yield From(world.pause(False))

    while True:
        yield From(spawn_population(world, conf))

        yield From(trollius.sleep(12))
        yield From(cleanup(world))
示例#11
0
def run_server():
    world = yield From(World.create(conf))

    counter = 0
    while True:
        model.name = "test_bot_%d" % counter
        print("Inserting %s..." % model.name)
        fut = yield From(world.insert_model(sdf))
        yield From(fut)
        print("Done. Waiting...")
        yield From(trollius.sleep(1.0))
        print("Done. Removing...")
        fut = yield From(world.delete_model(model.name))
        yield From(fut)
        print("Done. Waiting...")
        yield From(trollius.sleep(1.0))
        counter += 1
示例#12
0
def run_server():
    conf = parser.parse_args()
    conf.enable_light_sensor = False
    conf.output_directory = None
    conf.max_lifetime = 999999
    conf.initial_age_mu = 500
    conf.initial_age_sigma = 500

    world = yield From(World.create(conf))
    yield From(world.pause(False))

    trees, bboxes = yield From(world.generate_population(30))
    insert_queue = zip(trees, bboxes)

    for tree, bbox in insert_queue[:15]:
        fut = yield From(birth(world, tree, bbox, None))
        yield From(fut)
        yield From(sleep_sim_time(world, 1.0))

    sim_time_sec = 5.0

    while True:
        bots = []
        for tree, bbox in insert_queue[15:]:
            fut = yield From(birth(world, tree, bbox, None))
            bot = yield From(fut)
            bots.append(bot)
            yield From(sleep_sim_time(world, 1.0))

        print("Inserted all robots")

        before = time.time()
        yield From(sleep_sim_time(world, sim_time_sec))
        after = time.time()

        diff = after - before
        print(sim_time_sec / diff)

        futs = []
        for robot in bots:
            fut = yield From(world.delete_robot(robot))
            futs.append(fut)

        yield From(multi_future(futs))
        yield From(trollius.sleep(0.1))
        print("Deleted all robots")
示例#13
0
def run_server():
    conf = parser.parse_args()
    conf.enable_light_sensor = False
    conf.output_directory = None
    conf.max_lifetime = 999999
    conf.initial_age_mu = 500
    conf.initial_age_sigma = 500

    world = yield From(World.create(conf))
    yield From(world.pause(False))

    trees, bboxes = yield From(world.generate_population(30))
    insert_queue = zip(trees, bboxes)

    for tree, bbox in insert_queue[:15]:
        fut = yield From(birth(world, tree, bbox, None))
        yield From(fut)
        yield From(sleep_sim_time(world, 1.0))

    sim_time_sec = 5.0

    while True:
        bots = []
        for tree, bbox in insert_queue[15:]:
            fut = yield From(birth(world, tree, bbox, None))
            bot = yield From(fut)
            bots.append(bot)
            yield From(sleep_sim_time(world, 1.0))

        print("Inserted all robots")

        before = time.time()
        yield From(sleep_sim_time(world, sim_time_sec))
        after = time.time()

        diff = after - before
        print(sim_time_sec / diff)

        futs = []
        for robot in bots:
            fut = yield From(world.delete_robot(robot))
            futs.append(fut)

        yield From(multi_future(futs))
        yield From(trollius.sleep(0.1))
        print("Deleted all robots")
示例#14
0
def run_server(conf):

    conf.min_parts = 1
    conf.max_parts = 3
    conf.arena_size = (3, 3)

    world = yield From(World.create(conf))

    yield From(spawn_initial_robots(world, init_pop_size))

    robots = [r for r in world.robot_list()]

    parent_a = random.choice(robots)
    parent_b = random.choice(robots)
    print "parent_a id =", parent_a.id
    print "parent_b id =", parent_b.id

    yield From(do_mate(world, parent_a, parent_b))
示例#15
0
def run_server():
    conf = Config(proposal_threshold=0, output_directory='output')

    world = yield From(World.create(conf))
    yield From(world.pause(True))

    wall_x = conf.arena_size[0] / 2.0
    wall_y = conf.arena_size[1] / 2.0
    wall_points = [
        Vector3(-wall_x, -wall_y, 0),
        Vector3(wall_x, -wall_y, 0),
        Vector3(wall_x, wall_y, 0),
        Vector3(-wall_x, wall_y, 0)
    ]

    future = yield From(world.build_walls(wall_points))
    yield From(future)

    grid_size = (1, 2)
    spacing = 3 * conf.mating_distance
    grid_x, grid_y = grid_size
    x_offset = -(grid_x - 1) * spacing * 0.5
    y_offset = -(grid_y - 1) * spacing * 0.5
    poses = [
        Pose(position=Vector3(x_offset + spacing * i, y_offset +
                              spacing * j, 0))
        for i, j in itertools.product(range(grid_x), range(grid_y))
    ]

    trees, bboxes = yield From(world.generate_population(len(poses)))
    for pose, bbox in itertools.izip(poses, bboxes):
        pose.position += Vector3(0, 0, bbox[2])

    fut = yield From(world.insert_population(trees, poses))
    yield From(fut)

    while True:
        yield From(trollius.sleep(0.1))
        yield From(world.perform_reproduction())
示例#16
0
def run(conf):
    """

    :param conf:
    :return:
    """
    conf.evaluation_time = 5.0
    conf.output_directory = 'output'
    world = yield From(World.create(conf))
    yield From(world.pause(True))

    for i in range(3):
        ta, _, _ = yield From(world.generate_valid_robot())
        tb, _, _ = yield From(world.generate_valid_robot())
        ra = yield From(
            wait_for(
                world.insert_robot(ta,
                                   pose=Pose(position=Vector3(0, 3 *
                                                              i, 0.5)))))
        rb = yield From(
            wait_for(
                world.insert_robot(
                    tb, pose=Pose(position=Vector3(0, 3 * i + 1, 0.5)))))

        while True:
            # Attempt reproduction
            mate = yield From(world.attempt_mate(ra, rb))

            if mate:
                break

        tree, bbox = mate
        yield From(
            wait_for(
                world.insert_robot(
                    tree, pose=Pose(position=Vector3(0, 3 * i + 2, 0.5)))))
示例#17
0
def run_server():
    conf = parser.parse_args()
    conf.analyzer_address = None

    world = yield From(World.create(conf))
    yield From(world.pause(True))

    with open("/home/elte/mt/tol/scripts/starfish.yaml", "rb") as f:
        robot_yaml = f.read()

    body_spec = world.builder.body_builder.spec
    brain_spec = world.builder.brain_builder.spec
    bot = yaml_to_robot(body_spec, brain_spec, robot_yaml)

    fname = conf.output_directory + "/revolve_benchmark.csv"
    exists = os.path.exists(fname)
    if exists:
        f = open(fname, 'ab', buffering=1)
    else:
        f = open(fname, 'wb', buffering=1)

    output = csv.writer(f, delimiter=',')

    if not exists:
        output.writerow([
            'run', 'population_size', 'step_size', 'sim_time', 'real_time',
            'factor'
        ])

    n_bots = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
    sim_time = 5.0
    runs = 20

    yield From(world.pause(False))

    for n in n_bots:
        poses = get_poses(n)
        trees = [
            Tree.from_body_brain(bot.body, bot.brain, body_spec)
            for _ in range(n)
        ]

        for i in range(runs):
            yield From(wait_for(world.insert_population(trees, poses)))

            while world.last_time is None:
                yield From(trollius.sleep(0.1))

            sim_before = world.last_time
            before = time.time()

            while float(world.last_time - sim_before) < sim_time:
                yield From(trollius.sleep(0.1))

            sim_diff = float(world.last_time - sim_before)
            diff = time.time() - before

            output.writerow(
                (i, n, conf.world_step_size, sim_diff, diff, sim_diff / diff))

            yield From(wait_for(world.delete_all_robots()))
            yield From(trollius.sleep(0.3))