📄 fuzzysystem.cs
字号:
Dictionary<FuzzyRule, IMembershipFunction> conclusions = new Dictionary<FuzzyRule, IMembershipFunction>();
foreach (FuzzyRule rule in conditions.Keys)
{
MfCompositionType compType;
switch (_implMethod)
{
case ImplicationMethod.Min:
compType = MfCompositionType.Min;
break;
case ImplicationMethod.Production:
compType = MfCompositionType.Prod;
break;
default:
throw new Exception("Internal error.");
}
CompositeMembershipFunction resultMf = new CompositeMembershipFunction(
compType,
new ConstantMembershipFunction(conditions[rule]),
rule.Conclusion.Term.MembershipFunction);
conclusions.Add(rule, resultMf);
}
return conclusions;
}
/// <summary>
/// Aggregate results
/// </summary>
/// <param name="conclusions">Rules' results</param>
/// <returns>Aggregated fuzzy result</returns>
public Dictionary<FuzzyVariable, IMembershipFunction> Aggregate(Dictionary<FuzzyRule, IMembershipFunction> conclusions)
{
Dictionary<FuzzyVariable, IMembershipFunction> fuzzyResult = new Dictionary<FuzzyVariable, IMembershipFunction>();
foreach (FuzzyVariable var in _output)
{
List<IMembershipFunction> mfList = new List<IMembershipFunction>();
foreach (FuzzyRule rule in conclusions.Keys)
{
if (rule.Conclusion.Var == var)
{
mfList.Add(conclusions[rule]);
}
}
MfCompositionType composType;
switch (_aggrMethod)
{
case AggregationMethod.Max:
composType = MfCompositionType.Max;
break;
case AggregationMethod.Sum:
composType = MfCompositionType.Sum;
break;
default:
throw new Exception("Internal exception.");
}
fuzzyResult.Add(var, new CompositeMembershipFunction(composType, mfList));
}
return fuzzyResult;
}
/// <summary>
/// Calculate crisp result for each rule
/// </summary>
/// <param name="fuzzyResult"></param>
/// <returns></returns>
public Dictionary<FuzzyVariable, double> Defuzzify(Dictionary<FuzzyVariable, IMembershipFunction> fuzzyResult)
{
Dictionary<FuzzyVariable, double> crispResult = new Dictionary<FuzzyVariable, double>();
foreach (FuzzyVariable var in fuzzyResult.Keys)
{
crispResult.Add(var, Defuzzify(fuzzyResult[var], var.Min, var.Max));
}
return crispResult;
}
///////////////////////////////////////////////////////////////////////
#region Helpers
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;
}
double EvaluateCondition(ICondition condition, Dictionary<FuzzyVariable, Dictionary<FuzzyTerm, double>> fuzzifiedInput)
{
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.Not)
{
result = 1.0 - result;
}
return result;
}
else if (condition is SingleCondition)
{
SingleCondition cond = (SingleCondition)condition;
double result = fuzzifiedInput[cond.Var][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 + cond2 - cond1 * cond2;
}
else
{
throw new Exception("Internal error.");
}
}
else
{
throw new Exception("Internal error.");
}
}
double Defuzzify(IMembershipFunction mf, double min, double max)
{
if (_defuzzMethod == DefuzzificationMethod.Centroid)
{
int k = 50;
double step = (max - min) / k;
//
// Calculate a center of gravity as integral
//
double ptLeft = 0.0;
double ptCenter = 0.0;
double ptRight = 0.0;
double valLeft = 0.0;
double valCenter = 0.0;
double valRight = 0.0;
double val2Left = 0.0;
double val2Center = 0.0;
double val2Right = 0.0;
double numerator = 0.0;
double denominator = 0.0;
for (int i = 0; i < k; i++)
{
if (i == 0)
{
ptRight = min;
valRight = mf.GetValue(ptRight);
val2Right = ptRight * valRight;
}
ptLeft = ptRight;
ptCenter = min + step * ((double)i + 0.5);
ptRight = min + step * (i + 1);
valLeft = valRight;
valCenter = mf.GetValue(ptCenter);
valRight = mf.GetValue(ptRight);
val2Left = val2Right;
val2Center = ptCenter * valCenter;
val2Right = ptRight * valRight;
numerator += step * (val2Left + 4 * val2Center + val2Right) / 3.0;
denominator += step * (valLeft + 4 * valCenter + valRight) / 3.0;
}
return numerator / denominator;
}
else if (_defuzzMethod == DefuzzificationMethod.Bisector)
{
// TODO:
throw new NotSupportedException();
}
else
{
throw new Exception("Internal exception.");
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -