def ruleMD(stain):
if stain < 0 or stain > 100:
return 0.0
else: # 当传入的参数在0-100之间时,该处有两种情况
# 计算MD的结果,并且和同参数下的SD结果相比较,得出一个结果
if stain >= 0 and stain <= 50:
return stain / 50.0
else:
# 同上的操作,得出结果和同参数下的LD相比较
return (100 - stain) / 50.0
def ruleSD(stain):
# SD部分的rule
# 当输入的参数0 <= x <= 50, 执行该方法
result = (50 - stain) / 50.0
returnMDresult = ruleMD(stain)
# 传参数到MD中,计算,并比较
# 1、相同,则返回结果为SD,2、SD的结果大,则返回SD,3、MD的结果大,则返回MD的返回值
if result < returnMDresult:
return 2.0
else:
return 1.0
def ruleLD(stain):
# LD部分的rule
# 当输入的参数在50 - 100之间时,执行
result = (stain - 50) / 50
returnMDresult = ruleMD(stain)
# 同时将参数传入给MD,同时比较MD方法传回来的参数和该方法求出的值相比较,求出最后的最适合的预测值
# ********** Begin **********#
if result < returnMDresult:
return 2.0
else:
return 3.0
# ********** End **********#
def ruleMG(oil):
# 当传入的参数在0 - 100之间时,该处有两种情况
if oil < 0 or oil > 100:
return 0 # 当在论域之外时,直接返回无结果
else:
if oil >= 0 and oil <= 50:
return oil / 50.0 # 计算MD的结果,并且和同参数下的SD结果相比较,得出一个结果
else:
return (100 - oil) / 50 # 同上的操作,得出结果和同参数下的LD相比较
def ruleSG(oil):
if oil < 0 or oil > 50:
return 0.0
else:
# SG部分的rule
# 当输入的参数0<=x<=50,执行该方法
result = (50 - oil) / 50.0
returnMGresult = ruleMG(oil)
# 传参数到MD中,计算,并比较
# 1、相同,则返回结果为SD,2、SD的结果大,则返回SD,3、MD的结果大,则返回MD的返回值
if result < returnMGresult:
return 2.0
else:
return 1.0
def ruleLG(oil):
# LD部分的rula
# 当输入的参数在50 - 100之间时,执行
# 同时将参数传入给MG,同时比较MG方法传回来的参数和该方法求出的值相比较,求出最后的最适合的预测值
returnMGresult = ruleMG(oil)
result = (oil - 50) / 50.0
# 比较后,得到预测值
if result < returnMGresult:
return 2.0
else:
return 3.0
# F函数,总的函数,从该函数中分流到rule的三个函数中
def Function(oil, stain):
# VS: SD, SG
# S: MD, SG
# M: SD, MG MD, MG LD, SG
# L: SD, LG MD,LG LD,MG
# XL: LD, LG
# 根据规则输出最后的洗涤时间
# 需要客户的正确输入
# ********** Begin **********#
if stain >= 0 and stain <= 50:
result_D = ruleSD(stain)
else:
result_D = ruleLD(stain)
if oil >= 0 and oil <= 50:
result_G = ruleSG(oil)
else:
result_G = ruleLG(oil)
# ********** End **********#
# 比较最后的结果,返回结果控制规则表,例如VS在表格中的坐标是(1,1),S的坐标是(2,1)
if result_D == 1.0 and result_G == 1.0:
return 1 # return VS
elif result_G == 1.0 and result_D == 2.0:
return 2 # return S
elif (result_D == 1.0 and result_G == 2.0) or (result_G == 2.0 and result_D == 2.0) or (
result_G == 1.0 and result_D == 3.0):
return 3 # reutrn M
elif (result_D == 1.0 and result_G == 3.0) or (result_D == 2.0 and result_G == 3.0) or (
result_D == 3.0 and result_G == 2.0):
return 4 # return L
elif result_G == 3.0 and result_D == 3.0:
return 5 # return VL