Exemple #1
0
 def __auto_membership_angle():
     angle = ctrl.Consequent(np.arange(0, 45, 1), u'Ângulo')
     angle[u'Zero'] = fuzzy.pimf(angle.universe, 0, 2, 5, 7)
     angle[u'Baixo'] = fuzzy.pimf(angle.universe, 3, 5, 15, 25)
     angle[u'Médio'] = fuzzy.pimf(angle.universe, 15, 18, 23, 35)
     angle[u'Alto'] = fuzzy.trimf(angle.universe, [25, 35, 45])
     return angle
Exemple #2
0
 def __auto_membership_angle():
     angle = ctrl.Consequent(np.arange(0, 45, 1), 'angle')
     angle['zero'] = fuzzy.pimf(angle.universe, 0, 2, 5, 7)
     angle['low'] = fuzzy.pimf(angle.universe, 3, 5, 15, 25)
     angle['medium'] = fuzzy.pimf(angle.universe, 15, 18, 23, 35)
     angle['high'] = fuzzy.trimf(angle.universe, [25, 35, 45])
     return angle
Exemple #3
0
 def __auto_membership_distance():
     distance = ctrl.Antecedent(np.arange(0, 0.5, 0.001), u'Distância')
     distance[u'Centro'] = fuzzy.pimf(distance.universe, 0, 0.01, 0.02,
                                      0.03)
     distance[u'Baixa'] = fuzzy.pimf(distance.universe, 0.02, 0.03, 0.05,
                                     0.1)
     distance[u'Média'] = fuzzy.trimf(distance.universe, [0.039, 0.10, 0.2])
     distance[u'Alta'] = fuzzy.pimf(distance.universe, 0.15, 0.2, 0.4, 0.5)
     return distance
Exemple #4
0
 def __auto_membership_distance():
     distance = ctrl.Antecedent(np.arange(0, 0.5, 0.001), 'distance')
     distance['center'] = fuzzy.pimf(distance.universe, 0, 0.01, 0.02, 0.03)
     distance['low_dist'] = fuzzy.pimf(distance.universe, 0.02, 0.03, 0.05,
                                       0.1)
     distance['medium_dist'] = fuzzy.trimf(distance.universe,
                                           [0.039, 0.10, 0.2])
     distance['high_dist'] = fuzzy.pimf(distance.universe, 0.15, 0.2, 0.4,
                                        0.5)
     return distance
Exemple #5
0
 def __auto_membership_radius_curvature():
     radius_curvature = ctrl.Antecedent(np.arange(-1, 201, 1),
                                        'radius_curvature')
     radius_curvature['low_r_curv'] = fuzzy.pimf(radius_curvature.universe,
                                                 -1, 3, 8, 10)
     radius_curvature['medium_r_curv'] = fuzzy.pimf(
         radius_curvature.universe, 5, 30, 80, 130)
     radius_curvature['high_r_curv'] = fuzzy.pimf(radius_curvature.universe,
                                                  100, 130, 170, 201)
     return radius_curvature
Exemple #6
0
 def __auto_membership_radius_curvature():
     radius_curvature = ctrl.Antecedent(np.arange(-1, 201, 1),
                                        u'Raio de Curvatura')
     radius_curvature[u'Baixo'] = fuzzy.pimf(radius_curvature.universe, -1,
                                             3, 8, 10)
     radius_curvature[u'Médio'] = fuzzy.pimf(radius_curvature.universe, 5,
                                             30, 80, 130)
     radius_curvature[u'Alto'] = fuzzy.pimf(radius_curvature.universe, 100,
                                            130, 170, 201)
     return radius_curvature
Exemple #7
0
def membership_f(mf, x, abcd):
    """
    Returns y values corresponding to type of type of Membership fn.
    arguments:
        mf - string containing type of Membership function
        x  - x axis values
    """
    if mf == "zmf":
        return fuzz.zmf(x, abcd[0], abcd[1])  # zmf(x, a, b)
    elif mf == "trimf":
        return fuzz.trimf(x, abcd[0:3])  # trimf(x, abc)
    elif mf == "dsigmf":
        return fuzz.dsigmf(x, abcd[0], abcd[1], abcd[2], abcd[3])  # dsigmf(x, b1, c1, b2, c2)
    elif mf == "gauss2mf":
        return fuzz.gauss2mf(x, abcd[0], abcd[1], abcd[2], abcd[3])  # gauss2mf(x, mean1, sigma1, mean2, sigma2)
    elif mf == "gaussmf":
        return fuzz.gaussmf(x, abcd[0], abcd[1])  # gaussmf(x, mean, sigma)
    elif mf == "gbellmf":
        return fuzz.gbellmf(x, abcd[0], abcd[1], abcd[2])  # gbellmf(x, a, b, c)
    elif mf == "piecemf":
        return fuzz.piecemf(x, abcd[0:3])  # piecemf(x, abc)
    elif mf == "pimf":
        return fuzz.pimf(x, abcd[0], abcd[1], abcd[2], abcd[3])  # pimf(x, a, b, c, d)
    elif mf == "psigmf":
        return fuzz.psigmf(x, abcd[0], abcd[1], abcd[2], abcd[3])  # psigmf(x, b1, c1, b2, c2)
    elif mf == "sigmf":
        return fuzz.sigmf(x, abcd[0], abcd[1])  # sigmf(x, b, c)
    elif mf == "smf":
        return fuzz.smf(x, abcd[0], abcd[1])  # smf(x, a, b)
    elif mf == "trapmf":
        return fuzz.trapmf(x, abcd)  # trapmf(x, abcd)
Exemple #8
0
def membership_f(mf, x, abcd):
    """
    Returns y values corresponding to type of type of Membership fn.
    arguments:
        mf - string containing type of Membership function
        x  - x axis values
    """
    if mf == 'zmf': return fuzz.zmf(x, abcd[0], abcd[1])  # zmf(x, a, b)
    elif mf == 'trimf': return fuzz.trimf(x, abcd[0:3])  # trimf(x, abc)
    elif mf == 'dsigmf':
        return fuzz.dsigmf(x, abcd[0], abcd[1], abcd[2],
                           abcd[3])  # dsigmf(x, b1, c1, b2, c2)
    elif mf == 'gauss2mf':
        return fuzz.gauss2mf(
            x, abcd[0], abcd[1], abcd[2],
            abcd[3])  # gauss2mf(x, mean1, sigma1, mean2, sigma2)
    elif mf == 'gaussmf':
        return fuzz.gaussmf(x, abcd[0], abcd[1])  # gaussmf(x, mean, sigma)
    elif mf == 'gbellmf':
        return fuzz.gbellmf(x, abcd[0], abcd[1],
                            abcd[2])  # gbellmf(x, a, b, c)
    elif mf == 'piecemf':
        return fuzz.piecemf(x, abcd[0:3])  # piecemf(x, abc)
    elif mf == 'pimf':
        return fuzz.pimf(x, abcd[0], abcd[1], abcd[2],
                         abcd[3])  # pimf(x, a, b, c, d)
    elif mf == 'psigmf':
        return fuzz.psigmf(x, abcd[0], abcd[1], abcd[2],
                           abcd[3])  # psigmf(x, b1, c1, b2, c2)
    elif mf == 'sigmf':
        return fuzz.sigmf(x, abcd[0], abcd[1])  # sigmf(x, b, c)
    elif mf == 'smf':
        return fuzz.smf(x, abcd[0], abcd[1])  # smf(x, a, b)
    elif mf == 'trapmf':
        return fuzz.trapmf(x, abcd)  # trapmf(x, abcd)
Exemple #9
0
    def tri_pi_tri(var, term: str, values):
        var[term] = fuzzy_or(
            var.universe, trimf(var.universe,
                                [values[0], values[1], values[1]]),
            var.universe,
            pimf(var.universe, values[1], values[1], values[2], values[2]))[1]

        var[term] = fuzzy_or(
            var.universe, var[term].mf, var.universe,
            trimf(var.universe, [values[2], values[2], values[3]]))[1]

        return var
Exemple #10
0
def membership_f(mf, x, abc = [0,0,0], a = 1, b = 2, c = 3, d = 4, abcd = [0,0,0,0]):

        return {
            'trimf'   : fuzz.trimf(x, abc),                                 # trimf(x, abc)
            'dsigmf'  : fuzz.dsigmf(x, a, b, c, d),                         # dsigmf(x, b1, c1, b2, c2)
            'gauss2mf': fuzz.gauss2mf(x, a, b, c, d),                       # gauss2mf(x, mean1, sigma1, mean2, sigma2)
            'gaussmf' : fuzz.gaussmf(x, a, b),                              # gaussmf(x, mean, sigma)
            'gbellmf' : fuzz.gbellmf(x, a, b, c),                           # gbellmf(x, a, b, c)
            'piecemf' : fuzz.piecemf(x, abc),                               # piecemf(x, abc)
            'pimf'    : fuzz.pimf(x, a, b, c, d),                           # pimf(x, a, b, c, d)
            'psigmf'  : fuzz.psigmf(x, a, b, c, d),                         # psigmf(x, b1, c1, b2, c2)
            'sigmf'   : fuzz.sigmf(x, a, b),                                # sigmf(x, b, c)
            'smf'     : fuzz.smf(x, a, b),                                  # smf(x, a, b)
            'trapmf'  : fuzz.trapmf(x, abcd),                               # trapmf(x, abcd)
            'zmf'     : fuzz.zmf(x, a, b),                                  # zmf(x, a, b)
                }[mf]
Exemple #11
0
def membership_f(mf, x, abc = [0,0,0], a = 1, b = 2, c = 3, d = 4, abcd = [0,0,0,0]):
    """
    Returns y values corresponding to type of type of Membership fn.
    arguments:
        mf - string containing type of Membership function
        x  - x axis values
        abc - list containing triangular edge point x-values
    """
    return {
        'trimf'   : fuzz.trimf(x, abc),                                 # trimf(x, abc)
        'dsigmf'  : fuzz.dsigmf(x, a, b, c, d),                         # dsigmf(x, b1, c1, b2, c2)
        'gauss2mf': fuzz.gauss2mf(x, a, b, c, d),                       # gauss2mf(x, mean1, sigma1, mean2, sigma2)
        'gaussmf' : fuzz.gaussmf(x, a, b),                              # gaussmf(x, mean, sigma)
        'gbellmf' : fuzz.gbellmf(x, a, b, c),                           # gbellmf(x, a, b, c)
        'piecemf' : fuzz.piecemf(x, abc),                               # piecemf(x, abc)
        'pimf'    : fuzz.pimf(x, a, b, c, d),                           # pimf(x, a, b, c, d)
        'psigmf'  : fuzz.psigmf(x, a, b, c, d),                         # psigmf(x, b1, c1, b2, c2)
        'sigmf'   : fuzz.sigmf(x, a, b),                                # sigmf(x, b, c)
        'smf'     : fuzz.smf(x, a, b),                                  # smf(x, a, b)
        'trapmf'  : fuzz.trapmf(x, abcd),                               # trapmf(x, abcd)
        'zmf'     : fuzz.zmf(x, a, b),                                  # zmf(x, a, b)
            }[mf]
Exemple #12
0
def membership_f(mf, x, abc=[0, 0, 0], a=1, b=2, c=3, d=4, abcd=[0, 0, 0, 0]):
    """
    Returns y values corresponding to type of type of Membership fn.
    arguments:
        mf - string containing type of Membership function
        x  - x axis values
        abc - list containing triangular edge point x-values
    """
    return {
        "trimf": fuzz.trimf(x, abc),  # trimf(x, abc)
        "dsigmf": fuzz.dsigmf(x, a, b, c, d),  # dsigmf(x, b1, c1, b2, c2)
        "gauss2mf": fuzz.gauss2mf(x, a, b, c, d),  # gauss2mf(x, mean1, sigma1, mean2, sigma2)
        "gaussmf": fuzz.gaussmf(x, a, b),  # gaussmf(x, mean, sigma)
        "gbellmf": fuzz.gbellmf(x, a, b, c),  # gbellmf(x, a, b, c)
        "piecemf": fuzz.piecemf(x, abc),  # piecemf(x, abc)
        "pimf": fuzz.pimf(x, a, b, c, d),  # pimf(x, a, b, c, d)
        "psigmf": fuzz.psigmf(x, a, b, c, d),  # psigmf(x, b1, c1, b2, c2)
        "sigmf": fuzz.sigmf(x, a, b),  # sigmf(x, b, c)
        "smf": fuzz.smf(x, a, b),  # smf(x, a, b)
        "trapmf": fuzz.trapmf(x, abcd),  # trapmf(x, abcd)
        "zmf": fuzz.zmf(x, a, b),  # zmf(x, a, b)
    }[mf]
Exemple #13
0
# Sigmoid membership function
center = 5.0
width_control = 2.0
sigmf = fuzz.sigmf(x, center, width_control)

# S-function
foot = 1.0
ceiling = 9.0
smf = fuzz.smf(x, foot, ceiling)

# Z-function
zmf = fuzz.zmf(x, foot, ceiling)

# Pi-function
pimf = fuzz.pimf(x, 0.0, 4.0, 5.0, 10.0)

# Gaussian function
mean = 5.0
sigma = 1.25
gaussmf = fuzz.gaussmf(x, mean, sigma)

# Generalized Bell-Shaped function
width = 2.0
slope = 4.0
center = 5.0
gbellmf = fuzz.gbellmf(x, width, slope, center)

fig_scale = 1.5
plt.figure(figsize=(6.4 * fig_scale, 4.8 * fig_scale))
Exemple #14
0
import numpy as np
import skfuzzy as fuzz
import matplotlib.pyplot as plt

# Generate universe variables
#   * Quality and service on subjective ranges [0, 10]
#   * Tip has a range of [0, 25] in units of percentage points
x_qual = np.arange(0, 11, 1)
x_serv = np.arange(0, 11, 1)
x_tip = np.arange(0, 26, 1)

# Generate fuzzy membership functions
qual_lo = fuzz.gaussmf(x_qual, 3, 0.5)
qual_md = fuzz.gbellmf(x_qual, 1, 1, 5)
qual_hi = fuzz.piecemf(x_qual, [6, 7, 10])
serv_lo = fuzz.pimf(x_serv, 0, 2.9, 3, 8)
serv_md = fuzz.sigmoid(x_serv, 0.5)
serv_hi = fuzz.smf(x_serv, 6, 9)
tip_lo = fuzz.trapmf(x_tip, [0, 0, 3, 6])
tip_md = fuzz.trimf(x_tip, [0, 13, 25])
tip_hi = fuzz.zmf(x_tip, 14, 20)
# Visualize these universes and membership functions
fig, (ax0, ax1, ax2) = plt.subplots(nrows=3, figsize=(8, 9))

ax0.plot(x_qual, qual_lo, 'b', linewidth=1.5, label='Bad')
ax0.plot(x_qual, qual_md, 'g', linewidth=1.5, label='Decent')
ax0.plot(x_qual, qual_hi, 'r', linewidth=1.5, label='Great')
ax0.set_title('Food quality')
ax0.legend()

ax1.plot(x_serv, serv_lo, 'b', linewidth=1.5, label='Poor')
import numpy as np
import skfuzzy as fuzz
from matplotlib import pyplot as plt

# Problem: from service quality and food quality to tip amount
x_service = np.arange(0, 10.01, 0.5)
x_food = np.arange(0, 10.01, 0.5)
x_tip = np.arange(0, 25.01, 1.0)

# Membership functions
service_low = fuzz.trimf(x_service, [0, 0, 5])
service_middle = fuzz.trimf(x_service, [0, 5, 10])
service_high = fuzz.trimf(x_service, [5, 10, 10])

food_low = fuzz.zmf(x_food, 0, 5)
food_middle = fuzz.pimf(x_food, 0, 4, 5, 10)
food_high = fuzz.smf(x_food, 5, 10)

tip_low = fuzz.trimf(x_tip, [0, 0, 13])
tip_middle = fuzz.trimf(x_tip, [0, 13, 25])
tip_high = fuzz.trimf(x_tip, [13, 25, 25])

# Input: service score and food score
service_score = 9.5
food_score = 4.0

service_low_degree = fuzz.interp_membership(x_service, service_low,
                                            service_score)
service_middle_degree = fuzz.interp_membership(x_service, service_middle,
                                               service_score)
service_high_degree = fuzz.interp_membership(x_service, service_high,
def FuzzyController(autonomy, MSD, time):
    # Generate universe variables
    #   * Quality and service on subjective ranges [0, 10]
    #   * Tip has a range of [0, 25] in units of percentage points
    x_MSD = np.arange(0, 400, 1)
    x_autonomy = np.arange(0, 400, 1)
    x_time = np.arange(0, 1000, 1)
    x_method = np.arange(0, 1, 0.01)

    # Generate fuzzy membership functions
    MSD_lo = fuzz.zmf(x_MSD, 0, 200)
    MSD_md = fuzz.pimf(x_MSD, 100, 150, 250, 300)
    MSD_hi = fuzz.smf(x_MSD, 200, 400)
    autonomy_lo = fuzz.zmf(x_autonomy, 0, 200)
    autonomy_md = fuzz.pimf(x_autonomy, 100, 150, 250, 300)
    autonomy_hi = fuzz.smf(x_autonomy, 200, 400)
    time_lo = fuzz.zmf(x_time, 0, 500)
    time_md = fuzz.pimf(x_time, 250, 400, 600, 750)
    time_hi = fuzz.smf(x_time, 500, 1000)
    method_lo = fuzz.zmf(x_method, 0, 0.5)
    method_md = fuzz.pimf(x_method, 0.25, 0.4, 0.6, 0.75)
    method_hi = fuzz.smf(x_method, 0.5, 1)

    # We need the activation of our fuzzy membership functions at these values.
    # The exact values 6.5 and 9.8 do not exist on our universes...
    # This is what fuzz.interp_membership exists for!
    autonomy = 100
    MSD = 50
    time = 800

    MSD_level_lo = fuzz.interp_membership(x_MSD, MSD_lo, MSD)
    MSD_level_md = fuzz.interp_membership(x_MSD, MSD_md, MSD)
    MSD_level_hi = fuzz.interp_membership(x_MSD, MSD_hi, MSD)

    autonomy_level_lo = fuzz.interp_membership(x_autonomy, autonomy_lo, autonomy)
    autonomy_level_md = fuzz.interp_membership(x_autonomy, autonomy_md, autonomy)
    autonomy_level_hi = fuzz.interp_membership(x_autonomy, autonomy_hi, autonomy)

    time_level_lo = fuzz.interp_membership(x_time, time_lo, time)
    time_level_md = fuzz.interp_membership(x_time, time_md, time)
    time_level_hi = fuzz.interp_membership(x_time, time_hi, time)

    # Rules
    active_rule11 = np.fmin(MSD_level_lo, autonomy_level_lo)
    active_rule12 = np.fmax(np.fmin(time_level_lo, autonomy_level_lo), active_rule11)
    method_activation_lo = np.fmin(active_rule12, method_lo)

    active_rule21 = np.fmin(MSD_level_md, autonomy_level_md)
    active_rule22 = np.fmax(np.fmin(time_level_md, autonomy_level_md), active_rule21)
    method_activation_md = np.fmin(active_rule22, method_md)

    active_rule31 = np.fmin(MSD_level_hi, autonomy_level_hi)
    active_rule32 = np.fmax(np.fmin(time_level_hi, autonomy_level_hi), active_rule31)
    method_activation_hi = np.fmin(active_rule32, method_hi)

    method0 = np.zeros_like(x_method)

    # Aggregate all three output membership functions together
    aggregated = np.fmax(method_activation_lo,
                         np.fmax(method_activation_md, method_activation_hi))

    # Calculate defuzzified result
    method = fuzz.defuzz(x_method, aggregated, 'centroid')
    method_activation = fuzz.interp_membership(x_method, aggregated, method)  # for plot

    method1 = np.arange(method, method + 1, 1)
    method_lo1 = fuzz.zmf(method1, 0, 0.5)
    method_md1 = fuzz.pimf(method1, 0.25, 0.4, 0.6, 0.75)
    method_hi1 = fuzz.smf(method1, 0.5, 1)

    stats = {"pot-fields": method_lo1[0], "bio": method_md1[0], "rules": method_hi1[0]}
    method = max(stats.items(), key=operator.itemgetter(1))[0]
    return method
Exemple #17
0

lete    = ctrl.Antecedent(np.arange(0, 130, 1), 'lete')
leve    = ctrl.Antecedent(np.arange(0, 130, 1), 'leve')
le      = ctrl.Antecedent(np.arange(0, 130, 1), 'le')
te      = ctrl.Antecedent(np.arange(0, 130, 1), 'te')
ve      = ctrl.Antecedent(np.arange(0, 130, 1), 've')
ld      = ctrl.Antecedent(np.arange(0, 130, 1), 'ld')
vt      = ctrl.Antecedent(np.arange(0, 130, 1), 'vt')
td      = ctrl.Antecedent(np.arange(0, 130, 1), 'td')
vd      = ctrl.Antecedent(np.arange(0, 130, 1), 'vd')

alerta  = ctrl.Consequent(np.arange(0, 130, 1), 'alerta')


lete['bajo']     = fuzz.pimf(lete.universe, 10, 30, 50, 80)
lete['medio']    = fuzz.pimf(lete.universe, 20, 40, 60, 90)
lete['alto']     = fuzz.pimf(lete.universe, 30, 60, 80, 130)

leve['bajo']     = fuzz.pimf(leve.universe, 30, 50, 80, 90)
leve['medio']    = fuzz.pimf(leve.universe, 40, 60, 100, 110)
leve['alto']     = fuzz.pimf(leve.universe, 50, 70, 120, 130)

le['bajo']       = fuzz.pimf(le.universe, 0, 10, 20, 30)
le['medio']      = fuzz.pimf(le.universe, 10, 30, 50, 60)
le['alto']       = fuzz.pimf(le.universe, 20, 80, 90, 130)

te['bajo']       = fuzz.pimf(te.universe, 0, 10, 50, 60)
te['medio']      = fuzz.pimf(te.universe, 10, 20, 100, 120)
te['alto']       = fuzz.pimf(te.universe, 20, 40, 120, 130)
Exemple #18
0
###########################################

import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl

# New Antecedent/Consequent objects hold universe variables and membership
########### Controle do angulo de guinada
# functions

distance = ctrl.Antecedent(np.arange(0, 0.5, 0.001), 'distance')
angle = ctrl.Consequent(np.arange(0, 0.45, 0.01), 'angle')

# Auto-membership function

distance['center'] = fuzz.pimf(distance.universe, 0, 0.01, 0.02, 0.04)
distance['low_dist'] = fuzz.trimf(distance.universe, [0.02, 0.05, 0.1])
distance['medium_dist'] = fuzz.trimf(distance.universe, [0.05, 0.10, 0.2])
distance['high_dist'] = fuzz.pimf(distance.universe, 0.15, 0.2, 0.4, 0.5)
distance.view()

angle['zero'] = fuzz.pimf(angle.universe, 0, 0.02, 0.05, 0.07)
angle['low'] = fuzz.trimf(angle.universe, [0.03, 0.1, 0.25])
angle['medium'] = fuzz.trimf(angle.universe, [0.15, 0.25, 0.35])
angle['high'] = fuzz.trimf(angle.universe, [0.25, 0.35, 0.45])
angle.view()

# Regras

rule1 = ctrl.Rule(distance['center'], angle['zero'])
rule2 = ctrl.Rule(distance['low_dist'], angle['low'])
Exemple #19
0
 def __auto_membership_speed():
     speed = ctrl.Consequent(np.arange(25, 75, 1), 'speed')
     speed['low'] = fuzzy.pimf(speed.universe, 25, 32, 35, 45)
     speed['medium'] = fuzzy.pimf(speed.universe, 40, 45, 50, 54)
     speed['high'] = fuzzy.pimf(speed.universe, 48, 57, 65, 75)
     return speed
Exemple #20
0
 def __auto_membership_speed():
     speed = ctrl.Consequent(np.arange(25, 75, 1), u'Velocidade')
     speed[u'Baixa'] = fuzzy.pimf(speed.universe, 25, 32, 35, 45)
     speed[u'Média'] = fuzzy.pimf(speed.universe, 40, 45, 50, 54)
     speed[u'Alta'] = fuzzy.pimf(speed.universe, 48, 57, 65, 75)
     return speed
Exemple #21
0
valence['very low'] = fuzz.gaussmf(valence.universe, -4.8, 1.6)
valence['low'] = fuzz.gaussmf(valence.universe, -3, 1.5)
valence['mid'] = fuzz.gaussmf(valence.universe, 0, 1.3)
valence['high'] = fuzz.gaussmf(valence.universe, 3.5, 1.7)
valence['very high'] = fuzz.gaussmf(valence.universe, 4.9, 1.3)

angle['happiness'] = fuzz.gaussmf(angle.universe, 13, 53)
angle['excitement'] = fuzz.gaussmf(angle.universe, 70, 40)
angle['frustration'] = fuzz.gaussmf(angle.universe, 135, 35)
angle['depression'] = fuzz.gaussmf(angle.universe, 218, 37)
angle['boredom'] = fuzz.gaussmf(angle.universe, 280, 40)
angle['calm'] = fuzz.gaussmf(angle.universe, 350, 55)

intensity['low'] = fuzz.sigmf(intensity.universe, 2, -4)
intensity['middle'] = fuzz.pimf(intensity.universe, 2, 2.1, 4.9, 5)
intensity['high'] = fuzz.sigmf(intensity.universe, 5, 4)

arousal.view()
valence.view()
angle.view()
intensity.view()

#### arousal-valence to angle relations ####
rule1a = ctrl.Rule(valence['very low'] & arousal['very low'],
                   angle['depression'])
rule2a = ctrl.Rule(valence['very low'] & arousal['low'], angle['depression'])
rule3a = ctrl.Rule(valence['very low'] & arousal['mid'], angle['depression'])
rule4a = ctrl.Rule(valence['very low'] & arousal['high'], angle['frustration'])
rule5a = ctrl.Rule(valence['very low'] & arousal['very high'],
                   angle['frustration'])
Exemple #22
0
    def __init__(self):
        # Create fuzzy variables
        num_pieces = ctrl.Antecedent(np.arange(2, 33, 1), 'num_pieces')
        num_moves = ctrl.Antecedent(np.arange(0, 150, 1), 'num_moves')
        white_king_advanced_squares = ctrl.Antecedent(
            np.arange(1, 9, 1), 'white_king_advanced_squares')
        black_king_advanced_squares = ctrl.Antecedent(
            np.arange(1, 9, 1), 'black_king_advanced_squares')
        game_phase = ctrl.Consequent(np.linspace(0, 1, 50), 'game_phase')

        # Populate the fuzzy variables with membership functions
        num_pieces['LOW'] = fuzz.gaussmf(num_pieces.universe, 0, 8)
        num_pieces['MEDIUM'] = fuzz.pimf(num_pieces.universe, 4, 15, 17, 28)
        num_pieces['HIGH'] = fuzz.gaussmf(num_pieces.universe, 32, 8)

        num_moves['LOW'] = fuzz.sigmf(num_moves.universe, 10, -.4)
        num_moves['MEDIUM'] = fuzz.pimf(num_moves.universe, 10, 20, 30, 60)
        num_moves['HIGH'] = fuzz.sigmf(num_moves.universe, 45, .1)

        a = fuzz.trimf(black_king_advanced_squares.universe, [1, 1, 8])
        b = fuzz.trimf(black_king_advanced_squares.universe, [1, 8, 8])
        white_king_advanced_squares['RETREATED'] = a
        white_king_advanced_squares['ADVANCED'] = b
        black_king_advanced_squares['RETREATED'] = a
        black_king_advanced_squares['ADVANCED'] = b

        n = ['OPENING', 'MIDDLE', 'END']
        # game_phase.automf(names=n)

        game_phase['OPENING'] = fuzz.trimf(game_phase.universe, [0, 0, .4])
        game_phase['MIDDLE'] = fuzz.trimf(game_phase.universe, [.3, .5, .7])
        game_phase['END'] = fuzz.trimf(game_phase.universe, [.6, 1, 1])

        # Add extra game phases for king file rules.
        game_phase['PROBABLY_OPENING'] = fuzz.trapmf(game_phase.universe,
                                                     [0, 0, .5, .6]) / 4
        game_phase['PROBABLY_END'] = fuzz.trapmf(game_phase.universe,
                                                 [.4, .5, 1, 1]) / 4

        rules = [
            ctrl.Rule(antecedent=num_pieces['HIGH'],
                      consequent=game_phase['OPENING']),
            ctrl.Rule(antecedent=num_pieces['MEDIUM'],
                      consequent=game_phase['MIDDLE']),
            ctrl.Rule(antecedent=num_pieces['LOW'],
                      consequent=game_phase['END']),
            ctrl.Rule(antecedent=num_moves['LOW'],
                      consequent=game_phase['OPENING']),
            ctrl.Rule(antecedent=num_moves['MEDIUM'],
                      consequent=game_phase['MIDDLE']),
            ctrl.Rule(antecedent=num_moves['HIGH'],
                      consequent=game_phase['END']),
            ctrl.Rule(antecedent=(white_king_advanced_squares['RETREATED']
                                  & black_king_advanced_squares['RETREATED']),
                      consequent=game_phase['PROBABLY_OPENING']),
            ctrl.Rule(antecedent=(white_king_advanced_squares['RETREATED']
                                  & black_king_advanced_squares['ADVANCED']),
                      consequent=game_phase['PROBABLY_OPENING']),
            ctrl.Rule(antecedent=(white_king_advanced_squares['ADVANCED']
                                  & black_king_advanced_squares['RETREATED']),
                      consequent=game_phase['PROBABLY_OPENING']),
            ctrl.Rule(antecedent=(white_king_advanced_squares['ADVANCED']
                                  & black_king_advanced_squares['ADVANCED']),
                      consequent=game_phase['PROBABLY_END'])
        ]

        system = ctrl.ControlSystem(rules=rules)
        self.sim = ctrl.ControlSystemSimulation(
            system, flush_after_run=1)  # TODO: when to flush.