Beispiel #1
0
    def test_Maps(self):
        nP = 10
        m = np.random.rand(2 * nP)

        wires = maps.Wires(("sigma", nP), ("mu", nP))

        objfct1 = objective_function.L2ObjectiveFunction(mapping=wires.sigma)
        objfct2 = objective_function.L2ObjectiveFunction(mapping=wires.mu)

        objfct3 = objfct1 + objfct2

        objfct4 = objective_function.L2ObjectiveFunction(nP=nP)

        self.assertTrue(objfct1.nP == 2 * nP)
        self.assertTrue(objfct2.nP == 2 * nP)
        self.assertTrue(objfct3.nP == 2 * nP)

        # print(objfct1.nP, objfct4.nP, objfct4.W.shape, objfct1.W.shape, m[:nP].shape)
        self.assertTrue(objfct1(m) == objfct4(m[:nP]))
        self.assertTrue(objfct2(m) == objfct4(m[nP:]))

        self.assertTrue(objfct3(m) == objfct1(m) + objfct2(m))

        objfct1.test()
        objfct2.test()
        objfct3.test()
Beispiel #2
0
    def test_ComboW(self):
        nP = 15
        m = np.random.rand(nP)

        phi1 = objective_function.L2ObjectiveFunction(nP=nP)
        phi2 = objective_function.L2ObjectiveFunction(nP=nP)

        alpha1 = 2.0
        alpha2 = 0.5

        phi = alpha1 * phi1 + alpha2 * phi2

        r = phi.W * m

        r1 = phi1.W * m
        r2 = phi2.W * m

        print(phi(m), 0.5 * np.inner(r, r))

        self.assertTrue(np.allclose(phi(m), 0.5 * np.inner(r, r)))
        self.assertTrue(
            np.allclose(
                phi(m), 0.5 * (alpha1 * np.inner(r1, r1) + alpha2 * np.inner(r2, r2))
            )
        )
Beispiel #3
0
    def test_updateMultipliers(self):
        nP = 10

        m = np.random.rand(nP)

        W1 = utils.sdiag(np.random.rand(nP))
        W2 = utils.sdiag(np.random.rand(nP))

        phi1 = objective_function.L2ObjectiveFunction(W=W1)
        phi2 = objective_function.L2ObjectiveFunction(W=W2)

        phi = phi1 + phi2

        self.assertTrue(phi(m) == phi1(m) + phi2(m))

        phi.multipliers[0] = utils.Zero()
        self.assertTrue(phi(m) == phi2(m))

        phi.multipliers[0] = 1.0
        phi.multipliers[1] = utils.Zero()

        self.assertTrue(len(phi.objfcts) == 2)
        self.assertTrue(len(phi.multipliers) == 2)
        self.assertTrue(len(phi) == 2)

        self.assertTrue(phi(m) == phi1(m))
Beispiel #4
0
    def test_3sum(self):
        nP = 90

        alpha1 = 0.3
        alpha2 = 0.6
        alpha3inv = 9

        phi1 = objective_function.L2ObjectiveFunction(W=sp.eye(nP))
        phi2 = objective_function.L2ObjectiveFunction(W=sp.eye(nP))
        phi3 = objective_function.L2ObjectiveFunction(W=sp.eye(nP))

        phi = alpha1 * phi1 + alpha2 * phi2 + phi3 / alpha3inv

        m = np.random.rand(nP)

        self.assertTrue(
            np.all(phi.multipliers == np.r_[alpha1, alpha2, 1.0 / alpha3inv])
        )

        self.assertTrue(
            np.allclose(
                (alpha1 * phi1(m) + alpha2 * phi2(m) + phi3(m) / alpha3inv), phi(m)
            )
        )

        self.assertTrue(len(phi.objfcts) == 3)

        self.assertTrue(phi.test())
Beispiel #5
0
    def test_sum(self):
        scalar = 10.0
        nP = 100.0
        objfct = objective_function.L2ObjectiveFunction(
            W=sp.eye(nP)
        ) + scalar * objective_function.L2ObjectiveFunction(W=sp.eye(nP))
        self.assertTrue(objfct.test(eps=1e-9))

        self.assertTrue(np.all(objfct.multipliers == np.r_[1.0, scalar]))
Beispiel #6
0
 def test_ZeroObjFct(self):
     # This is not a combo objective function, it will just give back an
     # L2 objective function. That might be ok? or should this be a combo
     # objective function?
     nP = 20
     alpha = 2.0
     phi = alpha * (
         objective_function.L2ObjectiveFunction(W=sp.eye(nP))
         + utils.Zero() * objective_function.L2ObjectiveFunction()
     )
     self.assertTrue(len(phi.objfcts) == 1)
     self.assertTrue(phi.test())
Beispiel #7
0
    def test_ComboConstruction(self):
        nP = 10
        m = np.random.rand(nP)
        v = np.random.rand(nP)

        phi1 = objective_function.L2ObjectiveFunction(nP=nP)
        phi2 = objective_function.L2ObjectiveFunction(nP=nP)

        phi3 = 2 * phi1 + 3 * phi2

        phi4 = objective_function.ComboObjectiveFunction([phi1, phi2], [2, 3])

        self.assertTrue(phi3(m) == phi4(m))
        self.assertTrue(np.all(phi3.deriv(m) == phi4.deriv(m)))
        self.assertTrue(np.all(phi3.deriv2(m, v) == phi4.deriv2(m, v)))
Beispiel #8
0
 def test_deriv2(self):
     nP = 100
     mapping = maps.ExpMap(nP=nP)
     m = np.random.rand(nP)
     v = np.random.rand(nP)
     objfct = objective_function.L2ObjectiveFunction(nP=nP, mapping=mapping)
     self.assertTrue(np.allclose(objfct.deriv2(m=m, v=v), objfct.deriv2(m=m) * v))
Beispiel #9
0
    def test_early_exits(self):
        nP = 10

        m = np.random.rand(nP)
        v = np.random.rand(nP)

        W1 = utils.sdiag(np.random.rand(nP))
        phi1 = objective_function.L2ObjectiveFunction(W=W1)

        phi2 = Error_if_Hit_ObjFct()

        objfct = phi1 + 0 * phi2

        self.assertTrue(len(objfct) == 2)
        self.assertTrue(np.all(objfct.multipliers == np.r_[1, 0]))
        self.assertTrue(objfct(m) == phi1(m))
        self.assertTrue(np.all(objfct.deriv(m) == phi1.deriv(m)))
        self.assertTrue(np.all(objfct.deriv2(m, v) == phi1.deriv2(m, v)))

        objfct.multipliers[1] = utils.Zero()

        self.assertTrue(len(objfct) == 2)
        self.assertTrue(np.all(objfct.multipliers == np.r_[1, 0]))
        self.assertTrue(objfct(m) == phi1(m))
        self.assertTrue(np.all(objfct.deriv(m) == phi1.deriv(m)))
        self.assertTrue(np.all(objfct.deriv2(m, v) == phi1.deriv2(m, v)))
Beispiel #10
0
    def test_sum_fail(self):
        nP1 = 10
        nP2 = 30

        phi1 = objective_function.L2ObjectiveFunction(
            W=utils.sdiag(np.random.rand(nP1))
        )

        phi2 = objective_function.L2ObjectiveFunction(
            W=utils.sdiag(np.random.rand(nP2))
        )

        with self.assertRaises(Exception):
            phi = phi1 + phi2

        with self.assertRaises(Exception):
            phi = phi1 + 100 * phi2
Beispiel #11
0
    def test_2sum(self):
        nP = 80
        alpha1 = 100
        alpha2 = 200

        phi1 = (
            objective_function.L2ObjectiveFunction(W=utils.sdiag(np.random.rand(nP)))
            + alpha1 * objective_function.L2ObjectiveFunction()
        )
        phi2 = objective_function.L2ObjectiveFunction() + alpha2 * phi1
        self.assertTrue(phi2.test(eps=EPS))

        self.assertTrue(len(phi1.multipliers) == 2)
        self.assertTrue(len(phi2.multipliers) == 2)

        self.assertTrue(len(phi1.objfcts) == 2)
        self.assertTrue(len(phi2.objfcts) == 2)
        self.assertTrue(len(phi2) == 2)

        self.assertTrue(len(phi1) == 2)
        self.assertTrue(len(phi2) == 2)

        self.assertTrue(np.all(phi1.multipliers == np.r_[1.0, alpha1]))
        self.assertTrue(np.all(phi2.multipliers == np.r_[1.0, alpha2]))
Beispiel #12
0
    def test_updating_multipliers(self):
        nP = 20

        phi1 = objective_function.L2ObjectiveFunction(nP=nP)
        phi2 = objective_function.L2ObjectiveFunction(nP=nP)

        phi3 = 2 * phi1 + 4 * phi2

        self.assertTrue(all(phi3.multipliers == np.r_[2, 4]))

        phi3.multipliers[1] = 3
        self.assertTrue(all(phi3.multipliers == np.r_[2, 3]))

        phi3.multipliers = np.r_[1.0, 5.0]
        self.assertTrue(all(phi3.multipliers == np.r_[1.0, 5.0]))

        with self.assertRaises(Exception):
            phi3.multipliers[0] = "a"

        with self.assertRaises(Exception):
            phi3.multipliers = np.r_[0.0, 3.0, 4.0]

        with self.assertRaises(Exception):
            phi3.multipliers = ["a", "b"]
Beispiel #13
0
    def test_scalarmul(self):
        scalar = 10.0
        nP = 100
        objfct_a = objective_function.L2ObjectiveFunction(
            W=utils.sdiag(np.random.randn(nP))
        )
        objfct_b = scalar * objfct_a
        m = np.random.rand(nP)

        objfct_c = objfct_a + objfct_b

        self.assertTrue(scalar * objfct_a(m) == objfct_b(m))
        self.assertTrue(objfct_b.test())
        self.assertTrue(objfct_c(m) == objfct_a(m) + objfct_b(m))

        self.assertTrue(len(objfct_c.objfcts) == 2)
        self.assertTrue(len(objfct_c.multipliers) == 2)
        self.assertTrue(len(objfct_c) == 2)
Beispiel #14
0
    def test_mappings_and_cell_weights(self):
        mesh = discretize.TensorMesh([8, 7, 6])
        m = np.random.rand(2 * mesh.nC)
        v = np.random.rand(2 * mesh.nC)

        cell_weights = np.random.rand(mesh.nC)

        wires = maps.Wires(("sigma", mesh.nC), ("mu", mesh.nC))

        reg = regularization.SimpleSmall(mesh,
                                         mapping=wires.sigma,
                                         cell_weights=cell_weights)

        objfct = objective_function.L2ObjectiveFunction(W=utils.sdiag(
            np.sqrt(cell_weights)),
                                                        mapping=wires.sigma)

        self.assertTrue(reg(m) == objfct(m))
        self.assertTrue(np.all(reg.deriv(m) == objfct.deriv(m)))
        self.assertTrue(np.all(reg.deriv2(m, v=v) == objfct.deriv2(m, v=v)))
Beispiel #15
0
 def test_derivs(self):
     objfct = objective_function.L2ObjectiveFunction()
     self.assertTrue(objfct.test(eps=1e-9))