Exemple #1
0
        static void Main(string[] args)
        {
            //IF ((temperature is hot) OR (temperature is too-hot)) AND (target is warm) THEN command is cool

            //((temperature is hot) OR (temperature is too-hot)) AND (target is warm)
            Node rootNode = new Node();

            FuzzyInferenceSystem fis = new FuzzyInferenceSystem();

            //input conditions
            Condition cold = new Condition("cold", new Triangle(-0.4f,0.0f,0.4f));
            Condition medium = new Condition("medium", new Triangle(0.1f,0.5f,0.9f));
            Condition warm = new Condition("hot", new Triangle(0.6f,1.0f,1.4f));
            Variable inputHeat = new Variable("heat");
            fis.AddInputVariableMembership(inputHeat, new Condition[]{cold,medium,warm});

            Condition down = new Condition("down", new Triangle(-0.4f,0.0f,0.4f));
            Condition up = new Condition("up", new Triangle(0.6f,1.0f,1.4f));
            Variable outputMotion = new Variable("motion");
            fis.AddOutputVariableMembership(outputMotion, new Condition[]{down,up});

            fis.AddRule("IF (heat is not hot) then motion is down");
            fis.AddRule("IF (heat is hot) then motion is up");
            //fis.AddRule("IF (heat is cold) then motion is up");

            //Test
            fis.SetInputVariable(inputHeat,

                .0f);
            fis.Run();
            float val = fis.GetOutputVariableValue("motion");

            Console.WriteLine("Input {0} -> Output {1}", inputHeat.Value, val);

            Console.ReadLine();
        }
 public void AddOutputVariableMembership(Variable v, IEnumerable<Condition> conditions)
 {
     OutputVariables[v] = new List<Condition>();
     foreach (var c in conditions)
         OutputVariables[v].Add(c);
 }
 public void SetInputVariable(Variable v, float value)
 {
     inputValues[v] = value;
     v.Value = value;
 }
Exemple #4
0
        public Node ParseInputSyntax(Node rootNode, string expression)
        {
            expression = expression.ToUpper();

            var freqs = expression
                    .Where(c => Char.IsLetter(c))
                    .GroupBy(c => c)
                    .ToDictionary(g => g.Key, g => g.Count());

            int firstSBrac = expression.IndexOf('(');
            int firstEBrac = ScanMatchingBrace(expression, firstSBrac);

            if (firstEBrac - firstSBrac == expression.Length)
                 return ParseInputSyntax(rootNode, expression.Substring(firstSBrac+1, firstEBrac - firstSBrac-2));

            // base case, raw expression
            if (firstEBrac == -1 || firstSBrac == -1)
            {
                int isIndex = expression.IndexOf("IS");
                int notIndex = expression.IndexOf("NOT");

                string left = expression.Substring(0, isIndex - 1);

                string right = expression.Substring(isIndex + 3 + (notIndex > 0 ? 4 : 0));

                Variable inputVar = new Variable(left);
                // Condition cond = new Condition(right);

                Condition cond = GetInputConditionFromFis(inputVar, right);

                // IF there is a not
                var clause = new Clause(inputVar, cond);
                if(notIndex > 0)
                {
                    Node node = new Node(new NotFunction(), clause, null);
                    return node;
                }

                return clause;
            }

            string leftParse = expression.Substring(firstSBrac + 1, firstEBrac - 2);
            Node leftOperand = new Node();
            rootNode.LeftOperand = ParseInputSyntax(leftOperand, leftParse);

            expression = expression.Substring(firstEBrac).TrimStart();
            string function = expression.Substring(0, expression.IndexOf(' '));
            IFunction func = null;
            switch (function.ToLower().Trim())
            {
                case "and":
                    func = new AndFunction();
                    break;
                case "or":
                    func = new OrFunction();
                    break;
                default:
                    break;
            }
            rootNode.Function = func;
            expression = expression.Substring(expression.IndexOf(' ') + 1);

            int lastSBrac = expression.IndexOf('(');
            int lastEBrac = ScanMatchingBrace(expression, firstSBrac);

            string rightParse = expression.Substring(lastSBrac + 1, lastEBrac - 2);
            Node rightOperand = new Node();
            rootNode.RightOperand = ParseInputSyntax(rightOperand, rightParse);

            return rootNode;
        }
Exemple #5
0
 private Condition GetOutputConditionFromFis(Variable vari, string conditionName)
 {
     foreach (var v in fis.OutputVariables[vari])
     {
         if (v.Name == conditionName)
             return v;
     }
     throw new Exception("Condition " + conditionName + " not found in FIS");
 }
Exemple #6
0
        public Rule ParseLexicalStatement(string expression)
        {
            //IF blah THEN blah
            expression = expression.ToUpper();
            expression = expression.Substring(3);
            int indexOfThen = expression.IndexOf("THEN") + 4;
            string inputSynt = expression.Substring(0, indexOfThen-4).TrimEnd();

            Node inputNode = new Node();
            inputNode = ParseInputSyntax(inputNode, inputSynt);

            expression = expression.Substring(indexOfThen);

            // output
            int isIndex = expression.IndexOf("IS");

            string left = expression.Substring(0, isIndex - 1).Trim();
            string right = expression.Substring(isIndex + 3).Trim();

            Variable outputVar = new Variable(left);

            Condition cond = GetOutputConditionFromFis(outputVar, right);

            Rule r = new Rule();
            r.InputClause = inputNode;
            r.OutputClause = new Clause(outputVar, cond);

            return r;
        }
Exemple #7
0
 public void SetValue(Variable var, float value)
 {
     inputVariables[var].SetValue(value);
 }