示例#1
0
def my_model_k1():
    """
    Initialization of the ANFIS regressor for k1 prediction. Single output.
    Fuzzy reasoning and rules can be modified according to human-expertise.
    :return:
    model: Initialized ANFIS
    """
    invardefs = [
        ('x0', make_gauss_mfs(0.5, np.linspace(0, 1, 2))),
        ('x1', make_gauss_mfs(0.5, np.linspace(0, 1, 4))),
        ('x2', make_gauss_mfs(0.5, np.linspace(0, 1, 3))),
        ('x3', make_gauss_mfs(0.5, np.linspace(0, 1, 3))),
        ('x4', make_gauss_mfs(0.5, np.linspace(0, 1, 6))),
        ('x5', make_gauss_mfs(0.5, np.linspace(0, 1, 3))),
        ('x6', make_gauss_mfs(0.5, np.linspace(0, 1, 6))),
        ('x7', make_gauss_mfs(0.5, np.linspace(0, 1, 5))),
        ('x8', make_gauss_mfs(0.5, np.linspace(0, 1, 5))),
        ('x9', make_gauss_mfs(0.5, np.linspace(0, 1, 7))),
    ]
    outvars = ['k1']
    model = anfis.AnfisNet('My_Anfis', invardefs, outvars, grid=False)

    rules = [[1, 1, 2, 2, 4, 1, 4, 0, 1, 6], [0, 0, 0, 0, 0, 0, 0, 3, 4, 2],
             [1, 3, 2, 1, 5, 2, 5, 2, 0, 0], [0, 0, 1, 1, 1, 0, 1, 1, 3, 1],
             [1, 3, 2, 2, 5, 2, 5, 0, 0, 3], [0, 2, 0, 0, 2, 1, 2, 4, 3, 4],
             [0, 2, 1, 1, 3, 1, 3, 1, 2, 4], [1, 1, 2, 1, 4, 1, 4, 2, 1, 5]]
    model.set_rules(rules)
    return model
示例#2
0
def my_model_class1():
    """
    Initialization of the ANFIS classifier for virtual generic model. Multi-output.
    Fuzzy reasoning and rules can be modified according to human-expertise.
    :return:
    model: Initialized ANFIS
    """
    invardefs = [('x0', make_gauss_mfs(0.5, np.linspace(0, 1, 5))),
                 ('x1', make_gauss_mfs(0.5, np.linspace(0, 1, 4))),
                 ('x2', make_gauss_mfs(0.5, np.linspace(0, 1, 3))),
                 ('x3', make_gauss_mfs(0.5, np.linspace(0, 1, 3))),
                 ('x4', make_gauss_mfs(0.5, np.linspace(0, 1, 7))),
                 ('x5', make_gauss_mfs(0.5, np.linspace(0, 1, 3))),
                 ('x6', make_gauss_mfs(0.5, np.linspace(0, 1, 5))),
                 ('x7', make_gauss_mfs(0.5, np.linspace(0, 1, 5))),
                 ('x8', make_gauss_mfs(0.5, np.linspace(0, 1, 2))),
                 ('x9', make_gauss_mfs(0.5, np.linspace(0, 1, 7)))]

    outvars = ['dc1', 'dc2', 'dc3', 'dc4', 'dc5', 'dc6', 'dc7', 'dc8']
    model = anfis.AnfisNet('My_Anfis',
                           invardefs,
                           outvars,
                           hybrid=False,
                           grid=False)
    # 20.07
    rules = [[0, 0, 1, 2, 6, 0, 0, 0, 1, 3], [3, 0, 0, 0, 2, 0, 3, 2, 0, 0],
             [2, 3, 2, 1, 1, 2, 2, 2, 1, 4], [1, 0, 0, 1, 5, 0, 1, 1, 0, 2],
             [0, 2, 2, 2, 4, 1, 0, 0, 1, 1], [4, 1, 1, 0, 0, 0, 4, 2, 0, 5],
             [1, 1, 1, 1, 2, 0, 1, 4, 0, 6], [2, 0, 1, 1, 3, 0, 2, 3, 1, 3]]

    model.set_rules(rules, hybrid=False)
    return model
示例#3
0
def read(filename):
    '''
        Read a text file with the parameter definitions for an ANFIS model.
        Return an instance of an AnfisNet for it.
    '''
    with open(filename, 'r') as fh:
        mdesc = ' '.join(_read_comment_line(fh, _ANF_STR))
        # Input variables and MFs:
        inames = _read_comment_line(fh, _IN_STR)
        mfnums = [int(v) for v in _read_comment_line(fh, _MF_STR)]
        invardefs = []
        for i, varname in enumerate(inames):
            mfdefs = [_read_mf_line(fh) for _ in range(mfnums[i])]
            invardefs.append((varname, mfdefs))
        # Output variables and rules:
        outvars = _read_comment_line(fh, _OUT_STR)
        model = anfis.AnfisNet(mdesc, invardefs, outvars)
        coeff_list = []
        rule_count = reduce(lambda x, y: x * y, mfnums, 1)
        for i in range(rule_count):
            rule_i = [_read_rule_line(fh) for _ in outvars]
            coeff_list.append(rule_i)
        model.coeff = torch.tensor(coeff_list, dtype=torch.float)
    print('Read', filename)
    return model
示例#4
0
def ex2_model():
    invardefs = [
            ('x', make_bell_mfs(2.5, 2, [1, 6])),
            ('y', make_bell_mfs(2.5, 2, [1, 6])),
            ('z', make_bell_mfs(2.5, 2, [1, 6])),
            ]
    outvars = ['output']
    model = anfis.AnfisNet('Jang\'s example 2', invardefs, outvars)
    return model
示例#5
0
def classifier_rig(window="small"):
    """
    Initialization of the ANFIS classifier for test rig. Multi-output.
    Fuzzy reasoning and rules can be modified according to human-expertise.
    :param window: "small", or "large" window size
    :return:
    model: Initialized ANFIS
    """
    if window == "small":
        invardefs = [
            ('x0', make_gauss_mfs(0.5, np.linspace(0, 1, 2))),
            ('x1', make_gauss_mfs(0.5, np.linspace(0, 1, 6))),
            ('x2', make_gauss_mfs(0.5, np.linspace(0, 1, 4))),
            ('x3', make_gauss_mfs(0.5, np.linspace(0, 1, 3))),
            ('x4', make_gauss_mfs(0.5, np.linspace(0, 1, 3))),
            ('x5', make_gauss_mfs(0.5, np.linspace(0, 1, 3))),
            ('x6', make_gauss_mfs(0.5, np.linspace(0, 1, 2))),
            ('x7', make_gauss_mfs(0.5, np.linspace(0, 1, 3))),
            ('x8', make_gauss_mfs(0.5, np.linspace(0, 1, 2))),
            ('x9', make_gauss_mfs(0.5, np.linspace(0, 1, 3))),
        ]
        # 22.07 small
        rules = [[1, 0, 0, 2, 2, 0, 0, 2, 0,
                  2], [0, 2, 2, 1, 0, 1, 1, 1, 1, 1],
                 [0, 5, 1, 0, 1, 2, 1, 0, 1,
                  0], [0, 1, 2, 0, 1, 2, 1, 0, 1, 0],
                 [0, 4, 3, 1, 0, 1, 1, 1, 1, 1],
                 [0, 3, 3, 1, 0, 1, 1, 1, 1, 1]]
    else:
        invardefs = [
            ('x0', make_gauss_mfs(0.5, np.linspace(0, 1, 3))),
            ('x1', make_gauss_mfs(0.5, np.linspace(0, 1, 3))),
            ('x2', make_gauss_mfs(0.5, np.linspace(0, 1, 4))),
            ('x3', make_gauss_mfs(0.5, np.linspace(0, 1, 4))),
            ('x4', make_gauss_mfs(0.5, np.linspace(0, 1, 4))),
            ('x5', make_gauss_mfs(0.5, np.linspace(0, 1, 4))),
            ('x6', make_gauss_mfs(0.5, np.linspace(0, 1, 2))),
            ('x7', make_gauss_mfs(0.5, np.linspace(0, 1, 3))),
            ('x8', make_gauss_mfs(0.5, np.linspace(0, 1, 4))),
            ('x9', make_gauss_mfs(0.5, np.linspace(0, 1, 3))),
        ]
        # 22.07 Large
        rules = [[2, 0, 0, 3, 0, 0, 0, 2, 0,
                  2], [0, 2, 3, 0, 1, 1, 1, 1, 3, 1],
                 [1, 1, 1, 2, 3, 2, 1, 0, 1,
                  0], [1, 2, 2, 0, 2, 3, 1, 0, 2, 0],
                 [0, 2, 3, 1, 1, 1, 1, 1, 3, 1]]
    outvars = ['nc', 'dc1', 'dc2', 'dc3', 'dc4', 'dc5', 'dc6']
    model = anfis.AnfisNet('My_Anfis',
                           invardefs,
                           outvars,
                           hybrid=False,
                           grid=False)
    model.set_rules(rules, hybrid=False)
    return model
def vignette_ex3b():
    '''
        Not actually from Vignette, but I was just trying trapezoid Mfs
    '''
    invardefs = [
            ('x0', make_trap_mfs(2, 2, [-10, -5, 0, 5, 10])),
            ('x1', make_trap_mfs(2, 2, [-10, -5, 0, 5, 10])),
            ]
    outvars = ['y0']
    anf = anfis.AnfisNet('Jang\'s example 1', invardefs, outvars)
    return anf
示例#7
0
def ex1_model():
    '''
        These are the original (untrained) MFS for Jang's example 1.
    '''
    invardefs = [
            ('x0', make_bell_mfs(3.33333, 2, [-10, -3.333333, 3.333333, 10])),
            ('x1', make_bell_mfs(3.33333, 2, [-10, -3.333333, 3.333333, 10])),
            ]
    outvars = ['y0']
    anf = anfis.AnfisNet('Jang\'s example 1', invardefs, outvars)
    return anf
示例#8
0
def fuzzy_classifier(num_in, num_mfs=5):
    '''
        Make a fuzzy classifier with 5 MFS per input, and one output
    '''
    sigma = 10 / num_mfs
    mulist = torch.linspace(-5, 5, num_mfs).tolist()
    invardefs = [('x{}'.format(i), membership.make_gauss_mfs(sigma, mulist))
                 for i in range(num_in)]
    outvars = ['y0']
    model = anfis.AnfisNet('Simple classifier', invardefs, outvars)
    return model
def vignette_ex3():
    '''
        These are the original (untrained) MFS for Vignette example 3.
        Like example 1, but now using 5 Gaussian MFs.
    '''
    invardefs = [
            ('x0', make_gauss_mfs(2, [-10, -5, 0, 5, 10])),
            ('x1', make_gauss_mfs(2, [-10, -5, 0, 5, 10]))
            ]
    outvars = ['y0']
    anf = anfis.AnfisNet('Vignette Example 3', invardefs, outvars)
    return anf
示例#10
0
def model():
    invardefs = [
        # ainda n escolhi os centros e sigma
        ('x(t-18)', make_gauss_mfs(sigma=0.2, mu_list=[0.4, 1.2])),
        ('x(t-12)', make_gauss_mfs(sigma=0.2, mu_list=[0.4, 1.2])),
        ('x(t-6)', make_gauss_mfs(sigma=0.2, mu_list=[0.4, 1.2])),
        ('x(t)', make_gauss_mfs(sigma=0.2, mu_list=[0.4, 1.2]))
    ]
    outvars = ['ys']

    model = anfis.AnfisNet('mackey-glass', invardefs, outvars)
    return model
def plant_model_untrained():
    '''
        Thhis is the initial (untrained) model: (y_now, y_next) -> u
        Uses 3 Bell membership functions for each input.
    '''
    invardefs = [
        ('y_now', make_bell_mfs(1, 2, [-1, 0, 1])),
        ('y_next', make_bell_mfs(1, 2, [-1, 0, 1])),
    ]
    outvars = ['u']
    anf = anfis.AnfisNet('Plant Model', invardefs, outvars)
    return anf
def vignette_ex1():
    '''
        These are the original (untrained) MFS for Vignette example 1.
        Uses 4 Bell membership functions in each input
    '''
    invardefs = [
            ('x0', make_bell_mfs(4, 1, [-10, -3.5, 3.5, 10])),
            ('x1', make_bell_mfs(4, 1, [-10, -3.5, 3.5, 10])),
            ]
    outvars = ['y0']
    anf = anfis.AnfisNet('Vignette Example 1', invardefs, outvars)
    return anf
def vignette_ex2():
    '''
        These are the original (untrained) MFS for Vignette example 2.
        Like example 1, but uses 5 Bell MFs for each input.
    '''
    invardefs = [
            ('x0', make_bell_mfs(4, 1, [-10, -5, 0, 5, 10])),
            ('x1', make_bell_mfs(4, 1, [-10, -5, 5, 0, 10])),
            ]
    outvars = ['y0']
    anf = anfis.AnfisNet('Vignette Example 1', invardefs, outvars)
    return anf
def vignette_ex5():
    '''
        These are the original (untrained) MFS for Vignette example 5
        Same MFs as for example 3, but now there will be two outputs.
        These will be: y0 = sinc(x0, x1) and y1 = 1 - sinc(x0,x1).
    '''
    invardefs = [
            ('x0', make_gauss_mfs(2, [-10, -5, 0, 5, 10])),
            ('x1', make_gauss_mfs(2, [-10, -5, 0, 5, 10]))
            ]
    outvars = ['y0', 'y1']
    anf = anfis.AnfisNet('Vignette Example 5', invardefs, outvars)
    return anf
示例#15
0
文件: noisy.py 项目: gtLara/anfis
def ex1_model():
    '''
        Define modelo e parametros para funcoes de pertinencia
    '''
    # invardefs = [
    #         ('x0', make_bell_mfs(3.33333, 2, list(np.linspace(0, 2*np.pi, 3))))
    #         ]
    invardefs = [('x0',
                  make_gauss_mfs(sigma=1.2,
                                 mu_list=np.linspace(0, 2 * np.pi, 3)))]
    outvars = ['y0']

    anf = anfis.AnfisNet('Aproximacao senoidal', invardefs, outvars)
    return anf
示例#16
0
def jang_ex4_trained_model():
    '''
        Example 4 model, from Jang's data; 4 variables with 2 MFs each.
        These are the final 'trained' values from pg. 683.
    '''
    # Data from Table VI:
    mfs = [
        (0.1790, 2.0456, 0.4798),  # SMALL1
        (0.1584, 2.0103, 1.4975),  # LARGE1
        (0.2410, 1.9533, 0.2960),  # SMALL2
        (0.2923, 1.9178, 1.7824),  # LARGE2
        (0.3798, 2.1490, 0.6599),  # SMALL3
        (0.4884, 1.8967, 1.6465),  # LARGE3
        (0.2815, 2.0170, 0.3341),  # SMALL4
        (0.1616, 2.0165, 1.4727),  # LARGE4
    ]
    invardefs = [
        ('xm18', [BellMembFunc(*mfs[0]),
                  BellMembFunc(*mfs[1])]),
        ('xm12', [BellMembFunc(*mfs[2]),
                  BellMembFunc(*mfs[3])]),
        ('xm6', [BellMembFunc(*mfs[4]),
                 BellMembFunc(*mfs[5])]),
        ('x', [BellMembFunc(*mfs[6]),
               BellMembFunc(*mfs[7])]),
    ]
    outvars = ['xp6']
    model = anfis.AnfisNet('Jang\'s example 4 (trained)', invardefs, outvars)
    # Jang calls this "the parameter matrix C" on pg 683:
    coeff = torch.tensor([
        [0.2167, 0.7233, -0.0365, 0.5433, 0.0276],
        [0.2141, 0.5704, -0.4826, 1.2452, -0.3778],
        [-0.0683, 0.0022, 0.6495, 2.7320, -2.2916],
        [-0.2616, 0.9190, -2.9931, 1.9467, 1.6555],
        [-0.3293, -0.8943, 1.4290, -1.6550, 2.3735],
        [2.5820, -2.3109, 3.7925, -5.8068, 4.0478],
        [0.8797, -0.9407, 2.2487, 0.7759, -2.0714],
        [-0.8417, -1.5394, -1.5329, 2.2834, 2.4140],
        [-0.6422, -0.4384, 0.9792, -0.3993, 1.5593],
        [1.5534, -0.0542, -4.7256, 0.7244, 2.7350],
        [-0.6864, -2.2435, 0.1585, 0.5304, 3.5411],
        [-0.3190, -1.3160, 0.9689, 1.4887, 0.7079],
        [-0.3200, -0.4654, 0.4880, -0.0559, 0.9622],
        [4.0220, -3.8886, 1.0547, -0.7427, -0.4464],
        [0.3338, -0.3306, -0.5961, 1.1220, 0.3529],
        [-0.5572, 0.9190, -0.8745, 2.1899, -0.9497],
    ])
    model.coeff = coeff.unsqueeze(1)  # add extra dim for output vars
    return model
示例#17
0
def vignette_ex5(in_feat=2):
    '''
        These are the original (untrained) MFS for Vignette example 5.
        Uses 3 Gaussian membership functions for each of 3 inputs
        (but the Iris data has 4 inputs, so I'll use 4 variables)
    '''
    def mk_var(name):
        return (name, make_gauss_mfs(1, [-5, 0.08, 1.67]))
    invardefs = [mk_var(name) for name in
                 ['sepal length (cm)',  'sepal width (cm)',
                  'petal length (cm)', 'petal width (cm)'][:in_feat]
                 ]
    outvars = ['setosa', 'versicolor', 'virginica']
    anf = anfis.AnfisNet('Iris Plants Database', invardefs, outvars)
    return anf
示例#18
0
def ex4_model():
    '''
        Example 4 model, from Jang's data; 4 variables with 2 MFs each.
        Predict x(t+6) based on x(t-18), x(t-12), x(t-6), x(t)
        These are the starting MFs values he suggests.
    '''
    invardefs = [
            ('xm18', make_bell_mfs(0.444045, 2, [0.425606, 1.313696])),
            ('xm12', make_bell_mfs(0.444045, 2, [0.425606, 1.313696])),
            ('xm6',  make_bell_mfs(0.444045, 2, [0.425606, 1.313696])),
            ('x',    make_bell_mfs(0.444045, 2, [0.425606, 1.313696])),
            ]
    outvars = ['xp6']
    model = anfis.AnfisNet('Jang\'s example 4', invardefs, outvars)
    return model
示例#19
0
def ex3_model(mfnum=7):
    '''
        Example 3 model, with variable number of Bell MFs, range (-1,+1).
        Specify the no. of MFs, or make it 0 and I'll use Jang's 5 centers.
        Either way, the Bell width/slope values are from Jang's data.
    '''
    # The paper says 7 MFs are best, but his code uses 5 MFs
    if mfnum < 1:  # use the 5 MF values from Jang's code
        centers = [-0.999921, -0.499961, 0.000000, 0.499961, 0.99992]
    else:  # just spread them evenly accross the range (-1, +1)
        centers = np.linspace(-1, 1, mfnum)
    invardefs = [('k', make_bell_mfs(0.249980, 4, centers))]
    outvars = ['y']
    model = anfis.AnfisNet('Jang\'s example 3', invardefs, outvars)
    return model
示例#20
0
def initial_anfis():
    '''
        Build and return a (non-trained) anfis model: (theta, dtheta) -> force
        Assume range for theta is (-20, 20) and dtheta is (-50, 50)
        Use 2 Bell MFs for each input, and non-hybrid learning.
    '''
    invardefs = [
        ('theta', make_bell_mfs(20, 2, [-20, 20])),
        ('dtheta', make_bell_mfs(50, 2, [-50, 50])),
    ]
    outvars = ['force']
    anf = anfis.AnfisNet('Pendulum Controller',
                         invardefs,
                         outvars,
                         hybrid=False)
    return anf
def createModel():
    invardefs = [
        ('prey_count', [
            TriangularMembFunc(0, 1000, 2000),
            TriangularMembFunc(1000, 10000, 100000)
        ]),
        ('predator_count', [
            TriangularMembFunc(0, 1000, 2000),
            TriangularMembFunc(1000, 10000, 100000)
        ]),
    ]
    outvars = ['food']
    model = anfis.AnfisNet('Simple model', invardefs, outvars)
    coeff = torch.tensor([[0.001, 0.0001, -0.0365], [0.001, 0.0001, -0.4826],
                          [0.001, 0.0001, 0.6495], [0.001, 0.0001, -2.9931]])
    model.coeff = coeff.unsqueeze(1)
    return model
示例#22
0
def MakeModel():
    """creates ANFIS model in Pytorch 
       Initial alpha beta gamma values hardcoded
       membership functions generated by BellMembFunc
    """
    #starting alpha, beta gamma for membership functions
    p = {
        'S1alpha': 1.5,
        'S1beta': 2,
        'S1gamma': 5,
        'M1alpha': 1,
        'M1beta': 2,
        'M1gamma': 7.5,
        'L1alpha': 1.5,
        'L1beta': 2,
        'L1gamma': 10,
        'S2alpha': 2.5,
        'S2beta': 2,
        'S2gamma': 8,
        'M2alpha': 1.5,
        'M2beta': 2,
        'M2gamma': 12,
        'L2alpha': 2.5,
        'L2beta': 2,
        'L2gamma': 16
    }

    invardefs = [
        ('x0', [
            BellMembFunc(p['S1alpha'], p['S1beta'], p['S1gamma']),
            BellMembFunc(p['M1alpha'], p['M1beta'], p['M1gamma']),
            BellMembFunc(p['L1alpha'], p['L1beta'], p['L1gamma'])
        ]),
        ('x1', [
            BellMembFunc(p['S2alpha'], p['S2beta'], p['S2gamma']),
            BellMembFunc(p['M2alpha'], p['M2beta'], p['M2gamma']),
            BellMembFunc(p['L2alpha'], p['L2beta'], p['L2gamma'])
        ]),
    ]
    outvars = ['y0']

    return anfis.AnfisNet('SSIE616', invardefs, outvars)
def vignette_ex1_trained():
    '''
        This is a hard-coded version of Vignette example 1, R version,
        using the mfs/coefficients calculated by R after 57 epochs.
    '''
    invardefs = [
            ('x0', [
                    BellMembFunc(3.939986, 1.628525, -9.979724),
                    BellMembFunc(3.433400, 1.818008, -5.150898),
                    BellMembFunc(3.433400, 1.818008,  5.150898),
                    BellMembFunc(3.939986, 1.628525,  9.979724),
                    ]),
            ('x1', [
                    BellMembFunc(3.939986, 1.628525, -9.979724),
                    BellMembFunc(3.433400, 1.818008, -5.150898),
                    BellMembFunc(3.433400, 1.818008,  5.150898),
                    BellMembFunc(3.939986, 1.628525,  9.979724),
                    ])
            ]
    outvars = ['y0']
    anf = anfis.AnfisNet('Vignette Example 1 (R version)', invardefs, outvars)
    rules = torch.tensor([
        [[-0.03990093, -0.03990093, -0.85724840]],
        [[0.12247975,  -0.02936995,  1.22666375]],
        [[0.12247975,   0.02936995,  1.22666375]],
        [[-0.03990093,  0.03990093, -0.85724840]],
        [[-0.02936995,  0.12247975,  1.22666375]],
        [[0.07627426,   0.07627426,  0.31795799]],
        [[0.07627426,  -0.07627426,  0.31795799]],
        [[-0.02936995, -0.12247975,  1.22666375]],
        [[0.02936995,   0.12247975,  1.22666375]],
        [[-0.07627426,  0.07627426,  0.31795799]],
        [[-0.07627426, -0.07627426,  0.31795799]],
        [[0.02936995,  -0.12247975,  1.22666375]],
        [[0.03990093,  -0.03990093, -0.85724840]],
        [[-0.12247975, -0.02936995,  1.22666375]],
        [[-0.12247975,  0.02936995,  1.22666375]],
        [[0.03990093,   0.03990093, -0.85724840]],
        ], dtype=dtype)
    anf.coeff = rules
    return anf
示例#24
0
def jang_traned_anfis():
    '''
        This is the trained ANFIS model from Jang's book (pg 474)
    '''
    invardefs = [
        ('theta', make_bell_mfs(-1.59, 2.34, [-19.49, 19.49])),
        ('dtheta', make_bell_mfs(85.51, 1.94, [-23.21, 23.21])),
    ]
    outvars = ['force']
    coeffs = torch.tensor([
        [0.0502, 0.1646, -10.09],
        [0.0083, 0.0119, -1.09],
        [0.0083, 0.0119, 1.09],
        [0.0502, 0.1646, 10.09],
    ],
                          dtype=dtype).unsqueeze(1)
    anf = anfis.AnfisNet('Pendulum Controller',
                         invardefs,
                         outvars,
                         hybrid=False)
    anf.coeff = coeffs
    return anf
示例#25
0
文件: iris.py 项目: gtLara/anfis
def model(data, n_rules):

    # numpy and norm
    x, y = data.dataset.tensors
    x = x.numpy()
    x, minimum, maximum = normalize(data=x)

    # cmeans
    # como o numero de entradas é constante n de regras = n de funcoes de
    # pertinencia = numero de centros

    modelo = cmenas(k=n_rules)
    modelo.train(data=x, MAX=15, tol=1e-2)
    centros = modelo.C

    # denorm
    centros = denormalize(data=centros, m=minimum, M=maximum)

    def mk_var(name, centros, i):  # de iris_example
        return (name,
                make_gauss_mfs(1,
                               [centros[0, i], centros[1, i], centros[2, i]]))

    def mk_var(name, centros, i):  # de iris_example
        return (name, make_gauss_mfs(1,
                                     [centros[n, i] for n in range(n_rules)]))

    invardefs = [
        mk_var(name, centros, i) for i, name in enumerate(
            ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm'])
    ]

    outvars = ['Species']

    model = anfis.AnfisNet('iris', invardefs, outvars)
    return model
示例#26
0
def model(data, n_rules):

    # numpy and norm
    x, y = data.dataset.tensors
    y = y.float()
    x = x.numpy()
    x, minimum, maximum = normalize(data=x)
    x = x.astype(float)

    # cmeans
    # como o numero de entradas é constante n de regras = n de funcoes de
    # pertinencia = numero de centros

    modelo = cmenas(k=n_rules)
    modelo.train(data=x, MAX=15, tol=1e-2)
    centros = modelo.C

    # denorm
    centros = denormalize(data=centros, m=minimum, M=maximum)

    names = [
        'radius_mean', 'texture_mean', 'perimeter_mean', 'area_mean',
        'smoothness_mean', 'compactness_mean', 'concavity_mean'
        'conc_mean', 'points_mean', 'symmetry_mean'
    ]

    def mk_var(name, centros, i):
        return (name, make_gauss_mfs(3,
                                     [centros[n, i] for n in range(n_rules)]))

    invardefs = [mk_var(name, centros, i) for i, name in enumerate(names)]

    outvars = ['diagnosis']

    model = anfis.AnfisNet('breast-cancer', invardefs, outvars)
    return model
示例#27
0
def my_model_c1(rules='less'):
    """
    Initialization of the ANFIS regressor for c1 prediction. Single output.
    Fuzzy reasoning and rules can be modified according to human-expertise.
    :param rules: one rule for each case (less) or three rules for each case (more)
    :return:
    model: Initialized ANFIS
    """
    if rules == 'less':
        invardefs = [
            ('x0', make_gauss_mfs(0.5, np.linspace(0, 1, 3))),
            ('x1', make_gauss_mfs(0.5, np.linspace(0, 1, 7))),
            ('x2', make_gauss_mfs(0.5, np.linspace(0, 1, 4))),
            ('x3', make_gauss_mfs(0.5, np.linspace(0, 1, 7))),
            ('x4', make_gauss_mfs(0.5, np.linspace(0, 1, 6))),
            ('x5', make_gauss_mfs(0.5, np.linspace(0, 1, 6))),
            ('x6', make_gauss_mfs(0.5, np.linspace(0, 1, 7))),
            ('x7', make_gauss_mfs(0.5, np.linspace(0, 1, 5))),
            ('x8', make_gauss_mfs(0.5, np.linspace(0, 1, 7))),
            ('x9', make_gauss_mfs(0.5, np.linspace(0, 1, 7))),
        ]

        # 14.07
        rules = [[1, 0, 0, 6, 5, 1, 6, 0, 0,
                  6], [0, 4, 0, 2, 2, 0, 2, 0, 3, 2],
                 [2, 5, 3, 1, 1, 5, 1, 4, 5,
                  1], [0, 1, 0, 5, 4, 0, 5, 0, 1, 5],
                 [2, 2, 2, 4, 0, 4, 4, 3, 3,
                  4], [1, 6, 1, 0, 2, 3, 0, 1, 6, 0],
                 [1, 4, 1, 2, 3, 3, 2, 2, 4, 2],
                 [1, 3, 0, 3, 4, 2, 3, 0, 2, 3]]

    elif rules == 'more':
        invardefs = [
            ('x0', make_gauss_mfs(0.5, np.linspace(0, 1, 5))),
            ('x1', make_gauss_mfs(0.5, np.linspace(0, 1, 6))),
            ('x2', make_gauss_mfs(0.5, np.linspace(0, 1, 10))),
            ('x3', make_gauss_mfs(0.5, np.linspace(0, 1, 5))),
            ('x4', make_gauss_mfs(0.5, np.linspace(0, 1, 8))),
            ('x5', make_gauss_mfs(0.5, np.linspace(0, 1, 5))),
            ('x6', make_gauss_mfs(0.5, np.linspace(0, 1, 8))),
            ('x7', make_gauss_mfs(0.5, np.linspace(0, 1, 9))),
            ('x8', make_gauss_mfs(0.5, np.linspace(0, 1, 5))),
            ('x9', make_gauss_mfs(0.5, np.linspace(0, 1, 11))),
        ]

        rules = [[4, 1, 6, 4, 0, 4, 0, 1, 4,
                  4], [3, 0, 8, 4, 3, 4, 3, 4, 4, 7],
                 [4, 5, 8, 4, 5, 4, 5, 6, 4,
                  8], [3, 0, 7, 4, 2, 4, 2, 3, 4, 6],
                 [4, 4, 8, 4, 4, 4, 4, 5, 4,
                  7], [4, 3, 9, 4, 7, 4, 7, 8, 4, 10],
                 [4, 3, 9, 4, 6, 4, 6, 7, 4,
                  9], [4, 2, 6, 4, 1, 4, 1, 2, 4, 5],
                 [0, 2, 5, 0, 0, 0, 1, 0, 0,
                  0], [4, 2, 1, 4, 0, 4, 1, 1, 4, 3],
                 [2, 2, 4, 2, 0, 2, 1, 1, 2,
                  2], [0, 2, 2, 0, 0, 0, 1, 1, 0, 2],
                 [0, 2, 5, 0, 0, 0, 1, 0, 0,
                  1], [3, 2, 0, 3, 0, 3, 1, 1, 3, 3],
                 [0, 2, 3, 0, 0, 0, 1, 0, 0, 2],
                 [1, 2, 4, 1, 0, 1, 1, 1, 1, 2]]
    outvars = ['c1']
    model = anfis.AnfisNet('My_Anfis', invardefs, outvars, grid=False)
    model.set_rules(rules)
    return model
示例#28
0
def jfml_pendulum_2_model():
    '''
        This is a hard-coded version of a JFML example model:
            InvertedPendulum TSK 2, with order-0 and order-1 outputs.
        see: testlib/CreateInvertedPendulumTSKExampleXML2.py in Py4jfml
    '''
    angle = ('Angle',
             OrderedDict([
                 ('very negative', TrapezoidalMembFunc(0.0, 0.0, 48.0, 88.0)),
                 ('negative', TriangularMembFunc(48.0, 88.0, 128.0)),
                 ('zero', TriangularMembFunc(88.0, 128.0, 168.0)),
                 ('positive', TriangularMembFunc(128.0, 168.0, 208.0)),
                 ('very positive',
                  TrapezoidalMembFunc(168.0, 208.0, 255.0, 255.0)),
             ]))
    change = ('ChangeAngle',
              OrderedDict([
                  ('very negative', TrapezoidalMembFunc(0.0, 0.0, 48.0, 88.0)),
                  ('negative', TriangularMembFunc(48.0, 88.0, 128.0)),
                  ('zero', TriangularMembFunc(88.0, 128.0, 168.0)),
                  ('positive', TriangularMembFunc(128.0, 168.0, 208.0)),
                  ('very positive',
                   TrapezoidalMembFunc(168.0, 208.0, 255.0, 255.0)),
              ]))
    invardefs = [angle, change]
    outvars = ['Force']
    anf = anfis.AnfisNet('JFML inverted pendulum, TSK 2', invardefs, outvars)
    # Enter the coefficients; basically five kinds for the output:
    force = {  # Membership functions for force, as order-1 TSK:
            'vneg': [48.0, 0.01, 0.02],
            'neg':  [88.0, 0.00, 0.00],   # Was order-0, so expanded
            'neu':  [128.0, 0.05, 0.05],
            'pos':  [168.0, 0.00, 0.00],  # Was order-0, so expanded
            'vpos': [208.0, 0.05, 0.03],
            }
    # Now swap each JFML constant term from the front to the back:
    for mfname, mfdef in force.items():
        force[mfname] = mfdef[1:] + mfdef[:1]
    # Finally, enumerate output for all possible input MF conbinations:
    coeffs = torch.tensor([
        force['vneg'],  # RULE1 ang_vneg ca_vneg force_vneg
        force['vneg'],  # RULE1 ang_vneg ca_neg force_vneg
        force['vneg'],  # RULE2 ang_vneg ca_neu force_vneg
        force['neg'],  # RULE3 ang_vneg ca_pos force_neg
        force['neu'],  # RULE4 ang_vneg ca_vpos force_neu
        force['vneg'],  # RULE1 ang__neg ca_vneg force_vneg
        force['vneg'],  # RULE1 ang__neg ca_neg force_vneg
        force['neg'],  # RULE5 ang_neg ca_neu force_neg
        force['neu'],  # RULE6 ang_neg ca_pos force_neu
        force['pos'],  # RULE7 ang_neg ca_vpos force_pos
        force['vneg'],  # RULE8 ang_neu ca_vneg force_vneg
        force['neg'],  # RULE9 ang_neu ca_neg force_neg
        force['neu'],  # RULE10 ang_neu ca_neu force_neu
        force['pos'],  # RULE11 ang_neu ca_pos force_pos
        force['vpos'],  # RULE12 ang_neu ca_vpos force_vpos
        force['neg'],  # RULE13 ang_pos ca_vneg force_neg
        force['neu'],  # RULE14 ang_pos ca_neg force_neu
        force['pos'],  # RULE15 ang_pos ca_neu force_pos
        force['vpos'],  # RULE19 ang_pos_ ca_pos force_vpos
        force['vpos'],  # RULE19 ang_pos_ ca_vpos force_vpos
        force['neu'],  # RULE16 ang_vpos ca_vneg force_neu
        force['pos'],  # RULE17 ang_vpos ca_neg force_pos
        force['vpos'],  # RULE18 ang_vpos ca_neu force_vpos
        force['vpos'],  # RULE19 ang_vpos ca_pos force_vpos
        force['vpos'],  # RULE19 ang_vpos ca_vpos force_vpos
    ])
    # Rules 1 and 19 above involved 'or', so I just expanded them out.
    anf.coeff = coeffs.unsqueeze(1)  # add extra dim for output vars
    return anf
def vignette_ex5_trained():
    '''
        This is a hard-coded version of Vignette example 3, R version,
        using the mfs/coefficients calculated by R after 10 epochs.
    '''
    invardefs = [
            ('x0', [
                    GaussMembFunc(-9.989877,  2.024529),
                    GaussMembFunc(-4.861332,  2.009401),
                    GaussMembFunc(-5.100757e-12,  1.884703e+00),
                    GaussMembFunc(4.861332, 2.009401),
                    GaussMembFunc(9.989877, 2.024529),
                    ]),
            ('x1', [
                    GaussMembFunc(-9.989877, 2.024529),
                    GaussMembFunc(-4.861332, 2.009401),
                    GaussMembFunc(-7.534084e-13, 1.884703e+00),
                    GaussMembFunc(4.861332, 2.009401),
                    GaussMembFunc(9.989877, 2.024529),
                    ])
            ]
    outvars = ['y0', 'y1']
    anf = anfis.AnfisNet('Vignette Example 5 (R version)', invardefs, outvars)
    y0_coeff = torch.tensor([
      4.614289e-03, 4.614289e-03, 7.887969e-02, -1.349178e-02,
      -9.089431e-03, -1.694363e-01, 7.549623e-02, 5.862259e-14,
      6.962636e-01, -1.349178e-02, 9.089431e-03, -1.694363e-01,
      4.614289e-03, -4.614289e-03, 7.887969e-02, -9.089431e-03,
      -1.349178e-02, -1.694363e-01, 2.645509e-02, 2.645509e-02,
      3.146186e-01, -1.372046e-01, 1.590475e-13, -9.501776e-01,
      2.645509e-02, -2.645509e-02, 3.146186e-01, -9.089431e-03,
      1.349178e-02, -1.694363e-01, 3.138560e-13, 7.549623e-02,
      6.962636e-01, -7.561163e-14, -1.372046e-01, -9.501776e-01,
      -3.100872e-14, -9.339810e-13, 1.363890e+00, -4.795844e-14,
      1.372046e-01, -9.501776e-01, -2.681160e-13, -7.549623e-02,
      6.962636e-01, 9.089431e-03, -1.349178e-02, -1.694363e-01,
      -2.645509e-02, 2.645509e-02, 3.146186e-01, 1.372046e-01,
      1.790106e-13, -9.501776e-01, -2.645509e-02, -2.645509e-02,
      3.146186e-01, 9.089431e-03, 1.349178e-02, -1.694363e-01,
      -4.614289e-03, 4.614289e-03, 7.887969e-02, 1.349178e-02,
      -9.089431e-03, -1.694363e-01, -7.549623e-02, -7.225253e-14,
      6.962636e-01, 1.349178e-02, 9.089431e-03, -1.694363e-01,
      -4.614289e-03, -4.614289e-03, 7.887969e-02,
        ], dtype=dtype).view(25, 3)
    y1_coeff = torch.tensor([
     -1.563721e-02, 1.563721e-02, 7.029522e-01, 6.511928e-03,
     -2.049419e-03, 1.070100e+00, 8.517531e-02, 2.918635e-13,
     -2.147609e-01, 6.511928e-03, 2.049419e-03, 1.070100e+00,
     -1.563721e-02, 1.563721e-02, 7.029522e-01, 2.049419e-03,
     -6.511928e-03, 1.070100e+00, 3.083698e-02, 3.083698e-02,
     -6.477780e-01, 1.310872e-01, 6.044816e-14, 1.928089e+00,
     -3.083698e-02, 3.083698e-02, 6.477780e-01, 2.049419e-03,
     -6.511928e-03, 1.070100e+00, 5.274627e-13, 8.517531e-02,
     -2.147609e-01, 2.688203e-13, 1.310872e-01, 1.928089e+00,
     -3.522058e-15, 9.355811e-13, 3.521679e-01, 1.036118e-13,
     -1.310872e-01, 1.928089e+00, 2.760916e-13, 8.517531e-02,
     -2.147609e-01, 2.049419e-03, 6.511928e-03, 1.070100e+00,
     -3.083698e-02, 3.083698e-02, 6.477780e-01, 1.310872e-01,
     -1.518057e-13, 1.928089e+00, 3.083698e-02, 3.083698e-02,
     -6.477780e-01, 2.049419e-03, 6.511928e-03, 1.070100e+00,
     -1.563721e-02, 1.563721e-02, 7.029522e-01, 6.511928e-03,
     -2.049419e-03, 1.070100e+00, 8.517531e-02, 1.358819e-13,
     -2.147609e-01, 6.511928e-03, 2.049419e-03, 1.070100e+00,
     -1.563721e-02, 1.563721e-02, 7.029522e-01,
        ], dtype=dtype).view(25, 3)
    anf.coeff = torch.stack([y0_coeff, y1_coeff], dim=1)
    return anf