Skip to content

Commit

Permalink
adding get_rules
Browse files Browse the repository at this point in the history
  • Loading branch information
sspola committed May 9, 2023
1 parent 940e70c commit d3661ea
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
setup(
name = 'simpful',
packages = ['simpful'], # this must be the same as the name above
version = '2.10.0',
version = '2.11.0',
description = 'A user-friendly Python library for fuzzy logic',
author = 'Marco S. Nobile',
author_email = '[email protected]',
Expand Down
31 changes: 27 additions & 4 deletions simpful/simpful.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,17 @@ def set_constant(self, name, value, verbose=False):
def add_rules_from_file(self, path, verbose=False):
"""
Imports new fuzzy rules by reading the strings from a text file.
Args:
path: path to the file containing the rules.
verbose: True/False, toggles verbose mode.
"""
if path[-3:].lower()!=".xls" and path[-4:].lower()!=".xlsx":
with open(path) as fi:
rules_strings = fi.readlines()
self.add_rules(rules_strings, verbose=verbose)
else:
raise NotImplementedError("Excel support not available yet.")
raise NotImplementedError("Excel support not available.")


def _sanitize(self, rule):
Expand Down Expand Up @@ -404,11 +408,32 @@ def add_rules(self, rules, verbose=False):
print()
if verbose: print(" * %d rules successfully added" % len(rules))

def get_rules(self):
"""
Returns the rule base of the fuzzy system.
Returns:
a list containing the fuzzy rules as strings, in the same order they were added.
"""
rule_base = []

for r in self._rules:
ant = str(r[0])
ant = ant.replace("c.(", "(")
ant = ant.replace("f.(", "(")
ant = "IF "+ant

cons = r[1]
cons = " THEN ("+cons[0]+" IS "+cons[1]+")"

rule = ant + cons
rule_base.append(rule)

return rule_base

def replace_rule(self, i, new_rule, verbose=False):
"""
Replace the i-th rule in the fuzzy system.
Replaces the i-th rule in the FuzzySystem object. Rules are stored in the same order they were added.
Args:
i: index of the rule to be replaced in the fuzzy system.
Expand All @@ -428,8 +453,6 @@ def replace_rule(self, i, new_rule, verbose=False):
if verbose:
print( " * Rule", i, "replaced with new rule: IF", parsed_antecedent, "THEN", parsed_consequent)
print()
#if verbose: print(" * Rule %d successfully replacd" % i)



def add_linguistic_variable(self, name, LV, verbose=False):
Expand Down

0 comments on commit d3661ea

Please sign in to comment.