Esempio n. 1
0
    def test_quartic(self):
        print('Quartic')
        quartic = example_functions.Polynomial([1, 0, 0, 0, 1])
        optimiser = GradOptimiser.GradOptimiser()

        optimiser.add_variable('x', quartic.x, -2, 2)
        result = optimiser.optimise(quartic.cost)
        self.assertAlmostEqual(result['x'], 0)
Esempio n. 2
0
    def test_quartic(self):
        print('Quartic')
        quartic = example_functions.Polynomial([1, 0, 0, 0, 1])
        optimiser = SA.SA()

        optimiser.add_variable('x', quartic.x, -2, 2)
        result = optimiser.optimise(quartic.cost, max_iterations=1000)
        self.assertAlmostEqual(result['x'], 0, places=3)
Esempio n. 3
0
    def test_quartic(self):

        print('Quartic')
        # Test y = x^4 + 1, dy/dx = 4 x^3
        quadratic = example_functions.Polynomial([1, 0, 0, 0, 1])
        sensitivity = Sensitivity.Sensitivity(dx=self.dx)
        sensitivity.add_variable('x', quadratic.x)

        # x: 1
        quadratic.x[0] = 1
        gradient = sensitivity.sensitivity(quadratic.cost)
        self.assertAlmostEqual(gradient['x'], 4, places=3)

        # x: 2
        quadratic.x[0] = 2
        gradient = sensitivity.sensitivity(quadratic.cost)
        self.assertAlmostEqual(gradient['x'], 32, places=3)

        # x: -2
        quadratic.x[0] = -2
        gradient = sensitivity.sensitivity(quadratic.cost)
        self.assertAlmostEqual(gradient['x'], -32, places=3)