本文整理汇总了Python中skfuzzy.trimf函数的典型用法代码示例。如果您正苦于以下问题:Python trimf函数的具体用法?Python trimf怎么用?Python trimf使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了trimf函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, lowest=0, highest=40, step=0.5):
super(ControlTemperature, self).__init__()
self.lowest = lowest
self.highest = highest
self.step = step
self.temp = np.arange(lowest, highest, step)
self.temp_err = np.arange(-50, 50, 0.1)
self.temp_roc = np.arange(-30, 30, 0.1) # degress c per minute
self.chill_out = np.arange(-100, 100, 1)
# self.heat_out = np.arange(0, 100, 1)
#Current Temperature
self.te_too_cold = fuzz.trapmf(self.temp_err, [-40, -30, -1.5, -1])
self.te_cold = fuzz.trimf(self.temp_err, [-1.5, -1, -0.5])
self.te_optimal = fuzz.trimf(self.temp_err, [-0.5, 0, 0.5])
self.te_hot = fuzz.trimf(self.temp_err, [0.5, 1, 1.5])
self.te_too_hot = fuzz.trapmf(self.temp_err, [1, 1.5, 30, 40])
#Temperature Rate of Change (deg C per minute)
self.tr_cooling_quickly = fuzz.trapmf(self.temp_roc, [-20, -10, -0.5, -0.25])
# Output - Chiller
self.co_off = fuzz.trimf(self.chill_out, [0, 0, 5])
self.co_low = fuzz.trimf(self.chill_out, [5, 20, 40])
self.co_medium = fuzz.trimf(self.chill_out, [20, 40, 60])
self.co_high = fuzz.trapmf(self.chill_out, [40, 60, 100, 100])
# Output - Heater
self.ho_off = fuzz.trimf(self.chill_out, [-5, 0, 0])
self.ho_low = fuzz.trimf(self.chill_out, [-40, -20, -5])
self.ho_medium = fuzz.trimf(self.chill_out, [-60, -40, -20])
self.ho_high = fuzz.trapmf(self.chill_out, [-100, -100, -60, -40])
示例2: test_tipping_problem
def test_tipping_problem():
# The full tipping problem uses many of these methods
food = ctrl.Antecedent(np.linspace(0, 10, 11), 'quality')
service = ctrl.Antecedent(np.linspace(0, 10, 11), 'service')
tip = ctrl.Consequent(np.linspace(0, 25, 26), 'tip')
food.automf(3)
service.automf(3)
# Manual membership function definition
tip['bad'] = fuzz.trimf(tip.universe, [0, 0, 13])
tip['middling'] = fuzz.trimf(tip.universe, [0, 13, 25])
tip['lots'] = fuzz.trimf(tip.universe, [13, 25, 25])
# Define fuzzy rules
rule1 = ctrl.Rule(food['poor'] | service['poor'], tip['bad'])
rule2 = ctrl.Rule(service['average'], tip['middling'])
rule3 = ctrl.Rule(service['good'] | food['good'], tip['lots'])
# The control system - defined both possible ways
tipping = ctrl.ControlSystem([rule1, rule2, rule3])
tipping2 = ctrl.ControlSystem()
tipping2.addrule(rule2)
tipping2.addrule(rule3)
tipping2.addrule(rule1)
tip_sim = ctrl.ControlSystemSimulation(tipping)
tip_sim2 = ctrl.ControlSystemSimulation(tipping2)
# Inputs added both possible ways
inputs = {'quality': 6.5, 'service': 9.8}
for key, value in inputs.items():
tip_sim.input[key] = value
tip_sim2.inputs(inputs)
# Compute the system
tip_sim.compute()
tip_sim2.compute()
# Ensure both methods of defining rules yield the same results
for val0, val1 in zip(tip_sim.output.values(),
tip_sim2.output.values()):
tst.assert_allclose(val0, val1)
# Verify against manual computation
tst.assert_allclose(tip_sim.output['tip'], 19.8578, atol=1e-2, rtol=1e-2)
示例3: membership_f
def membership_f(mf, x, abcd):
"""
Returns y values corresponding to type of type of Membership fn.
arguments:
mf - string containing type of Membership function
x - x axis values
"""
if mf == "zmf":
return fuzz.zmf(x, abcd[0], abcd[1]) # zmf(x, a, b)
elif mf == "trimf":
return fuzz.trimf(x, abcd[0:3]) # trimf(x, abc)
elif mf == "dsigmf":
return fuzz.dsigmf(x, abcd[0], abcd[1], abcd[2], abcd[3]) # dsigmf(x, b1, c1, b2, c2)
elif mf == "gauss2mf":
return fuzz.gauss2mf(x, abcd[0], abcd[1], abcd[2], abcd[3]) # gauss2mf(x, mean1, sigma1, mean2, sigma2)
elif mf == "gaussmf":
return fuzz.gaussmf(x, abcd[0], abcd[1]) # gaussmf(x, mean, sigma)
elif mf == "gbellmf":
return fuzz.gbellmf(x, abcd[0], abcd[1], abcd[2]) # gbellmf(x, a, b, c)
elif mf == "piecemf":
return fuzz.piecemf(x, abcd[0:3]) # piecemf(x, abc)
elif mf == "pimf":
return fuzz.pimf(x, abcd[0], abcd[1], abcd[2], abcd[3]) # pimf(x, a, b, c, d)
elif mf == "psigmf":
return fuzz.psigmf(x, abcd[0], abcd[1], abcd[2], abcd[3]) # psigmf(x, b1, c1, b2, c2)
elif mf == "sigmf":
return fuzz.sigmf(x, abcd[0], abcd[1]) # sigmf(x, b, c)
elif mf == "smf":
return fuzz.smf(x, abcd[0], abcd[1]) # smf(x, a, b)
elif mf == "trapmf":
return fuzz.trapmf(x, abcd) # trapmf(x, abcd)
示例4: rangeToMF
def rangeToMF(range, type):
"""
Translate the range into a list of x_values and an MF function.
range : list/tuple
range to translate to MF
type : string
type of MF:
'sing' - singleton MF
'gauss' - gaussian MF function (mean, standard deviation)
'tri' - triangular MF function
'trap' - trapezoidal MF function
"""
c = 100 #number of x points
minR = float(min(range))
maxR = float(max(range))
if type == 'sing': #singleton
c = 10 #special short mf for singleton
if maxR == minR:
#print "SAME R"
ran = max(abs(0.15*minR),0.0001)
xrange = [minR-ran, minR+ran, ran/c]
Xs = [minR-2.*ran, minR-1.*ran, minR, minR+1.*ran, minR+2.*ran,]
Ys = [0.0, 0.0, 1.0, 0.0, 0.0]
else:
ran = abs(maxR-minR)
xrange = [minR, maxR, ran/c]
Xs, Ys = singleton_to_fuzzy(sum(range)/len(range), xrange)
elif type == 'gauss': #gaussian MF
std_range = (1./4.) #0.25
if minR == maxR: ran = max(0.0001,abs(0.05*minR))#0.05
else: ran = abs(maxR - minR) #check for min=max and get range
Xs = np.arange(minR - 0.5*ran, maxR + 0.5*ran, 2*ran/c)
Ys = fuzz.gaussmf(Xs, sum(range)/len(range), std_range*(ran)) #gaussian mf with 4sigma = range (to 97.7% "certainty")
elif type == 'tri': #triangular MF
if minR == maxR:
ran = max(abs(0.2*minR),0.001)
xrange = [0.9*minR, 1.1*maxR, ran/c,]
Xs, Ys = singleton_to_fuzzy(sum(range)/len(range), xrange)
else:
Xs = np.arange(0.9*minR, 1.1*maxR, (1.1*maxR-0.9*minR)/c) #create fuzzy MF for output
Ys = fuzz.trimf(Xs, [minR, sum(range)/len(range), maxR])
elif type == 'trap': #trapezoidal MF
if minR == maxR:
ran = max(abs(0.2*minR),0.001)
xrange = [0.9*minR, 1.1*maxR, ran/c,]
Xs, Ys = singleton_to_fuzzy(sum(range)/len(range), xrange)
else:
Xs = np.arange(0.9*minR, 1.1*maxR, (1.1*maxR-0.9*minR)/c) #create fuzzy MF for output
Ys = fuzz.trapmf(Xs, [minR, minR, maxR, maxR])
else:
raise StandardError("Unknown type of membership function: %s" % type)
return [np.asarray(Xs), np.asarray(Ys)] #create MF
示例5: __init__
def __init__(self):
# Generate universe functions
self.distance = np.arange(0.,181.,1.)
self.acceleration = np.arange(0.,0.1,0.01)
# Generate Distance membership functions
self.near = fuzz.trapmf(self.distance, (-1.,-1.,20.,65.))
self.medium = fuzz.trapmf(self.distance,(35.,80.,120.,135.))
self.far = fuzz.trapmf(self.distance,(105.,170.,180.,200.))
# Generate Acceleration membership functions
self.slow = fuzz.trimf(self.acceleration, (-1.,0.,0.05))
self.normal = fuzz.trapmf(self.acceleration,(0.02,0.035,0.04,0.07))
self.fast = fuzz.trapmf(self.acceleration,(0.06,0.085,0.1,0.2))
# Fuzzy relation
self.R1 = fuzz.relation_product(self.near,self.slow)
self.R2 = fuzz.relation_product(self.medium,self.normal)
self.R3 = fuzz.relation_product(self.far,self.fast)
# Combine the fuzzy relation
self.R_combined = np.fmax(self.R1, np.fmax(self.R2, self.R3))
self.thetaOne = 0.0
self.thetaTwo = 0.0
self.InputDistanceAngle = 0.0
self.OutputAcceleration = 0.0
self.visualize = True
示例6: test_tipping_problem
def test_tipping_problem():
# The full tipping problem uses many of these methods
food = Antecedent(np.linspace(0, 10, 11), 'quality')
service = Antecedent(np.linspace(0, 10, 11), 'service')
tip = Consequent(np.linspace(0, 25, 26), 'tip')
food.automf(3)
service.automf(3)
# Manual membership function definition
tip['bad'] = trimf(tip.universe, [0, 0, 13])
tip['middling'] = trimf(tip.universe, [0, 13, 25])
tip['lots'] = trimf(tip.universe, [13, 25, 25])
# Define fuzzy rules
rule1 = Rule(food['poor'] | service['poor'], tip['bad'])
rule2 = Rule(service['average'], tip['middling'])
rule3 = Rule(service['good'] | food['good'], tip['lots'])
# The control system - defined both possible ways
tipping = ControlSystem([rule1, rule2, rule3])
tipping2 = ControlSystem()
tipping2.addrule(rule2)
tipping2.addrule(rule3)
tipping2.addrule(rule1)
tip_sim = ControlSystemSimulation(tipping)
tip_sim2 = ControlSystemSimulation(tipping2)
# Inputs added both possible ways
inputs = {'quality': 6.5, 'service': 9.8}
for key, value in inputs.items():
tip_sim.input[key] = value
tip_sim2.inputs(inputs)
# Compute the system
tip_sim.compute()
tip_sim2.compute()
assert tip_sim.output == tip_sim2.output
tst.assert_allclose(tip_sim.output['tip'], 20.244508118433625)
示例7: test_lambda_cut_series
def test_lambda_cut_series():
x = np.arange(21) - 10
mfx = fuzz.trimf(x, [-2, 3, 5])
expected = np.array([[ 0. , -2., 5.],
[ 0.25, 0., 4.],
[ 0.5 , 1., 4.],
[ 0.75, 2., 3.],
[ 1. , 3., 3.]])
assert_allclose(expected, fuzz.lambda_cut_series(x, mfx, 5))
示例8: __init__
def __init__(self):
# Generate universe variables
distance = ctrl.Antecedent(np.arange(-440, 441, 1), 'distance') # paddle.x - ball.x
paddle_speed = ctrl.Consequent(np.arange(-12,13,4), 'speed') # paddle.x +=paddle_speed
# Auto-membership function population is possible with .automf(3, 5, or 7)
# Generate fuzzy membership functions
distance['far right'] = fuzz.trimf(distance.universe, [-440, -250, -110])
distance['close right'] = fuzz.trimf(distance.universe, [-200, -10, 0])
distance['close left'] = fuzz.trimf(distance.universe, [0, 10, 200])
distance['far left'] = fuzz.trimf(distance.universe, [200, 440, 440])
paddle_speed.automf(7)
rule1 = ctrl.Rule(distance['far left'], paddle_speed['dismal'])
rule2 = ctrl.Rule(distance['close left'], paddle_speed['dismal'])
rule3 = ctrl.Rule(distance['close right'], paddle_speed['excellent'])
rule4 = ctrl.Rule(distance['far right'], paddle_speed['excellent'])
# rule5 = ctrl.Rule(distance['above'], paddle_speed['average'])
paddle_ctrl = ctrl.ControlSystem([rule1, rule2, rule3, rule4])
self.agent = ctrl.ControlSystemSimulation(paddle_ctrl)
示例9: __init__
def __init__(self):
# Generate universe variables
distance = ctrl.Antecedent(np.arange(-8, 9, 1), 'distance')
trajectory = ctrl.Consequent(np.arange(-2, 3, 1), 'trajectory')
# Auto-membership function population is possible with .automf(3, 5, or 7)
distance.automf(3)
# Custom membership functions can be built interactively with a familiar,
# Pythonic API
trajectory['low'] = fuzz.trimf(trajectory.universe, [-2, -2, -2])
trajectory['medium'] = fuzz.trimf(trajectory.universe, [0, 0, 2])
trajectory['high'] = fuzz.trimf(trajectory.universe, [0, 2, 2])
rule1 = ctrl.Rule(distance['poor'], trajectory['low'])
rule2 = ctrl.Rule(distance['average'], trajectory['medium'])
rule3 = ctrl.Rule(distance['good'], trajectory['high'])
# Pass inputs to the ControlSystem using Antecedent labels with Pythonic API
# Note: if you like passing many inputs all at once, use .inputs(dict_of_data)
trajectoryping_ctrl = ctrl.ControlSystem([rule1, rule2, rule3])
self.agent = ctrl.ControlSystemSimulation(trajectoryping_ctrl)
示例10: test_lambda_cut
def test_lambda_cut():
x = np.arange(21) - 10
mfx = fuzz.trimf(x, [-2, 3, 5])
# fuzz.lambda_cut test
expected = np.r_[0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0]
result = fuzz.lambda_cut(mfx, 0.33)
assert_allclose(expected, result)
# fuzz.arglcut test
expected = np.r_[10, 11, 12, 13, 14]
result = fuzz.arglcut(mfx, 0.33)
assert len(result) == 1
assert_allclose(expected, result[0])
示例11: membership_f
def membership_f(mf, x, abc = [0,0,0], a = 1, b = 2, c = 3, d = 4, abcd = [0,0,0,0]):
return {
'trimf' : fuzz.trimf(x, abc), # trimf(x, abc)
'dsigmf' : fuzz.dsigmf(x, a, b, c, d), # dsigmf(x, b1, c1, b2, c2)
'gauss2mf': fuzz.gauss2mf(x, a, b, c, d), # gauss2mf(x, mean1, sigma1, mean2, sigma2)
'gaussmf' : fuzz.gaussmf(x, a, b), # gaussmf(x, mean, sigma)
'gbellmf' : fuzz.gbellmf(x, a, b, c), # gbellmf(x, a, b, c)
'piecemf' : fuzz.piecemf(x, abc), # piecemf(x, abc)
'pimf' : fuzz.pimf(x, a, b, c, d), # pimf(x, a, b, c, d)
'psigmf' : fuzz.psigmf(x, a, b, c, d), # psigmf(x, b1, c1, b2, c2)
'sigmf' : fuzz.sigmf(x, a, b), # sigmf(x, b, c)
'smf' : fuzz.smf(x, a, b), # smf(x, a, b)
'trapmf' : fuzz.trapmf(x, abcd), # trapmf(x, abcd)
'zmf' : fuzz.zmf(x, a, b), # zmf(x, a, b)
}[mf]
示例12: throttle_command
def throttle_command(speed_read):
# Universe of discourse
speed = np.arange(60,150,.1)
throttle_value = np.arange(0,1,0.01)
# Input membership functions
too_slow = fuzz.trapmf(speed,[60,60,80,85])
slow = fuzz.trimf(speed,[83,90,97])
cruise = fuzz.trimf(speed,[95,100,105])
fast = fuzz.trimf(speed,[103,110,117])
too_fast = fuzz.trapmf(speed,[115,120,150,150])
# Output membership functions
very_low = fuzz.trimf(throttle_value,[0,0,0.15])
low = fuzz.trimf(throttle_value,[0.1,0.25,0.4])
medium = fuzz.trimf(throttle_value,[0.35,0.5,0.65])
high = fuzz.trimf(throttle_value,[0.6,0.75,0.9])
very_high = fuzz.trimf(throttle_value,[0.85,1,1])
# membership values
speed_cat_too_slow = fuzz.interp_membership(speed,too_slow,speed_read)
speed_cat_slow = fuzz.interp_membership(speed,slow,speed_read)
speed_cat_cruise = fuzz.interp_membership(speed,cruise,speed_read)
speed_cat_fast = fuzz.interp_membership(speed,fast,speed_read)
speed_cat_too_fast = fuzz.interp_membership(speed,too_fast,speed_read)
# If part of rules
rule1 = speed_cat_too_slow
rule2 = speed_cat_slow
rule3 = speed_cat_cruise
rule4 = speed_cat_fast
rule5 = speed_cat_too_fast
# Then part of rules
imp1 = np.fmin(rule1,very_high)
imp2 = np.fmin(rule2,high)
imp3 = np.fmin(rule3,medium)
imp4 = np.fmin(rule4,low)
imp5 = np.fmin(rule5,very_low)
aggregate_membership = np.fmax(imp1,np.fmax(imp2,np.fmax(imp3,np.fmax(imp4,imp5))))
return fuzz.defuzz(throttle_value,aggregate_membership,'centroid')
示例13: test_bisector
def test_bisector():
x = np.arange(6)
mfx = fuzz.trimf(x, [0, 5, 5])
expected = 3.53553390593274
# Test both triangle code paths
assert_allclose(expected, fuzz.defuzz(x, mfx, 'bisector'))
assert_allclose(5 - expected, fuzz.defuzz(x, 1 - mfx, 'bisector'))
# Test singleton input
y = np.r_[2]
mfy = np.r_[0.33]
assert_allclose(y, fuzz.defuzz(y, mfy, 'bisector'))
# Test rectangle code path
mfx = fuzz.trapmf(x, [2, 2, 4, 4])
assert_allclose(3., fuzz.defuzz(x, mfx, 'bisector'))
示例14: load_rules
def load_rules(self, filename):
with open(filename, 'r') as jsonfile:
rules = json.loads(jsonfile.read())
self.actions = rules['actions']
self.state = rules['state']
self.rules = rules['results']
self.x = np.arange(self.state['min'], self.state['max'])
for subset, parts in self.rules.items():
aggregated = []
points = []
for n, v in parts.items():
print n
if v['mf'] == 'tri':
mf = fuzz.trimf(self.x, v['shp'])
if v['mf'] == 'trap':
mf = fuzz.trapmf(self.x, v['shp'])
elif v['mf'] == 'gbell':
mf = fuzz.gbellmf(self.x, v['shp'])
aggregated.append(mf)
points.append(v['pts'])
self.rules[subset] = (aggregated, points)
示例15: membership_f
def membership_f(mf, x, abc=[0, 0, 0], a=1, b=2, c=3, d=4, abcd=[0, 0, 0, 0]):
"""
Returns y values corresponding to type of type of Membership fn.
arguments:
mf - string containing type of Membership function
x - x axis values
abc - list containing triangular edge point x-values
"""
return {
"trimf": fuzz.trimf(x, abc), # trimf(x, abc)
"dsigmf": fuzz.dsigmf(x, a, b, c, d), # dsigmf(x, b1, c1, b2, c2)
"gauss2mf": fuzz.gauss2mf(x, a, b, c, d), # gauss2mf(x, mean1, sigma1, mean2, sigma2)
"gaussmf": fuzz.gaussmf(x, a, b), # gaussmf(x, mean, sigma)
"gbellmf": fuzz.gbellmf(x, a, b, c), # gbellmf(x, a, b, c)
"piecemf": fuzz.piecemf(x, abc), # piecemf(x, abc)
"pimf": fuzz.pimf(x, a, b, c, d), # pimf(x, a, b, c, d)
"psigmf": fuzz.psigmf(x, a, b, c, d), # psigmf(x, b1, c1, b2, c2)
"sigmf": fuzz.sigmf(x, a, b), # sigmf(x, b, c)
"smf": fuzz.smf(x, a, b), # smf(x, a, b)
"trapmf": fuzz.trapmf(x, abcd), # trapmf(x, abcd)
"zmf": fuzz.zmf(x, a, b), # zmf(x, a, b)
}[mf]