Check models in parallel

This commit is contained in:
Brandon Rozek 2024-11-26 16:24:49 -05:00
parent fb074ff0e8
commit aeb8ae0440
4 changed files with 26 additions and 14 deletions

View file

@ -1,4 +1,7 @@
from typing import Set from typing import Set
def immutable(_self, _name, _value):
raise Exception("Model values are immutable")
def set_to_str(x: Set) -> str: def set_to_str(x: Set) -> str:
return "{" + ", ".join((str(xi) for xi in x)) + "}" return "{" + ", ".join((str(xi) for xi in x)) + "}"

View file

@ -1,3 +1,4 @@
from common import immutable
from typing import Optional, Set, Tuple from typing import Optional, Set, Tuple
from functools import lru_cache from functools import lru_cache
@ -6,13 +7,14 @@ class Operation:
self.symbol = symbol self.symbol = symbol
self.arity = arity self.arity = arity
self.hashed_value = hash(self.symbol) + self.arity self.hashed_value = hash(self.symbol) + self.arity
def immutable(self, name, value):
raise Exception("Operations are immutable")
self.__setattr__ = immutable self.__setattr__ = immutable
def __hash__(self): def __hash__(self):
return self.hashed_value return self.hashed_value
def __eq__(self, x):
return self.symbol == x.symbol and self.arity == x.arity
def __call__(self, *args): def __call__(self, *args):
# Ensure the arity is met # Ensure the arity is met
assert len(args) == self.arity assert len(args) == self.arity

View file

@ -2,7 +2,7 @@
Matrix model semantics and satisfiability of Matrix model semantics and satisfiability of
a given logic. a given logic.
""" """
from common import set_to_str from common import set_to_str, immutable
from logic import ( from logic import (
get_propostional_variables, Logic, get_propostional_variables, Logic,
Operation, PropositionalVariable, Term Operation, PropositionalVariable, Term
@ -15,13 +15,10 @@ from typing import Dict, List, Optional, Set, Tuple
__all__ = ['ModelValue', 'ModelFunction', 'Model', 'Interpretation'] __all__ = ['ModelValue', 'ModelFunction', 'Model', 'Interpretation']
class ModelValue: class ModelValue:
def __init__(self, name): def __init__(self, name):
self.name = name self.name = name
self.hashed_value = hash(self.name) self.hashed_value = hash(self.name)
def immutable(self, name, value):
raise Exception("Model values are immutable")
self.__setattr__ = immutable self.__setattr__ = immutable
def __str__(self): def __str__(self):
return self.name return self.name

View file

@ -1,10 +1,13 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from os import cpu_count
import argparse import argparse
import multiprocessing
from parse_magic import ( from parse_magic import (
SourceFile, SourceFile,
parse_matrices parse_matrices
) )
from vsp import has_vsp from vsp import has_vsp, VSP_Result
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser(description="VSP Checker") parser = argparse.ArgumentParser(description="VSP Checker")
@ -22,14 +25,21 @@ if __name__ == "__main__":
print(f"Parsed {len(solutions)} matrices") print(f"Parsed {len(solutions)} matrices")
num_has_vsp = 0 num_has_vsp = 0
for i, (model, interpretation) in enumerate(solutions): with multiprocessing.Pool(processes=max(cpu_count() - 2, 1)) as pool:
vsp_result = has_vsp(model, interpretation) results = [
print(vsp_result) pool.apply_async(has_vsp, (model, interpretation,))
for model, interpretation in solutions
]
if args['verbose'] or vsp_result.has_vsp: for i, result in enumerate(results):
print(model) vsp_result: VSP_Result = result.get()
print(vsp_result)
if vsp_result.has_vsp: if args['verbose'] or vsp_result.has_vsp:
num_has_vsp += 1 model = solutions[i][0]
print(model)
if vsp_result.has_vsp:
num_has_vsp += 1
print(f"Tested {len(solutions)} models, {num_has_vsp} of which satisfy VSP") print(f"Tested {len(solutions)} models, {num_has_vsp} of which satisfy VSP")