AddVariable() публичный Метод

Adds a linguistic variable to the database.
The linguistic variable was not initialized. The linguistic variable name already exists in the database.
public AddVariable ( LinguisticVariable variable ) : void
variable LinguisticVariable A linguistic variable to add.
Результат void
Пример #1
0
        public InferenceSystem SetupInferenceSystem(int width)
        {
            var mp = new TrapezoidalFunction(centerPoint - width/2, centerPoint, centerPoint + width/2);
            var mn = new TrapezoidalFunction(-centerPoint - width/2, -centerPoint, -centerPoint + width/2);
            var sp = new TrapezoidalFunction(-centerPoint/2 + width/4, (double) centerPoint/2, centerPoint/2 + width/3);
            var sn = new TrapezoidalFunction(-centerPoint/2 - width/3, (double) -centerPoint/2, centerPoint/2 - width/4);
            var ze = new TrapezoidalFunction(-(double)centerPoint / 4, 0, (double)centerPoint / 4);

            var mpSet = new FuzzySet("MP", mp);
            var mnSet = new FuzzySet("MN", mn);
            var spSet = new FuzzySet("SP", sp);
            var snSet = new FuzzySet("SN", sn);
            var zeSet = new FuzzySet("ZE", ze);

            var ruleDatabase = new Database();

            for (int i = 0; i < windowSize*windowSize - 1; i++)
            {
                var variable = new LinguisticVariable(String.Format("IN{0}", i), -255, 255);
                variable.AddLabel(mpSet);
                variable.AddLabel(mnSet);
                ruleDatabase.AddVariable(variable);
            }

            var outVariable = new LinguisticVariable("OUT", -centerPoint - width/2, centerPoint + width/2);
            outVariable.AddLabel(spSet);
            outVariable.AddLabel(snSet);
            outVariable.AddLabel(zeSet);
            ruleDatabase.AddVariable(outVariable);
            var inferenceSystem = new InferenceSystem(ruleDatabase, new CentroidDefuzzifier(100));
            string rule1 = "IF ";
            string rule2 = "IF ";
            string rule3 = "IF ";
            for (int i = 0; i < windowSize*windowSize - 1; i++)
            {
                rule1 += String.Format("IN{0} is MP and ", i);
                rule2 += String.Format("IN{0} is MN and ", i);
                rule3 += String.Format("IN{0} is not MP and IN{0} is not MN AND ", i);
            }

            rule1 = rule1.Remove(rule1.Length - 4, 4);
            rule2 = rule2.Remove(rule2.Length - 4, 4);
            rule3 = "IF NOT (" + rule1.Replace("IF", "") + ") AND NOT(" + rule2.Replace("IF", "") + ")";

            rule1 += " then OUT is SN";
            rule2 += " then OUT is SP";
            rule3 += " then OUT is ZE";

            inferenceSystem.NewRule("Rule1", rule1);
            inferenceSystem.NewRule("Rule2", rule2);
            inferenceSystem.NewRule("Rule3", rule3);

            return inferenceSystem;
        }
Пример #2
0
        public void SetInput1()
        {
            this.Temp_opon = new LinguisticVariable("Temperatura", 0, 180);

            TrapezoidalFunction function1 = new TrapezoidalFunction(30, 90, TrapezoidalFunction.EdgeType.Right);
            FuzzySet            set1      = new FuzzySet("zimne", function1);
            TrapezoidalFunction function2 = new TrapezoidalFunction(70, 90, 110);
            FuzzySet            set2      = new FuzzySet("średnie", function2);
            TrapezoidalFunction function3 = new TrapezoidalFunction(90, 150, TrapezoidalFunction.EdgeType.Left);
            FuzzySet            set3      = new FuzzySet("gorące", function3);

            Temp_opon.AddLabel(set1);
            Temp_opon.AddLabel(set2);
            Temp_opon.AddLabel(set3);

            database.AddVariable(Temp_opon);

            double y1;
            double y2;
            double y3;

            for (float x = 0; x < 180; x += 0.5f)
            {
                if (Temp_opon.GetLabelMembership("zimne", x + 0.5f) + Temp_opon.GetLabelMembership("zimne", x) > 0)
                {
                    y1 = Temp_opon.GetLabelMembership("zimne", x);
                    chart4.Series["Zimne"].Points.AddXY(x, y1);
                }
                if (Temp_opon.GetLabelMembership("średnie", x + 0.5f) + Temp_opon.GetLabelMembership("średnie", x) > 0)
                {
                    y2 = Temp_opon.GetLabelMembership("średnie", x);
                    chart4.Series["Średnie"].Points.AddXY(x, y2);
                }
                if (Temp_opon.GetLabelMembership("gorące", x + 0.5f) + Temp_opon.GetLabelMembership("gorące", x) > 0)
                {
                    y3 = Temp_opon.GetLabelMembership("gorące", x);
                    chart4.Series["Gorące"].Points.AddXY(x, y3);
                }
            }
        }
Пример #3
0
        // Hardcode initializing the Fuzzy Inference System
        void InitFuzzyEngine( )
        {

            // Linguistic labels (fuzzy sets) that compose the distances
            FuzzySet fsNear = new FuzzySet( "Near", new TrapezoidalFunction( 15, 50, TrapezoidalFunction.EdgeType.Right ) );
            FuzzySet fsMedium = new FuzzySet( "Medium", new TrapezoidalFunction( 15, 50, 60, 100 ) );
            FuzzySet fsFar = new FuzzySet( "Far", new TrapezoidalFunction( 60, 100, TrapezoidalFunction.EdgeType.Left ) );

            // Right Distance (Input)
            LinguisticVariable lvRight = new LinguisticVariable( "RightDistance", 0, 120 );
            lvRight.AddLabel( fsNear );
            lvRight.AddLabel( fsMedium );
            lvRight.AddLabel( fsFar );

            // Left Distance (Input)
            LinguisticVariable lvLeft = new LinguisticVariable( "LeftDistance", 0, 120 );
            lvLeft.AddLabel( fsNear );
            lvLeft.AddLabel( fsMedium );
            lvLeft.AddLabel( fsFar );

            // Front Distance (Input)
            LinguisticVariable lvFront = new LinguisticVariable( "FrontalDistance", 0, 120 );
            lvFront.AddLabel( fsNear );
            lvFront.AddLabel( fsMedium );
            lvFront.AddLabel( fsFar );

            // Linguistic labels (fuzzy sets) that compose the angle
            FuzzySet fsVN = new FuzzySet( "VeryNegative", new TrapezoidalFunction( -40, -35, TrapezoidalFunction.EdgeType.Right ) );
            FuzzySet fsN = new FuzzySet( "Negative", new TrapezoidalFunction( -40, -35, -25, -20 ) );
            FuzzySet fsLN = new FuzzySet( "LittleNegative", new TrapezoidalFunction( -25, -20, -10, -5 ) );
            FuzzySet fsZero = new FuzzySet( "Zero", new TrapezoidalFunction( -10, 5, 5, 10 ) );
            FuzzySet fsLP = new FuzzySet( "LittlePositive", new TrapezoidalFunction( 5, 10, 20, 25 ) );
            FuzzySet fsP = new FuzzySet( "Positive", new TrapezoidalFunction( 20, 25, 35, 40 ) );
            FuzzySet fsVP = new FuzzySet( "VeryPositive", new TrapezoidalFunction( 35, 40, TrapezoidalFunction.EdgeType.Left ) );

            // Angle
            LinguisticVariable lvAngle = new LinguisticVariable( "Angle", -50, 50 );
            lvAngle.AddLabel( fsVN );
            lvAngle.AddLabel( fsN );
            lvAngle.AddLabel( fsLN );
            lvAngle.AddLabel( fsZero );
            lvAngle.AddLabel( fsLP );
            lvAngle.AddLabel( fsP );
            lvAngle.AddLabel( fsVP );

            // The database
            Database fuzzyDB = new Database( );
            fuzzyDB.AddVariable( lvFront );
            fuzzyDB.AddVariable( lvLeft );
            fuzzyDB.AddVariable( lvRight );
            fuzzyDB.AddVariable( lvAngle );

            // Creating the inference system
            IS = new InferenceSystem( fuzzyDB, new CentroidDefuzzifier( 1000 ) );

            // Going Straight
            IS.NewRule( "Rule 1", "IF FrontalDistance IS Far THEN Angle IS Zero" );
            // Going Straight (if can go anywhere)
            IS.NewRule( "Rule 2", "IF FrontalDistance IS Far AND RightDistance IS Far AND LeftDistance IS Far THEN Angle IS Zero" );
            // Near right wall
            IS.NewRule( "Rule 3", "IF RightDistance IS Near AND LeftDistance IS Not Near THEN Angle IS LittleNegative" );
            // Near left wall
            IS.NewRule("Rule 4", "IF RightDistance IS Not Near AND LeftDistance IS Near THEN Angle IS LittlePositive");
            // Near front wall - room at right
            IS.NewRule( "Rule 5", "IF RightDistance IS Far AND FrontalDistance IS Near THEN Angle IS Positive" );
            // Near front wall - room at left
            IS.NewRule( "Rule 6", "IF LeftDistance IS Far AND FrontalDistance IS Near THEN Angle IS Negative" );
            // Near front wall - room at both sides - go right
            IS.NewRule( "Rule 7", "IF RightDistance IS Far AND LeftDistance IS Far AND FrontalDistance IS Near THEN Angle IS Positive" );
        }
        private async Task<InferenceSystem> InitFuzzyEngineDiagnosis(long[] symptomesId, long[] diagnosesId)
        {
            var fuzzyDB = new AForge.Fuzzy.Database();

            LinguisticVariable lv = new LinguisticVariable("Diagnosis", 0, diagnosesId.Count() * 100);

            fuzzyDB.AddVariable(lv);

            var diagnosesRepo = _unitOfWork.RepositoryAsync<Diagnosis>();

            var diagnosis = diagnosesRepo.Query(d => diagnosesId.Any(did => did == d.Id)).Select().Distinct().OrderBy(d => d.Id).ToList();

            var i = 0;
            foreach (var diagnoses in diagnosis)
            {
                i++;

                lv.AddLabel(new FuzzySet("Diagnosis" + i, new TrapezoidalFunction(
                (i-1) * 100, i * 100 - 50, i * 100 - 50, i * 100)));
                
                foreach(var s in diagnoses.Symptoms)
                {
                    LinguisticVariable lvs = new LinguisticVariable(s.Symptom.Name, 0, 100);
                    lvs.AddLabel(SymptomFuzzySet.Common);

                    try
                    {
                        fuzzyDB.AddVariable(lvs);
                    }
                    catch(Exception exc)
                    {

                    }
                }
            }

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

            i = 0;
            foreach (var diagnoses in diagnosis)
            {
                i++;
                IS.NewRule(diagnoses.RuleName, diagnoses.Rule + i);
            }

            foreach (var diagnoses in diagnosis)
            {
                foreach (var s in diagnoses.Symptoms)
                {
                    if (!symptomesId.Any(sid => sid == s.SymptomId))
                    {
                        IS.SetInput(s.Symptom.Name, 1);
                    }
                }
            }

            return IS;
        }
        private InferenceSystem SetupInferenceSystem(byte minLuma, byte maxLuma, byte meanLuma)
        {
            var lumaIn = new LinguisticVariable("LumaIn", minLuma, maxLuma);
            var lumaOut = new LinguisticVariable("LumaOut", 0, 255);

            var darkFunction = new TrapezoidalFunction(minLuma, meanLuma, TrapezoidalFunction.EdgeType.Right);
            var darkSet = new FuzzySet("Dark", darkFunction);

            var mediumFunction = new TrapezoidalFunction(minLuma, meanLuma, maxLuma);
            var mediumSet = new FuzzySet("Medium", mediumFunction);

            var lightFunction = new TrapezoidalFunction(meanLuma, maxLuma, TrapezoidalFunction.EdgeType.Left);
            var lightSet = new FuzzySet("Light", lightFunction);

            lumaIn.AddLabel(darkSet);
            lumaIn.AddLabel(mediumSet);
            lumaIn.AddLabel(lightSet);

            var whiteFunction = new SingletonFunction(255);
            var whiteSet = new FuzzySet("White", whiteFunction);

            var blackFunction = new SingletonFunction(0);
            var blackSet = new FuzzySet("Black", blackFunction);

            var grayFunction = new SingletonFunction(128);
            var graySet = new FuzzySet("Gray", grayFunction);

            lumaOut.AddLabel(blackSet);
            lumaOut.AddLabel(graySet);
            lumaOut.AddLabel(whiteSet);

            var database = new Database();
            database.AddVariable(lumaIn);
            database.AddVariable(lumaOut);

            var inferenceSystem = new InferenceSystem(database, new CogDefuzzifier());
            inferenceSystem.NewRule("Rule 1", "IF LumaIn IS Dark THEN LumaOut is Black");
            inferenceSystem.NewRule("Rule 2", "IF LumaIn IS Medium THEN LumaOut is Gray");
            inferenceSystem.NewRule("Rule 3", "IF LumaIn IS Light THEN LumaOut is White");

            return inferenceSystem;
        }
Пример #6
0
        private void InitFuzzyEngineFor8Person()
        {
            //Superficie Captacion SC
            FuzzySet scChico = new FuzzySet("Chico", new TrapezoidalFunction(300, 400, TrapezoidalFunction.EdgeType.Right));
            FuzzySet scMediano = new FuzzySet("Mediano", new TrapezoidalFunction(300, 500, 700));
            FuzzySet scGrande = new FuzzySet("Grande", new TrapezoidalFunction(500, 700, 900));
            FuzzySet scMuyGrande = new FuzzySet("MuyGrande", new TrapezoidalFunction(800, 1000, TrapezoidalFunction.EdgeType.Left));

            LinguisticVariable lvSuperficieCaptacion = new LinguisticVariable("SC", 0, 1200);
            lvSuperficieCaptacion.AddLabel(scChico);
            lvSuperficieCaptacion.AddLabel(scMediano);
            lvSuperficieCaptacion.AddLabel(scGrande);
            lvSuperficieCaptacion.AddLabel(scMuyGrande);

            //volumen del sistema de almacenamiento VA
            FuzzySet vaPequenio = new FuzzySet("Pequenio", new TrapezoidalFunction(30, 40, TrapezoidalFunction.EdgeType.Right));
            FuzzySet vaIntermedio = new FuzzySet("Intermedio", new TrapezoidalFunction(30, 50, 70));
            FuzzySet vaConsiderable = new FuzzySet("Considerable", new TrapezoidalFunction(60, 70, TrapezoidalFunction.EdgeType.Left));

            LinguisticVariable lvVolumenAlmacenamiento = new LinguisticVariable("VA", 0, 100);
            lvVolumenAlmacenamiento.AddLabel(vaPequenio);
            lvVolumenAlmacenamiento.AddLabel(vaIntermedio);
            lvVolumenAlmacenamiento.AddLabel(vaConsiderable);

            //precipitaciones pluviales PP
            FuzzySet ppMuyBaja = new FuzzySet("MuyBaja", new TrapezoidalFunction(20, 40, TrapezoidalFunction.EdgeType.Right));
            FuzzySet ppBaja = new FuzzySet("Baja", new TrapezoidalFunction(20, 40, 60));
            FuzzySet ppMedia = new FuzzySet("Media", new TrapezoidalFunction(40, 70, 100));
            FuzzySet ppAlta = new FuzzySet("Alta", new TrapezoidalFunction(80, 130, 180));
            FuzzySet ppMuyAlta = new FuzzySet("MuyAlta", new TrapezoidalFunction(150, 180, TrapezoidalFunction.EdgeType.Left));

            LinguisticVariable lvPrecipitacionesPluviales = new LinguisticVariable("PP", 0, 300);
            lvPrecipitacionesPluviales.AddLabel(ppMuyBaja);
            lvPrecipitacionesPluviales.AddLabel(ppBaja);
            lvPrecipitacionesPluviales.AddLabel(ppMedia);
            lvPrecipitacionesPluviales.AddLabel(ppAlta);
            lvPrecipitacionesPluviales.AddLabel(ppMuyAlta);

            //consumo C (OUTPUT)
            FuzzySet cBajo = new FuzzySet("Bajo", new TrapezoidalFunction(4000, 4400, TrapezoidalFunction.EdgeType.Right));
            FuzzySet cPromedio = new FuzzySet("Promedio", new TrapezoidalFunction(4000, 4400, 5000));
            FuzzySet dAlto = new FuzzySet("Alto", new TrapezoidalFunction(4600, 5000, TrapezoidalFunction.EdgeType.Left));

            LinguisticVariable lvConsumo = new LinguisticVariable("C", 0, 6000);
            lvConsumo.AddLabel(cBajo);
            lvConsumo.AddLabel(cPromedio);
            lvConsumo.AddLabel(dAlto);

            Database fuzzyDB = new Database();
            fuzzyDB.AddVariable(lvSuperficieCaptacion);
            fuzzyDB.AddVariable(lvVolumenAlmacenamiento);
            fuzzyDB.AddVariable(lvPrecipitacionesPluviales);
            fuzzyDB.AddVariable(lvConsumo);

            CentroidDefuzzifier centroide = new CentroidDefuzzifier(1000);
            SetInferenceSystemAndRules(GetInferenceSystemIndex(8), fuzzyDB, centroide);
        }
Пример #7
0
 private void setupDatabase()
 {
     db = new Database();
     db.AddVariable(lvWheelAngle);
     db.AddVariable(lvSteeringWheel);
     db.AddVariable(lvFriction);
     db.AddVariable(lvSpeed);
     db.AddVariable(lvAccelerator);
     db.AddVariable(lvBrake);
 }
Пример #8
0
        public static void InitFuzzyEngineDiagnosis2()
        {
            FuzzySet fsCommon0 = new FuzzySet("Common0", new TrapezoidalFunction(-50, -25, 0, 5));
            FuzzySet fsCommon = new FuzzySet("Common", new TrapezoidalFunction(0, 35, 70, 100));

            //IF PAD IS Common AND OS IS Common AND G IS Common AND SHVG IS Common AND VGBHYVG IS Common AND GBPDKH IS Common
            //AND NK IS Common AND US IS Common AND GBVZO IS Common AND GBVO IS Common AND GBVI IS Common AND MMPG IS Common
            //AND NRRSRKH IS Common
            //THEN Diagnosis IS Diagnosis1

            // right Distance (Input)
            LinguisticVariable SN = new LinguisticVariable("SN", -50, 100);
            SN.AddLabel(fsCommon0);
            SN.AddLabel(fsCommon);
            LinguisticVariable PAD = new LinguisticVariable("PAD", -50, 100);
            PAD.AddLabel(fsCommon0);
            PAD.AddLabel(fsCommon);
            LinguisticVariable OS = new LinguisticVariable("OS", -50, 100);
            OS.AddLabel(fsCommon0);
            OS.AddLabel(fsCommon);
            LinguisticVariable G = new LinguisticVariable("G", -50, 100);
            G.AddLabel(fsCommon0);
            G.AddLabel(fsCommon);
            LinguisticVariable SHVG = new LinguisticVariable("SHVG", -50, 100);
            SHVG.AddLabel(fsCommon0);
            SHVG.AddLabel(fsCommon);
            LinguisticVariable VGBHYVG = new LinguisticVariable("VGBHYVG", -50, 100);
            VGBHYVG.AddLabel(fsCommon0);
            VGBHYVG.AddLabel(fsCommon);
            LinguisticVariable GBPDKH = new LinguisticVariable("GBPDKH", -50, 100);
            GBPDKH.AddLabel(fsCommon0);
            GBPDKH.AddLabel(fsCommon);
            LinguisticVariable NK = new LinguisticVariable("NK", -50, 100);
            NK.AddLabel(fsCommon0);
            NK.AddLabel(fsCommon);
            LinguisticVariable US = new LinguisticVariable("US", -50, 100);
            US.AddLabel(fsCommon0);
            US.AddLabel(fsCommon);
            LinguisticVariable GBVZO = new LinguisticVariable("GBVZO", -50, 100);
            GBVZO.AddLabel(fsCommon0);
            GBVZO.AddLabel(fsCommon);
            LinguisticVariable GBVO = new LinguisticVariable("GBVO", -50, 100);
            GBVO.AddLabel(fsCommon0);
            GBVO.AddLabel(fsCommon);
            LinguisticVariable GBVI = new LinguisticVariable("GBVI", -50, 100);
            GBVI.AddLabel(fsCommon0);
            GBVI.AddLabel(fsCommon);
            LinguisticVariable MMPG = new LinguisticVariable("MMPG", -50, 100);
            MMPG.AddLabel(fsCommon0);
            MMPG.AddLabel(fsCommon);
            LinguisticVariable NRRSRKH = new LinguisticVariable("NRRSRKH", -50, 100);
            NRRSRKH.AddLabel(fsCommon0);
            NRRSRKH.AddLabel(fsCommon);

            // linguistic labels (fuzzy sets) that compose the angle
            FuzzySet fsVN = new FuzzySet("Diagnosis1", new TrapezoidalFunction(0, 50, 50, 100));
            // linguistic labels (fuzzy sets) that compose the angle
            FuzzySet fsVN2 = new FuzzySet("Diagnosis2", new TrapezoidalFunction(100, 150, 150, 200));
            // angle
            LinguisticVariable diagnosis = new LinguisticVariable("Diagnosis", 0, 200);
            diagnosis.AddLabel(fsVN);
            diagnosis.AddLabel(fsVN2);

            // the database
            Database fuzzyDB = new Database();
            fuzzyDB.AddVariable(SN);
            fuzzyDB.AddVariable(PAD);
            fuzzyDB.AddVariable(OS);
            fuzzyDB.AddVariable(G);
            fuzzyDB.AddVariable(SHVG);
            fuzzyDB.AddVariable(VGBHYVG);
            fuzzyDB.AddVariable(GBPDKH);
            fuzzyDB.AddVariable(NK);
            fuzzyDB.AddVariable(US);
            fuzzyDB.AddVariable(GBVZO);
            fuzzyDB.AddVariable(GBVO);
            fuzzyDB.AddVariable(GBVI);
            fuzzyDB.AddVariable(MMPG);
            fuzzyDB.AddVariable(NRRSRKH);
            fuzzyDB.AddVariable(diagnosis);

            // creating the inference system
            IS = new InferenceSystem(fuzzyDB, new CentroidDefuzzifier(1000));

            //// going Straight
            //IS.NewRule("Rule 1", @"IF PAD IS Common AND OS IS Common
            //THEN Diagnosis IS Diagnosis1");
            //IS.NewRule("Rule 2", @"IF SN IS Common
            //THEN Diagnosis IS Diagnosis2");

            // going Straight
            IS.NewRule("Rule 1", @"IF PAD IS Common AND OS IS Common AND G IS Common AND SHVG IS Common
                AND VGBHYVG IS Common AND GBPDKH IS Common AND NK IS Common AND US IS Common
                AND GBVZO IS Common AND GBVO IS Common AND GBVI IS Common AND MMPG IS Common AND NRRSRKH IS Common
                THEN Diagnosis IS Diagnosis1");
            IS.NewRule("Rule 2", @"IF VGBHYVG IS Common AND GBPDKH IS Common AND GBVZO IS Common AND GBVO IS Common AND SN IS Common
                THEN Diagnosis IS Diagnosis2");

            //// going Straight
            //IS.NewRule("Rule 1", "IF FrontalDistance IS Far THEN Angle IS Zero");
            //// going Straight (if can go anywhere)
            //IS.NewRule("Rule 2", "IF FrontalDistance IS Far AND RightDistance IS Far AND " +
            //    "LeftDistance IS Far THEN Angle IS Zero");
            //// near right wall
            //IS.NewRule("Rule 3", "IF RightDistance IS Near AND LeftDistance IS Medium " +
            //    "THEN Angle IS LittleNegative");
            //// near left wall
            //IS.NewRule("Rule 4", "IF RightDistance IS Medium AND LeftDistance IS Near " +
            //    "THEN Angle IS LittlePositive");
            //// near front wall - room at right
            //IS.NewRule("Rule 5", "IF RightDistance IS Far AND FrontalDistance IS Near " +
            //    "THEN Angle IS Positive");
            //// near front wall - room at left
            //IS.NewRule("Rule 6", "IF LeftDistance IS Far AND FrontalDistance IS Near " +
            //    "THEN Angle IS Negative");
            //// near front wall - room at both sides - go right
            //IS.NewRule("Rule 7", "IF RightDistance IS Far AND LeftDistance IS Far AND " +
            //    "FrontalDistance IS Near THEN Angle IS Positive");
        }
Пример #9
0
        public static void InitFuzzyEngineDiagnosis()
        {
            // linguistic labels (fuzzy sets) that compose the distances
            FuzzySet fsNear = new FuzzySet("Near", new TrapezoidalFunction(
                0, 25, 25, 50));
            FuzzySet fsMedium = new FuzzySet("Medium", new TrapezoidalFunction(
                50, 75, 75, 100));
            FuzzySet fsFar = new FuzzySet("Far", new TrapezoidalFunction(
                100, 150, 150, 200));

            //IF PAD IS Common AND OS IS Common AND G IS Common AND SHVG IS Common AND VGBHYVG IS Common AND GBPDKH IS Common
            //AND NK IS Common AND US IS Common AND GBVZO IS Common AND GBVO IS Common AND GBVI IS Common AND MMPG IS Common
            //AND NRRSRKH IS Common
            //THEN Diagnosis IS Diagnosis1

            // right Distance (Input)
            LinguisticVariable lvRight = new LinguisticVariable("RightDistance", 0, 00);
            lvRight.AddLabel(fsNear);
            lvRight.AddLabel(fsMedium);
            lvRight.AddLabel(fsFar);

            // left Distance (Input)
            LinguisticVariable lvLeft = new LinguisticVariable("LeftDistance", 0, 200);
            lvLeft.AddLabel(fsNear);
            lvLeft.AddLabel(fsMedium);
            lvLeft.AddLabel(fsFar);

            // front Distance (Input)
            LinguisticVariable lvFront = new LinguisticVariable("FrontalDistance", 0, 200);
            lvFront.AddLabel(fsNear);
            lvFront.AddLabel(fsMedium);
            lvFront.AddLabel(fsFar);

            // linguistic labels (fuzzy sets) that compose the angle
            FuzzySet fsVN = new FuzzySet("VeryNegative", new TrapezoidalFunction(
                -40, -35, TrapezoidalFunction.EdgeType.Right));
            FuzzySet fsN = new FuzzySet("Negative", new TrapezoidalFunction(
                -40, -35, -25, -20));
            FuzzySet fsLN = new FuzzySet("LittleNegative", new TrapezoidalFunction(
                -25, -20, -10, -5));
            FuzzySet fsZero = new FuzzySet("Zero", new TrapezoidalFunction(
                -10, 5, 5, 10));
            FuzzySet fsLP = new FuzzySet("LittlePositive", new TrapezoidalFunction(
                5, 10, 20, 25));
            FuzzySet fsP = new FuzzySet("Positive", new TrapezoidalFunction(
                5, 25, 35, 40));
            FuzzySet fsVP = new FuzzySet("VeryPositive", new TrapezoidalFunction(
                35, 40, TrapezoidalFunction.EdgeType.Left));

            // angle
            LinguisticVariable lvAngle = new LinguisticVariable("Angle", -50, 50);
            lvAngle.AddLabel(fsVN);
            lvAngle.AddLabel(fsN);
            lvAngle.AddLabel(fsLN);
            lvAngle.AddLabel(fsZero);
            lvAngle.AddLabel(fsLP);
            lvAngle.AddLabel(fsP);
            lvAngle.AddLabel(fsVP);

            // the database
            Database fuzzyDB = new Database();
            fuzzyDB.AddVariable(lvFront);
            fuzzyDB.AddVariable(lvLeft);
            fuzzyDB.AddVariable(lvRight);
            fuzzyDB.AddVariable(lvAngle);

            // creating the inference system
            IS = new InferenceSystem(fuzzyDB, new CentroidDefuzzifier(1000));

            // going Straight
            IS.NewRule("Rule 1", "IF RightDistance IS Far AND LeftDistance IS Far THEN Angle IS VeryNegative");
            // going Straight
            IS.NewRule("Rule 2", "IF FrontalDistance IS Far THEN Angle IS Negative");
            IS.NewRule("Rule 3", "IF FrontalDistance IS Far AND RightDistance IS Far AND LeftDistance IS Far THEN Angle IS LittleNegative");

            //// going Straight
            //IS.NewRule("Rule 1", "IF FrontalDistance IS Far THEN Angle IS Zero");
            //// going Straight (if can go anywhere)
            //IS.NewRule("Rule 2", "IF FrontalDistance IS Far AND RightDistance IS Far AND " +
            //    "LeftDistance IS Far THEN Angle IS Zero");
            //// near right wall
            //IS.NewRule("Rule 3", "IF RightDistance IS Near AND LeftDistance IS Medium " +
            //    "THEN Angle IS LittleNegative");
            //// near left wall
            //IS.NewRule("Rule 4", "IF RightDistance IS Medium AND LeftDistance IS Near " +
            //    "THEN Angle IS LittlePositive");
            //// near front wall - room at right
            //IS.NewRule("Rule 5", "IF RightDistance IS Far AND FrontalDistance IS Near " +
            //    "THEN Angle IS Positive");
            //// near front wall - room at left
            //IS.NewRule("Rule 6", "IF LeftDistance IS Far AND FrontalDistance IS Near " +
            //    "THEN Angle IS Negative");
            //// near front wall - room at both sides - go right
            //IS.NewRule("Rule 7", "IF RightDistance IS Far AND LeftDistance IS Far AND " +
            //    "FrontalDistance IS Near THEN Angle IS Positive");
        }
Пример #10
0
        public FuzzyEngine()
        {
            // Linguistic labels (fuzzy sets) for Momentum
            FuzzySet momDown = new FuzzySet("Down", new TrapezoidalFunction(-20, 5, 5, 5));
            FuzzySet momNeutral = new FuzzySet("Neutral", new TrapezoidalFunction(-20, 0, 0, 20));
            FuzzySet momUp = new FuzzySet("Up", new TrapezoidalFunction(5, 20, 20, 20));


            // Linguistic labels (fuzzy sets) for RSI
            FuzzySet rsiLow = new FuzzySet("Low", new TrapezoidalFunction(0, 30, 30, 30));
            FuzzySet rsiMedium = new FuzzySet("Medium", new TrapezoidalFunction(0, 50, 50, 100));
            FuzzySet rsiHigh = new FuzzySet("High", new TrapezoidalFunction(70, 100, 100, 100));

            // MOM (Input)
            LinguisticVariable lvMom = new LinguisticVariable("MOM", -20, 20);
            lvMom.AddLabel(momDown);
            lvMom.AddLabel(momNeutral);
            lvMom.AddLabel(momUp);

            // RSI (Input)
            LinguisticVariable lvRsi = new LinguisticVariable("RSI", 0, 100);
            lvRsi.AddLabel(rsiLow);
            lvRsi.AddLabel(rsiMedium);
            lvRsi.AddLabel(rsiHigh);

            // Linguistic labels (fuzzy sets) that compose the Signal
            FuzzySet fsShort = new FuzzySet("Sell", new TrapezoidalFunction(-100, 0, 0, 00));
            FuzzySet fsHold = new FuzzySet("Hold", new TrapezoidalFunction(-50, 0, 0, 50));
            FuzzySet fsLong = new FuzzySet("Buy", new TrapezoidalFunction(0, 100, 100, 100));

            // Output
            LinguisticVariable lvSignal = new LinguisticVariable("Signal", -100, 100);
            lvSignal.AddLabel(fsShort);
            lvSignal.AddLabel(fsHold);
            lvSignal.AddLabel(fsLong);

            // The database
            Database fuzzyDB = new Database();
            fuzzyDB.AddVariable(lvMom);
            fuzzyDB.AddVariable(lvRsi);
            fuzzyDB.AddVariable(lvSignal);

            // Creating the inference system
            IS = new InferenceSystem(fuzzyDB, new CentroidDefuzzifier(1000));

            // Rules
            IS.NewRule("Rule 1", "IF RSI IS Low AND MOM IS Down THEN Signal IS Buy");
            IS.NewRule("Rule 2", "IF RSI IS Medium AND MOM IS Down THEN Signal IS Buy");
            IS.NewRule("Rule 3", "IF RSI IS High AND MOM IS Down THEN Signal IS Hold");

            IS.NewRule("Rule 4", "IF RSI IS Low AND MOM IS Neutral THEN Signal IS Buy");
            IS.NewRule("Rule 5", "IF RSI IS Medium AND MOM IS Neutral THEN Signal IS Hold");
            IS.NewRule("Rule 6", "IF RSI IS High AND MOM IS Neutral THEN Signal IS Sell");

            IS.NewRule("Rule 7", "IF RSI IS Low AND MOM IS Up THEN Signal IS Hold");
            IS.NewRule("Rule 8", "IF RSI IS Medium AND MOM IS Up THEN Signal IS Sell");
            IS.NewRule("Rule 9", "IF RSI IS High AND MOM IS Up THEN Signal IS Sell");



        }