示例#1
0
 def test_multiple_vector_valued(self):
     """
     For a function that returns a vector rather than a scalar, make
     sure Dofs.f(), Dofs.jac(), and Dofs.fd_jac() behave correctly.
     """
     for nparams1 in range(1, 5):
         for nvals1 in range(1, 5):
             nparams2 = np.random.randint(1, 6)
             nparams3 = np.random.randint(1, 6)
             nvals2 = np.random.randint(1, 6)
             nvals3 = np.random.randint(1, 6)
             o1 = Affine(nparams=nparams1, nvals=nvals1)
             o2 = Affine(nparams=nparams2, nvals=nvals2)
             o3 = Affine(nparams=nparams3, nvals=nvals3)
             dofs = Dofs([o1, o2, o3], diff_method="centered")
             dofs.set(
                 (np.random.rand(nparams1 + nparams2 + nparams3) - 0.5) * 4)
             f1 = np.matmul(o1.A, o1.x) + o1.B
             f2 = np.matmul(o2.A, o2.x) + o2.B
             f3 = np.matmul(o3.A, o3.x) + o3.B
             np.testing.assert_allclose(dofs.f(), np.concatenate((f1, f2, f3)), \
                                        rtol=1e-13, atol=1e-13)
             true_jac = np.zeros(
                 (nvals1 + nvals2 + nvals3, nparams1 + nparams2 + nparams3))
             true_jac[0:nvals1, 0:nparams1] = o1.A
             true_jac[nvals1:nvals1 + nvals2,
                      nparams1:nparams1 + nparams2] = o2.A
             true_jac[nvals1 + nvals2:nvals1 + nvals2 + nvals3, \
                      nparams1 + nparams2:nparams1 + nparams2 + nparams3] = o3.A
             np.testing.assert_allclose(dofs.jac(),
                                        true_jac,
                                        rtol=1e-13,
                                        atol=1e-13)
             np.testing.assert_allclose(dofs.fd_jac(), \
                                        true_jac, rtol=1e-7, atol=1e-7)
示例#2
0
    def test_with_dependents(self):
        """
        Test the case in which the original object depends on another object.
        """
        o1 = Adder(3)
        o2 = Adder(4)
        o1.set_dofs([10, 11, 12])
        o2.set_dofs([101, 102, 103, 104])
        o1.depends_on = ["o2"]
        o1.o2 = o2
        dofs = Dofs([o1.J])
        np.testing.assert_allclose(dofs.x, [10, 11, 12, 101, 102, 103, 104])
        self.assertEqual(dofs.all_owners, [o1, o2])
        self.assertEqual(dofs.dof_owners, [o1, o1, o1, o2, o2, o2, o2])
        np.testing.assert_allclose(dofs.indices, [0, 1, 2, 0, 1, 2, 3])
        f = dofs.f()  # f must be evaluated before we know nvals_per_func
        self.assertEqual(list(dofs.nvals_per_func), [1])
        self.assertEqual(dofs.nvals, 1)

        o1.fixed = [True, False, True]
        o2.fixed = [False, False, True, True]
        del o1.depends_on
        o2.depends_on = ["o1"]
        o2.o1 = o1
        dofs = Dofs([o2.J])
        np.testing.assert_allclose(dofs.x, [101, 102, 11])
        self.assertEqual(dofs.all_owners, [o2, o1])
        self.assertEqual(dofs.dof_owners, [o2, o2, o1])
        np.testing.assert_allclose(dofs.indices, [0, 1, 1])
示例#3
0
    def test_no_dependents(self):
        """
        Tests for an object that does not depend on other objects.
        """
        obj = Adder(4)
        obj.set_dofs([101, 102, 103, 104])
        dofs = Dofs([obj.J])
        np.testing.assert_allclose(dofs.x, [101, 102, 103, 104])
        self.assertEqual(dofs.all_owners, [obj])
        self.assertEqual(dofs.dof_owners, [obj, obj, obj, obj])
        np.testing.assert_allclose(dofs.indices, [0, 1, 2, 3])
        dummy = dofs.f()  # f must be evaluated before we know nvals_per_func
        self.assertEqual(list(dofs.nvals_per_func), [1])
        self.assertEqual(dofs.nvals, 1)

        obj.fixed = [True, False, True, False]
        dofs = Dofs([obj.J])
        np.testing.assert_allclose(dofs.x, [102, 104])
        self.assertEqual(dofs.all_owners, [obj])
        self.assertEqual(dofs.dof_owners, [obj, obj])
        np.testing.assert_allclose(dofs.indices, [1, 3])

        obj.fixed[0] = False
        dofs = Dofs([obj.J])
        np.testing.assert_allclose(dofs.x, [101, 102, 104])
        self.assertEqual(dofs.all_owners, [obj])
        self.assertEqual(dofs.dof_owners, [obj, obj, obj])
        np.testing.assert_allclose(dofs.indices, [0, 1, 3])
示例#4
0
    def test_failures(self):
        """
        Verify that if ObjectiveFailure is raised during function
        evaluations, a vector is returned filled with the expected
        number.
        """
        nvals = 3
        fail_val = 1.0e8
        o1 = Failer(nvals=nvals)
        d1 = Dofs([o1], fail=fail_val)
        # First eval should not fail:
        f = d1.f()
        np.testing.assert_allclose(f, np.full(nvals, 1.0))
        # There should be a failure on the 2nd evaluation:
        f = d1.f()
        np.testing.assert_allclose(f, np.full(nvals, fail_val))
        # Third eval should not fail:
        f = d1.f()
        np.testing.assert_allclose(f, np.full(nvals, 1.0))

        # Try an example with >1 object in the dofs, and with NaN
        # instead of a finite value for the failure value.
        fail_val = np.NAN
        o2 = Failer(nvals=3)
        r2 = Rosenbrock()
        d2 = Dofs([o2, r2.terms], fail=fail_val)
        # First eval should not fail:
        f = d2.f()
        np.testing.assert_allclose(f, [1., 1., 1., -1., 0.])
        # There should be a failure on the 2nd evaluation:
        f = d2.f()
        np.testing.assert_allclose(f, np.full(5, fail_val))
        # Third eval should not fail:
        f = d2.f()
        np.testing.assert_allclose(f, [1., 1., 1., -1., 0.])
示例#5
0
 def test_mixed_vector_valued(self):
     """
     For a mixture of functions that return a scalar vs return a
     vector, make sure Dofs.f(), Dofs.jac(), and Dofs.fd_jac()
     behave correctly.
     """
     for nparams1 in range(1, 5):
         for nvals1 in range(1, 5):
             nparams2 = np.random.randint(1, 6)
             nparams3 = np.random.randint(1, 6)
             nvals2 = np.random.randint(1, 6)
             nvals3 = np.random.randint(1, 6)
             o1 = Affine(nparams=nparams1, nvals=nvals1)
             o2 = Affine(nparams=nparams2, nvals=nvals2)
             o3 = Affine(nparams=nparams3, nvals=nvals3)
             a1 = Adder(n=2)
             a2 = Adder(n=3)
             dofs = Dofs([o1, o2, a1, o3, a2], diff_method="centered")
             dofs.set(
                 (np.random.rand(nparams1 + nparams2 + nparams3 + 5) - 0.5)
                 * 4)
             f1 = np.matmul(o1.A, o1.x) + o1.B
             f2 = np.matmul(o2.A, o2.x) + o2.B
             f3 = np.array([a1.f])
             f4 = np.matmul(o3.A, o3.x) + o3.B
             f5 = np.array([a2.f])
             np.testing.assert_allclose(dofs.f(), np.concatenate((f1, f2, f3, f4, f5)), \
                                        rtol=1e-13, atol=1e-13)
             true_jac = np.zeros((nvals1 + nvals2 + nvals3 + 2,
                                  nparams1 + nparams2 + nparams3 + 5))
             true_jac[0:nvals1, 0:nparams1] = o1.A
             true_jac[nvals1:nvals1 + nvals2,
                      nparams1:nparams1 + nparams2] = o2.A
             true_jac[nvals1 + nvals2:nvals1 + nvals2 + 1, \
                      nparams1 + nparams2:nparams1 + nparams2 + 2] = np.ones(2)
             true_jac[nvals1 + nvals2 + 1:nvals1 + nvals2 + 1 + nvals3, \
                      nparams1 + nparams2 + 2:nparams1 + nparams2 + 2 + nparams3] = o3.A
             true_jac[nvals1 + nvals2 + 1 + nvals3:nvals1 + nvals2 + nvals3 + 2, \
                      nparams1 + nparams2 + nparams3 + 2:nparams1 + nparams2 + nparams3 + 5] = np.ones(3)
             np.testing.assert_allclose(dofs.jac(),
                                        true_jac,
                                        rtol=1e-13,
                                        atol=1e-13)
             np.testing.assert_allclose(dofs.fd_jac(), \
                                        true_jac, rtol=1e-7, atol=1e-7)
示例#6
0
 def test_vector_valued(self):
     """
     For a function that returns a vector rather than a scalar, make
     sure Dofs.f(), Dofs.jac(), and Dofs.fd_jac() behave correctly.
     """
     for nparams in range(1, 5):
         for nvals in range(1, 5):
             o = Affine(nparams=nparams, nvals=nvals)
             o.set_dofs((np.random.rand(nparams) - 0.5) * 4)
             dofs = Dofs([o], diff_method="centered")
             np.testing.assert_allclose(dofs.f(), np.matmul(o.A, o.x) + o.B, \
                                        rtol=1e-13, atol=1e-13)
             np.testing.assert_allclose(dofs.jac(),
                                        o.A,
                                        rtol=1e-13,
                                        atol=1e-13)
             np.testing.assert_allclose(dofs.fd_jac(), \
                                        o.A, rtol=1e-7, atol=1e-7)