예제 #1
0
    /// <summary>
    /// This method reads all Fuzzy Logic configuration from SQLite database, and applies
    /// this logic to all relevant fuzzy classes
    /// </summary>
    private void CreateFuzzyLogicFromSQLiteDB()
    {
        try
        {
            DataService db         = new DataService("fuzzy.db");
            var         FuzzParts  = db.GetFuzzyParts();
            var         FuzzValues = db.GetFuzzyValues();
            var         FuzzRules  = db.GetFuzzyRules();
            // all read so try to create everything from data
            FuzzyRuleManager mgr = new FuzzyRuleManager();
            foreach (FuzzyParts p in FuzzParts)
            {
                FuzzyVariable vv = new FuzzyVariable()
                {
                    Name = p.Name
                };
                if (string.Compare(p.FuzzyType, "Antecedent", false) == 0)
                {
                    mgr.Antecedents.Add(vv);
                }
                else
                {
                    mgr.Consequent = vv;
                }
                foreach (FuzzyValues val in FuzzValues)
                {
                    if (string.Compare(val.SetName, p.Name, true) == 0)
                    {
                        FuzzyValue fval = new FuzzyValue()
                        {
                            Name = val.Name, Parent = vv
                        };
                        double[] MFValues = ParseValues(val.MemberValues);
                        if (string.Compare(val.MemberType, "trapmf", true) == 0) // trapezoid MF?
                        {
                            fval.MF = new TrapezoidMemberShip(MFValues[0], MFValues[1], MFValues[2], MFValues[3]);
                        }
                        else
                        {
                            fval.MF = new TriangleMembership(MFValues[0], MFValues[1], MFValues[2]);
                        }

                        vv.AddValue(fval);
                    }
                }
            }
            foreach (FuzzyRules r in FuzzRules)
            {
                mgr.AddNewStringRule(r.Rule);
            }
            // apply rule manager to member variable
            mngr           = mgr;
            ReadFromSQLite = true;
            LogManager.Instance.AddLog("Success Create Fuzzy Logic from SQLite DB");
        }
        catch (Exception ex)
        {
            Debug.LogError(ex.Message);
        }
    }
예제 #2
0
    // prepare Metadata condequent for Fuzzy Logic with code
    private FuzzyVariable PrepareMetadata()
    {
        FuzzyVariable Metadata = new FuzzyVariable()
        {
            Name = "Metadata"
        };

        FuzzyValue Few = new FuzzyValue()
        {
            Name = "Few"
        };
        FuzzyValue Medium = new FuzzyValue()
        {
            Name = "Medium"
        };
        FuzzyValue All = new FuzzyValue()
        {
            Name = "All"
        };

        Few.MF    = new TrapezoidMemberShip(0, 0, 10, 30);
        Medium.MF = new TrapezoidMemberShip(0, 30, 60, 90);
        All.MF    = new TrapezoidMemberShip(60, 90, 100, 100);

        Metadata.AddValue(Few);
        Metadata.AddValue(Medium);
        Metadata.AddValue(All);

        return(Metadata);
    }
예제 #3
0
        public bool Equals(FuzzyValue <T> other)
        {
            int thisInt  = EnumInt32ToInt.Convert(this.linguisticVariable);
            int otherInt = EnumInt32ToInt.Convert(other.linguisticVariable);

            return(thisInt == otherInt && this.membershipDegree == other.membershipDegree);
        }
예제 #4
0
            public LinguisticValue Infer()
            {
                List <LinguisticValue> rulesResults = new List <LinguisticValue>();

                foreach (FuzzyRule rule in rules)
                {
                    rulesResults.Add(rule.Infer(calc, context));
                }

                Dictionary <FuzzySet, List <FuzzyValue> > valuesBySet = new Dictionary <FuzzySet, List <FuzzyValue> >();

                foreach (LinguisticValue ruleResult in rulesResults)
                {
                    foreach (KeyValuePair <FuzzySet, FuzzyValue> kv in ruleResult.GetSets())
                    {
                        if (!valuesBySet.ContainsKey(kv.Key))
                        {
                            valuesBySet[kv.Key] = new List <FuzzyValue>();
                        }
                        valuesBySet[kv.Key].Add(kv.Value);
                    }
                }

                LinguisticValue result = new LinguisticValue();

                foreach (KeyValuePair <FuzzySet, List <FuzzyValue> > kv in valuesBySet)
                {
                    FuzzyValue totalMembership = calc.or(kv.Value.ToArray());
                    result.AddSet(kv.Key, totalMembership);
                }
                return(result);
            }
예제 #5
0
        private void OnAddValue(MouseEventArgs e, FuzzyVariable variable)
        {
            Random     rnd = new Random();
            FuzzyValue fv  = new FuzzyValue(variable, $"Value-{rnd.Next():X08}",
                                            $"Description-{rnd.Next():X08}");

            variable.AddValue(fv);
        }
예제 #6
0
        private void OnDuplicateValue(MouseEventArgs e, FuzzyVariable variable, FuzzyValue value)
        {
            FuzzyValue copy = value.Clone();

            //copy.Name += " (kopia)";
            //copy.Description += " (kopia)";

            variable.AddValue(copy);
        }
예제 #7
0
        private async Task OnRemoveValue(MouseEventArgs e, FuzzyValue value)
        {
            if (!await MessageBox.OkCancel(this.Modal, "Pytanie", $"Czy chcesz <b>usunąć</b> wartość lingwistyczną {value.Name} ze zmiennej {value.Variable.Name}?"))
            {
                return;
            }

            //ModelProvider.Data.Model.RemoveValue(value);
            value.Variable.RemoveValue(value);
        }
예제 #8
0
        public void FuzzyNot_01()
        {
            FuzzyValue <Temperature> val01 = new FuzzyValue <Temperature>(Temperature.Cold, 0.8f);
            FuzzyValue <Temperature> val02 = new FuzzyValue <Temperature>(Temperature.Warm, 0.25f);
            FuzzyValueSet            set   = new FuzzyValueSet();

            set.Set(val01);
            set.Set(val02);
            FuzzyNot exp    = new FuzzyNot(new FuzzyVariableExpression <Temperature>(Temperature.Cold));
            var      result = exp.Evaluate(set);

            Assert.AreEqual(0.2f, result, 0.000005);
        }
예제 #9
0
        private async Task OnEditValueDescription(MouseEventArgs e, FuzzyValue value)
        {
            var in_data = new FuzzyValueDescriptionData(value.Name, value.Description);

            (bool result, FuzzyValueDescriptionData out_data) = await Modals.FuzzyEditors.FuzzyValueDescriptionEditor(this.Modal, in_data);

            if (result)
            {
                value.Description = out_data.Description;
                value.Name        = out_data.Name;
                value.Variable.ChartHolder.UpdateChart();
            }
        }
예제 #10
0
        public void TestFuzzySet_Simple()
        {
            FuzzyValueSet            set   = new FuzzyValueSet();
            FuzzyValue <Temperature> value = new FuzzyValue <Temperature>()
            {
                linguisticVariable = Temperature.Hot, membershipDegree = 0.5f
            };

            set.Set(value);
            var result = set.Get(Temperature.Hot);

            Assert.AreEqual(Temperature.Hot, result.linguisticVariable);
            Assert.AreEqual(0.5f, result.membershipDegree);
        }
예제 #11
0
        public override bool Equals(object obj)
        {
            System.Type otherType = obj.GetType();
            if (!otherType.IsValueType && obj == null)
            {
                return(false);
            }
            if (otherType != typeof(FuzzyValue <T>))
            {
                return(false);
            }
            FuzzyValue <T> other = (FuzzyValue <T>)obj;

            return(this.Equals(other));
        }
예제 #12
0
            public LinguisticValue Infer(FuzzyCalculator calc, FuzzyContext context)
            {
                List <FuzzyValue> inputValues = new List <FuzzyValue>(inputVariables.Count);

                foreach (FuzzySet set in inputVariables)
                {
                    inputValues.Add(context.fuzzyInputValues[set]);
                }
                FuzzyValue membership = calc.and(inputValues.ToArray());

                LinguisticValue result = new LinguisticValue();

                foreach (FuzzySet set in outputVariables)
                {
                    result.AddSet(set, membership);
                }
                return(result);
            }
예제 #13
0
        public void TestFuzzySet_DiffEnum_A()
        {
            FuzzyValueSet            set     = new FuzzyValueSet();
            FuzzyValue <Temperature> value01 = new FuzzyValue <Temperature>()
            {
                linguisticVariable = Temperature.Hot, membershipDegree = 0.5f
            };
            FuzzyValue <Command> value02 = new FuzzyValue <Command>()
            {
                linguisticVariable = Command.Heat, membershipDegree = 1f
            };

            set.Set(value02);
            set.Set(value01);
            var result = set.Get(Temperature.Hot);

            Assert.AreEqual(Temperature.Hot, result.linguisticVariable);
            Assert.AreEqual(0.5f, result.membershipDegree);
        }
예제 #14
0
    // prepare Distance antecedent for Fuzzy logic using code
    private FuzzyVariable PrepareDistance()
    {
        FuzzyVariable Distance = new FuzzyVariable()
        {
            Name = "Distance"
        };

        FuzzyValue VeryClose = new FuzzyValue()
        {
            Name = "VeryClose"
        };
        FuzzyValue Close = new FuzzyValue()
        {
            Name = "Close"
        };
        FuzzyValue Medium = new FuzzyValue()
        {
            Name = "Medium"
        };
        FuzzyValue Far = new FuzzyValue()
        {
            Name = "Far"
        };
        FuzzyValue VeryFar = new FuzzyValue()
        {
            Name = "VeryFar"
        };

        VeryClose.MF = new TrapezoidMemberShip(0, 0, 50, 300);
        Close.MF     = new TriangleMembership(0, 300, 1000);
        Medium.MF    = new TriangleMembership(300, 1000, 2000);
        Far.MF       = new TriangleMembership(1000, 2000, 3000);
        VeryFar.MF   = new TrapezoidMemberShip(2000, 3000, 4000, 4000);

        Distance.AddValue(VeryClose);
        Distance.AddValue(Close);
        Distance.AddValue(Medium);
        Distance.AddValue(Far);
        Distance.AddValue(VeryFar);
        return(Distance);
    }
예제 #15
0
    // prepare speed antecedent for Fuzzy logic using code
    private FuzzyVariable PrepareSpeed()
    {
        FuzzyVariable speed = new FuzzyVariable()
        {
            Name = "Speed"
        };
        FuzzyValue VerySlow = new FuzzyValue()
        {
            Name = "VerySlow"
        };
        FuzzyValue Slow = new FuzzyValue()
        {
            Name = "Slow"
        };
        FuzzyValue Medium = new FuzzyValue()
        {
            Name = "Medium"
        };
        FuzzyValue Fast = new FuzzyValue()
        {
            Name = "Fast"
        };
        FuzzyValue VeryFast = new FuzzyValue()
        {
            Name = "VeryFast"
        };

        VerySlow.MF = new TrapezoidMemberShip(0, 0, 5, 15);
        Slow.MF     = new TriangleMembership(5, 20, 30);
        Medium.MF   = new TriangleMembership(20, 30, 50);
        Fast.MF     = new TrapezoidMemberShip(30, 60, 80, 100);
        VeryFast.MF = new TrapezoidMemberShip(80, 100, 120, 120);
        speed.AddValue(VerySlow);
        speed.AddValue(Slow);
        speed.AddValue(Medium);
        speed.AddValue(Fast);
        speed.AddValue(VeryFast);
        return(speed);
    }
예제 #16
0
        private void ParseCurrentSet()
        {
            int    index = _fuzzySets.Count;
            string title = "Unknown";

            CalcType selectedType = CalcType.Unknown;

            switch (comboClass.SelectedIndex)
            {
            case 0:
                selectedType = CalcType.Singleton;
                title        = "Singleton";
                break;

            case 1:
                selectedType = CalcType.Gamma;
                title        = "Gamma";
                break;

            case 2:
                selectedType = CalcType.L;
                title        = "L";
                break;

            case 3:
                selectedType = CalcType.T;
                title        = "T";
                break;
            }

            if (!this.VerifyForm(selectedType))
            {
                return;
            }

            FuzzyValue value_a = this.ParseFuzzyValue(textInputOne.Text);
            FuzzyValue value_b = this.ParseFuzzyValue(textInputTwo.Text);
            FuzzyValue value_c = this.ParseFuzzyValue(textInputThree.Text);

            if (!this.VerifyCorrectness(selectedType, value_a, value_b, value_c))
            {
                return;
            }

            this.ShowPopup("Addition", "A new fuzzy set is just being added.");
            this._fuzzySets.Add(new FuzzySet {
                Type = selectedType,
                a    = value_a,
                b    = value_b,
                c    = value_c
            });
            this.DrawSet(title + " #" + _fuzzySets.Count, this._fuzzySets[this._fuzzySets.Count - 1].GetPlot());

            textInputOne.Text = textInputTwo.Text = textInputThree.Text = string.Empty;

#if DEBUG
            System.Diagnostics.Debug.WriteLine("FuzzySet #" + index + ", type is = " + this._fuzzySets[index].Type.ToString());
            System.Diagnostics.Debug.WriteLine("FuzzySet #" + index + " value a is x = " + this._fuzzySets[index].a.x.ToString());
            System.Diagnostics.Debug.WriteLine("FuzzySet #" + index + " value a is value = " + this._fuzzySets[index].a.value.ToString());
            System.Diagnostics.Debug.WriteLine("FuzzySet #" + index + " value b is x = " + this._fuzzySets[index].b.x.ToString());
            System.Diagnostics.Debug.WriteLine("FuzzySet #" + index + " value b is value = " + this._fuzzySets[index].b.value.ToString());
            System.Diagnostics.Debug.WriteLine("FuzzySet #" + index + " value c is x = " + this._fuzzySets[index].c.x.ToString());
            System.Diagnostics.Debug.WriteLine("FuzzySet #" + index + " value c is value = " + this._fuzzySets[index].c.value.ToString());
#endif
        }
예제 #17
0
        private bool VerifyCorrectness(CalcType selectedType, FuzzyValue a, FuzzyValue b, FuzzyValue c)
        {
            //Value must exist
            if (selectedType == CalcType.Singleton && a.x == null)
            {
                this.ShowPopup("It won't work!", "The specified (x) value is not valid.");
                return(false);
            }
            else if ((selectedType == CalcType.Gamma || selectedType == CalcType.T || selectedType == CalcType.L) && (a.x == null || a.value == null))
            {
                this.ShowPopup("It won't work!", "The specified (x,a) value is not valid.");
                return(false);
            }
            else if ((selectedType == CalcType.Gamma || selectedType == CalcType.T || selectedType == CalcType.L) && (b.x == null || b.value == null))
            {
                this.ShowPopup("It won't work!", "The specified (x,b) value is not valid.");
                return(false);
            }
            else if (selectedType == CalcType.T && (c.x == null || c.value == null))
            {
                this.ShowPopup("It won't work!", "The specified (x,c) value is not valid.");
                return(false);
            }

            switch (selectedType)
            {
            case CalcType.Gamma:
                //a < b
                //x1 < x2
                if (a.value > b.value)
                {
                    this.ShowPopup("It won't work!", "The value of 'a' cannot be greater than the value of 'b'");
                    return(false);
                }
                else if (a.x > b.x)
                {
                    this.ShowPopup("It won't work!", "The value of x in (x,a) cannot be greater than the value of x in (x,b)");
                    return(false);
                }
                break;

            case CalcType.L:
                // a > b
                // x1 > x2
                if (a.value > b.value)
                {
                    this.ShowPopup("It won't work!", "The value of 'a' cannot be greater than the value of 'b'");
                    return(false);
                }
                else if (a.x < b.x)
                {
                    this.ShowPopup("It won't work!", "The value of x in (x,a) must be greater than the value of x in (x,b)");
                    return(false);
                }
                break;

            case CalcType.T:
                //a < b < c
                //x1 < x2 > x2
                if (a.value > b.value)
                {
                    this.ShowPopup("It won't work!", "The value of 'a' cannot be greater than the value of 'b'");
                    return(false);
                }
                else if (b.value > c.value)
                {
                    this.ShowPopup("It won't work!", "The value of 'b' cannot be greater than the value of 'c'");
                }
                else if (a.x > b.x)
                {
                    this.ShowPopup("It won't work!", "The value of x in (x,a) cannot be greater than the value of x in (x,b)");
                    return(false);
                }
                else if (b.x < c.x)
                {
                    this.ShowPopup("It won't work!", "The value of x in (x,b) cannot be greater than the value of x in (x,c)");
                    return(false);
                }
                break;
            }

            //X must be between
            if ((selectedType == CalcType.Gamma || selectedType == CalcType.L || selectedType == CalcType.T) && (a.x < 0 || a.x > 1))
            {
                this.ShowPopup("It won't work!", "The value of x in (x,a) must be between 0-1");
                return(false);
            }
            else if ((selectedType == CalcType.Gamma || selectedType == CalcType.L || selectedType == CalcType.T) && (b.x < 0 || b.x > 1))
            {
                this.ShowPopup("It won't work!", "The value of x in (x,b) must be between 0-1");
                return(false);
            }
            else if (selectedType == CalcType.T && (c.x < 0 || c.x > 1))
            {
                this.ShowPopup("It won't work!", "The value of x in (x,c) must be between 0-1");
                return(false);
            }

            return(true);
        }
예제 #18
0
 static FuzzyValue FuzzyOr(FuzzyValue v, FuzzyValue v2)
 {
     return(() => {
         return Mathf.Max(v(), v2());
     });
 }
예제 #19
0
 public int AddFuzzyValue(FuzzyValue value)
 {
     values.Add(value);
     vs.Add(0);
     return(values.Count - 1);
 }
 public void AddSet(FuzzySet set, FuzzyValue membership)
 {
     sets[set] = membership;
 }
예제 #21
0
 static FuzzyValue FuzzyAnd(FuzzyValue v, FuzzyValue v2)
 {
     return(() => {
         return Mathf.Min(v(), v2());
     });
 }
예제 #22
0
 private void AssertFuzzyValue <T>(T linguisticVar, float memebershipDegree, FuzzyValue <T> fuzzyValue) where T : struct, IConvertible
 {
     Assert.AreEqual(linguisticVar, fuzzyValue.linguisticVariable);
     Assert.AreEqual(memebershipDegree, fuzzyValue.membershipDegree, 0.005f);
 }
예제 #23
0
 static FuzzyValue FuzzyNot(FuzzyValue v)
 {
     return(() => {
         return 1.0f - v();
     });
 }