/// <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); } }
// prepare rule manager private FuzzyRuleManager PrepareRuleManager() { FuzzyRuleManager mgr = new FuzzyRuleManager(); mgr.Antecedents.Add(PrepareSpeed()); mgr.Antecedents.Add(PrepareDistance()); mgr.Consequent = PrepareMetadata(); PrepareRules(mgr); return(mgr); }
// prepare fuzzy logic rules using code private void PrepareRules(FuzzyRuleManager mgr) { mgr.AddNewRule("Distance.VeryFar", "none", "Metadata.Few"); mgr.AddNewRule("Distance.Far", "Speed.Fast", "Metadata.Few"); mgr.AddNewRule("Distance.Far", "Speed.Medium", "Metadata.Few"); mgr.AddNewRule("Distance.Far", "Speed.Slow", "Metadata.Medium"); mgr.AddNewRule("Distance.Far", "Speed.VerySlow", "Metadata.Medium"); mgr.AddNewRule("none", "Speed.VeryFast", "Metadata.Few"); mgr.AddNewRule("Distance.Medium", "Speed.Medium", "Metadata.Few"); mgr.AddNewRule("Distance.Medium", "Speed.Slow", "Metadata.Medium"); mgr.AddNewRule("Distance.Medium", "Speed.VerySlow", "Metadata.Medium"); mgr.AddNewRule("Distance.Close", "Speed.Fast", "Metadata.Medium"); mgr.AddNewRule("Distance.Close", "Speed.Medium", "Metadata.Medium"); mgr.AddNewRule("Distance.Close", "Speed.Slow", "Metadata.Medium"); mgr.AddNewRule("Distance.Close", "Speed.VerySlow", "Metadata.All"); mgr.AddNewRule("Distance.VeryClose", "Speed.Fast", "Metadata.Medium"); mgr.AddNewRule("Distance.VeryClose", "Speed.Medium", "Metadata.All"); mgr.AddNewRule("Distance.VeryClose", "Speed.Slow", "Metadata.All"); mgr.AddNewRule("Distance.VeryClose", "Speed.VerySlow", "Metadata.All"); }
/// <summary> /// Creates fuzzy logic from code /// </summary> private void CreateFuzzyLogic() { mngr = PrepareRuleManager(); LogManager.Instance.AddLog("Create Fuzzy Logic from code!"); }