Смекни!
smekni.com

Информационное обеспечение системы управления подъёмно-транспортным механизмом (стр. 12 из 13)

5. Заде Л. Понятие лингвистических переменных и его применение к принятию приближенных решений. - М.: Мир, 1976. - 165 с.

6. Zadeh L.A. Fuzzy logic and approximate reasoning // Synthese, 1975. - V. 80. P. 407 - 428.

7. Мелихов А.Н., Баронец В.Д. Проектирование микропроцессорных устройств обработки нечеткой информации. ‑ Ростов-на-Дону.: Изд-во Ростовского университета, 1990. - 128 с.

8. Берштейн Л.С., Финаев В.И. Адаптивное управление с нечеткими стратегиями. – Ростов-на-Дону: Изд-во Рост. ун-та, 1993. - 134 с.


ПРИЛОЖЕНИЕ

Листинг программы

1) FuzzyRule.cs

/*

* biblioteka dlya cozdaniya ne4etkovo block’a plavil

*/

using System;

using System.Collections.Generic;

namespace AI.Fuzzy.Library

{

// Alias for a fuzzy single condition

using FuzzyCondition = SingleCondition<FuzzyVariable, FuzzyTerm>;

// Alias for a fuzzy conclusion

using FuzzyConclusion = SingleCondition<FuzzyVariable, FuzzyTerm>;

// Alias for a conclusion for Sugeno fuzzy systems

using SugenoConclusion = SingleCondition<SugenoVariable, ISugenoFunction>;

/// <summary>

/// And/Or operator type

/// </summary>

public enum OperatorType

{

/// <summary>

/// And operator

/// </summary>

And,

/// <summary>

/// Or operator

/// </summary>

Or

}

/// <summary>

/// Hedge modifiers

/// </summary>

public enum HedgeType

{

/// <summary>

/// None

/// </summary>

None,

/// <summary>

/// Cube root

/// </summary>

Slightly,

/// <summary>

/// Square root

/// </summary>

Somewhat,

/// <summary>

/// Square

/// </summary>

Very,

/// <summary>

/// Cube

/// </summary>

Extremely

}

/// <summary>

/// Interface of conditions used in the 'if' expression

/// </summary>

public interface ICondition

{}

/// <summary>

/// Single condition

/// </summary>

public class SingleCondition<VariableType, ValueType> : ICondition

where VariableType : class, INamedVariable

where ValueType : class, INamedValue

{

VariableType _var = null;

bool _not = false;

ValueType _term = null;

/// <summary>

/// Default constructor

/// </summary>

internal SingleCondition()

{

}

/// <summary>

/// Constructor

/// </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>

internal SingleCondition(VariableType var, ValueType term)

{

_var = var;

_term = term;

}

/// <summary>

/// Constructor

/// </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>

internal SingleCondition(VariableType var, ValueType term, bool not)

: this(var, term)

{

_not = not;

}

/// <summary>

/// A linguistic variable to which the condition is related

/// </summary>

public VariableType Var

{

get { return _var; }

set { _var = value; }

}

/// <summary>

/// Is MF inverted

/// </summary>

public bool Not

{

get { return _not; }

set { _not = value; }

}

/// <summary>

/// A term in expression 'var is term'

/// </summary>

public ValueType Term //TODO: 'Term' is bad property name here

{

get { return _term; }

set { _term = value; }

}

}

/// <summary>

/// Several conditions linked by or/and operators

/// </summary>

public class Conditions : ICondition

{

bool _not = false;

OperatorType _op = OperatorType.And;

List<ICondition> _conditins = new List<ICondition>();

/// <summary>

/// Is MF inverted

/// </summary>

public bool Not

{

get { return _not; }

set { _not = value; }

}

/// <summary>

/// Operator that links expressions (and/or)

/// </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;