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; }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { List <double> NumInputs = new List <double>(); List <LinguisticVariable> InputVars = new List <LinguisticVariable>(); List <LinguisticVariable> OutputVars = new List <LinguisticVariable>(); List <string> Rules = new List <string>(); AForge.Fuzzy.InferenceSystem IS = null; string VarName = null; double FailsafeVal = double.NaN; if (!DA.GetData(0, ref IS)) { return; } if (!DA.GetData(1, ref VarName)) { return; } if (!DA.GetData(2, ref FailsafeVal)) { return; } try { double outVal = IS.Evaluate(VarName); DA.SetData(0, outVal); DA.SetData(1, false); } catch (Exception) { DA.SetData(0, FailsafeVal); DA.SetData(1, true); } }
// 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; }
// Segun la investigación de Ricardo las reglas definidas son siempre las mismas para la distinta cantidad de personas. // Por eso este método es común a las 4 inicializaciones (2, 4, 6 y 8 personas). private void SetInferenceSystemAndRules(int is_index, Database fuzzyDB, CentroidDefuzzifier centroide) { IS[is_index] = new InferenceSystem(fuzzyDB, centroide); // SC Chico IS[is_index].NewRule("Rule 1", IF_IS("SC", "Chico") + AND_IS("VA", "Pequenio") + AND_IS("PP", "MuyBaja") + THEN_IS("C", "Bajo")); IS[is_index].NewRule("Rule 2", IF_IS("SC", "Chico") + AND_IS("VA", "Pequenio") + AND_IS("PP", "Baja") + THEN_IS("C", "Bajo")); IS[is_index].NewRule("Rule 3", IF_IS("SC", "Chico") + AND_IS("VA", "Pequenio") + AND_IS("PP", "Media") + THEN_IS("C", "Bajo")); IS[is_index].NewRule("Rule 4", IF_IS("SC", "Chico") + AND_IS("VA", "Pequenio") + AND_IS("PP", "Alta") + THEN_IS("C", "Promedio")); IS[is_index].NewRule("Rule 5", IF_IS("SC", "Chico") + AND_IS("VA", "Pequenio") + AND_IS("PP", "MuyAlta") + THEN_IS("C", "Promedio")); IS[is_index].NewRule("Rule 6", IF_IS("SC", "Chico") + AND_IS("VA", "Intermedio") + AND_IS("PP", "MuyBaja") + THEN_IS("C", "Bajo")); IS[is_index].NewRule("Rule 7", IF_IS("SC", "Chico") + AND_IS("VA", "Intermedio") + AND_IS("PP", "Baja") + THEN_IS("C", "Bajo")); IS[is_index].NewRule("Rule 8", IF_IS("SC", "Chico") + AND_IS("VA", "Intermedio") + AND_IS("PP", "Media") + THEN_IS("C", "Bajo")); IS[is_index].NewRule("Rule 9", IF_IS("SC", "Chico") + AND_IS("VA", "Intermedio") + AND_IS("PP", "Alta") + THEN_IS("C", "Promedio")); IS[is_index].NewRule("Rule 10", IF_IS("SC", "Chico") + AND_IS("VA", "Intermedio") + AND_IS("PP", "MuyAlta") + THEN_IS("C", "Promedio")); IS[is_index].NewRule("Rule 11", IF_IS("SC", "Chico") + AND_IS("VA", "Considerable") + AND_IS("PP", "MuyBaja") + THEN_IS("C", "Bajo")); IS[is_index].NewRule("Rule 12", IF_IS("SC", "Chico") + AND_IS("VA", "Considerable") + AND_IS("PP", "Baja") + THEN_IS("C", "Bajo")); IS[is_index].NewRule("Rule 13", IF_IS("SC", "Chico") + AND_IS("VA", "Considerable") + AND_IS("PP", "Media") + THEN_IS("C", "Bajo")); IS[is_index].NewRule("Rule 14", IF_IS("SC", "Chico") + AND_IS("VA", "Considerable") + AND_IS("PP", "Alta") + THEN_IS("C", "Promedio")); IS[is_index].NewRule("Rule 15", IF_IS("SC", "Chico") + AND_IS("VA", "Considerable") + AND_IS("PP", "MuyAlta") + THEN_IS("C", "Promedio")); // SC Mediano IS[is_index].NewRule("Rule 16", IF_IS("SC", "Mediano") + AND_IS("VA", "Pequenio") + AND_IS("PP", "MuyBaja") + THEN_IS("C", "Bajo")); IS[is_index].NewRule("Rule 17", IF_IS("SC", "Mediano") + AND_IS("VA", "Pequenio") + AND_IS("PP", "Baja") + THEN_IS("C", "Bajo")); IS[is_index].NewRule("Rule 18", IF_IS("SC", "Mediano") + AND_IS("VA", "Pequenio") + AND_IS("PP", "Media") + THEN_IS("C", "Promedio")); IS[is_index].NewRule("Rule 19", IF_IS("SC", "Mediano") + AND_IS("VA", "Pequenio") + AND_IS("PP", "Alta") + THEN_IS("C", "Promedio")); IS[is_index].NewRule("Rule 20", IF_IS("SC", "Mediano") + AND_IS("VA", "Pequenio") + AND_IS("PP", "MuyAlta") + THEN_IS("C", "Promedio")); IS[is_index].NewRule("Rule 21", IF_IS("SC", "Mediano") + AND_IS("VA", "Intermedio") + AND_IS("PP", "MuyBaja") + THEN_IS("C", "Bajo")); IS[is_index].NewRule("Rule 22", IF_IS("SC", "Mediano") + AND_IS("VA", "Intermedio") + AND_IS("PP", "Baja") + THEN_IS("C", "Bajo")); IS[is_index].NewRule("Rule 23", IF_IS("SC", "Mediano") + AND_IS("VA", "Intermedio") + AND_IS("PP", "Media") + THEN_IS("C", "Promedio")); IS[is_index].NewRule("Rule 24", IF_IS("SC", "Mediano") + AND_IS("VA", "Intermedio") + AND_IS("PP", "Alta") + THEN_IS("C", "Promedio")); IS[is_index].NewRule("Rule 25", IF_IS("SC", "Mediano") + AND_IS("VA", "Intermedio") + AND_IS("PP", "MuyAlta") + THEN_IS("C", "Alto")); IS[is_index].NewRule("Rule 26", IF_IS("SC", "Mediano") + AND_IS("VA", "Considerable") + AND_IS("PP", "MuyBaja") + THEN_IS("C", "Bajo")); IS[is_index].NewRule("Rule 27", IF_IS("SC", "Mediano") + AND_IS("VA", "Considerable") + AND_IS("PP", "Baja") + THEN_IS("C", "Bajo")); IS[is_index].NewRule("Rule 28", IF_IS("SC", "Mediano") + AND_IS("VA", "Considerable") + AND_IS("PP", "Media") + THEN_IS("C", "Promedio")); IS[is_index].NewRule("Rule 29", IF_IS("SC", "Mediano") + AND_IS("VA", "Considerable") + AND_IS("PP", "Alta") + THEN_IS("C", "Alto")); IS[is_index].NewRule("Rule 30", IF_IS("SC", "Mediano") + AND_IS("VA", "Considerable") + AND_IS("PP", "MuyAlta") + THEN_IS("C", "Alto")); // SC Grande IS[is_index].NewRule("Rule 31", IF_IS("SC", "Grande") + AND_IS("VA", "Pequenio") + AND_IS("PP", "MuyBaja") + THEN_IS("C", "Bajo")); IS[is_index].NewRule("Rule 32", IF_IS("SC", "Grande") + AND_IS("VA", "Pequenio") + AND_IS("PP", "Baja") + THEN_IS("C", "Promedio")); IS[is_index].NewRule("Rule 33", IF_IS("SC", "Grande") + AND_IS("VA", "Pequenio") + AND_IS("PP", "Media") + THEN_IS("C", "Promedio")); IS[is_index].NewRule("Rule 34", IF_IS("SC", "Grande") + AND_IS("VA", "Pequenio") + AND_IS("PP", "Alta") + THEN_IS("C", "Promedio")); IS[is_index].NewRule("Rule 35", IF_IS("SC", "Grande") + AND_IS("VA", "Pequenio") + AND_IS("PP", "MuyAlta") + THEN_IS("C", "Promedio")); IS[is_index].NewRule("Rule 36", IF_IS("SC", "Grande") + AND_IS("VA", "Intermedio") + AND_IS("PP", "MuyBaja") + THEN_IS("C", "Bajo")); IS[is_index].NewRule("Rule 37", IF_IS("SC", "Grande") + AND_IS("VA", "Intermedio") + AND_IS("PP", "Baja") + THEN_IS("C", "Promedio")); IS[is_index].NewRule("Rule 38", IF_IS("SC", "Grande") + AND_IS("VA", "Intermedio") + AND_IS("PP", "Media") + THEN_IS("C", "Promedio")); IS[is_index].NewRule("Rule 39", IF_IS("SC", "Grande") + AND_IS("VA", "Intermedio") + AND_IS("PP", "Alta") + THEN_IS("C", "Alto")); IS[is_index].NewRule("Rule 40", IF_IS("SC", "Grande") + AND_IS("VA", "Intermedio") + AND_IS("PP", "MuyAlta") + THEN_IS("C", "Alto")); IS[is_index].NewRule("Rule 41", IF_IS("SC", "Grande") + AND_IS("VA", "Considerable") + AND_IS("PP", "MuyBaja") + THEN_IS("C", "Bajo")); IS[is_index].NewRule("Rule 42", IF_IS("SC", "Grande") + AND_IS("VA", "Considerable") + AND_IS("PP", "Baja") + THEN_IS("C", "Promedio")); IS[is_index].NewRule("Rule 43", IF_IS("SC", "Grande") + AND_IS("VA", "Considerable") + AND_IS("PP", "Media") + THEN_IS("C", "Alto")); IS[is_index].NewRule("Rule 44", IF_IS("SC", "Grande") + AND_IS("VA", "Considerable") + AND_IS("PP", "Alta") + THEN_IS("C", "Alto")); IS[is_index].NewRule("Rule 45", IF_IS("SC", "Grande") + AND_IS("VA", "Considerable") + AND_IS("PP", "MuyAlta") + THEN_IS("C", "Alto")); // SC MuyGrande IS[is_index].NewRule("Rule 46", IF_IS("SC", "MuyGrande") + AND_IS("VA", "Pequenio") + AND_IS("PP", "MuyBaja") + THEN_IS("C", "Bajo")); IS[is_index].NewRule("Rule 47", IF_IS("SC", "MuyGrande") + AND_IS("VA", "Pequenio") + AND_IS("PP", "Baja") + THEN_IS("C", "Promedio")); IS[is_index].NewRule("Rule 48", IF_IS("SC", "MuyGrande") + AND_IS("VA", "Pequenio") + AND_IS("PP", "Media") + THEN_IS("C", "Promedio")); IS[is_index].NewRule("Rule 49", IF_IS("SC", "MuyGrande") + AND_IS("VA", "Pequenio") + AND_IS("PP", "Alta") + THEN_IS("C", "Promedio")); IS[is_index].NewRule("Rule 50", IF_IS("SC", "MuyGrande") + AND_IS("VA", "Pequenio") + AND_IS("PP", "MuyAlta") + THEN_IS("C", "Promedio")); IS[is_index].NewRule("Rule 51", IF_IS("SC", "MuyGrande") + AND_IS("VA", "Intermedio") + AND_IS("PP", "MuyBaja") + THEN_IS("C", "Bajo")); IS[is_index].NewRule("Rule 52", IF_IS("SC", "MuyGrande") + AND_IS("VA", "Intermedio") + AND_IS("PP", "Baja") + THEN_IS("C", "Promedio")); IS[is_index].NewRule("Rule 53", IF_IS("SC", "MuyGrande") + AND_IS("VA", "Intermedio") + AND_IS("PP", "Media") + THEN_IS("C", "Alto")); IS[is_index].NewRule("Rule 54", IF_IS("SC", "MuyGrande") + AND_IS("VA", "Intermedio") + AND_IS("PP", "Alta") + THEN_IS("C", "Alto")); IS[is_index].NewRule("Rule 55", IF_IS("SC", "MuyGrande") + AND_IS("VA", "Intermedio") + AND_IS("PP", "MuyAlta") + THEN_IS("C", "Alto")); IS[is_index].NewRule("Rule 56", IF_IS("SC", "MuyGrande") + AND_IS("VA", "Considerable") + AND_IS("PP", "MuyBaja") + THEN_IS("C", "Bajo")); IS[is_index].NewRule("Rule 57", IF_IS("SC", "MuyGrande") + AND_IS("VA", "Considerable") + AND_IS("PP", "Baja") + THEN_IS("C", "Alto")); IS[is_index].NewRule("Rule 58", IF_IS("SC", "MuyGrande") + AND_IS("VA", "Considerable") + AND_IS("PP", "Media") + THEN_IS("C", "Alto")); IS[is_index].NewRule("Rule 59", IF_IS("SC", "MuyGrande") + AND_IS("VA", "Considerable") + AND_IS("PP", "Alta") + THEN_IS("C", "Alto")); IS[is_index].NewRule("Rule 60", IF_IS("SC", "MuyGrande") + AND_IS("VA", "Considerable") + AND_IS("PP", "MuyAlta") + THEN_IS("C", "Alto")); }
private byte Modify(InferenceSystem system, int[] windowData, byte center) { for (int i = 0; i < windowSize*windowSize - 1; i++) { system.SetInput(String.Format("IN{0}", i), windowData[i]); } int x = center + (byte) system.Evaluate("OUT"); if (x < 0) { return 0; } if (x > 255) { return 255; } return (byte) x; }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { List <double> NumInputs = new List <double>(); List <LinguisticVariable> InputVars = new List <LinguisticVariable>(); List <LinguisticVariable> OutputVars = new List <LinguisticVariable>(); List <string> Rules = new List <string>(); if (!DA.GetDataList(0, NumInputs)) { return; } if (!DA.GetDataList(1, InputVars)) { return; } if (!DA.GetDataList(2, OutputVars)) { return; } if (!DA.GetDataList(3, Rules)) { return; } // creating the database Database fuzzyDB = new Database(); foreach (object x in InputVars) { LinguisticVariable LV = (LinguisticVariable)x; fuzzyDB.AddVariable(LV); } foreach (object y in OutputVars) { LinguisticVariable LV = (LinguisticVariable)y; fuzzyDB.AddVariable(LV); } // creating the inference system AForge.Fuzzy.InferenceSystem InfrSys = new AForge.Fuzzy.InferenceSystem(fuzzyDB, new AForge.Fuzzy.CentroidDefuzzifier(1000)); int C = Rules.Count; for (int i = 0; i <= C - 1; i++) { string ruleName = "Rule" + i; InfrSys.NewRule(ruleName, Rules[i]); } // Setting inputs int D = InputVars.Count; for (int j = 0; j <= D - 1; j++) { LinguisticVariable LV = (LinguisticVariable)InputVars[j]; double val = NumInputs[j]; InfrSys.SetInput(LV.Name, Convert.ToSingle(val)); } DA.SetData(0, InfrSys); }
private void setupInfSystem() { IS = new InferenceSystem(db, new CentroidDefuzzifier(1000)); #region WheelAngle IS.NewRule("TurnLeftMax", "IF SteeringWheel IS LeftMax THEN WheelAngle IS VeryNegative"); IS.NewRule("TurnLeftMed", "IF SteeringWheel IS LeftMedium THEN WheelAngle IS Negative"); IS.NewRule("TurnLeftMin", "IF SteeringWheel IS LeftMin THEN WheelAngle IS LittleNegative"); IS.NewRule("GoStraight", "IF SteeringWheel IS Middle THEN WheelAngle IS Zero"); IS.NewRule("TurnRightMin", "IF SteeringWheel IS RightMin THEN WheelAngle IS LittlePositive"); IS.NewRule("TurnRightMed", "IF SteeringWheel IS RightMedium THEN WheelAngle IS Positive"); IS.NewRule("TurnRightMax", "IF SteeringWheel IS RightMax THEN WheelAngle IS VeryPositive"); #endregion IS.NewRule("Rule 1", "IF Speed IS Stopped AND Accelerator IS Much THEN Speed IS Slow"); IS.NewRule("Rule 2", "IF Speed IS Stopped AND Accelerator IS Medium THEN Speed IS VerySlow"); IS.NewRule("Rule 3","IF Speed IS Stopped AND Accelerator IS Little THEN Speed IS VerySlow"); IS.NewRule("Rule 5", "IF Speed IS VerySlow AND Accelerator IS Much THEN Speed IS Regular"); IS.NewRule("Rule 6", "IF Speed IS VerySlow AND Accelerator IS Medium THEN Speed IS Slow"); IS.NewRule("Rule 7", "IF Speed IS VerySlow AND Accelerator IS Little THEN Speed IS VerySlow"); IS.NewRule("Rule 9", "IF Speed IS Slow AND Accelerator IS Much THEN Speed IS LittleFast"); IS.NewRule("Rule 10", "IF Speed IS Slow AND Accelerator IS Medium THEN Speed IS Regular"); IS.NewRule("Rule 11", "IF Speed IS Slow AND Accelerator IS Little THEN Speed IS Slow"); IS.NewRule("Rule 13", "IF Speed IS Regular AND Accelerator IS Much THEN Speed IS Fast"); IS.NewRule("Rule 14", "IF Speed IS Regular AND Accelerator IS Medium THEN Speed IS LittleFast"); IS.NewRule("Rule 15", "IF Speed IS Regular AND Accelerator IS Little THEN Speed IS Regular"); IS.NewRule("Rule 17", "IF Speed IS LittleFast AND Accelerator IS Much THEN Speed IS VeryFast"); IS.NewRule("Rule 18", "IF Speed IS LittleFast AND Accelerator IS Medium THEN Speed IS Fast"); IS.NewRule("Rule 19", "IF Speed IS LittleFast AND Accelerator IS Little THEN Speed IS LittleFast"); IS.NewRule("Rule 21", "IF Speed IS Fast AND Accelerator IS Much THEN Speed IS VeryFast"); IS.NewRule("Rule 22", "IF Speed IS Fast AND Accelerator IS Medium THEN Speed IS VeryFast"); IS.NewRule("Rule 23", "IF Speed IS Fast AND Accelerator IS Little THEN Speed IS Fast"); IS.NewRule("Rule 25", "IF Speed IS VeryFast AND Brake IS Much THEN Speed IS LittleFast"); IS.NewRule("Rule 26", "IF Speed IS VeryFast AND Brake IS Medium THEN Speed IS Fast"); IS.NewRule("Rule 27", "IF Speed IS VeryFast AND Brake IS Little THEN Speed IS Fast"); IS.NewRule("Rule 29", "IF Speed IS Fast AND Brake IS Much THEN Speed IS Slow"); IS.NewRule("Rule 30", "IF Speed IS Fast AND Brake IS Medium THEN Speed IS Regular"); IS.NewRule("Rule 31", "IF Speed IS Fast AND Brake IS Little THEN Speed IS LittleFast"); IS.NewRule("Rule 33", "IF Speed IS LittleFast AND Brake IS Much THEN Speed IS VerySlow"); IS.NewRule("Rule 34", "IF Speed IS LittleFast AND Brake IS Medium THEN Speed IS Slow"); IS.NewRule("Rule 35", "IF Speed IS LittleFast AND Brake IS Little THEN Speed IS Regular"); IS.NewRule("Rule 37", "IF Speed IS Regular AND Brake IS Much THEN Speed IS Stopped"); IS.NewRule("Rule 38", "IF Speed IS Regular AND Brake IS Medium THEN Speed IS VerySlow"); IS.NewRule("Rule 39", "IF Speed IS Regular AND Brake IS Little THEN Speed IS Slow"); IS.NewRule("Rule 41", "IF Speed IS Slow AND Brake IS Much THEN Speed IS Stopped"); IS.NewRule("Rule 42", "IF Speed IS Slow AND Brake IS Medium THEN Speed IS VerySlow"); IS.NewRule("Rule 43", "IF Speed IS Slow AND Brake IS Little THEN Speed IS VerySlow"); IS.NewRule("Rule 45", "IF Speed IS VerySlow AND Brake IS Much THEN Speed IS Stopped"); IS.NewRule("Rule 46", "IF Speed IS VerySlow AND Brake IS Medium THEN Speed IS Stopped"); IS.NewRule("Rule 47", "IF Speed IS VerySlow AND Brake IS Little THEN Speed IS Stopped"); }
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"); }
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"); }
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"); }