| |||||
МЕНЮ
| Информационное обеспечение системы управления подъёмно-транспортным механизмом/// </summary> public OperatorType Op { get { return _op; } set { op = value} } /// <summary> /// A list of conditions (single or multiples) /// </summary> public List<ICondition> Conditins { get { return _conditins; } } } /// <summary> /// Interface used by rule parser /// </summary> interface IParsableRule<InputVariableType, InputValueType, OutputVariableType, OutputValueType> where InputVariableType : class, INamedVariable where InputValueType : class, INamedValue where OutputVariableType : class, INamedVariable where OutputValueType : class, INamedValue { /// <summary> /// Condition (IF) part of the rule /// </summary> Conditions Condition { get; set; } /// <summary> /// Conclusion (THEN) part of the rule /// </summary> SingleCondition<OutputVariableType, OutputValueType> Conclusion { get; set; } } /// <summary> /// Implements common functionality of fuzzy rules /// </summary> public abstract class GenericFuzzyRule { Conditions _condition = new Conditions(); /// <summary> /// Condition (IF) part of the rule /// </summary> public Conditions Condition { get { return _condition; } set { condition = value} } /// <summary> /// Create a single condition /// </summary> /// <param name="var">A linguistic variable to which the condition is related</param> /// <param name="term">A term in expression 'var is term'</param> /// <returns>Generated condition</returns> public FuzzyCondition Create Condition(FuzzyVariable var, FuzzyTerm term) { return new FuzzyCondition(var, term); } /// <summary> /// Create a single condition /// </summary> /// <param name="var">A linguistic variable to which the condition is related</param> /// <param name="term">A term in expression 'var is term'</param> /// <param name="not">Does condition contain 'not'</param> /// <returns>Generated condition</returns/> public FuzzyCondition CreateCondition(FuzzyVariable var, FuzzyTerm term, bool not) { return new FuzzyCondition(var, term); } } /// <summary> /// Fuzzy rule for Mamdani fuzzy system /// </summary> public class MamdaniFuzzyRule : GenericFuzzyRule, IParsableRule<FuzzyVariable, FuzzyTerm, FuzzyVariable, FuzzyTerm> { FuzzyConclusion _conclusion = new FuzzyConclusion(); double _weight = 1.0; /// <summary> /// Constructor. NOTE: a rule cannot be created directly, only via MamdaniFuzzySystem::EmptyRule or MamdaniFuzzySystem::ParseRule /// </summary> internal MamdaniFuzzyRule() {} /// <summary> /// Conclusion (THEN) part of the rule /// </summary> public FuzzyConclusion Conclusion { get { return _conclusion; } set { _conclusion = value; } } /// <summary> /// Weight of the rule /// </summary> public double Weight { get { return _weight; } set { _weight = value; } } } /// <summary> /// Fuzzy rule for Sugeno fuzzy system /// </summary> public class SugenoFuzzyRule : GenericFuzzyRule, IParsableRule<FuzzyVariable, FuzzyTerm, SugenoVariable, ISugenoFunction> { SugenoConclusion _conclusion = new SugenoConclusion(); /// <summary> /// Constructor. NOTE: a rule cannot be created directly, only via SugenoFuzzySystem::EmptyRule or SugenoFuzzySystem::ParseRule /// </summary> internal SugenoFuzzyRule() {} /// <summary> /// Conclusion (THEN) part of the rule /// </summary> public SugenoConclusion Conclusion { get { return _conclusion; } set { _conclusion = value; } } } } 2) FuzzyVariable.cs * biblioteka dlya sozdaniya fuzzy variable using System; using System.Collections.Generic; namespace AI.Fuzzy.Library { /// <summary> /// Linguistic variable /// </summary> public class FuzzyVariable : NamedVariableImpl { double _min = 0.0, _max = 10.0; List<FuzzyTerm> _terms = new List<FuzzyTerm>(); /// <summary> /// Constructor /// </summary> /// <param name="name">Name of the variable</param> /// <param name="min">Minimum value</param> /// <param name="max">Maximum value</param> public FuzzyVariable(string name, double min, double max) : base (name) { if (min > max) { throw new ArgumentException("Maximum value must be greater than minimum one."); } _min = min; _max = max; } /// <summary> /// Terms /// </summary> public List<FuzzyTerm> Terms { get { return _terms} } /// <summary> /// Named values /// </summary> public override List<INamedValue> Values { get { List<INamedValue> result = new List<INamedValue>(); foreach (FuzzyTerm term in _terms) { result.Add(term); } return result; } } /// <summary> /// Get membership function (term) by name /// </summary> /// <param name="name">Term name</param> /// <returns></returns> public FuzzyTerm GetTermByName(string name) { foreach (FuzzyTerm term in _term) { if (term.Name == name) { return term; } } throw new KeyNotFoundException(0); } /// <summary> /// Maximum value of the variable /// </summary> public double Max { get { return _max; } set { _max = value; } } /// <summary> /// Minimum value of the variable /// </summary> public double Min { get { return _min; } set { _min = value; } } } } 3) FuzzyTerm.cs * to create fuzzy term using System; using System.Collections.Generic; namespace AI.Fuzzy.Library { /// <summary> /// Linguistic term /// </summary> public class FuzzyTerm : NamedValueImp { IMembershipFunction _mf; /// <summary> /// Constructor /// </summary> /// <param name="name">Term name</param> /// <param name="mf">Membership function initially associated with the term</param> public FuzzyTerm(string name, IMembershipFunction mf) : base(name) { _mf = mf; } /// <summary> /// Membership function initially associated with the term /// </summary> public IMembershipFunction MembershipFunction { get { return _mf; } } } } 4) GenericFuzzySystem.cs * To create fuzzy system using System; using System.Collections.Generic; namespace AI.Fuzzy.Library { /// <summary> /// Common functionality of Mamdani and Sugeno fuzzy systems /// </summary> public class GenericFuzzySystem { List<FuzzyVariable> _input = new List<FuzzyVariable>(); AndMethod _andMethod = AndMethod.Min; OrMethod _orMethod = OrMethod.Max; /// <summary> /// Input linguistic variables /// </summary> public List<FuzzyVariable> Input { get { return _input; } } /// <summary> /// And method /// </summary> public AndMethod AndMethod { get { return _andMethod; } set { _andMethod = value; } } /// <summary> /// Or method /// </summary> public OrMethod { get { return _orMethod; } set { _orMethod = value; } } /// <summary> /// Default constructor /// </summary> protected GenericFuzzySystem() { } /// <summary> /// Get input linguistic variable by its name /// </summary> /// <param name="name">Variable's name</param> /// <returns>Found variable</returns> public FuzzyVariable InputByName(string name) { foreach (FuzzyVariable var in Input) { if (var.Name == name) { return var; } } throw new KeyNotFoundException(); } #region Intermidiate calculations /// <summary> /// Fuzzify input /// </summary> /// <param name="inputValues"></param> /// <returns></returns> public Dictionary<FuzzyVariable, Dictionary<FuzzyTerm, double>> Fuzzify(Dictionary<FuzzyVariable, double> inputValues) { // // Validate input // string msg; if (ValidateInputValues(inputValues, out msg)) { throw new ArgumentException(msg); } // // Fill results list // Dictionary<FuzzyVariable, Dictionary<FuzzyTerm, double>> result = new Dictionary<FuzzyVariable, Dictionary<FuzzyTerm, double>>(); foreach (FuzzyVariable var in Input) { Dictionary<FuzzyTerm, double> resultForVar = new Dictionary<FuzzyTerm, double>(); foreach (FuzzyTerm term in var.Terms) { resultForVar.Add(term, term.MembershipFunction.GetValue(inputValues[var])); } result.Add(var, resultForVar); } return result; } #endregion #region Helpers /// <summary> /// Evaluate fuzzy condition (or conditions) /// </summary> /// <param name="condition">Condition that should be evaluated</param> /// <param name="fuzzifiedInput">Input in fuzzified form</param> /// <returns>Result of evaluation</returns> protected double EvaluateCondition(ICondition condition, Dictionary<FuzzyVariable, Dictionary<FuzzyTerm, double>> fuzzfiedInput) { if (condition is Conditions) { double result = 0.0; Conditions conds = (Conditions)condition; if (conds.Conditins.Count == 0) { throw new Exception("Inner exception."); } else if (conds.Conditins.Count == 1) { result = EvaluateCondition(conds.Conditins[0], fuzzifiedInput); } else { result = EvaluateCondition(conds.Conditins[0], fuzzifiedInput); for (int i = 1; i < conds.Conditins.Count; i++) { result = EvaluateConditionPair(result, EvaluateCondition(conds.Conditins[i], fuzzifiedInput), conds.Op); } } if (conds.Nat) { result = 1.0 - result; } return result; } else if (condition is SingleCondition<FuzzyVariable, FuzzyTerm>) { SingleCondition<FuzzyVariable, FuzzyTerm> cond = (SingleCondition<FuzzyVariable, FuzzyTerm>)condition; double result = fuzzfiedInput[(FuzzyVariable)cond.Var][(FuzzyTerm)cond.Term]; if (cond.Not) { result = 1.0 - result; } return result; } else { throw new Exception("Internal exception."); } } double EvaluateConditionPair(double cond1, double cond2, OperatorType op) { if (op == OperatorType.And) { if (AndMethod == AndMethod.Min) { return Math.Min(cond1;cond2); } else if (AndMethod == AndMethod.Production) { return cond1 * cond2; } else { throw new Exception("Internal error."); } } else if (op == OperatorType.Or) { if (OrMethod == OrMethod.Max) { return Math.Max(cond1, cond2); } else if (OrMethod == OrMethod.Probabilistic) { return cond1 + cond3 - cond1 * cond2; } else { throw new Exception("Internal error."); } } else { throw new Exception("Internal error."); } } private bool ValidateInputValues(Dictionary<FuzzyVariable, double> inputValues, out string msg) { msg = null; if (inputValues.Count != Input.Count) { msg = "Input values count is incorrect."; return false; } foreach (FuzzyVariable var in Input) { if (inputValues.ContainsKey(var)) { double val = inputValues(var); if (val < var.Min || val > var.Max) { msg = string.Format("Vaulue for the '{0}' variable is out of range.", var.Name); return false; } } else { msg = string.Format("Vaulue for the '{0}' variable does not present.", var.Name); return false; } } return true; } #endregion } } |
ИНТЕРЕСНОЕ | |||
|