Ejemplo n.º 1
0
    def test_non_convex(self):
        """ Test for non convex cases.
        """
        for size in range(2, 101):
            cond = random.randint(1, 100)

            numpy.random.seed(cond)
            res = helpers.gen_general_obj(size, False, cond, 1)

            self.assertEqual(res.shape, (size, size))
            if size > 50:
                self.assertLess(helpers.mindiag(res), 0)
            self.assertAlmostEqual(numpy.linalg.cond(res), cond)

            numpy.random.seed(cond)
            half = helpers.gen_general_obj(size, False, cond, 0.5)
            self.assertTrue(np.allclose(res, half * 2))
Ejemplo n.º 2
0
    def test_gen_obj(self):
        # A very large matrix generated in a general way is statistically
        # very unlikely to be PSD by accident, so we want to observe that
        # the generator with convex option makes very large PSD matrices
        # and the generator with nonconvex option will nor 'accidentally'
        # make PSD matrices (though there is always some chance of a fluke)

        # Generate a convex Q for 200 variables with condition number 20
        Q = hp.gen_general_obj(200, True, 20, 20)
        self.assertAlmostEqual(np.linalg.cond(Q), 20)
        self.assertTrue(mh.isPSD(Q))
        np.testing.assert_almost_equal(Q, 0.5 * (Q + Q.T))

        # Generate a nonconvex Q for 200 variables with condition number 20
        Q = hp.gen_general_obj(200, False, 20, 20)
        self.assertAlmostEqual(np.linalg.cond(Q), 20)
        self.assertFalse(mh.isPSD(Q))
        np.testing.assert_almost_equal(Q, 0.5 * (Q + Q.T))