Esempio n. 1
0
    def test_all_supported_methods(self):
        randomness = RandomnessManager(2, bucket_size=10000)

        # loop through all supported methods
        for method in SUPPORTED_RANDOM_METHODS:
            for i in range(10):
                randomness.get_random(method)
Esempio n. 2
0
 def __init__(self, random_method='pseudo_random'):
     if random_method not in SUPPORTED_RANDOM_METHODS:
         print("Given random_method is not valid! Valid options includes:\n"
               "{}".format('\n'.join(
                   (' - {}'.format(m) for m in SUPPORTED_RANDOM_METHODS))))
         import sys
         sys.exit(1)
     self.random_method = random_method
     self.random = RandomnessManager()
Esempio n. 3
0
    def test_all_supported_methods_higher_dimensions(self):

        for num_dim in range(2, 10):
            # loop through all supported methods
            for method in SUPPORTED_RANDOM_METHODS:
                # NOTE: the fast sampler random_method requires some minimum sample size
                randomness = RandomnessManager(
                    num_dim, bucket_size=10 if method != "fast" else 10000)
                for i in range(10):
                    randomness.get_random(method)
Esempio n. 4
0
    def init(self, **kwargs):
        """The delayed **initialisation** method

        :param num_dim: the number of dimensions

        """
        super().init(**kwargs)
        self.random = RandomnessManager(num_dim=kwargs["num_dim"])

        self.use_original_method = False

        if self.args.engine == "klampt":
            self.low, self.high = (
                [-np.pi] * kwargs["num_dim"],
                [np.pi] * kwargs["num_dim"],
            )
        elif self.args.engine == "4d":
            self.low, self.high = [
                [0, 0, -np.pi, -np.pi],
                [self.args.env.dim[0], self.args.env.dim[1], np.pi, np.pi],
            ]
        else:
            self.use_original_method = True
Esempio n. 5
0
    def test_redraw(self):
        # test drawing more than NUM_DATA_POINTS to see if it will automatically refill

        test_bucket_size = 10
        randomness = RandomnessManager(2, bucket_size=test_bucket_size)

        method = "pseudo_random"
        randomness.get_random(method)

        # test it is currently one less than the requested size
        self.assertEqual(len(randomness.random_draws[method]),
                         test_bucket_size - 1)

        # exhaust the sampled points
        for i in range(test_bucket_size - 1):
            randomness.get_random(method)

        # ensure all points being exhausted
        self.assertEqual(len(randomness.random_draws[method]), 0)

        # ensure it will refill
        randomness.get_random(method)
        self.assertEqual(len(randomness.random_draws[method]),
                         test_bucket_size - 1)
Esempio n. 6
0
class RandomPolicySampler(Sampler):
    @overrides
    def __init__(self, random_method='pseudo_random'):
        if random_method not in SUPPORTED_RANDOM_METHODS:
            print("Given random_method is not valid! Valid options includes:\n"
                  "{}".format('\n'.join(
                      (' - {}'.format(m) for m in SUPPORTED_RANDOM_METHODS))))
            import sys
            sys.exit(1)
        self.random_method = random_method
        self.random = RandomnessManager()

    @overrides
    def get_next_pos(self):
        # Random path
        if random.random() < self.args.goalBias:
            # goal bias
            p = self.goal_pos
        else:
            p = self.random.get_random(self.random_method)
            p[0] *= self.args.XDIM
            p[1] *= self.args.YDIM
        return p, self.report_success, self.report_fail
Esempio n. 7
0
class RandomPolicySampler(Sampler):
    r"""Uniformly and randomly samples configurations across :math:`d` where
    :math:`d` is
    the dimensionality of the *C-Space*.
    :class:`~samplers.randomPolicySampler.RandomPolicySampler` samples configuration
    :math:`q \in \mathbb{R}^d` across each dimension uniformly with an
    :math:`0 \le \epsilon < 1` bias towds the goal configuration.

    A random number :math:`p \sim \mathcal{U}(0,1)` is first drawn, then the
    configuration :math:`q_\text{new}` that this function returns is given by

    .. math::
        q_\text{new} =
        \begin{cases}
            q \sim \mathcal{U}(0,1)^d & \text{if } p < \epsilon\\
            q_\text{target}  & \text{otherwise.}
        \end{cases}

    :py:const:`CONSTANT`
    """
    @overrides
    def __init__(self, random_method: str = "pseudo_random", **kwargs):
        """
        :param random_method: the kind of random method to use. Must be a choice from
            :data:`randomness.SUPPORTED_RANDOM_METHODS`.
        :param kwargs: pass through to super class
        """
        super().__init__(**kwargs)
        if random_method not in SUPPORTED_RANDOM_METHODS:
            raise ValueError(
                "Given random_method is not valid! Valid options includes:\n"
                "{}".format("\n".join(
                    (" - {}".format(m) for m in SUPPORTED_RANDOM_METHODS))))

        self.random_method = random_method
        self.random = None

    @overrides
    def init(self, **kwargs):
        """The delayed **initialisation** method

        :param num_dim: the number of dimensions

        """
        super().init(**kwargs)
        self.random = RandomnessManager(num_dim=kwargs["num_dim"])

        self.use_original_method = False

        if self.args.engine == "klampt":
            self.low, self.high = (
                [-np.pi] * kwargs["num_dim"],
                [np.pi] * kwargs["num_dim"],
            )
        elif self.args.engine == "4d":
            self.low, self.high = [
                [0, 0, -np.pi, -np.pi],
                [self.args.env.dim[0], self.args.env.dim[1], np.pi, np.pi],
            ]
        else:
            self.use_original_method = True

    @overrides
    def get_next_pos(self) -> Sampler.GetNextPosReturnType:
        # Random path
        if random.random() < self.args.goalBias:
            # goal bias
            p = self.goal_pos
        else:
            if self.use_original_method:
                p = self.random.get_random(self.random_method)
                p *= self.args.env.dim
            else:
                p = np.random.uniform(self.low, self.high)

        return p, self.report_success, self.report_fail
Esempio n. 8
0
 def test_non_existing_method(self):
     with self.assertRaises(ValueError):
         randomness = RandomnessManager(2)
         randomness.get_random("my_non_existing_random_method")