FuzzySystem.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using FuzzyLogicPCL.FuzzySets;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace FuzzyLogicPCL
  5. {
  6. public class FuzzySystem
  7. {
  8. String Name { get; set; }
  9. List<LinguisticVariable> Inputs;
  10. LinguisticVariable Output;
  11. List<FuzzyRule> Rules;
  12. List<FuzzyValue> Problem;
  13. public FuzzySystem(String _name)
  14. {
  15. Name = _name;
  16. Inputs = new List<LinguisticVariable>();
  17. Rules = new List<FuzzyRule>();
  18. Problem = new List<FuzzyValue>();
  19. }
  20. public void addInputVariable(LinguisticVariable lv)
  21. {
  22. Inputs.Add(lv);
  23. }
  24. public void addOutputVariable(LinguisticVariable lv)
  25. {
  26. Output = lv;
  27. }
  28. public void addFuzzyRule(FuzzyRule fuzzyRule)
  29. {
  30. Rules.Add(fuzzyRule);
  31. }
  32. public void addFuzzyRule(string ruleStr)
  33. {
  34. FuzzyRule rule = new FuzzyRule(ruleStr, this);
  35. Rules.Add(rule);
  36. }
  37. public void SetInputVariable(LinguisticVariable inputVar, double value)
  38. {
  39. Problem.Add(new FuzzyValue(inputVar, value));
  40. }
  41. public double Solve()
  42. {
  43. // Application des règles et calcul du fuzzy set résultant
  44. FuzzySet res = new FuzzySet(Output.MinValue, Output.MaxValue);
  45. res.Add(Output.MinValue, 0);
  46. res.Add(Output.MaxValue, 0);
  47. foreach (FuzzyRule rule in Rules)
  48. {
  49. // Calcul
  50. res = res | rule.Apply(Problem);
  51. }
  52. // Defuzzification
  53. return res.Centroid();
  54. }
  55. internal LinguisticVariable LinguisticVariableByName(string name)
  56. {
  57. foreach (LinguisticVariable input in Inputs)
  58. {
  59. if (input.Name.ToUpper().Equals(name))
  60. {
  61. return input;
  62. }
  63. }
  64. if (Output.Name.ToUpper().Equals(name))
  65. {
  66. return Output;
  67. }
  68. return null;
  69. }
  70. public void ResetCase()
  71. {
  72. Problem.Clear();
  73. }
  74. }
  75. }