Example #1
0
        public DecisionMakerType2(string stretchedParametersXml, FuzzyVariable outputVariable, RulesList ruleDefinitions, List<FuzzyVariable> includedVariables)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Type2ParamsList));
            XmlReaderSettings settings = new XmlReaderSettings();
            using (StringReader textReader = new StringReader(stretchedParametersXml))
            {
                using (XmlReader xmlReader = XmlReader.Create(textReader, settings))
                {
                    Type2ParamsList deserializedT2Params = (Type2ParamsList)serializer.Deserialize(xmlReader);

                    inputVariables = new List<Type2FuzzyVariable>();
                    foreach (var variable in deserializedT2Params.Items)
                    {
                        if (includedVariables.Exists(v => v.Name == variable.ParamName))
                        {
                            Type2FuzzyVariable newVariable = new Type2FuzzyVariable(
                                variable.ParamName,
                                variable.Min,
                                variable.Max,
                                variable.Unit);
                            foreach (var term in variable.Terms)
                            {
                                Type2FuzzyTerm newTerm = new Type2FuzzyTerm(
                                    term.name,
                                    new NormalMembershipFunction(term.b.Upper, term.c.Upper),
                                    new NormalMembershipFunction(term.b.Lower, term.c.Lower));
                                newVariable.Terms.Add(newTerm);
                            }

                            inputVariables.Add(newVariable);
                        }
                    }

                    // output variable
                    this.outputVariable = new Type2FuzzyVariable(
                            outputVariable.Name,
                            outputVariable.Min,
                            outputVariable.Max,
                            outputVariable.Unit);
                    foreach (var term in outputVariable.Terms)
                    {
                        Type2FuzzyTerm newTerm = new Type2FuzzyTerm(
                            term.Name,
                            new NormalMembershipFunction(
                                (term.MembershipFunction as NormalMembershipFunction).B,
                                (term.MembershipFunction as NormalMembershipFunction).Sigma),
                            new NormalMembershipFunction(
                                (term.MembershipFunction as NormalMembershipFunction).B,
                                (term.MembershipFunction as NormalMembershipFunction).Sigma));
                        this.outputVariable.Terms.Add(newTerm);
                    }

                    // rules
                    rules = new List<Type2MamdaniFuzzyRule>();
                    var updatedDefinitions = new List<RuleDef>();
                    foreach (var rule in ruleDefinitions.Items)
                    {
                        string[] splitDef = rule.Definition.Split(
                            new[] {"if", "and", "(", ")"},
                            StringSplitOptions.RemoveEmptyEntries);

                        string updatedDef = string.Empty;
                        foreach (var condition in splitDef)
                        {
                            if (condition == string.Empty)
                            {
                                continue;
                            }

                            var trimmedCondition = condition.Trim();
                            if (trimmedCondition.StartsWith("then"))
                            {
                                updatedDef += trimmedCondition;
                            }
                            else
                            {
                                string variable = trimmedCondition.Split(' ')[0];
                                if (includedVariables.Exists(v => v.Name == variable))
                                {
                                    string keyword = updatedDef == string.Empty ? "if" : "and";
                                    updatedDef += string.Format("{0} ({1}) ", keyword, trimmedCondition);
                                }
                            }
                        }

                        if (!updatedDefinitions.Exists(r => r.Definition.Equals(updatedDef)))
                        {
                            updatedDefinitions.Add(new RuleDef { Definition = updatedDef, Weight = rule.Weight });
                            Type2MamdaniFuzzyRule newRule = ParseRule(updatedDef);
                            newRule.Weight = rule.Weight;
                            rules.Add(newRule);
                        }
                    }

                    isInitialized = true;
                }
            }
        }
Example #2
0
        public DecisionMakerType2(DecisionMakerType1 type1Source)
        {
            this.type1Source = type1Source;

            // input variables
            inputVariables = new List<Type2FuzzyVariable>();
            foreach (var variable in type1Source.InputVariables)
            {
                Type2FuzzyVariable newVariable = new Type2FuzzyVariable(
                    variable.Name,
                    variable.Min,
                    variable.Max,
                    variable.Unit);
                foreach (var term in variable.Terms)
                {
                    Type2FuzzyTerm newTerm = new Type2FuzzyTerm(
                        term.Name,
                        new NormalMembershipFunction(
                            (term.MembershipFunction as NormalMembershipFunction).B,
                            (term.MembershipFunction as NormalMembershipFunction).Sigma),
                        new NormalMembershipFunction(
                            (term.MembershipFunction as NormalMembershipFunction).B,
                            (term.MembershipFunction as NormalMembershipFunction).Sigma));
                    newVariable.Terms.Add(newTerm);
                }

                inputVariables.Add(newVariable);
            }

            // output variable
            outputVariable = new Type2FuzzyVariable(
                    type1Source.OutputVariable.Name,
                    type1Source.OutputVariable.Min,
                    type1Source.OutputVariable.Max,
                    type1Source.OutputVariable.Unit);
            foreach (var term in type1Source.OutputVariable.Terms)
            {
                Type2FuzzyTerm newTerm = new Type2FuzzyTerm(
                    term.Name,
                    new NormalMembershipFunction(
                        (term.MembershipFunction as NormalMembershipFunction).B,
                        (term.MembershipFunction as NormalMembershipFunction).Sigma),
                    new NormalMembershipFunction(
                        (term.MembershipFunction as NormalMembershipFunction).B,
                        (term.MembershipFunction as NormalMembershipFunction).Sigma));
                outputVariable.Terms.Add(newTerm);
            }

            // rules
            rules = new List<Type2MamdaniFuzzyRule>();
            foreach (var rule in type1Source.RulesDefinitions.Items)
            {
                Type2MamdaniFuzzyRule newRule = ParseRule(rule.Definition);
                newRule.Weight = rule.Weight;
                rules.Add(newRule);
            }

            isInitialized = false;
        }