Esempio n. 1
0
    // Start is called before the first frame update
    void Start()
    {
        controller = GetComponent <CarController>();

        var acceleration = new LinguisticVariable("Acceleration");
        var accelerate   = acceleration.MembershipFunctions.AddTrapezoid("Accelerate", 1, 1.5, 2, 2);
        var coast        = acceleration.MembershipFunctions.AddTriangle("Coast", 0.5, 1, 1.5);
        var decelerate   = acceleration.MembershipFunctions.AddTrapezoid("Decelerate", 0, 0, 0.5, 1);

        var distance = new LinguisticVariable("Distance");
        var close    = distance.MembershipFunctions.AddTrapezoid("Close", 0, 0, 6, 10);
        var far      = distance.MembershipFunctions.AddTrapezoid("Far", 5, 20, 100, 100);

        var deltaToTargetDistance = new LinguisticVariable("DistanceDelta");
        var below   = deltaToTargetDistance.MembershipFunctions.AddTrapezoid("Below", 0, 0, 5, 10);
        var onPoint = deltaToTargetDistance.MembershipFunctions.AddTriangle("Middle", 5, 10, 15);
        var above   = deltaToTargetDistance.MembershipFunctions.AddTrapezoid("Above", 10, 15, 20, 20);

        var rule1 = Rule.If(deltaToTargetDistance.Is(below).Or(distance.Is(close))).Then(acceleration.Is(decelerate));
        var rule2 = Rule.If(deltaToTargetDistance.Is(above).And(distance.IsNot(close))).Then(acceleration.Is(accelerate));
        var rule3 = Rule.If(deltaToTargetDistance.Is(onPoint)).Then(acceleration.Is(coast));

        fuzzyEngine = new FuzzyEngineFactory().Default();

        fuzzyEngine.Rules.Add(rule1);
        fuzzyEngine.Rules.Add(rule2);

        StartCoroutine(AICycle());
    }
        public void RuleValid_Success()
        {
            //Arrange
            LinguisticVariable water = new LinguisticVariable("Water");
            var cold = water.MembershipFunctions.AddTrapezoid("Cold", 0, 0, 20, 40);
            var warm = water.MembershipFunctions.AddTrapezoid("Warm", 30, 50, 50, 70);
            var hot  = water.MembershipFunctions.AddTrapezoid("Hot", 50, 80, 100, 100);

            LinguisticVariable power = new LinguisticVariable("Power");
            var low  = power.MembershipFunctions.AddTrapezoid("Low", 0, 25, 25, 50);
            var high = power.MembershipFunctions.AddTrapezoid("High", 25, 50, 50, 75);

            //Act
            var rule1 = new FuzzyRule().If(water.Is(cold)).Then(power.Is(high));                                        //valid
            var rule2 = new FuzzyRule().If(water.IsNot(cold)).Then(power.Is(high));                                     //valid
            var rule3 = new FuzzyRule().If(water.Is(cold).Or(water.Is(warm))).Then(power.Is(high));                     //valid
            var rule4 = new FuzzyRule().If(water.Is(cold).Or(water.Is(warm)).And(water.Is(hot))).Then(power.Is(high));  //valid
            var rule5 = new FuzzyRule().If(water.Is(cold).And(water.Is(warm)).And(water.Is(hot))).Then(power.Is(high)); //valid
            var rule6 = new FuzzyRule().If(water.Is(cold).Or(water.Is(warm)).Or(water.Is(hot))).Then(power.Is(high));   //valid

            var result1 = rule1.IsValid();
            var result2 = rule2.IsValid();
            var result3 = rule3.IsValid();
            var result4 = rule4.IsValid();
            var result5 = rule3.IsValid();
            var result6 = rule4.IsValid();

            //Assert
            Assert.That(result1, Is.True, "result1");
            Assert.That(result2, Is.True, "result2");
            Assert.That(result3, Is.True, "result3");
            Assert.That(result4, Is.True, "result4");
            Assert.That(result5, Is.True, "result5");
            Assert.That(result6, Is.True, "result6");
        }