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"
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"
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 test_simple_robot(self): """ Tests whether a simple robot is correctly encoded. :return: """ protobuf_robot = yaml_to_robot(body_spec, brain_spec, basic_yaml_object) yaml_robot = rty(protobuf_robot) robot = yaml.load(yaml_robot) self.assertEquals(0, robot["id"], "Robot ID not correctly set.") self.assertEquals("Core", robot["body"]["id"], "Root ID not correctly set. (%s)" % robot["body"]["id"]) self.assertEquals(2, len(robot["body"]["children"]), "Root should have two children.")
def test_simple_robot(self): """ Tests whether a simple robot is correctly encoded. :return: """ protobuf_robot = yaml_to_robot(body_spec, brain_spec, basic_yaml_object) yaml_robot = rty(protobuf_robot) robot = yaml.load(yaml_robot) self.assertEquals(0, robot["id"], "Robot ID not correctly set.") self.assertEquals( "Core", robot["body"]["id"], "Root ID not correctly set. (%s)" % robot["body"]["id"]) self.assertEquals(2, len(robot["body"]["children"]), "Root should have two children.")
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
def ytr(yaml): return yaml_to_robot(body_spec, brain_spec, yaml)
id: Wheel type: Wheel params: red: 0.0 green: 1.0 blue: 0.0 5: id: Wheel2 type: Wheel params: red: 0.0 green: 1.0 blue: 0.0 brain: params: Wheel-out-0: type: Oscillator period: 3 Wheel2-out-0: type: Oscillator period: 3 ''' bot = yaml_to_robot(body_spec, brain_spec, bot_yaml) builder = RobotBuilder(BodyBuilder(body_spec), NeuralNetBuilder(brain_spec)) model = builder.get_sdf_model(bot, "libtolrobotcontrol.so") model.translate(Vector3(0, 0, 0.5)) sdf = SDF() sdf.add_element(model) print(str(sdf))
def ytr(yaml): return yaml_to_robot(body_spec, brain_spec, yaml)
default=15 ), ], ), }, ) brain_spec = NeuralNetImplementation({ "Simple" : NeuronSpec(params=["bias"]), "Oscillator": NeuronSpec( params=["period", "phaseOffset", "amplitude"] ) }) # Enter the test cases below (make alterations to the basic yaml object) # Body test cases missing_body = yaml_to_robot(body_spec, brain_spec, basic_yaml_object) missing_body.body.root.Clear() missing_id = yaml_to_robot(body_spec, brain_spec, basic_yaml_object) missing_id.body.root.id = "" missing_part_type = yaml_to_robot(body_spec, brain_spec, basic_yaml_object) missing_part_type.body.root.type = "" part_not_in_spec = yaml_to_robot(body_spec, brain_spec, basic_yaml_object) part_not_in_spec.body.root.type = "NonExistent" arity_fail = yaml_to_robot(body_spec, brain_spec, basic_yaml_object) arity_fail.body.root.child[0].src = 5 slot_reuse = yaml_to_robot(body_spec, brain_spec, basic_yaml_object)
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()
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))
outputs=2, params=[ParamSpec("param_a", default=-1), ParamSpec("param_b", default=15)] ) }) brain_spec = NeuralNetImplementation({ "Simple": NeuronSpec(params=["bias"]), "Oscillator": NeuronSpec( params=["period", "phaseOffset", "amplitude"] ) }) # Enter the test cases below (make alterations to the basic yaml object) # Body test cases missing_body = yaml_to_robot(body_spec, brain_spec, basic_yaml_object) missing_body.body.root.Clear() missing_id = yaml_to_robot(body_spec, brain_spec, basic_yaml_object) missing_id.body.root.id = "" missing_part_type = yaml_to_robot(body_spec, brain_spec, basic_yaml_object) missing_part_type.body.root.type = "" part_not_in_spec = yaml_to_robot(body_spec, brain_spec, basic_yaml_object) part_not_in_spec.body.root.type = "NonExistent" arity_fail = yaml_to_robot(body_spec, brain_spec, basic_yaml_object) arity_fail.body.root.child[0].src = 5 slot_reuse = yaml_to_robot(body_spec, brain_spec, basic_yaml_object)