Пример #1
0
    def test_supply_property(self):
        """
        Test that we can supply a property instead of a function.
        """
        iden = Identity()
        lst = LeastSquaresTerm.from_sigma(Target(iden, 'f'), 3, sigma=0.1)
        self.assertEqual(lst.goal, 3)
        self.assertAlmostEqual(lst.weight, 100, places=13)

        iden.set_dofs([17])
        self.assertEqual(lst.f_in(), 17)
        correct_value = ((17 - 3) / 0.1) ** 2
        self.assertAlmostEqual(lst.f_out(), correct_value, places=11)
Пример #2
0
    def test_supply_attribute(self):
        """
        Test that we can supply an attribute instead of a function.
        """
        iden = Identity()
        lst = LeastSquaresTerm(Target(iden, 'x'), 3, weight=0.1)
        self.assertEqual(lst.goal, 3)
        self.assertAlmostEqual(lst.weight, 0.1, places=13)

        iden.set_dofs([17])
        self.assertEqual(lst.f_in(), 17)
        correct_value = 0.1 * ((17 - 3) ** 2)
        self.assertAlmostEqual(lst.f_out(), correct_value, places=13)
Пример #3
0
    def test_supply_object(self):
        """
        Test that we can supply an object with a J function instead of a function.
        """
        iden = Identity()
        # Note here we supply iden instead of iden.J
        lst = LeastSquaresTerm.from_sigma(iden, 3, sigma=0.1)
        self.assertEqual(lst.f_in, iden.J)
        self.assertEqual(lst.goal, 3)
        self.assertAlmostEqual(lst.weight, 100, places=13)

        iden.set_dofs([17])
        self.assertEqual(lst.f_in(), 17)
        correct_value = ((17 - 3) / 0.1) ** 2
        self.assertAlmostEqual(lst.f_out(), correct_value, places=11)
Пример #4
0
 def test_gradient(self):
     iden = Identity()
     for n in range(1, 10):
         iden.set_dofs([np.random.rand() * 4 - 2])
         # Supply an object to finite_difference():
         fd_grad = Dofs([iden]).fd_jac().flatten()
         np.testing.assert_allclose(fd_grad, iden.df)
         np.testing.assert_allclose(fd_grad, iden.dJ())
         # Supply a function to finite_difference():
         fd_grad = Dofs([iden.J]).fd_jac().flatten()
         np.testing.assert_allclose(fd_grad, iden.df)
         np.testing.assert_allclose(fd_grad, iden.dJ())
         # Supply an attribute to finite_difference():
         fd_grad = Dofs([Target(iden, "f")]).fd_jac().flatten()
         np.testing.assert_allclose(fd_grad, iden.df)
         np.testing.assert_allclose(fd_grad, iden.dJ())
Пример #5
0
    def test_basic(self):
        iden = Identity()
        self.assertAlmostEqual(iden.J(), 0, places=13)
        np.testing.assert_allclose(iden.get_dofs(), np.array([0.0]))
        np.testing.assert_allclose(iden.fixed, np.array([False]))

        x = 3.5
        iden = Identity(x)
        self.assertAlmostEqual(iden.J(), x, places=13)
        np.testing.assert_allclose(iden.get_dofs(), np.array([x]))
        np.testing.assert_allclose(iden.fixed, np.array([False]))

        y = -2
        iden.set_dofs([y])
        self.assertAlmostEqual(iden.J(), y, places=13)
        np.testing.assert_allclose(iden.get_dofs(), np.array([y]))
        np.testing.assert_allclose(iden.fixed, np.array([False]))
Пример #6
0
    def test_basic(self):
        """
        Test basic usage
        """
        iden = Identity()
        lst = LeastSquaresTerm(iden.J, 3, weight=0.1)
        self.assertEqual(lst.f_in, iden.J)
        self.assertEqual(lst.goal, 3)
        self.assertAlmostEqual(lst.weight, 0.1, places=13)

        lst = LeastSquaresTerm.from_sigma(iden.J, 3, sigma=0.1)
        self.assertEqual(lst.f_in, iden.J)
        self.assertEqual(lst.goal, 3)
        self.assertAlmostEqual(lst.weight, 100.0, places=13)

        iden.set_dofs([17])
        self.assertEqual(lst.f_in(), 17)
        correct_value = ((17 - 3) / 0.1) ** 2
        self.assertAlmostEqual(lst.f_out(), correct_value, places=11)
Пример #7
0
    def test_supply_tuples(self):
        """
        Test basic usage
        """
        # Objective function f(x) = ((x - 3) / 2) ** 2
        iden1 = Identity()
        term1 = (iden1.J, 3, 0.25)
        prob = LeastSquaresProblem([term1])
        self.assertAlmostEqual(prob.objective(), 2.25)
        self.assertAlmostEqual(prob.objective(), sum(t.f_out() for t in prob.terms))
        self.assertEqual(len(prob.dofs.f()), 1)
        self.assertAlmostEqual(prob.dofs.f()[0], 0)
        self.assertEqual(len(prob.f()), 1)
        self.assertAlmostEqual(prob.f()[0], -1.5)
        self.assertAlmostEqual(prob.objective_from_shifted_f(prob.f()), 2.25)
        self.assertAlmostEqual(prob.objective_from_unshifted_f(prob.dofs.f()), 2.25)
        iden1.set_dofs([10])
        self.assertAlmostEqual(prob.objective(), 12.25)
        self.assertAlmostEqual(prob.objective(), sum(t.f_out() for t in prob.terms))
        self.assertAlmostEqual(prob.objective_from_shifted_f(prob.f()), 12.25)
        self.assertAlmostEqual(prob.objective_from_unshifted_f(prob.dofs.f()), 12.25)
        self.assertAlmostEqual(prob.objective([0]), 2.25)
        self.assertAlmostEqual(prob.objective([10]), 12.25)
        self.assertEqual(prob.dofs.all_owners, [iden1])
        self.assertEqual(prob.dofs.dof_owners, [iden1])

        # Objective function
        # f(x,y) = ((x - 3) / 2) ** 2 + ((y + 4) / 5) ** 2
        iden2 = Identity()
        term2 = (iden2.J, -4, 0.04)
        prob = LeastSquaresProblem([term1, term2])
        self.assertAlmostEqual(prob.objective(), 12.89)
        self.assertAlmostEqual(prob.objective(), sum(t.f_out() for t in prob.terms))
        self.assertEqual(len(prob.f()), 2)
        self.assertAlmostEqual(prob.objective_from_shifted_f(prob.f()), 12.89)
        self.assertAlmostEqual(prob.objective_from_unshifted_f(prob.dofs.f()), 12.89)
        iden1.set_dofs([5])
        iden2.set_dofs([-7])
        self.assertAlmostEqual(prob.objective(), 1.36)
        self.assertAlmostEqual(prob.objective(), sum(t.f_out() for t in prob.terms))
        self.assertEqual(len(prob.f()), 2)
        self.assertAlmostEqual(prob.objective_from_shifted_f(prob.f()), 1.36)
        self.assertAlmostEqual(prob.objective_from_unshifted_f(prob.dofs.f()), 1.36)
        self.assertAlmostEqual(prob.objective([10, 0]), 12.89)
        self.assertAlmostEqual(prob.objective([5, -7]), 1.36)
        self.assertEqual(prob.dofs.dof_owners, [iden1, iden2])
        self.assertEqual(prob.dofs.all_owners, [iden1, iden2])