AForge.Fuzzy.Database.AddVariable C# (CSharp) Method

AddVariable() public method

Adds a linguistic variable to the database.
The linguistic variable was not initialized. The linguistic variable name already exists in the database.
public AddVariable ( LinguisticVariable variable ) : void
variable LinguisticVariable A linguistic variable to add.
return void
        public void AddVariable( LinguisticVariable variable )
        {
            // checking for existing name
            if ( this.variables.ContainsKey( variable.Name ) )
                throw new ArgumentException( "The linguistic variable name already exists in the database." );
            
            // adding label
            this.variables.Add( variable.Name, variable );
        }

Usage Example

Example #1
0
        public InferenceSystem SetupInferenceSystem(int width)
        {
            var mp = new TrapezoidalFunction(centerPoint - width/2, centerPoint, centerPoint + width/2);
            var mn = new TrapezoidalFunction(-centerPoint - width/2, -centerPoint, -centerPoint + width/2);
            var sp = new TrapezoidalFunction(-centerPoint/2 + width/4, (double) centerPoint/2, centerPoint/2 + width/3);
            var sn = new TrapezoidalFunction(-centerPoint/2 - width/3, (double) -centerPoint/2, centerPoint/2 - width/4);
            var ze = new TrapezoidalFunction(-(double)centerPoint / 4, 0, (double)centerPoint / 4);

            var mpSet = new FuzzySet("MP", mp);
            var mnSet = new FuzzySet("MN", mn);
            var spSet = new FuzzySet("SP", sp);
            var snSet = new FuzzySet("SN", sn);
            var zeSet = new FuzzySet("ZE", ze);

            var ruleDatabase = new Database();

            for (int i = 0; i < windowSize*windowSize - 1; i++)
            {
                var variable = new LinguisticVariable(String.Format("IN{0}", i), -255, 255);
                variable.AddLabel(mpSet);
                variable.AddLabel(mnSet);
                ruleDatabase.AddVariable(variable);
            }

            var outVariable = new LinguisticVariable("OUT", -centerPoint - width/2, centerPoint + width/2);
            outVariable.AddLabel(spSet);
            outVariable.AddLabel(snSet);
            outVariable.AddLabel(zeSet);
            ruleDatabase.AddVariable(outVariable);
            var inferenceSystem = new InferenceSystem(ruleDatabase, new CentroidDefuzzifier(100));
            string rule1 = "IF ";
            string rule2 = "IF ";
            string rule3 = "IF ";
            for (int i = 0; i < windowSize*windowSize - 1; i++)
            {
                rule1 += String.Format("IN{0} is MP and ", i);
                rule2 += String.Format("IN{0} is MN and ", i);
                rule3 += String.Format("IN{0} is not MP and IN{0} is not MN AND ", i);
            }

            rule1 = rule1.Remove(rule1.Length - 4, 4);
            rule2 = rule2.Remove(rule2.Length - 4, 4);
            rule3 = "IF NOT (" + rule1.Replace("IF", "") + ") AND NOT(" + rule2.Replace("IF", "") + ")";

            rule1 += " then OUT is SN";
            rule2 += " then OUT is SP";
            rule3 += " then OUT is ZE";

            inferenceSystem.NewRule("Rule1", rule1);
            inferenceSystem.NewRule("Rule2", rule2);
            inferenceSystem.NewRule("Rule3", rule3);

            return inferenceSystem;
        }
All Usage Examples Of AForge.Fuzzy.Database::AddVariable