示例#1
0
    def test_stack(self):
        x = amnet.Variable(3, name='x')

        w1 = np.array([[1, -2, 3]])
        b1 = np.zeros(1)

        w2 = np.array([[-5, 6, -7], [-8, 9, 10]])
        b2 = np.array([11, -12])

        w3 = np.array([[7, 8, 9]])
        b3 = np.array([-1])

        y1 = amnet.Linear(w1, x)
        y2 = amnet.Affine(w2, x, b2)
        y3 = amnet.Affine(w3, x, b3)

        y4 = x

        ystack = amnet.atoms.from_list([y1, y2, y3, y4])
        self.assertEqual(ystack.outdim, 4+3)
        self.assertEqual(ystack.indim, 3)

        def ystack_true(xinp):
            y1v = np.dot(w1, xinp) + b1
            y2v = np.dot(w2, xinp) + b2
            y3v = np.dot(w3, xinp) + b3
            y4v = xinp
            return np.concatenate((y1v, y2v, y3v, y4v), axis=0)

        for (xv0, xv1, xv2) in product(self.floatvals2, repeat=3):
            xinp = np.array([xv0, xv1, xv2])
            ysv = ystack.eval(xinp)
            ytv = ystack_true(xinp)
            self.assertAlmostEqual(norm(ysv - ytv), 0)
示例#2
0
def make_vgc(alpha):
    # inputs
    e_var = amnet.Variable(1, name='e')
    edot_var = amnet.Variable(1, name='edot')
    x = amnet.stack(e_var, edot_var)

    # affine transformations
    zero1 = amnet.atoms.make_const(np.zeros(1), x)
    ae = amnet.Affine(np.array([[alpha, 0]]), x, np.zeros(1))
    e = amnet.Affine(np.array([[1, 0]]), x, np.zeros(1))
    neg_e = amnet.Affine(np.array([[-1, 0]]), x, np.zeros(1))
    edot = amnet.Affine(np.array([[0, 1]]), x, np.zeros(1))
    neg_edot = amnet.Affine(np.array([[0, -1]]), x, np.zeros(1))

    return amnet.atoms.gate_or(amnet.atoms.gate_or(zero1, ae, neg_e, neg_edot),
                               ae, e, edot)
示例#3
0
def max_aff(A, b, phi):
    """ returns an AMN that evaluates to 
        max_i(sum_j a_{ij} phi_j + b_i) """
    (m, n) = A.shape
    assert len(b) == m
    assert phi.outdim == n
    assert m >= 1 and n >= 1

    # OLD: inefficient
    #phi_aff = amnet.Affine(
    #    A,
    #    phi,
    #    b
    #)
    #return max_all(phi_aff)

    outlist = []
    for i in range(m):
        if b[i] == 0:
            outlist.append(amnet.Linear(A[i, :].reshape((1, n)), phi))
        else:
            outlist.append(
                amnet.Affine(A[i, :].reshape((1, n)), phi, b[i].reshape(
                    (1, ))))
    assert len(outlist) == m
    return max_list(outlist)
示例#4
0
    def test_SmtEncoder_dag(self):
        xyz = amnet.Variable(3, name='xyz')
        x = amnet.atoms.select(xyz, 0)
        yz = amnet.Linear(
            np.array([[0, 1, 0], [0, 0, 1]]),
            xyz
        )

        maxyz = amnet.atoms.max_all(yz)
        twoxp1 = amnet.Affine(
            np.array([[2]]),
            x,
            np.array([1])
        )
        twox = amnet.atoms.add2(x, x)
        threex = amnet.atoms.add2(x, twox)
        fivexp1 = amnet.atoms.add2(twoxp1, threex)
        phi = amnet.atoms.add2(fivexp1, maxyz)

        def true_dag(fpin):
            x, y, z = fpin
            return 5*x + 1 + max(y, z)

        self.validate_outputs(
            phi=phi,
            onvals=itertools.product(self.floatvals2, repeat=3),
            true_f=true_dag
        )
示例#5
0
    def test_to_from_list(self):
        x = amnet.Variable(2, name='x')

        w = np.array([[1, 2], [3, 4], [5, 6]])
        b = np.array([7, 8, 9])
        y = amnet.Affine(
            w,
            x,
            b
        )

        self.assertEqual(y.outdim, 3)

        ylist = amnet.atoms.to_list(y)
        self.assertEqual(len(ylist), y.outdim)

        ytilde = amnet.atoms.from_list(ylist)
        self.assertEqual(ytilde.outdim, y.outdim)

        for (xv0, xv1) in product(self.floatvals, repeat=2):
            xinp = np.array([xv0, xv1])
            yv = y.eval(xinp)
            ylv = np.array([yi.eval(xinp) for yi in ylist]).flatten()  # note the flatten!
            ytv = ytilde.eval(xinp)
            self.assertAlmostEqual(norm(yv - ylv), 0)
            self.assertAlmostEqual(norm(yv - ytv), 0)
示例#6
0
    def test_max_all_max_aff(self):
        x = amnet.Variable(4, name='x')

        w = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]])
        b = np.array([1.1, 1.2, 1.3])

        y = amnet.Affine(
            w,
            x,
            b
        )
        max_y = amnet.atoms.max_all(y)
        maff = amnet.atoms.max_aff(w, b, x)

        def true_max_aff(xinp):
            return np.max(np.dot(w, xinp) + b)

        for xv in itertools.product(self.floatvals2, repeat=4):
            xinp = np.array(xv)

            tv = true_max_aff(xinp)
            max_y_v = max_y.eval(xinp)
            maff_v  = maff.eval(xinp)

            self.assertAlmostEqual(norm(tv - max_y_v), 0)
            self.assertAlmostEqual(norm(tv - maff_v), 0)
示例#7
0
    def stability_search1(self):
        #Ad = self.Ad_osc
        Ad = self.Ad_met
        (n, _) = Ad.shape
        assert n == 2

        xsys = amnet.Variable(n, name='xsys')
        phi = amnet.Affine(
            Ad,
            xsys,
            np.zeros(n)
        )

        # look for a Lyapunov function
        #amnet.lyap.stability_search1(phi, xsys, 10)
        amnet.lyap.stability_search1(phi, xsys, 4)  # enough for Metzler sys
示例#8
0
    def test_SmtEncoder_identity(self):
        x = amnet.Variable(2, name='x')
        w = np.array([[1, 2], [3, 4]])
        b = np.array([-1, -1])
        y = amnet.Affine(w, x, b)
        z = amnet.atoms.identity(y)

        self.assertEqual(y.outdim, 2)
        self.assertEqual(z.outdim, 2)
        self.assertEqual(z.indim, 2)

        def true_z(fpin):
            return np.dot(w, fpin) + b

        self.validate_outputs(
            phi=z,
            onvals=itertools.product(self.floatvals, repeat=z.indim),
            true_f=true_z
        )
示例#9
0
    def test_disprove_maxaff_local_lyapunov(self):
        # a simple system
        Ad = self.Ad_diag
        (n, _) = Ad.shape
        assert n == 2

        xsys = amnet.Variable(n, name='xsys')
        phi = amnet.Affine(
            Ad,
            xsys,
            np.zeros(n)
        )

        # a simple Lyapunov function
        Astar = np.array([[1, 1], [1, -1], [-1, 1], [-1, -1]])
        bstar = np.zeros(4)
        xc = amnet.lyap.find_local_counterexample(phi, xsys, Astar, bstar)

        # simple Lyapunov function works
        self.assertTrue(xc is None)
示例#10
0
    def test_identity(self):
        x = amnet.Variable(2, name='x')

        w = np.array([[1, 2], [3, 4], [5, 6]])
        b = np.array([7, 8, 9])

        y = amnet.Affine(
            w,
            x,
            b
        )
        self.assertEqual(y.outdim, 3)

        z = amnet.atoms.neg(y)
        self.assertEqual(z.outdim, y.outdim)

        def aff(x): return np.dot(w, x) + b
        for (xv0, xv1) in product(self.floatvals, repeat=2):
            xinp = np.array([xv0, xv1])
            yv = y.eval(xinp)
            zv = z.eval(xinp)
            self.assertAlmostEqual(norm(yv - aff(xinp)), 0)
            self.assertAlmostEqual(norm(zv + aff(xinp)), 0)
示例#11
0
def triplexer(phi, a, b, c, d, e, f):
    assert phi.outdim == 1
    assert all([len(p) == 4 for p in [a, b, c, d, e, f]])

    x = [None] * 4
    y = [None] * 4
    z = [None] * 4
    w = [None] * 4

    # Layer 1 weights
    for i in range(3):
        x[i] = amnet.Affine(
            np.array(a[i]).reshape((1, 1)), phi,
            np.array(b[i]).reshape((1, )))
        y[i] = amnet.Affine(
            np.array(c[i]).reshape((1, 1)), phi,
            np.array(d[i]).reshape((1, )))
        z[i] = amnet.Affine(
            np.array(e[i]).reshape((1, 1)), phi,
            np.array(f[i]).reshape((1, )))

    # Layer 1 nonlinearity
    for i in range(3):
        w[i] = amnet.Mu(x[i], y[i], z[i])

    # Layer 2 weights
    x[3] = amnet.Affine(
        np.array(a[3]).reshape((1, 1)), w[1],
        np.array(b[3]).reshape((1, )))
    y[3] = amnet.Affine(
        np.array(c[3]).reshape((1, 1)), w[2],
        np.array(d[3]).reshape((1, )))
    z[3] = amnet.Affine(
        np.array(e[3]).reshape((1, 1)), w[0],
        np.array(f[3]).reshape((1, )))

    # Layer 2 nonlinearity
    w[3] = amnet.Mu(x[3], y[3], z[3])

    return w[3]