コード例 #1
0
ファイル: test_cnfshuffle.py プロジェクト: prwang/cnfgen
    def test_inverse(self) :
        cnf = self.random_cnf(4,10,100)

        # Generate random variable, clause and polarity permutations.
        variable_permutation = list(range(10))
        random.shuffle(variable_permutation)
        clause_permutation = list(range(100))
        random.shuffle(clause_permutation)
        polarity_flip = [random.choice([-1,1]) for x in range(10)]

        # variable_permutation has the permutation already applied.
        old_variables = list(cnf.variables())
        new_variables = [old_variables[sigma_i] for sigma_i in variable_permutation]

        # Shuffle
        shuffle = cnfshuffle.Shuffle(cnf, new_variables, clause_permutation, polarity_flip)
        
        # The inverse variable permutation is the original list, do nothing.
        # The inverse clause permutation is the group inverse permutation.
        i_clause_permutation = self.inverse_permutation(clause_permutation)
        # The polarity flip applies to new variables. So if we flipped
        # the i-th variable now we want to flip the sigma(i)-th
        # variable.
        i_polarity_flip = [polarity_flip[i] for i in variable_permutation]

        # Inverse shuffle.
        cnf2 = cnfshuffle.Shuffle(shuffle, old_variables, i_clause_permutation, i_polarity_flip)
        self.assertCnfEqual(cnf2,cnf)
コード例 #2
0
 def test_deterministic(self):
     cnf = self.random_cnf(4, 10, 100)
     random.seed(42)
     shuffle = cnfshuffle.Shuffle(cnf)
     random.seed(42)
     shuffle2 = cnfshuffle.Shuffle(cnf)
     self.assertCnfEqual(shuffle2, shuffle)
コード例 #3
0
ファイル: test_cnfshuffle.py プロジェクト: prwang/cnfgen
 def test_identity(self) :
     cnf = self.random_cnf(4,10,100)
     variable_permutation = list(range(1,11))
     clause_permutation = list(range(100))
     polarity_flip = [1]*10
     shuffle = cnfshuffle.Shuffle(cnf, variable_permutation, clause_permutation, polarity_flip)
     self.assertCnfEqual(cnf,shuffle)
コード例 #4
0
ファイル: test_cnfshuffle.py プロジェクト: prwang/cnfgen
 def test_variable_and_polarity_flip(self) :
     cnf = cnfformula.CNF([[(True,'x'),(True,'y'),(False,'z')]])
     variable_permutation = ['y','z','x']
     clause_permutation = list(range(1))
     polarity_flip = [1,-1,-1]
     shuffle = cnfshuffle.Shuffle(cnf, variable_permutation, clause_permutation, polarity_flip)
     expected = cnfformula.CNF([[(False,'y'),(True,'z'),(True,'x')]])
     self.assertCnfEqual(expected,shuffle)
コード例 #5
0
ファイル: test_cnfshuffle.py プロジェクト: prwang/cnfgen
 def test_variable_permutation(self) :
     cnf = cnfformula.CNF([[(True,'x'),(True,'y'),(False,'z')]])
     variable_permutation = ['y','z','x']
     clause_permutation = list(range(1))
     polarity_flip = [1]*3
     shuffle = cnfshuffle.Shuffle(cnf, variable_permutation, clause_permutation, polarity_flip)
     self.assertSequenceEqual(list(shuffle.variables()),variable_permutation)
     self.assertSequenceEqual(list(shuffle.clauses()),list(cnf.clauses()))
コード例 #6
0
ファイル: test_cnfshuffle.py プロジェクト: prwang/cnfgen
 def test_backwards_compatible(self) :
     cnf = self.random_cnf(4,10,100)
     random.seed(44)
     shuffle = cnfshuffle.Shuffle(cnf)
     reference_output = shuffle.dimacs()+"\n"
     input = io.StringIO(cnf.dimacs())
     dimacs_shuffle = io.StringIO()
     random.seed(44)
     shufflereference.stableshuffle(input, dimacs_shuffle)
     self.assertMultiLineEqual(dimacs_shuffle.getvalue(), reference_output)
コード例 #7
0
 def test_identity(self):
     cnf = self.random_cnf(4, 10, 100)
     variables_permutation = list(range(1, 11))
     constraints_permutation = list(range(100))
     polarity_flips = [1] * 10
     shuffle = cnfshuffle.Shuffle(
         cnf,
         variables_permutation=variables_permutation,
         constraints_permutation=constraints_permutation,
         polarity_flips=polarity_flips)
     self.assertCnfEqual(cnf, shuffle)
コード例 #8
0
ファイル: test_cnfshuffle.py プロジェクト: prwang/cnfgen
 def equivalence_check_helper(self, cnf, variable_permutation, clause_permutation, polarity_flip) :
     variables = list(cnf.variables())
     massimos_fancy_input = [variables[p] for p in variable_permutation]
     random.seed(43)
     shuffle = cnfshuffle.Shuffle(cnf, massimos_fancy_input, clause_permutation, polarity_flip)
     reference_output = shuffle.dimacs()+"\n"
     input = io.StringIO(cnf.dimacs())
     dimacs_shuffle = io.StringIO()
     random.seed(43)
     shufflereference.stableshuffle(input, dimacs_shuffle, massimos_fancy_input, clause_permutation, polarity_flip)
     self.assertMultiLineEqual(dimacs_shuffle.getvalue(), reference_output)
コード例 #9
0
 def test_polarity_flip(self):
     cnf = cnfformula.CNF([[(True, 'x'), (True, 'y'), (False, 'z')]])
     variables_permutation = list(cnf.variables())
     constraints_permutation = list(range(1))
     polarity_flips = [1, -1, -1]
     shuffle = cnfshuffle.Shuffle(
         cnf,
         variables_permutation=variables_permutation,
         constraints_permutation=constraints_permutation,
         polarity_flips=polarity_flips)
     expected = cnfformula.CNF([[(True, 'x'), (False, 'y'), (True, 'z')]])
     self.assertCnfEqual(expected, shuffle)
コード例 #10
0
 def test_backwards_compatible(self):
     cnf = self.random_cnf(4, 10, 5)
     random.seed(44)
     shuffle = cnfshuffle.Shuffle(cnf,
                                  polarity_flips='random',
                                  variables_permutation='random',
                                  constraints_permutation='random')
     reference_output = shuffle.dimacs() + "\n"
     input = io.StringIO(cnf.dimacs())
     dimacs_shuffle = io.StringIO()
     random.seed(44)
     shufflereference.stableshuffle(input, dimacs_shuffle)
     self.assertMultiLineEqual(dimacs_shuffle.getvalue(), reference_output)
コード例 #11
0
 def test_cmdline_reshuffler(self):
     cnf = self.random_cnf(4, 10, 100)
     random.seed('45')
     shuffle = cnfshuffle.Shuffle(cnf)
     reference_output = shuffle.dimacs()
     input = io.StringIO(cnf.dimacs())
     dimacs_shuffle = io.StringIO()
     argv = ['cnfshuffle', '--input', '-', '--output', '-', '--seed', '45']
     try:
         import sys
         sys.stdin = input
         sys.stdout = dimacs_shuffle
         cnfshuffle.command_line_utility(argv)
     except Exception as e:
         self.fail(e)
     finally:
         sys.stdin = sys.__stdin__
         sys.stdout = sys.__stdout__
     self.assertMultiLineEqual(dimacs_shuffle.getvalue(), reference_output)