class ControlSystem(object):
"""
Base class to contain a Fuzzy Control System.
Parameters
----------
rules : Rule or iterable of Rules, optional
If provided, the system is initialized and populated with a set of
fuzzy Rules (see ``skfuzzy.control.Rule``). This is optional. If
omitted the ControlSystem can be built interactively.
"""
def __init__(self, outFileName, phenotypeList, rules=None):
"""
Initialization method for the fuzzy ControlSystem object.
""" + '\n'.join(ControlSystem.__doc__.split('\n')[1:])
self.outFileName=outFileName
self.Refs = [] #au lieu de "label's fct"
try:
f = open(outFileName +'_FuzzyPrediction.txt', 'r')
except Exception as inst:
print(type(inst))
print(inst.args)
print(inst)
print('cannot open', outFileName +'_FuzzyPrediction.txt')
raise
else:
#build the DB header
self.headerDB = f.readline().rstrip('\n').split('\t') #strip off first row
print("self.headerDB= "+str(self.headerDB))
f.close()
# Construct a system from provided rules, if given
if rules is not None:
if hasattr(rules, '__iter__'):
for rule in rules:
self.addrule(rule)
else:
try:
self.addrule(rules)
except:
raise ValueError("Optional argument `rules` must be a "
"FuzzyRule or iterable of FuzzyRules.")
def rulesDB(self):
"""
yields Rules in the fuzzy prediction file.
"""
# We have to expose the rules in a list
rulesDBlist=[]
try:
f = open(self.outFileName +'_FuzzyPrediction.txt', 'r')
except Exception as inst:
print(type(inst))
print(inst.args)
print(inst)
print('cannot open', outFileName +'_FuzzyPrediction.txt')
raise
else:
for i,line in enumerate(f):
if i==0:
pass
else:
lineList = line.strip('\n').split('\t')
rulesDBlist.append(lineList)
return rulesDBlist
@property
def antecedents(self):
"""Generator which yields Antecedents in the system."""
Ants=[]
for att in self.headerDB:
if att !='Ref' and att!='Class':
print("antecedents: att= "+str(att))
Ants.append(Antecedent(np.arange(0, 1.1, 0.1),att))
return Ants
@property
def consequents(self,phenotypeList):
"""Generator which yields Consequents in the system."""
Cons=[]
for cons in phenotypeList:
Cons.append(Consequent(np.arange(0, 1.1, 0.1),cons))
return Cons
@property
def fuzzy_variables(self):
"""
Generator which yields fuzzy variables in the system.
This includes Antecedents, Consequents, and Intermediaries.
"""
Vars=[]
for Fvar in self.headerDB:
if Fvar !='Ref' and Fvar!='Class':
print("FuzzyVariable: att= "+str(Fvar))
Vars.append(FuzzyVariable(np.arange(0, 1.1, 0.1),Fvar))
return Vars
# if isinstance(line, FuzzyVariable):
def addrule(self, rule):
"""
Add a new rule to the system.
"""
if not isinstance(rule, Rule):
raise ValueError("Input rule must have a Rule object format!")
# Ensure no label duplication
Labels= self.Refs
if rule.label in self.Refs:
raise ValueError("Input rule cannot have same label, '{0}', "
"as any other rule.".format(rule.label))
try:
f = open(self.outFileName +'_FuzzyPrediction.txt','a') # Outputs tab delimited text file of rule population and respective rule stats
except Exception as inst:
print(type(inst))
print(inst.args)
print(inst)
print('cannot open', self.outFileName +'_FuzzyPrediction.txt')
raise
# if (not hasattr(rule.antecedent, '__iter__')):
f.write(str(rule.label)+'\t')
for i,ant in enumerate(self.headerDB):
if (ant in str(rule.antecedent)) and ant!='Ref' and ant!='Class':
f.write(str(rule.antecedent)[str(rule.antecedent).index("[")+1:-1]+'\t')
elif ant=='Ref' or ant== 'Class':
pass
else:
f.write('None'+'\t')
cons= int(str(rule.consequent[0]).index("["))
f.write(str(rule.consequent[0])[0:cons-len(str(rule.consequent[0])) ]+'\n')
f.close()
self.Refs.append(rule.label)
print("rulesDB Labels= "+str(self.Refs))
print("self.rulesDB= "+str(self.rulesDB()))