Exemplo n.º 1
0
 def samples(self, count=10, mp=NUMPY):
     """Return :count: samples from """
     if mp not in [NUMPY, TORCH]:
         raise RuntimeError("Cannot sample without numpy-derived package.")
     samples = mathpack_convert(self.widths, mp) * mp.random(
         count, len(self.bounds))
     samples += mathpack_convert([_[0] for _ in self.bounds], mp)
     return samples
Exemplo n.º 2
0
 def ret(args, t=None, mp=mp):
     if len(args) == 3:
         x, th, intx = args[:3]
         u = control_function(mp.vect_cat(x, th, intx), mp=mp)
         return mathpack_convert([v * mp.sin(th), u + curve(0), x], mp)
     else:
         x, y, th, intx = args[:4]
         u = control_function(mp.vect_cat(x, th, intx), mp=mp)
         return mathpack_convert(
             [v * mp.sin(th), v * mp.cos(th), u + curve(y), x], mp)
Exemplo n.º 3
0
 def ret(args, t=None, mp=mp):
     if len(args) == 2:
         x, theta = args[:2]
         u = control_function(mp.vect_cat(x, theta), mp=mp)
         return mathpack_convert([v * mp.sin(theta), u], mp)
     else:
         x, y, theta = args[:3]
         u = control_function(mp.vect_cat(x, theta), mp=mp)
         return mathpack_convert(
             [v * mp.sin(theta), v * mp.cos(theta), u + curve(y)], mp)
Exemplo n.º 4
0
def flow(ip, vf, control_points=None):
    """Return a time series representing the solution to the differential equation represented by the vector field with
    the starting point. Uses numpy.

    :param ip: initial point of the differential equation
    :param vf: vector field defining the differential equation
    :param control_points: optional numpy array giving the times where the solution should be given.
    Defaults to all multiples of 10^(-4) in [0, 1] interval."""
    if control_points is None:
        control_points = np.arange(0, 1, 1E-4)

    control_points = mathpack_convert(control_points, NUMPY)
    ip = mathpack_convert(ip, NUMPY)

    def safe_vf(x, t):
        return mathpack_convert(vf(x, t), NUMPY)

    return odeint(safe_vf, ip, control_points)
Exemplo n.º 5
0
    def __call__(self, x, mp=TORCH):
        x = mathpack_convert(x, mp)
        if mp.size(x) != 3:
            raise ValueError("bad recurrent controller eval")
        self._prep_for_package(mp)

        ret = mp.matmul(self.first_layer, mp.v2c(x))
        ret = mp.tanh(ret)
        ret = mp.matmul(self.second_layer, ret)
        ret = mp.tanh(ret)
        return -ret[0][0]
Exemplo n.º 6
0
    def __call__(self, x, mp=NUMPY):
        """Return the value of the control signal at the given point."""
        x = mathpack_convert(x, mp)
        if mp.size(x) != 2:
            raise ValueError("bad tuncali controller eval")
        self._prep_for_package(mp)

        ret = mp.matmul(self.first_layer, mp.v2c(x))
        ret = mp.tanh(ret)
        ret = mp.matmul(self.second_layer, ret)
        ret = mp.tanh(ret)
        return -ret[0][0]
Exemplo n.º 7
0
    def constraints(cls, vf, pts, mp=NUMPY):
        """Computes the coefficients of an inequality on the linear representation of the form which must be satisfied
        in order to make the value of the Lie derivative negative at each of the given points."""
        matrix = []
        for pt in pts:
            dyn = vf(pt)
            row = [0] * qf_monomial_count(mp.size(pt))

            for i, state_value in enumerate(pt):
                for j, dynamics_value in enumerate(dyn):
                    linear_coord = square_coord_to_linear(i, j, mp.size(pt))
                    row[linear_coord] += state_value * dynamics_value
            matrix.append(row)
        return mathpack_convert(matrix, mp)
def safe_region_samples(count,
                        region=TUNCALI_PHASE_U,
                        exclude=TUNCALI_PHASE_X0,
                        mp=NUMPY):
    samples = region.samples(count, mp=mp)
    if exclude is not None:
        samples = [_ for _ in samples if _ not in exclude]

    while len(samples) < count:
        new_samples = region.samples(count - len(samples), mp=mp)
        if exclude is not None:
            new_samples = [_ for _ in new_samples if _ not in exclude]
        samples.extend(new_samples)
    return mathpack_convert(samples, mp)
Exemplo n.º 9
0
    def __init__(self, weights=None, n_hidden=3):
        if weights is None:
            weights = torch.rand(4 * n_hidden)
        else:
            weights = mathpack_convert(weights, TORCH)
            weights = torch.flatten(weights)

        if weights.numel() != 4 * n_hidden:
            raise ValueError("bad recurrent controller init")

        self.first_layer = weights[:3 * n_hidden].reshape(n_hidden, 3)
        self.first_layer.requires_grad = True
        self.second_layer = weights[3 * n_hidden:].reshape(1, n_hidden)
        self.second_layer.requires_grad = True
        self.n_hidden = n_hidden
Exemplo n.º 10
0
 def _prep_for_package(self, mp):
     self._linear_rep = mathpack_convert(self._linear_rep, mp)
     self._square_rep = mathpack_convert(self._square_rep, mp)
Exemplo n.º 11
0
 def _prep_for_package(self, mp):
     self.first_layer = mathpack_convert(self.first_layer, mp)
     self.second_layer = mathpack_convert(self.second_layer, mp)
     if mp == TORCH:
         self.first_layer.requires_grad = True
         self.second_layer.requires_grad = True
Exemplo n.º 12
0
 def safe_vf(x, t):
     return mathpack_convert(vf(x, t), NUMPY)
Exemplo n.º 13
0
 def ret(args, t=None, mp=mp):
     x, y, theta = args[:3]
     return mathpack_convert([
         v * mp.sin(theta), v * mp.cos(theta),
         control_function(args, mp) + curve(y)
     ], mp)