def factory(cli_arg: str, main_config: types.YAMLDict, cmdopts: types.Cmdopts) -> PopulationVariableDensity: """ Factory to create :class:`PopulationVariableDensity` derived classes from the command line definition. """ attr = vd.Parser()(cli_arg) sgp = pm.module_load_tiered(project=cmdopts['project'], path='generators.scenario_generator_parser') kw = sgp.ScenarioGeneratorParser().to_dict(cmdopts['scenario']) extent = ArenaExtent(Vector3D(kw['arena_x'], kw['arena_y'], kw['arena_z'])) densities = [ x for x in np.linspace( attr['density_min'], attr['density_max'], num=attr['cardinality']) ] def __init__(self) -> None: PopulationVariableDensity.__init__(self, cli_arg, main_config, cmdopts['batch_input_root'], densities, extent) return type( cli_arg, # type: ignore (PopulationVariableDensity, ), {"__init__": __init__})
def __init__(self, dims: Vector3D, origin: Vector3D = Vector3D()) -> None: self._origin = origin self.dims = dims self.ll = origin self.ur = origin + dims self.center = origin + dims / 2.0
def factory(cli_arg: str, main_config: types.YAMLDict, cmdopts: types.Cmdopts) -> PopulationConstantDensity: """ Factory to create :class:`PopulationConstantDensity` derived classes from the command line definition. """ attr = cd.Parser()(cli_arg) sgp = pm.module_load_tiered(project=cmdopts['project'], path='generators.scenario_generator_parser') kw = sgp.ScenarioGeneratorParser().to_dict(cmdopts['scenario']) is_2x1 = kw['arena_x'] == 2 * kw['arena_y'] is_1x1 = kw['arena_x'] == kw['arena_y'] if is_2x1: r = range(kw['arena_x'], kw['arena_x'] + attr['cardinality'] * attr['arena_size_inc'], attr['arena_size_inc']) dims = [ sierra.core.utils.ArenaExtent( Vector3D(x, int(x / 2), kw['arena_z'])) for x in r ] elif is_1x1: r = range(kw['arena_x'], kw['arena_x'] + attr['cardinality'] * attr['arena_size_inc'], attr['arena_size_inc']) dims = [ sierra.core.utils.ArenaExtent(Vector3D(x, x, kw['arena_z'])) for x in r ] else: raise NotImplementedError( "Unsupported arena X,Y scaling '{0}': Must be [2x1,1x1]") def __init__(self) -> None: PopulationConstantDensity.__init__(self, cli_arg, main_config, cmdopts['batch_input_root'], attr["target_density"], dims, kw['scenario_tag']) return type( cli_arg, # type: ignore (PopulationConstantDensity, ), {"__init__": __init__})
def _gen_camera_config(self, ext: ArenaExtent, index: int, n_cameras) -> tuple: angle = (index % n_cameras) * (2.0 * math.pi / n_cameras) look_at = Vector3D(ext.xsize() / 2.0, ext.ysize() / 2.0, 0.0) hyp = math.sqrt(2 * max(look_at.x, look_at.y)**2) # As time proceeds and we interpolate between cameras, we want to slowly # be zooming in/moving the cameras closer to the arena center. if self.cmdline == 'sierra.sw+interp+zoom': factor = math.sqrt((n_cameras + index) / n_cameras) else: factor = 1.0 pos_x = (hyp * math.cos(angle) + look_at.x) / factor pos_y = (hyp * math.sin(angle) + look_at.y) / factor pos_z = (max(ext.xsize(), ext.ysize()) * 0.50) / factor pos = Vector3D(pos_x, pos_y, pos_z) # This is what the ARGoS source does for the up vector for the default # camera configuration up = Vector3D(0, 0, 1) return index, up, look_at, pos
def factory(cli_arg: str, main_config: types.YAMLDict, cmdopts: types.Cmdopts) -> PopulationSize: """ Factory to create :class:`PopulationSize` derived classes from the command line definition. """ parser = population_size.Parser() attr = parser(cli_arg) max_sizes = parser.to_sizes(attr) if cmdopts['robot_positions']: positions = [ Vector3D.from_str(s, astype=float) for s in cmdopts['robot_positions'] ] else: # Get the dimensions of the effective arena from the scenario so we can # place robots randomly within it. sgp = pm.module_load_tiered( project=cmdopts['project'], path='generators.scenario_generator_parser') kw = sgp.ScenarioGeneratorParser().to_dict(cmdopts['scenario']) xs = random.choices(range(0, kw['arena_x']), k=max_sizes[-1]) ys = random.choices(range(0, kw['arena_y']), k=max_sizes[-1]) zs = random.choices(range(0, kw['arena_z']), k=max_sizes[-1]) positions = [Vector3D(x, y, z) for x, y, z in zip(xs, ys, zs)] def __init__(self) -> None: PopulationSize.__init__(self, cli_arg, main_config, cmdopts['batch_input_root'], cmdopts['robot'], max_sizes, positions) return type( cli_arg, # type: ignore (PopulationSize, ), {"__init__": __init__})
def __init__(self, criteria: bc.IConcreteBatchCriteria, exp_num: int, cmdopts: types.Cmdopts) -> None: self.exp_num = exp_num self.exp_input_root = os.path.join( cmdopts['batch_input_root'], criteria.gen_exp_dirnames(cmdopts)[exp_num]) self.exp_def_fpath = os.path.join(self.exp_input_root, sierra.core.config.kPickleLeaf) self.logger = logging.getLogger(__name__) self.criteria = criteria from_bivar_bc1 = False from_bivar_bc2 = False from_univar_bc = False if criteria.is_bivar(): bivar = tp.cast(bc.BivarBatchCriteria, criteria) from_bivar_bc1 = hasattr(bivar.criteria1, 'exp_scenario_name') from_bivar_bc2 = hasattr(bivar.criteria2, 'exp_scenario_name') else: from_univar_bc = hasattr(criteria, 'exp_scenario_name') # Need to get per-experiment arena dimensions from batch criteria, as # they might be different for each experiment if from_univar_bc: self.arena_dim = criteria.arena_dims()[exp_num] self.scenario_name = criteria.exp_scenario_name(exp_num) self.logger.debug( "Read scenario dimensions '%s' from univariate batch criteria", self.arena_dim) elif from_bivar_bc1 or from_bivar_bc2: self.arena_dim = criteria.arena_dims()[exp_num] self.logger.debug( "Read scenario dimensions '%s' bivariate batch criteria", self.arena_dim) self.scenario_name = criteria.exp_scenario_name(exp_num) else: # Default case: scenario dimensions read from cmdline sgp = pm.module_load_tiered( project=cmdopts['project'], path='generators.scenario_generator_parser') kw = sgp.ScenarioGeneratorParser().to_dict(cmdopts['scenario']) self.arena_dim = ArenaExtent( Vector3D(kw['arena_x'], kw['arena_y'], kw['arena_z'])) self.logger.debug("Read scenario dimensions %s from cmdline spec", self.arena_dim) self.scenario_name = cmdopts['scenario']
def arena_dims(self) -> tp.List[utils.ArenaExtent]: """ Returns: Arena dimensions used for each experiment in the batch. Not applicable to all criteria. """ dims = [] for exp in self.gen_attr_changelist(): for c in exp: if c.path == ".//arena" and c.attr == "size": x, y, z = c.value.split(',') dims.append(utils.ArenaExtent(Vector3D(int(float(x)), int(float( y)), int(float(z))))) assert len(dims) > 0,\ "Scenario dimensions not contained in batch criteria" return dims
def gen_attr_changelist(self) -> tp.List[XMLAttrChangeSet]: """ Generate list of sets of changes to input file to set the # robots for a set of arena sizes such that the swarm density is constant. Robots are approximated as point masses. """ if not self.already_added: for changeset in self.attr_changes: for path, attr, value in changeset: if path == ".//arena" and attr == "size": x, y, z = [int(float(_)) for _ in value.split(",")] extent = sierra.core.utils.ArenaExtent( Vector3D(x, y, z)) # ARGoS won't start if there are 0 robots, so you always # need to put at least 1. n_robots = int(extent.area() * (self.target_density / 100.0)) if n_robots == 0: n_robots = 1 self.logger.warning( "n_robots set to 1 even though \ calculated as 0 for area=%s,density=%s", extent.area, self.target_density) changeset.add( XMLAttrChange(".//arena/distribute/entity", "quantity", str(n_robots))) self.logger.debug( "Calculated swarm size=%d for extent=%s,density=%s", n_robots, str(extent), self.target_density) break self.already_added = True return self.attr_changes