예제 #1
0
  def Evaluate( self, membershipFunction, min, max, step ):
  
    membershipFunction.SetComposeFunction( BinaryFunction.GodelSNorm() )
  
    # Find the maximum value of the membership function
    flatMaxFunction = MembershipFunction.FlatMembershipFunction()
    flatMaxFunction.SetParameters( [ self.MaximumValue( membershipFunction, min, max, step ) ] )
  
    # This function is the max value when the original membership function achieves its max, and is zero otherwise
    maxFunction = MembershipFunction.MembershipFunction()
    maxFunction.AddBaseFunction( flatMaxFunction )
    maxFunction.AddBaseFunction( membershipFunction )
    maxFunction.SetComposeFunction( EqualBinaryFunction() )
  
    numeratorFunction = MembershipFunction.MembershipFunction()
    numeratorFunction.AddBaseFunction( maxFunction )
    numeratorFunction.AddBaseFunction( XMembershipFunction() )
    numeratorFunction.SetComposeFunction( BinaryFunction.GoguenTNorm() ) # Multiply x * func and integrate
  
    denominatorFunction = MembershipFunction.MembershipFunction()
    denominatorFunction.AddBaseFunction( maxFunction )
  
    num = self.Integrate( numeratorFunction, min, max, step )  
    denom = self.Integrate( denominatorFunction, min, max, step )

    if ( denom == 0 ):
      logging.warning( "DefuzzifierMOM::Evaluate: Membership function has zero maximum." )
      return 0

    return num / denom
예제 #2
0
 def Copy(self, other):
     if (other.OutputMembershipFunction != None):
         copyOutputMembershipFunction = MembershipFunction.MembershipFunction(
         )
         copyOutputMembershipFunction.Copy(other.OutputMembershipFunction)
         self.OutputMembershipFunction = copyOutputMembershipFunction
     for name in other.InputMembershipFunctions:
         copyInputMembershipFunction = MembershipFunction.MembershipFunction(
         )
         copyInputMembershipFunction.Copy(
             other.InputMembershipFunctions[name])
         self.InputMembershipFunctions[name] = copyInputMembershipFunction
     if (other.ComposeFunction != None):
         copyComposeFunction = MembershipFunction.MembershipFunction()
         copyComposeFunction.Copy(other.ComposeFunction)
         self.ComposeFunction = copyComposeFunction
예제 #3
0
 def setUp(self):
     self.c = Shape()
     self.ordered = [(20,0),(40,1),(60,0),(80,1),(100,0)]
     self.unordered = [(10.0,0.5),(12,1),(24.000,0),(14.0,0.500),(0,0)]
     self.mftri = MembershipFunction()
     self.mftri.tri(0,2,4)
     self.mftrap = MembershipFunction()
     self.mftrap.trap(0,2,4,6)
     self.mfout = MembershipFunction()
     self.mfout.tri(4,6,8)
     self.mfcold = MembershipFunction()
     self.mfcold.trap(0,0,2,4)
     self.mfdict = {}
     self.mfdict['cold'] = self.mfcold
     self.mfdict['blast'] = self.mfout
     self.rule = Rule(self.mfdict)
     self.cin = {}
     self.cin['temp']=3
예제 #4
0
    def Evaluate(self, inputValues, transformOutputFunction):
        if (self.ComposeFunction == None
                or self.OutputMembershipFunction == None):
            emptyMembershipFunction = MembershipFunction.FlatMembershipFunction(
            )
            emptyMembershipFunction.SetParameters([0])
            return emptyMembershipFunction

        # Find the input membership values
        totalMembership = 1 - self.ComposeFunction.Evaluate(
            0, 1)  # This yields 1 for t-norm and 0 for s-norm
        ruleUsed = False

        for name in inputValues:
            if (name not in self.InputMembershipFunctions):
                continue

            currentMembership = self.InputMembershipFunctions[name].Evaluate(
                inputValues[name])
            totalMembership = self.ComposeFunction.Evaluate(
                totalMembership, currentMembership)
            ruleUsed = True

        # Assign the total memership to be zero if the rule is never used
        if (ruleUsed == False):
            totalMembership = 0

        # Compose the flat membership function
        flatMembershipFunction = MembershipFunction.FlatMembershipFunction()
        flatMembershipFunction.SetParameters([totalMembership])

        # Apply clipping or scaling to output membership function
        transformedOutputMembershipFunction = MembershipFunction.MembershipFunction(
        )
        transformedOutputMembershipFunction.AddBaseFunction(
            self.OutputMembershipFunction)
        transformedOutputMembershipFunction.AddBaseFunction(
            flatMembershipFunction)
        transformedOutputMembershipFunction.SetComposeFunction(
            transformOutputFunction)

        # Return the transformed output membership function
        return transformedOutputMembershipFunction
예제 #5
0
    def AddInputMembershipFunction(self, newInputMembershipFunction,
                                   inputName):
        if (inputName not in self.InputMembershipFunctions):
            self.InputMembershipFunctions[
                inputName] = MembershipFunction.MembershipFunction()
            self.InputMembershipFunctions[inputName].SetComposeFunction(
                self.ComposeFunction)

        self.InputMembershipFunctions[inputName].AddBaseFunction(
            newInputMembershipFunction)
예제 #6
0
  def Evaluate( self, membershipFunction, min, max, step ):
  
    membershipFunction.SetComposeFunction( BinaryFunction.GodelSNorm() )
  
    numeratorFunction = MembershipFunction.MembershipFunction()
    numeratorFunction.AddBaseFunction( membershipFunction )
    numeratorFunction.AddBaseFunction( XMembershipFunction() )
    numeratorFunction.SetComposeFunction( BinaryFunction.GoguenTNorm() ) # Multiply x * func and integrate
  
    denominatorFunction = MembershipFunction.MembershipFunction()
    denominatorFunction.AddBaseFunction( membershipFunction )
  
    num = self.Integrate( numeratorFunction, min, max, step )  
    denom = self.Integrate( denominatorFunction, min, max, step )

    if ( denom == 0 ):
      logging.warning( "DefuzzifierCOA::Evaluate: Membership function has zero area." )
      return 0

    return num / denom
    def __init__(self, min_thrust, max_thrust):
        self.min_thrust = min_thrust
        self.max_thrust = max_thrust
        self.recommended_left = 0.0
        self.recommended_right = 0.0
        self.lastErr = 0.0

        # prepare membership functions for input parameters
        self.angle_err = {}
        self.angle_err[2] = MembershipFunction.MembershipFunction(
            False, True, 1.0 / 45.0, 90.0)
        self.angle_err[1] = MembershipFunction.MembershipFunction(
            False, False, 1.0 / 45.0, 45.0)
        self.angle_err[0] = MembershipFunction.MembershipFunction(
            False, False, 1.0 / 45.0, 0.0)
        self.angle_err[-1] = MembershipFunction.MembershipFunction(
            False, False, 1.0 / 45.0, -45.0)
        self.angle_err[-2] = MembershipFunction.MembershipFunction(
            True, False, 1.0 / 45.0, -90.0)

        self.ang_vel = {}
        self.ang_vel[2] = MembershipFunction.MembershipFunction(
            False, True, 1.0 / 20.0, 40.0)
        self.ang_vel[1] = MembershipFunction.MembershipFunction(
            False, False, 1.0 / 20.0, 20.0)
        self.ang_vel[0] = MembershipFunction.MembershipFunction(
            False, False, 1.0 / 20.0, 0.0)
        self.ang_vel[-1] = MembershipFunction.MembershipFunction(
            False, False, 1.0 / 20.0, -20.0)
        self.ang_vel[-2] = MembershipFunction.MembershipFunction(
            True, False, 1.0 / 20.0, -40.0)

        # prepare membership functions for outpur thrust recommendations
        self.thr_reco = {}
        self.thr_reco[3] = MembershipFunction.MembershipFunction(
            False, False, 1.0 / 5.0, 60.0)
        self.thr_reco[2] = MembershipFunction.MembershipFunction(
            False, False, 1.0 / 5.0, 40.0)
        self.thr_reco[1] = MembershipFunction.MembershipFunction(
            False, False, 1.0 / 5.0, 20.0)
        self.thr_reco[0] = MembershipFunction.MembershipFunction(
            False, False, 1.0 / 5.0, 0.0)  #!!!

        # prepare rulebase
        self.rules = []

        self.rules.append(Rule(-2, -2, 3))
        self.rules.append(Rule(-2, -1, 3))
        self.rules.append(Rule(-2, 0, 2))
        self.rules.append(Rule(-2, 1, 1))
        self.rules.append(Rule(-2, 2, 0))

        self.rules.append(Rule(-1, -2, 3))
        self.rules.append(Rule(-1, -1, 2))
        self.rules.append(Rule(-1, -0, 1))
        self.rules.append(Rule(-1, 1, 0))
        self.rules.append(Rule(-1, 2, -1))

        self.rules.append(Rule(0, -2, 2))
        self.rules.append(Rule(0, -1, 1))
        self.rules.append(Rule(0, 0, 0))
        self.rules.append(Rule(0, 1, -1))
        self.rules.append(Rule(0, 2, -2))

        self.rules.append(Rule(1, -2, 1))
        self.rules.append(Rule(1, -1, 0))
        self.rules.append(Rule(1, 0, -1))
        self.rules.append(Rule(1, 1, -2))
        self.rules.append(Rule(1, 2, -3))

        self.rules.append(Rule(2, -2, 0))
        self.rules.append(Rule(2, -1, -1))
        self.rules.append(Rule(2, 0, -2))
        self.rules.append(Rule(2, 1, -3))
        self.rules.append(Rule(2, 2, -3))
예제 #8
0
class TestShape(unittest.TestCase):
    def setUp(self):
        self.c = Shape()
        self.ordered = [(20,0),(40,1),(60,0),(80,1),(100,0)]
        self.unordered = [(10.0,0.5),(12,1),(24.000,0),(14.0,0.500),(0,0)]
        self.mftri = MembershipFunction()
        self.mftri.tri(0,2,4)
        self.mftrap = MembershipFunction()
        self.mftrap.trap(0,2,4,6)
        self.mfout = MembershipFunction()
        self.mfout.tri(4,6,8)
        self.mfcold = MembershipFunction()
        self.mfcold.trap(0,0,2,4)
        self.mfdict = {}
        self.mfdict['cold'] = self.mfcold
        self.mfdict['blast'] = self.mfout
        self.rule = Rule(self.mfdict)
        self.cin = {}
        self.cin['temp']=3
    def test_addPoint_presentationValue(self):
        p = [(20,0),(34,0.7),(46,0.7),(54,0.3),(66,0.3),(70,0.5),(100,0.5)]
        for point in p:
            self.c.addPoint(point)
        self.c.solve()
        self.assertEqual(round(self.c.x,3),60.943)
    def test_addPoint_sorted(self):
        p = self.ordered
        for point in p:
            self.c.addPoint(point)
        self.c.solve()
        self.assertEqual(self.c.x,60)
    def test_addPoint_unsorted(self):
        p = self.unordered
        for point in p:
            self.c.addPoint(point)
        self.c.solve()
        self.assertEqual(self.c.x,12)
    def test_setPoints_unsorted(self):
        p = self.unordered
        self.c.setPoints(p)
        self.c.solve()
        self.assertEqual(self.c.x,12)
    def test_resultTri(self):
        self.assertEqual(self.mftri.getResult(0.5).points,[(0,0),(1,0.5),(3,0.5),(4,0)])
    def test_resultTrap(self):
        self.assertEqual(self.mftrap.getResult(0.5).points,[(0,0),(1,0.5),(5,0.5),(6,0)])
    def test_getDegreeTrap(self):
        self.assertEqual(self.mftrap.getDegree(1),0.5)
    def test_getDegreeTri(self):
        self.assertEqual(self.mftri.getDegree(3),0.5)
    def test_heaterRule(self):
        self.rule.addCondition('temp', 'cold')
        self.rule.setResult('heater','blast')
        (feat, shape) = self.rule.getResult(self.cin)
        shape.solve()
        self.assertEqual(shape.x, 6)
    def test_intersects(self):
        s1 = Segment((0,0),(1,1))
        s2 = Segment((0,1),(1,0))
        self.assertTrue(s1.intersects(s2))
    def test_noIntersect(self):
        s1 = Segment((0,0),(1,1))
        s2 = Segment((0,1),(2,3))
        self.assertTrue(not s1.intersects(s2))
    def test_intersect1(self):
        s1 = Segment((0,0),(1,1))
        s2 = Segment((0,1),(1,0))
        self.assertEqual(s1.intersection(s2), (0.5,0.5))
    def test_intersect2(self):
        s1 = Segment((1,3),(3,1))
        s2 = Segment((1,1),(3,3))
        self.assertEqual(s1.intersection(s2), (2.0,2.0))
    def test_addSegment(self):
        s = Shape()
        s.addSegment(Segment((0,0),(1,1)))
        s.addSegment(Segment((1,1),(2,2)))
        s.addSegment(Segment((2,2),(3,0)))
        self.assertEqual(s.points, [(0,0),(1,1),(2,2),(3,0)])
    def test_addShapes(self):
        s1 = Shape()
        s1.setPoints([(0,0),(1,1),(2,1),(3,0)])
        s2 = Shape()
        s2.setPoints([(2,0),(4,2),(5,0)])
        s3 = s1+s2
        self.assertEqual(s3.points, [(0,0),(1,1),(2,1),(2.5,0.5),(4,2),(5,0)])
예제 #9
0
    def __init__(self, min_thrust, max_thrust, reactions):
        self.min_thrust = min_thrust
        self.max_thrust = max_thrust
        self.recommended_left = 0.0
        self.recommended_right = 0.0
        self.lastErr = 0.0
        
        # prepare membership functions for input parameters
        self.angle_err = {}
        self.angle_err[4] = MembershipFunction.MembershipFunction(False, True, 1.0/20.0, 60.0)
        self.angle_err[3] = MembershipFunction.MembershipFunction(False, False, 1.0/15.0, 40.0)
        self.angle_err[2] = MembershipFunction.MembershipFunction(False, False, 1.0/15.0, 20.0)
        self.angle_err[1] = MembershipFunction.MembershipFunction(False, False, 1.0/10.0, 10.0)
        self.angle_err[0] = MembershipFunction.MembershipFunction(False, False, 1.0/3.0, 0.0)
        self.angle_err[-1] = MembershipFunction.MembershipFunction(False, False, 1.0/10.0, -10.0)
        self.angle_err[-2] = MembershipFunction.MembershipFunction(False, False, 1.0/15.0, -20.0)
        self.angle_err[-3] = MembershipFunction.MembershipFunction(False, False, 1.0/15.0, -40.0)
        self.angle_err[-4] = MembershipFunction.MembershipFunction(True, False, 1.0/20.0, -60.0)
        
        self.ang_vel = {}
        self.ang_vel[4] = MembershipFunction.MembershipFunction(False, True, 1.0/7.5, 30.0)
        self.ang_vel[3] = MembershipFunction.MembershipFunction(False, False, 1.0/7.5, 22.5)
        self.ang_vel[2] = MembershipFunction.MembershipFunction(False, False, 1.0/7.5, 12.5)
        self.ang_vel[1] = MembershipFunction.MembershipFunction(False, False, 1.0/5.0, 5.0)
        self.ang_vel[0] = MembershipFunction.MembershipFunction(False, False, 1.0/5.0, 0.0)
        self.ang_vel[-1] = MembershipFunction.MembershipFunction(False, False, 1.0/5.0, -5.0)
        self.ang_vel[-2] = MembershipFunction.MembershipFunction(False, False, 1.0/7.5, -12.5)
        self.ang_vel[-3] = MembershipFunction.MembershipFunction(False, False, 1.0/7.5, -22.5)
        self.ang_vel[-4] = MembershipFunction.MembershipFunction(True, False, 1.0/7.5, -30.0)
                  
        # prepare membership functions for output thrust recommendations
        span = max_thrust - min_thrust
        factor = span / 8.0
        self.thr_reco = {}
        self.thr_reco[8] = MembershipFunction.MembershipFunction(False, False, factor, max_thrust)
        self.thr_reco[7] = MembershipFunction.MembershipFunction(False, False, factor, min_thrust + span/8.0 * 7)
        self.thr_reco[6] = MembershipFunction.MembershipFunction(False, False, factor, min_thrust + span/8.0 * 6)
        self.thr_reco[5] = MembershipFunction.MembershipFunction(False, False, factor, min_thrust + span/8.0 * 5)
        self.thr_reco[4] = MembershipFunction.MembershipFunction(False, False, factor, min_thrust + span/8.0 * 4)
        self.thr_reco[3] = MembershipFunction.MembershipFunction(False, False, factor, min_thrust + span/8.0 * 3)
        self.thr_reco[2] = MembershipFunction.MembershipFunction(False, False, factor, min_thrust + span/8.0 * 2)
        self.thr_reco[1] = MembershipFunction.MembershipFunction(False, False, factor, min_thrust + span/8.0 * 1)
        self.thr_reco[0] = MembershipFunction.MembershipFunction(False, False, factor, min_thrust)
        
        # prepare rulebase
        self.rules = []

        z = 0

        for verse in range(9):
            for column in range(9):
                self.rules.append(Rule(verse - 4, column - 4, reactions[z]))
                z += 1
        print(str(z) + " rules loaded")
예제 #10
0
import OperationCompelement as comp
import OperationUnion as uni
import OperationIntersection as inter
import OperationImplication as imp
import Defuzzifier as defuz
import math
import matplotlib.pyplot as plt

#Contoh ini menggunakan mesin inferensi Tsukamoto
#Dengan operasi-operasi sebagai berikut :
#Union = min
#Intersection = max
#Implikasi = tsukamoto (khusus pada inferensi tsukamoto)

#Domain
distance = mf.linspace(200, 10000, 98001)
area = mf.linspace(4, 20, 16001)
facility = mf.linspace(0, 100, 10001)
price = mf.linspace(250, 1500, 12501)

#Fuzzy Set
distanceNear = ["trapmf", -math.inf, -math.inf, 2000, 4000]
distanceNearMf = mf.membership(distanceNear, distance)
plt.plot(distance, distanceNearMf, label="Near")
distanceMid = ["trapmf", 2000, 4000, 6000, 8000]
distanceMidMf = mf.membership(distanceMid, distance)
plt.plot(distance, distanceMidMf, label="Mid")
distanceFar = ["trapmf", 6000, 8000, math.inf, math.inf]
distanceFarMf = mf.membership(distanceFar, distance)
plt.plot(distance, distanceFarMf, label="Far")
plt.xlabel('Distance (m)')
예제 #11
0
import OperationUnion as uni
import OperationIntersection as inter
import OperationImplication as imp
import Defuzzifier as defuz
import math
import matplotlib.pyplot as plt

inpJarak = [500]
inpJarakMf = []
inpLuas = [10]
inpLuasMf = []
inpFasilitas = [60]
inpFasilitasMf = []

#Domain
jarak = mf.linspace(0, 10000, 10001)
luas = mf.linspace(0, 20, 2001)
fasilitas = mf.linspace(0, 100, 101)
harga = mf.linspace(250, 1000, 75001)

#Fuzzy Set
jarakDekat = ["trapmf", -math.inf, -math.inf, 2000, 4000]
jarakDekatMf = mf.membership(jarakDekat, jarak)
plt.plot(jarak, jarakDekatMf)
jarakSedang = ["trapmf", 2000, 4000, 6000, 8000]
jarakSedangMf = mf.membership(jarakSedang, jarak)
plt.plot(jarak, jarakSedangMf)
jarakJauh = ["trapmf", 6000, 8000, math.inf, math.inf]
jarakJauhMf = mf.membership(jarakJauh, jarak)
plt.plot(jarak, jarakJauhMf)
plt.show()