示例#1
0
    def test_ransom_seed(self):
        """Test setting random seeds.
        """
        np.random.seed(12345)
        value = helpers.choose_num(10)
        self.assertNotEqual(value, helpers.choose_num(10))

        np.random.seed(12345)
        self.assertEqual(value, helpers.choose_num(10))

        np.random.seed(12346)
        self.assertNotEqual(value, helpers.choose_num(10))
示例#2
0
    def _make_info(self):
        md = self.param['m']
        l = self.param['l']
        second_deg = self.param['second_deg']

        # Note that we only consider the following two cases of box constraints:
        # y(i) in [0, +inf) or  [0, u] where u is a nonnegative scalar.
        # Clearly, any other interval can be obtained by using the mapping
        # y <--- c1+c2*y.
        # It is assumed that the last m_inf constraints are of the form [0, inf)
        # The remaining m_inf - m_inf_deg - inf_nonactive constraints are where
        # F=0, y>0

        # y variables which are bounded below and above
        # There will be m - m_inf variables with double sided bounds. each upper
        # bound is chosen uniform in [0,10]
        md_upp_deg = choose_num(second_deg)
        # degenerate with y at upper bound: F=0, y=u
        md_low_deg = second_deg - md_upp_deg
        # degenerate with y at lower bound: F=0, y=0
        md_upp_nonactive = choose_num(md - md_upp_deg - md_low_deg)
        # F not active, y at upper bound: F<0, y=u
        md_low_nonactive = choose_num(
            md - md_upp_deg - md_low_deg - md_upp_nonactive)
        # F not active, y at lower bound: F>0, y=0

        l_deg = self.param['first_deg']
        l_nonactive = choose_num(l - l_deg)
        self.info.update({
            'ms': 0,
            'ms_deg': 0,
            'ms_nonactive': 0,
            'ms_active': 0,
            'md': md,
            'md_upp_deg': md_upp_deg,
            'md_low_deg': md_low_deg,
            'md_upp_nonactive': md_upp_nonactive,
            'md_low_nonactive': md_low_nonactive,
            'md_float': (
                md - md_upp_deg - md_low_deg - md_upp_nonactive - md_low_nonactive),
            # Randomly decide how many of the non-degenerate first level ctrs
            # should be nonactive
            'l': l,
            'l_deg': l_deg,
            'l_nonactive': l_nonactive,
            'l_active': l - l_deg - l_nonactive})
示例#3
0
    def test_greater_one(self):
        """ Test giving a number greater than 1.

        This test runs choose_num NUMBER_OF_TRIALS times.
        """
        m = 10
        for _ in xrange(NUMBER_OF_TRIALS):
            res = helpers.choose_num(m)
            self.assertGreaterEqual(res, 0)
            self.assertLessEqual(res, m)
示例#4
0
 def test_between_zero_and_one(self):
     """ Test giving a number between 0 and 1.
     """
     self.assertEqual(helpers.choose_num(0.5), 0)
示例#5
0
 def test_zero(self):
     """ Test giving 0.
     """
     self.assertEqual(helpers.choose_num(0), 0)
示例#6
0
    def _make_info(self):
        '''
        Randomly allots statuses to constraints (active, nonactive, degenerate).
        The 'l' series gives the allotment for upper level constraints Ax <= 0.
        The 'ms' series determines what happens with the single-bounded lower
        level variables.  The 'md' series determines what happens with the
        double-bounded lower level variables.

        Since the ordering of variables and constraints within these groups is
        not significant, the generator just determines how many will have a
        given status and then the first l_deg upper level constraints will be
        degenerate, the next l_non will be nonactive, etc.
        '''

        # Decide how many of the non-degenerate first level ctrs should be
        # nonactive
        l = self.param['l']
        l_deg = self.param['first_deg']
        l_nonactive = choose_num(l - l_deg)
        l_active = l - l_deg - l_nonactive
        self.info.update({
            'l': l,
            'l_deg': l_deg,
            'l_nonactive': l_nonactive,
            'l_active': l_active})

        m = self.param['m']
        second_deg = self.param['second_deg']

        md = 1 + choose_num(m - 2)
        ms = m - md

        # single bounded y variables (only bounded below):
        ms_deg = max(choose_num(min(ms, second_deg)),
                     second_deg - m + ms)
        ms_nonactive = choose_num(ms - ms_deg)
        ms_active = ms - ms_deg - ms_nonactive
        self.info.update({
            'm': m,
            'ms': ms,
            'ms_deg': ms_deg,  # F=0, y=0
            'ms_nonactive': ms_nonactive,  # F>0, y=0
            'ms_active': ms_active})  # F=0, y>0

        # divvy up degeneracy numbers so there are second_deg degenerate y vars

        remaining_degen = second_deg - self.info['ms_deg']
        md_upp_deg = choose_num(remaining_degen)  # F=0, y=u
        md_low_deg = remaining_degen - md_upp_deg  # F=0, y=0

        self.info.update({
            'md': md,
            'md_upp_deg': md_upp_deg,
            'md_low_deg': md_low_deg})

        # double bounded y variables (bounded below and above):
        md_nondegen = md - self.info['md_upp_deg'] - self.info['md_low_deg']
        md_upp_non = choose_num(md_nondegen)  # F<0, y=u
        md_low_non = choose_num(md_nondegen - md_upp_non)  # F>0, y=0
        md_float = md_nondegen - md_upp_non - md_low_non  # F=0, 0<y<u
        self.info.update({
            'md_upp_nonactive': md_upp_non,
            'md_low_nonactive': md_low_non,
            'md_float': md_float})

        info = self.info
        param = self.param
        assert self.m == info['ms'] + info['md']
        assert info['ms'] == info['ms_deg'] + \
            info['ms_active'] + info['ms_nonactive']
        assert info['md'] == info['md_upp_deg'] + info['md_upp_nonactive'] + \
            info['md_low_deg'] + info['md_low_nonactive'] + \
            info['md_float']
        assert param['second_deg'] == info['ms_deg'] + info['md_upp_deg'] + \
            info['md_low_deg']

        assert info['ms_deg'] >= 0
        assert info['ms_active'] >= 0
        assert info['ms_nonactive'] >= 0
        assert info['md_upp_deg'] >= 0
        assert info['md_upp_nonactive'] >= 0
        assert info['md_low_deg'] >= 0
        assert info['md_low_nonactive'] >= 0
        assert info['md_float'] >= 0
示例#7
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})