コード例 #1
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"
コード例 #2
0
ファイル: create_osc.py プロジェクト: egdman/tol-revolve
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"
コード例 #3
0
    def run(self, conf):
        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

        self.body_spec = get_body_spec(conf)
        self.brain_spec = get_brain_spec(conf)
        self.nn_parser = NeuralNetworkParser(self.brain_spec)

        print "OPENING FILES!!!!!!!!!!!!!!!!!!!"
        with open("body/{0}".format(self.body_file), 'r') as robot_file:
            robot_yaml = robot_file.read()

        for filename in self.brain_files:
            with open("brains/{0}".format(filename, 'r')) as brain_file:
                br_yaml = brain_file.read()
                self.brain_genotypes.append(
                    yaml_to_genotype(br_yaml, self.brain_spec))

        yield From(wait_for(self.pause(True)))

        pose = Pose(position=Vector3(0, 0, 0.5), rotation=rotate_vertical(0))

        robot_pb = yaml_to_robot(self.body_spec, self.brain_spec, robot_yaml)
        tree = Tree.from_body_brain(robot_pb.body, robot_pb.brain,
                                    self.body_spec)

        print "INSERTING ROBOT!!!!!!!!!!!!!!!!!!!!!!"
        robot = yield From(wait_for(self.insert_robot(tree, pose)))
        self.robot_name = robot.name

        self.modify_nn_publisher = yield From(
            self.manager.advertise(
                '/gazebo/default/{0}/modify_neural_network'.format(
                    self.robot_name),
                'gazebo.msgs.ModifyNeuralNetwork',
            ))
        # Wait for connections
        yield From(self.modify_nn_publisher.wait_for_listener())

        brain_num = 0
        num_of_brains = len(self.brain_genotypes)
        print "Number of brains = {0}".format(num_of_brains)

        yield From(wait_for(self.pause(False)))
        while (True):
            if self.timers.is_it_time('evaluate', self.time_period,
                                      self.get_world_time()):
                n = brain_num % num_of_brains
                print "Switching brain to #{0}!!!!!!!!!".format(n)
                yield From(self.insert_brain(self.brain_genotypes[n]))
                self.timers.reset('evaluate', self.get_world_time())
                brain_num += 1

            yield From(trollius.sleep(0.1))
コード例 #4
0
ファイル: track_genes.py プロジェクト: ElteHupkes/tol-revolve
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
コード例 #5
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())
コード例 #6
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()