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> /// Initializes a new Fuzzy <see cref="InferenceSystem"/>. /// </summary> /// /// <param name="database">A fuzzy <see cref="Database"/> containing the system linguistic /// variables.</param> /// <param name="defuzzifier">A defuzzyfier method used to evaluate the numeric otput /// of the system.</param> /// <param name="normOperator">A <see cref="INorm"/> operator used to evaluate the norms /// in the <see cref="InferenceSystem"/>. For more information of the norm evaluation see <see cref="Rule"/>.</param> /// <param name="conormOperator">A <see cref="ICoNorm"/> operator used to evaluate the /// conorms in the <see cref="InferenceSystem"/>. For more information of the conorm evaluation see <see cref="Rule"/>.</param> /// public InferenceSystem( Database database, IDefuzzifier defuzzifier, INorm normOperator, ICoNorm conormOperator ) { this.database = database; this.defuzzifier = defuzzifier; this.normOperator = normOperator; this.conormOperator = conormOperator; this.rulebase = new Rulebase( ); }
/// <summary> /// Initializes a new Fuzzy <see cref="InferenceSystem"/>. /// </summary> /// /// <param name="database">A fuzzy <see cref="Database"/> containing the system linguistic variables.</param> /// <param name="defuzzifier">A defuzzyfier method used to evaluate the numeric uotput of the system.</param> /// public InferenceSystem( Database database, IDefuzzifier defuzzifier ) : this( database, defuzzifier, new MinimumNorm( ), new MaximumCoNorm( ) ) { }
/// <summary> /// Initializes a new instance of the <see cref="Rule"/> class using as /// CoNorm the <see cref="MaximumCoNorm"/> and as Norm the <see cref="MinimumNorm"/>. /// </summary> /// /// <param name="fuzzyDatabase">A fuzzy <see cref="Database"/> containig the linguistic variables /// (see <see cref="LinguisticVariable"/>) that will be used in the <see cref="Rule"/>.</param> /// /// <param name="name">Name of this <see cref="Rule"/>.</param> /// /// <param name="rule">A string representing the <see cref="Rule"/>. It must be a "IF..THEN" /// statement. For a more detailed description see <see cref="Rule"/> class.</param> /// public Rule( Database fuzzyDatabase, string name, string rule ) : this( fuzzyDatabase, name, rule, new MinimumNorm( ), new MaximumCoNorm( ) ) { }
/// <summary> /// Initializes a new instance of the <see cref="Rule"/> class. /// </summary> /// /// <param name="fuzzyDatabase">A fuzzy <see cref="Database"/> containig the linguistic variables /// (see <see cref="LinguisticVariable"/>) that will be used in the Rule.</param> /// /// <param name="name">Name of this <see cref="Rule"/>.</param> /// /// <param name="rule">A string representing the <see cref="Rule"/>. It must be a "IF..THEN" statement. /// For a more detailed description see <see cref="Rule"/> class.</param> /// /// <param name="normOperator">A class that implements a <see cref="INorm"/> interface to /// evaluate the AND operations of the Rule. </param> /// /// <param name="coNormOperator">A class that implements a <see cref="ICoNorm"/> interface /// to evaluate the OR operations of the Rule. </param> /// public Rule( Database fuzzyDatabase, string name, string rule, INorm normOperator, ICoNorm coNormOperator ) { // the list with the RPN expression rpnTokenList = new List<object>( ); // setting attributes this.name = name; this.rule = rule; this.database = fuzzyDatabase; this.normOperator = normOperator; this.conormOperator = coNormOperator; this.notOperator = new NotOperator( ); // parsing the rule to obtain RPN of the expression ParseRule( ); }
// 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 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); }
private void setupDatabase() { db = new Database(); db.AddVariable(lvWheelAngle); db.AddVariable(lvSteeringWheel); db.AddVariable(lvFriction); db.AddVariable(lvSpeed); db.AddVariable(lvAccelerator); db.AddVariable(lvBrake); }
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"); }