Exemplo n.º 1
0
 def testNotEqualDifferentMaximum(self):
     spec_1 = array_spec.BoundedArraySpec((1, 2),
                                          np.int32,
                                          minimum=0.0,
                                          maximum=2.0)
     spec_2 = array_spec.BoundedArraySpec((1, 2),
                                          np.int32,
                                          minimum=[0.0, 0.0],
                                          maximum=[1.0, 1.0])
     self.assertNotEqual(spec_1, spec_2)
Exemplo n.º 2
0
 def testEqualBroadcastingBounds(self):
     spec_1 = array_spec.BoundedArraySpec((1, 2),
                                          np.int32,
                                          minimum=0.0,
                                          maximum=1.0)
     spec_2 = array_spec.BoundedArraySpec((1, 2),
                                          np.int32,
                                          minimum=[0.0, 0.0],
                                          maximum=[1.0, 1.0])
     self.assertEqual(spec_1, spec_2)
    def _compute_action_spec(self, discrete_actions, continuous_actions):
        """Helper for `__init__`: compute our environment's action spec."""
        valid_actions = []

        # First discrete actions:
        if discrete_actions is not None:
            try:
                # Get an array of upper and lower bounds for each discrete action.
                min_, max_ = zip(*discrete_actions)
                # Total number of discrete actions provided on each time step.
                shape = (len(discrete_actions), )
            except TypeError:
                min_, max_ = discrete_actions  # Enforces 2-tuple.
                shape = (1, )
            spec = specs.BoundedArraySpec(shape=shape,
                                          dtype='int32',
                                          minimum=min_,
                                          maximum=max_,
                                          name='discrete')
            valid_actions.append(spec)

        # Then continuous actions:
        if continuous_actions is not None:
            try:
                # Get an array of upper and lower bounds for each continuous action.
                min_, max_ = zip(*continuous_actions)
                # Total number of continuous actions provided on each time step.
                shape = (len(continuous_actions), )
            except TypeError:
                min_, max_ = continuous_actions  # Enforces 2-tuple
                shape = (1, )
            spec = specs.BoundedArraySpec(shape=shape,
                                          dtype='float32',
                                          minimum=min_,
                                          maximum=max_,
                                          name='continuous')
            valid_actions.append(spec)

        # And in total we have this many actions.
        action_size = sum(value.shape[0] for value in valid_actions)

        if action_size <= 0:
            raise ValueError(
                'A pycolab Environment adapter was initialised '
                'without any discrete or continuous actions specified.')

        # Use arrays directly if we only have one.
        if len(valid_actions) == 1:
            valid_actions = valid_actions[0]

        return valid_actions, action_size
Exemplo n.º 4
0
    def testScalarBounds(self):
        spec = array_spec.BoundedArraySpec((),
                                           np.float,
                                           minimum=0.0,
                                           maximum=1.0)

        self.assertIsInstance(spec.minimum, np.ndarray)
        self.assertIsInstance(spec.maximum, np.ndarray)

        # Sanity check that numpy compares correctly to a scalar for an empty shape.
        self.assertEqual(0.0, spec.minimum)
        self.assertEqual(1.0, spec.maximum)

        # Check that the spec doesn't fail its own input validation.
        _ = array_spec.BoundedArraySpec(spec.shape, spec.dtype, spec.minimum,
                                        spec.maximum)
Exemplo n.º 5
0
 def testGenerateValue(self):
     spec = array_spec.BoundedArraySpec((2, 2),
                                        np.int32,
                                        minimum=5,
                                        maximum=10)
     test_value = spec.generate_value()
     spec.validate(test_value)
Exemplo n.º 6
0
 def testRepr(self):
     as_string = repr(
         array_spec.BoundedArraySpec((1, 2),
                                     np.int32,
                                     minimum=101.0,
                                     maximum=73.0))
     self.assertIn("101", as_string)
     self.assertIn("73", as_string)
Exemplo n.º 7
0
 def testValidateBounds(self):
     spec = array_spec.BoundedArraySpec((2, 2),
                                        np.int32,
                                        minimum=5,
                                        maximum=10)
     spec.validate(np.array([[5, 6], [8, 10]], dtype=np.int32))
     with self.assertRaises(ValueError):
         spec.validate(np.array([[5, 6], [8, 11]], dtype=np.int32))
     with self.assertRaises(ValueError):
         spec.validate(np.array([[4, 6], [8, 10]], dtype=np.int32))
Exemplo n.º 8
0
    def testNotEqualOtherClass(self):
        spec_1 = array_spec.BoundedArraySpec((1, 2),
                                             np.int32,
                                             minimum=[0.0, -0.6],
                                             maximum=[1.0, 1.0])
        spec_2 = array_spec.ArraySpec((1, 2), np.int32)
        self.assertNotEqual(spec_1, spec_2)
        self.assertNotEqual(spec_2, spec_1)

        spec_2 = None
        self.assertNotEqual(spec_1, spec_2)
        self.assertNotEqual(spec_2, spec_1)

        spec_2 = ()
        self.assertNotEqual(spec_1, spec_2)
        self.assertNotEqual(spec_2, spec_1)
Exemplo n.º 9
0
 def testMinMaxAttributes(self):
     spec = array_spec.BoundedArraySpec((1, 2, 3), np.float32, 0, (5, 5, 5))
     self.assertEqual(type(spec.minimum), np.ndarray)
     self.assertEqual(type(spec.maximum), np.ndarray)
Exemplo n.º 10
0
 def testInvalidMaximum(self):
     with self.assertRaisesRegex(ValueError, "not compatible"):
         array_spec.BoundedArraySpec((3, 5), np.uint8, 0, (1, 1, 1))
Exemplo n.º 11
0
 def testNotWriteable(self):
     spec = array_spec.BoundedArraySpec((1, 2, 3), np.float32, 0, (5, 5, 5))
     with self.assertRaisesRegex(ValueError, "read-only"):
         spec.minimum[0] = -1
     with self.assertRaisesRegex(ValueError, "read-only"):
         spec.maximum[0] = 100