mirror of
https://github.com/Brandon-Rozek/matmod.git
synced 2025-07-05 07:37:22 +00:00
Compare commits
No commits in common. "c9a4f6ce365c01a5a5cc3896a07a975eb413f6a2" and "d14ad5279834c8d83e4f79f8b447db76b97ad376" have entirely different histories.
c9a4f6ce36
...
d14ad52798
1 changed files with 11 additions and 31 deletions
|
@ -15,18 +15,7 @@ from logic import (
|
||||||
)
|
)
|
||||||
from vsp import has_vsp
|
from vsp import has_vsp
|
||||||
|
|
||||||
class SourceFile:
|
def parse_matrices(infile: TextIO) -> List[Tuple[Model, Dict]]:
|
||||||
def __init__(self, fileobj: TextIO):
|
|
||||||
self.fileobj = fileobj
|
|
||||||
self.current_line = 0
|
|
||||||
|
|
||||||
def __next__(self):
|
|
||||||
contents = next(self.fileobj)
|
|
||||||
self.current_line += 1
|
|
||||||
return contents
|
|
||||||
|
|
||||||
|
|
||||||
def parse_matrices(infile: SourceFile) -> List[Tuple[Model, Dict]]:
|
|
||||||
next(infile) # Skip header line
|
next(infile) # Skip header line
|
||||||
|
|
||||||
solutions: List[Tuple[Model, Dict]] = []
|
solutions: List[Tuple[Model, Dict]] = []
|
||||||
|
@ -38,40 +27,31 @@ def parse_matrices(infile: SourceFile) -> List[Tuple[Model, Dict]]:
|
||||||
|
|
||||||
carrier_set = carrier_set_from_size(size)
|
carrier_set = carrier_set_from_size(size)
|
||||||
|
|
||||||
num_negation = 0
|
|
||||||
while True:
|
while True:
|
||||||
mnegation = parse_negation(infile, size)
|
mnegation = parse_negation(infile, size)
|
||||||
if mnegation is None:
|
if mnegation is None:
|
||||||
break
|
break
|
||||||
num_negation += 1
|
|
||||||
|
|
||||||
num_order = 0
|
|
||||||
while True:
|
while True:
|
||||||
result = parse_order(infile, size)
|
result = parse_order(infile, size)
|
||||||
if result is None:
|
if result is None:
|
||||||
break
|
break
|
||||||
mconjunction, mdisjunction = result
|
mconjunction, mdisjunction = result
|
||||||
num_order += 1
|
|
||||||
|
|
||||||
num_designated = 0
|
|
||||||
while True:
|
while True:
|
||||||
designated_values = parse_designated(infile, size)
|
designated_values = parse_designated(infile, size)
|
||||||
if designated_values is None:
|
if designated_values is None:
|
||||||
break
|
break
|
||||||
num_designated += 1
|
|
||||||
|
|
||||||
results = parse_implication(infile, size)
|
results = parse_implication(infile, size)
|
||||||
if result is None:
|
if result is None:
|
||||||
break
|
break
|
||||||
|
|
||||||
num_implication = 0
|
|
||||||
for mimplication in results:
|
for mimplication in results:
|
||||||
logical_operations = {
|
logical_operations = {
|
||||||
mnegation, mimplication
|
mnegation, mimplication
|
||||||
}
|
}
|
||||||
num_implication += 1
|
model = Model(carrier_set, logical_operations, designated_values, name=str(len(solutions)))
|
||||||
model_name = f"{size}.{num_negation}.{num_order}.{num_designated}.{num_implication}"
|
|
||||||
model = Model(carrier_set, logical_operations, designated_values, name=model_name)
|
|
||||||
interpretation = {
|
interpretation = {
|
||||||
Negation: mnegation,
|
Negation: mnegation,
|
||||||
Implication: mimplication
|
Implication: mimplication
|
||||||
|
@ -97,17 +77,17 @@ def carrier_set_from_size(size: int):
|
||||||
mvalue_from_index(i) for i in range(size + 1)
|
mvalue_from_index(i) for i in range(size + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
def parse_size(infile: SourceFile) -> Optional[int]:
|
def parse_size(infile: TextIO) -> Optional[int]:
|
||||||
"""
|
"""
|
||||||
Parse the line representing the matrix size.
|
Parse the line representing the matrix size.
|
||||||
"""
|
"""
|
||||||
size = int(next(infile))
|
size = int(next(infile))
|
||||||
if size == -1:
|
if size == -1:
|
||||||
return None
|
return None
|
||||||
assert size > 0, f"Unexpected size at line {infile.current_line}"
|
assert size > 0, "Unexpected size"
|
||||||
return size
|
return size
|
||||||
|
|
||||||
def parse_negation(infile: SourceFile, size: int) -> Optional[ModelFunction]:
|
def parse_negation(infile: TextIO, size: int) -> Optional[ModelFunction]:
|
||||||
"""
|
"""
|
||||||
Parse the line representing the negation table.
|
Parse the line representing the negation table.
|
||||||
"""
|
"""
|
||||||
|
@ -116,7 +96,7 @@ def parse_negation(infile: SourceFile, size: int) -> Optional[ModelFunction]:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
row = line.split(" ")
|
row = line.split(" ")
|
||||||
assert len(row) == size + 1, f"Negation table doesn't match size at line {infile.current_line}"
|
assert len(row) == size + 1, "Negation table doesn't match size"
|
||||||
mapping = {}
|
mapping = {}
|
||||||
|
|
||||||
for i, j in zip(range(size + 1), row):
|
for i, j in zip(range(size + 1), row):
|
||||||
|
@ -198,7 +178,7 @@ def parse_order(infile: TextIO, size: int) -> Optional[Tuple[ModelFunction, Mode
|
||||||
|
|
||||||
table = line.split(" ")
|
table = line.split(" ")
|
||||||
|
|
||||||
assert len(table) == (size + 1)**2, f"Order table doesn't match expected size at line {infile.current_line}"
|
assert len(table) == (size + 1)**2
|
||||||
|
|
||||||
omapping = {}
|
omapping = {}
|
||||||
table_i = 0
|
table_i = 0
|
||||||
|
@ -248,7 +228,7 @@ def parse_designated(infile: TextIO, size: int) -> Optional[Set[ModelValue]]:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
row = line.split(" ")
|
row = line.split(" ")
|
||||||
assert len(row) == size + 1, f"Designated table doesn't match expected size at line {infile.current_line}"
|
assert len(row) == size + 1, "Designated table doesn't match size"
|
||||||
|
|
||||||
designated_values = set()
|
designated_values = set()
|
||||||
|
|
||||||
|
@ -272,7 +252,7 @@ def parse_implication(infile: TextIO, size: int) -> Optional[List[ModelFunction]
|
||||||
# Split and remove the last '-1' character
|
# Split and remove the last '-1' character
|
||||||
table = line.split(" ")[:-1]
|
table = line.split(" ")[:-1]
|
||||||
|
|
||||||
assert len(table) % (size + 1)**2 == 0, f"Implication table does not match expected size at line {infile.current_line}"
|
assert len(table) % (size + 1)**2 == 0
|
||||||
|
|
||||||
table_i = 0
|
table_i = 0
|
||||||
mimplications: List[ModelFunction] = []
|
mimplications: List[ModelFunction] = []
|
||||||
|
@ -297,7 +277,7 @@ def parse_implication(infile: TextIO, size: int) -> Optional[List[ModelFunction]
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
solutions: List[Model] = parse_matrices(SourceFile(sys.stdin))
|
solutions: List[Model] = parse_matrices(sys.stdin)
|
||||||
print(f"Parsed {len(solutions)} matrices")
|
print(f"Parsed {len(solutions)} matrices")
|
||||||
for i, (model, interpretation) in enumerate(solutions):
|
for i, (model, interpretation) in enumerate(solutions):
|
||||||
print(model)
|
print(model)
|
||||||
|
|
Loading…
Add table
Reference in a new issue