public FuzzySet EvalMembership(double input, string tofind) { FuzzySet result = new FuzzySet(); for (int i = 0; i < regions.Length; i++) { if (regions[i].Leftmost == true && regions[i].Rightmost == false) { // is the leftmost region if (input <= regions[i].Rightbound.X && input > regions[i].Peak.X) { point tmp = new point(); tmp = tmp.liner_interpolation(regions[i].Peak, regions[i].Rightbound, input, tofind); result.Data.Add(regions[i].Name, tmp.Y); } else if (input <= regions[i].Peak.X) { result.Data.Add(regions[i].Name, 1.0); } else { result.Data.Add(regions[i].Name, 0.0); } } else if (regions[i].Rightmost == true && regions[i].Leftmost == false) { // is the rightmost region if (input >= regions[i].Leftbound.X && input < regions[i].Peak.X) { point tmp = new point(); tmp = tmp.liner_interpolation(regions[i].Peak, regions[i].Leftbound, input, tofind); result.Data.Add(regions[i].Name, tmp.Y); } else if (input >= regions[i].Peak.X) { result.Data.Add(regions[i].Name, 1.0); } else { result.Data.Add(regions[i].Name, 0.0); } } else { // middle region if (input >= regions[i].Leftbound.X && input < regions[i].Peak.X) { point tmp = new point(); tmp = tmp.liner_interpolation(regions[i].Leftbound, regions[i].Peak, input, tofind); result.Data.Add(regions[i].Name, tmp.Y); } else if (input >= regions[i].Peak.X && input <= regions[i].Rightbound.X) { point tmp = new point(); tmp = tmp.liner_interpolation(regions[i].Peak, regions[i].Rightbound, input, tofind); result.Data.Add(regions[i].Name, tmp.Y); } else { result.Data.Add(regions[i].Name, 0.0); } } } return(result); }
static void Main(string[] args) { //input memebership function creation Region very_cold = create_region("very cold", 0.0, 0.0, 60.0, 1.0, 65.0, 0.0, true, false); Region cold = create_region("cold", 60.0, 0.0, 65.0, 1.0, 70.0, 0.0, false, false); Region warm = create_region("warm", 65.0, 0.0, 70.0, 1.0, 75.0, 0.0, false, false); Region hot = create_region("hot", 70.0, 0.0, 75.0, 1.0, 80.0, 0.0, false, false); Region very_hot = create_region("very hot", 75.0, 0.0, 80.0, 1.0, 0.0, 0.0, false, true); MembershipFunction temp = new MembershipFunction(very_cold, cold, warm, hot, very_hot); // output memebership function creation string[] regions = { "hi cool", "cool", "no change", "heat", "hi heat" }; Region hicool = create_region("hi cool", 0.0, 0.0, -100.0, 1.0, -50.0, 0.0, true, false); Region cool = create_region("cool", -50.25, 0.0, -50.0, 1.0, -1.0, 0.0, false, false); Region no_change = create_region("no change", -1.0, 0.0, 0.0, 1.0, 1.0, 0.0, false, false); Region heat = create_region("heat", 1.0, 0.0, 50.0, 1.0, 50.25, 0.0, false, false); Region hiheat = create_region("hi heat", 50.0, 0.0, 100.0, 1.0, 0.0, 0.0, false, true); MembershipFunction heat_cool = new MembershipFunction(hicool, cool, no_change, heat, hiheat); // get user input bool cont = true; while (cont) { double current_temp = 0; double target_temp = 0; bool current_flag = false; bool target_flag = false; while (!current_flag) { Console.WriteLine("Please Enter a current temperature (ex. 75.0)"); current_flag = Double.TryParse(Console.ReadLine(), out current_temp); } while (!target_flag) { Console.WriteLine("Please Enter a target temperature (ex. 75.0)"); target_flag = Double.TryParse(Console.ReadLine(), out target_temp); } FuzzySet current_fuzz = new FuzzySet(); FuzzySet target_fuzz = new FuzzySet(); current_fuzz = temp.EvalMembership(current_temp, "y"); target_fuzz = temp.EvalMembership(target_temp, "y"); FuzzySet output = new FuzzySet(); output = output.BaseRules(target_fuzz, current_fuzz); string result = heat_cool.defuzzify(output); Console.WriteLine(result); Console.WriteLine("would you like to run another execution y/n"); char cont_char = Convert.ToChar(Console.ReadLine()); if (cont_char == 'y') { cont = true; } else { cont = false; } } }
public string defuzzify(FuzzySet data, string type = "max") { type.ToLower(); if (type == "max") { double max = 0.0; string region = string.Empty; foreach (string key in data.Data.Keys) { if (data.Data[key] != 0.0) { if (data.Data[key] > max) { max = data.Data[key]; region = key; } } } string[] regions = { "hi cool", "cool", "no change", "heat", "hi heat" }; point value = new point(); point value1 = new Fuzzy.point(); if (region == regions[0]) { value = value.liner_interpolation(this.Regions[0].Peak, this.Regions[0].Leftbound, max, "x"); return("Output is: " + Math.Abs(value.X) + "% cooling"); } else if (region == regions[1]) { value = value.liner_interpolation(this.Regions[1].Peak, this.Regions[1].Leftbound, max, "x"); value1 = value1.liner_interpolation(this.Regions[1].Peak, this.Regions[1].Rightbound, max, "x"); double output = (value.X + value1.X) / 2; return("Output is: " + Math.Abs(output) + "% cooling"); } else if (region == regions[2]) { value = value.liner_interpolation(this.Regions[2].Peak, this.Regions[2].Leftbound, max, "x"); value1 = value1.liner_interpolation(this.Regions[2].Peak, this.Regions[2].Rightbound, max, "x"); double output = (value.X + value1.X) / 2; return("Output is: " + Math.Abs(output) + "% sytstem is off"); } else if (region == regions[3]) { value = value.liner_interpolation(this.Regions[3].Peak, this.Regions[3].Leftbound, max, "x"); value1 = value1.liner_interpolation(this.Regions[3].Peak, this.Regions[3].Rightbound, max, "x"); double output = (value.X + value1.X) / 2; return("Output is: " + Math.Abs(output) + "% heating"); } else if (region == regions[4]) { value = value.liner_interpolation(this.Regions[4].Peak, this.Regions[4].Rightbound, max, "x"); return("Output is: " + Math.Abs(value.X) + "% heating"); } else { return(string.Empty); } } else if (type == "gravity") { // logic for doing center of gravity method return(string.Empty); } else { // logic for doing center of area return(string.Empty); } }
public FuzzySet BaseRules(FuzzySet target_tmp, FuzzySet current_tmp) { List <double> HiCool = new List <double>(); List <double> Cool = new List <double>(); List <double> NoChange = new List <double>(); List <double> Heat = new List <double>(); List <double> HiHeat = new List <double>(); HiCool.Add(0.0); Cool.Add(0.0); NoChange.Add(0.0); Heat.Add(0.0); HiHeat.Add(0.0); ////////////+---------------------------------------------------------------------+ ////////////| target Temp | Knowledge Base | ////////////+-----------------------+---------+---------+----------+--------------+ ////////////| Current temp | Very Cold | Cold | Warm | Hot | Very Hot | ////////////+---------------------------------------------------------------------+ ////////////| Very Cold | No Change | heat | hi-heat | hi-heat | hi-heat | ////////////+---------------------------------------------------------------------+ ////////////| Cold | cool | no change | heat | hi-heat | hi-heat | ////////////+---------------------------------------------------------------------+ ////////////| Warm | hi-cool | cool | No Change | heat | hi-heat | ////////////+---------------------------------------------------------------------+ ////////////| Hot | hi-cool | hi-cool | cool | No Change | heat | ////////////+---------------------------------------------------------------------+ ////////////| Very Hot | hi-cool | hi-cool | hi-cool | cool | No Change | ////////////+------------+----------+---------+---------+----------+--------------+ // no_change senerios if (target_tmp.Data["very cold"] != 0.0 && current_tmp.Data["very cold"] != 0.0) { double tmp = Math.Min(target_tmp.Data["very cold"], current_tmp.Data["very cold"]); NoChange.Add(tmp); } if (target_tmp.Data["cold"] != 0.0 && current_tmp.Data["cold"] != 0.0) { double tmp = Math.Min(target_tmp.Data["cold"], current_tmp.Data["cold"]); NoChange.Add(tmp); } if (target_tmp.Data["warm"] != 0.0 && current_tmp.Data["warm"] != 0.0) { double tmp = Math.Min(target_tmp.Data["warm"], current_tmp.Data["warm"]); NoChange.Add(tmp); } if (target_tmp.Data["hot"] != 0.0 && current_tmp.Data["hot"] != 0.0) { double tmp = Math.Min(target_tmp.Data["hot"], current_tmp.Data["hot"]); NoChange.Add(tmp); } if (target_tmp.Data["very hot"] != 0.0 && current_tmp.Data["very hot"] != 0.0) { double tmp = Math.Min(target_tmp.Data["very hot"], current_tmp.Data["very hot"]); NoChange.Add(tmp); } if (target_tmp.Data["very cold"] != 0.0 && current_tmp.Data["cold"] != 0.0) { double tmp = Math.Min(target_tmp.Data["very cold"], current_tmp.Data["cold"]); Cool.Add(tmp); } if (target_tmp.Data["very cold"] != 0.0 && (current_tmp.Data["warm"] != 0.0 || current_tmp.Data["hot"] != 0.0 || current_tmp.Data["very hot"] != 0.0)) { double tmp = Math.Min(target_tmp.Data["very cold"], current_tmp.Data["warm"]); double tmp1 = Math.Min(target_tmp.Data["very cold"], current_tmp.Data["hot"]); double tmp2 = Math.Min(target_tmp.Data["very cold"], current_tmp.Data["very hot"]); double value = Math.Max(tmp1, tmp); double value2 = Math.Max(value, tmp2); HiCool.Add(value2); } if (target_tmp.Data["cold"] != 0.0 && current_tmp.Data["very cold"] != 0.0) { double tmp = Math.Min(target_tmp.Data["cold"], current_tmp.Data["very cold"]); Heat.Add(tmp); } if (target_tmp.Data["cold"] != 0.0 && current_tmp.Data["warm"] != 0.0) { double tmp = Math.Min(target_tmp.Data["cold"], current_tmp.Data["warm"]); Cool.Add(tmp); } if (target_tmp.Data["cold"] != 0.0 && current_tmp.Data["hot"] != 0.0 || current_tmp.Data["very hot"] != 0.0) { double tmp = Math.Min(target_tmp.Data["cold"], current_tmp.Data["hot"]); double tmp2 = Math.Min(target_tmp.Data["cold"], current_tmp.Data["very hot"]); double value = Math.Max(tmp, tmp2); HiCool.Add(value); } if (target_tmp.Data["warm"] != 0.0 && current_tmp.Data["very cold"] != 0.0) { double tmp = Math.Min(target_tmp.Data["warm"], current_tmp.Data["very cold"]); HiHeat.Add(tmp); } if (target_tmp.Data["warm"] != 0.0 && current_tmp.Data["cold"] != 0.0) { double tmp = Math.Min(target_tmp.Data["warm"], current_tmp.Data["cold"]); Heat.Add(tmp); } if (target_tmp.Data["warm"] != 0.0 && current_tmp.Data["hot"] != 0.0) { double tmp = Math.Min(target_tmp.Data["warm"], current_tmp.Data["hot"]); Cool.Add(tmp); } if (target_tmp.Data["warm"] != 0.0 && current_tmp.Data["very hot"] != 0.0) { double tmp = Math.Min(target_tmp.Data["warm"], current_tmp.Data["very hot"]); HiCool.Add(tmp); } if (target_tmp.Data["hot"] != 0.0 && (current_tmp.Data["very cold"] != 0.0 || current_tmp.Data["cold"] != 0.0)) { double tmp = Math.Min(target_tmp.Data["hot"], current_tmp.Data["very cold"]); double tmp2 = Math.Min(target_tmp.Data["hot"], current_tmp.Data["cold"]); double value = Math.Max(tmp, tmp2); HiHeat.Add(value); } if (target_tmp.Data["hot"] != 0.0 && current_tmp.Data["warm"] != 0.0) { double tmp = Math.Min(target_tmp.Data["hot"], current_tmp.Data["warm"]); Heat.Add(tmp); } if (target_tmp.Data["hot"] != 0.0 && current_tmp.Data["very hot"] != 0.0) { double tmp = Math.Min(target_tmp.Data["hot"], current_tmp.Data["very hot"]); Cool.Add(tmp); } if (target_tmp.Data["very hot"] != 0.0 && (current_tmp.Data["very cold"] != 0.0 || current_tmp.Data["cold"] != 0.0 || current_tmp.Data["warm"] != 0.0)) { double tmp = Math.Min(target_tmp.Data["very hot"], current_tmp.Data["very cold"]); double tmp2 = Math.Min(target_tmp.Data["very hot"], current_tmp.Data["cold"]); double tmp3 = Math.Min(target_tmp.Data["very hot"], current_tmp.Data["warm"]); double value = Math.Max(tmp, tmp2); double value2 = Math.Max(tmp3, value); HiHeat.Add(value2); } if (target_tmp.Data["very hot"] != 0.0 && current_tmp.Data["hot"] != 0.0) { double tmp = Math.Min(target_tmp.Data["very hot"], current_tmp.Data["hot"]); Heat.Add(tmp); } double max_hicool = 0.0; string[] regions = { "hi cool", "cool", "no change", "heat", "hi heat" }; double[] dom = new double[5]; foreach (double item in HiCool) { max_hicool = Math.Max(max_hicool, item); } dom[0] = max_hicool; double max_cool = 0.0; foreach (double item in Cool) { max_cool = Math.Max(max_cool, item); } dom[1] = max_cool; double no_change = 0.0; foreach (double item in NoChange) { no_change = Math.Max(no_change, item); } dom[2] = no_change; double max_heat = 0.0; foreach (double item in Heat) { max_heat = Math.Max(max_heat, item); } dom[3] = max_heat; double max_hiheat = 0.0; foreach (double item in HiHeat) { max_hiheat = Math.Max(max_hiheat, item); } dom[4] = max_hiheat; FuzzySet result = new FuzzySet(regions, dom, "output"); return(result); }