Python自定义模糊隶属度函数构建规则咨询及automf报错排查
Hey there! Let's tackle your fuzzy logic issues head-on—you're hitting two common pain points: wonky plots from manual membership functions and that frustrating AssertionError: Total area is zero in defuzzification! error. Let's break down the hard rules for building valid custom membership functions, plus fix your code snippet along the way.
Core Rules for Custom Membership Functions
These rules apply to all standard functions like trimf (triangular), trapmf (trapezoidal), or even custom piecewise functions:
Parameter order must follow the function's logic
Fortrimf(triangular membership function), the three parameters[a, b, c]must satisfya ≤ b ≤ c. This defines the left edge (a, 0), peak (b, 1), and right edge (c, 0) of the triangle. Your code snippet has[20, 26, 3...]—that third parameter3is smaller than26, which breaks the order and creates that messy, nonsensical plot.All functions must collectively cover your universe of discourse
TheAssertionErrorhappens because there are input values in yourPP.universewhere all membership functions return 0. When you try to defuzzify, there's no area to calculate, hence the error. Make sure the union of your fuzzy sets covers the entire range of your input variable (e.g., if your temperature range is 0-50°C, your cold/warm/hot functions should span from 0 to 50 with no gaps).Align parameters with real-world meaning
Yourcoldfuzzy set should match intuitive logic. Ifcoldrefers to low temperatures, a validtrimfmight be[10, 15, 20](starts at 10°C, peaks at 15°C, fades out by 20°C). Your current[20,26,3...]doesn't make sense for "cold"—it's trying to create a triangle that peaks at 26 then drops to 3, which is physically backwards.Ensure reasonable overlap between adjacent sets
Adjacent fuzzy sets (like cold ↔ warm) should overlap slightly (usually 20-50% of their peak values). This creates smooth transitions in your fuzzy logic output. No overlap can lead to gaps (causing the zero-area error), while too much overlap can make your logic ambiguous.Validate output stays within [0, 1]
Fuzzy membership values must always be between 0 (no membership) and 1 (full membership). Misordered parameters or misaligned ranges can cause functions to spit out values outside this range, breaking downstream defuzzification.
Quick Fix for Your Code Snippet
Your broken cold set can be fixed by correcting the parameter order and aligning with real-world logic. For example:
# Assuming PP.universe is a temperature range like [0, 50] T22['cold'] = fuzz.trimf(PP.universe, [0, 10, 20]) # Cold: 0-20°C, peak at 10 T22['warm'] = fuzz.trimf(PP.universe, [15, 25, 35]) # Warm: 15-35°C, peak at 25 T22['hot'] = fuzz.trimf(PP.universe, [30, 40, 50]) # Hot: 30-50°C, peak at 40
This setup covers the full 0-50 range, has reasonable overlap, and follows the a ≤ b ≤ c rule for trimf.
Why automf() Might Have Failed
automf(n) generates n membership functions evenly across your universe. If you called automf(3,5,7) (passing multiple numbers), that's invalid syntax—automf only takes a single integer for the number of sets. Even with valid syntax, if your universe is misaligned with your fuzzy set definitions (e.g., universe is [0,100] but you only care about 0-50), automf might create sets that leave gaps in your relevant range.
内容的提问来源于stack exchange,提问作者Asim

