/// <summary>
        /// Creates a variable with evenly sized and spaced trapezoidal functions.
        /// </summary>
        /// <param name="varName">The name of this variable.</param>
        /// <param name="min">Minimum value.</param>
        /// <param name="max">Maxiumum value.</param>
        /// <param name="termNames">The terms of this variable.</param>
        /// <returns>Symmetric variable.</returns>
        public static FuzzyVariable CreateSymmetricTrapezoidalVariable(string varName, double min, double max, params string[] termNames)
        {
            FuzzyVariable fuzzyVar = new FuzzyVariable(varName, min, max);

            int count = 0;
            double spread = (max - min) / ((termNames.Count() * 2) - 1);

            foreach (string term in termNames)
            {
                if(count == 0)
                {
                    fuzzyVar.Terms.Add(new FuzzyTerm(term, new TrapezoidMembershipFunction(spread * count, spread * count, spread * ++count, spread * ++count)));
                }
                else if(count == (termNames.Count() * 2) - 2)
                {
                    fuzzyVar.Terms.Add(new FuzzyTerm(term, new TrapezoidMembershipFunction(spread * --count, spread * ++count, spread * ++count, spread * count)));
                }
                else
                {
                    fuzzyVar.Terms.Add(new FuzzyTerm(term, new TrapezoidMembershipFunction(spread * --count, spread * ++count, spread * ++count, spread * ++count)));
                }
            }

            return fuzzyVar;
        }
Beispiel #2
0
 /// <summary>
 /// Get coefficient by fuzzy variable
 /// </summary>
 /// <param name="var">Fuzzy variable</param>
 /// <returns>Coefficient's value</returns>
 public double GetCoefficient(FuzzyVariable var)
 {
     if (var == null)
     {
         return(_constValue);
     }
     else
     {
         return(_coeffs[var]);
     }
 }
        public DecisionMakerType1(
            List<FuzzyVariable> inputVariables,
            FuzzyVariable outputVariable,
            RulesList ruleDefinitions,
            List<string> includedVariables = null)
        {
            if (includedVariables == null)
            {
                includedVariables = (from v in inputVariables select v.Name).ToList();
            }

            this.fsWellEval = new MamdaniFuzzySystem();
            this.fsWellEval.Input.AddRange(from v in inputVariables where includedVariables.Contains(v.Name) select v);
            this.fsWellEval.Output.Add(outputVariable);

            this.RulesDefinitions = new RulesList();
            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.Contains(variable))
                        {
                            string keyword = updatedDef == string.Empty ? "if" : "and";
                            updatedDef += string.Format("{0} ({1}) ", keyword, trimmedCondition);
                        }
                    }
                }

                if (!RulesDefinitions.Items.Exists(r => r.Definition.Equals(updatedDef)))
                {
                    this.RulesDefinitions.Items.Add(new RuleDef { Definition = updatedDef, Weight = rule.Weight });
                    MamdaniFuzzyRule newRule = this.fsWellEval.ParseRule(updatedDef);
                    this.fsWellEval.Rules.Add(newRule);
                }
            }
        }
Beispiel #4
0
 /// <summary>
 /// Set coefficient by fuzzy variable
 /// </summary>
 /// <param name="var">Fuzzy variable</param>
 /// <param name="coeff">New value of the coefficient</param>
 public void SetCoefficient(FuzzyVariable var, double coeff)
 {
     if (var == null)
     {
         _constValue = coeff;
     }
     else
     {
         _coeffs[var] = coeff;
     }
 }
Beispiel #5
0
        MamdaniFuzzySystem CreateSystem()
        {
            //
            // Create empty fuzzy system
            //
            MamdaniFuzzySystem fsTips = new MamdaniFuzzySystem();

            //
            // Create input variables for the system
            //
            FuzzyVariable fvService = new FuzzyVariable("service", 0.0, 10.0);
            fvService.Terms.Add(new FuzzyTerm("poor", new TriangularMembershipFunction(-5.0, 0.0, 5.0)));
            fvService.Terms.Add(new FuzzyTerm("good", new TriangularMembershipFunction(0.0, 5.0, 10.0)));
            fvService.Terms.Add(new FuzzyTerm("excellent", new TriangularMembershipFunction(5.0, 10.0, 15.0)));
            fsTips.Input.Add(fvService);

            FuzzyVariable fvFood = new FuzzyVariable("food", 0.0, 10.0);
            fvFood.Terms.Add(new FuzzyTerm("rancid", new TrapezoidMembershipFunction(0.0, 0.0, 1.0, 3.0)));
            fvFood.Terms.Add(new FuzzyTerm("delicious", new TrapezoidMembershipFunction(7.0, 9.0, 10.0, 10.0)));
            fsTips.Input.Add(fvFood);

            //
            // Create output variables for the system
            //
            FuzzyVariable fvTips = new FuzzyVariable("tips", 0.0, 30.0);
            fvTips.Terms.Add(new FuzzyTerm("cheap", new TriangularMembershipFunction(0.0, 5.0, 10.0)));
            fvTips.Terms.Add(new FuzzyTerm("average", new TriangularMembershipFunction(10.0, 15.0, 20.0)));
            fvTips.Terms.Add(new FuzzyTerm("generous", new TriangularMembershipFunction(20.0, 25.0, 30.0)));
            fsTips.Output.Add(fvTips);

            //
            // Create three fuzzy rules
            //
            try
            {
                MamdaniFuzzyRule rule1 = fsTips.ParseRule("if (service is poor )  or (food is rancid) then tips is cheap");
                MamdaniFuzzyRule rule2 = fsTips.ParseRule("if ((service is good)) then tips is average");
                MamdaniFuzzyRule rule3 = fsTips.ParseRule("if (service is excellent) or (food is delicious) then (tips is generous)");

                fsTips.Rules.Add(rule1);
                fsTips.Rules.Add(rule2);
                fsTips.Rules.Add(rule3);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Parsing exception: {0}", ex.Message));
                return null;
            }

            return fsTips;
        }
        /// <summary>
        /// Creates a variable with evenly sized and spaced triangular functions.
        /// </summary>
        /// <param name="varName">The name of this variable.</param>
        /// <param name="min">Minimum value.</param>
        /// <param name="max">Maxiumum value.</param>
        /// <param name="termNames">The terms of this variable.</param>
        /// <returns>Symmetric variable.</returns>
        public static FuzzyVariable CreateSymmetricTriangularVariable(string varName, double min, double max, params string[] termNames)
        {
            FuzzyVariable fuzzyVar = new FuzzyVariable(varName, min, max);

            int count = 0;
            double spread = (max - min) / (termNames.Count() - 1);

            foreach (string term in termNames)
            {
                fuzzyVar.Terms.Add(new FuzzyTerm(term, new TriangularMembershipFunction(spread * --count, spread * ++count, spread * ++count)));
            }

            return fuzzyVar;
        }
Beispiel #7
0
        public static FuzzyVariable GetOutputVariable()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ParamsList));
            var fs = new FileStream(VARIABLES_DATA, FileMode.Open);
            var parametersList = (ParamsList)serializer.Deserialize(fs);
            var parameters =
                (from item in parametersList.Items where item.Type == ParameterType.Output select item).ToList();
            fs.Close();

            if (parameters.Count > 0)
            {
                FuzzyVariable fsVariable = new FuzzyVariable(
                   parameters[0].ParamName,
                   parameters[0].Min,
                   parameters[0].Max);
                foreach (Term term in parameters[0].Terms)
                {
                    fsVariable.Terms.Add(new FuzzyTerm(term.Name, new NormalMembershipFunction(term.b, term.c)));
                }

                return fsVariable;
            }

            return null;
        }
Beispiel #8
0
        public static List<FuzzyVariable> GetInputVariables(int stage = 3)
        {
            List<FuzzyVariable> variables = new List<FuzzyVariable>();
            XmlSerializer serializer = new XmlSerializer(typeof(ParamsList));
            var fs = new FileStream(VARIABLES_DATA, FileMode.Open);
            var parametersList = (ParamsList)serializer.Deserialize(fs);
            var parameters = (from item in parametersList.Items
                              where item.Type == ParameterType.Input && item.Stage <= stage
                              select item)
                              .ToList();
            fs.Close();

            // Create input and output variables for the system
            foreach (var inputVariable in parameters)
            {
                FuzzyVariable fsVariable = new FuzzyVariable(
                    inputVariable.ParamName,
                    inputVariable.Min,
                    inputVariable.Max);
                foreach (Term term in inputVariable.Terms)
                {
                    fsVariable.Terms.Add(new FuzzyTerm(term.Name, new NormalMembershipFunction(term.b, term.c)));
                }

                variables.Add(fsVariable);
            }

            return variables;
        }
Beispiel #9
0
 void AddSugenoFuzzyRule(
     SugenoFuzzySystem fs,
     FuzzyVariable fv1,
     FuzzyVariable fv2,
     SugenoVariable sv,
     string value1,
     string value2,
     string result)
 {
     SugenoFuzzyRule rule = fs.EmptyRule();
     rule.Condition.Op = OperatorType.And;
     rule.Condition.ConditionsList.Add(rule.CreateCondition(fv1, fv1.GetTermByName(value1)));
     rule.Condition.ConditionsList.Add(rule.CreateCondition(fv2, fv2.GetTermByName(value2)));
     rule.Conclusion.Var = sv;
     rule.Conclusion.Term = sv.GetFuncByName(result);
     fs.Rules.Add(rule);
 }
Beispiel #10
0
        SugenoFuzzySystem CreateSystem()
        {
            //
            // Create empty Sugeno Fuzzy System
            //
            SugenoFuzzySystem fsCruiseControl = new SugenoFuzzySystem();

            //
            // Create input variables for the system
            //
            FuzzyVariable fvSpeedError = new FuzzyVariable("SpeedError", -20.0, 20.0);
            fvSpeedError.Terms.Add(new FuzzyTerm("slower", new TriangularMembershipFunction(-35.0, -20.0, -5.0)));
            fvSpeedError.Terms.Add(new FuzzyTerm("zero", new TriangularMembershipFunction(-15.0, -0.0, 15.0)));
            fvSpeedError.Terms.Add(new FuzzyTerm("faster", new TriangularMembershipFunction(5.0, 20.0, 35.0)));
            fsCruiseControl.Input.Add(fvSpeedError);

            FuzzyVariable fvSpeedErrorDot = new FuzzyVariable("SpeedErrorDot", -5.0, 5.0);
            fvSpeedErrorDot.Terms.Add(new FuzzyTerm("slower", new TriangularMembershipFunction(-9.0, -5.0, -1.0)));
            fvSpeedErrorDot.Terms.Add(new FuzzyTerm("zero", new TriangularMembershipFunction(-4.0, -0.0, 4.0)));
            fvSpeedErrorDot.Terms.Add(new FuzzyTerm("faster", new TriangularMembershipFunction(1.0, 5.0, 9.0)));
            fsCruiseControl.Input.Add(fvSpeedErrorDot);

            //
            // Create output variables for the system
            //
            SugenoVariable svAccelerate = new SugenoVariable("Accelerate");
            svAccelerate.Functions.Add(fsCruiseControl.CreateSugenoFunction("zero", new double[] { 0.0, 0.0, 0.0 }));
            svAccelerate.Functions.Add(fsCruiseControl.CreateSugenoFunction("faster", new double[] { 0.0, 0.0, 1.0 }));
            svAccelerate.Functions.Add(fsCruiseControl.CreateSugenoFunction("slower", new double[] { 0.0, 0.0, -1.0 }));
            svAccelerate.Functions.Add(fsCruiseControl.CreateSugenoFunction("func", new double[] { -0.04, -0.1, 0.0 }));
            fsCruiseControl.Output.Add(svAccelerate);

            //
            // Create fuzzy rules
            //
            AddSugenoFuzzyRule(fsCruiseControl, fvSpeedError, fvSpeedErrorDot, svAccelerate, "slower", "slower", "faster");
            AddSugenoFuzzyRule(fsCruiseControl, fvSpeedError, fvSpeedErrorDot, svAccelerate, "slower", "zero", "faster");
            AddSugenoFuzzyRule(fsCruiseControl, fvSpeedError, fvSpeedErrorDot, svAccelerate, "slower", "faster", "zero");
            AddSugenoFuzzyRule(fsCruiseControl, fvSpeedError, fvSpeedErrorDot, svAccelerate, "zero", "slower", "faster");
            AddSugenoFuzzyRule(fsCruiseControl, fvSpeedError, fvSpeedErrorDot, svAccelerate, "zero", "zero", "func");
            AddSugenoFuzzyRule(fsCruiseControl, fvSpeedError, fvSpeedErrorDot, svAccelerate, "zero", "faster", "slower");
            AddSugenoFuzzyRule(fsCruiseControl, fvSpeedError, fvSpeedErrorDot, svAccelerate, "faster", "slower", "zero");
            AddSugenoFuzzyRule(fsCruiseControl, fvSpeedError, fvSpeedErrorDot, svAccelerate, "faster", "zero", "slower");
            AddSugenoFuzzyRule(fsCruiseControl, fvSpeedError, fvSpeedErrorDot, svAccelerate, "faster", "faster", "slower");

            //
            // Adding the same rules in text form
            //
            ///////////////////////////////////////////////////////////////////
            //SugenoFuzzyRule rule1 = fsCruiseControl.ParseRule("if (SpeedError is slower) and (SpeedErrorDot is slower) then (Accelerate is faster)");
            //SugenoFuzzyRule rule2 = fsCruiseControl.ParseRule("if (SpeedError is slower) and (SpeedErrorDot is zero) then (Accelerate is faster)");
            //SugenoFuzzyRule rule3 = fsCruiseControl.ParseRule("if (SpeedError is slower) and (SpeedErrorDot is faster) then (Accelerate is zero)");
            //SugenoFuzzyRule rule4 = fsCruiseControl.ParseRule("if (SpeedError is zero) and (SpeedErrorDot is slower) then (Accelerate is faster)");
            //SugenoFuzzyRule rule5 = fsCruiseControl.ParseRule("if (SpeedError is zero) and (SpeedErrorDot is zero) then (Accelerate is func)");
            //SugenoFuzzyRule rule6 = fsCruiseControl.ParseRule("if (SpeedError is zero) and (SpeedErrorDot is faster) then (Accelerate is slower)");
            //SugenoFuzzyRule rule7 = fsCruiseControl.ParseRule("if (SpeedError is faster) and (SpeedErrorDot is slower) then (Accelerate is faster)");
            //SugenoFuzzyRule rule8 = fsCruiseControl.ParseRule("if (SpeedError is faster) and (SpeedErrorDot is zero) then (Accelerate is slower)");
            //SugenoFuzzyRule rule9 = fsCruiseControl.ParseRule("if (SpeedError is faster) and (SpeedErrorDot is faster) then (Accelerate is slower)");

            //fsCruiseControl.Rules.Add(rule1);
            //fsCruiseControl.Rules.Add(rule2);
            //fsCruiseControl.Rules.Add(rule3);
            //fsCruiseControl.Rules.Add(rule4);
            //fsCruiseControl.Rules.Add(rule5);
            //fsCruiseControl.Rules.Add(rule6);
            //fsCruiseControl.Rules.Add(rule7);
            //fsCruiseControl.Rules.Add(rule8);
            //fsCruiseControl.Rules.Add(rule9);

            ///////////////////////////////////////////////////////////////////

            return fsCruiseControl;
        }
 /// <summary>
 /// Create a single condition
 /// </summary>
 /// <param name="var">A linguistic variable to which the condition is related</param>
 /// <param name="term">A term in expression 'var is term'</param>
 /// <param name="not">Does condition contain 'not'</param>
 /// <param name="hedge">Hedge modifier</param>
 /// <returns>Generated condition</returns>
 public FuzzyCondition CreateCondition(FuzzyVariable var, FuzzyTerm term, bool not, HedgeType hedge)
 {
     return(new FuzzyCondition(var, term, not, hedge));
 }
 /// <summary>
 /// Create a single condition
 /// </summary>
 /// <param name="var">A linguistic variable to which the condition is related</param>
 /// <param name="term">A term in expression 'var is term'</param>
 /// <param name="not">Does condition contain 'not'</param>
 /// <returns>Generated condition</returns>
 public FuzzyCondition CreateCondition(FuzzyVariable var, FuzzyTerm term, bool not)
 {
     return(new FuzzyCondition(var, term, not));
 }
        private List<FuzzyVariable> GetInputVariables(FuzzyCheckDBContext context)
        {
            List<FuzzyVariable> variables = new List<FuzzyVariable>();
            foreach (var inputVariable in context.Parameters)
            {
                if (inputVariable.IsInput)
                {
                    FuzzyVariable fsVariable = new FuzzyVariable(
                        inputVariable.Name,
                        inputVariable.Min,
                        inputVariable.Max);
                    variables.Add(fsVariable);
                }
            }

            foreach (var variable in variables)
            {
                var dbVariable = context.Parameters.Single(p => p.Name == variable.Name);
                foreach (var term in dbVariable.Terms)
                {
                    variable.Terms.Add(new FuzzyTerm(term.Name, new NormalMembershipFunction(term.b, term.c)));
                }
            }

            return variables;
        }
        private double PreprocessInputValue(FuzzyVariable variable, double value)
        {
            if (value <= variable.Min)
            {
                return variable.Min;
            }

            if (value >= variable.Max)
            {
                return variable.Max;
            }

            return value;
        }
        private static MamdaniFuzzySystem UpdateParamsByMultiplier(
            DecisionMakerType1 originalDecisionMaker,
            double multiplier,
            VariableMFParam paramToUpdate,
            out bool canStretchMore)
        {
            canStretchMore = false;

            // Create empty fuzzy system
            MamdaniFuzzySystem newFs = new MamdaniFuzzySystem();

            // Create input and output variables for the system
            foreach (var inputVariable in originalDecisionMaker.fsWellEval.Input)
            {
                FuzzyVariable fsVariable = new FuzzyVariable(inputVariable.Name, inputVariable.Min, inputVariable.Max);
                foreach (var term in inputVariable.Terms)
                {
                    var mf = term.MembershipFunction as NormalMembershipFunction;
                    if (paramToUpdate == VariableMFParam.Mean)
                    {
                        if (mf != null && (mf.B * multiplier >= fsVariable.Min && mf.B * multiplier <= fsVariable.Max))
                        {
                            fsVariable.Terms.Add(
                                new FuzzyTerm(term.Name, new NormalMembershipFunction(mf.B * multiplier, mf.Sigma)));
                            canStretchMore = true;
                        }
                        else
                        {
                            if (mf != null)
                            {
                                fsVariable.Terms.Add(new FuzzyTerm(term.Name, new NormalMembershipFunction(mf.B, mf.Sigma)));
                                canStretchMore = false;
                            }
                        }
                    }

                    if (paramToUpdate == VariableMFParam.Deviation)
                    {
                        if (mf != null && (mf.Sigma * multiplier >= 0.001 && mf.Sigma * multiplier <= 10))
                        {
                            fsVariable.Terms.Add(
                                new FuzzyTerm(term.Name, new NormalMembershipFunction(mf.B, mf.Sigma * multiplier)));

                        }
                        else
                        {
                            if (mf != null)
                            {
                                fsVariable.Terms.Add(new FuzzyTerm(term.Name, new NormalMembershipFunction(mf.B, mf.Sigma)));

                            }
                        }
                    }

                    if (paramToUpdate == VariableMFParam.RuleWeight)
                    {
                        if (mf != null)
                        {
                            fsVariable.Terms.Add(new FuzzyTerm(term.Name, new NormalMembershipFunction(mf.B, mf.Sigma)));
                        }
                    }
                }

                newFs.Input.Add(fsVariable);
            }

            foreach (var outputVariable in originalDecisionMaker.fsWellEval.Output)
            {
                var newVariable = new FuzzyVariable(
                    outputVariable.Name,
                    outputVariable.Min,
                    outputVariable.Max,
                    outputVariable.Unit);
                newVariable.Terms.Clear();
                newVariable.Terms.AddRange(outputVariable.Terms);
                newFs.Output.Add(newVariable);
            }

            for (int i = 0; i < originalDecisionMaker.RulesDefinitions.Items.Count; i++)
            {
                MamdaniFuzzyRule newRule = newFs.ParseRule(originalDecisionMaker.RulesDefinitions.Items[i].Definition);
                double oldWeight = originalDecisionMaker.fsWellEval.Rules[i].Weight;
                if (paramToUpdate == VariableMFParam.RuleWeight)
                {
                    if (oldWeight * multiplier >= 0.001 && oldWeight * multiplier <= 1)
                    {
                        newRule.Weight = oldWeight * multiplier;
                    }
                }
                else
                {
                    newRule.Weight = oldWeight;
                }

                newFs.Rules.Add(newRule);
            }

            return newFs;
        }
        private static Bitmap GetTermImg(FuzzyVariable fv)
        {
            Bitmap bmp = new Bitmap(400, 200);
            if (fv != null)
            {
                RelationImage img = new RelationImage(fv);
                img.DrawImage(Graphics.FromImage(bmp));
            }

            return bmp;
        }
        private FuzzyVariable GetOutputVariable()
        {
            var parameters = (from item in db.Parameters where !item.IsInput select item).ToList();
            List<FuzzyVariable> variables = new List<FuzzyVariable>();
            foreach (var variable in parameters)
            {
                FuzzyVariable fsVariable = new FuzzyVariable(
                    variable.Name,
                    variable.Min,
                    variable.Max);
                foreach (var term in variable.Terms)
                {
                    fsVariable.Terms.Add(new FuzzyTerm(term.Name, new NormalMembershipFunction(term.b, term.c)));
                }

                return fsVariable;
            }

            return null;
        }
        private List<FuzzyVariable> GetInputVariables()
        {
            List<FuzzyVariable> variables = new List<FuzzyVariable>();
            foreach (var inputVariable in db.Parameters)
            {
                if (inputVariable.IsInput /* &&parameterValues.Any(pv => pv.ID == inputVariable.Id)*/)
                {
                    FuzzyVariable fsVariable = new FuzzyVariable(
                        inputVariable.Name,
                        inputVariable.Min,
                        inputVariable.Max);
                    variables.Add(fsVariable);
                }
            }

            foreach (var variable in variables)
            {
                var dbVariable = db.Parameters.Single(p => p.Name == variable.Name);
                foreach (var term in dbVariable.Terms)
                {
                    variable.Terms.Add(new FuzzyTerm(term.Name, new NormalMembershipFunction(term.b, term.c)));
                }
            }

            return variables;
        }
        string DefuzzifyToClosestTerm(FuzzyVariable outputVariable, IMembershipFunction compositeMF, double min, double max)
        {
            int k = 50;
            double step = (max - min) / k;

            double maxArea = 0;
            string matchingTerm = outputVariable.Terms[0].Name;

            foreach (var term in outputVariable.Terms)
            {
                double coverageArea = 0;

                double leftX = min;
                double rightX = leftX + step;

                while (rightX <= max)
                {
                    coverageArea += step * (Math.Min(term.MembershipFunction.GetValue(leftX), compositeMF.GetValue(leftX)));
                    leftX += step;
                    rightX += step;
                }

                if (coverageArea > maxArea)
                {
                    maxArea = coverageArea;
                    matchingTerm = term.Name;
                }
            }

            return matchingTerm;
        }
Beispiel #20
0
        public static Dictionary<NodeInstance, double> PrioritizeNodes(List<NodeInstance> nodes)
        {
            MamdaniFuzzySystem fsNodeSys = new MamdaniFuzzySystem();

            FuzzyVariable fvCPU = new FuzzyVariable("cpu", 0.0, 1);
            fvCPU.Terms.Add(new FuzzyTerm("low", new TriangularMembershipFunction(-.50, 0, .50)));
            fvCPU.Terms.Add(new FuzzyTerm("med", new TriangularMembershipFunction(0, .50, 1)));
            fvCPU.Terms.Add(new FuzzyTerm("high", new TriangularMembershipFunction(.50, 1, 1.5)));
            fsNodeSys.Input.Add(fvCPU);

            FuzzyVariable fvBandwidth = new FuzzyVariable("bandwidth", 0.0, 1);
            fvBandwidth.Terms.Add(new FuzzyTerm("low", new TriangularMembershipFunction(-.50, 0, .50)));
            fvBandwidth.Terms.Add(new FuzzyTerm("med", new TriangularMembershipFunction(0, .50, 1)));
            fvBandwidth.Terms.Add(new FuzzyTerm("high", new TriangularMembershipFunction(.50, 1, 1.5)));
            fsNodeSys.Input.Add(fvBandwidth);

            FuzzyVariable fvFreeSpace = new FuzzyVariable("freespace", 0.0, 1);
            fvFreeSpace.Terms.Add(new FuzzyTerm("low", new TriangularMembershipFunction(-.5, 0, .5)));
            fvFreeSpace.Terms.Add(new FuzzyTerm("moderate", new TriangularMembershipFunction(0, .5, 1)));
            fvFreeSpace.Terms.Add(new FuzzyTerm("ample", new TriangularMembershipFunction(.5, 1, 1.5)));
            fsNodeSys.Input.Add(fvFreeSpace);

            //
            // Create output variables for the system
            //
            FuzzyVariable fvRank = new FuzzyVariable("rank", 0, 1);
            fvRank.Terms.Add(new FuzzyTerm("low", new TriangularMembershipFunction(-0.25, 0, 0.25)));
            fvRank.Terms.Add(new FuzzyTerm("med_low", new TriangularMembershipFunction(0, 0.25, 0.50)));
            fvRank.Terms.Add(new FuzzyTerm("med", new TriangularMembershipFunction(0.25, 0.50, 0.75)));
            fvRank.Terms.Add(new FuzzyTerm("med_high", new TriangularMembershipFunction(0.50, 0.75, 1)));
            fvRank.Terms.Add(new FuzzyTerm("high", new TriangularMembershipFunction(0.75, 1, 1.25)));

            fsNodeSys.Output.Add(fvRank);

            MamdaniFuzzyRule rule1 = fsNodeSys.ParseRule("if (cpu is low) and (bandwidth is high) and (freespace is ample) then rank is med");
            MamdaniFuzzyRule rule2 = fsNodeSys.ParseRule("if (cpu is low) and (bandwidth is high) and (freespace is moderate) then rank is med_low");
            MamdaniFuzzyRule rule3 = fsNodeSys.ParseRule("if (cpu is low) and (bandwidth is high) and (freespace is low) then rank is low");
            MamdaniFuzzyRule rule4 = fsNodeSys.ParseRule("if (cpu is low) and (bandwidth is med) and (freespace is ample) then rank is med_high");
            MamdaniFuzzyRule rule5 = fsNodeSys.ParseRule("if (cpu is low) and (bandwidth is med) and (freespace is moderate) then rank is med_high");
            MamdaniFuzzyRule rule6 = fsNodeSys.ParseRule("if (cpu is low) and (bandwidth is med) and (freespace is low) then rank is med_low");
            MamdaniFuzzyRule rule7 = fsNodeSys.ParseRule("if (cpu is low) and (bandwidth is low) and (freespace is ample) then rank is high");
            MamdaniFuzzyRule rule8 = fsNodeSys.ParseRule("if (cpu is low) and (bandwidth is low) and (freespace is moderate) then rank is med_high");
            MamdaniFuzzyRule rule9 = fsNodeSys.ParseRule("if (cpu is low) and (bandwidth is low) and (freespace is low) then rank is med_low");
            MamdaniFuzzyRule rule10 = fsNodeSys.ParseRule("if (cpu is med) and (bandwidth is high) and (freespace is ample) then rank is med");
            MamdaniFuzzyRule rule11 = fsNodeSys.ParseRule("if (cpu is med) and (bandwidth is high) and (freespace is moderate) then rank is med_low");
            MamdaniFuzzyRule rule12 = fsNodeSys.ParseRule("if (cpu is med) and (bandwidth is high) and (freespace is low) then rank is med_low");
            MamdaniFuzzyRule rule13 = fsNodeSys.ParseRule("if (cpu is med) and (bandwidth is med) and (freespace is ample) then rank is med");
            MamdaniFuzzyRule rule14 = fsNodeSys.ParseRule("if (cpu is med) and (bandwidth is med) and (freespace is moderate) then rank is med");
            MamdaniFuzzyRule rule15 = fsNodeSys.ParseRule("if (cpu is med) and (bandwidth is med) and (freespace is low) then rank is low");
            MamdaniFuzzyRule rule16 = fsNodeSys.ParseRule("if (cpu is med) and (bandwidth is low) and (freespace is ample) then rank is med_high");
            MamdaniFuzzyRule rule17 = fsNodeSys.ParseRule("if (cpu is med) and (bandwidth is low) and (freespace is moderate) then rank is med_high");
            MamdaniFuzzyRule rule18 = fsNodeSys.ParseRule("if (cpu is med) and (bandwidth is low) and (freespace is low) then rank is low");
            MamdaniFuzzyRule rule19 = fsNodeSys.ParseRule("if (cpu is high) and (bandwidth is high) and (freespace is ample) then rank is med");
            MamdaniFuzzyRule rule20 = fsNodeSys.ParseRule("if (cpu is high) and (bandwidth is high) and (freespace is moderate) then rank is med_low");
            MamdaniFuzzyRule rule21 = fsNodeSys.ParseRule("if (cpu is high) and (bandwidth is high) and (freespace is low) then rank is low");
            MamdaniFuzzyRule rule22 = fsNodeSys.ParseRule("if (cpu is high) and (bandwidth is med) and (freespace is ample) then rank is med");
            MamdaniFuzzyRule rule23 = fsNodeSys.ParseRule("if (cpu is high) and (bandwidth is med) and (freespace is moderate) then rank is med_low");
            MamdaniFuzzyRule rule24 = fsNodeSys.ParseRule("if (cpu is high) and (bandwidth is med) and (freespace is low) then rank is low");
            MamdaniFuzzyRule rule25 = fsNodeSys.ParseRule("if (cpu is high) and (bandwidth is low) and (freespace is ample) then rank is med_low");
            MamdaniFuzzyRule rule26 = fsNodeSys.ParseRule("if (cpu is high) and (bandwidth is low) and (freespace is moderate) then rank is med");
            MamdaniFuzzyRule rule27 = fsNodeSys.ParseRule("if (cpu is high) and (bandwidth is low) and (freespace is low) then rank is low");

            fsNodeSys.Rules.Add(rule1);
            fsNodeSys.Rules.Add(rule2);
            fsNodeSys.Rules.Add(rule3);
            fsNodeSys.Rules.Add(rule4);
            fsNodeSys.Rules.Add(rule5);
            fsNodeSys.Rules.Add(rule6);
            fsNodeSys.Rules.Add(rule7);
            fsNodeSys.Rules.Add(rule8);
            fsNodeSys.Rules.Add(rule9);
            fsNodeSys.Rules.Add(rule10);
            fsNodeSys.Rules.Add(rule11);
            fsNodeSys.Rules.Add(rule12);
            fsNodeSys.Rules.Add(rule13);
            fsNodeSys.Rules.Add(rule14);
            fsNodeSys.Rules.Add(rule15);
            fsNodeSys.Rules.Add(rule16);
            fsNodeSys.Rules.Add(rule17);
            fsNodeSys.Rules.Add(rule18);
            fsNodeSys.Rules.Add(rule19);
            fsNodeSys.Rules.Add(rule20);
            fsNodeSys.Rules.Add(rule21);
            fsNodeSys.Rules.Add(rule22);
            fsNodeSys.Rules.Add(rule23);
            fsNodeSys.Rules.Add(rule24);
            fsNodeSys.Rules.Add(rule25);
            fsNodeSys.Rules.Add(rule26);
            fsNodeSys.Rules.Add(rule27);

            var rankedNodes = new Dictionary<NodeInstance, double>();

            for (int i = 0; i < nodes.Count; i++)
            {
                //
                // Fuzzify input values
                //
                Dictionary<FuzzyVariable, double> inputValues = new Dictionary<FuzzyVariable, double>();
                inputValues.Add(fvCPU, nodes[i].CPU_Utilization);
                inputValues.Add(fvBandwidth, nodes[i].UsedBandwidth / nodes[i].MaxBandwidth);
                inputValues.Add(fvFreeSpace, nodes[i].FreeSpace / nodes[i].MaxBackupSpace);

                //
                // Calculate the result
                //
                Dictionary<FuzzyVariable, double> result = fsNodeSys.Calculate(inputValues);

                double rank = result[fvRank];
                rankedNodes.Add(nodes[i], rank);

                //Console.WriteLine(nodes[i].ToString());
                //Console.WriteLine("Rank: " + Math.Round(rank, 2).ToString());
                //Console.WriteLine();
            }
            var sortedNodes
                = (from entry in rankedNodes orderby entry.Value descending select entry)
                .ToDictionary(pair => pair.Key, pair => pair.Value);
            return sortedNodes;
        }
Beispiel #21
0
 /// <summary>
 /// Creates new instance of image just to display a single-dimensional fuzzy set without any values specified.
 /// </summary>
 /// <param name="singleDimensionalSet">Single-dimensional set to display</param>
 public RelationImage(FuzzyVariable singleDimensionalSet)
 {
     _variable = singleDimensionalSet;
     _fullySpecified = false;
     _specifiedMembership = null;
 }
        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;
                }
            }
        }