mirror of
https://github.com/Brandon-Rozek/matmod.git
synced 2024-11-20 21:56:29 -05:00
Introduced ordering at model level...
This commit is contained in:
parent
20ccacc166
commit
ae8658fda2
2 changed files with 67 additions and 22 deletions
|
@ -46,7 +46,6 @@ def only_rules_with(rules: Set[Rule], operation: Operation) -> Set[Rule]:
|
||||||
return result_rules
|
return result_rules
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def possible_interpretations(
|
def possible_interpretations(
|
||||||
logic: Logic, carrier_set: Set[ModelValue],
|
logic: Logic, carrier_set: Set[ModelValue],
|
||||||
designated_values: Set[ModelValue]):
|
designated_values: Set[ModelValue]):
|
||||||
|
@ -88,10 +87,25 @@ def possible_interpretations(
|
||||||
yield interpretation
|
yield interpretation
|
||||||
|
|
||||||
def generate_model(logic: Logic, number_elements: int, num_solutions: int = -1, print_model=False):
|
def generate_model(logic: Logic, number_elements: int, num_solutions: int = -1, print_model=False):
|
||||||
|
assert number_elements > 0
|
||||||
carrier_set = {
|
carrier_set = {
|
||||||
ModelValue("a" + str(i)) for i in range(number_elements)
|
ModelValue("a" + str(i)) for i in range(number_elements)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ordering = set()
|
||||||
|
|
||||||
|
# a(0) is less than all other elements
|
||||||
|
a0 = ModelValue("a0")
|
||||||
|
for v in carrier_set:
|
||||||
|
if v != a0:
|
||||||
|
ordering.add(a0 < v)
|
||||||
|
|
||||||
|
# Every other element is less than a(n - 1)
|
||||||
|
an = ModelValue(f"a{number_elements-1}")
|
||||||
|
for v in carrier_set:
|
||||||
|
if an != v:
|
||||||
|
ordering.add(v < an)
|
||||||
|
|
||||||
possible_designated_values = possible_designations(carrier_set)
|
possible_designated_values = possible_designations(carrier_set)
|
||||||
|
|
||||||
satisfied_models = []
|
satisfied_models = []
|
||||||
|
@ -102,7 +116,7 @@ def generate_model(logic: Logic, number_elements: int, num_solutions: int = -1,
|
||||||
|
|
||||||
for interpretation in possible_interps:
|
for interpretation in possible_interps:
|
||||||
is_valid = True
|
is_valid = True
|
||||||
model = Model(carrier_set, set(interpretation.values()), designated_values)
|
model = Model(carrier_set, set(interpretation.values()), designated_values, ordering)
|
||||||
# Iteratively test possible interpretations
|
# Iteratively test possible interpretations
|
||||||
# by adding one axiom at a time
|
# by adding one axiom at a time
|
||||||
for rule in logic.rules:
|
for rule in logic.rules:
|
||||||
|
|
43
model.py
43
model.py
|
@ -6,7 +6,7 @@ from logic import (
|
||||||
PropositionalVariable, get_propostional_variables, Logic, Term,
|
PropositionalVariable, get_propostional_variables, Logic, Term,
|
||||||
Operation
|
Operation
|
||||||
)
|
)
|
||||||
from typing import Set, List, Dict, Tuple
|
from typing import Set, List, Dict, Tuple, Optional
|
||||||
from itertools import product
|
from itertools import product
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
|
|
||||||
|
@ -27,6 +27,9 @@ class ModelValue:
|
||||||
return self.hashed_value
|
return self.hashed_value
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
return isinstance(other, ModelValue) and self.name == other.name
|
return isinstance(other, ModelValue) and self.name == other.name
|
||||||
|
def __lt__(self, other):
|
||||||
|
assert isinstance(other, ModelValue)
|
||||||
|
return ModelOrderConstraint(self, other)
|
||||||
|
|
||||||
|
|
||||||
class ModelFunction:
|
class ModelFunction:
|
||||||
|
@ -58,17 +61,32 @@ class ModelFunction:
|
||||||
# def __eq__(self, other):
|
# def __eq__(self, other):
|
||||||
# return isinstance(other, ModelFunction) and self.name == other.name and self.arity == other.arity
|
# return isinstance(other, ModelFunction) and self.name == other.name and self.arity == other.arity
|
||||||
|
|
||||||
|
class ModelOrderConstraint:
|
||||||
|
# a < b
|
||||||
|
def __init__(self, a: ModelValue, b: ModelValue):
|
||||||
|
self.a = a
|
||||||
|
self.b = b
|
||||||
|
def __hash__(self):
|
||||||
|
return hash(self.a) * hash(self.b)
|
||||||
|
def __eq__(self, other):
|
||||||
|
return isinstance(other, ModelOrderConstraint) and \
|
||||||
|
self.a == other.a and self.b == other.b
|
||||||
|
|
||||||
class Model:
|
class Model:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
carrier_set: Set[ModelValue],
|
carrier_set: Set[ModelValue],
|
||||||
logical_operations: Set[ModelFunction],
|
logical_operations: Set[ModelFunction],
|
||||||
designated_values: Set[ModelValue]
|
designated_values: Set[ModelValue],
|
||||||
|
ordering: Optional[Set[ModelOrderConstraint]] = None
|
||||||
):
|
):
|
||||||
assert designated_values <= carrier_set
|
assert designated_values <= carrier_set
|
||||||
self.carrier_set = carrier_set
|
self.carrier_set = carrier_set
|
||||||
self.logical_operations = logical_operations
|
self.logical_operations = logical_operations
|
||||||
self.designated_values = designated_values
|
self.designated_values = designated_values
|
||||||
|
self.ordering = ordering if ordering is not None else set()
|
||||||
|
# TODO: Make sure ordering is "valid"
|
||||||
|
# That is: transitive, etc.
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
result = f"""Carrier Set: {set_to_str(self.carrier_set)}
|
result = f"""Carrier Set: {set_to_str(self.carrier_set)}
|
||||||
|
@ -116,20 +134,33 @@ def satisfiable(logic: Logic, model: Model, interpretation: Dict[Operation, Mode
|
||||||
pvars = tuple(get_propostional_variables(tuple(logic.rules)))
|
pvars = tuple(get_propostional_variables(tuple(logic.rules)))
|
||||||
mappings = all_model_valuations_cached(pvars, tuple(model.carrier_set))
|
mappings = all_model_valuations_cached(pvars, tuple(model.carrier_set))
|
||||||
|
|
||||||
|
"""
|
||||||
|
TODO: Make sure that ordering for conjunction and disjunction
|
||||||
|
at the model function level.
|
||||||
|
"""
|
||||||
|
|
||||||
for mapping in mappings:
|
for mapping in mappings:
|
||||||
for rule in logic.rules:
|
for rule in logic.rules:
|
||||||
premise_met = True
|
premise_met = True
|
||||||
|
premise_ts = set()
|
||||||
for premise in rule.premises:
|
for premise in rule.premises:
|
||||||
t = evaluate_term(premise, mapping, interpretation)
|
premise_t = evaluate_term(premise, mapping, interpretation)
|
||||||
if t not in model.designated_values:
|
if premise_t not in model.designated_values:
|
||||||
premise_met = False
|
premise_met = False
|
||||||
break
|
break
|
||||||
|
premise_ts.add(premise_t)
|
||||||
|
|
||||||
if not premise_met:
|
if not premise_met:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
t = evaluate_term(rule.conclusion, mapping, interpretation)
|
consequent_t = evaluate_term(rule.conclusion, mapping, interpretation)
|
||||||
if t not in model.designated_values:
|
|
||||||
|
if consequent_t not in model.designated_values:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Make sure ordering constraint is met
|
||||||
|
for premise_t in premise_ts:
|
||||||
|
if consequent_t < premise_t in model.ordering:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
Loading…
Reference in a new issue