def broad_test(axiom_list, outputfilepath="testresults.txt"): """ Tests every combination of axioms in a cnf-like format (which seemed appropriate). Input format as follows: axiom_list is an iterable (generally a list) of lists, in which every element is a function which returns the cnf for an axiom. Every sublist indicates that one of the axioms within it must be a part of every trial. Thus, a singleton is a necessary part (which is for example generally appropriate for atLeastOne). Duplicate axioms within a trial as well as duplicate trials are removed efficiently, so the encoding need not prevent duplicates. """ # get every single axiom used (without duplicates) all_used_axioms = set() for sublist in axiom_list: for ax in sublist: all_used_axioms.add(ax) # map every axiom to an integer ax2int = {} int2ax = {} for i, ax in enumerate(all_used_axioms): ax2int[ax] = i int2ax[i] = ax # map integers to corresponding cnfs # allows us to only calculate them once print("Gathering all cnf formulas:") int2cnf = {} for ax, i in ax2int.items(): print(ax.__name__) int2cnf[i] = ax() # transform axioms given into ints, for easier duplicate removal axiom_list = [[ax2int[ax] for ax in sublist] for sublist in axiom_list] # get every trial by taking the cartesian product of input trials = product(*axiom_list) # remove duplicate axioms per trial trials = [tuple(sorted(list(set(x)))) for x in trials] # remove duplicate trials trials = list(set(trials)) trials.sort() with open(outputfilepath, 'w') as f: print("Solving:") for trial in tqdm(trials): cnf = chain(*[int2cnf[ax] for ax in trial]) ans = pylgl.solve(cnf) if ans == "UNSAT": ans = "Unsatisfiable" else: ans = "Satisfiable " axnames = [int2ax[ax].__name__ for ax in trial] f.write(f"{ans}: {', '.join(axnames)}\n")
def test_cnf1_vars(self): self.assertEqual(solve(clauses1, vars=7), [1, -2, -3, -4, 5, -6, -7])
def test_cnf3_3vars(self): self.assertEqual(solve(clauses3, vars=3), [-1, -2, -3])
def test_cnf3(self): self.assertEqual(solve(clauses3), [[-1, -2, -3], [-1, -2, 3]])
def test_cnf2(self): self.assertEqual(solve(clauses2), "UNSAT")
def test_each_clause_gen(self): self.assertEqual(solve([(x for x in clause) for clause in clauses1]), [1, -2, -3, -4, 5])
def test_gen_clauses(self): def gen_clauses(): for clause in clauses1: yield clause self.assertEqual(solve(gen_clauses()), [1, -2, -3, -4, 5])
def test_each_clause_tuples(self): self.assertEqual(solve([tuple(clause) for clause in clauses1]), [1, -2, -3, -4, 5])
def test_tuple_caluses(self): self.assertEqual(solve(tuple(clauses1)), [1, -2, -3, -4, 5])
def test_each_clause_iter(self): self.assertEqual(solve([iter(clause) for clause in clauses1]), [1, -2, -3, -4, 5])
def test_iter_clauses(self): self.assertEqual(solve(iter(clauses1)), [1, -2, -3, -4, 5])
def test_cnf1(self): self.assertEqual(solve(clauses1), [1, -2, -3, -4, 5]) if sys.version_info[0] == 2: cls = [[long(lit) for lit in clause] for clause in clauses1] self.assertEqual(solve(cls), [1, -2, -3, -4, 5])
def test_no_clauses(self): for n in range(7): self.assertEqual(solve([], n), [-i for i in range(1, n + 1)])
#cnf4a = cnfAtLeastOne() + cnfKellyCardinalityStrategyproofness() + cnfProportionality() # impossible (3,4,3) #cnf4b = cnfAtLeastOne() + cnfKellyCardinalityStrategyproofness() + cnfWeakEfficiency() # possible (3,4,3) #cnf4c = cnfAtLeastOne() + cnfKellyCardinalityStrategyproofness() + cnfParetoEfficiency() #possible (3,4,3) #cnf6a = cnfAtLeastOne() + cnfKellyWeakSupersetStrategyproofness() + cnfParetoEfficiency() + cnfProportionality() # possible (3,4,3) #cnf6b = cnfAtLeastOne() + cnfKellyWeakSupersetStrategyproofness() + cnfParetoEfficiency() + cnfSemiPartylistProportionality() #impossible (3,4,3) #cnf7 = cnfAtLeastOne() + cnfProportionality() + cnfOptimisticCardinalityStrategyproofness() + cnfProportionalityVotingRule() + cnfParetoEfficiency() # possible (3,4,3) #cnf8a = cnfAtLeastOne() + cnfProportionality() + cnfPessimisticCardinalityStrategyproofness() + cnfParetoEfficiency() # possible (3,4,3) #cnf8b = cnfAtLeastOne() + cnfSemiPartylistProportionality() + cnfPessimisticCardinalityStrategyproofness() + cnfParetoEfficiency() # impossible (3,4,3) cnf = cnfAtLeastOne() + cnfOptimisticCardinalityStrategyproofness( ) + cnfPessimisticCardinalityStrategyproofness() + cnfProportionality( ) #+ cnfParetoEfficiency() #dimacs(cnf, sum([len(x) for x in cnfAtLeastOne()]), len(cnf), 'cnf/kellyc+prop+par-343.cnf') #t1 = time.time() ans = pylgl.solve(cnf) #t2 = time.time() #print(t2 - t1) #embed() lits = [] a = sorted([x for x in ans if x > 0]) for i in a: lit = int2lit[i] lits.append(lit) print("{} elects: {}".format(lit[1], lit[0])) """counter = 0 for sol in pylgl.itersolve(cnf): counter += 1 if(counter % 100 == 0): print(counter) a = sorted([int2lit[x] for x in sol if x > 0])
def run_SAT(self): self.SAT_result = pylgl.solve(self.clauses)