Sync rules alg with paper (remove clusters based groups)

This commit is contained in:
Aleksey Filippov 2025-02-26 14:11:46 +04:00
parent b2534ba05e
commit 602d36681d
3 changed files with 954 additions and 1170 deletions

File diff suppressed because one or more lines are too long

View File

@ -288,6 +288,26 @@ def simplify_and_group_rules(
return new_rules
def simplify_rules(X: pd.DataFrame, rules: List[Rule]) -> List[Rule]:
minmax = _get_variables_minmax(X)
new_rules: List[Rule] = []
for rule in rules:
intervals = _get_varibles_interval(rule.get_antecedent())
new_atoms = []
for key, value in intervals.items():
val: float = 0
if value[0] is None and value[1] is not None:
val = minmax[key][0]
if value[1] is None and value[0] is not None:
val = minmax[key][1]
if value[0] is not None and value[1] is not None:
val = (value[0] + value[1]) / 2
new_atoms.append(RuleAtom(key, ComparisonType.EQUALS, val))
new_rules.append(Rule(new_atoms, rule.get_consequent()))
return new_rules
def _get_fuzzy_rule_atom(
fuzzy_variable: FuzzyVariable, value: float
) -> Tuple[Term, float]:
@ -299,6 +319,30 @@ def _get_fuzzy_rule_atom(
return (fuzzy_variable[best_value[0]], best_value[1])
def simplify_rules_fuzz(
X: pd.DataFrame,
rules: List[Rule],
fuzzy_variables: Dict[str, FuzzyVariable],
) -> List[Rule]:
minmax = _get_variables_minmax(X)
new_rules: List[Rule] = []
for rule in rules:
intervals = _get_varibles_interval(rule.get_antecedent())
new_atoms = []
for key, value in intervals.items():
val: float = 0
if value[0] is None and value[1] is not None:
val = minmax[key][0]
if value[1] is None and value[0] is not None:
val = minmax[key][1]
if value[0] is not None and value[1] is not None:
val = (value[0] + value[1]) / 2
new_atoms.append(RuleAtom(key, ComparisonType.EQUALS, val))
new_rules.append(Rule(new_atoms, rule.get_consequent()))
return new_rules
def _get_fuzzy_rules(
rules: List[Rule], fuzzy_variables: Dict[str, FuzzyVariable]
) -> List[Tuple[List[RuleAtom], Term, float]]:
@ -328,11 +372,11 @@ def _get_fuzzy_rules(
def _delete_same_fuzzy_rules(
rules_cluster: List[Tuple[List[RuleAtom], Term, float]]
rules: List[Tuple[List[RuleAtom], Term, float]]
) -> List[Tuple[List[RuleAtom], Term, float]]:
same_rules: List[int] = []
for rule1_index, rule1 in enumerate(rules_cluster):
for rule2_index, rule2 in enumerate(rules_cluster):
for rule1_index, rule1 in enumerate(rules):
for rule2_index, rule2 in enumerate(rules):
if rule1_index >= rule2_index:
continue
# Remove the same rules
@ -347,10 +391,10 @@ def _delete_same_fuzzy_rules(
if str(rule1[0]) == str(rule2[0]) and str(rule1[2]) > str(rule2[2]):
same_rules.append(rule1_index)
break
return [rule for index, rule in enumerate(rules_cluster) if index not in same_rules]
return [rule for index, rule in enumerate(rules) if index not in same_rules]
def get_fuzzy_rules(
def get_grouped_fuzzy_rules(
clustered_rules: List[List[Rule]], fuzzy_variables: Dict[str, FuzzyVariable]
) -> List[FuzzyRule]:
fuzzy_rules: List[List[Tuple[List[RuleAtom], Term, float]]] = []
@ -363,3 +407,12 @@ def get_fuzzy_rules(
for cluster in fuzzy_rules
for item in cluster
]
def get_fuzzy_rules(
rules: List[Rule], fuzzy_variables: Dict[str, FuzzyVariable]
) -> List[FuzzyRule]:
fuzzy_rules: List[Tuple[List[RuleAtom], Term, float]] = []
fuzzy_rules = _get_fuzzy_rules(rules, fuzzy_variables)
fuzzy_rules = _delete_same_fuzzy_rules(fuzzy_rules)
return [FuzzyRule(reduce(and_, item[0]), item[1]) for item in fuzzy_rules]

File diff suppressed because one or more lines are too long