Example #1
0
        static private List <IExpression> ExtractSingleCondidtions(List <IExpression> conditionExpression, List <FuzzyVariable> input, Dictionary <string, Lexem> lexems)
        {
            List <IExpression> copyExpressions = conditionExpression.GetRange(0, conditionExpression.Count);
            List <IExpression> expressions     = new List <IExpression>();

            while (copyExpressions.Count > 0)
            {
                if (copyExpressions[0] is VarLexem <FuzzyVariable> )
                {
                    //
                    // Разбор переменной
                    //
                    VarLexem <FuzzyVariable> varLexem = (VarLexem <FuzzyVariable>)copyExpressions[0];
                    if (copyExpressions.Count < 3)
                    {
                        throw new Exception(string.Format("Состояние начинается с '{0}' не корректно.", varLexem.Text));
                    }

                    if (varLexem.Input == false)
                    {
                        throw new Exception("Переменная в состоянии должна быть входной переменной.");
                    }

                    //
                    // Разбор "is" лексемы
                    //
                    Lexem exprIs = (Lexem)copyExpressions[1];
                    if (exprIs != lexems["is"])
                    {
                        throw new Exception(string.Format("'is' ключевое слово должно идти после {0} идентификатора.", varLexem.Text));
                    }


                    //
                    // Разбор 'not' лексемы (если существует)
                    //
                    int  cur = 2;
                    bool not = false;
                    if (copyExpressions[cur] == lexems["not"])
                    {
                        not = true;
                        cur++;

                        if (copyExpressions.Count <= cur)
                        {
                            throw new Exception("Ошибка около 'not' в состоянии части правила.");
                        }
                    }

                    //"slightly"    - немного
                    //"somewhat"    - в некотором роде
                    //"very"        - очень
                    //"extremely"   - чрезвычайно

                    //
                    // Разбор hedge модификатора (если существует)
                    //
                    HedgeType hedge = HedgeType.None;
                    if (copyExpressions[cur] == lexems["slightly"])
                    {
                        hedge = HedgeType.Slightly;
                    }
                    else if (copyExpressions[cur] == lexems["somewhat"])
                    {
                        hedge = HedgeType.Somewhat;
                    }
                    else if (copyExpressions[cur] == lexems["very"])
                    {
                        hedge = HedgeType.Very;
                    }
                    else if (copyExpressions[cur] == lexems["extremely"])
                    {
                        hedge = HedgeType.Extremely;
                    }

                    if (hedge != HedgeType.None)
                    {
                        cur++;

                        if (copyExpressions.Count <= cur)
                        {
                            throw new Exception(string.Format("Ошибка около '{0}' в состоянии части правила.", hedge.ToString().ToLower()));
                        }
                    }

                    //
                    // Разбор терма
                    //
                    Lexem exprTerm = (Lexem)copyExpressions[cur];
                    if (!(exprTerm is IAltLexem))
                    {
                        throw new Exception(string.Format("Неверный идентификатор '{0}' в стостоянии части правила.", exprTerm.Text));
                    }
                    IAltLexem             altLexem  = (IAltLexem)exprTerm;
                    TermLexem <FuzzyTerm> termLexem = null;
                    do
                    {
                        if (!(altLexem is TermLexem <FuzzyTerm>))
                        {
                            continue;
                        }

                        termLexem = (TermLexem <FuzzyTerm>)altLexem;
                        if (!varLexem.Var.Values.Contains(termLexem.Term))
                        {
                            termLexem = null;
                            continue;
                        }
                    }while ((altLexem = altLexem.Alternative) != null && termLexem == null);

                    if (termLexem == null)
                    {
                        throw new Exception(string.Format("Неверный идентификатор '{0}' в стостоянии части правила.", exprTerm.Text));
                    }

                    //
                    // Добавление нового выражения состояния
                    //
                    FuzzyCondition condition = new FuzzyCondition(varLexem.Var, termLexem.Term, not, hedge);
                    expressions.Add(new ConditionExpression(copyExpressions.GetRange(0, cur + 1), condition));
                    copyExpressions.RemoveRange(0, cur + 1);
                }
                else
                {
                    IExpression expr = copyExpressions[0];
                    if (expr == lexems["and"] ||
                        expr == lexems["or"] ||
                        expr == lexems["("] ||
                        expr == lexems[")"])
                    {
                        expressions.Add(expr);
                        copyExpressions.RemoveAt(0);
                    }
                    else
                    {
                        Lexem unknownLexem = (Lexem)expr;
                        throw new Exception(string.Format("Лексема  '{0}' найдена в ошибочном месте в состоянии части правила.", unknownLexem.Text));
                    }
                }
            }

            return(expressions);
        }
Example #2
0
        /// <summary>
        /// Подсчет нечеткого состояние
        /// </summary>
        /// <param name="condition">Состояние которое должно быть подсчитано</param>
        /// <param name="fuzzifiedInput">Входы в нечеткой форме</param>
        /// <returns>Результат нечеткого состояния</returns>
        protected double EvaluateCondition(ICondition condition, Dictionary <FuzzyVariable, Dictionary <FuzzyTerm, double> > fuzzifiedInput)
        {
            if (condition is Conditions)                            //Если состояние
            {
                double     result = 0.0;
                Conditions conds  = (Conditions)condition;

                if (conds.ConditionsList.Count == 0)
                {
                    throw new Exception("Состояний нет.");
                }
                else if (conds.ConditionsList.Count == 1)
                {
                    result = EvaluateCondition(conds.ConditionsList[0], fuzzifiedInput);
                }
                else
                {
                    result = EvaluateCondition(conds.ConditionsList[0], fuzzifiedInput);
                    for (int i = 1; i < conds.ConditionsList.Count; i++)
                    {
                        result = EvaluateConditionPair(result, EvaluateCondition(conds.ConditionsList[i], fuzzifiedInput), conds.Op);
                    }
                }

                if (conds.Not)
                {
                    result = 1.0 - result;
                }

                return(result);
            }
            else if (condition is FuzzyCondition)
            {
                FuzzyCondition cond   = (FuzzyCondition)condition;
                double         result = fuzzifiedInput[(FuzzyVariable)cond.Var][(FuzzyTerm)cond.Term];

                switch (cond.Hedge)
                {
                case HedgeType.Slightly:
                    result = Math.Pow(result, 1.0 / 3.0);     //Cube root
                    break;

                case HedgeType.Somewhat:
                    result = Math.Sqrt(result);
                    break;

                case HedgeType.Very:
                    result = result * result;
                    break;

                case HedgeType.Extremely:
                    result = Math.Pow(result, 3);
                    break;

                default:
                    break;
                }

                if (cond.Not)
                {
                    result = 1.0 - result;
                }
                return(result);
            }
            else
            {
                throw new Exception("Типо состояния не найден.");
            }
        }
Example #3
0
 public ConditionExpression(List <IExpression> expressions, FuzzyCondition condition)
 {
     _expressions = expressions;
     _condition   = condition;
 }