Exemplo n.º 1
0
 def test_shuffle_clauses(self):
     ref_sols = set(tuple(sol) for sol in itersolve(clauses1))
     for _ in range(10):
         cnf = copy.deepcopy(clauses1)
         # shuffling the clauses does not change the solutions
         random.shuffle(cnf)
         self.assertEqual(set(tuple(sol) for sol in itersolve(cnf)),
                          ref_sols)
Exemplo n.º 2
0
    def test_cnf1(self):
        for sol in itersolve(clauses1, nvars1):
            #sys.stderr.write('%r\n' % repr(sol))
            self.assertTrue(evaluate(clauses1, sol))

        sols = list(itersolve(clauses1, vars=nvars1))
        self.assertEqual(len(sols), 18)
        # ensure solutions are unique
        self.assertEqual(len(set(tuple(sol) for sol in sols)), 18)
Exemplo n.º 3
0
    def test_gen_clauses(self):
        def gen_clauses():
            for clause in clauses1:
                yield clause

        self.assertTrue(
            all(evaluate(clauses1, sol) for sol in itersolve(gen_clauses())))
Exemplo n.º 4
0
def process_cnf_file(path):
    sys.stdout.write('%30s:  ' % basename(path))
    sys.stdout.flush()

    clauses, n_vars = read_cnf(path)
    sys.stdout.write('vars: %6d   cls: %6d   ' % (n_vars, len(clauses)))
    sys.stdout.flush()
    n_sol = 0
    for sol in itersolve(clauses, n_vars):
        sys.stdout.write('.')
        sys.stdout.flush()
        assert evaluate(clauses, sol)
        n_sol += 1
    sys.stdout.write("%d\n" % n_sol)
    sys.stdout.flush()
    return n_sol
Exemplo n.º 5
0
 def test_cnf3_3vars(self):
     self.assertEqual(list(itersolve(clauses3, 3)),
                      [[-1, -2, -3], [-1, -2, 3]])
Exemplo n.º 6
0
 def test_cnf2(self):
     self.assertEqual(list(itersolve(clauses2, nvars2)), [])
Exemplo n.º 7
0
 def test_many_clauses(self):
     ref_sols = set(tuple(sol) for sol in itersolve(clauses1))
     # repeating the clauses many times does not change the solutions
     cnf = 100 * copy.deepcopy(clauses1)
     self.assertEqual(set(tuple(sol) for sol in itersolve(cnf)), ref_sols)
Exemplo n.º 8
0
 def test_each_clause_gen(self):
     self.assertTrue(
         all(
             evaluate(clauses1, sol)
             for sol in itersolve([(x for x in clause)
                                   for clause in clauses1])))
Exemplo n.º 9
0
 def test_each_clause_tuples(self):
     self.assertTrue(
         all(
             evaluate(clauses1, sol)
             for sol in itersolve([tuple(clause) for clause in clauses1])))
Exemplo n.º 10
0
 def test_tuple_caluses(self):
     self.assertTrue(
         all(evaluate(clauses1, sol) for sol in itersolve(tuple(clauses1))))
Exemplo n.º 11
0
 def test_iter_clauses(self):
     self.assertTrue(
         all(evaluate(clauses1, sol) for sol in itersolve(iter(clauses1))))
Exemplo n.º 12
0
 def test_no_clauses(self):
     for n in range(7):
         self.assertEqual(len(list(itersolve([], vars=n))), 2**n)
Exemplo n.º 13
0
def cnfNondictatorial():
	cnf = []
	for i in allVoters():
		clause = []
		for r in allProfiles():
			for x in alternatives(lambda x : top(i,x,r)):
				clause.append(negLiteral(r,x))
		cnf.append(clause)
	return cnf


cnf_ex = ( cnfAtLeastOne() + cnfResolute() + cnfSurjective() + cnfStrategyProof() + cnfNondictatorial() )


cnf_ex1 = ( cnfAtLeastOne() + cnfResolute() + cnfStrategyProof() )
l_ex1 = list( itersolve( cnf_ex1 ) )


cnf_test = ( cnfAtLeastOne() + cnfResolute() + cnfSurjective() + cnfStrategyProof() )
l_test = list( itersolve( cnf_test ) )