Example #1
0
 def generate_mf_group(self, G, x):
     mf_group = {}
     for (k, v) in G.iteritems():
         shp = v['shp']
         mf = v['mf']
         if mf == 'trap':
             mf_group[k] = trapmf(x, shp)
         if mf == 'tri':
             mf_group[k] = trimf(x, shp)
         if mf == 'gbell':
             mf_group[k] = gbellmf(x, shp[0], shp[1], shp[2])
         if mf == 'gauss':
             mf_group[k] = gaussmf(x, shp[0], shp[1])
         if mf == 'gauss2':
             mf_group[k] = gauss2mf(x, shp[0], shp[1])
         if mf == 'sig':
             mf_group[k] = sigmf(x, shp[0], shp[1])
         if mf == 'psig':
             mf_group[k] = psigmf(x, shp[0], shp[1], shp[2], shp[3])
         if mf == 'zmf':
             mf_group[k] = zmf(x, shp[0], shp[1], shp[2], shp[3])
         if mf == 'smf':
             mf_group[k] = smf(x, shp[0], shp[1], shp[2], shp[3])
         if mf == 'pimf':
             mf_group[k] = pimf(x, shp[0], shp[1], shp[2], shp[3])
         if mf == 'piecemf':
             mf_group[k] = piecemf(x, shp[0], shp[1], shp[2], shp[3])
     return mf_group
Example #2
0
def _plot_mf_for(x):
    '''Given the x-values, plot a series of example membership functions'''
    tests = OrderedDict([
        # Gaussians:
        ('gauss', skmemb.gaussmf(x, 50, 10)),
        ('left gauss', leftgaussmf(x, 50, 10)),
        ('right gauss', rightgaussmf(x, 50, 10)),
        # Triangles:
        ('triangular', skmemb.trimf(x, [25, 50, 75])),
        ('left linear', leftlinearmf(x, 25, 75)),
        ('right linear', rightlinearmf(x, 25, 75)),
        # fuzzylite:
        ('cosine', cosinemf(x, 50, 50)),
        ('inc concave', concavemf(x, 50, 75)),
        ('dec concave', concavemf(x, 50, 25)),
        ('spike', spikemf(x, 50, 50)),
        ('inc ramp', rampmf(x, 25, 75)),
        ('dec ramp', rampmf(x, 75, 25)),
        # Rectangle-ish
        ('trapezoid', skmemb.trapmf(x, [20, 40, 60, 80])),
        ('rectangle', rectanglemf(x, 25, 75)),
        ('singleton', singletonmf(x, 50)),
    ])
    # Example point sets:
    ps_tests = [[(40, 0.5), (60, 1)],
                [(10, 0.5), (25, 0.25), (40, 0.75), (80, .5)],
                [(0, 1), (40, 0.25), (50, .5), (99, 0)]]
    # Now try some interpolation methods on these:
    for method in ['linear', 'lagrange', 'spline', 'cubic']:
        tests.update([('{} ex{}'.format(method, i), pointsetmf(x, ps, method))
                      for i, ps in enumerate(ps_tests)])
    return tests
Example #3
0
 def generate_mf_group(self, G, x):
     mf_group = {}
     for (k, v) in G.iteritems():
         shp = v['shp']
         mf = v['mf']
         if mf == 'trap':
             mf_group[k] = trapmf(x, shp)
         if mf == 'tri':
             mf_group[k] = trimf(x, shp)
         if mf == 'gbell':
             mf_group[k] = gbellmf(x, shp[0], shp[1], shp[2])
         if mf == 'gauss':
             mf_group[k] = gaussmf(x, shp[0], shp[1])
         if mf == 'gauss2':
             mf_group[k] = gauss2mf(x, shp[0], shp[1])
         if mf == 'sig':
             mf_group[k] = sigmf(x, shp[0], shp[1])
         if mf == 'psig':
             mf_group[k] = psigmf(x, shp[0], shp[1], shp[2], shp[3])
         if mf == 'zmf':
             mf_group[k] = zmf(x, shp[0], shp[1], shp[2], shp[3])
         if mf == 'smf':
             mf_group[k] = smf(x, shp[0], shp[1], shp[2], shp[3])
         if mf == 'pimf':
             mf_group[k] = pimf(x, shp[0], shp[1], shp[2], shp[3])
         if mf == 'piecemf':
             mf_group[k] = piecemf(x, shp[0], shp[1], shp[2], shp[3])
     return mf_group
Example #4
0
def generate_membership(Image_fs):
	""" this function accepts Image and its corresponding fuzzy set and generates the membership 
	values for each pixel based on the input parameters"""	
	if np.shape(Image_fs)==(512,512):
		
		mean=np.mean(Image_fs) #  /|\		figure out hoe to calculate mean using restricted equivaleance
							   # /_*_\
		std=np.std(Image_fs)
		#Membership_value=np.zeros((512,512))
		#Membership_value=0.582*((np.exp(1-abs(Image_fs-mean)))-1)
		#return Membership_value
		# k=abs(Image_fs-mean)
		# exp=np.exp(1-k)
		# val=exp-1
		membership_val=membership.gaussmf(Image_fs,mean,std)
		if np.amin(membership_val)<0 or np.amax(membership_val)>1:
			log.warning('Value Error: membership value out of range')

		else:
			return membership_val

	else:
		log.warning('shape of the Image passed to <generate_membership()> is INVALID!')
Example #5
0
    'above': above,
    'any': any_of,
    'below': below,
    'extremely': extremely,
    'intensify': intensify,
    'more_or_less': more_or_less,
    'norm': norm,
    'not': is_not,
    'plus': plus,
    'seldom': seldom,
    'slightly': slightly,
    'somewhat': somewhat,
    'very': very,
}


def test_all_hedges(y):
    results = []
    hedgenames = sorted(all_hedges.keys())  # Want them in alphabetical order
    for name in hedgenames:
        func = all_hedges[name]
        results.append(func(y))
    return (hedgenames, results)


if __name__ == '__main__':
    x = np.arange(0, 100)
    y = skmemb.gaussmf(x, 50, 15)
    (titles, data) = test_all_hedges(y)
    extramf.visualise_all(x, [y] + data, ['original (gaussian)'] + titles)
    def __init__(self):
        # Create input and output variables
        self.lsonar = ctrl.Antecedent(self.sonar_interval, 'lsonar')
        self.rsonar = ctrl.Antecedent(self.sonar_interval, 'rsonar')
        self.last_theta = ctrl.Antecedent(self.theta_interval, 'last_theta')
        self.theta = ctrl.Consequent(self.theta_interval, 'theta')
        self.v_x = ctrl.Consequent(self.v_interval, 'v_x')

        # we need membership functions!
        distance_levels = ['dangerous', 'unsafe', 'safe']
        theta_directions = ['left', 'centre', 'right']
        velocity_levels = ['slow', 'medium', 'fast']

        self.lsonar.automf(names=distance_levels)
        self.rsonar.automf(names=distance_levels)
        self.last_theta.automf(names=theta_directions)
        self.theta.automf(names=theta_directions)
        self.v_x.automf(names=velocity_levels)

        low_mshp = mshp.trimf(self.sonar_interval, [0.25, 0.25, 0.31])
        mid_mshp = mshp.trimf(self.sonar_interval, [0.28, 0.35, 0.45])
        high_mshp = mshp.trapmf(self.sonar_interval, [0.4, 0.6, 2.55, 2.55])

        self.lsonar['dangerous'] = low_mshp
        self.lsonar['unsafe'] = mid_mshp
        self.lsonar['safe'] = high_mshp

        self.rsonar['dangerous'] = low_mshp
        self.rsonar['unsafe'] = mid_mshp
        self.rsonar['safe'] = high_mshp

        left_angle_mshp = mshp.gaussmf(self.theta_interval, 0.7, 0.2)
        centre_angle_mshp = mshp.gaussmf(self.theta_interval, 0, 0.1)
        right_angle_mshp = mshp.gaussmf(self.theta_interval, -0.7, 0.2)
        
        self.theta['left'] = left_angle_mshp
        self.theta['centre'] = centre_angle_mshp
        self.theta['right'] = right_angle_mshp

        self.last_theta['left'] = left_angle_mshp
        self.last_theta['centre'] = centre_angle_mshp
        self.last_theta['right'] = right_angle_mshp

        self.v_x['slow'] = mshp.trimf(self.v_interval, [0, 0, 0.32])
        self.v_x['medium'] = mshp.gaussmf(self.v_interval, 0.5, 0.36)
        self.v_x['fast'] = mshp.trapmf(self.v_interval, [0.75, 0.95, 1.0, 1.0])

        # Here the rules are defined!
        # look at http://pythonhosted.org/scikit-fuzzy/auto_examples/plot_control_system_advanced.html#set-up-the-fuzzy-control-system
        # and http://pythonhosted.org/scikit-fuzzy/auto_examples/plot_tipping_problem_newapi.html
        # for good examples
        rules = []

        # Sonars are NOT dangerous, go fast and forward
        rules.append(ctrl.Rule(label="FastAndForward", 
                               antecedent=(~self.lsonar['dangerous'] & ~self.rsonar['dangerous']), 
                               consequent=(self.theta['centre'], self.v_x['fast'])))

        # Right sonar reads dangerous, left does not, steer left
        rules.append(ctrl.Rule(label="SteerLeftCaution", 
                               antecedent=(~self.lsonar['dangerous'] & self.rsonar['dangerous']), 
                               consequent=(self.theta['left'], self.v_x['slow'])))
        # Left sonar reads dangerous, right does not, steer left
        rules.append(ctrl.Rule(label="SteerRightCaution", 
                               antecedent=(self.lsonar['dangerous'] & ~self.rsonar['dangerous']), 
                               consequent=(self.theta['right'], self.v_x['slow'])))

        # Right sonar is NOT safe, left reads safe, turn left full speed
        rules.append(ctrl.Rule(label="SteerLeftReckless", 
                               antecedent=(self.lsonar['safe'] & ~self.rsonar['safe']), 
                               consequent=(self.theta['left'], self.v_x['fast'])))
        # Left sonar is NOT safe, right reads safe, turn right full speed
        rules.append(ctrl.Rule(label="SteerRightReckless", 
                               antecedent=(~self.lsonar['safe'] & self.rsonar['safe']), 
                               consequent=(self.theta['right'], self.v_x['fast'])))

        # Left sonar is NOT dangerous and right is NOT safe, turn left medium speed
        rules.append(ctrl.Rule(label="SteerLeftWatchSpeed", 
                               antecedent=(~self.lsonar['dangerous'] & ~self.rsonar['safe']), 
                               consequent=(self.theta['left'], self.v_x['medium'])))
        # Right sonar is NOT dangerous and left is NOT safe, turn right medium speed
        rules.append(ctrl.Rule(label="SteerRightWatchSpeed", 
                               antecedent=(~self.lsonar['safe'] & ~self.rsonar['dangerous']), 
                               consequent=(self.theta['right'], self.v_x['medium'])))

        # Last theta was right, right sonar is dangerous, turn left slow speed
        rules.append(ctrl.Rule(label="SteerLeftMedSpeed", 
                               antecedent=(self.rsonar['dangerous'] & self.last_theta['right']), 
                               consequent=(self.theta['left'], self.v_x['medium'])))
        # Last theta was left, left sonar is dangerous, turn right slow speed
        rules.append(ctrl.Rule(label="SteerRightMedSpeed", 
                               antecedent=(self.lsonar['dangerous'] & self.last_theta['left']), 
                               consequent=(self.theta['right'], self.v_x['medium'])))

        # Last theta was left, rsonar reads not dangerous, turn centre, full speed
        rules.append(ctrl.Rule(label="OutOfDangerFromLeft", 
                               antecedent=(~self.rsonar['dangerous'] & self.last_theta['left']), 
                               consequent=(self.theta['centre'], self.v_x['fast'])))
        # Last theta was right, lsonar reads not dangerous, turn centre, full speed
        rules.append(ctrl.Rule(label="OutOfDangerFromRight", 
                               antecedent=(~self.lsonar['dangerous'] & self.last_theta['right']), 
                               consequent=(self.theta['centre'], self.v_x['fast'])))

        # Create the control system
        self.system = ctrl.ControlSystem(rules=rules)

        # System is created, just create a simulation, input values and call compute()
        self.sim = ctrl.ControlSystemSimulation(self.system)
Example #7
0
def test_gaussmf():
    x = np.arange(-4, 5.1, 0.1)
    expected = np.exp(-(x - 1.33)**2 / (2 * 0.45**2))
    test = gaussmf(x, 1.33, 0.45)
    assert_allclose(test, expected)
Example #8
0
def test_gaussmf():
    x = np.arange(-4, 5.1, 0.1)
    expected = np.exp(- (x - 1.33)**2 / (2 * 0.45**2))
    test = gaussmf(x, 1.33, 0.45)
    assert_allclose(test, expected)
def field(A0,Td,w,t):                                       #potenziale vettore assumendo 
    return A0 *(((np.cos((t-Td/2)*pi/Td))**2)* np.sin(w*(t-Td/2)) * fuzzy.gaussmf(t-Td/2,0,Td/4) *
                                                 fuzzy.gbellmf(t-Td/2,Td/4,7,0))        #inviluppo cos^2
Example #10
0
def rightgaussmf(x, mean, sigma):
    ''' Like Gaussian, but always 1 when >= mean (so, slopes up only)'''
    mf = skmemb.gaussmf(x, mean, sigma)
    mf[np.nonzero(x >= mean)] = 1
    return mf
Example #11
0
def leftgaussmf(x, mean, sigma):
    ''' Like Gaussian, but always 1 when <= mean (so, slopes down only)'''
    mf = skmemb.gaussmf(x, mean, sigma)
    mf[np.nonzero(x <= mean)] = 1
    return mf