/// <summary> /// Executes the fuzzy inference, obtaining the <see cref="FuzzyOutput"/> of the system for the required /// <see cref="LinguisticVariable"/>. /// </summary> /// /// <param name="variableName">Name of the <see cref="LinguisticVariable"/> to evaluate.</param> /// /// <returns>A <see cref="FuzzyOutput"/> containing the fuzzy output of the system for the /// <see cref="LinguisticVariable"/> specified in <paramref name="variableName"/>.</returns> /// /// <exception cref="KeyNotFoundException">The variable indicated was not found in the database.</exception> /// public FuzzyOutput ExecuteInference(string variableName) { // gets the variable LinguisticVariable lingVar = database.GetVariable(variableName); // object to store the fuzzy output FuzzyOutput fuzzyOutput = new FuzzyOutput(lingVar); // select only rules with the variable as output Rule[] rules = rulebase.GetRules( ); foreach (Rule r in rules) { if (r.Output.Variable.Name == variableName) { string labelName = r.Output.Label.Name; float firingStrength = r.EvaluateFiringStrength( ); if (firingStrength > 0) { fuzzyOutput.AddOutput(labelName, firingStrength); } } } // returns the fuzzy output obtained return(fuzzyOutput); }
/// <summary> /// Initializes a new instance of the <see cref="FuzzyOutput"/> class. /// </summary> /// /// <param name="outputVar">A <see cref="LinguisticVariable"/> representing a Fuzzy Inference System's output.</param> /// internal FuzzyOutput(LinguisticVariable outputVar) { // instance of the constraints list this.outputList = new List <OutputConstraint>(20); // output linguistic variable this.outputVar = outputVar; }
/// <summary> /// Adds a linguistic variable to the database. /// </summary> /// /// <param name="variable">A linguistic variable to add.</param> /// /// <exception cref="NullReferenceException">The linguistic variable was not initialized.</exception> /// <exception cref="ArgumentException">The linguistic variable name already exists in the database.</exception> /// public void AddVariable( LinguisticVariable variable ) { // checking for existing name if ( this.variables.ContainsKey( variable.Name ) ) throw new ArgumentException( "The linguistic variable name already exists in the database." ); // adding label this.variables.Add( variable.Name, variable ); }
/// <summary> /// Initializes a new instance of the <see cref="Clause"/> class. /// </summary> /// /// <param name="variable">Linguistic variable of the clause. </param> /// /// <param name="label">Label of the linguistic variable, a fuzzy set used as label into the linguistic variable.</param> /// /// <exception cref="KeyNotFoundException">The label indicated was not found in the linguistic variable.</exception> /// public Clause(LinguisticVariable variable, FuzzySet label) { // check if label belongs to var. variable.GetLabel(label.Name); // initializing attributes this.label = label; this.variable = variable; }
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> /// Adds a linguistic variable to the database. /// </summary> /// /// <param name="variable">A linguistic variable to add.</param> /// /// <exception cref="NullReferenceException">The linguistic variable was not initialized.</exception> /// <exception cref="ArgumentException">The linguistic variable name already exists in the database.</exception> /// public void AddVariable(LinguisticVariable variable) { // checking for existing name if (this.variables.ContainsKey(variable.Name)) { throw new ArgumentException("The linguistic variable name already exists in the database."); } // adding label this.variables.Add(variable.Name, variable); }
// Testing basic funcionality of linguistic variables private void runLingVarTestButton_Click( object sender, EventArgs e ) { ClearDataSeries( ); // create a linguistic variable to represent temperature LinguisticVariable lvTemperature = new LinguisticVariable( "Temperature", 0, 80 ); // create the linguistic labels (fuzzy sets) that compose the temperature TrapezoidalFunction function1 = new TrapezoidalFunction( 10, 15, TrapezoidalFunction.EdgeType.Right ); FuzzySet fsCold = new FuzzySet( "Cold", function1 ); TrapezoidalFunction function2 = new TrapezoidalFunction( 10, 15, 20, 25 ); FuzzySet fsCool = new FuzzySet( "Cool", function2 ); TrapezoidalFunction function3 = new TrapezoidalFunction( 20, 25, 30, 35 ); FuzzySet fsWarm = new FuzzySet( "Warm", function3 ); TrapezoidalFunction function4 = new TrapezoidalFunction( 30, 35, TrapezoidalFunction.EdgeType.Left ); FuzzySet fsHot = new FuzzySet( "Hot", function4 ); // adding labels to the variable lvTemperature.AddLabel( fsCold ); lvTemperature.AddLabel( fsCool ); lvTemperature.AddLabel( fsWarm ); lvTemperature.AddLabel( fsHot ); // get membership of some points to the cool fuzzy set double[][,] chartValues = new double[4][,]; for ( int i = 0; i < 4; i++ ) chartValues[i] = new double[160, 2]; // showing the shape of the linguistic variable - the shape of its labels memberships from start to end int j = 0; for ( float x = 0; x < 80; x += 0.5f, j++ ) { double y1 = lvTemperature.GetLabelMembership( "Cold", x ); double y2 = lvTemperature.GetLabelMembership( "Cool", x ); double y3 = lvTemperature.GetLabelMembership( "Warm", x ); double y4 = lvTemperature.GetLabelMembership( "Hot", x ); chartValues[0][j, 0] = x; chartValues[0][j, 1] = y1; chartValues[1][j, 0] = x; chartValues[1][j, 1] = y2; chartValues[2][j, 0] = x; chartValues[2][j, 1] = y3; chartValues[3][j, 0] = x; chartValues[3][j, 1] = y4; } // plot membership to a chart chart.UpdateDataSeries( "COLD", chartValues[0] ); chart.UpdateDataSeries( "COOL", chartValues[1] ); chart.UpdateDataSeries( "WARM", chartValues[2] ); chart.UpdateDataSeries( "HOT", chartValues[3] ); }
public void SetInput2() { this.Power = new LinguisticVariable("Moc_samochodu", 20, 180); TrapezoidalFunction function1 = new TrapezoidalFunction(40, 100, TrapezoidalFunction.EdgeType.Right); FuzzySet set1 = new FuzzySet("mała", function1); TrapezoidalFunction function2 = new TrapezoidalFunction(70, 100, 130); FuzzySet set2 = new FuzzySet("średnia", function2); TrapezoidalFunction function3 = new TrapezoidalFunction(100, 160, TrapezoidalFunction.EdgeType.Left); FuzzySet set3 = new FuzzySet("duża", function3); Power.AddLabel(set1); Power.AddLabel(set2); Power.AddLabel(set3); database.AddVariable(Power); double y1; double y2; double y3; for (float x = 20; x < 180; x += 0.5f) { if (Power.GetLabelMembership("mała", x + 0.5f) + Power.GetLabelMembership("mała", x) > 0) { y1 = Power.GetLabelMembership("mała", x); chart5.Series["Mała"].Points.AddXY(x, y1); } if (Power.GetLabelMembership("średnia", x + 0.5f) + Power.GetLabelMembership("średnia", x) > 0) { y2 = Power.GetLabelMembership("średnia", x); chart5.Series["Średnia"].Points.AddXY(x, y2); } if (Power.GetLabelMembership("duża", x + 0.5f) + Power.GetLabelMembership("duża", x) > 0) { y3 = Power.GetLabelMembership("duża", x); chart5.Series["Duża"].Points.AddXY(x, y3); } } }
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); } } }
//----------------------------------------------------------------------------------------------------------------- // PROTECTED METHODS //----------------------------------------------------------------------------------------------------------------- protected LinguisticVariable DefineBooleanVariable(string name, float start = -1f, float end = 1f) { LinguisticVariable varAttacking = new LinguisticVariable(name, start, end); varAttacking.AddLabel(new FuzzySet(AIBoolean.FALSE.ToString(), new SingletonFunction((float)((int)AIBoolean.FALSE)))); varAttacking.AddLabel(new FuzzySet(AIBoolean.TRUE.ToString(), new SingletonFunction((float)((int)AIBoolean.TRUE)))); return varAttacking; }
protected LinguisticVariable DefineBlockingVariable(string name, float start = -1f, float end = 2f) { LinguisticVariable varBlocking = new LinguisticVariable(name, start, end); varBlocking.AddLabel(new FuzzySet(AIBlocking.Air.ToString(), new SingletonFunction((float)((int)AIBlocking.Air)))); varBlocking.AddLabel(new FuzzySet(AIBlocking.High.ToString(), new SingletonFunction((float)((int)AIBlocking.High)))); varBlocking.AddLabel(new FuzzySet(AIBlocking.Low.ToString(), new SingletonFunction((float)((int)AIBlocking.Low)))); return varBlocking; }
/// <summary> /// Converts the Fuzzy Rule to RPN (Reverse Polish Notation). For debug proposes, the string representation of the /// RPN expression can be acessed by calling <see cref="GetRPNExpression"/> method. /// </summary> /// private void ParseRule( ) { // flag to incicate we are on consequent state bool consequent = false; // tokens like IF and THEN will be searched always in upper case string upRule = rule.ToUpper( ); // the rule must start with IF, and must have a THEN somewhere if (!upRule.StartsWith("IF")) { throw new ArgumentException("A Fuzzy Rule must start with an IF statement."); } if (upRule.IndexOf("THEN") < 0) { throw new ArgumentException("Missing the consequent (THEN) statement."); } // building a list with all the expression (rule) string tokens string spacedRule = rule.Replace("(", " ( ").Replace(")", " ) "); // getting the tokens list string[] tokensList = GetRuleTokens(spacedRule); // stack to convert to RPN Stack <string> s = new Stack <string>( ); // storing the last token string lastToken = "IF"; // linguistic var read, used to build clause LinguisticVariable lingVar = null; // verifying each token for (int i = 0; i < tokensList.Length; i++) { // removing spaces string token = tokensList[i].Trim( ); // getting upper case string upToken = token.ToUpper( ); // ignoring these tokens if (upToken == "" || upToken == "IF") { continue; } // if the THEN is found, the rule is now on consequent if (upToken == "THEN") { lastToken = upToken; consequent = true; continue; } // if we got a linguistic variable, an IS statement and a label is needed if (lastToken == "VAR") { if (upToken == "IS") { lastToken = upToken; } else { throw new ArgumentException("An IS statement is expected after a linguistic variable."); } } // if we got an IS statement, a label must follow it else if (lastToken == "IS") { try { FuzzySet fs = lingVar.GetLabel(token); Clause c = new Clause(lingVar, fs); if (consequent) { output = c; } else { rpnTokenList.Add(c); } lastToken = "LAB"; } catch (KeyNotFoundException) { throw new ArgumentException("Linguistic label " + token + " was not found on the variable " + lingVar.Name + "."); } } // not VAR and not IS statement else { // openning new scope if (upToken == "(") { // if we are on consequent, only variables can be found if (consequent) { throw new ArgumentException("Linguistic variable expected after a THEN statement."); } // if its a (, just push it s.Push(upToken); lastToken = upToken; } // operators else if (upToken == "AND" || upToken == "OR" || unaryOperators.IndexOf(upToken) >= 0) { // if we are on consequent, only variables can be found if (consequent) { throw new ArgumentException("Linguistic variable expected after a THEN statement."); } // pop all the higher priority operators until the stack is empty while ((s.Count > 0) && (Priority(s.Peek( )) > Priority(upToken))) { rpnTokenList.Add(s.Pop( )); } // pushing the operator s.Push(upToken); lastToken = upToken; } // closing the scope else if (upToken == ")") { // if we are on consequent, only variables can be found if (consequent) { throw new ArgumentException("Linguistic variable expected after a THEN statement."); } // if there is nothing on the stack, an oppening parenthesis is missing. if (s.Count == 0) { throw new ArgumentException("Openning parenthesis missing."); } // pop the tokens and copy to output until openning is found while (s.Peek( ) != "(") { rpnTokenList.Add(s.Pop( )); if (s.Count == 0) { throw new ArgumentException("Openning parenthesis missing."); } } s.Pop( ); // saving last token... lastToken = upToken; } // finally, the token is a variable else { // find the variable try { lingVar = database.GetVariable(token); lastToken = "VAR"; } catch (KeyNotFoundException) { throw new ArgumentException("Linguistic variable " + token + " was not found on the database."); } } } } // popping all operators left in stack while (s.Count > 0) { rpnTokenList.Add(s.Pop( )); } }
protected LinguisticVariable DefineHorizontalMovementVariable(string name, float start = -1f, float end = 2f) { LinguisticVariable varHorizontalMovement = new LinguisticVariable(name, start, end); varHorizontalMovement.AddLabel(new FuzzySet(AIHorizontalMovement.MovingBack.ToString(), new SingletonFunction((float)((int)AIHorizontalMovement.MovingBack)))); varHorizontalMovement.AddLabel(new FuzzySet(AIHorizontalMovement.MovingForward.ToString(), new SingletonFunction((float)((int)AIHorizontalMovement.MovingForward)))); varHorizontalMovement.AddLabel(new FuzzySet(AIHorizontalMovement.Still.ToString(), new SingletonFunction((float)((int)AIHorizontalMovement.Still)))); return varHorizontalMovement; }
protected LinguisticVariable DefineHitConfirmTypeVariable(string name, float start = -1f, float end = 1f) { LinguisticVariable varHitConfirmType = new LinguisticVariable(name, start, end); varHitConfirmType.AddLabel(new FuzzySet(HitConfirmType.Hit.ToString(), new SingletonFunction((float)((int)HitConfirmType.Hit)))); varHitConfirmType.AddLabel(new FuzzySet(HitConfirmType.Throw.ToString(), new SingletonFunction((float)((int)HitConfirmType.Throw)))); return varHitConfirmType; }
protected LinguisticVariable DefineGaugeVariable(string name, float start = -1f, float end = 4f) { LinguisticVariable varGaugeUsage = new LinguisticVariable(name, start, end); varGaugeUsage.AddLabel(new FuzzySet(GaugeUsage.None.ToString(), new TrapezoidalFunction(start, 0.00f, 0.24f, 0.26f))); varGaugeUsage.AddLabel(new FuzzySet(GaugeUsage.Quarter.ToString(), new TrapezoidalFunction(0.25f, 0.25f, 0.49f, 0.51f))); varGaugeUsage.AddLabel(new FuzzySet(GaugeUsage.Half.ToString(), new TrapezoidalFunction(0.50f, 0.50f, 0.74f, 0.76f))); varGaugeUsage.AddLabel(new FuzzySet(GaugeUsage.ThreeQuarters.ToString(),new TrapezoidalFunction(0.75f, 0.75f, 0.99f, 1.01f))); varGaugeUsage.AddLabel(new FuzzySet(GaugeUsage.All.ToString(), new TrapezoidalFunction(1.00f, 1.00f, end))); return varGaugeUsage; }
protected LinguisticVariable DefineFrameDataVariable(string name, float start = -1f, float end = 3f) { LinguisticVariable varFrameData = new LinguisticVariable(name, start, end); varFrameData.AddLabel(new FuzzySet(CurrentFrameData.ActiveFrames.ToString(), new SingletonFunction((float)((int)CurrentFrameData.ActiveFrames)))); varFrameData.AddLabel(new FuzzySet(CurrentFrameData.RecoveryFrames.ToString(), new SingletonFunction((float)((int)CurrentFrameData.RecoveryFrames)))); varFrameData.AddLabel(new FuzzySet(CurrentFrameData.StartupFrames.ToString(), new SingletonFunction((float)((int)CurrentFrameData.StartupFrames)))); return varFrameData; }
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"); }
// 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" ); }
/// <summary> /// Initializes a new instance of the <see cref="FuzzyOutput"/> class. /// </summary> /// /// <param name="outputVar">A <see cref="LinguisticVariable"/> representing a Fuzzy Inference System's output.</param> /// internal FuzzyOutput( LinguisticVariable outputVar ) { // instance of the constraints list this.outputList = new List<OutputConstraint>( 20 ); // output linguistic variable this.outputVar = outputVar; }
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; }
protected LinguisticVariable DefineDamageVariable(string name, float start = 0f, float end = 1f) { LinguisticVariable varDamage = new LinguisticVariable(name, start - 1f, end + 1f); varDamage.AddLabel(new FuzzySet( AIDamage.VeryWeak.ToString(), new TrapezoidalFunction(start - 1f, start, this.aiDefinitions.damage.veryWeak, this.aiDefinitions.damage.weak) )); varDamage.AddLabel(new FuzzySet( AIDamage.Weak.ToString(), new TrapezoidalFunction(this.aiDefinitions.damage.veryWeak, this.aiDefinitions.damage.weak, this.aiDefinitions.damage.medium) )); varDamage.AddLabel(new FuzzySet( AIDamage.Medium.ToString(), new TrapezoidalFunction(this.aiDefinitions.damage.weak, this.aiDefinitions.damage.medium, this.aiDefinitions.damage.strong) )); varDamage.AddLabel(new FuzzySet( AIDamage.Strong.ToString(), new TrapezoidalFunction(this.aiDefinitions.damage.medium, this.aiDefinitions.damage.strong, this.aiDefinitions.damage.veryStrong) )); varDamage.AddLabel(new FuzzySet( AIDamage.VeryStrong.ToString(), new TrapezoidalFunction(this.aiDefinitions.damage.strong, this.aiDefinitions.damage.veryStrong, end, end + 1f) )); return varDamage; }
protected LinguisticVariable DefineDistanceVariable(string name, float start = 0f, float end = 1f) { LinguisticVariable varDistance = new LinguisticVariable(name, start - 1f, end + 1f); varDistance.AddLabel(new FuzzySet( CharacterDistance.VeryClose.ToString(), new TrapezoidalFunction(start - 1f, start, this.aiDefinitions.distance.veryClose, this.aiDefinitions.distance.close) )); varDistance.AddLabel(new FuzzySet( CharacterDistance.Close.ToString(), new TrapezoidalFunction(this.aiDefinitions.distance.veryClose, this.aiDefinitions.distance.close, this.aiDefinitions.distance.mid) )); varDistance.AddLabel(new FuzzySet( CharacterDistance.Mid.ToString(), new TrapezoidalFunction(this.aiDefinitions.distance.close, this.aiDefinitions.distance.mid, this.aiDefinitions.distance.far) )); varDistance.AddLabel(new FuzzySet( CharacterDistance.Far.ToString(), new TrapezoidalFunction(this.aiDefinitions.distance.mid, this.aiDefinitions.distance.far, this.aiDefinitions.distance.veryFar) )); varDistance.AddLabel(new FuzzySet( CharacterDistance.VeryFar.ToString(), new TrapezoidalFunction(this.aiDefinitions.distance.far, this.aiDefinitions.distance.veryFar, end, end + 1f) )); return varDistance; }
/// <summary> /// Initializes a new instance of the <see cref="Clause"/> class. /// </summary> /// /// <param name="variable">Linguistic variable of the clause. </param> /// /// <param name="label">Label of the linguistic variable, a fuzzy set used as label into the linguistic variable.</param> /// /// <exception cref="KeyNotFoundException">The label indicated was not found in the linguistic variable.</exception> /// public Clause( LinguisticVariable variable, FuzzySet label ) { // check if label belongs to var. variable.GetLabel( label.Name ); // initializing attributes this.label = label; this.variable = variable; }
protected LinguisticVariable DefineFrameSpeedVariable(string name, float start = -1f, float end = 4f) { LinguisticVariable varFrameSpeed = new LinguisticVariable(name, start, end); varFrameSpeed.AddLabel(new FuzzySet(FrameSpeed.VerySlow.ToString(), new SingletonFunction(0f))); varFrameSpeed.AddLabel(new FuzzySet(FrameSpeed.Slow.ToString(), new SingletonFunction(1f))); varFrameSpeed.AddLabel(new FuzzySet(FrameSpeed.Normal.ToString(), new SingletonFunction(2f))); varFrameSpeed.AddLabel(new FuzzySet(FrameSpeed.Fast.ToString(), new SingletonFunction(3f))); varFrameSpeed.AddLabel(new FuzzySet(FrameSpeed.VeryFast.ToString(), new SingletonFunction(4f))); return varFrameSpeed; }
protected LinguisticVariable DefineMovementSpeedVariable(string name, float start = 0f, float end = 1000f) { LinguisticVariable varMovementSpeed = new LinguisticVariable(name, start - 1f, end + 1f); varMovementSpeed.AddLabel(new FuzzySet( AIMovementSpeed.VerySlow.ToString(), new TrapezoidalFunction(start - 1f, start, this.aiDefinitions.speed.verySlow, this.aiDefinitions.speed.slow) )); varMovementSpeed.AddLabel(new FuzzySet( AIMovementSpeed.Slow.ToString(), new TrapezoidalFunction(this.aiDefinitions.speed.verySlow, this.aiDefinitions.speed.slow, this.aiDefinitions.speed.normal) )); varMovementSpeed.AddLabel(new FuzzySet( AIMovementSpeed.Normal.ToString(), new TrapezoidalFunction(this.aiDefinitions.speed.slow, this.aiDefinitions.speed.normal, this.aiDefinitions.speed.fast) )); varMovementSpeed.AddLabel(new FuzzySet( AIMovementSpeed.Fast.ToString(), new TrapezoidalFunction(this.aiDefinitions.speed.normal, this.aiDefinitions.speed.fast, this.aiDefinitions.speed.veryFast) )); varMovementSpeed.AddLabel(new FuzzySet( AIMovementSpeed.VeryFast.ToString(), new TrapezoidalFunction(this.aiDefinitions.speed.fast, this.aiDefinitions.speed.veryFast, end, end + 1f) )); return varMovementSpeed; }
protected LinguisticVariable DefineHealthVariable(string name, float start = 0f, float end = 1f) { LinguisticVariable varHealth = new LinguisticVariable(name, start - 1f, end + 1f); if (this.aiDefinitions.health.healthy <= start){ varHealth.AddLabel(new FuzzySet(HealthStatus.Dead.ToString(), new SingletonFunction(start))); }else{ varHealth.AddLabel(new FuzzySet( HealthStatus.Dead.ToString(), new TrapezoidalFunction(start - 1f, start, this.aiDefinitions.health.dead, this.aiDefinitions.health.almostDead) )); } varHealth.AddLabel(new FuzzySet( HealthStatus.AlmostDead.ToString(), new TrapezoidalFunction(this.aiDefinitions.health.dead, this.aiDefinitions.health.almostDead, this.aiDefinitions.health.criticallyWounded) )); varHealth.AddLabel(new FuzzySet( HealthStatus.CriticallyWounded.ToString(), new TrapezoidalFunction(this.aiDefinitions.health.almostDead, this.aiDefinitions.health.criticallyWounded, this.aiDefinitions.health.seriouslyWounded) )); varHealth.AddLabel(new FuzzySet( HealthStatus.SeriouslyWounded.ToString(), new TrapezoidalFunction(this.aiDefinitions.health.criticallyWounded, this.aiDefinitions.health.seriouslyWounded, this.aiDefinitions.health.moderatelyWounded) )); varHealth.AddLabel(new FuzzySet( HealthStatus.ModeratelyWounded.ToString(), new TrapezoidalFunction(this.aiDefinitions.health.seriouslyWounded, this.aiDefinitions.health.moderatelyWounded, this.aiDefinitions.health.lightlyWounded) )); varHealth.AddLabel(new FuzzySet( HealthStatus.LightlyWounded.ToString(), new TrapezoidalFunction(this.aiDefinitions.health.moderatelyWounded, this.aiDefinitions.health.lightlyWounded, this.aiDefinitions.health.scratched) )); varHealth.AddLabel(new FuzzySet( HealthStatus.Scratched.ToString(), new TrapezoidalFunction(this.aiDefinitions.health.lightlyWounded, this.aiDefinitions.health.scratched, this.aiDefinitions.health.healthy) )); if (this.aiDefinitions.health.healthy >= end){ varHealth.AddLabel(new FuzzySet(HealthStatus.Healthy.ToString(), new SingletonFunction(end))); }else{ varHealth.AddLabel(new FuzzySet( HealthStatus.Healthy.ToString(), new TrapezoidalFunction(this.aiDefinitions.health.scratched, this.aiDefinitions.health.healthy, end, end + 1f) )); } return varHealth; }
protected LinguisticVariable DefineVerticalMovementVariable(string name, float start = 0f, float end = 2f) { LinguisticVariable varVerticalMovement = new LinguisticVariable(name, start, end); varVerticalMovement.AddLabel(new FuzzySet(AIVerticalMovement.Crouching.ToString(), new SingletonFunction((float)((int)AIVerticalMovement.Crouching)))); varVerticalMovement.AddLabel(new FuzzySet(AIVerticalMovement.Jumping.ToString(), new SingletonFunction((float)((int)AIVerticalMovement.Jumping)))); varVerticalMovement.AddLabel(new FuzzySet(AIVerticalMovement.Standing.ToString(), new SingletonFunction((float)((int)AIVerticalMovement.Standing)))); return varVerticalMovement; }
protected LinguisticVariable DefineHitTypeVariable(string name, float start = -1f, float end = 7f) { LinguisticVariable varHitType = new LinguisticVariable(name, start, end); varHitType.AddLabel(new FuzzySet(HitType.HighKnockdown.ToString(), new SingletonFunction((float)((int)HitType.HighKnockdown)))); varHitType.AddLabel(new FuzzySet(HitType.Mid.ToString(), new SingletonFunction((float)((int)HitType.Mid)))); varHitType.AddLabel(new FuzzySet(HitType.KnockBack.ToString(), new SingletonFunction((float)((int)HitType.KnockBack)))); varHitType.AddLabel(new FuzzySet(HitType.Launcher.ToString(), new SingletonFunction((float)((int)HitType.Launcher)))); varHitType.AddLabel(new FuzzySet(HitType.Low.ToString(), new SingletonFunction((float)((int)HitType.Low)))); varHitType.AddLabel(new FuzzySet(HitType.MidKnockdown.ToString(), new SingletonFunction((float)((int)HitType.MidKnockdown)))); varHitType.AddLabel(new FuzzySet(HitType.Overhead.ToString(), new SingletonFunction((float)((int)HitType.Overhead)))); varHitType.AddLabel(new FuzzySet(HitType.Sweep.ToString(), new SingletonFunction((float)((int)HitType.Sweep)))); return varHitType; }
public void SetOutput() { this.Risk = new LinguisticVariable("Ryzyko", 0, 30); TrapezoidalFunction function1 = new TrapezoidalFunction(5, 10, TrapezoidalFunction.EdgeType.Right); FuzzySet set1 = new FuzzySet("niskie", function1); TrapezoidalFunction function2 = new TrapezoidalFunction(5, 10, 15); FuzzySet set2 = new FuzzySet("średnio_niskie", function2); TrapezoidalFunction function3 = new TrapezoidalFunction(10, 15, 20); FuzzySet set3 = new FuzzySet("średnie", function3); TrapezoidalFunction function4 = new TrapezoidalFunction(15, 20, 25); FuzzySet set4 = new FuzzySet("średnio_wysokie", function4); TrapezoidalFunction function5 = new TrapezoidalFunction(20, 25, TrapezoidalFunction.EdgeType.Left); FuzzySet set5 = new FuzzySet("wysokie", function5); Risk.AddLabel(set1); Risk.AddLabel(set2); Risk.AddLabel(set3); Risk.AddLabel(set4); Risk.AddLabel(set5); database.AddVariable(Risk); double y1; double y2; double y3; double y4; double y5; for (float x = 0; x < 30; x += 0.05f) { if (Risk.GetLabelMembership("niskie", x + 0.05f) + Risk.GetLabelMembership("niskie", x) > 0) { y1 = Risk.GetLabelMembership("niskie", x); chart6.Series["Niskie"].Points.AddXY(x, y1); } if (Risk.GetLabelMembership("średnio_niskie", x + 0.05f) + Risk.GetLabelMembership("średnio_niskie", x) > 0) { y2 = Risk.GetLabelMembership("średnio_niskie", x); chart6.Series["Śr_nisk"].Points.AddXY(x, y2); } if (Risk.GetLabelMembership("średnie", x + 0.05f) + Risk.GetLabelMembership("średnie", x) > 0) { y3 = Risk.GetLabelMembership("średnie", x); chart6.Series["Średnie"].Points.AddXY(x, y3); } if (Risk.GetLabelMembership("średnio_wysokie", x + 0.05f) + Risk.GetLabelMembership("średnio_wysokie", x) > 0) { y4 = Risk.GetLabelMembership("średnio_wysokie", x); chart6.Series["Śr_wys"].Points.AddXY(x, y4); } if (Risk.GetLabelMembership("wysokie", x + 0.05f) + Risk.GetLabelMembership("wysokie", x) > 0) { y5 = Risk.GetLabelMembership("wysokie", x); chart6.Series["Wysokie"].Points.AddXY(x, y5); } } }
protected LinguisticVariable DefineJumpArcVariable(string name, float start = 0f, float end = 1f) { LinguisticVariable varJumpArc = new LinguisticVariable(name, start - 1f, end + 1f); varJumpArc.AddLabel(new FuzzySet( JumpArc.TakeOff.ToString(), new TrapezoidalFunction(start - 1f, start, 0.3f, 0.4f) )); varJumpArc.AddLabel(new FuzzySet( JumpArc.Jumping.ToString(), new TrapezoidalFunction(0.3f, 0.4f, 0.55f, 0.65f) )); varJumpArc.AddLabel(new FuzzySet( JumpArc.Top.ToString(), new TrapezoidalFunction(0.55f, 0.65f, 0.75f) )); varJumpArc.AddLabel(new FuzzySet( JumpArc.Falling.ToString(), new TrapezoidalFunction(0.65f, 0.75f, 0.85f, 0.95f) )); varJumpArc.AddLabel(new FuzzySet( JumpArc.Landing.ToString(), new TrapezoidalFunction(0.85f, 0.95f, end, end + 1f) )); return varJumpArc; }
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); }
protected LinguisticVariable DefineOutputVariable(string name) { float start = 0f; float end = 1f; LinguisticVariable varOutput = new LinguisticVariable(name, start - 1f, end + 1f); varOutput.AddLabel(new FuzzySet( AIDesirability.TheWorstOption.ToString(), new TrapezoidalFunction(start - 1f, start, this.aiDefinitions.desirability.theWorstOption, this.aiDefinitions.desirability.veryUndesirable) )); varOutput.AddLabel(new FuzzySet( AIDesirability.VeryUndesirable.ToString(), new TrapezoidalFunction(this.aiDefinitions.desirability.theWorstOption, this.aiDefinitions.desirability.veryUndesirable, this.aiDefinitions.desirability.undesirable) )); varOutput.AddLabel(new FuzzySet( AIDesirability.Undesirable.ToString(), new TrapezoidalFunction(this.aiDefinitions.desirability.veryUndesirable, this.aiDefinitions.desirability.undesirable, this.aiDefinitions.desirability.notBad) )); varOutput.AddLabel(new FuzzySet( AIDesirability.NotBad.ToString(), new TrapezoidalFunction(this.aiDefinitions.desirability.undesirable, this.aiDefinitions.desirability.notBad, this.aiDefinitions.desirability.desirable) )); varOutput.AddLabel(new FuzzySet( AIDesirability.Desirable.ToString(), new TrapezoidalFunction(this.aiDefinitions.desirability.notBad, this.aiDefinitions.desirability.desirable, this.aiDefinitions.desirability.veryDesirable) )); varOutput.AddLabel(new FuzzySet( AIDesirability.VeryDesirable.ToString(), new TrapezoidalFunction(this.aiDefinitions.desirability.desirable, this.aiDefinitions.desirability.veryDesirable, this.aiDefinitions.desirability.theBestOption) )); varOutput.AddLabel(new FuzzySet( AIDesirability.TheBestOption.ToString(), new TrapezoidalFunction(this.aiDefinitions.desirability.veryDesirable, this.aiDefinitions.desirability.theBestOption, end, end + 1f) )); return varOutput; }
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 virtual void SetAIInformation(AIInfo ai) { this.ai = ai; if (ai != null){ AI4Unity.Fuzzy.InferenceSystem inferenceSystem = ai.GenerateInferenceSystem(); if (inferenceSystem != null){ this.inferenceEngine = new InferenceSystemThread( inferenceSystem, ai.GetDesirabilityScore(ai.advancedOptions.defaultDesirability) ); this.gaugeVar = this.inferenceEngine.GetInputVariable(AICondition.Attacking_GaugeUsage_Self); this.damageVar = this.inferenceEngine.GetInputVariable(AICondition.Attacking_Damage_Self); }else{ this.inferenceEngine = null; this.gaugeVar = null; this.damageVar = null; } }else{ this.inferenceEngine = null; this.gaugeVar = null; this.damageVar = null; } UFE.OnGameBegin -= this.OnGameBegin; UFE.OnGameBegin += this.OnGameBegin; base.Initialize(this.inputReferences); }
private void setupVariables() { #region WheelAngle lvWheelAngle = new LinguisticVariable("WheelAngle", -58.2f, 58.2f); lvWheelAngle.AddLabel(fsVeryNegative); lvWheelAngle.AddLabel(fsNegative); lvWheelAngle.AddLabel(fsLittleNegative); lvWheelAngle.AddLabel(fsZero); lvWheelAngle.AddLabel(fsLittlePositive); lvWheelAngle.AddLabel(fsPositive); lvWheelAngle.AddLabel(fsVeryPositive); #endregion #region SteeringWheel lvSteeringWheel = new LinguisticVariable("SteeringWheel", -2.5f, 2.5f); lvSteeringWheel.AddLabel(fsLeftMax); lvSteeringWheel.AddLabel(fsLeftMedium); lvSteeringWheel.AddLabel(fsLeftMin); lvSteeringWheel.AddLabel(fsMiddle); lvSteeringWheel.AddLabel(fsRightMin); lvSteeringWheel.AddLabel(fsRightMedium); lvSteeringWheel.AddLabel(fsRightMax); #endregion #region Friction lvFriction = new LinguisticVariable("Friction", 0, 10); lvFriction.AddLabel(fsNullFriction); lvFriction.AddLabel(fsLowFriction); lvFriction.AddLabel(fsMediumFriction); lvFriction.AddLabel(fsHighFriction); #endregion #region Speed lvSpeed = new LinguisticVariable("Speed", -5, 190); lvSpeed.AddLabel(fsStopped); lvSpeed.AddLabel(fsVerySlow); lvSpeed.AddLabel(fsSlow); lvSpeed.AddLabel(fsRegular); lvSpeed.AddLabel(fsLittleFast); lvSpeed.AddLabel(fsFast); lvSpeed.AddLabel(fsVeryFast); #endregion #region Accelerator lvAccelerator = new LinguisticVariable("Accelerator", 0, 1); lvAccelerator.AddLabel(fsNone); lvAccelerator.AddLabel(fsLittle); lvAccelerator.AddLabel(fsMedium); lvAccelerator.AddLabel(fsMuch); #endregion #region Brake lvBrake = new LinguisticVariable("Brake", 0, 1); lvBrake.AddLabel(fsNone); lvBrake.AddLabel(fsLittle); lvBrake.AddLabel(fsMedium); lvBrake.AddLabel(fsMuch); #endregion }
protected LinguisticVariable DefineAttackTypeVariable(string name, float start = -1f, float end = 6f) { LinguisticVariable varAttackType = new LinguisticVariable(name, start, end); varAttackType.AddLabel(new FuzzySet(AttackType.AntiAir.ToString(), new SingletonFunction((float)((int)AttackType.AntiAir)))); varAttackType.AddLabel(new FuzzySet(AttackType.BackLauncher.ToString(), new SingletonFunction((float)((int)AttackType.BackLauncher)))); varAttackType.AddLabel(new FuzzySet(AttackType.Dive.ToString(), new SingletonFunction((float)((int)AttackType.Dive)))); varAttackType.AddLabel(new FuzzySet(AttackType.ForwardLauncher.ToString(), new SingletonFunction((float)((int)AttackType.ForwardLauncher)))); varAttackType.AddLabel(new FuzzySet(AttackType.Neutral.ToString(), new SingletonFunction((float)((int)AttackType.Neutral)))); varAttackType.AddLabel(new FuzzySet(AttackType.NormalAttack.ToString(), new SingletonFunction((float)((int)AttackType.NormalAttack)))); varAttackType.AddLabel(new FuzzySet(AttackType.Projectile.ToString(), new SingletonFunction((float)((int)AttackType.Projectile)))); return varAttackType; }
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"); }