def test1(): test_name = 'SYS: In:7-2 Out:9-2 DATA: In:3 Out:3' print "*************************************" print "TESTING: ", test_name inMFs = input_7gaussMFs #system in outMFs = output_9gaussMFs defuzz = None outForm = 'tri' inDataForm = 'tri' outDataForm = 'tri' errType = 'fuzzy' input_arrays, output_arrays = generate_MFs(inMFs, outMFs) inputMFs = { ('VL_SYS_UNION', 'phi'): copy.deepcopy(input_arrays['phi']),('FWD_SYS_UNION', 'phi'): copy.deepcopy(input_arrays['phi']), ('WING_SYS_TYPE', 'phi'): copy.deepcopy(input_arrays['phi']),('ENG_SYS_TYPE', 'phi'): copy.deepcopy(input_arrays['phi']), ('VL_SYS_UNION', 'w'): copy.deepcopy(input_arrays['w']),('VL_SYS_TYPE', 'TP'): copy.deepcopy(input_arrays['TP']),('WING_SYS_TYPE', 'WS'): copy.deepcopy(input_arrays['WS']),('VL_SYS_PROP', 'sigma'): copy.deepcopy(input_arrays['sigma']), ('VL_SYS_TYPE', 'e_d'): copy.deepcopy(input_arrays['e_d']),('VL_SYS_DRV', 'eta_d'): copy.deepcopy(input_arrays['eta_d']),('FWD_SYS_DRV', 'eta_d'): copy.deepcopy(input_arrays['eta_d']),('FWD_SYS_PROP', 'eta_p'): copy.deepcopy(input_arrays['eta_p']), } inputPARAMs = { ('VL_SYS_UNION', 'phi'): copy.deepcopy(inMFs['phi']), ('FWD_SYS_UNION', 'phi'): copy.deepcopy(inMFs['phi']),('WING_SYS_TYPE', 'phi'): copy.deepcopy(inMFs['phi']),('ENG_SYS_TYPE', 'phi'): copy.deepcopy(inMFs['phi']), ('VL_SYS_UNION', 'w'): copy.deepcopy(inMFs['w']), ('VL_SYS_TYPE', 'TP'): copy.deepcopy(inMFs['TP']),('WING_SYS_TYPE', 'WS'): copy.deepcopy(inMFs['WS']), ('VL_SYS_PROP', 'sigma'): copy.deepcopy(inMFs['sigma']), ('VL_SYS_TYPE', 'e_d'): copy.deepcopy(inMFs['e_d']),('VL_SYS_DRV', 'eta_d'): copy.deepcopy(inMFs['eta_d']),('FWD_SYS_DRV', 'eta_d'): copy.deepcopy(inMFs['eta_d']),('FWD_SYS_PROP', 'eta_p'): copy.deepcopy(inMFs['eta_p']), } outputMFs = {'sys_phi' : copy.deepcopy(output_arrays['sys_phi'])} outputPARAMs = {'sys_phi' : copy.deepcopy(outMFs['sys_phi'])} combinedData = copy.deepcopy(data) print combinedData[0] #generate rules with Timer() as t: rule_grid = train_system(inputMFs, outputMFs, combinedData, inDataMFs=inDataForm, outDataMFs=outDataForm, ruleMethod=3) #write out FCL write_fcl_file_FRBS(inputPARAMs, outputPARAMs, rule_grid, defuzz, 'test_sys_phi.fcl') #get system inputs, outputs, rulebase, AND_operator, OR_operator, aggregator, implication, \ defuzz = build_fuzz_system('test_sys_phi.fcl') sys = Fuzzy_System(inputs, outputs, rulebase, AND_operator, OR_operator, aggregator, implication, defuzz) print '=> ', t.secs, 'secs to build', len(sys.rulebase), 'rules' #test system with Timer() as t: error = getError(combinedData, sys, inMF=inDataForm, outMF=outDataForm, sysOutType=errType) print '=> ', t.secs, 'secs to check error' print 'Total System Error:', sum([err[2] for err in error]) print 'Mean Square System Error:', (1.0/len(error))*sum([err[2]**2 for err in error]) print 'Root Mean Square System Error:', ( (1.0/len(error)) * sum([err[2]**2 for err in error]) )**0.5 #actual vs. predicted plot plt.figure() plt.title('Actual vs. Predicted at Max Alpha Cut'+test_name) for err in error: if outDataForm == 'gauss': AC_actual = fuzzyOps.alpha_cut(0.8, [err[0][0],err[0][1]]) else: AC_actual = fuzzyOps.alpha_at_val(err[0][0],err[0][1]) if outForm == 'gauss': AC_pred = fuzzyOps.alpha_cut(0.8, (err[1][0],err[1][1])) else: AC_pred = fuzzyOps.alpha_at_val(err[1][0],err[1][1]) plt.scatter(AC_actual[0], AC_pred[0], marker='o', c='r') plt.scatter(AC_actual[1], AC_pred[1], marker='x', c='b') plt.plot([1,9],[1,9], '--k') plt.xlim([1,9]) plt.ylim([1,9]) plt.xlabel('Actual') plt.ylabel('Predicted') #visuzlize system with random data point #i = random.randrange(0, len(combinedData)) #inputs = {key[0]+"_"+key[1]:combinedData[i][0][key] for key in combinedData[i][0]} #sys.run(inputs, TESTMODE=True) #check random data points (9) plt.figure() plt.title('Random Tests:'+test_name) for j in range(9): i = random.randrange(0, len(combinedData)) inputs = {key[0]+"_"+key[1]:combinedData[i][0][key] for key in combinedData[i][0]} sysOut = sys.run(inputs) sysOut = sysOut[sysOut.keys()[0]] plt.subplot(3,3,j+1) plt.plot(sysOut[0], sysOut[1], '-r') plt.plot(combinedData[i][2][0], combinedData[i][2][1], '--k') plt.ylim([0,1.1]) plt.xlim([1,9]) #actual vs. error plot plt.figure() plt.title('Actual (Centroid) vs. Error'+test_name) cents = [fuzz.defuzz(err[0][0], err[0][1], 'centroid') for err in error] plt.scatter(cents, [err[2] for err in error]) plt.xlabel('Actual (Centroid)') plt.ylabel('Fuzzy Error')
#SYSTEM_f = fuzz.trapmf(SYSTEM_f_x, [f[0],f[0],f[1],f[1]]) #trap input MFS #SYSTEM_f = fuzz.trimf(SYSTEM_f_x, [f[0],0.5*(f[0]+f[1]),f[1]]) #tri input MFS SYSTEM_f = fuzzyOps.rangeToMF(f, 'trap') LoD = [min(alt[6]), max(alt[6])] #WING_LoD_x = np.arange(0.9*LoD[0], 1.1*LoD[1], (1.1*LoD[1]-0.8*LoD[0])/100.0) #WING_LoD = fuzz.trapmf(WING_LoD_x, [LoD[0],LoD[0],LoD[1],LoD[1]]) #trap input MFS #WING_LoD = fuzz.trimf(WING_LoD_x, [LoD[0],0.5*(LoD[0]+LoD[1]),LoD[1]]) #tri input MFS WING_LoD = fuzzyOps.rangeToMF(LoD, 'trap') output = sys.run({'SYSTEM_f': SYSTEM_f, 'WING_LoD': WING_LoD }, TESTMODE=True) output = output['sys_LoD'] #plot ranges from max alpha cuts outRange = fuzzyOps.alpha_at_val(output[0], output[1]) #calculate overlap overlap = 100.*float(min(max(outRange), max(alt[7])) - max(min(outRange), min(alt[7])))/ \ max( max(outRange)-min(outRange) , max(alt[7])-min(alt[7]) ) print alt[0], overlap, '% overlap' ax1.bar(outRange[0], 0.3, width=outRange[1]-outRange[0], bottom=ps_inputs.index(alt)+0.7, color='r') ax1.bar(alt[7][0], 0.3, width=alt[7][1]-alt[7][0], bottom=ps_inputs.index(alt)+1.0, color='b') ax1.text(max(max(outRange),max(alt[7]))+0.5, ps_inputs.index(alt)+0.9, str(round(overlap,1))+'%', fontsize=10) #plot actual outputs i = ps_inputs.index(alt) ax2[(i-(i%2))/2, i%2].plot(output[0], output[1], '-r') exp_x = np.arange(0.9*min(alt[7]), 1.1*max(alt[7]), (max(alt[7])-min(alt[7]))/50.)
SYSTEM_f = fuzzyOps.rangeToMF(f, 'trap') LoD = [min(alt[6]), max(alt[6])] #WING_LoD_x = np.arange(0.9*LoD[0], 1.1*LoD[1], (1.1*LoD[1]-0.8*LoD[0])/100.0) #WING_LoD = fuzz.trapmf(WING_LoD_x, [LoD[0],LoD[0],LoD[1],LoD[1]]) #trap input MFS #WING_LoD = fuzz.trimf(WING_LoD_x, [LoD[0],0.5*(LoD[0]+LoD[1]),LoD[1]]) #tri input MFS WING_LoD = fuzzyOps.rangeToMF(LoD, 'trap') output = sys.run({ 'SYSTEM_f': SYSTEM_f, 'WING_LoD': WING_LoD }, TESTMODE=True) output = output['sys_LoD'] #plot ranges from max alpha cuts outRange = fuzzyOps.alpha_at_val(output[0], output[1]) #calculate overlap overlap = 100.*float(min(max(outRange), max(alt[7])) - max(min(outRange), min(alt[7])))/ \ max( max(outRange)-min(outRange) , max(alt[7])-min(alt[7]) ) print alt[0], overlap, '% overlap' ax1.bar(outRange[0], 0.3, width=outRange[1] - outRange[0], bottom=ps_inputs.index(alt) + 0.7, color='r') ax1.bar(alt[7][0], 0.3, width=alt[7][1] - alt[7][0], bottom=ps_inputs.index(alt) + 1.0,
def testMFs(tData, vData, test_name, inMFs, outMFs, outForm, inDataForm, outDataForm, defuzz=None, sysOutType="fuzzy", errType="dist"): print "*************************************" print "TESTING: ", test_name print len(tData), "training points", len(vData), "validation points." #inMFs = input_5gaussMFs #system in #outMFs = output_7gaussMFs #defuzz = None #outForm = 'gauss' #inDataForm = 'gauss' #outDataForm = 'gauss' #errType = 'fuzzy' input_arrays, output_arrays = generate_MFs(inMFs, outMFs) inputMFs = { inp: copy.deepcopy(input_arrays[inp[1]]) for inp in inputList } inputPARAMs = {inp: copy.deepcopy(inMFs[inp[1]]) for inp in inputList } outputMFs = {'sys_FoM' : copy.deepcopy(output_arrays['sys_FoM'])} outputPARAMs = {'sys_FoM' : copy.deepcopy(outMFs['sys_FoM'])} combinedData = copy.deepcopy(tData) #generate rules with Timer() as t: rule_grid = train_system(inputMFs, outputMFs, combinedData, inDataMFs=inDataForm, outDataMFs=outDataForm) #write out FCL write_fcl_file_FRBS(inputPARAMs, outputPARAMs, rule_grid, defuzz, 'test_sys_fom.fcl') #get system inputs, outputs, rulebase, AND_operator, OR_operator, aggregator, implication, \ defuzz = build_fuzz_system('test_sys_fom.fcl') sys = Fuzzy_System(inputs, outputs, rulebase, AND_operator, OR_operator, aggregator, implication, defuzz) print '=> ', t.secs, 'secs to build', len(sys.rulebase), 'rules' #test system testData = copy.deepcopy(vData) with Timer() as t: error = getError(testData, sys, inMF=inDataForm, outMF=outDataForm, sysOutType=sysOutType, errType=errType) print '=> ', t.secs, 'secs to check error' print 'Total System Error:', sum([err[2] for err in error if err[2] <> None]) print 'Mean Square System Error:', (1.0/ len([err for err in error if err[2] <> None]) ) * \ sum([err[2]**2 for err in error if err[2] <> None]) print 'Root Mean Square System Error:', ( ( 1.0/len([err for err in error if err[2] <> None]) ) *sum([err[2]**2 for err in error if err[2] <> None]) )**0.5 #check random data points for time check = 15 t_tot = 0.0 for j in range(check): i = random.randrange(0, len(testData)) inputs = {key[0]+"_"+key[1]:testData[i][0][key] for key in testData[i][0]} with Timer() as t: sysOut = sys.run(inputs) t_tot = t_tot + float(t.secs) print 'Average system time of %d points => %.5f s' % (check, t_tot/check) print ' => %.5f s per rule' % ((t_tot/check)/len(sys.rulebase)) #actual vs. predicted plot alpha = 0.7 plt.figure() plt.title('Actual vs. Predicted at Max Alpha Cut'+test_name) for err in error: if outDataForm == 'gauss': AC_actual = fuzzyOps.alpha_cut(alpha, [err[0][0],err[0][1]]) else: AC_actual = fuzzyOps.alpha_at_val(err[0][0],err[0][1]) if outForm == 'gauss': AC_pred = fuzzyOps.alpha_cut(alpha, (err[1][0],err[1][1])) else: AC_pred = fuzzyOps.alpha_at_val(err[1][0],err[1][1]) if AC_actual <> None and AC_pred <> None: plt.scatter(AC_actual[0], AC_pred[0], marker='o', c='r') plt.scatter(AC_actual[1], AC_pred[1], marker='x', c='b') plt.plot([0,9],[0,9], '--k') plt.xlim([0.4,1.0]) plt.ylim([0.4, 1.0]) plt.xlabel('Actual') plt.ylabel('Predicted') #visuzlize system with random data point #i = random.randrange(0, len(combinedData)) #inputs = {key[0]+"_"+key[1]:combinedData[i][0][key] for key in combinedData[i][0]} #sys.run(inputs, TESTMODE=True) #check random data points for time """ plotData = copy.deepcopy(vData) plt.figure() plt.title('Random Tests:'+test_name) for j in range(9): i = random.randrange(0, len(plotData)) inputs = {key[0]+"_"+key[1]:plotData[i][0][key] for key in plotData[i][0]} sysOut = sys.run(inputs) sysOut = sysOut[sysOut.keys()[0]] plt.subplot(3,3,j+1) plt.plot(sysOut[0], sysOut[1], '-r') plt.plot(plotData[i][2][0], plotData[i][2][1], '--k') plt.ylim([0,1.1]) plt.xlim([1,9]) """ #actual vs. error plot plt.figure() plt.title('Actual (Centroid) vs. Error'+test_name) cents = [fuzz.defuzz(err[0][0], err[0][1], 'centroid') for err in error] plt.scatter(cents, [err[2] for err in error]) plt.xlabel('Actual (Centroid)') plt.ylabel('Fuzzy Error') plt.show() return len(sys.rulebase), (1.0/len(error))*sum([err[2]**2 for err in error])
def getRangeError(truthData, system, inMF='sing', outMF='sing'): """ Get a system's error against a set of truth data using Range outputs. (MUST BE SYSTEM WITH FUZZY OUTPUT, NOT CRISP). For crisp (sing) outMF, error is distance outside alpha-cut at maximum of system's output fuzzy membership fucntion. For fuzzy outMF (gauss, tri, trap), gets output alpha-cut range at alpha=1.0, and error is error function by fuzErrorAC. ------INPUTS------ truthData - list truth data to compare system output to in form: [Quant Input, Qual Input, Output] with inputs as {('input'): [values], ('input'): [values], ... } and outputs as [output value(s)] system : module instance of system to get error on !MUST BE FUZZY OUTPUT! inMF : string type of MF to use for input data ('sing', 'tri', 'trap', 'gauss') outMF : string type of MF to use for output data ('sing', 'tri', 'trap', 'gauss') sysOutType : string type of output to use from system ('crisp' or 'fuzzy') converts fuzzy outputs to crisp via 'centroid' defuzzification ------OUTPUTS------ error : list list of [truth[min,max], truth[min,max], error] """ q = 0 #use quantitative data error = [] #track error for output #Turn Data to MFs for point in truthData: #create input MFs for each input for inp in point[q]: if inMF == 'sing': #create singleton MF point[q][inp] = sum(point[q][inp])/len(point[q][inp]) #get mean of range (single value) elif inMF == 'tri': #create triangluar MF (min, avg, max) x_range = np.arange(point[q][inp][0]*0.9, point[q][inp][1]*1.1, (point[q][inp][1]*1.1 - point[q][inp][0]*0.9)/150) y_vals = fuzz.trimf(x_range, [point[q][inp][0], sum(point[q][inp])/len(point[q][inp]), point[q][inp][1]]) point[q][inp] = [x_range, y_vals] elif inMF == 'trap': #create traoeziodal MF (min, min, max, max) x_range = np.arange(point[q][inp][0]*0.9, point[q][inp][1]*1.1, (point[q][inp][1]*1.1 - point[q][inp][0]*0.9)/150) y_vals = fuzz.trimf(x_range, [dataIt[q][inp][0], point[q][inp][0], point[q][inp][0], point[q][inp][1]]) point[q][inp] = [x_range, y_vals] #create output MFs if outMF == 'sing': #create singleton MF point[2] = sum(point[2])/len(point[2]) #get average for singleton value elif outMF == 'tri': #create singleton MF x_range = np.arange(point[2][0]*0.9, point[2][1]*1.1, (point[2][1]*1.1 - point[2][0]*0.9)/150) y_vals = fuzz.trimf(x_range, [point[2][0], sum(point[2])/len(point[2]), point[2][1]]) point[2] = [x_range, y_vals] elif outMF == 'trap': #create singleton MF x_range = np.arange(point[2][0]*0.9, point[2][1]*1.1, (point[2][1]*1.1 - point[2][0]*0.9)/150) y_vals = fuzz.trimf(x_range, [point[2][0], point[2][0], point[2][1], point[2][1]]) point[2] = [x_range, y_vals] #Get system responses to data for point in truthData: inputs = {key[0]+"_"+key[1]:point[0][key] for key in point[0]} output = system.run(inputs) if isinstance(output, dict): output = output.itervalues().next() point.append(output) for point in truthData: #get error if not outMF=='sing': #if data is fuzzy err = fuzzy_error.fuzErrorAC(point[2], point[3]) truthRange = fuzzyOps.alpha_at_val(point[2][0], point[2][1], alpha=1.0) outRange = fuzzyOps.alpha_at_val(point[3], point[3]) else: outRange = fuzzyOps.alpha_at_val(point[3][0], point[3][1]) if None in outRange: err = None elif point[2] >= min(outRange) and point[2] <= max(outRange): err = 0 elif point[2] < min(outRange): err = point[2] - min(outRange) elif point[2] > max(outRange): err = point[2] - max(outRange) truthRange = point[2] error.append([truthRange, outRange, err]) return error
def testMFs(tData, vData, test_name, inMFs, outMFs, outForm, inDataForm, outDataForm, defuzz=None, sysOutType="fuzzy", errType="dist", plot=True): print "*************************************" print "TESTING: ", test_name print len(tData), "training points", len(vData), "validation points." #inMFs = input_5gaussMFs #system in #outMFs = output_7gaussMFs #defuzz = None #outForm = 'gauss' #inDataForm = 'gauss' #outDataForm = 'gauss' #errType = 'fuzzy' input_arrays, output_arrays = generate_MFs(inMFs, outMFs) inputMFs = { inp: copy.deepcopy(input_arrays[inp[1]]) for inp in inputList } inputPARAMs = {inp: copy.deepcopy(inMFs[inp[1]]) for inp in inputList } outputMFs = {'sys_phi' : copy.deepcopy(output_arrays['sys_phi'])} outputPARAMs = {'sys_phi' : copy.deepcopy(outMFs['sys_phi'])} combinedData = copy.deepcopy(tData) #generate rules with Timer() as t: rule_grid = train_system(inputMFs, outputMFs, combinedData, inDataMFs=inDataForm, outDataMFs=outDataForm) #write out FCL write_fcl_file_FRBS(inputPARAMs, outputPARAMs, rule_grid, defuzz, 'test_sys_phi.fcl') #get system inputs, outputs, rulebase, AND_operator, OR_operator, aggregator, implication, \ defuzz = build_fuzz_system('test_sys_phi.fcl') sys = Fuzzy_System(inputs, outputs, rulebase, AND_operator, OR_operator, aggregator, implication, defuzz) print '=> ', t.secs, 'secs to build', len(sys.rulebase), 'rules' #test system testData = copy.deepcopy(vData) with Timer() as t: error = getError(testData, sys, inMF=inDataForm, outMF=outDataForm, sysOutType=sysOutType, errType=errType) print '=> ', t.secs, 'secs to check error' print 'Total System Error:', sum([err[2] for err in error if err[2] <> None]) print 'Mean Square System Error:', sum([err[2]**2 for err in error if err[2] <> None]) / len([err for err in error if err[2] <> None]) print 'Root Mean Square System Error:', ( sum([err[2]**2 for err in error if err[2] <> None]) / len([err for err in error if err[2] <> None]) )**0.5 #check random data points for time check = 15 t_tot = 0.0 for j in range(check): i = random.randrange(0, len(testData)) inputs = {key[0]+"_"+key[1]:testData[i][0][key] for key in testData[i][0]} with Timer() as t: sysOut = sys.run(inputs) t_tot = t_tot + float(t.secs) print 'Average system time of %d points => %.5f s' % (check, t_tot/check) print ' => %.5f s per rule' % ((t_tot/check)/len(sys.rulebase)) #actual vs. predicted plot alpha = 0.7 plt.figure() plt.title('Actual vs. Predicted at Max Alpha Cut'+test_name) for err in error: if outDataForm == 'gauss': AC_actual = fuzzyOps.alpha_cut(alpha, [err[0][0],err[0][1]]) else: AC_actual = fuzzyOps.alpha_at_val(err[0][0],err[0][1]) if outForm == 'gauss': AC_pred = fuzzyOps.alpha_cut(alpha, (err[1][0],err[1][1])) else: AC_pred = fuzzyOps.alpha_at_val(err[1][0],err[1][1]) if AC_actual <> None and AC_pred <> None: plt.scatter(AC_actual[0], AC_pred[0], marker='o', c='r') plt.scatter(AC_actual[1], AC_pred[1], marker='x', c='b') plt.plot([1,9],[1,9], '--k') plt.xlim([1,9]) plt.ylim([1,9]) plt.xlabel('Actual') plt.ylabel('Predicted') #visuzlize system with random data point #i = random.randrange(0, len(combinedData)) #inputs = {key[0]+"_"+key[1]:combinedData[i][0][key] for key in combinedData[i][0]} #sys.run(inputs, TESTMODE=True) #check random data points for time """ plt.figure() plt.title('Random Tests:'+test_name) for j in range(9): i = random.randrange(0, len(combinedData)) inputs = {key[0]+"_"+key[1]:combinedData[i][0][key] for key in combinedData[i][0]} sysOut = sys.run(inputs) sysOut = sysOut[sysOut.keys()[0]] plt.subplot(3,3,j+1) plt.plot(sysOut[0], sysOut[1], '-r') plt.plot(combinedData[i][2][0], combinedData[i][2][1], '--k') plt.ylim([0,1.1]) plt.xlim([1,9]) """ #actual vs. error plot """ plt.figure() plt.title('Actual (Centroid) vs. Error'+test_name) cents = [fuzz.defuzz(err[0][0], err[0][1], 'centroid') for err in error] plt.scatter(cents, [err[2] for err in error]) plt.xlabel('Actual (Centroid)') plt.ylabel('Fuzzy Error') """ return len(sys.rulebase), sum([err[2]**2 for err in error if err[2] <> None]) / len([err for err in error if err[2] <> None])