def fitness(inputIDs):
     
     inputIDs = [int(round(x,0)) for x in inputIDs]
     print "Inputs:", inputIDs,
     
     if not 1 in inputIDs: return 9999.0
     
     inMFs = {}
     inPARAMs = {} 
     for i in range(len(input_list)):
         if inputIDs[i] == 1: 
             inMFs[input_list[i]] = inputMFs[input_list[i]]
             inPARAMs[input_list[i]] = inputPARAMs[input_list[i]]
         
     
     dataVal = copy.deepcopy(combDataVal)
     dataTrain = copy.deepcopy(combDataTrain)
     
     rule_grid = train_system(inMFs, outputMFs, dataTrain, inDataMFs=inDataForm, outDataMFs=outDataForm)
     write_fcl_file_FRBS(inPARAMs, outputPARAMs, rule_grid, None, 'opt_sys_phi.fcl')     #write out FCL
     inputs, outputs, rulebase, AND_operator, OR_operator, aggregator, implication, defuzz = build_fuzz_system('opt_sys_phi.fcl')    #get system
     sys = Fuzzy_System(inputs, outputs, rulebase, AND_operator, OR_operator, aggregator, implication, defuzz)
     #sys.run(None, TESTMODE=True)
     error = getError(dataVal, sys, inMF=inDataForm, outMF=outDataForm, sysOutType=errType)     #test system
     base_mse = (1.0/len(error))*sum([err[2]**2 for err in error if err <> None])
     print 'MSE:', base_mse
 
     return base_mse
Example #2
0
    def fitness(varList, penalty=True):  #, tBESTfit=tempBESTfit):
        defuzz = None
        #try:
        with Timer() as t:
            #translate varlist to MFs (MFs in list form)
            inMFs, outMFs = deparameterize(list(MFstructIn), list(MFstructOut),
                                           list(varList))

            #translate list MFs into MF functions (MF in array form)
            inMF_funcs, outMF_funcs = read_data.generate_MFs(inMFs, outMFs)

            #train system
            rule_grid = train_numerical.train_system(inMF_funcs,
                                                     outMF_funcs,
                                                     train_data,
                                                     inDataMFs=inMFform,
                                                     outDataMFs=outMFform,
                                                     maxDataN=trainMax)

            #write out FCL
            train_numerical.write_fcl_file_FRBS(inMFs, outMFs, rule_grid,
                                                defuzz, fclName)

            #build test system
            inputs, outputs, rulebase, AND_operator, OR_operator, aggregator, \
                implication, defuzz = build_fuzz_system(fclName)
            testSys = Fuzzy_System(inputs, outputs, rulebase, AND_operator, \
                OR_operator, aggregator, implication, defuzz)

            #test system
            error = getError(copy.deepcopy(val_data),
                             testSys,
                             inMF=inMFform,
                             outMF=outMFform,
                             sysOutType=errorType,
                             errType='dist')
            MSE = (1.0 / len([err for err in error if err[2] <> None])) * sum(
                [err[2]**2 for err in error if err[2] <> None])
            if penalty:
                k = float(len([e for e in error if e[2] == None])) / len(error)
                MSE = MSE * math.exp(
                    2.0 * k)  #penalty for lots of "Nones" from error function
            else:
                k = 0

        print " ==> system fitness %.4f in %.1f sec (k=%.2f)" % (MSE, t.secs,
                                                                 k)

        if MSE < tempBESTfit[0]:
            train_numerical.write_fcl_file_FRBS(
                inMFs, outMFs, rule_grid, defuzz,
                fclName[:-4] + '_tempBEST_' + str(MSE) + '.fcl')
            tempBESTfit[0] = MSE
            print "--- new temp best written ---"

        return MSE
Example #3
0
 def fitness(varList, penalty=True):#, tBESTfit=tempBESTfit):
     defuzz = None
     #try:
     with Timer() as t:
         #translate varlist to MFs (MFs in list form)
         inMFs, outMFs = deparameterize(list(MFstructIn), list(MFstructOut), list(varList))
         
         #translate list MFs into MF functions (MF in array form)
         inMF_funcs, outMF_funcs = read_data.generate_MFs(inMFs, outMFs)
         
         #train system
         rule_grid = train_numerical.train_system(inMF_funcs, outMF_funcs, train_data, 
                                                 inDataMFs=inMFform, outDataMFs=outMFform, 
                                                 maxDataN=trainMax)
         
         #write out FCL
         train_numerical.write_fcl_file_FRBS(inMFs, outMFs, rule_grid, defuzz, fclName)
         
         #build test system
         inputs, outputs, rulebase, AND_operator, OR_operator, aggregator, \
             implication, defuzz = build_fuzz_system(fclName)
         testSys = Fuzzy_System(inputs, outputs, rulebase, AND_operator, \
             OR_operator, aggregator, implication, defuzz)
         
         #test system
         error = getError(copy.deepcopy(val_data), testSys, inMF=inMFform, 
                             outMF=outMFform, sysOutType=errorType, errType='dist')
         MSE = (1.0/len([err for err in error if err[2] <> None]))*sum([err[2]**2 for err in error if err[2] <> None])
         if penalty:
             k = float(len([e for e in error if e[2] == None]))/len(error)
             MSE = MSE*math.exp(2.0*k) #penalty for lots of "Nones" from error function
         else: k = 0
         
     print " ==> system fitness %.4f in %.1f sec (k=%.2f)" % (MSE, t.secs,k)    
     
     if MSE < tempBESTfit[0]: 
         train_numerical.write_fcl_file_FRBS(inMFs, outMFs, rule_grid, defuzz, fclName[:-4]+'_tempBEST_'+str(MSE)+'.fcl')
         tempBESTfit[0] = MSE
         print "--- new temp best written ---"
         
     return MSE
Example #4
0
    def fitness(inputIDs):

        inputIDs = [int(round(x, 0)) for x in inputIDs]
        print "Inputs:", inputIDs,

        if not 1 in inputIDs: return 9999.0

        inMFs = {}
        inPARAMs = {}
        for i in range(len(input_list)):
            if inputIDs[i] == 1:
                inMFs[input_list[i]] = inputMFs[input_list[i]]
                inPARAMs[input_list[i]] = inputPARAMs[input_list[i]]

        dataVal = copy.deepcopy(combDataVal)
        dataTrain = copy.deepcopy(combDataTrain)

        rule_grid = train_system(inMFs,
                                 outputMFs,
                                 dataTrain,
                                 inDataMFs=inDataForm,
                                 outDataMFs=outDataForm)
        write_fcl_file_FRBS(inPARAMs, outputPARAMs, rule_grid, None,
                            'opt_sys_phi.fcl')  #write out FCL
        inputs, outputs, rulebase, AND_operator, OR_operator, aggregator, implication, defuzz = build_fuzz_system(
            'opt_sys_phi.fcl')  #get system
        sys = Fuzzy_System(inputs, outputs, rulebase, AND_operator,
                           OR_operator, aggregator, implication, defuzz)
        #sys.run(None, TESTMODE=True)
        error = getError(dataVal,
                         sys,
                         inMF=inDataForm,
                         outMF=outDataForm,
                         sysOutType=errType)  #test system
        base_mse = (1.0 / len(error)) * sum(
            [err[2]**2 for err in error if err <> None])
        print 'MSE:', base_mse

        return base_mse
Example #5
0
def test4():
    test_name = 'Full phis and majors Inputs'

    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_TYPE', 'phi'): copy.deepcopy(input_arrays['phi']),
        ('VL_SYS_PROP', 'phi'): copy.deepcopy(input_arrays['phi']),
        ('VL_SYS_DRV', 'phi'): copy.deepcopy(input_arrays['phi']),
        ('VL_SYS_TECH', 'phi'): copy.deepcopy(input_arrays['phi']),
        ('FWD_SYS_TYPE', 'phi'): copy.deepcopy(input_arrays['phi']),
        ('FWD_SYS_PROP', 'phi'): copy.deepcopy(input_arrays['phi']),
        ('FWD_SYS_DRV', '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']),
    }
    inputPARAMs = {
        ('VL_SYS_TYPE', 'phi'): copy.deepcopy(inMFs['phi']),
        ('VL_SYS_PROP', 'phi'): copy.deepcopy(inMFs['phi']),
        ('VL_SYS_DRV', 'phi'): copy.deepcopy(inMFs['phi']),
        ('VL_SYS_TECH', 'phi'): copy.deepcopy(inMFs['phi']),
        ('FWD_SYS_TYPE', 'phi'): copy.deepcopy(inMFs['phi']),
        ('FWD_SYS_PROP', 'phi'): copy.deepcopy(inMFs['phi']),
        ('FWD_SYS_DRV', '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']),
    }

    outputMFs = {'sys_phi': copy.deepcopy(output_arrays['sys_phi'])}
    outputPARAMs = {'sys_phi': copy.deepcopy(outMFs['sys_phi'])}

    combinedData = copy.deepcopy(combData)

    #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
    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')
Example #6
0
def input_test(baselineMSE=None, inMFlist=[]):
    test_name = 'input'
    print "*************************************"
    print "TESTING:  ", test_name
    inMFs = input_5gaussMFs  #system in
    outMFs = output_9gaussMFs
    defuzz = None

    outForm = 'gauss'
    inDataForm = 'gauss'
    outDataForm = 'gauss'
    outType = 'fuzzy'
    errCalc = 'dist'

    input_arrays, output_arrays = generate_MFs(inMFs, outMFs)
    inputMFs = {(d[1], d[3]): copy.deepcopy(input_arrays[d[3]])
                for d in dataIn}
    inputPARAMs = {(d[1], d[3]): copy.deepcopy(inMFs[d[3]]) for d in dataIn}
    outputMFs = {'sys_phi': copy.deepcopy(output_arrays['sys_phi'])}
    outputPARAMs = {'sys_phi': copy.deepcopy(outMFs['sys_phi'])}

    t_base = 0.05  #threshold for
    t_rem = 0.01  #threshold for removing a parameter (as percent of base MSE)

    #GET BASELINE ERROR:
    print "Getting baseline error for ", len(inputMFs), "inputs ... "
    #random.shuffle(combData)
    #

    #random.shuffle(combData)
    combDataVal = combData[:]
    print len(combDataVal), "data points for validation."
    #random.shuffle(combData)
    combDataTrain = combData[:]
    print len(combDataTrain), "data points for training."

    combinedData = copy.deepcopy(
        combDataTrain)  #copy data to preserve raw data
    valData = copy.deepcopy(combDataVal)

    #if starting from scratch
    if baselineMSE == None:
        rule_grid = train_system(inputMFs,
                                 outputMFs,
                                 combinedData,
                                 inDataMFs=inDataForm,
                                 outDataMFs=outDataForm)  #train rule grid
        write_fcl_file_FRBS(inputPARAMs, outputPARAMs, rule_grid, defuzz,
                            'test_sys_phi.fcl')  #write out FCL
        inputs, outputs, rulebase, AND_operator, OR_operator, aggregator, implication, defuzz = build_fuzz_system(
            'test_sys_phi.fcl')  #get system

        sys = Fuzzy_System(inputs, outputs, rulebase, AND_operator,
                           OR_operator, aggregator, implication, defuzz)

        error = getError(valData,
                         sys,
                         inMF=inDataForm,
                         outMF=outDataForm,
                         sysOutType=outType,
                         errType=errCalc)  #test system
        #print "TOTAL ERROR:", sum([x[2] for x in error])
        base_mse = (1.0 / len([err for err in error if err[2] <> None])) * sum(
            [err[2]**2 for err in error if err[2] <> None])

        for inp in copy.deepcopy(
                inputMFs
        ):  #for each input, check if all antecedents are same MF and remove
            input_antMFs = []
            for rule in rule_grid:
                for ant in rule:
                    if (ant[0],
                            ant[1]) == inp and (not ant[2] in input_antMFs):
                        input_antMFs.append(ant[2])

            if len(input_antMFs) == 1:
                print inp, "removed. No effect on consequent from antecedent MFs."
                inputMFs.pop(inp)
                inputPARAMs.pop(inp)
    #if already baselined
    else:
        base_mse = baselineMSE  #for

    if len(inMFlist) > 0:  #if inputs limited, reduce input list
        print "Using reduced input set..."
        for inp in copy.deepcopy(inputMFs):
            if not inp in inMFlist:
                inputMFs.pop(inp)
                inputPARAMs.pop(inp)

    print 'Baseline Mean Square System Error:', base_mse

    inputMFs_rem = {}  #store removed parameters
    inputPARAMs_rem = {}
    input_deltas = []  #track inputs and effects
    input_count = len(inputMFs)

    while True:
        print "_________________________________________________________________"
        print "CHECKING MODEL EFFECTS OF ALL INPUTS..."
        for inp in copy.deepcopy(
                inputMFs):  #for each input get delta error for input removeal

            combinedData = copy.deepcopy(
                combDataTrain)  #copy data to preserve raw data
            valData = copy.deepcopy(combDataVal)

            print "Checking Input", inp,
            #REMOVE INPUT:
            inputMFs_rem[inp] = inputMFs.pop(inp)
            inputPARAMs_rem[inp] = inputPARAMs.pop(inp)

            #GET NEW ERROR

            rule_grid = train_system(inputMFs,
                                     outputMFs,
                                     combinedData,
                                     inDataMFs=inDataForm,
                                     outDataMFs=outDataForm)  #train rule grid
            write_fcl_file_FRBS(inputPARAMs, outputPARAMs, rule_grid, defuzz,
                                'test_sys_phi.fcl')  #write out FCL
            inputs, outputs, rulebase, AND_operator, OR_operator, aggregator, implication, defuzz = build_fuzz_system(
                'test_sys_phi.fcl')  #get system
            sys1 = Fuzzy_System(inputs, outputs, rulebase, AND_operator,
                                OR_operator, aggregator, implication, defuzz)
            print len(sys1.inputs), 'remaining', len(rule_grid), 'rules.'

            error = getError(valData,
                             sys1,
                             inMF=inDataForm,
                             outMF=outDataForm,
                             sysOutType=outType,
                             errType=errCalc)  #test system
            #print "TOTAL ERROR:", sum([x[2] for x in error])
            new_mse = (1.0 / len([err
                                  for err in error if err[2] <> None])) * sum([
                                      err[2]**2
                                      for err in error if err[2] <> None
                                  ])
            del_mse = (new_mse - base_mse) / base_mse
            print '  ==> Adjusted MSE: %.4f; normalized delta_MSE: %.6f' % (
                new_mse, del_mse)
            input_deltas.append([inp, del_mse])

            #ADD INPUT BACK:
            inputMFs[inp] = inputMFs_rem.pop(inp)  #remove from removed lists
            inputPARAMs[inp] = inputPARAMs_rem.pop(inp)

        t_rem_ = copy.copy(t_rem)

        print "---------------------------------------"
        print "REMOVING INPUTS WITH LITTLE EFFECT..."
        while True:

            for pair in input_deltas:  #make new model without inputs below t_rem threshold
                if pair[1] < t_rem_:
                    inputMFs_rem[pair[0]] = inputMFs.pop(
                        pair[0])  #move inputs to removal list
                    inputPARAMs_rem[pair[0]] = inputPARAMs.pop(pair[0])
            print "%d inputs out of %d remaining at removal delta of %.4f" % (
                len(inputMFs), len(dataIn), t_rem_)

            combinedData = copy.deepcopy(
                combDataTrain)  #copy data to preserve raw data
            valData = copy.deepcopy(combDataVal)

            #GET NEW SYSTEM ERROR
            rule_grid = train_system(inputMFs,
                                     outputMFs,
                                     combinedData,
                                     inDataMFs=inDataForm,
                                     outDataMFs=outDataForm)
            write_fcl_file_FRBS(inputPARAMs, outputPARAMs, rule_grid, defuzz,
                                'test_sys_phi.fcl')  #write out FCL
            inputs, outputs, rulebase, AND_operator, OR_operator, aggregator, implication, \
                defuzz = build_fuzz_system('test_sys_phi.fcl')    #get system
            sys = Fuzzy_System(inputs, outputs, rulebase, AND_operator,
                               OR_operator, aggregator, implication, defuzz)
            print '%d inputs remaining %d rules at removal delta of %.4f' % (
                len(sys.inputs), len(rule_grid), t_rem_)

            error = getError(valData,
                             sys,
                             inMF=inDataForm,
                             outMF=outDataForm,
                             sysOutType=outType,
                             errType=errCalc)  #test system
            newbase_mse = (
                1.0 / len([err for err in error if err[2] <> None])) * sum(
                    [err[2]**2 for err in error if err[2] <> None])
            del_mse = (newbase_mse - base_mse) / base_mse
            print 'New System MSE: %.4f; normalized delta_MSE: %.4f' % (
                (1.0 / len(error)) * sum([err[2]**2
                                          for err in error]), del_mse)

            if (newbase_mse - base_mse) / base_mse < t_base or t_rem_ < 0.0001:
                break  #if model within t_base, break

            else:  #move all inputs back and adjust t_rem
                for inp in copy.deepcopy(inputMFs_rem):
                    inputMFs[inp] = inputMFs_rem.pop(inp)
                    inputPARAMs[inp] = inputPARAMs_rem.pop(inp)
                t_rem_ = t_rem_ - 0.5 * t_rem_

        print "%d inputs out of %d remaining:" % (len(inputMFs), len(dataIn))
        for inp in inputMFs:
            print inp
        if input_count == len(inputMFs):
            print "No inputs removed... COMPLETE!"
            break
        else:
            input_count = len(inputMFs)
            #base_mse = newbase_mse
            input_deltas = []  #track inputs and effects
            inputMFs_rem = {}  #clear out removed parameters
            inputPARAMs_rem = {}  #clear out removed parameters
            print "REEVALUATING"

    print "%d inputs out of %d3 remaining:" % (len(inputMFs), len(dataIn))
    for inp in inputMFs:
        print inp
def test5():
    test_name = 'Full phi Inputs'
    
    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_TYPE', 'phi'):         copy.deepcopy(input_arrays['phi']), 
                    ('VL_SYS_PROP', 'phi'):         copy.deepcopy(input_arrays['phi']), 
                    ('VL_SYS_DRV', 'phi'):          copy.deepcopy(input_arrays['phi']), 
                    ('VL_SYS_TECH', 'phi'):         copy.deepcopy(input_arrays['phi']),
                    ('FWD_SYS_TYPE', 'phi'):        copy.deepcopy(input_arrays['phi']), 
                    ('FWD_SYS_PROP', 'phi'):        copy.deepcopy(input_arrays['phi']), 
                    ('FWD_SYS_DRV', 'phi'):         copy.deepcopy(input_arrays['phi']),
                    ('WING_SYS_TYPE', 'phi'):       copy.deepcopy(input_arrays['phi']),
                    ('ENG_SYS_TYPE', 'phi'):        copy.deepcopy(input_arrays['phi']),                
                }
    inputPARAMs = { ('VL_SYS_TYPE', 'phi'):         copy.deepcopy(inMFs['phi']), 
                    ('VL_SYS_PROP', 'phi'):         copy.deepcopy(inMFs['phi']), 
                    ('VL_SYS_DRV', 'phi'):          copy.deepcopy(inMFs['phi']), 
                    ('VL_SYS_TECH', 'phi'):         copy.deepcopy(inMFs['phi']),
                    ('FWD_SYS_TYPE', 'phi'):        copy.deepcopy(inMFs['phi']), 
                    ('FWD_SYS_PROP', 'phi'):        copy.deepcopy(inMFs['phi']), 
                    ('FWD_SYS_DRV', 'phi'):         copy.deepcopy(inMFs['phi']),
                    ('WING_SYS_TYPE', 'phi'):       copy.deepcopy(inMFs['phi']),
                    ('ENG_SYS_TYPE', 'phi'):        copy.deepcopy(inMFs['phi']),  
                }
    
    outputMFs = {'sys_phi' : copy.deepcopy(output_arrays['sys_phi'])}
    outputPARAMs = {'sys_phi' : copy.deepcopy(outMFs['sys_phi'])}
    
    combinedData = copy.deepcopy(combData)
    
    #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
    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')       
def input_test(baselineMSE=None, inMFlist=[]):
    test_name = 'input'
    print "*************************************"
    print "TESTING:  ", test_name
    inMFs = input_5gaussMFs       #system in
    outMFs = output_9gaussMFs
    defuzz = None
    
    outForm = 'gauss'
    inDataForm = 'gauss'
    outDataForm = 'gauss'
    outType = 'fuzzy'
    errCalc='dist'
        
    input_arrays, output_arrays = generate_MFs(inMFs, outMFs)
    inputMFs    = { (d[1], d[3]): copy.deepcopy(input_arrays[d[3]]) for d in dataIn }
    inputPARAMs = { (d[1], d[3]): copy.deepcopy(inMFs[d[3]]) for d in dataIn }
    outputMFs = {'sys_phi' : copy.deepcopy(output_arrays['sys_phi'])}
    outputPARAMs = {'sys_phi' : copy.deepcopy(outMFs['sys_phi'])}
    
    t_base = 0.05 #threshold for 
    t_rem = 0.01 #threshold for removing a parameter (as percent of base MSE)

    #GET BASELINE ERROR:
    print "Getting baseline error for ", len(inputMFs), "inputs ... "
    #random.shuffle(combData)
    #
    
    #random.shuffle(combData)
    combDataVal = combData[:] 
    print len(combDataVal), "data points for validation."
    #random.shuffle(combData)
    combDataTrain = combData[:]
    print len(combDataTrain), "data points for training."
    
    combinedData = copy.deepcopy(combDataTrain) #copy data to preserve raw data
    valData = copy.deepcopy(combDataVal)
    
    #if starting from scratch
    if baselineMSE == None: 
        rule_grid = train_system(inputMFs, outputMFs, combinedData, inDataMFs=inDataForm, outDataMFs=outDataForm) #train rule grid
        write_fcl_file_FRBS(inputPARAMs, outputPARAMs, rule_grid, defuzz, 'test_sys_phi.fcl')     #write out FCL    
        inputs, outputs, rulebase, AND_operator, OR_operator, aggregator, implication, defuzz = build_fuzz_system('test_sys_phi.fcl')    #get system
        
        sys = Fuzzy_System(inputs, outputs, rulebase, AND_operator, OR_operator, aggregator, implication, defuzz)
        
        error = getError(valData, sys, inMF=inDataForm, outMF=outDataForm, sysOutType=outType, errType=errCalc)     #test system
        #print "TOTAL ERROR:", sum([x[2] for x in error])
        base_mse = (1.0/len([err for err in error if err[2] <> None]))*sum([err[2]**2 for err in error if err[2] <> None])
        
        for inp in copy.deepcopy(inputMFs): #for each input, check if all antecedents are same MF and remove
            input_antMFs = []
            for rule in rule_grid: 
                for ant in rule: 
                    if (ant[0], ant[1]) == inp and (not ant[2] in input_antMFs): 
                        input_antMFs.append(ant[2])
            
            if len(input_antMFs) == 1: 
                print inp, "removed. No effect on consequent from antecedent MFs."
                inputMFs.pop(inp)
                inputPARAMs.pop(inp)
    #if already baselined
    else: 
        base_mse = baselineMSE #for 
        
    if len(inMFlist) > 0: #if inputs limited, reduce input list
        print "Using reduced input set..."
        for inp in copy.deepcopy(inputMFs):
            if not inp in inMFlist:
                inputMFs.pop(inp)
                inputPARAMs.pop(inp)
    
    print 'Baseline Mean Square System Error:', base_mse

    inputMFs_rem = {} #store removed parameters
    inputPARAMs_rem = {}
    input_deltas = [] #track inputs and effects
    input_count = len(inputMFs)
    
    while True:
        print "_________________________________________________________________"
        print "CHECKING MODEL EFFECTS OF ALL INPUTS..."
        for inp in copy.deepcopy(inputMFs): #for each input get delta error for input removeal
                        
            combinedData = copy.deepcopy(combDataTrain) #copy data to preserve raw data
            valData = copy.deepcopy(combDataVal)
            
            print "Checking Input", inp, 
            #REMOVE INPUT:
            inputMFs_rem[inp] = inputMFs.pop(inp)
            inputPARAMs_rem[inp] = inputPARAMs.pop(inp)
                    
            #GET NEW ERROR
            
            rule_grid = train_system(inputMFs, outputMFs, combinedData, inDataMFs=inDataForm, outDataMFs=outDataForm) #train rule grid
            write_fcl_file_FRBS(inputPARAMs, outputPARAMs, rule_grid, defuzz, 'test_sys_phi.fcl')     #write out FCL
            inputs, outputs, rulebase, AND_operator, OR_operator, aggregator, implication, defuzz = build_fuzz_system('test_sys_phi.fcl')    #get system
            sys1 = Fuzzy_System(inputs, outputs, rulebase, AND_operator, OR_operator, aggregator, implication, defuzz)
            print len(sys1.inputs), 'remaining', len(rule_grid), 'rules.'
            
            error = getError(valData, sys1, inMF=inDataForm, outMF=outDataForm, sysOutType=outType, errType=errCalc)     #test system
            #print "TOTAL ERROR:", sum([x[2] for x in error])
            new_mse = (1.0/len([err for err in error if err[2] <> None]))*sum([err[2]**2 for err in error if err[2] <> None])
            del_mse = (new_mse - base_mse)/base_mse
            print '  ==> Adjusted MSE: %.4f; normalized delta_MSE: %.6f' % (new_mse, del_mse)
            input_deltas.append([inp, del_mse])
        
            #ADD INPUT BACK:
            inputMFs[inp] = inputMFs_rem.pop(inp) #remove from removed lists
            inputPARAMs[inp] = inputPARAMs_rem.pop(inp)
            
        t_rem_ = copy.copy(t_rem)
        
        print "---------------------------------------"
        print "REMOVING INPUTS WITH LITTLE EFFECT..."
        while True:
            
            for pair in input_deltas:         #make new model without inputs below t_rem threshold
                if pair[1] < t_rem_:
                    inputMFs_rem[pair[0]]    = inputMFs.pop(pair[0]) #move inputs to removal list
                    inputPARAMs_rem[pair[0]] = inputPARAMs.pop(pair[0])
            print "%d inputs out of %d remaining at removal delta of %.4f" % (len(inputMFs), len(dataIn), t_rem_)
            
            combinedData = copy.deepcopy(combDataTrain) #copy data to preserve raw data
            valData = copy.deepcopy(combDataVal)
                        
            #GET NEW SYSTEM ERROR
            rule_grid = train_system(inputMFs, outputMFs, combinedData, inDataMFs=inDataForm, outDataMFs=outDataForm)
            write_fcl_file_FRBS(inputPARAMs, outputPARAMs, rule_grid, defuzz, 'test_sys_phi.fcl')     #write out FCL
            inputs, outputs, rulebase, AND_operator, OR_operator, aggregator, implication, \
                defuzz = build_fuzz_system('test_sys_phi.fcl')    #get system
            sys = Fuzzy_System(inputs, outputs, rulebase, AND_operator, OR_operator, aggregator, implication, defuzz)
            print '%d inputs remaining %d rules at removal delta of %.4f' % (len(sys.inputs), len(rule_grid), t_rem_)
            
            error = getError(valData, sys, inMF=inDataForm, outMF=outDataForm, sysOutType=outType, errType=errCalc)     #test system
            newbase_mse = (1.0/len([err for err in error if err[2] <> None]))*sum([err[2]**2 for err in error if err[2] <> None])
            del_mse = (newbase_mse - base_mse)/base_mse
            print 'New System MSE: %.4f; normalized delta_MSE: %.4f' % ((1.0/len(error))*sum([err[2]**2 for err in error]), del_mse)
            
            
            if (newbase_mse - base_mse)/base_mse < t_base or t_rem_ < 0.0001: break #if model within t_base, break
            
            else: #move all inputs back and adjust t_rem
                for inp in copy.deepcopy(inputMFs_rem):
                    inputMFs[inp]    = inputMFs_rem.pop(inp) 
                    inputPARAMs[inp] = inputPARAMs_rem.pop(inp)
                t_rem_ = t_rem_ - 0.5*t_rem_
            
        print "%d inputs out of %d remaining:" % (len(inputMFs), len(dataIn))
        for inp in inputMFs: print inp
        if input_count == len(inputMFs):
            print "No inputs removed... COMPLETE!"
            break
        else:
            input_count = len(inputMFs)
            #base_mse = newbase_mse
            input_deltas = [] #track inputs and effects
            inputMFs_rem = {} #clear out removed parameters
            inputPARAMs_rem = {} #clear out removed parameters
            print "REEVALUATING"
        
    print "%d inputs out of %d3 remaining:" % (len(inputMFs), len(dataIn))
    for inp in inputMFs: print inp
Example #9
0
def test_NEFPROX_MackeyGlass():    
    t_max = 3000
    dt = 1.0
    
    beta = 0.2
    tau = 17
    n = 10.0
    gamma = 0.1
    
    t = [0.0]
    x = [1.2]
    
    while t[-1] < t_max:
        x_tau = x[-1*min(len(x), tau+1)]
        x_dot = beta*(x_tau/(1+x_tau**n)) - gamma*x[-1]
        x.append(x[-1] + x_dot*dt)
        t.append(t[-1]+dt)
        
    plt.figure()
    plt.plot( t[1118:2117], x[1118:2117] )
    plt.show()
        
    #collect data
    inputData = []
    for i in range(1118, 2117):
        i1 = {  ('DATA', 't_18'): [x[i-18]],
                ('DATA', 't_12'): [x[i-12]],
                ('DATA', 't_6'):  [x[i-6]],
                ('DATA', 't_0'):  [x[i-0]], }
        o1 = [x[i+6]]
        inputData.append([i1, None, o1])
    print 'min:', min([id[2][0] for id in inputData])
    print 'max:', max([id[2][0] for id in inputData])
    minx = min([id[2][0] for id in inputData])
    maxx = max([id[2][0] for id in inputData])
    
    n = 7 #number of MFs
    m = 3 #MF type (3,4)#
    
    half_width = (maxx-minx)/float(n-1)
    step_width = 2*half_width/(m-1)
    MFs = []
    for i in range(n):
        range_start = minx+(i-1)*half_width
        MFparams = [range_start + i*step_width for i in range(m)]
        MFs.append(MFparams)
    MFdict = {'A'+str(i): MFs[i] for i in range(len(MFs))}
    
    triInputMFs = { ('DATA_t_18'): copy.deepcopy(MFdict),
                    ('DATA_t_12'): copy.deepcopy(MFdict),
                    ('DATA_t_6'):  copy.deepcopy(MFdict),
                    ('DATA_t_0'):  copy.deepcopy(MFdict), }
                 
    triOutputMFs = {('t_plus_6'):  MFdict }
    
    inputMFs, outputMFs = generate_MFs(triInputMFs, triOutputMFs)
    
    #append MFparams (just for neuro-fuzzy systems)
    for inp in inputMFs:
        for ling in inputMFs[inp]: 
            inputMFs[inp][ling] = inputMFs[inp][ling] + [triInputMFs[inp][ling]]
    
    #append MFparams (just for neuro-fuzzy systems)
    for otp in outputMFs:
        for ling in outputMFs[otp]:
            outputMFs[otp][ling] = outputMFs[otp][ling] + [triOutputMFs[otp][ling]]
    
    #ranges for constraints
    inLimits = {k:[minx - half_width - 0.1*(maxx-minx), maxx + half_width + 0.1*(maxx-minx)] for k in inputMFs}
    outLimits = {k:[minx - half_width - 0.1*(maxx-minx), maxx + half_width + 0.1*(maxx-minx)] for k in outputMFs}
    
    NFS = NEFPROX({}, {}, [], 'centroid') #system built
    NFS, optData = train_NEFPROX(NFS, inputData[:700], inputData[700:], inputMFs, outputMFs, 
                                    inLimits, outLimits, sigma=0.0001, maxIterations=20)
    write_fcl_file_NFS(NFS, 'mackey_glass_test.fcl')
    
    #write report 
    f = open( "test_report.txt", 'w' )
    for k in vars(optData):
        f.write(str(k) + "=" + str(vars(optData)[k])+"\n")
    f.close()

    NFSoutput = []
    for x1 in inputData:
        inData = {inp[0]+'_'+inp[1]: sum(x1[0][inp])/len(x1[0][inp]) for inp in x1[0]}  
        output = NFS.run( inData )
        NFSoutput.append(output.itervalues().next())
    
    plt.figure()
    plt.plot(t[1118:2117], x[1118:2117])
    plt.plot(t[1118+6:2117+6], NFSoutput)
    plt.show()

    print "----- INPUT MFS -----"
    for mf in NFS.inputMFs:
        print mf, NFS.inputMFs[mf][2],
    print "----- OUTPUT MFS -----"
    for mf in NFS.outputMFs:
        print mf, NFS.outputMFs[mf][2],
    print "----- RULES -----"
    for rule in NFS.layer2:
        for inp in NFS.connect1to2[rule]: print inp,
        print NFS.connect2to3[rule].keys()

    NFS.plot()
    
    import fuzzy_error
    
    error = fuzzy_error.getError(copy.deepcopy(inputData[300:800]), NFS, inMF='sing', outMF='sing', sysOutType='crisp')
    error = sorted(error, key=lambda x: x[0])
    plt.figure()
    plt.scatter([x[0] for x in error],[x[1] for x in error])
    plt.plot([0.0,5.0],[0.0,5.0],'-r')
    plt.xlim([min([x[0] for x in error]),max([x[0] for x in error])])
    plt.ylim([min([x[0] for x in error]),max([x[0] for x in error])])
    
    NFS.defuzz = None
    error2 = fuzzy_error.getRangeError(copy.deepcopy(inputData[0:500]), NFS, inMF='sing', outMF='sing')
    plt.figure()
    for err in error2:
        plt.plot([err[0], err[0]], err[1], '-o', c='#666666', lw=1.5, ms=0.5)
    plt.plot([0.0,5.0],[0.0,5.0],'-r')
    plt.xlim([min([x[0] for x in error]),max([x[0] for x in error])])
    plt.ylim([min([x[0] for x in error]),max([x[0] for x in error])])
    plt.show()