def fuzErrorAC(dataF, outF, acuts=11): #test fuzzy error function based on interval math on alpha cuts #get fuzzy sets for outF (x,y) and dataF (x,y) and get error using acuts # of cuts #dataF - #outF - #acuts - number of alpha cuts to compare system at (default = 11) - ac = np.array(range(acuts))/float(acuts-1) #set alpha cuts #get lambda cuts for data and output dataLC = [fuzz.lambda_cut(dataF[1], c) for c in ac] outLC = [fuzz.lambda_cut(outF[1], c) for c in ac] #get alpha cut intervals for each MF from lambda cuts dataACi = [] outACi = [] for i in range(len(dataLC)): dataACi.append( [j for j, x in enumerate(dataLC[i]) if x == 1]) for i in range(len(outLC)): outACi.append( [j for j, x in enumerate(outLC[i]) if x == 1]) dataAC = [] outAC = [] for i in range(len(dataACi)): if len(dataACi[i]) > 0: dataAC.append([ dataF[0][min(dataACi[i])], dataF[0][max(dataACi[i])] ]) else: dataAC.append([]) for i in range(len(outACi)): if len(outACi[i]) > 0: outAC.append([ outF[0][min(outACi[i])], outF[0][max(outACi[i])] ]) else: outAC.append([]) #get differences in intervals error_ints = [] for i in range(len(dataAC)): if len(dataAC[i]) > 0 and len(outAC[i]) > 0: error_ints.append([ min(dataAC[i]) - min(outAC[i]), max(dataAC[i]) - max(outAC[i]) ]) #get the position error (do the fuzzy sets line up) if len([sum(err)/len(err) for err in error_ints]) > 0.0: pos_error = sum([sum(err)/len(err) for err in error_ints])/ \ len([sum(err)/len(err) for err in error_ints]) else: pos_error = 100.0 #get the spread error (do the sets have the same MF shape) if len([(err[0])+(err[1]) for err in error_ints]) > 0.0: spread_error = sum([(err[0])+(err[1]) for err in error_ints])/ \ len([(err[0])+(err[1]) for err in error_ints]) else: spread_error = 100.0 pos_sign = 1 if pos_error < 0: pos_sign = -1 #get sign of error return pos_error, spread_error
def alpha_at_val(x,y,alpha=None): """ Takes in a fuzzy membership function, determines, the maximum membership degree, and then returns the alpha-cut interval at that maximum ------- INPUTS ------ x : list/array x values for membership function y : list/array y values for membership function alpha : float alpha value to find alpha-cut at (If None, uses max of MF) """ try: if alpha == None: alpha = 0.999*max(y) #get maximum membership lc = fuzz.lambda_cut(y, alpha) #get lamda cut mm = [x[i] for i in range(len(lc)) if lc[i] == 1] #get x values for alpha cut if not isinstance(mm, list): mm = [mm] if len(mm) > 0: #get range of cut return [ min(mm), max(mm)] elif alpha == None: #catch for single peak value y = list(y) imax = y.index(max(y)) print imax return [x[imax], x[imax]] else: #if cut is empty return [0, 0] except: return [None, None]
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])