AForge.NET

  :: AForge.NET Framework :: Articles :: Forums ::

Fuzzy logic in Unity

Forum to discuss AForge.NET Framework, its features, API, how-tos, etc.

Fuzzy logic in Unity

Postby Erven » Fri May 27, 2016 3:23 pm

Hello!
I'm trying to implement Fuzzy Logic into my Unity game. Enemy should take the Size, and Distance values of player, than decide is he should chase him or not, so Sugeno is the output I choose.
I've made a function that initializes the fuzzy and it goes like:

Code: Select all
    void InitFuzzyEngine ()
    {
        FuzzySet fsDistClose = new FuzzySet("Close", new TrapezoidalFunction(30, 70, TrapezoidalFunction.EdgeType.Right));
        FuzzySet fsDistMedium = new FuzzySet("Medium", new TrapezoidalFunction(30, 60, 80, 100));
        FuzzySet fsDistFar = new FuzzySet("Far", new TrapezoidalFunction(70, 100, TrapezoidalFunction.EdgeType.Left));     

        LinguisticVariable lvDistance = new LinguisticVariable("Distance", 0, 1500);
        lvDistance.AddLabel(fsDistClose);
        lvDistance.AddLabel(fsDistMedium);
        lvDistance.AddLabel(fsDistFar);

        FuzzySet fsSizeSmall = new FuzzySet("Small", new TrapezoidalFunction(2, 4, TrapezoidalFunction.EdgeType.Right));
        FuzzySet fsSizeModerate = new FuzzySet("Moderate", new TrapezoidalFunction(2, 4, 6, 10));
        FuzzySet fsSizeBig = new FuzzySet("Big", new TrapezoidalFunction(5, 10, TrapezoidalFunction.EdgeType.Left));

        LinguisticVariable lvSize = new LinguisticVariable("Size", 0, 300);
        lvSize.AddLabel(fsSizeSmall);
        lvSize.AddLabel(fsSizeModerate);
        lvSize.AddLabel(fsSizeBig);

        FuzzySet fsUndesirable = new FuzzySet("Undesirable", new TrapezoidalFunction(20, 50, TrapezoidalFunction.EdgeType.Right));
        FuzzySet fsDesirable = new FuzzySet("Desirable", new TrapezoidalFunction(20, 40, 50, 80));
        FuzzySet fsVeryDesirable = new FuzzySet("VeryDesirable", new TrapezoidalFunction(50, 80, TrapezoidalFunction.EdgeType.Left));     

        LinguisticVariable lvEffect = new LinguisticVariable("Effect", 0, 150);
        lvEffect.AddLabel(fsUndesirable);
        lvEffect.AddLabel(fsDesirable);
        lvEffect.AddLabel(fsVeryDesirable);

        Database fuzzyDB = new Database();
        fuzzyDB.AddVariable(lvDistance);
        fuzzyDB.AddVariable(lvSize);
        fuzzyDB.AddVariable(lvEffect);

        InferenceSystem IS = new InferenceSystem(fuzzyDB, new CentroidDefuzzifier(1000));

        IS.NewRule("Rule1", "IF Distance IS Far AND Size IS Big THAN Undesirable");
        IS.NewRule("Rule2", "IF Distance IS Far AND Size IS Moderate THAN Undesirable");
        IS.NewRule("Rule3", "IF Distance IS Far AND Size IS Small THAN Undesirable");

        IS.NewRule("Rule4", "IF Distance IS Medium AND Size IS Big THAN Desirable");
        IS.NewRule("Rule5", "IF Distance IS Medium AND Size IS Moderate THAN Desirable");
        IS.NewRule("Rule6", "IF Distance IS Medium AND Size IS Small THAN Undesirable");

        IS.NewRule("Rule7", "IF Distance IS Close AND Size IS Big THAN VeryDesirable");
        IS.NewRule("Rule8", "IF Distance IS Close AND Size IS Moderate THAN VeryDesirable");
        IS.NewRule("Rule9", "IF Distance IS Close AND Size IS Small THAN Desirable");

        IS.SetInput("Size", txtSize);
        IS.SetInput("Distance", txtDistance);

        try
        {
            newOutput = IS.Evaluate("Effect");
        }
        catch
        {
        }
         
    }


But it is not giving me an output. How to fix it?
Erven
 
Posts: 1
Joined: Fri May 27, 2016 2:53 pm



Return to AForge.NET Framework