Compare commits

..

No commits in common. "b4b5a7d4e6eed7fb79d142e61f63299f65afe43e" and "c9a4f6ce365c01a5a5cc3896a07a975eb413f6a2" have entirely different histories.

6 changed files with 55 additions and 256 deletions

View file

@ -9,7 +9,7 @@ Interested in seeing which satisfiable models from [arranstewart/magic](https://
(2) Run our tool! It will first attempt to parse all the matrices in the output file and then check for the variable sharing property one-by-one. (2) Run our tool! It will first attempt to parse all the matrices in the output file and then check for the variable sharing property one-by-one.
``` ```
python3 parse_magic.py < examples/R6 python3 parse_magic.py < UGLY_FILE_FROM_MAGIC
``` ```
If you face any troubles, feel free to reach out. This tool also has capabilities to generate satisfiable models given a specification (see: R.py), however, it is much slower than magic so you're better off using that. If you face any troubles, feel free to reach out. This tool also has capabilities to generate satisfiable models given a specification (see: R.py), however, it is much slower than magic so you're better off using that.

View file

@ -1,32 +0,0 @@
0 1 1 1 1 1 0 0
1
1 1 0 1
0 1
1 1 0 1 -1
-1
-1
2
1 1 1 0 1 1 0 0 1
0 1 1
2 2 2 0 1 2 0 0 2 -1
0 0 1
2 2 2 0 2 2 0 1 2 -1
-1
-1
3
1 1 1 1 0 1 0 1 0 0 1 1 0 0 0 1
0 1 0 1
3 3 3 3 0 1 2 3 0 0 1 3 0 0 0 3 3 3 3 3 0 1 2 3 0 0 3 3 0 0 2 3 -1
0 0 0 1
3 3 3 3 2 3 2 3 1 1 3 3 0 1 2 3 -1
-1
1 1 1 1 0 1 1 1 0 0 1 1 0 0 0 1
0 1 1 1
3 3 3 3 0 1 2 3 0 0 1 3 0 0 0 3 3 3 3 3 0 1 2 3 0 0 2 3 0 0 0 3 -1
0 0 1 1
3 3 3 3 0 2 2 3 0 1 2 3 0 0 0 3 3 3 3 3 0 3 3 3 0 1 2 3 0 1 1 3 -1
0 0 0 1
3 3 3 3 0 3 3 3 0 1 3 3 0 1 2 3 -1
-1
-1
-1

View file

@ -1,78 +0,0 @@
1 1 1 1 1 1 0 0
1
1 0
1 1 0 1
0 1
1 1 0 1 -1
-1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 1 1 1 0 1 1 1 1 1 0 0 1 0 1 1 0 0 0 1 1 1 0 0 0 0 1 1 0 0 0 0 0 1
0 1 1 1 1 1
5 5 5 5 5 5 0 1 2 3 4 5 0 0 1 0 2 5 0 0 0 1 3 5 0 0 0 0 1 5 0 0 0 0 0 5 5 5 5 5 5 5 0 1 2 3 4 5 0 0 2 0 2 5 0 0 0 1 3 5 0 0 0 0 1 5 0 0 0 0 0 5 5 5 5 5 5 5 0 1 2 3 4 5 0 0 2 0 2 5 0 0 0 3 3 5 0 0 0 0 1 5 0 0 0 0 0 5 -1
-1
-1
-1
-1

View file

@ -1,11 +0,0 @@
# Example Ugly Data Format Files
These are example files that you can use with the `parse_magic.py` script.
## R6
Contains all models of R up to size 6.
## R4-MN
Contains all models of a fragment of R without negation up to size 4.

View file

@ -3,7 +3,6 @@ Parses the Magic Ugly Data File Format
Assumes the base logic is R with no extra connectives Assumes the base logic is R with no extra connectives
""" """
import argparse
import sys import sys
from typing import TextIO, List, Optional, Tuple, Set, Dict from typing import TextIO, List, Optional, Tuple, Set, Dict
@ -26,133 +25,68 @@ class SourceFile:
self.current_line += 1 self.current_line += 1
return contents return contents
class UglyHeader:
def __init__(self, negation: bool, necessitation: bool):
self.negation = negation
self.necessitation = necessitation
class ModelBuilder:
def __init__(self):
self.size : int = 0
self.carrier_set : Set[ModelValue] = set()
self.num_negation: int = 0
self.mnegation: Optional[ModelFunction] = None
self.num_order: int = 0
self.mconjunction: Optional[ModelFunction] = None
self.mdisjunction: Optional[ModelFunction] = None
self.num_designated: int = 0
self.designated_values: Set[ModelValue] = set()
self.num_implication: int = 0
self.mimplication: Optional[ModelFunction] = None
def parse_matrices(infile: SourceFile) -> List[Tuple[Model, Dict]]: def parse_matrices(infile: SourceFile) -> List[Tuple[Model, Dict]]:
solutions = [] # Reset next(infile) # Skip header line
header = parse_header(infile)
current_model_parts = ModelBuilder() solutions: List[Tuple[Model, Dict]] = []
process_sizes(infile, header, current_model_parts, solutions)
return solutions
def process_sizes(infile: SourceFile, header: UglyHeader, current_model_parts: ModelBuilder, solutions: List[Tuple[Model, Dict]]):
"""Stage 1"""
while True: while True:
size = parse_size(infile) size = parse_size(infile)
if size is None: if size is None:
break break
carrier_set = carrier_set_from_size(size)
current_model_parts.size = size
current_model_parts.carrier_set = carrier_set
process_negations(infile, header, current_model_parts, solutions)
def process_negations(infile: SourceFile, header: UglyHeader, current_model_parts: ModelBuilder, solutions: List[Tuple[Model, Dict]]): carrier_set = carrier_set_from_size(size)
"""Stage 2 (Optional)"""
num_negation = 0 num_negation = 0
while True: while True:
mnegation = None mnegation = parse_negation(infile, size)
if header.negation:
mnegation = parse_single_negation(infile, current_model_parts.size)
if mnegation is None: if mnegation is None:
break break
num_negation += 1 num_negation += 1
current_model_parts.num_negation = num_negation
current_model_parts.mnegation = mnegation
process_orders(infile, header, current_model_parts, solutions)
if not header.negation:
break
def process_orders(infile: SourceFile, header: UglyHeader, current_model_parts: ModelBuilder, solutions: List[Tuple[Model, Dict]]):
"""Stage 3"""
num_order = 0 num_order = 0
while True: while True:
result = parse_single_order(infile, current_model_parts.size) result = parse_order(infile, size)
if result is None: if result is None:
break break
num_order += 1
mconjunction, mdisjunction = result mconjunction, mdisjunction = result
current_model_parts.num_order = num_order num_order += 1
current_model_parts.mconjunction = mconjunction
current_model_parts.mdisjunction = mdisjunction
process_designateds(infile, header, current_model_parts, solutions)
def process_designateds(infile: SourceFile, header: UglyHeader, current_model_parts: ModelBuilder, solutions: List[Tuple[Model, Dict]]):
"""Stage 4"""
num_designated = 0 num_designated = 0
while True: while True:
designated_values = parse_single_designated(infile, current_model_parts.size) designated_values = parse_designated(infile, size)
if designated_values is None: if designated_values is None:
break break
num_designated += 1 num_designated += 1
current_model_parts.num_designated = num_designated
current_model_parts.designated_values = designated_values
process_implications(infile, header, current_model_parts, solutions)
def process_implications( results = parse_implication(infile, size)
infile: SourceFile, header: UglyHeader, current_model_parts: ModelBuilder, solutions: List[Tuple[Model, Dict]]): if result is None:
"""Stage 5""" break
results = parse_implications(infile, current_model_parts.size)
for num_implication, mimplication in enumerate(results, 1):
current_model_parts.num_implication = num_implication
current_model_parts.mimplication = mimplication
process_model(current_model_parts, solutions)
def process_model(mp: ModelBuilder, solutions: List[Tuple[Model, Dict]]): num_implication = 0
"""Create Model""" for mimplication in results:
assert mp.mimplication is not None logical_operations = {
assert len(mp.carrier_set) > 0 mnegation, mimplication
logical_operations = { mp.mimplication }
model_name = f"{mp.size}{'.' + str(mp.num_negation) if mp.num_negation != 0 else ''}.{mp.num_order}.{mp.num_designated}.{mp.num_implication}"
model = Model(mp.carrier_set, logical_operations, mp.designated_values, name=model_name)
interpretation = {
Implication: mp.mimplication
} }
if mp.mnegation is not None: num_implication += 1
logical_operations.add(mp.mnegation) model_name = f"{size}.{num_negation}.{num_order}.{num_designated}.{num_implication}"
interpretation[Negation] = mp.mnegation model = Model(carrier_set, logical_operations, designated_values, name=model_name)
if mp.mconjunction is not None: interpretation = {
logical_operations.add(mp.mconjunction) Negation: mnegation,
interpretation[Conjunction] = mp.mconjunction Implication: mimplication
if mp.mdisjunction is not None: }
logical_operations.add(mp.mdisjunction) if mconjunction is not None:
interpretation[Disjunction] = mp.mdisjunction logical_operations.add(mconjunction)
interpretation[Conjunction] = mconjunction
if mdisjunction is not None:
logical_operations.add(mdisjunction)
interpretation[Disjunction] = mdisjunction
solutions.append((model, interpretation)) solutions.append((model, interpretation))
print(f"Parsed Matrix {model.name}") print(f"Parsed Matrix {model.name}")
def parse_header(infile: SourceFile) -> UglyHeader: return solutions
"""
Parse the header line from the ugly data format.
NOTE: Currently Incomplete.
"""
header_line = next(infile).strip()
header_tokens = header_line.split(" ")
assert header_tokens[0] in ["0", "1"]
assert header_tokens[6] in ["0", "1"]
negation_defined = bool(int(header_tokens[0]))
necessitation_defined = bool(int(header_tokens[6]))
return UglyHeader(negation_defined, necessitation_defined)
def carrier_set_from_size(size: int): def carrier_set_from_size(size: int):
""" """
@ -173,7 +107,7 @@ def parse_size(infile: SourceFile) -> Optional[int]:
assert size > 0, f"Unexpected size at line {infile.current_line}" assert size > 0, f"Unexpected size at line {infile.current_line}"
return size return size
def parse_single_negation(infile: SourceFile, size: int) -> Optional[ModelFunction]: def parse_negation(infile: SourceFile, size: int) -> Optional[ModelFunction]:
""" """
Parse the line representing the negation table. Parse the line representing the negation table.
""" """
@ -254,7 +188,7 @@ def determine_dresult(size: int, ordering: Dict[ModelValue, ModelValue], a: Mode
if not invalid: if not invalid:
return c return c
def parse_single_order(infile: TextIO, size: int) -> Optional[Tuple[ModelFunction, ModelFunction]]: def parse_order(infile: TextIO, size: int) -> Optional[Tuple[ModelFunction, ModelFunction]]:
""" """
Parse the line representing the ordering table Parse the line representing the ordering table
""" """
@ -305,7 +239,7 @@ def parse_single_order(infile: TextIO, size: int) -> Optional[Tuple[ModelFunctio
return mconjunction, mdisjunction return mconjunction, mdisjunction
def parse_single_designated(infile: TextIO, size: int) -> Optional[Set[ModelValue]]: def parse_designated(infile: TextIO, size: int) -> Optional[Set[ModelValue]]:
""" """
Parse the line representing which model values are designated. Parse the line representing which model values are designated.
""" """
@ -326,12 +260,14 @@ def parse_single_designated(infile: TextIO, size: int) -> Optional[Set[ModelValu
return designated_values return designated_values
def parse_implications(infile: TextIO, size: int) -> List[ModelFunction]: def parse_implication(infile: TextIO, size: int) -> Optional[List[ModelFunction]]:
""" """
Parse the line representing the list of implication Parse the line representing the list of implication
tables. tables.
""" """
line = next(infile).strip() line = next(infile).strip()
if line == '-1':
return None
# Split and remove the last '-1' character # Split and remove the last '-1' character
table = line.split(" ")[:-1] table = line.split(" ")[:-1]
@ -361,19 +297,8 @@ def parse_implications(infile: TextIO, size: int) -> List[ModelFunction]:
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser(description="VSP Checker") solutions: List[Model] = parse_matrices(SourceFile(sys.stdin))
parser.add_argument("--verbose", action='store_true', help="Print out all parsed matrices")
args = vars(parser.parse_args())
solutions = parse_matrices(SourceFile(sys.stdin))
print(f"Parsed {len(solutions)} matrices") print(f"Parsed {len(solutions)} matrices")
num_has_vsp = 0
for i, (model, interpretation) in enumerate(solutions): for i, (model, interpretation) in enumerate(solutions):
vsp_result = has_vsp(model, interpretation)
print(vsp_result)
if args['verbose'] or vsp_result.has_vsp:
print(model) print(model)
print(has_vsp(model, interpretation))
if vsp_result.has_vsp:
num_has_vsp += 1
print(f"Tested {len(solutions)} models, {num_has_vsp} of which satisfy VSP")

5
vsp.py
View file

@ -63,11 +63,6 @@ def has_vsp(model: Model, interpretation: Dict[Operation, ModelFunction]) -> VSP
""" """
impfunction = interpretation[Implication] impfunction = interpretation[Implication]
# NOTE: No models with only one designated
# value satisfies VSP
if len(model.designated_values) == 1:
return VSP_Result(False, model.name)
# Compute I the set of tuples (x, y) where # Compute I the set of tuples (x, y) where
# x -> y does not take a designiated value # x -> y does not take a designiated value
I: Set[Tuple[ModelValue, ModelValue]] = set() I: Set[Tuple[ModelValue, ModelValue]] = set()