def create_obs_spec(vehicle):
    obs_spec = None
    for attached in vehicle.sensors:
        sensor = attached.sensor
        ss = StreamSpec.from_yaml(sensor.observations_spec)
        obs_spec = (ss if obs_spec is None else StreamSpec.join(obs_spec, ss))
        assert obs_spec is not None
    assert obs_spec is not None
    return obs_spec
Beispiel #2
0
 def get_spec(self):
     self._make_sure_inited()
     ncmds = self.discdds.get_num_commands()
     cmd = make_streamels_finite_commands(ncommands=ncmds, default=0)
     cmd_spec = StreamSpec(id_stream=None, streamels=cmd, extra={})
     obs_spec = StreamSpec(id_stream=None,
                           streamels=make_streamels_rgb_float(self.shape),
                           extra={})
     return BootSpec(obs_spec, cmd_spec)
def create_obs_spec(vehicle):
    obs_spec = None
    for attached in vehicle.sensors:
        sensor = attached.sensor
        ss = StreamSpec.from_yaml(sensor.observations_spec)
        obs_spec = (ss if obs_spec is None
                    else StreamSpec.join(obs_spec, ss))
        assert obs_spec is not None
    assert obs_spec is not None
    return obs_spec
    def __init__(self, robot, obs_nuisance=[], cmd_nuisance=[]):
        self.inner_robot_name = robot 

        boot_config = get_boot_config()
        id_robot, self.robot = boot_config.robots.instance_smarter(robot)

        if not isinstance(self.robot, RobotInterface):
            msg = 'Expected RobotInterface, got %s' % describe_type(self.robot)
            raise ValueError(msg)
        
        warnings.warn('handle the case better')
        self.desc = ('EquivRobot(%s,obs:%s,cmd:%s)'
                    % (id_robot, obs_nuisance, cmd_nuisance))

        # convert to (possibly empty) list of strings
        if isinstance(obs_nuisance, str):
            obs_nuisance = [obs_nuisance]
        if isinstance(cmd_nuisance, str):
            cmd_nuisance = [cmd_nuisance]

        instance = lambda y: boot_config.nuisances.instance_smarter(y)[1]
        
        self.obs_nuisances = [instance(x) for x in obs_nuisance]
        self.cmd_nuisances = [instance(x) for x in cmd_nuisance]
        # No - we should not call inverse() before transform_spec()

        obs_spec = self.robot.get_spec().get_observations()
        for n in self.obs_nuisances:
            obs_spec = n.transform_spec(obs_spec)

        cmd_spec = self.robot.get_spec().get_commands()
        for n in self.cmd_nuisances:
            cmd_spec = n.transform_spec(cmd_spec)

        # We don't really need to compute this...
        try:
            self.cmd_nuisances_inv = [x.inverse() for x in self.cmd_nuisances]
    
            # now initialize in reverse
            cmd_spec_i = cmd_spec
            for n in reversed(self.cmd_nuisances_inv):
                cmd_spec_i = n.transform_spec(cmd_spec_i)
    
            StreamSpec.check_same_spec(cmd_spec_i,
                                   self.robot.get_spec().get_commands())
            # TODO: why do we do this for commands, not for osbservations?
        except Exception as e:
            logger.warning('It seems that this chain of nuisances is not '
                           'exact, but it could be OK to continue. '
                           ' The chain is %s; the error is:\n%s' % 
                           (cmd_nuisance, indent(str(e).strip(), '> ')))

        self.spec = BootSpec(obs_spec=obs_spec, cmd_spec=cmd_spec)
        self.obs_nuisances_id = obs_nuisance
        self.cmd_nuisances_id = cmd_nuisance
Beispiel #5
0
    def __init__(self, base, top):
        self.base = base
        self.top = top

        cmd1 = StreamSpec.from_yaml(self.base.get_commands_spec())
        cmd2 = StreamSpec.from_yaml(self.top.get_commands_spec())

        cmd_spec = StreamSpec.join(cmd1, cmd2).to_yaml()

        components = [self.base.get_state_space(),
                      self.top.get_state_space()]
        state_space = ProductManifold(components)
        Dynamics.__init__(self, commands_spec=cmd_spec,
                          state_space=state_space)
Beispiel #6
0
    def __init__(self, base, top):
        self.base = base
        self.top = top

        cmd1 = StreamSpec.from_yaml(self.base.get_commands_spec())
        cmd2 = StreamSpec.from_yaml(self.top.get_commands_spec())

        cmd_spec = StreamSpec.join(cmd1, cmd2).to_yaml()

        components = [self.base.get_state_space(), self.top.get_state_space()]
        state_space = ProductManifold(components)
        Dynamics.__init__(self,
                          commands_spec=cmd_spec,
                          state_space=state_space)
def check_pickling(x):
    spec = StreamSpec.from_yaml(x)
    sio = StringIO()
    pickle.dump(spec, sio)
    sio = StringIO(sio.getvalue())
    spec2 = pickle.load(sio)
    assert spec == spec2
def test_compression1():
    spec = StreamSpec.from_yaml({"shape": [100, 100], "format": "C", "range": [0, 5], "default": 4})

    yaml2 = spec.to_yaml()
    assert yaml2["format"] == "C"
    assert yaml2["range"] == [0, 5]
    assert yaml2["default"] == 4
def check_conversions(x):
    spec = StreamSpec.from_yaml(x)
    assert isinstance(spec, StreamSpec)
    y = spec.to_yaml()
    assert isinstance(y, dict)
    spec2 = spec.from_yaml(y)
    assert spec == spec2
    def __init__(self,
                 world=None,
                 id_world=None,
                 vehicle=None,
                 id_vehicle=None,
                 dt=VehiclesConstants.DEFAULT_SIMULATION_DT,
                 **kwargs):
        self.dt = dt

        if not ((world is not None) ^ (id_world is not None)):
            raise ValueError('Specify exactly one of "world" and "id_world".')
        if not ((vehicle is not None) ^ (id_vehicle is not None)):
            raise ValueError('Specify exactly one of "vehicle" '
                             'and "id_vehicle".')

        vehicles = get_conftools_vehicles()
        worlds = get_conftools_worlds()

        # TODO: user shortcuts
        if vehicle is not None:
            id_vehicle = vehicle['id']
            vehicle_spec = vehicle
            # TODO: check well formed
            vehicle = vehicles.instance_spec(vehicle)
        else:
            vehicle_spec = vehicles[id_vehicle]
            vehicle = vehicles.instance(id_vehicle)

        if world is not None:
            id_world = world['id']
            # TODO: check well formed
            world_spec = world
            world = worlds.instance_spec(world)
        else:
            world_spec = worlds[id_world]
            world = worlds.instance(id_world)

        VehicleSimulation.__init__(self, vehicle, world, **kwargs)

        cmd_spec = StreamSpec.from_yaml(
                                self.vehicle.dynamics.get_commands_spec())

        self.last_commands = cmd_spec.get_default_value()

        if len(self.vehicle.sensors) == 0:
            raise Exception('Vehicle %r has no sensors defined.' % id_vehicle)

        obs_spec = create_obs_spec(self.vehicle)
        self._boot_spec = BootSpec(obs_spec=obs_spec, cmd_spec=cmd_spec,
                                   id_robot=id_vehicle)
        # XXX: id, desc, extra?
        self.commands_source = BootOlympicsConstants.CMD_SOURCE_REST

        self.boot_episode_started = False

        # Save for later
        self.id_world = id_world
        self.id_vehicle = id_vehicle
        self.world_spec = world_spec
        self.vehicle_spec = vehicle_spec
def check_parsing(x):
    StreamSpec.from_yaml(x)
def test_default_values_5():
    """ 2D default values """
    spec = StreamSpec.from_yaml({"shape": [2, 2], "format": "D", "range": [0, 5], "default": 4})
    assert_allclose(spec.get_default_value(), [[4, 4], [4, 4]])
def test_default_values_4():
    """ Given default value; all the same. """
    spec = StreamSpec.from_yaml({"shape": [3], "format": "D", "range": [0, 5], "default": 4})
    assert_allclose(spec.get_default_value(), [4, 4, 4])
def test_default_values_3():
    """ Given default value. """
    spec = StreamSpec.from_yaml({"shape": [3], "format": "D", "range": [0, 3], "default": [1, 2, 3]})
    assert_allclose(spec.get_default_value(), [1, 2, 3])
def test_default_values_2():
    """ Default default value is middle of range, 
        rounded down for discrete. """
    spec = StreamSpec.from_yaml({"shape": [3], "format": "D", "range": [0, 1]})
    assert_allclose(spec.get_default_value(), [0, 0, 0])
def test_default_values_1():
    """ Default default value is middle of range. """
    spec = StreamSpec.from_yaml({"shape": [3], "format": "C", "range": [0, 1]})
    assert_allclose(spec.get_default_value(), [0.5, 0.5, 0.5])
def check_conversions(stream_spec1, nuisance):
    # print('Checking %s / %s ' % (stream_spec1, nuisance))
    nuisance_inv = None
    try:
        try:
            stream_spec2 = nuisance.transform_spec(stream_spec1)
        except UnsupportedSpec as e:
            logger.info('Skipping %s/%s because incompatible: %s' % 
                        (stream_spec1, nuisance, e))
            return

        value1 = stream_spec1.get_random_value()
        stream_spec1.check_valid_value(value1)


        value2 = nuisance.transform_value(value1)
        stream_spec2.check_valid_value(value2)

        try:
            nuisance_inv = nuisance.inverse()
        except NuisanceNotInvertible as e:
            logger.info('Skipping some tests %s/%s because not invertible:'
                        ' %s' % (stream_spec1, nuisance, e))
            return

        try:
            stream_spec1b = nuisance_inv.transform_spec(stream_spec2)
        except UnsupportedSpec as e:
            msg = ('The inverse of the nuisance does not seem to be able '
                   'to handle the result:\n%s\n\n'
                   '  stream_spec1: %s\n'
                   '      nuisance: %s\n'
                   '  stream_spec2: %s\n'
                   '  nuisance_inv: %s\n' % 
                   (indent(str(e), '>'),
                    stream_spec1.to_yaml(), nuisance,
                    stream_spec2.to_yaml(), nuisance_inv))
            raise ValueError(msg)

        try:
            StreamSpec.check_same_spec(stream_spec1, stream_spec1b)
        except Exception as e:
            msg = ('The inverse of the nuisance does not recreate the '
                   'initial spec:\n%s\n\n'
                   '  stream_spec1: %s\n'
                   '      nuisance: %s\n'
                   '  stream_spec2: %s\n'
                   '  nuisance_inv: %s\n'
                   ' stream_spec1b: %s\n' % 
                   (indent(str(e), '>'),
                    stream_spec1.to_yaml(), nuisance,
                    stream_spec2.to_yaml(), nuisance_inv,
                    stream_spec1b.to_yaml()))
            raise ValueError(msg)

        value1b = nuisance_inv.transform_value(value2)
        stream_spec1.check_valid_value(value1b)

        # TODO: if exact
        assert_allclose(value1, value1b, rtol=1e-5)

    except:
        logger.error('Error while testing:')
        logger.error(' stream_spec:  %s ' % stream_spec1.to_yaml())
        logger.error(' nuisance:     %s' % nuisance)
        logger.error(' nuisance_inv: %s' % nuisance_inv)
        raise
    def __init__(self,
                 world=None,
                 id_world=None,
                 vehicle=None,
                 id_vehicle=None,
                 dt=VehiclesConstants.DEFAULT_SIMULATION_DT,
                 **kwargs):
        self.dt = dt

        if not ((world is not None) ^ (id_world is not None)):
            raise ValueError('Specify exactly one of "world" and "id_world".')
        if not ((vehicle is not None) ^ (id_vehicle is not None)):
            raise ValueError('Specify exactly one of "vehicle" '
                             'and "id_vehicle".')

        vehicles = get_conftools_vehicles()
        worlds = get_conftools_worlds()

        # TODO: user shortcuts
        if vehicle is not None:
            id_vehicle = vehicle['id']
            vehicle_spec = vehicle
            # TODO: check well formed
            vehicle = vehicles.instance_spec(vehicle)
        else:
            vehicle_spec = vehicles[id_vehicle]
            vehicle = vehicles.instance(id_vehicle)

        if world is not None:
            id_world = world['id']
            # TODO: check well formed
            world_spec = world
            world = worlds.instance_spec(world)
        else:
            world_spec = worlds[id_world]
            world = worlds.instance(id_world)

        VehicleSimulation.__init__(self, vehicle, world, **kwargs)

        cmd_spec = StreamSpec.from_yaml(
            self.vehicle.dynamics.get_commands_spec())

        self.last_commands = cmd_spec.get_default_value()

        if len(self.vehicle.sensors) == 0:
            raise Exception('Vehicle %r has no sensors defined.' % id_vehicle)

        obs_spec = create_obs_spec(self.vehicle)
        self._boot_spec = BootSpec(obs_spec=obs_spec,
                                   cmd_spec=cmd_spec,
                                   id_robot=id_vehicle)
        # XXX: id, desc, extra?
        self.commands_source = BootOlympicsConstants.CMD_SOURCE_REST

        self.boot_episode_started = False

        # Save for later
        self.id_world = id_world
        self.id_vehicle = id_vehicle
        self.world_spec = world_spec
        self.vehicle_spec = vehicle_spec