III. Cài đặt ứng dụng minh họa
1. Cài đặt chương trình mình họa bằng C#
• Định nghĩa các giá trị của biến ngôn ngữ
public enum LinguisticVariable
{ None = 0, //Temperature Freezing = 1, Cool = 2, Warm = 3, Hot = 4, //Cover Sunny = 5, Partly = 6, Overcast = 7, //Speed Slow = 8, Fast = 9 }
• Thiết kế Class FuzzyNumber
public class FuzzyNumber
{
private double _maximum= 0.0; //maximum cua số mờ private double _minimum = 0.0; //minimum cua số mờ
private double _number = 0.0; // tại vị trí này,membership lớn nhất private double _membership = 0.0; //độ phụ thuộc
private string _name = string.Empty; public FuzzyNumber()
{ }
public FuzzyNumber(string name, double lowRange, double highRange) {
}
public FuzzyNumber(LinguisticVariable type, double lowRange, double
highRange) {
…… }
public FuzzyNumber(string name, double lowRange, double highRange,
double number)
{ }
public void SetNumber(double number) {
_number = number; }
• Thiết kế các Class FuzzySet
public class FuzzySet : ArrayList
{
private string _name = string.Empty; public string Name
{ get { return _name; } set { _name = value; } } public FuzzySet() : base() { }
public FuzzySet(string name) {
_name = name; }
… }
Class FuzzySet chứa dữ liệu về tập mờ, Định nghĩa Class MembershipFunction:
Class MembershipFunction chứa các định nghĩa công thức Hàm phụ thuộc có dạng Triangular, RightTrapezoid và LeftTrapezoid …
public class MembershipFunction
{
public MembershipFunction() {
}
public static double Triangular_MembershipFunction(double a, double
b, double c, double x) { double membership = 0.0; if (x <= a) membership = 0; else if (x > a && x <= b) membership = (x - a) / (b - a); else if (x > b && x < c) membership = (c - x) / (c - b); else if (x >= c) membership = 0; return membership; }
public static double RightTrapezoid_MembershipFunction(double a, double
{ double membership = 0.0; if (x <= a) membership = 1.0; else if (x > a && x < b) membership = (b-x) / (b - a); else if (x >= b) membership = 0; return membership; }
public static double LeftTrapezoid_MembershipFunction(double a, double
b, double x) { double membership = 0.0; if (x <= a) membership = 0.0; else if (x > a && x < b)
membership = (x-a) / (b - a); else if (x >= b)
membership = 1.0; return membership; }
• Khởi tạo chương trình chạy:
Tạo 3 tập mờ CarSpeed, Temperature, cover.
FuzzySet carSpeedSet = new FuzzySet("CarSpeed");
FuzzySet temperatureSet = new FuzzySet("Temperature"); FuzzySet coverSet = new FuzzySet("Cover");
Tạo các khái niệm số mờ “Fzeezing”, “Cool”, “Warm”, “Hot” thuộc tập mờ Temperature
FuzzyNumber freezing = new FuzzyNumber("Freezing", 10, 50); FuzzyNumber cool = new FuzzyNumber("Cool", 30, 70);
FuzzyNumber warm = new FuzzyNumber("Warm", 50, 90); FuzzyNumber hot = new FuzzyNumber("Hot", 70, 110); temperatureSet.Add(freezing);
temperatureSet.Add(cool); temperatureSet.Add(warm); temperatureSet.Add(hot);
Tạo các khái niệm số mờ “Sunny”, “Partly”, “Overcast” thuộc tập mờ Cover
//Cover fuzzy set
FuzzyNumber sunny = new FuzzyNumber("Sunny", 0, 40); FuzzyNumber partly = new FuzzyNumber("Partly", 20, 80); FuzzyNumber overcast = new FuzzyNumber("Overcast", 60, 100); coverSet.Add(sunny);
coverSet.Add(partly); coverSet.Add(overcast);
Tạo các khái niệm số mờ “Slow”, “Fast” thuộc tập mờ CarSpeed
//car speed fuzzy set
FuzzyNumber slow = new FuzzyNumber("Slow", 0, 75, 25); FuzzyNumber fast = new FuzzyNumber("Fast", 25, 100, 75); carSpeedSet.Add(slow);
carSpeedSet.Add(fast);
Cài đặt các luật làm mờ:
public void Fuzzy()
{
double temperature = trackBar1.Value; double cover = trackbarCloudCover.Value; double membershipFast = 0.0; double membershipSlow = 0.0; double membershipFreezing = 0.0; double membershipCool = 0.0; double membershipWarm = 0.0; double membershipHot = 0.0; double membershipSunny = 0.0; double membershipPartly = 0.0; double membershipOvercast = 0.0; //freezing if (temperatureSet.Temperature_Is_Freezing(temperature)) { membershipFreezing =
temperatureSet.GetMemberShipOfVariable(LinguisticVariable.Freezing, temperature); membershipSlow = membershipFreezing;
carSpeedSet.DriverSlow(membershipSlow); }
// sunny and cool
if (coverSet.Cover_Is_Sunny(cover) &&
temperatureSet.Temperature_Is_Cool(temperature)) {
membershipSunny =
coverSet.GetMemberShipOfVariable(LinguisticVariable.Sunny, cover);
membershipCool =
temperatureSet.GetMemberShipOfVariable(LinguisticVariable.Cool , temperature);
membershipFast = FuzzySet.Min(membershipSunny, membershipCool); carSpeedSet.DriverFast(membershipFast);
}
// sunny and warm
if (coverSet.Cover_Is_Sunny(cover) &&
temperatureSet.Temperature_Is_Warm(temperature)) {
membershipSunny =
coverSet.GetMemberShipOfVariable(LinguisticVariable.Sunny, cover);
membershipWarm =
temperatureSet.GetMemberShipOfVariable(LinguisticVariable.Warm , temperature);
membershipFast = FuzzySet.Min(membershipSunny, membershipWarm);
}
// sunny and hot
if (coverSet.Cover_Is_Sunny(cover) &&
temperatureSet.Temperature_Is_Hot(temperature)) {
membershipSunny =
coverSet.GetMemberShipOfVariable(LinguisticVariable.Sunny, cover); membershipHot =
temperatureSet.GetMemberShipOfVariable(LinguisticVariable.Hot, temperature);
membershipFast = FuzzySet.Min(membershipSunny, membershipHot); carSpeedSet.DriverFast(membershipFast);
}
//partly and cool
if (coverSet.Cover_Is_Partly(cover) &&
temperatureSet.Temperature_Is_Cool(temperature)) {
membershipPartly =
coverSet.GetMemberShipOfVariable(LinguisticVariable.Partly, cover);
membershipCool =
temperatureSet.GetMemberShipOfVariable(LinguisticVariable.Cool , temperature);
membershipSlow = FuzzySet.Min(membershipPartly, membershipCool); carSpeedSet.DriverSlow(membershipSlow);
}
//partly and warm
if (coverSet.Cover_Is_Partly(cover) &&
temperatureSet.Temperature_Is_Warm(temperature)) {
membershipPartly =
coverSet.GetMemberShipOfVariable(LinguisticVariable.Partly, cover);
membershipWarm =
temperatureSet.GetMemberShipOfVariable(LinguisticVariable.Warm , temperature);
membershipFast = FuzzySet.Min(membershipPartly, membershipWarm); carSpeedSet.DriverFast(membershipFast);
}
//partly and hot
if (coverSet.Cover_Is_Partly(cover) &&
temperatureSet.Temperature_Is_Hot(temperature)) {
membershipPartly =
coverSet.GetMemberShipOfVariable(LinguisticVariable.Partly, cover);
membershipHot =
temperatureSet.GetMemberShipOfVariable(LinguisticVariable.Hot , temperature);
membershipFast = FuzzySet.Min(membershipPartly,
}
//overcast
if (coverSet.Cover_Is_Overcast(cover)) {
membershipOvercast =
coverSet.GetMemberShipOfVariable(LinguisticVariable.Overcast , cover);
membershipSlow = membershipOvercast; carSpeedSet.DriverSlow(membershipSlow); }
double speed = carSpeedSet.Defuzzy(); speedControl1.Speed = Math.Round(speed);
UpdateSpeed(speed); }