private void Sync(FuzzyType sender, FuzzyType recipient) { // todo. possible refinement of type inference engine // here we might also match constraints to crash early or // mix them to get more hints to aid us in type inference // // note that constraints cant be implemented in fuzzinesslevel-style, since // even completely Unknown type can define constraints useful for a Crisp type // the best way would be simply synchronize contents of the Constraints collection // regardless of fuzziness level of sender and recipient if (sender.FuzzinessLevel < recipient.FuzzinessLevel) { recipient.RuntimeType = sender.RuntimeType; } else if (sender.FuzzinessLevel > recipient.FuzzinessLevel) { // sender has nothing to say to the recipient } else if (sender.IsCrisp() && recipient.IsCrisp()) { if (!sender.SameBasisType(recipient)) { ReportContradiction(String.Format( "Sender '{0}' and recipient '{1}' have different basis types", sender, recipient)); } else { var senderArgs = sender.GenericArgs; var recipientArgs = recipient.GenericArgs; if (senderArgs.Count != recipientArgs.Count) { throw new ArgumentException(String.Format("This can't be: " + "Sender '{0}' and recipient '{1}' claimed that they share basis type", sender, recipient)); } else { for (var i = 0; i < senderArgs.Count; ++i) { Sync(senderArgs[i], recipientArgs[i]); } } } } }
// Enum-string conversions public static string FuzzyTypeToString(FuzzyType fuzzyType) { switch (fuzzyType) { case FuzzyType.Mamdani: return("Mamdani"); case FuzzyType.Larsen: return("Larsen"); case FuzzyType.Sugeno: return("Sugeno"); case FuzzyType.Sparse: return("Sparse"); default: return("Error: Invalid input"); } }
public Fuzzy(string field, object value, FuzzyType type = FuzzyType.Right) : base(field) { Value = value; Type = type; }
public static TypeLink IsIdenticalTo(this FuzzyType first, FuzzyType second) { return new IdentityLink(first, second); }
public static TypeLink BindToOneOfAlternatives(this FuzzyMethodBinding binding, FuzzyType bindingTypePoint, FuzzyType alternativeTypePoint) { return new MethodBindingToOneOfAlternativesLink(binding, bindingTypePoint, alternativeTypePoint); }
public static TypeLink IsFieldOf(this FuzzyType first, FuzzyType second, String fieldName) { return first.IsIdenticalTo(second.GetFieldOrProperty(fieldName)); }
/// <summary> /// Returns a new FuzzySystem of the appropriate sub-type. /// </summary> /// <param name="FileName"></param> /// <returns></returns> public static FuzzySystem GetFuzzySystemFromFile(string FileName) { // Open the file StreamReader file; string CurrentLine; try { file = new StreamReader(FileName); } catch (FileNotFoundException exc) { throw exc; } FuzzySystem fuzzy = null; // Find the type and create the fuzzy system accordingly while ((CurrentLine = file.ReadLine()) != null) { if (CurrentLine.StartsWith("Type=")) { FuzzyType type = FuzzySystem.StringToFuzzyType(CurrentLine.Replace("Type=", "").Replace("'", "")); switch (type) { case FuzzyType.Mamdani: fuzzy = new MamdaniFS(); break; case FuzzyType.Larsen: fuzzy = new LarsenFS(); break; case FuzzyType.Sugeno: fuzzy = new SugenoFS(); break; case FuzzyType.Sparse: default: throw new NotImplementedException(); } file = new StreamReader(FileName); break; } } bool FoundSystem = false; bool FoundIn = false; bool FoundOut = false; bool FoundRules = false; while ((CurrentLine = file.ReadLine()) != null) { // When we reach the [System] tag, we read the system setting from the following lines until we hit an empty line or the end of the file if (CurrentLine == "[System]") { FoundSystem = true; while ((CurrentLine = file.ReadLine()) != null && CurrentLine != "") { if (CurrentLine.StartsWith("Name=")) { fuzzy.Name = CurrentLine.Replace("Name=", "").Replace("'", ""); } //else if (CurrentLine.StartsWith("Type=")) //fuzzy.Type = StringToFuzzyType(CurrentLine.Replace("Type=", "").Replace("'", "")); else if (CurrentLine.StartsWith("Version=")) { fuzzy.Version = CurrentLine.Replace("Version=", ""); } else if (CurrentLine.StartsWith("NumInputs=")) { fuzzy.NumInputs = int.Parse(CurrentLine.Replace("NumInputs=", "")); } else if (CurrentLine.StartsWith("NumOutputs=")) { fuzzy.NumOutputs = int.Parse(CurrentLine.Replace("NumOutputs=", "")); } else if (CurrentLine.StartsWith("NumRules=")) { fuzzy.NumRules = int.Parse(CurrentLine.Replace("NumRules=", "")); } // Method types else if (CurrentLine.StartsWith("AndMethod=")) { fuzzy.AndMethod = FuzzySystem.StringToAndMethod(CurrentLine.Split('\'')[1]); } else if (CurrentLine.StartsWith("OrMethod=")) { fuzzy.OrMethod = FuzzySystem.StringToOrMethod(CurrentLine.Split('\'')[1]); } else if (CurrentLine.StartsWith("ImpMethod=")) { fuzzy.ImpMethod = FuzzySystem.StringToImpMethod(CurrentLine.Split('\'')[1]); } else if (CurrentLine.StartsWith("AggMethod=")) { fuzzy.AggMethod = FuzzySystem.StringToAggMethod(CurrentLine.Split('\'')[1]); } else if (CurrentLine.StartsWith("DefuzzMethod=")) { fuzzy.DefuzzMethod = FuzzySystem.StringToDefuzzMethod(CurrentLine.Split('\'')[1]); } } } // When we reach an [Input_] tag, we read the setting from the following lines until we hit an empty line or the end of the file if (CurrentLine.StartsWith("[Input") && CurrentLine.EndsWith("]")) { FoundIn = true; int index = int.Parse(CurrentLine.Replace("[Input", "").Replace("]", "")) - 1; fuzzy.Inputs[index] = new InputOutput { IsInput = true, Index = index + 1 }; while ((CurrentLine = file.ReadLine()) != null && CurrentLine != "") { if (CurrentLine.StartsWith("Name=")) { fuzzy.Inputs[index].Name = CurrentLine.Replace("Name=", "").Replace("'", ""); } else if (CurrentLine.StartsWith("Range=")) { fuzzy.Inputs[index].Range[0] = float.Parse(CurrentLine.Replace("Range=[", "").Split(' ')[0], System.Globalization.CultureInfo.InvariantCulture); fuzzy.Inputs[index].Range[1] = float.Parse(CurrentLine.Replace("]", "").Split(' ')[1], System.Globalization.CultureInfo.InvariantCulture); } else if (CurrentLine.StartsWith("NumMFs=")) { fuzzy.Inputs[index].NumMFs = int.Parse(CurrentLine.Replace("NumMFs=", "")); } else if (CurrentLine.StartsWith("MF")) { try { fuzzy.Inputs[index].MFs[int.Parse(CurrentLine.Split('=')[0].Remove(0, 2)) - 1] = new MembershipFunction(CurrentLine.Split('=')[1]); } catch (Exception exc) { throw exc; } } } } // When we reach an [Output_] tag, we read the setting from the following lines until we hit an empty line or the end of the file if (CurrentLine.StartsWith("[Output") && CurrentLine.EndsWith("]")) { FoundOut = true; int index = int.Parse(CurrentLine.Replace("[Output", "").Replace("]", "")) - 1; fuzzy.Outputs[index] = new InputOutput { IsInput = false, Index = index + 1 }; while ((CurrentLine = file.ReadLine()) != null && CurrentLine != "") { if (CurrentLine.StartsWith("Name=")) { fuzzy.Outputs[index].Name = CurrentLine.Replace("Name=", "").Replace("'", ""); } else if (CurrentLine.StartsWith("Range=")) { fuzzy.Outputs[index].Range[0] = float.Parse(CurrentLine.Replace("Range=[", "").Split(' ')[0], System.Globalization.CultureInfo.InvariantCulture); fuzzy.Outputs[index].Range[1] = float.Parse(CurrentLine.Replace("]", "").Split(' ')[1], System.Globalization.CultureInfo.InvariantCulture); } else if (CurrentLine.StartsWith("NumMFs=")) { fuzzy.Outputs[index].NumMFs = int.Parse(CurrentLine.Replace("NumMFs=", "")); } else if (CurrentLine.StartsWith("MF")) { fuzzy.Outputs[index].MFs[int.Parse(CurrentLine.Split('=')[0].Remove(0, 2)) - 1] = new MembershipFunction(CurrentLine.Split('=')[1]); } } } // When we reach the [Rules] tag, we read the setting from the following lines until we hit an empty line or the end of the file if (CurrentLine.StartsWith("[Rules") && CurrentLine.EndsWith("]")) { FoundRules = true; int index = 0; while ((CurrentLine = file.ReadLine()) != null && CurrentLine != "") { fuzzy.Rules[index] = new Rule(CurrentLine); index++; } } } // Check if we found all relevant data if (!(FoundSystem && FoundIn && FoundOut && FoundRules)) { string missing = "Could not find the following tags:"; if (!FoundSystem) { missing += " [System]"; } if (!FoundIn) { missing += " [Input]"; } if (!FoundSystem) { missing += " [Output]"; } if (!FoundSystem) { missing += " [Rules]"; } throw new Exception(missing); } if (fuzzy != null) { return(fuzzy); } throw new Exception("Couldn't read fuzzy system."); }
public IdentityLink(FuzzyType first, FuzzyType second) : base(first, second) { }