Example #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)
def main():
    conf = parser.parse_args()

    brain_spec = get_brain_spec(conf)
    print "READING FILES!!!!!!!!!!!!!!!!!!!"
    with open(conf.genotype_file_1, 'r') as gen_file1:
        gen_yaml1 = gen_file1.read()

    with open(conf.genotype_file_2, 'r') as gen_file2:
        gen_yaml2 = gen_file2.read()

    print "CONVERTING!!!!!!!!!"

    genotype1 = yaml_to_genotype(gen_yaml1,
                                 brain_spec,
                                 keep_historical_marks=True)
    genotype2 = yaml_to_genotype(gen_yaml2,
                                 brain_spec,
                                 keep_historical_marks=True)

    print "genotype 1:"
    print genotype1.debug_string()

    print "genotype 2:"
    print genotype2.debug_string()

    print "CALCULATING SIMILARITY!!!!!!!!!!!!!!!!!!"

    dissimilarity = GeneticEncoding.get_dissimilarity(genotype1, genotype2)
    print "DISSIMILARITY = {0}".format(dissimilarity)
Example #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)
def run():
    """
    :return:
    """
    conf = parser.parse_args()
    world = yield From(OfflineEvoManager.create(conf))
    yield From(world.run())
Example #5
0
def main():
    print "START"

    conf = parser.parse_args()
    conf.pose_update_frequency = 5

    def handler(loop, context):
        exc = context['exception']
        if isinstance(exc, DisconnectError) or isinstance(
                exc, ConnectionResetError):
            print("Got disconnect / connection reset - shutting down.")
            sys.exit(1)

        raise context['exception']

    try:
        loop = trollius.get_event_loop()
        loop.set_debug(enabled=True)
        #        logging.basicConfig(level=logging.DEBUG)
        loop.set_exception_handler(handler)

        loop.run_until_complete(run(conf))
        print "FINISH"

    except KeyboardInterrupt:
        print("Got Ctrl+C, shutting down.")
    except ConnectionRefusedError:
        print("Connection refused, are the world and the analyzer loaded?")
Example #6
0
def main():
    conf = parser.parse_args()

    in_path = conf.file_name
    out_path = os.path.join(os.path.dirname(in_path), conf.output)

    with open(in_path, 'r') as yamlfile:
        yaml_bot = yamlfile.read()

    body_spec = get_body_spec(conf)
    brain_spec = get_extended_brain_spec(conf)

    print "converting to protobuf..."
    pb_bot = yaml_to_robot(body_spec, brain_spec, yaml_bot)

    cpg_factory = CPG_Factory(body_spec=body_spec, brain_spec=brain_spec)

    loopback = conf.loopback
    print "Loopback: {0}".format(loopback)

    coupling = conf.coupling
    print "Coupling: {0}".format(coupling)

    cpg_factory.add_CPGs(pb_bot, loopback, coupling)

    print "converting to yaml..."
    yaml_bot = robot_to_yaml(body_spec, brain_spec, pb_bot)

    with open(out_path, 'w') as out_file:
        out_file.write(yaml_bot)

    print "done"
Example #7
0
def main():
    conf = parser.parse_args()

    in_path = conf.file_name
    out_path = os.path.join(os.path.dirname(in_path), conf.output)

    with open(in_path, 'r') as yamlfile:
        yaml_bot = yamlfile.read()

    body_spec = get_body_spec(conf)
    brain_spec = get_brain_spec(conf)

    print "converting to protobuf..."
    pb_bot = yaml_to_robot(body_spec, brain_spec, yaml_bot)

    cpg_factory = CPG_Factory(body_spec=body_spec, brain_spec=brain_spec)
    cpg_factory.add_CPGs(pb_bot, conf.type)

    print "converting to yaml..."
    yaml_bot = robot_to_yaml(body_spec, brain_spec, pb_bot)

    with open(out_path, 'w') as out_file:
        out_file.write(yaml_bot)

    print "done"
Example #8
0
def run():
    """
    :return:
    """
    conf = parser.parse_args()
    world = yield From(OfflineEvoManager.create(conf))
    yield From(world.run())
Example #9
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))
Example #11
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")
Example #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")
Example #13
0
def main():
    args = parser.parse_args()

    def handler(loop, context):
        exc = context['exception']
        if isinstance(exc, DisconnectError) or isinstance(
                exc, ConnectionResetError):
            print("Got disconnect / connection reset - shutting down.")
            sys.exit(0)

        raise context['exception']

    try:
        loop = trollius.get_event_loop()
        loop.set_debug(enabled=True)
        logging.basicConfig(level=logging.DEBUG)
        loop.set_exception_handler(handler)
        loop.run_until_complete(run_server(args))

    except KeyboardInterrupt:
        print("Got Ctrl+C, shutting down.")
Example #14
0
def main():
    args = parser.parse_args()
    seed = random.randint(1, 1000000) if args.seed < 0 else args.seed
    random.seed(seed)
    print("Seed: %d" % seed)

    def handler(loop, context):
        exc = context['exception']
        if isinstance(exc, DisconnectError) or isinstance(exc, ConnectionResetError):
            print("Got disconnect / connection reset - shutting down.")
            sys.exit(0)

        raise context['exception']

    try:
        loop = trollius.get_event_loop()
        loop.set_exception_handler(handler)
        loop.run_until_complete(run_server(args))
    except KeyboardInterrupt:
        print("Got Ctrl+C, shutting down.")
    except ConnectionRefusedError:
        print("Connection refused, are the world and the analyzer loaded?")
Example #15
0
def main():

    args = parser.parse_args()
    #   brain_spec = get_brain_spec(args)
    #   body_spec = get_body_spec(args)
    brain_parser = NeuralNetworkParser(brain_spec)

    pb_robot = yaml_to_robot(body_spec, brain_spec, yaml_robot)
    print "yaml converted to pb"

    mutator = Mutator()

    genotype = brain_parser.robot_to_genotype(pb_robot, mutator)

    print "pb converted to genotype"
    print ""
    print "neurons:"
    neurons, connections = genotype.to_lists()
    for neuron in neurons:
        print neuron
    print ""
    print "connections:"
    for connection in connections:
        print connection
Example #16
0
def main():
    try:
        loop = trollius.get_event_loop()
        loop.run_until_complete(run(parser.parse_args()))
    except KeyboardInterrupt:
        print("Got Ctrl+C, shutting down.")
Example #17
0
import uuid
from revolve.spec.msgs import Robot
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__))+'/../')

from tol.spec import get_body_spec
from tol.config import parser, make_revolve_config
from sdfbuilder.math import Vector3

conf = make_revolve_config(parser.parse_args())
body_spec = get_body_spec(conf)

print("exp,run,robot_id,gene_id,origin_id,type,r,g,b")


def find_part_uuid(type, vec, parent_parts):
    for pid, ptype, pvec, porigin in parent_parts:
        if ptype == type and abs(vec - pvec) < 1e-8:
            return pid, porigin

    return None, None


def process_parts(robot_id, part, parent_parts, cur=None):
    # Last three parameters are always color
    type, vec = part.type, Vector3(*[p.value for p in part.param[-3:]])
    pid, porigin = find_part_uuid(type, vec, parent_parts)

    if pid is None:
        pid, porigin = uuid.uuid4(), robot_id
Example #18
0
import uuid
from revolve.spec.msgs import Robot
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../')

from tol.spec import get_body_spec
from tol.config import parser, make_revolve_config
from sdfbuilder.math import Vector3

conf = make_revolve_config(parser.parse_args())
body_spec = get_body_spec(conf)

print("exp,run,robot_id,gene_id,origin_id,type,r,g,b")


def find_part_uuid(type, vec, parent_parts):
    for pid, ptype, pvec, porigin in parent_parts:
        if ptype == type and abs(vec - pvec) < 1e-8:
            return pid, porigin

    return None, None


def process_parts(robot_id, part, parent_parts, cur=None):
    # Last three parameters are always color
    type, vec = part.type, Vector3(*[p.value for p in part.param[-3:]])
    pid, porigin = find_part_uuid(type, vec, parent_parts)

    if pid is None:
        pid, porigin = uuid.uuid4(), robot_id
Example #19
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))
Example #20
0
import sys
import os
import glob

sys.path.append(os.path.dirname(os.path.abspath(__file__))+'/../')

from revolve.angle.representation import Tree
from revolve.spec import Robot
from tol.spec import get_body_spec
from tol.config import parser, make_revolve_config
from tol.util.analyze import list_extremities, count_joints, count_motors


conf = parser.parse_args([])
make_revolve_config(conf)
body_spec = get_body_spec(conf)

input_dirs = sys.argv[1:]

for input_dir in input_dirs:
    input_dir = os.path.abspath(input_dir)
    print("Processing %s..." % input_dir)
    files = glob.glob(os.path.join(input_dir, "*.pb"))

    with open(os.path.join(input_dir, "robot_details.csv"), 'w') as o:
        o.write("robot_id,size,extremity_id,extremity_size,joint_count,motor_count\n")
        for filename in files:
            robot = Robot()
            with open(filename, "rb") as f:
                robot.ParseFromString(f.read())
Example #21
0
import sys
import os
import glob

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../')

from revolve.angle.representation import Tree
from revolve.spec import Robot
from tol.spec import get_body_spec
from tol.config import parser, make_revolve_config
from tol.util.analyze import list_extremities, count_joints, count_motors

conf = parser.parse_args([])
make_revolve_config(conf)
body_spec = get_body_spec(conf)

input_dirs = sys.argv[1:]

for input_dir in input_dirs:
    input_dir = os.path.abspath(input_dir)
    print("Processing %s..." % input_dir)
    files = glob.glob(os.path.join(input_dir, "*.pb"))

    with open(os.path.join(input_dir, "robot_details.csv"), 'w') as o:
        o.write(
            "robot_id,size,extremity_id,extremity_size,joint_count,motor_count\n"
        )
        for filename in files:
            robot = Robot()
            with open(filename, "rb") as f:
                robot.ParseFromString(f.read())
Example #22
0
async def run():
    conf = parser.parse_args()
    conf.evaluation_time_sigma = 2.0
    conf.weight_mutation_probability = 0.8
    conf.weight_mutation_sigma = 5.0
    conf.param_mutation_probability = 0.8
    conf.param_mutation_sigma = 0.25

    # this is the world state update frequency in simulation Hz
    conf.pose_update_frequency = 5  # in simulation Hz

    # update frequency of sensors in simulation Hz (default 10Hz)
    # conf.sensor_update_rate = 10.

    # # these are irrelevant parameters but we need to set them anyway,
    # # otherwise it won't work
    # conf.min_parts = 1
    # conf.max_parts = 3
    # conf.arena_size = (3, 3)
    # conf.max_lifetime = 99999
    # conf.initial_age_mu = 99999
    # conf.initial_age_sigma = 1
    # conf.age_cutoff = 99999

    # create the learning manager
    world = await LearningManager.create(conf)
    await world.pause(True)

    path_to_log_dir = os.path.join(world.path_to_log_dir, "learner1")

    body_spec = get_body_spec(conf)
    brain_spec = get_extended_brain_spec(conf)

    # mutation spec contains info about what parameters of neurons can be mutated
    mut_spec = get_extended_mutation_spec(conf.param_mutation_sigma,
                                          conf.weight_mutation_sigma)

    # what types of neurons can be added to the network
    # allowed_types = ["Simple", "Sigmoid", "DifferentialCPG"]
    # allowed_types = ["Simple", "Sigmoid"]
    allowed_types = ["Simple"]

    mutator = Mutator(mut_spec, allowed_neuron_types=allowed_types)

    # if we are not restoring a saved state:
    if not world.do_restore:

        with open(conf.test_bot, 'r') as yamlfile:
            bot_yaml = yamlfile.read()

        pose = Pose(position=Vector3(0, 0, 0.2))
        bot = yaml_to_robot(body_spec, brain_spec, bot_yaml)
        tree = Tree.from_body_brain(bot.body, bot.brain, body_spec)

        robot = await world.insert_robot(tree, pose)

        learner = RobotLearner(world=world,
                               robot=robot,
                               brain_spec=brain_spec,
                               mutator=mutator,
                               conf=conf)

        gen_files = []
        init_brain_list = None

        if (os.path.isdir(path_to_log_dir)):
            gen_files = list(fname for fname in os.listdir(path_to_log_dir) if \
                fnmatch.fnmatch(fname, "gen_*_genotypes.log"))

        # if we are reading an initial population from a file:
        if len(gen_files) > 0:

            gen_files = sorted(gen_files,
                               key=lambda item: int(item.split('_')[1]))
            last_gen_file = gen_files[-1]

            num_generations = int(last_gen_file.split('_')[1])  # + 1
            num_brains_evaluated = conf.population_size * num_generations

            print("last generation file = {0}".format(last_gen_file))

            # get list of brains from the last generation log file:
            init_brain_list, min_mark, max_mark = \
                get_brains_from_file(os.path.join(path_to_log_dir, last_gen_file), brain_spec)

            print("Max historical mark = {0}".format(max_mark))

            # set mutator's innovation number according to the max historical mark:
            mutator.innovation_number = max_mark + 1

            learner.total_brains_evaluated = num_brains_evaluated
            learner.generation_number = num_generations

        # initialize learner with initial list of brains:
        await world.add_learner(learner, "learner1", init_brain_list)
        print(learner.parameter_string())

        # log experiment parameter values:
        create_param_log_file(conf, learner.generation_number,
                              os.path.join(path_to_log_dir, "parameters.log"))
        # copy the robot body file:
        shutil.copy(conf.test_bot, path_to_log_dir)

    # if we are restoring a saved state:
    else:
        print("WORLD RESTORED FROM {0}".format(world.world_snapshot_filename))
        print("STATE RESTORED FROM {0}".format(world.snapshot_filename))

    print("WORLD CREATED")
    await world.run()
Example #23
0
def main():
    try:
        loop = trollius.get_event_loop()
        loop.run_until_complete(run(parser.parse_args()))
    except KeyboardInterrupt:
        print("Got Ctrl+C, shutting down.")