Exemplo n.º 1
0
    def test_ransom_seed(self):
        """Test setting random seeds.
        """
        np.random.seed(12345)
        value = helpers.rand(2, 2)
        self.assertFalse(np.allclose(value, helpers.rand(2, 2)))

        np.random.seed(12345)
        self.assertTrue(np.allclose(value, helpers.rand(2, 2)))

        np.random.seed(12346)
        self.assertFalse(np.allclose(value, helpers.rand(2, 2)))
Exemplo n.º 2
0
 def test_two(self):
     """ Test for 2 by 2 matrix.
     """
     m = helpers.eye(2)
     v = helpers.rand(2, 2)
     self.assertTrue(np.array_equal(m.dot(v), v))
     self.assertTrue(np.array_equal(v.dot(m), v))
Exemplo n.º 3
0
 def test_one(self):
     """ Test for 1 by 1 matrix.
     """
     m = helpers.eye(1)
     v = helpers.rand(1)
     self.assertTrue(np.array_equal(m.dot(v), v))
     self.assertTrue(np.array_equal(v.dot(m), v))
Exemplo n.º 4
0
    def _make_ygen(self):
        self._make_u()

        num_double_at_upper = self.info[
            'md_upp_deg'] + self.info['md_upp_nonactive']
        num_not_floating = self.info['md'] - self.info['md_float']

        double_at_upper = npvec(self.u[:num_double_at_upper])
        double_at_lower = zeros(
            self.info['md_low_deg'] + self.info['md_low_nonactive'])
        double_floating = npvec(
            [randcst() * x for x in self.u[num_not_floating:]])

        single_at_lower = zeros(
            self.info['ms_deg'] + self.info['ms_nonactive'])
        single_floating = rand(self.info['ms_active'])

# ygen = conmat([npvec(u[:m_upp_deg+upp_nonactive]),            # double_at_upper
#                     zeros(m_low_deg+low_nonactive),          # double_at_lower
#                     v2,                                      # double_floating
#                     zeros(m_inf_deg+inf_nonactive),          # single_at_lower
# rand(m_inf-m_inf_deg-inf_nonactive)])    # single_floating
        self.info['ygen'] = conmat([
            double_at_upper,  # y=u cases
            double_at_lower,  # y=0 cases
            double_floating,  # 0<y<u cases
            single_at_lower,  # y=0 cases
            single_floating])  # y>0 cases

        for yi in self.info['ygen']:
            assert yi >= 0
        for i in range(self.info['md']):
            assert self.info['ygen'][i] <= self.u[i]
Exemplo n.º 5
0
 def test_non_diagonal_matrix(self):
     """ Test for multiple of non diagonal matrix and raise errors.
     """
     for size in range(2, 101):
         PU = PV = np.identity(size)
         PD = helpers.rand(size)
         cond = 73
         self.assertRaises(ValueError, helpers.adjust_cond, PU, PD, PV, cond)
Exemplo n.º 6
0
    def evaluate(self, size):
        """ Evaluate with a give size matrix.

        Args:
          size: Matrix size.
        """
        m = helpers.rand(size, size)
        PU, PD, PV = helpers.schur(m)
        self.assertTrue(np.allclose(np.dot(PU, np.dot(PD, PV)), m))
Exemplo n.º 7
0
    def evaluate(self, size):
        """ Evaluate with a give size matrix.

        Args:
          size: Matrix size.
        """
        ms = [helpers.rand(size, size) for _ in range(3)]
        ans = functools.reduce(lambda u, v: u.dot(v), ms)
        self.assertTrue(np.allclose(helpers.reconstruct(*ms), ans))
Exemplo n.º 8
0
    def _make_a_ulambda(self):
        xgen = self.info['xgen']
        ygen = self.info['ygen']

        # FIRST LEVEL CTRS A[x;y] + a <= 0
        # Generate the first level multipliers  ulambda  associated with A*[x;y]+a<=0.
        # Generate a so the constraints Ax+a <= 0 are loose or tight where
        # appropriate.
        Axy = self.A * conmat([xgen, ygen])
        self.a = -Axy - conmat([
            zeros(self.info['l_deg']),  # A + a = 0
            rand(self.info['l_nonactive']),  # A + a = 0
            zeros(self.info['l_active'])])  # A + a <=0

        self.info['ulambda'] = conmat([
            zeros(self.info['l_deg']),
            zeros(self.info['l_nonactive']),
            rand(self.info['l_active'])])
Exemplo n.º 9
0
    def evaluate(self, shape, **kwargs):
        """ Evaluate rand.

        Args:
          shape: Shape of result matrix of randint.
          kwargs: To be passed to randint.
        """
        m = helpers.rand(**kwargs)
        self.assertEqual(m.shape, shape)
        self.assertGreaterEqual(m.min(), 0)
        self.assertLessEqual(m.max(), 1)
Exemplo n.º 10
0
    def test(self):
        """ Test with a 100 by 100 matrix.
        """
        size = 100
        m = helpers.rand(size, size)
        min_diagonal = m.diagonal().min()
        res = helpers.tweak_diag(m)

        for i in range(size):
            self.assertGreaterEqual(res[i, i], m[i, i] - min_diagonal)
            self.assertLessEqual(res[i, i], m[i, i] - min_diagonal + 1)
Exemplo n.º 11
0
    def evaluate(self, size):
        """ Evaluate with a give size matrix.

        Args:
          size: Matrix size.
        """
        m = helpers.rand(size, size)
        PU, PD, PV = helpers.svd(m)

        self.assertTrue(np.allclose(np.dot(PU, np.dot(PD, PV)), m))
        for i in range(size):
            for j in range(size):
                if i != j:
                    self.assertEqual(PD[i, j], 0)
Exemplo n.º 12
0
 def test(self):
     """ Test with a simple input.
     """
     m = helpers.rand(100, 100)
     ans = m.diagonal().max()
     self.assertEqual(helpers.maxdiag(m), ans)
Exemplo n.º 13
0
 def setUp(self):
     """ Prepare tests.
     """
     u = helpers.rand(3, 3)
     v = helpers.rand(3, 3)
     self.tup = (u, v)
Exemplo n.º 14
0
    def _make_F_pi_sigma_index(self):
        N = self.N
        M = self.M
        u = self.u
        xgen = self.info['xgen']
        ygen = self.info['ygen']

        m = self.param['m']

        # Design q so that Nx + My + E^Tlambda + q = 0 at the solution (xgen,
        # ygen)

        q = -N * xgen - M * ygen
        q += conmat([
            # double bounded, degenerate at upper
            zeros(self.info['md_upp_deg']),
            # double bounded, nonactive at upper
            -rand(self.info['md_upp_nonactive']),
            # double bounded, degenerate at lower
            zeros(self.info['md_low_deg']),
            # double bounded, nonactive at lower
            rand(self.info['md_low_nonactive']),
            zeros(self.info['md_float']),         # double bounded, floating
            # single bounded, degenerate at lower
            zeros(self.info['ms_deg']),
            # single bounded, nonactive at lower
            rand(self.info['ms_nonactive']),
            zeros(self.info['ms_active'])])       # single bounded, floating

        #########################################
        ##        For later convenience        ##
        #########################################
        F = N * xgen + M * ygen + q

        mix_deg = self.param['mix_deg']
        tol_deg = self.param['tol_deg']

        # Calculate three index sets alpha, beta and gamma at (xgen, ygen).
        # alpha denotes the index set of i at which F(i) is active, but y(i) not.
        # beta_upp and beta_low denote the index sets of i at which F(i) is
        # active, and y(i) is active at the upper and the lower end point of
        # the finite interval [0, u] respectively.
        # beta_inf denotes the index set of i at which both F(i) and y(i) are
        # active for the infinite interval [0, inf).
        # gamma_upp and gamma_low denote the index sets of i at which F(i) is
        # not active, but y(i) is active at the upper and the lower point of
        # the finite interval [0, u] respectively.
        # gamma_inf denotes the index set of i at which F(i) is not active, but y(i)
        # is active for the infinite interval [0, inf).
        index = []
        md = self.info['md']

        for i in range(md):
            assert ygen[i] >= -tol_deg and ygen[i] < u[i] + tol_deg, \
                "{0} not in [0, {1}]".format(ygen[i], u[i])
            if abs(F[i]) <= tol_deg and ygen[i] > tol_deg and ygen[i] + tol_deg < u[i]:
                index.append(1)  # For the index set alpha.
            elif abs(F[i]) <= tol_deg and abs(ygen[i] - u[i]) <= tol_deg:
                index.append(2)  # For the index set beta_upp.
            elif abs(F[i]) <= tol_deg and abs(ygen[i]) <= tol_deg:
                index.append(3)  # For the index set beta_low.
            elif F[i] < -tol_deg and abs(ygen[i] - u[i]) <= tol_deg:
                index.append(-1)  # For the index set gamma_upp.
            elif F[i] > tol_deg and abs(ygen[i]) <= tol_deg:
                index.append(-1)  # For the index set gamma_low.
            else:
                raise Exception(("didn't know what to do with this case: "
                                 "ygen={0}, u[i] = {1}, F[i]={2}").format(
                                     ygen[i], u[i], F[i]))

        for i in range(md, m):
            if ygen[i] > F[i] + tol_deg:
                index.append(1)  # For the index set alpha.
            elif abs(ygen[i] - F[i]) <= tol_deg:
                index.append(4)  # For the index set beta_inf.
            else:
                index.append(-1)  # For the index set gamma_inf.

        # Generate the first level multipliers   pi    sigma
        # associated with other constraints other than the first level constraints
        # A*[x;y]+a<=0   in the relaxed nonlinear program. In particular,
        # pi            is associated with  F(x, y)=N*x+M*y+q, and
        # sigma                       with  y.
        mix_upp_deg = max(
            mix_deg - self.info['md_low_deg'] - self.info['ms_deg'],
            choose_num(self.info['md_upp_deg']))
        mix_upp_deg = min(mix_upp_deg, mix_deg)

        mix_low_deg = max(
            mix_deg - mix_upp_deg - self.info['ms_deg'],
            choose_num(self.info['md_low_deg']))
        mix_low_deg = min(mix_low_deg, mix_deg - mix_upp_deg)

        mix_inf_deg = mix_deg - mix_upp_deg - mix_low_deg
        mix_inf_deg = min(mix_inf_deg, mix_deg - mix_upp_deg - mix_inf_deg)

        assert mix_deg >= 0
        assert mix_upp_deg >= 0
        assert mix_low_deg >= 0
        assert mix_inf_deg >= 0

#        assert self.param['second_deg'] == self.info['ms_deg'] +
#               self.info['md_low_deg'] + self.info['md_upp_deg'] + mix_deg
        k_mix_inf = 0
        k_mix_upp = 0
        k_mix_low = 0
        pi = zeros(m, 1)
        sigma = zeros(m, 1)
        for i in range(m):
            if index[i] == 1:
                pi[i] = randcst() - randcst()
                sigma[i] = 0
            elif index[i] == 2:
                if k_mix_upp < mix_upp_deg:
                    pi[i] = 0
                    # The first mix_upp_deg constraints associated with F(i)<=0
                    # in the set beta_upp are degenerate.
                    sigma[i] = 0
                    # The first mix_upp_deg constraints associated with
                    # y(i)<=u(i) in the set beta_upp are degenerate.
                    k_mix_upp = k_mix_upp + 1
                else:
                    pi[i] = randcst()
                    sigma[i] = randcst()
            elif index[i] == 3:
                if k_mix_low < mix_low_deg:
                    pi[i] = 0
                    # The first mix_low_deg constraints associated with F(i)>=0
                    # in the set beta_low are degenerate.
                    sigma[i] = 0
                    # The first mix_low_deg constraints associated with
                    # y(i)>=0 in the set beta_low are degenerate.
                    k_mix_low = k_mix_low + 1
                else:
                    pi[i] = -randcst()
                    sigma[i] = -randcst()
            elif index[i] == 4:
                if k_mix_inf < mix_inf_deg:
                    pi[i] = 0
                    # The first mix_inf_deg constraints associated with F(i)>=0
                    # in the set beta_inf are degenerate.
                    sigma[i] = 0
                    # The first mix_inf_deg constraints associated with
                    # y(i)>=0 in the set beta_inf are degenerate.
                    k_mix_inf = k_mix_inf + 1
                else:
                    pi[i] = -randcst()
                    sigma[i] = -randcst()
            else:
                pi[i] = 0
                sigma[i] = randcst() - randcst()

        self.q = q
        self.info.update({
            'F': F,
            'mix_upp_deg': mix_upp_deg,
            'mix_low_deg': mix_low_deg,
            'mix_inf_deg': mix_inf_deg,
            'pi': pi,
            'sigma': sigma})
Exemplo n.º 15
0
 def _make_u(self):
     self.u = 10. * rand(self.info['md'])
Exemplo n.º 16
0
 def _make_xgen(self):
     self.info['xgen'] = 10 * (rand(self.n) - rand(self.n))