Example #1
0
    def run(self):

            """ inputs[0] =  ERROR AXIS          ., so stores all possible error values
                inputs[1] =  DEL_ERROR AXIS      .,     ,,
                inputs[2] =  CONTROL_OUTPUT AXIS .,     ,,
                    
                ERROR                  DEL_ERROR               CONTROL_OUTPUT         m_value for crisp e and delta_e values

                b[0][0] -ve Medium  || b[1][0] -ve Medium  ||  b[2][0] -ve Medium   ..        f[0] |  f_d[0] 
                b[0][1] -ve small   || b[1][1] -ve small   ||  b[2][1] -ve small    ..        f[1] |  f_d[1]
                b[0][2] zero        || b[1][2] zero        ||  b[2][2] zero         ..        f[2] |  f_d[2]
                b[0][3] +ve small   || b[1][3] +ve small   ||  b[2][3] +ve small    ..        f[3] |  f_d[3]
                b[0][4] +ve Medium  || b[1][4] +ve Medium  ||  b[2][4] +_ve Medium  ..        f[4] |  f_d[4] 
                
                f_mat is fuzzy fuzzy_matrix
            """
            inputs = [ np.arange(var[0], var[1]+1, 1) for var in self.var_ranges] #step size  = 1, third dimension of b matrix. As of now, an assumption.
            b  = []
            output = [0,0,0,0,0]
            out_final = []
            for i in range(3) :
                    b.append( [membership_f(self.mu[i], inputs[i], a) for a in self.d_mu[i] ])
            # To visualize the membership func. call .. [ visualize_mf(b,inputs)  ]
            
            f ,f_d = error_fuzzify(inputs, b, self.error, self.delta_e)            
            f_mat = fuzzy_matrix(f,f_d)
            output = rule_base(b, f_mat, output)
            print 'output : ', output
            aggregated = np.fmax(output[0], np.fmax(output[1],np.fmax(output[2], np.fmax(output[3], output[4]))))
            out_final = fuzz.defuzz(inputs[2], aggregated, 'centroid')
            out_activation = fuzz.interp_membership(inputs[2], aggregated, out_final)  # for plot
            visualize.visualize_mf(b,inputs,output, out_final, out_activation, aggregated)
            visualize.visualize_output(b, inputs, out_final, out_activation, aggregated)
            plt.show()
Example #2
0
    def run(self):

        """ 
        Finds appropriate value of pid gains

        NO arguments : 

        inputs : List to contain discrete values of io variables in their range (step size = 1) for plotting. i.e, x axis
            inputs[0] =  ERROR AXIS          ., so stores all possible error values
            inputs[1] =  DEL_ERROR AXIS      .,     ,,
            inputs[2] =  CONTROL_OUTPUT AXIS .,     ,,
                
        b : 3d list, each layer (i.e, 2d list) contains 1d lists of y-values (in x of step size 1) of a particular fuzzy 
            subset of a particular i/o variable 
        
        muval_de, muval_e: Stores membership value of error and delta_error for each fuzzy subsets

            ERROR                  DEL_ERROR               CONTROL_OUTPUT         m_value for crisp e and delta_e values

            b[0][0] -ve Medium  || b[1][0] -ve Medium  ||  b[2][0] -ve Medium   ..        muval[0] |  muval_d[0] 
            b[0][1] -ve small   || b[1][1] -ve small   ||  b[2][1] -ve small    ..        muval[1] |  muval_d[1]
            b[0][2] zero        || b[1][2] zero        ||  b[2][2] zero         ..        muval[2] |  muval_d[2]
            b[0][3] +ve small   || b[1][3] +ve small   ||  b[2][3] +ve small    ..        muval[3] |  muval_d[3]
            b[0][4] +ve Medium  || b[1][4] +ve Medium  ||  b[2][4] +_ve Medium  ..        muval[4] |  muval_d[4] 
            
        f_mat is a 2d matrix containing rule strengths
        """
        inputs = [ np.arange(var[0], var[1]+1, 1) for var in self.io_ranges]
        b  = []
        for i in range(3) :
                b.append( [membership_f(self.mf_types[i], inputs[i], a) for a in self.f_ssets[i] ])

        # visualize.visualize_mf(b,inputs)
        # fuzzify Error and delta error to obtain their membership values for corr. fuzzy subsets
        muval_e  = fuzzify(inputs[0], b[0], self.error)
        muval_de = fuzzify(inputs[1], b[1], self.delta_e) 

        # print 'muval_e:', muval_e
        # print 'muval_de:', muval_de
        # Obtain the rule strength matrix
        f_mat = fuzzy_matrix(muval_e, muval_de)
        #  obtian the y value clipped by output activation for output fuzzy subsets
        output = rule_base(b, f_mat)
        aggregated = np.fmax(output[0], np.fmax(output[1],np.fmax(output[2], np.fmax(output[3], output[4]))))
        out_final  = fuzz.defuzz(inputs[2], aggregated, 'centroid')
        print "output:",out_final
        # plotting final output
        visualize.visualize_output(b, inputs, output, out_final, aggregated)
        plt.show()