private void SetUpFisII() { //Input var impact = new LinguisticVariable("Impact", 0, 100); new List <FuzzySet>() { new FuzzySet("Low", new TrapezoidalFunction(0, 5, 25, 30)), new FuzzySet("Medium", new TrapezoidalFunction(20, 25, 55, 60)), new FuzzySet("High", new TrapezoidalFunction(50, 55, 95, 100)) }.ForEach(x => impact.AddLabel(x)); var onGround = new LinguisticVariable("On_ground", -0.5f, 1.5f); new List <FuzzySet>() { new FuzzySet("Low", new TrapezoidalFunction(-0.5f, -0.2f, 0.2f, 0.5f)), new FuzzySet("Medium", new TrapezoidalFunction(0.1f, 0.5f, 0.5f, 0.9f)), new FuzzySet("High", new TrapezoidalFunction(0.5f, 0.8f, 1.2f, 1.5f)) }.ForEach(x => onGround.AddLabel(x)); var timeOnGround = new LinguisticVariable("Time_on_ground", -1, 901); new List <FuzzySet>() { new FuzzySet("Brief", new TrapezoidalFunction(-1, 1, 1, 2)), new FuzzySet("Short", new TrapezoidalFunction(1, 5, 10, 15)), new FuzzySet("Moderate", new TrapezoidalFunction(10, 120, 480, 720)), new FuzzySet("Long", new TrapezoidalFunction(480, 900, 900, 901)) }.ForEach(x => timeOnGround.AddLabel(x)); //Output var fallWearable = new LinguisticVariable("Fall_wearable", -0.5f, 1.5f); new List <FuzzySet>() { new FuzzySet("Low", new TrapezoidalFunction(-0.5f, -0.2f, 0.2f, 0.5f)), new FuzzySet("Medium", new TrapezoidalFunction(0.1f, 0.5f, 0.5f, 0.9f)), new FuzzySet("High", new TrapezoidalFunction(0.5f, 0.8f, 1.2f, 1.5f)) }.ForEach(x => fallWearable.AddLabel(x)); var db = new Database(); db.AddVariable(impact); db.AddVariable(onGround); db.AddVariable(timeOnGround); db.AddVariable(fallWearable); fisII = new InferenceSystem(db, new CentroidDefuzzifier(1000)); fisII.NewRule("Rule 1", "IF Impact IS High AND On_ground IS High AND Time_on_ground IS Long THEN Fall_wearable IS High"); fisII.NewRule("Rule 2", "IF Impact IS Medium AND On_ground IS High AND Time_on_ground IS Long THEN Fall_wearable IS High"); fisII.NewRule("Rule 3", "IF Impact IS Low AND On_ground IS Low AND (Time_on_ground IS Short OR Time_on_ground IS Brief) THEN Fall_wearable IS Low"); fisII.NewRule("Rule 4", "IF Impact IS Medium AND On_ground IS Medium AND Time_on_ground IS Moderate THEN Fall_wearable IS Medium"); fisII.NewRule("Rule 5", "IF Impact IS High AND On_ground IS Medium AND (Time_on_ground IS Short OR Time_on_ground IS Brief) THEN Fall_wearable IS Medium"); }
private void Rules() { infSystem = new InferenceSystem(database, new CentroidDefuzzifier(120)); infSystem.NewRule("Rule 1", "IF Distance IS Far THEN Speed IS Slow"); //AITank Player tanka uzaksa yavaş infSystem.NewRule("Rule 2", "IF Distance IS Medi THEN Speed IS Medium"); // AITank Player tanka orta uzaklıkta ise orta hızda infSystem.NewRule("Rule 3", "IF Distance IS Near THEN Speed IS Fast"); //AITank Player tanka yakınsa hızlı gidecek }
private void SetUpFisI() { //Input var treshhold = new LinguisticVariable("Threshold", 0, 100); new List <FuzzySet>() { new FuzzySet("Low", new TrapezoidalFunction(0, 5, 25, 30)), new FuzzySet("Medium", new TrapezoidalFunction(20, 25, 55, 60)), new FuzzySet("High", new TrapezoidalFunction(50, 55, 95, 100)) }.ForEach(x => treshhold.AddLabel(x)); //Output var impact = new LinguisticVariable("Impact", 0, 100); new List <FuzzySet>() { new FuzzySet("Low", new TrapezoidalFunction(0, 5, 25, 30)), new FuzzySet("Medium", new TrapezoidalFunction(20, 25, 55, 60)), new FuzzySet("High", new TrapezoidalFunction(50, 55, 95, 100)) }.ForEach(x => impact.AddLabel(x)); var db = new Database(); db.AddVariable(treshhold); db.AddVariable(impact); fisI = new InferenceSystem(db, new CentroidDefuzzifier(1000)); fisI.NewRule("Rule 1", "IF Threshold IS Low THEN Impact IS Low"); fisI.NewRule("Rule 2", "IF Threshold IS Medium THEN Impact IS Medium"); fisI.NewRule("Rule 3", "IF Threshold IS High THEN Impact IS High"); }
private void SetRules() { infSystem = new InferenceSystem(database, new CentroidDefuzzifier(120)); infSystem.NewRule("Rule 1", "IF Distance IS Near THEN Speed IS Slow"); infSystem.NewRule("Rule 2", "IF Distance IS Med THEN Speed IS Medium"); infSystem.NewRule("Rule 3", "IF Distance IS Far THEN Speed IS Fast"); }
public InferenceSystem SetupInferenceSystem(int width) { var mp = new TrapezoidalFunction(centerPoint - width / 2, centerPoint, centerPoint + width / 2); var mn = new TrapezoidalFunction(-centerPoint - width / 2, -centerPoint, -centerPoint + width / 2); var sp = new TrapezoidalFunction(-centerPoint / 2 + width / 4, (double)centerPoint / 2, centerPoint / 2 + width / 3); var sn = new TrapezoidalFunction(-centerPoint / 2 - width / 3, (double)-centerPoint / 2, centerPoint / 2 - width / 4); var ze = new TrapezoidalFunction(-(double)centerPoint / 4, 0, (double)centerPoint / 4); var mpSet = new FuzzySet("MP", mp); var mnSet = new FuzzySet("MN", mn); var spSet = new FuzzySet("SP", sp); var snSet = new FuzzySet("SN", sn); var zeSet = new FuzzySet("ZE", ze); var ruleDatabase = new Database(); for (int i = 0; i < windowSize * windowSize - 1; i++) { var variable = new LinguisticVariable(String.Format("IN{0}", i), -255, 255); variable.AddLabel(mpSet); variable.AddLabel(mnSet); ruleDatabase.AddVariable(variable); } var outVariable = new LinguisticVariable("OUT", -centerPoint - width / 2, centerPoint + width / 2); outVariable.AddLabel(spSet); outVariable.AddLabel(snSet); outVariable.AddLabel(zeSet); ruleDatabase.AddVariable(outVariable); var inferenceSystem = new InferenceSystem(ruleDatabase, new CentroidDefuzzifier(100)); string rule1 = "IF "; string rule2 = "IF "; string rule3 = "IF "; for (int i = 0; i < windowSize * windowSize - 1; i++) { rule1 += String.Format("IN{0} is MP and ", i); rule2 += String.Format("IN{0} is MN and ", i); rule3 += String.Format("IN{0} is not MP and IN{0} is not MN AND ", i); } rule1 = rule1.Remove(rule1.Length - 4, 4); rule2 = rule2.Remove(rule2.Length - 4, 4); rule3 = "IF NOT (" + rule1.Replace("IF", "") + ") AND NOT(" + rule2.Replace("IF", "") + ")"; rule1 += " then OUT is SN"; rule2 += " then OUT is SP"; rule3 += " then OUT is ZE"; inferenceSystem.NewRule("Rule1", rule1); inferenceSystem.NewRule("Rule2", rule2); inferenceSystem.NewRule("Rule3", rule3); return(inferenceSystem); }
public ZoomGps() { // création du langage var ld = LinguistictDistance(); var lv = LinguisticVitesse(); var lz = LinguisticZoom(); // création des règles Database fuzzyDB = new Database(); fuzzyDB.AddVariable(ld); fuzzyDB.AddVariable(lv); fuzzyDB.AddVariable(lz); var IS = new InferenceSystem(fuzzyDB, new CentroidDefuzzifier(100)); IS.NewRule("Rule 1", "IF Distance IS Grande THEN Zoom IS Petit"); IS.NewRule("Rule 2", "IF Distance IS Faible AND Vitesse IS Lente THEN Zoom IS Normal"); IS.NewRule("Rule 3", "IF Distance IS Faible AND Vitesse IS PeuRapide THEN Zoom IS Normal"); IS.NewRule("Rule 4", "IF Distance IS Faible AND Vitesse IS Rapide THEN Zoom IS Gros"); IS.NewRule("Rule 5", "IF Distance IS Faible AND Vitesse IS TresRapide THEN Zoom IS Gros"); IS.NewRule("Rule 6", "IF Distance IS Moyenne AND Vitesse IS Lente THEN Zoom IS Petit"); IS.NewRule("Rule 7", "IF Distance IS Moyenne AND Vitesse IS PeuRapide THEN Zoom IS Normal"); IS.NewRule("Rule 8", "IF Distance IS Moyenne AND Vitesse IS Rapide THEN Zoom IS Normal"); IS.NewRule("Rule 9", "IF Distance IS Moyenne AND Vitesse IS TresRapide THEN Zoom IS Gros"); m_IS = IS; }
private void AddToDatabese() { database = new Database(); database.AddVariable(lvDistance); database.AddVariable(lvSpeed); infSystem = new InferenceSystem(database, new CentroidDefuzzifier(20)); infSystem.NewRule("Rule1", "IF Distance IS Near THEN Speed IS Slow"); infSystem.NewRule("Rule2", "IF Distance IS Med THEN Speed IS Medium"); infSystem.NewRule("Rule3", "IF Distance IS Far THEN Speed IS Fast"); }
private void Initialize(List <Variable> variables, List <String> roles) { _fuzzyDB = new Database(); foreach (var variable in variables) { _fuzzyDB.AddVariable(variable.FVariable); if (variable.Type == JConfig.VariableType.Ouput) { _outputName = variable.FVariable.Name; } } _centroidDefuzzifier = new CentroidDefuzzifier(1000); _system = new InferenceSystem(_fuzzyDB, _centroidDefuzzifier); foreach (var variable in variables) { if (variable.Type == JConfig.VariableType.Input) { _system.SetInput(variable.Name, (float)variable.ResultValue); } } int roleCounter = 0; foreach (var role in roles) { _system.NewRule("Role " + roleCounter++, role); } }
private InferenceSystem SetupInferenceSystem(byte minLuma, byte maxLuma, byte meanLuma) { var lumaIn = new LinguisticVariable("LumaIn", minLuma, maxLuma); var lumaOut = new LinguisticVariable("LumaOut", 0, 255); var darkFunction = new TrapezoidalFunction(minLuma, meanLuma, TrapezoidalFunction.EdgeType.Right); var darkSet = new FuzzySet("Dark", darkFunction); var mediumFunction = new TrapezoidalFunction(minLuma, meanLuma, maxLuma); var mediumSet = new FuzzySet("Medium", mediumFunction); var lightFunction = new TrapezoidalFunction(meanLuma, maxLuma, TrapezoidalFunction.EdgeType.Left); var lightSet = new FuzzySet("Light", lightFunction); lumaIn.AddLabel(darkSet); lumaIn.AddLabel(mediumSet); lumaIn.AddLabel(lightSet); var whiteFunction = new SingletonFunction(255); var whiteSet = new FuzzySet("White", whiteFunction); var blackFunction = new SingletonFunction(0); var blackSet = new FuzzySet("Black", blackFunction); var grayFunction = new SingletonFunction(128); var graySet = new FuzzySet("Gray", grayFunction); lumaOut.AddLabel(blackSet); lumaOut.AddLabel(graySet); lumaOut.AddLabel(whiteSet); var database = new Database(); database.AddVariable(lumaIn); database.AddVariable(lumaOut); var inferenceSystem = new InferenceSystem(database, new CogDefuzzifier()); inferenceSystem.NewRule("Rule 1", "IF LumaIn IS Dark THEN LumaOut is Black"); inferenceSystem.NewRule("Rule 2", "IF LumaIn IS Medium THEN LumaOut is Gray"); inferenceSystem.NewRule("Rule 3", "IF LumaIn IS Light THEN LumaOut is White"); return(inferenceSystem); }
private void InitFuzz() { this.Charts = new List <FuzzySetGroupCharts>(); Database db = new Database(); // fuzzy set to determine perceived distance FuzzySetGroupDesc distanceGroup = new FuzzySetGroupDesc("Distance"); distanceGroup.Add(new FuzzySetDescLeft("Near", 0.75f, 2.5f)); distanceGroup.Add(new FuzzySetDescMid("Medium", 0.75f, 2.5f, 3, 10)); distanceGroup.Add(new FuzzySetDescRight("Far", 3, 10)); this.Charts.Add(new FuzzySetGroupCharts(distanceGroup)); FuzzySet[] distanceSets = FuzzUtil.CreateFuzzySets(distanceGroup); db.AddVariable(FuzzUtil.CreateVariable("RightDistance", 0, 10, distanceSets)); db.AddVariable(FuzzUtil.CreateVariable("LeftDistance", 0, 10, distanceSets)); db.AddVariable(FuzzUtil.CreateVariable("FrontDistance", 0, 10, distanceSets)); // fuzzy set to determine heading directions FuzzySetGroupDesc headingGroup = new FuzzySetGroupDesc("Heading"); headingGroup.Add(new FuzzySetDescLeft("HardLeft", -35, -30)); headingGroup.Add(new FuzzySetDescMid("Left", -35, -30, -20, -15)); headingGroup.Add(new FuzzySetDescMid("SlightLeft", -20, -15, -5, 0)); headingGroup.Add(new FuzzySetDescMid("Straight", -5, -1, 1, 5)); headingGroup.Add(new FuzzySetDescMid("SlightRight", 0, 5, 15, 20)); headingGroup.Add(new FuzzySetDescMid("Right", 15, 20, 30, 35)); headingGroup.Add(new FuzzySetDescRight("HardRight", 30, 35)); this.Charts.Add(new FuzzySetGroupCharts(headingGroup)); FuzzySet[] headingSets = FuzzUtil.CreateFuzzySets(headingGroup); db.AddVariable(FuzzUtil.CreateVariable("Heading", -50, 50, headingSets)); _inferenceSystem = new InferenceSystem(db, new CentroidDefuzzifier(50)); // if all is clear ahead go for it _inferenceSystem.NewRule("1", "IF FrontDistance IS Far THEN Heading IS Straight"); // should handle left/right turns _inferenceSystem.NewRule("2", "IF FrontDistance IS Near AND RightDistance IS Near THEN Heading IS Left"); _inferenceSystem.NewRule("3", "IF FrontDistance IS Near AND LeftDistance IS Near THEN Heading IS Right"); // edge away from walls _inferenceSystem.NewRule("4", "IF LeftDistance IS Near AND (RightDistance IS Medium OR RightDistance IS Far) THEN Heading IS SlightRight"); _inferenceSystem.NewRule("5", "IF RightDistance IS Near AND (LeftDistance IS Medium OR LeftDistance IS Far) THEN Heading IS SlightLeft"); // attempt to give priority to left turns if both are available? _inferenceSystem.NewRule("6", "IF FrontDistance IS Near THEN Heading IS Left"); }
public FuzzyContainer() { registredVariables = new List <string>(); //var HR = new LinguisticVariable("HR", 0, 200); //new List<FuzzySet>() //{ // new FuzzySet("Low", new TrapezoidalFunction(0, 5, 45, 50)), // new FuzzySet("Medium", new TrapezoidalFunction(50, 55, 115, 120)), // new FuzzySet("High", new TrapezoidalFunction(120, 125, 195 ,200)) //}.ForEach(x => HR.AddLabel(x)); var impact = new LinguisticVariable("IMPACT", 0, 100); new List <FuzzySet>() { new FuzzySet("Low", new TrapezoidalFunction(0, 5, 25, 30)), new FuzzySet("Medium", new TrapezoidalFunction(20, 25, 55, 60)), new FuzzySet("High", new TrapezoidalFunction(50, 55, 95, 100)) }.ForEach(x => impact.AddLabel(x)); registredVariables.Add(impact.Name); Console.WriteLine("IMPACT name: " + impact.Name); var onGround = new LinguisticVariable("ON_GROUND", -0.5f, 1.5f); new List <FuzzySet>() { new FuzzySet("Low", new TrapezoidalFunction(-0.5f, -0.2f, 0.2f, 0.5f)), new FuzzySet("Medium", new TrapezoidalFunction(0.1f, 0.5f, 0.5f, 0.9f)), new FuzzySet("High", new TrapezoidalFunction(0.5f, 0.8f, 1.2f, 1.5f)) }.ForEach(x => onGround.AddLabel(x)); registredVariables.Add(onGround.Name); var timeOnGround = new LinguisticVariable("TIME_ON_GROUND", -1, 901); new List <FuzzySet>() { new FuzzySet("Brief", new TrapezoidalFunction(-1, 1, 1, 2)), new FuzzySet("Short", new TrapezoidalFunction(1, 5, 10, 15)), new FuzzySet("Moderate", new TrapezoidalFunction(10, 120, 480, 720)), new FuzzySet("Long", new TrapezoidalFunction(480, 900, 900, 901)) }.ForEach(x => timeOnGround.AddLabel(x)); registredVariables.Add(timeOnGround.Name); //Output var fallWearable = new LinguisticVariable("FALL", -0.5f, 1.5f); new List <FuzzySet>() { new FuzzySet("Low", new TrapezoidalFunction(-0.5f, -0.2f, 0.2f, 0.5f)), new FuzzySet("Medium", new TrapezoidalFunction(0.1f, 0.5f, 0.5f, 0.9f)), new FuzzySet("High", new TrapezoidalFunction(0.5f, 0.8f, 1.2f, 1.5f)) }.ForEach(x => fallWearable.AddLabel(x)); //var heartRate = new LinguisticVariable("HEART_RATE", 0, 100); //new List<FuzzySet>() //{ // new FuzzySet("Low", new TrapezoidalFunction(0, 5, 25, 30)), // new FuzzySet("Medium", new TrapezoidalFunction(20, 25, 55, 60)), // new FuzzySet("High", new TrapezoidalFunction(50, 55, 95 ,100)) //}.ForEach(x => heartRate.AddLabel(x)); var db = new Database(); //db.AddVariable(HR); //db.AddVariable(heartRate); db.AddVariable(impact); db.AddVariable(onGround); db.AddVariable(timeOnGround); db.AddVariable(fallWearable); fis = new InferenceSystem(db, new CentroidDefuzzifier(1000)); // fis.NewRule("Rule 1", "IF HR IS Low THEN HEART_RATE IS Low"); //fis.NewRule("Rule 2", "IF HR IS Medium THEN HEART_RATE IS Medium"); //fis.NewRule("Rule 3", "IF HR IS High THEN HEART_RATE IS High"); fis.NewRule("Rule 4", "IF IMPACT IS High AND ON_GROUND IS High AND TIME_ON_GROUND IS Long THEN FALL IS High"); fis.NewRule("Rule 5", "IF IMPACT IS Medium AND ON_GROUND IS High AND TIME_ON_GROUND IS Long THEN FALL IS High"); fis.NewRule("Rule 6", "IF IMPACT IS Low AND ON_GROUND IS Low AND (TIME_ON_GROUND IS Short OR TIME_ON_GROUND IS Brief) THEN FALL IS Low"); fis.NewRule("Rule 7", "IF IMPACT IS Medium AND ON_GROUND IS Medium AND TIME_ON_GROUND IS Moderate THEN FALL IS Medium"); fis.NewRule("Rule 8", "IF IMPACT IS High AND ON_GROUND IS Medium AND (TIME_ON_GROUND IS Short OR TIME_ON_GROUND IS Brief) THEN FALL IS Medium"); e2l = new EventToLabel(); }
//Definición del sistema de lógica difusa void InitFuzzyEngine() { //Funciones de Pertenencia //Demanda FuzzySet fsBaja = new FuzzySet("Baja", new TrapezoidalFunction(2f, 4f, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsMedia = new FuzzySet("Media", new TrapezoidalFunction(3f, 5f, 7f)); FuzzySet fsAlta = new FuzzySet("Alta", new TrapezoidalFunction(6f, 8f, TrapezoidalFunction.EdgeType.Left)); //Disponible FuzzySet fsPoco = new FuzzySet("Poco", new TrapezoidalFunction(4f, 8f, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsNormal = new FuzzySet("Normal", new TrapezoidalFunction(6f, 10f, 14f)); FuzzySet fsMucho = new FuzzySet("Mucho", new TrapezoidalFunction(12f, 16f, TrapezoidalFunction.EdgeType.Left)); //Fuerzas FuzzySet fsPbaja = new FuzzySet("Pbaja", new TrapezoidalFunction(0.1f, 0.2f, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsPmedia = new FuzzySet("Pmedia", new TrapezoidalFunction(0.3f, 0.5f, 0.7f)); FuzzySet fsPalta = new FuzzySet("Palta", new TrapezoidalFunction(0.6f, 0.8f, TrapezoidalFunction.EdgeType.Left)); //Entradas //Altura LinguisticVariable lvDemanda = new LinguisticVariable("Demanda", 0, 10); lvDemanda.AddLabel(fsBaja); lvDemanda.AddLabel(fsMedia); lvDemanda.AddLabel(fsAlta); //Velocidades LinguisticVariable lvDisponible = new LinguisticVariable("Disponible", 0, 20); lvDisponible.AddLabel(fsPoco); lvDisponible.AddLabel(fsNormal); lvDisponible.AddLabel(fsMucho); //Fuerzas LinguisticVariable lvFuerza = new LinguisticVariable("Prioridad", 0, 1); lvFuerza.AddLabel(fsPbaja); lvFuerza.AddLabel(fsPmedia); lvFuerza.AddLabel(fsPalta); //La base de datos Database fuzzyDb = new Database(); fuzzyDb.AddVariable(lvDemanda); fuzzyDb.AddVariable(lvFuerza); fuzzyDb.AddVariable(lvDisponible); //Sistema de interferencia ISL = new InferenceSystem(fuzzyDb, new CentroidDefuzzifier(800)); //Definicion de reglas ISL.NewRule("Rule 1", "IF Demanda IS Baja AND Disponible IS Poco THEN Prioridad IS Pmedia"); ISL.NewRule("Rule 2", "IF Demanda IS Baja AND Disponible IS Normal THEN Prioridad IS Pbaja"); ISL.NewRule("Rule 3", "IF Demanda IS Baja AND Disponible IS Mucho THEN Prioridad IS Pbaja"); ISL.NewRule("Rule 4", "IF Demanda IS Media AND Disponible IS Poco THEN Prioridad IS Palta"); ISL.NewRule("Rule 5", "IF Demanda IS Media AND Disponible IS Normal THEN Prioridad IS Pbaja"); ISL.NewRule("Rule 6", "IF Demanda IS Media AND Disponible IS Mucho THEN Prioridad IS Pbaja"); ISL.NewRule("Rule 7", "IF Demanda IS Alta AND Disponible IS Poco THEN Prioridad IS Palta"); ISL.NewRule("Rule 8", "IF Demanda IS Alta AND Disponible IS Normal THEN Prioridad IS Pmedia"); ISL.NewRule("Rule 9", "IF Demanda IS Alta AND Disponible IS Mucho THEN Prioridad IS Pbaja"); }
private void Form1_Load(object sender, EventArgs e) { /* * LinguisticVariable lvTemperature = new LinguisticVariable("Temperature", 0, 80); * * TrapezoidalFunction function1 = new TrapezoidalFunction(10, 15, TrapezoidalFunction.EdgeType.Right); * FuzzySet fsCold = new FuzzySet("Cold", function1); * TrapezoidalFunction function2 = new TrapezoidalFunction(10, 15, 20, 25); * FuzzySet fsCool = new FuzzySet("Cool", function2); * TrapezoidalFunction function3 = new TrapezoidalFunction(20, 25, 30, 35); * FuzzySet fsWarm = new FuzzySet("Warm", function3); * TrapezoidalFunction function4 = new TrapezoidalFunction(30, 35, TrapezoidalFunction.EdgeType.Left); * FuzzySet fsHot = new FuzzySet("Hot", function4); * * lvTemperature.AddLabel(fsCold); * lvTemperature.AddLabel(fsCool); * lvTemperature.AddLabel(fsWarm); * lvTemperature.AddLabel(fsHot); * * textBox1.Text += "Cold; Cool; Warm; Hot" + Environment.NewLine; * for (float x = 0; x < 80; x += 0.2f) * { * float y1 = lvTemperature.GetLabelMembership("Cold", x); * float y2 = lvTemperature.GetLabelMembership("Cool", x); * float y3 = lvTemperature.GetLabelMembership("Warm", x); * float y4 = lvTemperature.GetLabelMembership("Hot", x); * * textBox1.Text += y1.ToString() + " " + y2.ToString() + " " + y3.ToString() + " " + y4.ToString() + Environment.NewLine; * } */ /* * FuzzySet fsNear = new FuzzySet("Near", new TrapezoidalFunction(15, 50, TrapezoidalFunction.EdgeType.Right)); * FuzzySet fsMedium = new FuzzySet("Medium", new TrapezoidalFunction(15, 50, 60, 100)); * FuzzySet fsFar = new FuzzySet("Far", new TrapezoidalFunction(60, 100, TrapezoidalFunction.EdgeType.Left)); * * LinguisticVariable lvFront = new LinguisticVariable("FrontalDistance", 0, 120); * lvFront.AddLabel(fsNear); * lvFront.AddLabel(fsMedium); * lvFront.AddLabel(fsFar); * * FuzzySet fsZero = new FuzzySet("Zero", new TrapezoidalFunction(-10, 5, 5, 10)); * FuzzySet fsLP = new FuzzySet("LittlePositive", new TrapezoidalFunction(5, 10, 20, 25)); * FuzzySet fsP = new FuzzySet("Positive", new TrapezoidalFunction(20, 25, 35, 40)); * FuzzySet fsVP = new FuzzySet("VeryPositive", new TrapezoidalFunction(35, 40, TrapezoidalFunction.EdgeType.Left)); * * LinguisticVariable lvAngle = new LinguisticVariable("Angle", -10, 50); * lvAngle.AddLabel(fsZero); * lvAngle.AddLabel(fsLP); * lvAngle.AddLabel(fsP); * lvAngle.AddLabel(fsVP); * * Database fuzzyDB = new Database(); * fuzzyDB.AddVariable(lvFront); * fuzzyDB.AddVariable(lvAngle); * * InferenceSystem IS = new InferenceSystem(fuzzyDB, new CentroidDefuzzifier(1000)); * * IS.NewRule("Rule 1", "IF FrontalDistance IS Far THEN Angle IS Zero"); * IS.NewRule("Rule 2", "IF FrontalDistance IS Near THEN Angle IS Positive"); * * IS.SetInput("FrontalDistance", 20); * float a = IS.Evaluate("Angle"); // -> 29.999 * * try * { * FuzzyOutput fuzzyOutput = IS.ExecuteInference("Angle"); // -> 0.87 Positive * * foreach (FuzzyOutput.OutputConstraint oc in fuzzyOutput.OutputList) * { * Console.WriteLine(oc.Label + " - " + oc.FiringStrength.ToString()); * } * } * catch (Exception) * { * }*/ FuzzySet fsxM2 = new FuzzySet("xM2", new TrapezoidalFunction(-150, -80, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsxM1 = new FuzzySet("xM1", new TrapezoidalFunction(-90f, -65f, 0f)); FuzzySet fsxS = new FuzzySet("xS", new TrapezoidalFunction(-30f, 0, 30f)); FuzzySet fsxD1 = new FuzzySet("xD1", new TrapezoidalFunction(0f, 65f, 90f)); FuzzySet fsxD2 = new FuzzySet("xD2", new TrapezoidalFunction(80, 150, TrapezoidalFunction.EdgeType.Left)); LinguisticVariable lvX = new LinguisticVariable("XDist", -150, 150); lvX.AddLabel(fsxM2); lvX.AddLabel(fsxM1); lvX.AddLabel(fsxS); lvX.AddLabel(fsxD1); lvX.AddLabel(fsxD2); FuzzySet fsfM3 = new FuzzySet("fM3", new TrapezoidalFunction(-190, -140, -90)); FuzzySet fsfM2 = new FuzzySet("fM2", new TrapezoidalFunction(-140f, -90f, -45f)); FuzzySet fsfM1 = new FuzzySet("fM1", new TrapezoidalFunction(-90f, -45f, 0f)); FuzzySet fsfS = new FuzzySet("fS", new TrapezoidalFunction(-30f, 0, 30f)); FuzzySet fsfD1 = new FuzzySet("fD1", new TrapezoidalFunction(0f, 45f, 90f)); FuzzySet fsfD2 = new FuzzySet("fD2", new TrapezoidalFunction(45f, 90f, 140f)); FuzzySet fsfD3 = new FuzzySet("fD3", new TrapezoidalFunction(90, 140, 190)); LinguisticVariable lvF = new LinguisticVariable("FAngle", -190, 190); lvF.AddLabel(fsfM3); lvF.AddLabel(fsfM2); lvF.AddLabel(fsfM1); lvF.AddLabel(fsfS); lvF.AddLabel(fsfD1); lvF.AddLabel(fsfD2); lvF.AddLabel(fsfD3); FuzzySet fsaM3 = new FuzzySet("aM3", new TrapezoidalFunction(-45, -30, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsaM2 = new FuzzySet("aM2", new TrapezoidalFunction(-45f, -30f, -5f)); FuzzySet fsaM1 = new FuzzySet("aM1", new TrapezoidalFunction(-30f, -17f, -5f)); FuzzySet fsaS = new FuzzySet("aS", new TrapezoidalFunction(-5f, 0, 5f)); FuzzySet fsaD1 = new FuzzySet("aD1", new TrapezoidalFunction(5f, 17f, 30f)); FuzzySet fsaD2 = new FuzzySet("aD2", new TrapezoidalFunction(5f, 30f, 45f)); FuzzySet fsaD3 = new FuzzySet("aD3", new TrapezoidalFunction(30, 45, TrapezoidalFunction.EdgeType.Left)); LinguisticVariable lvA = new LinguisticVariable("OutAngle", -45, 45); lvA.AddLabel(fsaM3); lvA.AddLabel(fsaM2); lvA.AddLabel(fsaM1); lvA.AddLabel(fsaS); lvA.AddLabel(fsaD1); lvA.AddLabel(fsaD2); lvA.AddLabel(fsaD3); Database fuzzyDB = new Database(); fuzzyDB.AddVariable(lvX); fuzzyDB.AddVariable(lvF); fuzzyDB.AddVariable(lvA); /* * float x = -100, F = 180, A = 45; * float Px = 0, Pf = 0, Pa = 0; * int Nx = -1, Nf = -1, Na = -1; * * float _t = lvX.GetLabelMembership("xM2", x); * if (_t > Px) * { * Px = _t; * Nx = 0; * } * * _t = lvX.GetLabelMembership("xM1", x); * if (_t > Px) * { * Px = _t; * Nx = 1; * } * * _t = lvX.GetLabelMembership("xS", x); * if (_t > Px) * { * Px = _t; * Nx = 2; * } * * _t = lvX.GetLabelMembership("xD1", x); * if (_t > Px) * { * Px = _t; * Nx = 3; * } * * _t = lvX.GetLabelMembership("xD2", x); * if (_t > Px) * { * Px = _t; * Nx = 4; * }*/ InferenceSystem IS = new InferenceSystem(fuzzyDB, new CentroidDefuzzifier(1000)); IS.NewRule("Rule 1", "IF XDist IS xM2 AND FAngle IS fD3 THEN OutAngle IS aD3"); //Rule r = IS.GetRule("Rule 1"); //Clause cl = r.Output; IS.SetInput("XDist", -100); IS.SetInput("FAngle", 180); float fu = lvX.GetLabelMembership("xM2", -100); // 0.29 float fuu = lvF.GetLabelMembership("fD3", 180); // 0.2 float a = IS.Evaluate("OutAngle"); // -> 38.2 try { FuzzyOutput fuzzyOutput = IS.ExecuteInference("OutAngle"); // -> 0.2 ad3 foreach (FuzzyOutput.OutputConstraint oc in fuzzyOutput.OutputList) { Console.WriteLine(oc.Label + " - " + oc.FiringStrength.ToString()); } } catch (Exception) { } }
public FuzzyEngine() { // Linguistic labels (fuzzy sets) for Momentum var momDown = new FuzzySet("Down", new TrapezoidalFunction(-20, 5, 5, 5)); var momNeutral = new FuzzySet("Neutral", new TrapezoidalFunction(-20, 0, 0, 20)); var momUp = new FuzzySet("Up", new TrapezoidalFunction(5, 20, 20, 20)); // Linguistic labels (fuzzy sets) for RSI var rsiLow = new FuzzySet("Low", new TrapezoidalFunction(0, 30, 30, 30)); var rsiMedium = new FuzzySet("Medium", new TrapezoidalFunction(0, 50, 50, 100)); var rsiHigh = new FuzzySet("High", new TrapezoidalFunction(70, 100, 100, 100)); // MOM (Input) var lvMom = new LinguisticVariable("MOM", -20, 20); lvMom.AddLabel(momDown); lvMom.AddLabel(momNeutral); lvMom.AddLabel(momUp); // RSI (Input) var lvRsi = new LinguisticVariable("RSI", 0, 100); lvRsi.AddLabel(rsiLow); lvRsi.AddLabel(rsiMedium); lvRsi.AddLabel(rsiHigh); // Linguistic labels (fuzzy sets) that compose the Signal var fsShort = new FuzzySet("Sell", new TrapezoidalFunction(-100, 0, 0, 00)); var fsHold = new FuzzySet("Hold", new TrapezoidalFunction(-50, 0, 0, 50)); var fsLong = new FuzzySet("Buy", new TrapezoidalFunction(0, 100, 100, 100)); // Output var lvSignal = new LinguisticVariable("Signal", -100, 100); lvSignal.AddLabel(fsShort); lvSignal.AddLabel(fsHold); lvSignal.AddLabel(fsLong); // The database var fuzzyDB = new Database(); fuzzyDB.AddVariable(lvMom); fuzzyDB.AddVariable(lvRsi); fuzzyDB.AddVariable(lvSignal); // Creating the inference system IS = new InferenceSystem(fuzzyDB, new CentroidDefuzzifier(1000)); // Rules IS.NewRule("Rule 1", "IF RSI IS Low AND MOM IS Down THEN Signal IS Buy"); IS.NewRule("Rule 2", "IF RSI IS Medium AND MOM IS Down THEN Signal IS Buy"); IS.NewRule("Rule 3", "IF RSI IS High AND MOM IS Down THEN Signal IS Hold"); IS.NewRule("Rule 4", "IF RSI IS Low AND MOM IS Neutral THEN Signal IS Buy"); IS.NewRule("Rule 5", "IF RSI IS Medium AND MOM IS Neutral THEN Signal IS Hold"); IS.NewRule("Rule 6", "IF RSI IS High AND MOM IS Neutral THEN Signal IS Sell"); IS.NewRule("Rule 7", "IF RSI IS Low AND MOM IS Up THEN Signal IS Hold"); IS.NewRule("Rule 8", "IF RSI IS Medium AND MOM IS Up THEN Signal IS Sell"); IS.NewRule("Rule 9", "IF RSI IS High AND MOM IS Up THEN Signal IS Sell"); }
private void BuildInferenceSystem() { #region Add linguistic variables // Input var lvDistance = GetDistanceVariable(); var lvDate = GetDateVariable(); // Output var lvSeismicEnv = GetSeismicityVariable("Seismicity"); var lvWeekSeismicEnv = GetSeismicityVariable("WeekSeismicity"); var lvMonthSeismicEnv = GetSeismicityVariable("MonthSeismicity"); // Input Calculated average earthquake concetration (near only) var lvWeekConcentration = GetConcentrationVariable("WeekConcentration", StartDate.AddDays(-7), StartDate); var lvTwoWeeksConcentration = GetConcentrationVariable("TwoWeeksConcentration", StartDate.AddDays(-14), StartDate.AddDays(-7)); var lvFiveYearsConcentration = GetConcentrationVariable("FiveYearsConcentration", StartDate.AddYears(-5), StartDate.AddDays(-14)); var lvTenYearsConcentration = GetConcentrationVariable("TenYearsConcentration", StartDate.AddYears(-10), StartDate.AddYears(-5)); var lvFiftyYearsConcentration = GetConcentrationVariable("FiftyYearsConcentration", StartDate.AddYears(-50), StartDate.AddYears(-10)); var lvCenturyConcentration = GetConcentrationVariable("CenturyConcentration", StartDate.AddYears(-100), StartDate.AddYears(-50)); // Input Calculated maximum earthquake magnitude var lvMaxWeekMagnitude = GetMagnitudeVariable("MaxWeekMagnitude"); var lvMaxTwoWeeksMagnitude = GetMagnitudeVariable("MaxTwoWeeksMagnitude"); var lvMaxFiveYearsMagnitude = GetMagnitudeVariable("MaxFiveYearsMagnitude"); var lvMaxTenYearsMagnitude = GetMagnitudeVariable("MaxTenYearsMagnitude"); var lvMaxFiftyYearsMagnitude = GetMagnitudeVariable("MaxFiftyYearsMagnitude"); var lvMaxCenturyMagnitude = GetMagnitudeVariable("MaxCenturyMagnitude"); // Input Calculated biggest earthquake depth var lvMaxWeekDepth = GetDepthVariable("MaxWeekDepth"); var lvMaxTwoWeeksDepth = GetDepthVariable("MaxTwoWeeksDepth"); var lvMaxFiveYearsDepth = GetDepthVariable("MaxFiveYearsDepth"); var lvMaxTenYearsDepth = GetDepthVariable("MaxTenYearsDepth"); var lvMaxFiftyYearsDepth = GetDepthVariable("MaxFiftyYearsDepth"); var lvMaxCenturyDepth = GetDepthVariable("MaxCenturyDepth"); Database fuzzyDB = new Database(); fuzzyDB.AddVariable(lvDistance); fuzzyDB.AddVariable(lvDate); fuzzyDB.AddVariable(lvSeismicEnv); fuzzyDB.AddVariable(lvWeekSeismicEnv); fuzzyDB.AddVariable(lvMonthSeismicEnv); fuzzyDB.AddVariable(lvWeekConcentration); fuzzyDB.AddVariable(lvTwoWeeksConcentration); fuzzyDB.AddVariable(lvFiveYearsConcentration); fuzzyDB.AddVariable(lvTenYearsConcentration); fuzzyDB.AddVariable(lvFiftyYearsConcentration); fuzzyDB.AddVariable(lvCenturyConcentration); fuzzyDB.AddVariable(lvMaxWeekMagnitude); fuzzyDB.AddVariable(lvMaxTwoWeeksMagnitude); fuzzyDB.AddVariable(lvMaxFiveYearsMagnitude); fuzzyDB.AddVariable(lvMaxTenYearsMagnitude); fuzzyDB.AddVariable(lvMaxFiftyYearsMagnitude); fuzzyDB.AddVariable(lvMaxCenturyMagnitude); fuzzyDB.AddVariable(lvMaxWeekDepth); fuzzyDB.AddVariable(lvMaxTwoWeeksDepth); fuzzyDB.AddVariable(lvMaxFiveYearsDepth); fuzzyDB.AddVariable(lvMaxTenYearsDepth); fuzzyDB.AddVariable(lvMaxFiftyYearsDepth); fuzzyDB.AddVariable(lvMaxCenturyDepth); #endregion quakeSystem = new InferenceSystem(fuzzyDB, new CentroidDefuzzifier(1000), new MinimumNorm(), new MaximumCoNorm()); #region Construct rules // Current seismicity rules string nearAndSoonClause = "Distance IS Near AND (Date IS Week OR Date IS TwoWeeks)"; string tooStrongMagnitudeClause = "(MaxWeekMagnitude IS Great OR MaxWeekMagnitude IS Major)"; string strongMagnitudeClause = "MaxWeekMagnitude IS Strong"; string moderateMagnitudeClause = "MaxWeekMagnitude IS Moderate"; string lightMagnitudeClause = "MaxWeekMagnitude IS Light"; string belowLightMagnitudeClause = "(MaxWeekMagnitude IS Light OR MaxWeekMagnitude IS Minor OR MaxWeekMagnitude IS Micro)"; string shallowDepthClause = "MaxWeekDepth IS Shallow"; string notShallowDepthClause = "MaxWeekDepth IS NOT Shallow"; string fairlyDeepClause = "(MaxWeekDepth IS Fairly OR MaxWeekDepth is Deep)"; string veryDeepClause = "MaxWeekDepth IS VeryDeep"; // near events rules.Add(quakeSystem.NewRule("Rule 1", "IF " + nearAndSoonClause + " AND " + tooStrongMagnitudeClause + " THEN Seismicity IS Great")); rules.Add(quakeSystem.NewRule("Rule 2", "IF " + nearAndSoonClause + " AND " + strongMagnitudeClause + " AND " + shallowDepthClause + " THEN Seismicity is Great")); rules.Add(quakeSystem.NewRule("Rule 3", "IF " + nearAndSoonClause + " AND " + strongMagnitudeClause + " AND " + fairlyDeepClause + " THEN Seismicity is Strong")); rules.Add(quakeSystem.NewRule("Rule 4", "IF " + nearAndSoonClause + " AND " + strongMagnitudeClause + " AND " + veryDeepClause + " THEN Seismicity is Strong")); rules.Add(quakeSystem.NewRule("Rule 5", "IF " + nearAndSoonClause + " AND " + moderateMagnitudeClause + " AND " + shallowDepthClause + " THEN Seismicity is Strong")); rules.Add(quakeSystem.NewRule("Rule 6", "IF " + nearAndSoonClause + " AND " + moderateMagnitudeClause + " AND " + notShallowDepthClause + " THEN Seismicity is Medium")); rules.Add(quakeSystem.NewRule("Rule 7", "IF " + nearAndSoonClause + " AND " + lightMagnitudeClause + " AND " + shallowDepthClause + " THEN Seismicity is Medium")); rules.Add(quakeSystem.NewRule("Rule 8", "IF " + nearAndSoonClause + " AND " + belowLightMagnitudeClause + " AND " + notShallowDepthClause + " THEN Seismicity is Low")); // far events string fairAndSoonClause = "Distance IS Fair AND (Date IS Week OR Date is TwoWeeks)"; rules.Add(quakeSystem.NewRule("Rule 9", "IF " + fairAndSoonClause + " AND " + tooStrongMagnitudeClause + " THEN Seismicity IS Great")); rules.Add(quakeSystem.NewRule("Rule 10", "IF " + fairAndSoonClause + " AND " + strongMagnitudeClause + " AND " + shallowDepthClause + " THEN Seismicity IS Strong")); rules.Add(quakeSystem.NewRule("Rule 11", "IF " + fairAndSoonClause + " AND " + strongMagnitudeClause + " AND " + fairlyDeepClause + " THEN Seismicity IS Strong")); rules.Add(quakeSystem.NewRule("Rule 12", "IF " + fairAndSoonClause + " AND " + strongMagnitudeClause + " AND " + veryDeepClause + " THEN Seismicity IS Medium")); rules.Add(quakeSystem.NewRule("Rule 13", "IF " + fairAndSoonClause + " AND " + moderateMagnitudeClause + " AND " + shallowDepthClause + " THEN Seismicity IS Medium")); rules.Add(quakeSystem.NewRule("Rule 14", "IF " + fairAndSoonClause + " AND " + moderateMagnitudeClause + " AND " + notShallowDepthClause + " THEN Seismicity IS Low")); rules.Add(quakeSystem.NewRule("Rule 15", "IF " + fairAndSoonClause + " AND " + "(" + lightMagnitudeClause + " OR " + belowLightMagnitudeClause + ")" + " THEN Seismicity IS Low")); // Predict next week - subsiding string soonClause = "(Date IS Week OR Date IS TwoWeeks)"; rules.Add(quakeSystem.NewRule("Rule 16", "IF " + soonClause + " AND (WeekConcentration IS Big OR TwoWeeksConcentration IS Big) " + "AND (MaxWeekMagnitude IS Great OR MaxTwoWeeksMagnitude IS Great OR " + "MaxWeekMagnitude IS Major OR MaxTwoWeeksMagnitude IS Major)" + " THEN WeekSeismicity IS Strong")); rules.Add(quakeSystem.NewRule("Rule 17", "IF " + soonClause + " AND (WeekConcentration IS Big OR TwoWeeksConcentration IS Big) " + "AND (MaxWeekMagnitude IS Strong OR MaxTwoWeeksMagnitude IS Strong OR " + "MaxWeekMagnitude IS Moderate OR MaxTwoWeeksMagnitude IS Moderate)" + " THEN WeekSeismicity IS Medium")); rules.Add(quakeSystem.NewRule("Rule 18", "IF " + soonClause + " AND (WeekConcentration IS Big OR TwoWeeksConcentration IS Big) " + "AND (MaxWeekMagnitude IS Light OR MaxTwoWeeksMagnitude IS Light OR " + "MaxWeekMagnitude IS Minor OR MaxTwoWeeksMagnitude IS Minor OR " + "MaxWeekMagnitude IS Micro OR MaxTwoWeeksMagnitude IS Micro)" + " THEN WeekSeismicity IS Low")); // Predict next month string upToMonthClause = "(Date IS Week OR Date IS TwoWeeks OR Date IS Month)"; rules.Add(quakeSystem.NewRule("Rule 19", "IF " + upToMonthClause + " AND FiveYearsConcentration IS Medium AND TenYearsConcentration IS Medium " + "AND (MaxFiveYearsMagnitude IS Strong AND MaxTenYearsMagnitude IS Strong OR " + "MaxFiveYearsMagnitude IS Moderate AND MaxTenYearsMagnitude IS Moderate)" + " THEN MonthSeismicity IS Strong")); rules.Add(quakeSystem.NewRule("Rule 20", "IF " + upToMonthClause + " AND FiveYearsConcentration IS Medium AND TenYearsConcentration IS Medium " + "AND MaxFiveYearsMagnitude IS Light AND MaxTenYearsMagnitude IS Light" + " THEN MonthSeismicity IS Medium")); rules.Add(quakeSystem.NewRule("Rule 21", "IF " + upToMonthClause + " AND FiveYearsConcentration IS Medium AND TenYearsConcentration IS Medium " + "AND (MaxFiveYearsMagnitude IS Minor OR MaxFiveYearsMagnitude IS Micro) AND " + "(MaxTenYearsMagnitude IS Minor OR MaxTenYearsMagnitude IS Micro)" + " THEN MonthSeismicity IS Low")); string upToYearClause = "(Date IS Week OR Date IS TwoWeeks OR Date IS Month OR " + "Date IS ThreeMonths OR Date IS SixMonths OR Date IS Year)"; rules.Add(quakeSystem.NewRule("Rule 22", "IF " + upToYearClause + " AND FiftyYearsConcentration IS Small AND CenturyConcentration IS Small " + "AND (MaxFiftyYearsMagnitude IS Great AND MaxCenturyMagnitude IS Great OR " + "MaxFiftyYearsMagnitude IS Major AND MaxCenturyMagnitude IS Major)" + " THEN MonthSeismicity IS Great")); rules.Add(quakeSystem.NewRule("Rule 23", "IF " + upToYearClause + " AND NOT (MaxFiftyYearsMagnitude IS Great AND MaxCenturyMagnitude IS Great OR " + "MaxFiftyYearsMagnitude IS Major AND MaxCenturyMagnitude IS Major)" + " THEN MonthSeismicity IS Low")); #endregion }
void InitEvenly() { const int numTerms = 10; const float cross = 1.8f; RMS0 = new LingVar("rms0", numTerms, 0.4f / 10000f, 23f / 10000f, cross); RMS1 = new LingVar("rms1", numTerms, 0.4f / 10000f, 23f / 10000f, cross); Deviation0 = new LingVar("deviation0", numTerms, 0.5f / 10000f, 34.26f / 10000f, cross); Deviation1 = new LingVar("deviation1", numTerms, 0.5f / 10000f, 34.26f / 10000f, cross); Turns0 = new LingVar("turns0", numTerms, 0, 175, cross); Turns1 = new LingVar("turns1", numTerms, 0, 175, cross); Zeros0 = new LingVar("zeros0", numTerms, 14, 80, cross); Zeros1 = new LingVar("zeros1", numTerms, 14, 80, cross); deltaRMS = new LingVar("deltarms", numTerms, -12.5f, 20.8f, cross); const int numOutTerms = 10; output = new LingVar("out", numOutTerms, 1, numOutTerms, 1.3f); fuzzyDB = new Database(); //fuzzyDB.AddVariable(RMS1.lv); //fuzzyDB.AddVariable(RMS2.lv); //fuzzyDB.AddVariable(Deviation1.lv); //fuzzyDB.AddVariable(Deviation2.lv); fuzzyDB.AddVariable(Turns0.lv); fuzzyDB.AddVariable(Turns1.lv); fuzzyDB.AddVariable(Zeros0.lv); fuzzyDB.AddVariable(Zeros1.lv); fuzzyDB.AddVariable(deltaRMS.lv); fuzzyDB.AddVariable(output.lv); RulesCalc = new List <float> [numTerms, numTerms, numTerms, numTerms, numTerms][]; Rules = new int[numTerms, numTerms, numTerms, numTerms, numTerms]; for (int q = 0; q < numTerms; q++) { for (int w = 0; w < numTerms; w++) { for (int e = 0; e < numTerms; e++) { for (int r = 0; r < numTerms; r++) { for (int t = 0; t < numTerms; t++) { RulesCalc[q, w, e, r, t] = new List <float>[numOutTerms] { new List <float>(), new List <float>(), new List <float>(), new List <float>(), new List <float>(), new List <float>(), new List <float>(), new List <float>(), new List <float>(), new List <float>() }; Rules[q, w, e, r, t] = 0; } } } } } loadData(); for (int q = 0; q < numTerms; q++) { for (int w = 0; w < numTerms; w++) { for (int e = 0; e < numTerms; e++) { for (int r = 0; r < numTerms; r++) { for (int t = 0; t < numTerms; t++) { float[] average = new float[numOutTerms]; // среднее арифметическое for (int i = 0; i < numOutTerms; i++) // количество листов (столбцов с цифрами) / количество выходных термов { int count = RulesCalc[q, w, e, r, t][i].Count; // количество цифр в столбце if (count == 0) // если столбец пуст { average[i] = 0; } else // если в столбце что-то есть { average[i] = RulesCalc[q, w, e, r, t][i].Average(); } } // посчитали среднее для всех столбцов, можно выбирать правило float _t = average.Max(); if (_t != 0) { Rules[q, w, e, r, t] = Array.IndexOf(average, _t) + 1; } else { Rules[q, w, e, r, t] = 0; } } } } } } #region Заполнение нулевых ячеек таблицы правил //bool zers = true, zer = false; //double countenv = (Math.Pow(3, 5) - 1); //float div = 2f; //int ZEROSAAAAA1 = 0; //int ZEROSAAAAA2 = 0; //while (zers) //{ // ZEROSAAAAA1 = 0; // for (int q = 0; q < numTerms; q++) // for (int w = 0; w < numTerms; w++) // for (int e = 0; e < numTerms; e++) // for (int r = 0; r < numTerms; r++) // for (int t = 0; t < numTerms; t++) // { // if (Rules[q, w, e, r, t] == 0) // { // ZEROSAAAAA1++; // int[] environs = new int[numOutTerms]; // int notclass = 0, inclass = 0; // int outofrange = 0; // zer = true; // int fuu = 0; // for (int a = -1; a <= 1; a++) // for (int s = -1; s <= 1; s++) // for (int d = -1; d <= 1; d++) // for (int f = -1; f <= 1; f++) // for (int g = -1; g <= 1; g++) // { // if ((q + a >= 0 && w + s >= 0 && e + d >= 0 && r + f >= 0 && t + g >= 0) && // (q + a < numTerms && w + s < numTerms && e + d < numTerms && r + f < numTerms && t + g < numTerms)) // { // if (!(q == a && w == s && e == d && r == f && t == g)) // { // if (Rules[q + a, w + s, e + d, r + f, t + g] == 0) // notclass++; // else // { // environs[Rules[q + a, w + s, e + d, r + f, t + g] - 1]++; // inclass++; // } // } // } // else // { // int fu = 0; // if ((q + a < 0) || (q + a >= numTerms)) // { // fu++; // } // if ((w + s < 0) || (w + s >= numTerms)) // { // fu++; // } // if ((e + d < 0) || (e + d >= numTerms)) // { // fu++; // } // if ((r + f < 0) || (r + f >= numTerms)) // { // fu++; // } // if ((t + g < 0) || (t + g >= numTerms)) // { // fu++; // } // if (fu > fuu) fuu = fu; // outofrange++; // } // } // int max = environs.Max(); // if ((fuu == 0) && (max != 0)) // { // if (inclass > 242f / div) // { // Rules[q, w, e, r, t] = Array.IndexOf(environs, max) + 1; // } // } // else if (fuu == 1) // { // if (inclass > 161f / div) // { // Rules[q, w, e, r, t] = Array.IndexOf(environs, max) + 1; // } // } // else if (fuu == 2) // { // if (notclass > 107f / div) // { // Rules[q, w, e, r, t] = Array.IndexOf(environs, max) + 1; // } // } // else if (fuu == 3) // { // if (notclass > 71f / div) // { // Rules[q, w, e, r, t] = Array.IndexOf(environs, max) + 1; // } // } // else if (fuu == 4) // { // if (notclass > 47f / div) // { // Rules[q, w, e, r, t] = Array.IndexOf(environs, max) + 1; // } // } // else if (fuu == 5) // { // if (notclass > 31f / div) // { // Rules[q, w, e, r, t] = Array.IndexOf(environs, max) + 1; // } // } // } // } // if (!zer) zers = false; // zer = false; // if (ZEROSAAAAA1 == ZEROSAAAAA2) // zers = false; // else // ZEROSAAAAA2 = ZEROSAAAAA1; // Console.WriteLine(ZEROSAAAAA1); //} #endregion #region Оптимизация правил //RulesOptimizer[] rulOpt = new RulesOptimizer[numOutTerms]; //for (int i = 0; i < rulOpt.Length; i++) //{ // rulOpt[i] = new RulesOptimizer(); //} //for (int q = 0; q < numTerms; q++) // for (int w = 0; w < numTerms; w++) // for (int e = 0; e < numTerms; e++) // for (int r = 0; r < numTerms; r++) // for (int t = 0; t < numTerms; t++) // { // if (Rules[q, w, e, r, t] != 0) // { // rulOpt[Rules[q, w, e, r, t] - 1].addRule(q, w, e, r, t); // } // } //foreach (var item in rulOpt) //{ // item.optimize(); //} #endregion /* Формат правил * * лист * \/ * Прi = | 1 | 1 | 1 | 2 | 1 | <- массив листов * | 5 | 3 | | 4 | | * | | 6 | | | | * * rules[i] - List<int>[] * rules[i][k] - List<int> * rules[i][k][n] - int */ IS = new InferenceSystem(fuzzyDB, new CentroidDefuzzifier(CentrDefu)); string rule; for (int q = 0; q < numTerms; q++) { for (int w = 0; w < numTerms; w++) { for (int e = 0; e < numTerms; e++) { for (int r = 0; r < numTerms; r++) { for (int t = 0; t < numTerms; t++) { if (Rules[q, w, e, r, t] != 0) { rule = ""; rule += "IF turns0 IS " + Turns0.terms[q].Name; rule += " AND turns1 IS " + Turns1.terms[w].Name; rule += " AND zeros0 IS " + Zeros0.terms[e].Name; rule += " AND zeros1 IS " + Zeros1.terms[r].Name; rule += " AND deltarms IS " + deltaRMS.terms[t].Name; rule += " THEN out IS " + output.terms[Rules[q, w, e, r, t] - 1].Name; IS.NewRule(q.ToString() + w.ToString() + e.ToString() + r.ToString() + t.ToString(), rule); } } } } } } #region Тестирование для оптимизации //string rule = ""; //for (int q = 0; q < rulOpt.Length; q++) // перечисляет выходы от 0 до 9 //{ // for (int w = 0; w < rulOpt[q].Rules.Count; w++) // перебирает правила для каждого выхода // { // Clause[][] clauses = new Clause[rulOpt[q].Rules[w].Length][]; // размерность 5 х N // for (int e = 0; e < rulOpt[q].Rules[w].Length; e++) // от 0 до 4 перебирает лингвистические переменные // { // clauses[e] = new Clause[rulOpt[q].Rules[w][e].Count]; // кол-во клауз = кол-ву вариантов значений лингвистических переменных // for (int r = 0; r < clauses[e].Length; r++) // { // int itemp = rulOpt[q].Rules[w][e][r]; // switch (e) // { // case 0: // clauses[e][r] = new Clause(Turns0.lv, Turns0.terms[itemp]); // break; // case 1: // clauses[e][r] = new Clause(Turns1.lv, Turns1.terms[itemp]); // break; // case 2: // clauses[e][r] = new Clause(Zeros0.lv, Zeros0.terms[itemp]); // break; // case 3: // clauses[e][r] = new Clause(Zeros1.lv, Zeros1.terms[itemp]); // break; // case 4: // clauses[e][r] = new Clause(deltaRMS.lv, deltaRMS.terms[itemp]); // break; // } // } // } // rule = "IF "; // string temp; // for (int e = 0; e < clauses.Length; e++) // { // temp = "("; // for (int r = 0; r < clauses[e].Length - 1; r++) // { // temp += clauses[e][r] + " OR "; // } // temp += clauses[e][clauses[e].Length - 1] + ")"; // if (e == clauses.Length - 1) // rule += temp; // else // rule += temp + " AND "; // } // rule += " THEN out IS " + output.terms[q].Name; // //rule += "IF turns0 IS " + Turns0.terms[q].Name; // //rule += " AND turns1 IS " + Turns1.terms[w].Name; // //rule += " AND zeros0 IS " + Zeros0.terms[e].Name; // //rule += " AND zeros1 IS " + Zeros1.terms[r].Name; // //rule += " AND deltarms IS " + deltaRMS.terms[t].Name; // //rule += " THEN out IS " + output.terms[Rules[q, w, e, r, t] - 1].Name; // IS.NewRule(q.ToString() + w.ToString(), rule); // } //} #endregion }
void InitFuzzyEngine() { //Sunciones de Pertenencia //Alturas FuzzySet fsBaja = new FuzzySet("Baja", new TrapezoidalFunction(-1f, 0f, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsMedia = new FuzzySet("Media", new TrapezoidalFunction(-1f, -0.1f, 0.1f, 1f)); FuzzySet fsAlta = new FuzzySet("Alta", new TrapezoidalFunction(0f, 1f, TrapezoidalFunction.EdgeType.Left)); //Velocidades FuzzySet fsMn = new FuzzySet("Negativa", new TrapezoidalFunction(-1f, -0.5f, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsZero = new FuzzySet("Media", new TrapezoidalFunction(-1f, -0.5f, 0.5f, 1f)); FuzzySet fsMp = new FuzzySet("Positiva", new TrapezoidalFunction(0.5f, 1f, TrapezoidalFunction.EdgeType.Left)); //Fuerzas FuzzySet fsZf = new FuzzySet("PocaFuerza", new TrapezoidalFunction(0f, 1f, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsPf = new FuzzySet("ZeroFuerza", new TrapezoidalFunction(0.5f, 1f, 9f, 10f)); FuzzySet fsMf = new FuzzySet("MuchaFuerza", new TrapezoidalFunction(9.5f, 16f, TrapezoidalFunction.EdgeType.Left)); //Entradas //Altura LinguisticVariable lvAltura = new LinguisticVariable("Altura", -15, 15); lvAltura.AddLabel(fsBaja); lvAltura.AddLabel(fsMedia); lvAltura.AddLabel(fsAlta); //Velocidades LinguisticVariable lvVelocidad = new LinguisticVariable("Velocidad", -25, 25); lvVelocidad.AddLabel(fsMn); lvVelocidad.AddLabel(fsZero); lvVelocidad.AddLabel(fsMp); //Fuerzas LinguisticVariable lvFuerza = new LinguisticVariable("Fuerza", 0, 16); lvFuerza.AddLabel(fsPf); lvFuerza.AddLabel(fsZf); lvFuerza.AddLabel(fsMf); //La base de datos Database fuzzyDb = new Database(); fuzzyDb.AddVariable(lvAltura); fuzzyDb.AddVariable(lvFuerza); fuzzyDb.AddVariable(lvVelocidad); //Sistema de interferencia ISL = new InferenceSystem(fuzzyDb, new CentroidDefuzzifier(1000)); ISL.NewRule("Rule 1", "IF Altura IS Baja AND Velocidad IS Negativa THEN Fuerza IS MuchaFuerza"); ISL.NewRule("Rule 2", "IF Altura IS Baja AND Velocidad IS Media THEN Fuerza IS MuchaFuerza"); ISL.NewRule("Rule 3", "IF Altura IS Baja AND Velocidad IS Positiva THEN Fuerza IS MuchaFuerza"); ISL.NewRule("Rule 4", "IF Altura IS Media AND Velocidad IS Negativa THEN Fuerza IS MuchaFuerza"); ISL.NewRule("Rule 5", "IF Altura IS Media AND Velocidad IS Media THEN Fuerza IS ZeroFuerza"); ISL.NewRule("Rule 6", "IF Altura IS Media AND Velocidad IS Positiva THEN Fuerza IS ZeroFuerza"); ISL.NewRule("Rule 7", "IF Altura IS Alta AND Velocidad IS Negativa THEN Fuerza IS ZeroFuerza"); ISL.NewRule("Rule 8", "IF Altura IS Alta AND Velocidad IS Media THEN Fuerza IS ZeroFuerza"); ISL.NewRule("Rule 9", "IF Altura IS Alta AND Velocidad IS Positiva THEN Fuerza IS ZeroFuerza"); }
void loadData() { openFileDialog1.InitialDirectory = @"D:\YandexDisk\Программирование\GestureRecognize\GestureRecognize\bin\Debug"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { BinaryFormatter bf = new BinaryFormatter(); using (FileStream fs = File.OpenRead(openFileDialog1.FileName)) { data = (Data[, ])bf.Deserialize(fs); } } int numFeatures = 5, _num, k, numSamples = 5000; double threshold = 1.3d; // пороговое значение для отношения амплитуд int width = data[nums[0], 0].X.Count; int[] addr = new int[numFeatures]; string[] forRule = new string[numFeatures + 1]; for (int n = 0; n < nums.Length; n++) { k = nums[n]; for (int i = 0; i < numSamples; i++) { _num = 700 + ((width - 700 - 300) / numSamples) * i; // перебор точек 700 - 20000 double r0 = data[k, 0].RMS[_num], r1 = data[k, 1].RMS[_num], dr; if ((r0 >= r1) && (r0 / r1 >= threshold)) { dr = r0 / r1; } else if ((r0 < r1) && (r1 / r0 >= threshold)) { dr = -r1 / r0; } else { dr = 0; } //dr = r0 / r1; forRule[0] = chooseTerm(data[k, 0].Turns[_num], Turns0); forRule[1] = chooseTerm(data[k, 1].Turns[_num], Turns1); forRule[2] = chooseTerm(data[k, 0].ZeroCrossings[_num], Zeros0); forRule[3] = chooseTerm(data[k, 1].ZeroCrossings[_num], Zeros1); forRule[4] = chooseTerm(dr, deltaRMS); forRule[5] = chooseTerm(n + 1, output); IS = new InferenceSystem(fuzzyDB, new CentroidDefuzzifier(CentrDefu)); string rule = ""; rule += "IF turns0 IS " + forRule[0]; rule += " AND turns1 IS " + forRule[1]; rule += " AND zeros0 IS " + forRule[2]; rule += " AND zeros1 IS " + forRule[3]; rule += " AND deltarms IS " + forRule[4]; rule += " THEN out IS " + forRule[5]; IS.NewRule("test", rule); IS.SetInput(Turns0.lv.Name, data[k, 0].Turns[_num]); IS.SetInput(Turns1.lv.Name, data[k, 1].Turns[_num]); IS.SetInput(Zeros0.lv.Name, data[k, 0].ZeroCrossings[_num]); IS.SetInput(Zeros1.lv.Name, data[k, 1].ZeroCrossings[_num]); IS.SetInput(deltaRMS.lv.Name, (float)dr); FuzzyOutput fuzzyOutput = IS.ExecuteInference("out"); for (int j = 0; j < addr.Length; j++) { addr[j] = int.Parse(forRule[j].Substring(forRule[j].Length - 1)); } RulesCalc[addr[0], addr[1], addr[2], addr[3], addr[4]][n].Add(fuzzyOutput.OutputList[0].FiringStrength); } } }
/// <summary> /// Premier traitement en logique flou permettant d'obtenir les variables Valence et Arousal pour toute liste mesurée /// </summary> /// <param name="EMG"></param> /// <param name="ECG"></param> /// <param name="EDA"></param> public void ToValenceAndArousal(List <double> EMG, List <double> ECG, List <double> EDA, List <double> Valence, List <double> Arousal) { ResLogFlou1Label.Text = "Traitement en cours"; ResLogFlou1Label.Refresh(); //Pour traitement adapté à chaque signal int meanEMG = Mean(EMG); int meanHR = Mean(ECG); int meanGSR = Mean(EDA); int sEMG = (int)Math.Sqrt(Var(EMG)); int sHR = (int)Math.Sqrt(Var(ECG)); int sGSR = (int)Math.Sqrt(Var(EDA)); #region Input (EMG) var lvEMG = new LinguisticVariable("EMG", meanEMG - 10 * sEMG, meanEMG + 10 * sEMG); var fsLowEMG = new FuzzySet("Low", new TrapezoidalFunction(meanEMG - 10 * sEMG, meanEMG - 5 * sEMG, meanEMG - 2 * sEMG)); var fsMidEMG = new FuzzySet("Mid", new TrapezoidalFunction(meanEMG - 2 * sEMG, meanEMG, meanEMG + 2 * sEMG)); var fsHighEMG = new FuzzySet("High", new TrapezoidalFunction(meanEMG + 2 * sEMG, meanEMG + 5 * sEMG, meanEMG + 10 * sEMG)); lvEMG.AddLabel(fsLowEMG); lvEMG.AddLabel(fsMidEMG); lvEMG.AddLabel(fsHighEMG); #endregion #region Input (EDA/GSR) var lvGSR = new LinguisticVariable("GSR", meanGSR - 10 * sGSR, meanGSR + 10 * sGSR); var fsLowGSR = new FuzzySet("Low", new TrapezoidalFunction(meanGSR - 10 * sGSR, meanGSR - 5 * sGSR, meanGSR - 2 * sGSR)); var fsMidLowGSR = new FuzzySet("MidLow", new TrapezoidalFunction(meanGSR - 2 * sGSR, meanGSR, meanGSR + 2 * sGSR)); var fsMidGSR = new FuzzySet("Mid", new TrapezoidalFunction(meanGSR - 2 * sGSR, meanGSR, meanGSR + 2 * sGSR)); var fsMidHighGSR = new FuzzySet("MidHigh", new TrapezoidalFunction(meanGSR - 2 * sGSR, meanGSR, meanGSR + 2 * sGSR)); var fsHighGSR = new FuzzySet("High", new TrapezoidalFunction(meanGSR - 2 * sGSR, meanGSR, meanGSR + 10 * sGSR)); lvGSR.AddLabel(fsLowGSR); lvGSR.AddLabel(fsMidLowGSR); lvGSR.AddLabel(fsMidGSR); lvGSR.AddLabel(fsMidHighGSR); lvGSR.AddLabel(fsHighGSR); #endregion #region Input (ECG/HR) var lvHR = new LinguisticVariable("HR", meanHR - 10 * sHR, meanHR + 10 * sHR); var fsLowHR = new FuzzySet("Low", new TrapezoidalFunction(meanHR - 10 * sHR, meanHR - 5 * sHR, meanHR - 2 * sHR)); var fsMidHR = new FuzzySet("Mid", new TrapezoidalFunction(meanHR - 2 * sHR, meanHR, meanHR + 2 * sHR)); var fsHighHR = new FuzzySet("High", new TrapezoidalFunction(meanHR + 2 * sHR, meanHR + 5 * sHR, meanHR + 10 * sHR)); lvHR.AddLabel(fsLowHR); lvHR.AddLabel(fsMidHR); lvHR.AddLabel(fsHighHR); #endregion #region Output (Valence) var lvValence = new LinguisticVariable("Valence", 0, 30); var fsVeryLow = new FuzzySet("VeryLow", new TrapezoidalFunction(0, 3, 6)); var fsLow = new FuzzySet("Low", new TrapezoidalFunction(6, 7, 8)); var fsMidLow = new FuzzySet("MidLow", new TrapezoidalFunction(8, 9, 10)); var fsMid = new FuzzySet("Mid", new TrapezoidalFunction(10, 15, 20)); var fsMidHigh = new FuzzySet("MidHigh", new TrapezoidalFunction(20, 22, 24)); var fsHigh = new FuzzySet("High", new TrapezoidalFunction(24, 25, 26)); var fsVeryHigh = new FuzzySet("VeryHigh", new TrapezoidalFunction(26, 28, 30)); lvValence.AddLabel(fsVeryLow); lvValence.AddLabel(fsLow); lvValence.AddLabel(fsMidLow); lvValence.AddLabel(fsMid); lvValence.AddLabel(fsMidHigh); lvValence.AddLabel(fsHigh); lvValence.AddLabel(fsVeryHigh); #endregion #region Output (Arousal) var lvArousal = new LinguisticVariable("Arousal", 0, 30); lvArousal.AddLabel(fsVeryLow); lvArousal.AddLabel(fsLow); lvArousal.AddLabel(fsMidLow); lvArousal.AddLabel(fsMid); lvArousal.AddLabel(fsMidHigh); lvArousal.AddLabel(fsHigh); lvArousal.AddLabel(fsVeryHigh); #endregion #region Système Inference // Base de données pour les variables linguistiques //Liste des inputs : // EMG(Low, Mid, High) 0 - 30 // GSR(Low, Mid, High) 0 - 30 // HR (Low, Mid, High) 0 - 30 //Liste des outputs : // Valence(VeryLow, Low, MidLow, Mid, MidHigh, High, VeryHigh) 0 - 30 // Arousal(VeryLow, Low, MidLow, Mid, MidHigh, High, VeryHigh) 0 - 30 var fuzzyDb = new Database(); fuzzyDb.AddVariable(lvEMG); fuzzyDb.AddVariable(lvGSR); fuzzyDb.AddVariable(lvHR); fuzzyDb.AddVariable(lvValence); fuzzyDb.AddVariable(lvArousal); // Creation system inference // Initialise la methode de défuzzification : centre de gravité var inferenceSys = new InferenceSystem(fuzzyDb, new CentroidDefuzzifier(1000)); // Ajout des regles inferenceSys.NewRule("Rule 1", "IF GSR IS High THEN Arousal IS High"); inferenceSys.NewRule("Rule 2", "IF GSR IS MidHigh THEN Arousal IS MidHigh "); inferenceSys.NewRule("Rule 3", "IF GSR IS MidLow THEN Arousal IS MidLow"); inferenceSys.NewRule("Rule 4", "IF GSR IS Low THEN Arousal IS Low"); inferenceSys.NewRule("Rule 5", "IF HR IS Low THEN Arousal IS Low"); inferenceSys.NewRule("Rule 6", "IF HR IS High THEN Arousal IS High"); inferenceSys.NewRule("Rule 7", "IF GSR IS Low AND HR IS High THEN Arousal IS MidLow"); inferenceSys.NewRule("Rule 8", "IF GSR IS High AND HR IS Low THEN Arousal IS MidHigh"); inferenceSys.NewRule("Rule 9", "IF EMG IS High THEN Valence IS VeryLow"); inferenceSys.NewRule("Rule 10", "IF EMG IS Mid THEN Valence IS Low"); //inferenceSys.NewRule("Rule 11", "IF EMGsmile IS Mid THEN Valence IS High"); //inferenceSys.NewRule("Rule 12", "IF EMGsmile IS High THEN Valence IS very High"); inferenceSys.NewRule("Rule 14", "IF EMG IS Low THEN Valence IS VeryHigh"); inferenceSys.NewRule("Rule 15", "IF EMG IS Mid THEN Valence IS High"); inferenceSys.NewRule("Rule 16", "IF EMG IS High THEN Valence IS VeryLow"); inferenceSys.NewRule("Rule 17", "IF EMG IS High THEN Valence IS Low"); inferenceSys.NewRule("Rule 18", "IF EMG IS Low AND HR IS Low THEN Valence IS Low"); inferenceSys.NewRule("Rule 19", "IF EMG IS Low AND HR IS High THEN Valence IS High"); inferenceSys.NewRule("Rule 20", "IF GSR IS High AND HR IS Mid THEN Arousal IS High"); inferenceSys.NewRule("Rule 21", "IF GSR IS MidHigh AND HR IS Mid THEN Arousal IS MidHigh"); inferenceSys.NewRule("Rule 22", "IF GSR IS MidLow AND HR IS Mid THEN Arousal IS MidLow"); #endregion #region Exemple //Pour toutes les valeurs des listes float valEMG = -1, valGSR = -1, valHR = -1; for (int i = 0; i < EMG.Count; i++) { valEMG = (float)EMG[i]; valGSR = (float)EDA[i]; valHR = (float)ECG[i]; // Initialise les données d'entrées inferenceSys.SetInput("EMG", valEMG); inferenceSys.SetInput("GSR", valGSR); inferenceSys.SetInput("HR", valHR); // Evalue les données de sortie : Valence, Arousal var resValence = -1f; var resArousal = -1f; try { resValence = inferenceSys.Evaluate("Valence"); resArousal = inferenceSys.Evaluate("Arousal"); ResLogFlou1Label.Text = "" + i + "/" + EMG.Count + "_ EMG: " + valEMG + " + GSR: " + valGSR + " + HR: " + valHR + " = Valence: " + resValence + ", Arousal: " + resArousal; ResLogFlou1Label.Refresh(); //Stockage des résultats dans la liste adéquate au résultat Valence.Add((double)resValence); Arousal.Add((double)resArousal); } catch (Exception ex) { //throw new Exception(string.Format("Erreur : {0}", ex.Message)); Valence.Add(0); Arousal.Add(0); } } #endregion }
public static void Main() { // linguistic labels (fuzzy sets) that compose the distances var fsNear = new FuzzySet("Near", new TrapezoidalFunction( 450, 500, TrapezoidalFunction.EdgeType.Left)); var fsMedium = new FuzzySet("Medium", new TrapezoidalFunction( 100, 250, 460, 500)); var fsFar = new FuzzySet("Far", new TrapezoidalFunction( 0, 250, TrapezoidalFunction.EdgeType.Right)); // right Distance (Input) var lvDist = new LinguisticVariable("Distance", 0, 500); lvDist.AddLabel(fsNear); lvDist.AddLabel(fsMedium); lvDist.AddLabel(fsFar); // linguistic labels (fuzzy sets) that compose the angle var fsVeryDownSpeed = new FuzzySet("VeryNegative", new TrapezoidalFunction( -100, -10, TrapezoidalFunction.EdgeType.Right)); var fsDownSpeed = new FuzzySet("Negative", new TrapezoidalFunction( -50, -30, -15, -10)); var fsLittleDownSpeed = new FuzzySet("LittleNegative", new TrapezoidalFunction( -15, -8, -5, -1)); var fsZero = new FuzzySet("Zero", new TrapezoidalFunction( -10, 0, 5, 10)); var fsLittleUpSpeed = new FuzzySet("LittlePositive", new TrapezoidalFunction( 0, 5, 20, 30)); var fsUpSpeed = new FuzzySet("Positive", new TrapezoidalFunction( 20, 25, 35, 40)); var fsVeryUpSpeed = new FuzzySet("VeryPositive", new TrapezoidalFunction( 35, 100, TrapezoidalFunction.EdgeType.Left)); // angle var lvSpeed = new LinguisticVariable("Speed", -100, 100); lvSpeed.AddLabel(fsVeryDownSpeed); lvSpeed.AddLabel(fsDownSpeed); lvSpeed.AddLabel(fsLittleDownSpeed); lvSpeed.AddLabel(fsZero); lvSpeed.AddLabel(fsLittleUpSpeed); lvSpeed.AddLabel(fsUpSpeed); lvSpeed.AddLabel(fsVeryUpSpeed); // the database var fuzzyDb = new Database( ); fuzzyDb.AddVariable(lvDist); fuzzyDb.AddVariable(lvSpeed); // creating the inference system var IS = new InferenceSystem(fuzzyDb, new CentroidDefuzzifier(1000)); // going Straight IS.NewRule("Rule 1", "IF Distance IS Far THEN Speed IS VeryPositive"); // going Straight (if can go anywhere) IS.NewRule("Rule 2", "IF Distance IS Medium THEN Speed IS Positive"); // near right wall IS.NewRule("Rule 3", "IF Distance IS Near THEN Speed IS Negative"); // near left wall IS.SetInput("Distance", 0); /*try * { * double NewSpeed = IS.Evaluate( "Speed" ); * Console.WriteLine("Speed = {0} ", NewSpeed ); * } * catch ( Exception ) * { * Console.WriteLine("Speed not s"); * } * Console.ReadLine(); */ double curDist = 0; double newSpeed = 0; while (curDist < 500) { newSpeed = IS.Evaluate("Speed"); curDist += 2 * newSpeed / 3600; Console.WriteLine("Speed = {0}, position = {1} ", newSpeed, curDist); IS.SetInput("Distance", (float)(curDist)); //Console.ReadLine(); } Console.ReadLine(); }
private void Form1_Load(object sender, EventArgs e) { // rms FuzzySet fsLowRMS = new FuzzySet("Low", new TrapezoidalFunction( (float)RMSmin, Divide(RMSmin, RMSmax, 3, 1), TrapezoidalFunction.EdgeType.Right)); FuzzySet fsAverageRMS = new FuzzySet("Average", new TrapezoidalFunction( (float)RMSmin, Divide(RMSmin, RMSmax, 3, 1), Divide(RMSmin, RMSmax, 3, 2), (float)RMSmax)); FuzzySet fsHighRMS = new FuzzySet("High", new TrapezoidalFunction( Divide(RMSmin, RMSmax, 3, 2), (float)RMSmax, TrapezoidalFunction.EdgeType.Left)); // повороты FuzzySet fsLowTurns = new FuzzySet("Low", new TrapezoidalFunction( turnsMin, Divide(turnsMin, turnsMax, 3, 1), TrapezoidalFunction.EdgeType.Right)); FuzzySet fsAverageTurns = new FuzzySet("Average", new TrapezoidalFunction( turnsMin, Divide(turnsMin, turnsMax, 3, 1), Divide(turnsMin, turnsMax, 3, 2), turnsMax)); FuzzySet fsHighTurns = new FuzzySet("High", new TrapezoidalFunction( Divide(turnsMin, turnsMax, 3, 2), turnsMax, TrapezoidalFunction.EdgeType.Left)); LinguisticVariable lvRMS = new LinguisticVariable("RMS", (float)RMSmin, (float)RMSmax); lvRMS.AddLabel(fsLowRMS); lvRMS.AddLabel(fsAverageRMS); lvRMS.AddLabel(fsHighRMS); LinguisticVariable lvTurns = new LinguisticVariable("Turns", turnsMin, turnsMax); lvTurns.AddLabel(fsLowTurns); lvTurns.AddLabel(fsAverageTurns); lvTurns.AddLabel(fsHighTurns); // выход FuzzySet fsСlonus = new FuzzySet("True", new SingletonFunction(1)); FuzzySet fsClonusNot = new FuzzySet("False", new SingletonFunction(0)); LinguisticVariable lvClonus = new LinguisticVariable("Clonus", 0, 1); lvClonus.AddLabel(fsСlonus); lvClonus.AddLabel(fsClonusNot); Database fuzzyDB = new Database(); fuzzyDB.AddVariable(lvClonus); fuzzyDB.AddVariable(lvRMS); fuzzyDB.AddVariable(lvTurns); InferenceSystem IS = new InferenceSystem(fuzzyDB, new CentroidDefuzzifier(1000)); IS.NewRule("Rule 1", "IF RMS IS Low OR Turns IS Low THEN Clonus IS False"); IS.NewRule("Rule 2", "IF RMS IS High AND Turns IS High THEN Clonus IS True"); }
static void Main(string[] args) { InferenceSystem IS; int estatura = 0, simpatia = 0, inteligencia = 0, complexion = 0, tez = 0, bellezaE; float belleza = 0; string bellezaR = ""; FuzzySet fsBajo = new FuzzySet("Bajo", new TrapezoidalFunction(140, 155, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsMedio = new FuzzySet("Medio", new TrapezoidalFunction(160, 170, 175)); FuzzySet fsAlto = new FuzzySet("Alto", new TrapezoidalFunction(180, 190, TrapezoidalFunction.EdgeType.Left)); LinguisticVariable lvEstatura = new LinguisticVariable("Estatura", 140, 190); lvEstatura.AddLabel(fsBajo); lvEstatura.AddLabel(fsMedio); lvEstatura.AddLabel(fsAlto); FuzzySet fsMalGenio = new FuzzySet("MalGenio", new TrapezoidalFunction(0, 3, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsNeutro = new FuzzySet("Neutro", new TrapezoidalFunction(4, 5, 6)); FuzzySet fsAgradable = new FuzzySet("Agradable", new TrapezoidalFunction(7, 10, TrapezoidalFunction.EdgeType.Left)); LinguisticVariable lvSimpatia = new LinguisticVariable("Simpatia", 0, 10); lvSimpatia.AddLabel(fsMalGenio); lvSimpatia.AddLabel(fsNeutro); lvSimpatia.AddLabel(fsAgradable); FuzzySet fsPocaIntel = new FuzzySet("PocoInteligente", new TrapezoidalFunction(0, 3, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsNeutraIntel = new FuzzySet("InteligenciaNeutra", new TrapezoidalFunction(4, 5, 6)); FuzzySet fsAltaIntel = new FuzzySet("MuyInteligente", new TrapezoidalFunction(7, 10, TrapezoidalFunction.EdgeType.Left)); LinguisticVariable lvInteligencia = new LinguisticVariable("Inteligencia", 0, 10); lvInteligencia.AddLabel(fsPocaIntel); lvInteligencia.AddLabel(fsNeutraIntel); lvInteligencia.AddLabel(fsAltaIntel); FuzzySet fsComplexionBaja = new FuzzySet("ComplexionBaja", new TrapezoidalFunction(0, 3, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsComplexionNeutra = new FuzzySet("ComplexionNeutra", new TrapezoidalFunction(4, 5, 6)); FuzzySet fsComplexionGrande = new FuzzySet("ComplexionGrande", new TrapezoidalFunction(7, 10, TrapezoidalFunction.EdgeType.Left)); LinguisticVariable lvComplexion = new LinguisticVariable("Complexion", 0, 10); lvComplexion.AddLabel(fsComplexionBaja); lvComplexion.AddLabel(fsComplexionNeutra); lvComplexion.AddLabel(fsComplexionGrande); FuzzySet fsTezClara = new FuzzySet("TezClara", new TrapezoidalFunction(0, 3, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsTezNeutra = new FuzzySet("TezNeutra", new TrapezoidalFunction(4, 5, 6)); FuzzySet fsTezOscura = new FuzzySet("TezOscura", new TrapezoidalFunction(7, 10, TrapezoidalFunction.EdgeType.Left)); LinguisticVariable lvTez = new LinguisticVariable("Tez", 0, 10); lvTez.AddLabel(fsTezClara); lvTez.AddLabel(fsTezNeutra); lvTez.AddLabel(fsTezOscura); FuzzySet fsBellezaBaja = new FuzzySet("No_Chido", new TrapezoidalFunction(0, 3, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsBellezaNeutra = new FuzzySet("Chido", new TrapezoidalFunction(4, 5, 6)); FuzzySet fsBellezaAlta = new FuzzySet("Muy_Chido", new TrapezoidalFunction(7, 10, TrapezoidalFunction.EdgeType.Left)); LinguisticVariable lvBelleza = new LinguisticVariable("Belleza", 0, 10); lvBelleza.AddLabel(fsBellezaBaja); lvBelleza.AddLabel(fsBellezaNeutra); lvBelleza.AddLabel(fsBellezaAlta); Console.WriteLine("INGRESA LA ESTATURA (140 a 190 cm)"); estatura = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("INGRESA LA SIMPATIA (1=Mal Genio 10=Muy simpático)"); simpatia = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Ingresa la Inteligencia del 1 al 10 (No creo que deba explicarlo xd))"); inteligencia = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("¿Qué complexión tiene? 1=Nada en forma 10=En forma B)"); complexion = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Ingresa la Tez 1=Oscura 10=Blanco"); tez = Convert.ToInt32(Console.ReadLine()); Database fuzzyDB = new Database(); fuzzyDB.AddVariable(lvEstatura); fuzzyDB.AddVariable(lvSimpatia); fuzzyDB.AddVariable(lvInteligencia); fuzzyDB.AddVariable(lvComplexion); fuzzyDB.AddVariable(lvTez); fuzzyDB.AddVariable(lvBelleza); IS = new InferenceSystem(fuzzyDB, new CentroidDefuzzifier(1000)); IS.NewRule("Rule 1", "IF Estatura IS Alto THEN Belleza IS Muy_Chido"); IS.NewRule("Rule 2", "IF Simpatia IS Agradable THEN Belleza IS Muy_Chido"); IS.NewRule("Rule 3", "IF Inteligencia IS MuyInteligente THEN Belleza IS Muy_Chido"); IS.NewRule("Rule 4", "IF Complexion IS ComplexionGrande THEN Belleza IS Muy_Chido"); IS.NewRule("Rule 5", "IF Tez IS TezOscura THEN Belleza IS Muy_Chido"); IS.NewRule("Rule 6", "IF Estatura IS Medio THEN Belleza IS Chido"); IS.NewRule("Rule 7", "IF Simpatia IS Neutro THEN Belleza IS Chido"); IS.NewRule("Rule 8", "IF Inteligencia IS InteligenciaNeutra THEN Belleza IS Chido"); IS.NewRule("Rule 9", "IF Complexion IS ComplexionNeutra THEN Belleza IS Chido"); IS.NewRule("Rule 10", "IF Tez IS TezNeutra THEN Belleza IS Chido"); IS.NewRule("Rule 11", "IF Estatura IS Bajo THEN Belleza IS No_Chido"); IS.NewRule("Rule 12", "IF Simpatia IS MalGenio THEN Belleza IS No_Chido"); IS.NewRule("Rule 13", "IF Inteligencia IS PocoInteligente THEN Belleza IS No_Chido"); IS.NewRule("Rule 14", "IF Complexion IS ComplexionBaja THEN Belleza IS No_Chido"); IS.NewRule("Rule 15", "IF Tez IS TezClara THEN Belleza IS No_Chido"); IS.SetInput("Estatura", estatura); IS.SetInput("Simpatia", simpatia); IS.SetInput("Inteligencia", inteligencia); IS.SetInput("Complexion", complexion); IS.SetInput("Tez", tez); belleza = IS.Evaluate("Belleza"); //belleza.ToString("##0"); bellezaE = Convert.ToInt32(belleza.ToString("##0")); Console.WriteLine("*****************************************************"); Console.WriteLine(belleza); //Console.WriteLine(IS.SetInput("Belleza", belleza)); //Console.WriteLine(IS.GetLinguisticVariable("Belleza").GetLabel("Muy_Chido").Name); switch (bellezaE) { case int n when(n >= 0 && n <= 3): bellezaR = IS.GetLinguisticVariable("Belleza").GetLabel("No_Chido").Name.ToString(); break; case int n when(n >= 4 && n <= 6): bellezaR = IS.GetLinguisticVariable("Belleza").GetLabel("Chido").Name.ToString(); break; case int n when(n >= 7 && n <= 10): bellezaR = IS.GetLinguisticVariable("Belleza").GetLabel("Muy_Chido").Name.ToString(); break; } Console.WriteLine(bellezaR); }
void InitFuzzyEngine() { // Linguistic labels (fuzzy sets) para aciertos FuzzySet fsLow = new FuzzySet("Low", new TrapezoidalFunction(0.0, 0.33, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsNeutral = new FuzzySet("Normal", new TrapezoidalFunction(0.0, 0.33, 0.66, 1.00)); FuzzySet fsHigh = new FuzzySet("High", new TrapezoidalFunction(0.66, 1.00, TrapezoidalFunction.EdgeType.Left)); /* // Linguistic labels (fuzzy sets) para errores y ayuda * FuzzySet fsLowInv = new FuzzySet("Low", new TrapezoidalFunction(0.0, 0.33, TrapezoidalFunction.EdgeType.Right)); * FuzzySet fsHighInv = new FuzzySet("High", new TrapezoidalFunction(0.66, 1.00, TrapezoidalFunction.EdgeType.Left)); */ // Linguistic labels (fuzzy sets) para el tiempo FuzzySet fsFast = new FuzzySet("Fast", new TrapezoidalFunction(0, 0.33, TrapezoidalFunction.EdgeType.Left)); FuzzySet fsNormal = new FuzzySet("Normal", new TrapezoidalFunction(0, 0.33, 0.66, 1.00)); FuzzySet fsSlow = new FuzzySet("Slow", new TrapezoidalFunction(0.66, 1.00, TrapezoidalFunction.EdgeType.Right)); // Right Distance (Input) LinguisticVariable numAciertos = new LinguisticVariable("Aciertos", 0, 1); numAciertos.AddLabel(fsLow); numAciertos.AddLabel(fsNeutral); numAciertos.AddLabel(fsHigh); // Left Distance (Input) LinguisticVariable numErrores = new LinguisticVariable("Errores", 0, 1); numErrores.AddLabel(fsLow); numErrores.AddLabel(fsNeutral); numErrores.AddLabel(fsHigh); LinguisticVariable ayudas = new LinguisticVariable("Ayuda", 0, 1); ayudas.AddLabel(fsLow); ayudas.AddLabel(fsNeutral); ayudas.AddLabel(fsHigh); // Time (Input) LinguisticVariable tiempoSeg = new LinguisticVariable("Time", 0, 1); tiempoSeg.AddLabel(fsSlow); tiempoSeg.AddLabel(fsNormal); tiempoSeg.AddLabel(fsFast); // Linguistic labels (fuzzy sets) that compose Dificultad FuzzySet fsVeryWeak = new FuzzySet("VeryWeak", new TrapezoidalFunction(0.0, 0.20, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsWeak = new FuzzySet("Weak", new TrapezoidalFunction(0.0, 0.20, 0.35, 0.40)); FuzzySet fsBasico = new FuzzySet("Normal", new TrapezoidalFunction(0.35, 0.40, 0.55, 0.60)); FuzzySet fsGood = new FuzzySet("Good", new TrapezoidalFunction(0.55, 0.60, 0.75, 0.80)); FuzzySet fsGreat = new FuzzySet("Great", new TrapezoidalFunction(0.75, 0.80, TrapezoidalFunction.EdgeType.Left)); // AnglefsWonderFull lvDificultad = new LinguisticVariable("Dificultad", 0, 1.00); lvDificultad.AddLabel(fsVeryWeak); lvDificultad.AddLabel(fsWeak); lvDificultad.AddLabel(fsBasico); lvDificultad.AddLabel(fsGood); lvDificultad.AddLabel(fsGreat); // The database Database fuzzyDB = new Database(); fuzzyDB.AddVariable(numAciertos); fuzzyDB.AddVariable(numErrores); fuzzyDB.AddVariable(tiempoSeg); fuzzyDB.AddVariable(ayudas); fuzzyDB.AddVariable(lvDificultad); // Creating the inference system IS = new InferenceSystem(fuzzyDB, new CentroidDefuzzifier(1500)); IS.NewRule("RULE 1", "IF Time IS Fast AND Ayuda IS Low AND Errores IS Low AND Aciertos IS Low THEN Dificultad IS Weak"); IS.NewRule("RULE 2", "IF Time IS Fast AND Ayuda IS Low AND Errores IS Low AND Aciertos IS Normal THEN Dificultad IS Good"); IS.NewRule("RULE 3", "IF Time IS Fast AND Ayuda IS Low AND Errores IS Low AND Aciertos IS High THEN Dificultad IS Great"); IS.NewRule("RULE 4", "IF Time IS Fast AND Ayuda IS Low AND Errores IS Normal AND Aciertos IS Low THEN Dificultad IS Normal"); IS.NewRule("RULE 5", "IF Time IS Fast AND Ayuda IS Low AND Errores IS Normal AND Aciertos IS Normal THEN Dificultad IS Normal"); IS.NewRule("RULE 6", "IF Time IS Fast AND Ayuda IS Low AND Errores IS Normal AND Aciertos IS High THEN Dificultad IS Good"); IS.NewRule("RULE 7", "IF Time IS Fast AND Ayuda IS Low AND Errores IS High AND Aciertos IS Low THEN Dificultad IS Weak"); IS.NewRule("RULE 8", "IF Time IS Fast AND Ayuda IS Low AND Errores IS High AND Aciertos IS Normal THEN Dificultad IS Normal"); IS.NewRule("RULE 9", "IF Time IS Fast AND Ayuda IS Low AND Errores IS High AND Aciertos IS High THEN Dificultad IS Normal"); IS.NewRule("RULE 10", "IF Time IS Fast AND Ayuda IS Normal AND Errores IS Low AND Aciertos IS Low THEN Dificultad IS Weak"); IS.NewRule("RULE 11", "IF Time IS Fast AND Ayuda IS Normal AND Errores IS Low AND Aciertos IS Normal THEN Dificultad IS Normal"); IS.NewRule("RULE 12", "IF Time IS Fast AND Ayuda IS Normal AND Errores IS Low AND Aciertos IS High THEN Dificultad IS Great"); IS.NewRule("RULE 13", "IF Time IS Fast AND Ayuda IS Normal AND Errores IS Normal AND Aciertos IS Low THEN Dificultad IS Weak"); IS.NewRule("RULE 14", "IF Time IS Fast AND Ayuda IS Normal AND Errores IS Normal AND Aciertos IS Normal THEN Dificultad IS Good"); IS.NewRule("RULE 15", "IF Time IS Fast AND Ayuda IS Normal AND Errores IS Normal AND Aciertos IS High THEN Dificultad IS Good"); IS.NewRule("RULE 16", "IF Time IS Fast AND Ayuda IS Normal AND Errores IS High AND Aciertos IS Low THEN Dificultad IS Weak"); IS.NewRule("RULE 17", "IF Time IS Fast AND Ayuda IS Normal AND Errores IS High AND Aciertos IS Normal THEN Dificultad IS Weak"); IS.NewRule("RULE 18", "IF Time IS Fast AND Ayuda IS Normal AND Errores IS High AND Aciertos IS High THEN Dificultad IS Normal"); IS.NewRule("RULE 19", "IF Time IS Fast AND Ayuda IS High AND Errores IS Low AND Aciertos IS Low THEN Dificultad IS Weak"); IS.NewRule("RULE 20", "IF Time IS Fast AND Ayuda IS High AND Errores IS Low AND Aciertos IS Normal THEN Dificultad IS Normal"); IS.NewRule("RULE 21", "IF Time IS Fast AND Ayuda IS High AND Errores IS Low AND Aciertos IS High THEN Dificultad IS Good"); IS.NewRule("RULE 22", "IF Time IS Fast AND Ayuda IS High AND Errores IS Normal AND Aciertos IS Low THEN Dificultad IS Weak"); IS.NewRule("RULE 23", "IF Time IS Fast AND Ayuda IS High AND Errores IS Normal AND Aciertos IS Normal THEN Dificultad IS Normal"); IS.NewRule("RULE 24", "IF Time IS Fast AND Ayuda IS High AND Errores IS Normal AND Aciertos IS High THEN Dificultad IS Normal"); IS.NewRule("RULE 25", "IF Time IS Fast AND Ayuda IS High AND Errores IS High AND Aciertos IS Low THEN Dificultad IS Weak"); IS.NewRule("RULE 26", "IF Time IS Fast AND Ayuda IS High AND Errores IS High AND Aciertos IS Normal THEN Dificultad IS Weak"); IS.NewRule("RULE 27", "IF Time IS Fast AND Ayuda IS High AND Errores IS High AND Aciertos IS High THEN Dificultad IS Normal"); IS.NewRule("RULE 28", "IF Time IS Normal AND Ayuda IS Low AND Errores IS Low AND Aciertos IS Low THEN Dificultad IS Weak"); IS.NewRule("RULE 29", "IF Time IS Normal AND Ayuda IS Low AND Errores IS Low AND Aciertos IS Normal THEN Dificultad IS Normal"); IS.NewRule("RULE 30", "IF Time IS Normal AND Ayuda IS Low AND Errores IS Low AND Aciertos IS High THEN Dificultad IS Great"); IS.NewRule("RULE 31", "IF Time IS Normal AND Ayuda IS Low AND Errores IS Normal AND Aciertos IS Low THEN Dificultad IS Normal"); IS.NewRule("RULE 32", "IF Time IS Normal AND Ayuda IS Low AND Errores IS Normal AND Aciertos IS Normal THEN Dificultad IS Normal"); IS.NewRule("RULE 33", "IF Time IS Normal AND Ayuda IS Low AND Errores IS Normal AND Aciertos IS High THEN Dificultad IS Good"); IS.NewRule("RULE 34", "IF Time IS Normal AND Ayuda IS Low AND Errores IS High AND Aciertos IS Low THEN Dificultad IS Weak"); IS.NewRule("RULE 35", "IF Time IS Normal AND Ayuda IS Low AND Errores IS High AND Aciertos IS Normal THEN Dificultad IS Normal"); IS.NewRule("RULE 36", "IF Time IS Normal AND Ayuda IS Low AND Errores IS High AND Aciertos IS High THEN Dificultad IS Normal"); IS.NewRule("RULE 37", "IF Time IS Normal AND Ayuda IS Normal AND Errores IS Low AND Aciertos IS Low THEN Dificultad IS Normal"); IS.NewRule("RULE 38", "IF Time IS Normal AND Ayuda IS Normal AND Errores IS Low AND Aciertos IS Normal THEN Dificultad IS Normal"); IS.NewRule("RULE 39", "IF Time IS Normal AND Ayuda IS Normal AND Errores IS Low AND Aciertos IS High THEN Dificultad IS Good"); IS.NewRule("RULE 40", "IF Time IS Normal AND Ayuda IS Normal AND Errores IS Normal AND Aciertos IS Low THEN Dificultad IS Normal"); IS.NewRule("RULE 41", "IF Time IS Normal AND Ayuda IS Normal AND Errores IS Normal AND Aciertos IS Normal THEN Dificultad IS Normal"); IS.NewRule("RULE 42", "IF Time IS Normal AND Ayuda IS Normal AND Errores IS Normal AND Aciertos IS High THEN Dificultad IS Good"); IS.NewRule("RULE 43", "IF Time IS Normal AND Ayuda IS Normal AND Errores IS High AND Aciertos IS Low THEN Dificultad IS Weak"); IS.NewRule("RULE 44", "IF Time IS Normal AND Ayuda IS Normal AND Errores IS High AND Aciertos IS Normal THEN Dificultad IS Weak"); IS.NewRule("RULE 45", "IF Time IS Normal AND Ayuda IS Normal AND Errores IS High AND Aciertos IS High THEN Dificultad IS Normal"); IS.NewRule("RULE 46", "IF Time IS Normal AND Ayuda IS High AND Errores IS Low AND Aciertos IS Low THEN Dificultad IS Weak"); IS.NewRule("RULE 47", "IF Time IS Normal AND Ayuda IS High AND Errores IS Low AND Aciertos IS Normal THEN Dificultad IS Normal"); IS.NewRule("RULE 48", "IF Time IS Normal AND Ayuda IS High AND Errores IS Low AND Aciertos IS High THEN Dificultad IS Good"); IS.NewRule("RULE 49", "IF Time IS Normal AND Ayuda IS High AND Errores IS Normal AND Aciertos IS Low THEN Dificultad IS Weak"); IS.NewRule("RULE 50", "IF Time IS Normal AND Ayuda IS High AND Errores IS Normal AND Aciertos IS Normal THEN Dificultad IS Normal"); IS.NewRule("RULE 51", "IF Time IS Normal AND Ayuda IS High AND Errores IS Normal AND Aciertos IS High THEN Dificultad IS Normal"); IS.NewRule("RULE 52", "IF Time IS Normal AND Ayuda IS High AND Errores IS High AND Aciertos IS Low THEN Dificultad IS VeryWeak"); IS.NewRule("RULE 53", "IF Time IS Normal AND Ayuda IS High AND Errores IS High AND Aciertos IS Normal THEN Dificultad IS Weak"); IS.NewRule("RULE 54", "IF Time IS Normal AND Ayuda IS High AND Errores IS High AND Aciertos IS High THEN Dificultad IS Normal"); IS.NewRule("RULE 55", "IF Time IS Slow AND Ayuda IS Low AND Errores IS Low AND Aciertos IS Low THEN Dificultad IS Normal"); IS.NewRule("RULE 56", "IF Time IS Slow AND Ayuda IS Low AND Errores IS Low AND Aciertos IS Normal THEN Dificultad IS Normal"); IS.NewRule("RULE 57", "IF Time IS Slow AND Ayuda IS Low AND Errores IS Low AND Aciertos IS High THEN Dificultad IS Good"); IS.NewRule("RULE 58", "IF Time IS Slow AND Ayuda IS Low AND Errores IS Normal AND Aciertos IS Low THEN Dificultad IS Weak"); IS.NewRule("RULE 59", "IF Time IS Slow AND Ayuda IS Low AND Errores IS Normal AND Aciertos IS Normal THEN Dificultad IS Normal"); IS.NewRule("RULE 60", "IF Time IS Slow AND Ayuda IS Low AND Errores IS Normal AND Aciertos IS High THEN Dificultad IS Good"); IS.NewRule("RULE 61", "IF Time IS Slow AND Ayuda IS Low AND Errores IS High AND Aciertos IS Low THEN Dificultad IS Weak"); IS.NewRule("RULE 62", "IF Time IS Slow AND Ayuda IS Low AND Errores IS High AND Aciertos IS Normal THEN Dificultad IS Normal"); IS.NewRule("RULE 63", "IF Time IS Slow AND Ayuda IS Low AND Errores IS High AND Aciertos IS High THEN Dificultad IS Normal"); IS.NewRule("RULE 64", "IF Time IS Slow AND Ayuda IS Normal AND Errores IS Low AND Aciertos IS Low THEN Dificultad IS VeryWeak"); IS.NewRule("RULE 65", "IF Time IS Slow AND Ayuda IS Normal AND Errores IS Low AND Aciertos IS Normal THEN Dificultad IS Normal"); IS.NewRule("RULE 66", "IF Time IS Slow AND Ayuda IS Normal AND Errores IS Low AND Aciertos IS High THEN Dificultad IS Good"); IS.NewRule("RULE 67", "IF Time IS Slow AND Ayuda IS Normal AND Errores IS Normal AND Aciertos IS Low THEN Dificultad IS Weak"); IS.NewRule("RULE 68", "IF Time IS Slow AND Ayuda IS Normal AND Errores IS Normal AND Aciertos IS Normal THEN Dificultad IS Normal"); IS.NewRule("RULE 69", "IF Time IS Slow AND Ayuda IS Normal AND Errores IS Normal AND Aciertos IS High THEN Dificultad IS Normal"); IS.NewRule("RULE 70", "IF Time IS Slow AND Ayuda IS Normal AND Errores IS High AND Aciertos IS Low THEN Dificultad IS VeryWeak"); IS.NewRule("RULE 71", "IF Time IS Slow AND Ayuda IS Normal AND Errores IS High AND Aciertos IS Normal THEN Dificultad IS Weak"); IS.NewRule("RULE 72", "IF Time IS Slow AND Ayuda IS Normal AND Errores IS High AND Aciertos IS High THEN Dificultad IS Normal"); IS.NewRule("RULE 73", "IF Time IS Slow AND Ayuda IS High AND Errores IS Low AND Aciertos IS Low THEN Dificultad IS Weak"); IS.NewRule("RULE 74", "IF Time IS Slow AND Ayuda IS High AND Errores IS Low AND Aciertos IS Normal THEN Dificultad IS Weak"); IS.NewRule("RULE 75", "IF Time IS Slow AND Ayuda IS High AND Errores IS Low AND Aciertos IS High THEN Dificultad IS Normal"); IS.NewRule("RULE 76", "IF Time IS Slow AND Ayuda IS High AND Errores IS Normal AND Aciertos IS Low THEN Dificultad IS VeryWeak"); IS.NewRule("RULE 77", "IF Time IS Slow AND Ayuda IS High AND Errores IS Normal AND Aciertos IS Normal THEN Dificultad IS Weak"); IS.NewRule("RULE 78", "IF Time IS Slow AND Ayuda IS High AND Errores IS Normal AND Aciertos IS High THEN Dificultad IS Normal"); IS.NewRule("RULE 79", "IF Time IS Slow AND Ayuda IS High AND Errores IS High AND Aciertos IS Low THEN Dificultad IS VeryWeak"); IS.NewRule("RULE 80", "IF Time IS Slow AND Ayuda IS High AND Errores IS High AND Aciertos IS Normal THEN Dificultad IS VeryWeak"); IS.NewRule("RULE 81", "IF Time IS Slow AND Ayuda IS High AND Errores IS High AND Aciertos IS High THEN Dificultad IS Weak"); }
/// <summary> /// Define regras fuzzy /// </summary> private void SetRules() { // Regras da Reta IS.NewRule("R1", "IF RoadSegment IS Reta And Frontal IS FarF AND Left IS FarS THEN Angle IS VeryLittleNegative"); IS.NewRule("R2", "IF RoadSegment IS Reta And Frontal IS FarF AND Right IS FarS THEN Angle IS VeryLittlePositive"); IS.NewRule("R3", "IF RoadSegment IS Reta And Frontal IS FarF THEN Angle IS Zero"); IS.NewRule("R4", "IF RoadSegment IS Reta And Frontal IS MediumF AND Left Is FarS THEN Angle IS VeryLittleNegative"); IS.NewRule("R5", "IF RoadSegment IS Reta And Frontal IS MediumF AND Right Is FarS THEN Angle IS VeryLittlePositive"); IS.NewRule("R6", "IF RoadSegment IS Reta And Right Is NearS THEN Angle IS VeryLittleNegative"); IS.NewRule("R7", "IF RoadSegment IS Reta And Frontal Is NearF AND Right IS FarS THEN Angle IS Positive"); IS.NewRule("R8", "IF RoadSegment IS Reta And Frontal Is VeryNearF AND Right IS FarS THEN Angle IS Positive"); // Regras da Curva IS.NewRule("R9", "IF RoadSegment IS Curva And CurvedFrontalLeft Is FarCF Then Angle IS VeryNegative"); IS.NewRule("R10", "IF RoadSegment IS Curva And CurvedFrontalRight Is FarCF THEN Angle IS VeryPositive"); IS.NewRule("R11", "IF RoadSegment IS Curva And RightFront Is FarS THEN Angle IS Positive"); IS.NewRule("R12", "IF RoadSegment IS Curva And Right Is FarS THEN Angle IS Positive"); IS.NewRule("R13", "IF RoadSegment IS Curva And RightRear Is FarS THEN Angle IS Positive"); IS.NewRule("R14", "IF RoadSegment IS Curva And Frontal Is FarF THEN Angle IS Zero"); IS.NewRule("R15", "IF RoadSegment IS Curva And FrontalRight Is FarF THEN Angle IS Zero"); IS.NewRule("R16", "IF RoadSegment IS Curva And FrontalLeft Is FarF THEN Angle IS Zero"); IS.NewRule("R17", "IF RoadSegment IS Curva And LeftFront Is FarS THEN Angle IS Negative"); IS.NewRule("R18", "IF RoadSegment IS Curva And Left Is FarS THEN Angle IS Negative"); IS.NewRule("R19", "IF RoadSegment IS Curva And LeftRear Is FarS THEN Angle IS Negative"); IS.NewRule("R20", "IF RoadSegment IS Curva And CurvedFrontalLeft Is NearCF Then Angle IS VeryPositive"); IS.NewRule("R21", "IF RoadSegment IS Curva And LeftFront Is FarS THEN Angle IS Negative"); IS.NewRule("R22", "IF RoadSegment IS Curva And RightFront Is FarS THEN Angle IS Positive"); IS.NewRule("R23", "IF RoadSegment IS Curva And LeftFront Is FarS AND Left IS FarS AND LeftRear IS FarS THEN Angle IS VeryNegative"); IS.NewRule("R24", "IF RoadSegment IS Curva And RightFront Is FarS AND Right IS FarS AND RightRear IS FarS THEN Angle IS VeryPositive"); IS.NewRule("R25", "IF RoadSegment IS Curva And CurvedFrontalRight Is NearCF Then Angle IS VeryNegative"); //Regras RF IS.NewRule("R26", "IF RoadSegment IS Reta And RightFront IS FarS THEN Angle IS VeryLittlePositive"); IS.NewRule("R27", "IF RoadSegment IS Reta And RightFront Is FarS THEN Angle IS VeryLittlePositive"); IS.NewRule("R28", "IF RoadSegment IS Reta And RightFront Is MediumS THEN Angle IS Zero"); //Regras RR IS.NewRule("R29", "IF RoadSegment IS Reta And RightRear IS FarS THEN Angle IS VeryLittlePositive"); IS.NewRule("R30", "IF RoadSegment IS Reta And RightRear Is FarS THEN Angle IS VeryLittlePositive"); IS.NewRule("R31", "IF RoadSegment IS Reta And RightRear Is MediumS THEN Angle IS Zero"); //Regras LF IS.NewRule("R32", "IF RoadSegment IS Reta And LeftFront IS FarS THEN Angle IS VeryLittleNegative"); IS.NewRule("R33", "IF RoadSegment IS Reta And LeftFront Is FarS THEN Angle IS VeryLittleNegative"); IS.NewRule("R34", "IF RoadSegment IS Reta And LeftFront Is MediumS THEN Angle IS Zero"); //Regras LR IS.NewRule("R35", "IF RoadSegment IS Reta And LeftRear IS FarS THEN Angle IS VeryLittleNegative"); IS.NewRule("R36", "IF RoadSegment IS Reta And LeftRear Is FarS THEN Angle IS VeryLittleNegative"); IS.NewRule("R37", "IF RoadSegment IS Reta And LeftRear Is MediumS THEN Angle IS Zero"); // Regras Sensores Frontais IS.NewRule("R38", "IF RoadSegment IS Reta And FrontalRight Is NearF AND Right IS FarS THEN Angle IS Positive"); IS.NewRule("R39", "IF RoadSegment IS Reta And FrontalRight Is VeryNearF AND Right IS FarS THEN Angle IS Positive"); IS.NewRule("R40", "IF RoadSegment IS Reta And FrontalLeft Is NearF AND Right IS FarS THEN Angle IS Positive"); IS.NewRule("R41", "IF RoadSegment IS Reta And FrontalLeft Is VeryNearF AND Right IS FarS THEN Angle IS Positive"); IS.NewRule("R42", "IF RoadSegment IS Reta And CurvedFrontalLeft Is FarCF Then Angle IS Negative"); IS.NewRule("R43", "IF RoadSegment IS Reta And CurvedFrontalRight Is FarCF THEN Angle IS Positive"); // Aceleração IS.NewRule("R44", "IF RoadSegment IS Reta AND Frontal IS FarF AND FrontalRight IS FarF AND FrontalLeft IS FarF THEN Acceleration IS VeryFast"); IS.NewRule("R45", "IF RoadSegment IS Reta AND Frontal IS MediumF AND FrontalRight IS MediumF AND FrontalLeft IS MediumF THEN Acceleration IS Fast"); IS.NewRule("R46", "IF RoadSegment IS Reta AND CurvedFrontalRight IS NearCF THEN Acceleration IS Slow"); IS.NewRule("R47", "IF RoadSegment IS Reta AND CurvedFrontalRight IS VeryNearCF THEN Acceleration IS Slow"); IS.NewRule("R48", "IF RoadSegment IS Reta AND Frontal IS NearF AND FrontalRight IS NearF AND FrontalLeft IS NearF THEN Acceleration IS Stop"); IS.NewRule("R49", "IF RoadSegment IS Reta AND Frontal IS NearF AND FrontalRight IS NearF AND FrontalLeft IS NearF THEN Acceleration IS Stop"); IS.NewRule("R50", "IF CurvedFrontalLeft IS VeryNearCF THEN Acceleration IS Stop"); IS.NewRule("R51", "IF CurvedFrontalRight IS VeryNearCF THEN Acceleration IS Stop"); IS.NewRule("R52", "IF RoadSegment IS Reta AND Frontal IS FarF AND FrontalRight IS FarF AND FrontalLeft IS NOT FarF THEN Acceleration IS Normal"); IS.NewRule("R53", "IF RoadSegment IS Reta AND Frontal IS FarF AND FrontalLeft IS FarF AND FrontalRight IS NOT FarF THEN Acceleration IS Normal"); IS.NewRule("R54", "IF RoadSegment IS Curva AND Frontal IS NearF THEN Acceleration IS Slow"); IS.NewRule("R55", "IF RoadSegment IS Curva AND Frontal IS MediumF THEN Acceleration IS Normal"); IS.NewRule("R56", "IF RoadSegment IS Curva AND Frontal IS FarF THEN Acceleration IS Normal"); IS.NewRule("R57", "IF RoadSegment IS Curva AND Frontal IS FarF AND FrontalRight IS FarF AND FrontalLeft IS FarF THEN Acceleration IS VeryFast"); IS.NewRule("R58", "IF RoadSegment IS Curva THEN Acceleration IS Slow"); IS.NewRule("R59", "IF RoadSegment IS Curva AND Frontal IS MediumF AND FrontalRight IS MediumF AND FrontalLeft IS MediumF THEN Acceleration IS Fast"); IS.NewRule("R60", "IF RoadSegment IS Reta AND CurvedFrontalRight IS NOT NearCF AND CurvedFrontalRight IS NOT VeryNearCF AND CurvedFrontalLeft IS NOT NearCF AND CurvedFrontalLeft IS NOT VeryNearCF THEN Acceleration IS Fast"); IS.NewRule("R61", "IF CurvedFrontalLeft IS VeryNearCF THEN Angle IS Positive"); IS.NewRule("R62", "IF CurvedFrontalRight IS VeryNearCF THEN Angle IS Negative"); // Carro adversario IS.NewRule("R63", "IF RoadSegment IS Reta And CarroAdversario IS Right THEN Angle IS Positive"); IS.NewRule("R64", "IF RoadSegment IS Reta And CarroAdversario IS RightFront THEN Angle IS Positive"); IS.NewRule("R65", "IF RoadSegment IS Reta And CarroAdversario IS RightRear THEN Angle IS Positive"); IS.NewRule("R66", "IF RoadSegment IS Curva And CarroAdversario IS Right THEN Angle IS VeryPositive"); IS.NewRule("R67", "IF RoadSegment IS Curva And CarroAdversario IS RightFront THEN Angle IS VeryPositive"); IS.NewRule("R68", "IF RoadSegment IS Curva And CarroAdversario IS RightRear THEN Angle IS VeryPositive"); }
private void bunifuImageButton9_Click(object sender, EventArgs e) { // Kecepatan (0-120) FuzzySet fsLambat = new FuzzySet("Lambat", new TrapezoidalFunction(30, 50, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsSedang = new FuzzySet("Sedang", new TrapezoidalFunction(40, 60, 80)); FuzzySet fsCepat = new FuzzySet("Cepat", new TrapezoidalFunction(70, 80, TrapezoidalFunction.EdgeType.Left)); // Linguistik Kecepatan LinguisticVariable lvKecepatan = new LinguisticVariable("Kecepatan", 0, 120); lvKecepatan.AddLabel(fsLambat); lvKecepatan.AddLabel(fsSedang); lvKecepatan.AddLabel(fsCepat); // Jarak (0-10) FuzzySet fsDekat = new FuzzySet("Dekat", new TrapezoidalFunction(1, 2, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsJauh = new FuzzySet("Jauh", new TrapezoidalFunction(1, 4, 5)); FuzzySet fsSangatJauh = new FuzzySet("SangatJauh", new TrapezoidalFunction(4, 7, TrapezoidalFunction.EdgeType.Left)); // Linguistik Jarak LinguisticVariable lvJarak = new LinguisticVariable("Jarak", 0, 10); lvJarak.AddLabel(fsDekat); lvJarak.AddLabel(fsJauh); lvJarak.AddLabel(fsSangatJauh); // Kemiringan Jalan ((-90)-90) FuzzySet fsTurun = new FuzzySet("Turun", new TrapezoidalFunction(-40, -20, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsDatar = new FuzzySet("Datar", new TrapezoidalFunction(-25, 0, 25)); FuzzySet fsNaik = new FuzzySet("Naik", new TrapezoidalFunction(20, 40, TrapezoidalFunction.EdgeType.Left)); // Linguistik Kemiringan Jalan LinguisticVariable lvKemiringan = new LinguisticVariable("Kemiringan", -90, 90); lvKemiringan.AddLabel(fsTurun); lvKemiringan.AddLabel(fsDatar); lvKemiringan.AddLabel(fsNaik); // OUTPUT // Tingkat Pengereman (0-10) FuzzySet fsLemah = new FuzzySet("Lemah", new TrapezoidalFunction(3, 4, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsMedium = new FuzzySet("Sedang", new TrapezoidalFunction(3, 4, 6, 7)); FuzzySet fsKuat = new FuzzySet("Kuat", new TrapezoidalFunction(6, 7, TrapezoidalFunction.EdgeType.Left)); // Linguistik Kecepatan LinguisticVariable lvTingkatPengereman = new LinguisticVariable("TingkatPengereman", 0, 10); lvTingkatPengereman.AddLabel(fsLemah); lvTingkatPengereman.AddLabel(fsMedium); lvTingkatPengereman.AddLabel(fsKuat); // Database Database fuzzyDB = new Database(); fuzzyDB.AddVariable(lvJarak); fuzzyDB.AddVariable(lvKemiringan); fuzzyDB.AddVariable(lvKecepatan); fuzzyDB.AddVariable(lvTingkatPengereman); // Inferensi System Engine InferenceSystem IS = new InferenceSystem(fuzzyDB, new CentroidDefuzzifier(1000)); // Rules IS.NewRule("Rule 1", "IF Kecepatan IS Lambat AND Jarak IS Dekat AND Kemiringan IS Turun THEN TingkatPengereman IS Kuat"); IS.NewRule("Rule 2", "IF Kecepatan IS Lambat AND Jarak IS Dekat AND Kemiringan IS Naik THEN TingkatPengereman IS Lemah"); IS.NewRule("Rule 3", "IF Kecepatan IS Lambat AND Jarak IS Dekat AND Kemiringan IS Datar THEN TingkatPengereman IS Sedang"); IS.NewRule("Rule 4", "IF Kecepatan IS Lambat AND Jarak IS Jauh AND Kemiringan IS Datar THEN TingkatPengereman IS Lemah"); IS.NewRule("Rule 5", "IF Kecepatan IS Lambat AND Jarak IS Jauh AND Kemiringan IS Naik THEN TingkatPengereman IS Lemah"); IS.NewRule("Rule 6", "IF Kecepatan IS Lambat AND Jarak IS Jauh AND Kemiringan IS Turun THEN TingkatPengereman IS Sedang"); IS.NewRule("Rule 7", "IF Kecepatan IS Lambat AND Jarak IS SangatJauh AND Kemiringan IS Datar THEN TingkatPengereman IS Lemah"); IS.NewRule("Rule 8", "IF Kecepatan IS Lambat AND Jarak IS SangatJauh AND Kemiringan IS Naik THEN TingkatPengereman IS Lemah"); IS.NewRule("Rule 9", "IF Kecepatan IS Lambat AND Jarak IS SangatJauh AND Kemiringan IS Turun THEN TingkatPengereman IS Sedang"); IS.NewRule("Rule 10", "IF Kecepatan IS Sedang AND Jarak IS Dekat AND Kemiringan IS Datar THEN TingkatPengereman IS Kuat"); IS.NewRule("Rule 11", "IF Kecepatan IS Sedang AND Jarak IS Dekat AND Kemiringan IS Naik THEN TingkatPengereman IS Sedang"); IS.NewRule("Rule 12", "IF Kecepatan IS Sedang AND Jarak IS Dekat AND Kemiringan IS Turun THEN TingkatPengereman IS Kuat"); IS.NewRule("Rule 13", "IF Kecepatan IS Sedang AND Jarak IS Jauh AND Kemiringan IS Datar THEN TingkatPengereman IS Sedang"); IS.NewRule("Rule 14", "IF Kecepatan IS Sedang AND Jarak IS Jauh AND Kemiringan IS Naik THEN TingkatPengereman IS Lemah"); IS.NewRule("Rule 15", "IF Kecepatan IS Sedang AND Jarak IS Jauh AND Kemiringan IS Turun THEN TingkatPengereman IS Kuat"); IS.NewRule("Rule 16", "IF Kecepatan IS Sedang AND Jarak IS SangatJauh AND Kemiringan IS Datar THEN TingkatPengereman IS Lemah"); IS.NewRule("Rule 17", "IF Kecepatan IS Sedang AND Jarak IS SangatJauh AND Kemiringan IS Naik THEN TingkatPengereman IS Lemah"); IS.NewRule("Rule 18", "IF Kecepatan IS Sedang AND Jarak IS SangatJauh AND Kemiringan IS Turun THEN TingkatPengereman IS Sedang"); IS.NewRule("Rule 19", "IF Kecepatan IS Cepat AND Jarak IS Dekat AND Kemiringan IS Datar THEN TingkatPengereman IS Kuat"); IS.NewRule("Rule 20", "IF Kecepatan IS Cepat AND Jarak IS Dekat AND Kemiringan IS Naik THEN TingkatPengereman IS Sedang"); IS.NewRule("Rule 21", "IF Kecepatan IS Cepat AND Jarak IS Dekat AND Kemiringan IS Turun THEN TingkatPengereman IS Kuat"); IS.NewRule("Rule 22", "IF Kecepatan IS Cepat AND Jarak IS Jauh AND Kemiringan IS Datar THEN TingkatPengereman IS Kuat"); IS.NewRule("Rule 23", "IF Kecepatan IS Cepat AND Jarak IS Jauh AND Kemiringan IS Naik THEN TingkatPengereman IS Sedang"); IS.NewRule("Rule 24", "IF Kecepatan IS Cepat AND Jarak IS Jauh AND Kemiringan IS Turun THEN TingkatPengereman IS Kuat"); IS.NewRule("Rule 25", "IF Kecepatan IS Cepat AND Jarak IS SangatJauh AND Kemiringan IS Datar THEN TingkatPengereman IS Sedang"); IS.NewRule("Rule 26", "IF Kecepatan IS Cepat AND Jarak IS SangatJauh AND Kemiringan IS Naik THEN TingkatPengereman IS Lemah"); IS.NewRule("Rule 27", "IF Kecepatan IS Cepat AND Jarak IS SangatJauh AND Kemiringan IS Turun THEN TingkatPengereman IS Kuat"); // Set Input IS.SetInput("Kecepatan", (float)Convert.ToDouble(TB_Speed.Value)); IS.SetInput("Jarak", (float)Convert.ToDouble(TB_Distance.Value)); if (TB_AngleMin.Value != 0) { IS.SetInput("Kemiringan", (float)Convert.ToDouble(TB_AngleMin.Value)); } IS.SetInput("Kemiringan", (float)Convert.ToDouble(TB_AnglePlus.Value)); // Get output double BrakeLevel = IS.Evaluate("TingkatPengereman"); if (TB_Speed.Value == 0 && TB_Distance.Value == 0 && TB_AngleMin.Value == 0 && TB_AnglePlus.Value == 0) { BrakeLevel = 0; Gauge_Break.Value = Convert.ToInt32(BrakeLevel * 10); lblvalueBrake.Text = Convert.ToString(Math.Ceiling(BrakeLevel)); } else { Gauge_Break.Value = Convert.ToInt32(BrakeLevel * 10); } lblvalueBrake.Text = Convert.ToString(Math.Ceiling(BrakeLevel)); }
public void JNCK() { InitializeComponent(); // INPUT // Kecepatan (0-120) FuzzySet fsLambat = new FuzzySet("Lambat", new TrapezoidalFunction(30, 50, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsSedang = new FuzzySet("Sedang", new TrapezoidalFunction(40, 60, 80)); FuzzySet fsCepat = new FuzzySet("Cepat", new TrapezoidalFunction(70, 80, TrapezoidalFunction.EdgeType.Left)); // Linguistik Kecepatan LinguisticVariable lvKecepatan = new LinguisticVariable("Kecepatan", 0, 120); lvKecepatan.AddLabel(fsLambat); lvKecepatan.AddLabel(fsSedang); lvKecepatan.AddLabel(fsCepat); // Jarak (0-10) FuzzySet fsDekat = new FuzzySet("Dekat", new TrapezoidalFunction(1, 2, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsJauh = new FuzzySet("Jauh", new TrapezoidalFunction(1, 4, 5)); FuzzySet fsSangatJauh = new FuzzySet("SangatJauh", new TrapezoidalFunction(4, 7, TrapezoidalFunction.EdgeType.Left)); // Linguistik Jarak LinguisticVariable lvJarak = new LinguisticVariable("Jarak", 0, 10); lvJarak.AddLabel(fsDekat); lvJarak.AddLabel(fsJauh); lvJarak.AddLabel(fsSangatJauh); // Kemiringan Jalan ((-90)-90) FuzzySet fsTurun = new FuzzySet("Turun", new TrapezoidalFunction(-40, -20, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsDatar = new FuzzySet("Datar", new TrapezoidalFunction(-25, 0, 25)); FuzzySet fsNaik = new FuzzySet("Naik", new TrapezoidalFunction(20, 40, TrapezoidalFunction.EdgeType.Left)); // Linguistik Kemiringan Jalan LinguisticVariable lvKemiringan = new LinguisticVariable("Kemiringan", -90, 90); lvKemiringan.AddLabel(fsTurun); lvKemiringan.AddLabel(fsDatar); lvKemiringan.AddLabel(fsNaik); // OUTPUT // Tingkat Pengereman (0-10) FuzzySet fsLemah = new FuzzySet("Lemah", new TrapezoidalFunction(3, 4, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsMedium = new FuzzySet("Sedang", new TrapezoidalFunction(3, 4, 6, 7)); FuzzySet fsKuat = new FuzzySet("Kuat", new TrapezoidalFunction(6, 7, TrapezoidalFunction.EdgeType.Left)); // Linguistik Kecepatan LinguisticVariable lvTingkatPengereman = new LinguisticVariable("TingkatPengereman", 0, 10); lvTingkatPengereman.AddLabel(fsLemah); lvTingkatPengereman.AddLabel(fsMedium); lvTingkatPengereman.AddLabel(fsKuat); // Database Database fuzzyDB = new Database(); fuzzyDB.AddVariable(lvJarak); fuzzyDB.AddVariable(lvKemiringan); fuzzyDB.AddVariable(lvKecepatan); fuzzyDB.AddVariable(lvTingkatPengereman); // Inferensi System Engine InferenceSystem IS = new InferenceSystem(fuzzyDB, new CentroidDefuzzifier(1000)); // Rules IS.NewRule("Rule 1", "IF Kecepatan IS Lambat AND Jarak IS Dekat AND Kemiringan IS Turun THEN TingkatPengereman IS Kuat"); IS.NewRule("Rule 2", "IF Kecepatan IS Lambat AND Jarak IS Dekat AND Kemiringan IS Naik THEN TingkatPengereman IS Lemah"); IS.NewRule("Rule 3", "IF Kecepatan IS Lambat AND Jarak IS Dekat AND Kemiringan IS Datar THEN TingkatPengereman IS Sedang"); IS.NewRule("Rule 4", "IF Kecepatan IS Lambat AND Jarak IS Jauh AND Kemiringan IS Datar THEN TingkatPengereman IS Lemah"); IS.NewRule("Rule 5", "IF Kecepatan IS Lambat AND Jarak IS Jauh AND Kemiringan IS Naik THEN TingkatPengereman IS Lemah"); IS.NewRule("Rule 6", "IF Kecepatan IS Lambat AND Jarak IS Jauh AND Kemiringan IS Turun THEN TingkatPengereman IS Sedang"); IS.NewRule("Rule 7", "IF Kecepatan IS Lambat AND Jarak IS SangatJauh AND Kemiringan IS Datar THEN TingkatPengereman IS Lemah"); IS.NewRule("Rule 8", "IF Kecepatan IS Lambat AND Jarak IS SangatJauh AND Kemiringan IS Naik THEN TingkatPengereman IS Lemah"); IS.NewRule("Rule 9", "IF Kecepatan IS Lambat AND Jarak IS SangatJauh AND Kemiringan IS Turun THEN TingkatPengereman IS Sedang"); IS.NewRule("Rule 10", "IF Kecepatan IS Sedang AND Jarak IS Dekat AND Kemiringan IS Datar THEN TingkatPengereman IS Kuat"); IS.NewRule("Rule 11", "IF Kecepatan IS Sedang AND Jarak IS Dekat AND Kemiringan IS Naik THEN TingkatPengereman IS Sedang"); IS.NewRule("Rule 12", "IF Kecepatan IS Sedang AND Jarak IS Dekat AND Kemiringan IS Turun THEN TingkatPengereman IS Kuat"); IS.NewRule("Rule 13", "IF Kecepatan IS Sedang AND Jarak IS Jauh AND Kemiringan IS Datar THEN TingkatPengereman IS Sedang"); IS.NewRule("Rule 14", "IF Kecepatan IS Sedang AND Jarak IS Jauh AND Kemiringan IS Naik THEN TingkatPengereman IS Lemah"); IS.NewRule("Rule 15", "IF Kecepatan IS Sedang AND Jarak IS Jauh AND Kemiringan IS Turun THEN TingkatPengereman IS Kuat"); IS.NewRule("Rule 16", "IF Kecepatan IS Sedang AND Jarak IS SangatJauh AND Kemiringan IS Datar THEN TingkatPengereman IS Lemah"); IS.NewRule("Rule 17", "IF Kecepatan IS Sedang AND Jarak IS SangatJauh AND Kemiringan IS Naik THEN TingkatPengereman IS Lemah"); IS.NewRule("Rule 18", "IF Kecepatan IS Sedang AND Jarak IS SangatJauh AND Kemiringan IS Turun THEN TingkatPengereman IS Sedang"); IS.NewRule("Rule 19", "IF Kecepatan IS Cepat AND Jarak IS Dekat AND Kemiringan IS Datar THEN TingkatPengereman IS Kuat"); IS.NewRule("Rule 20", "IF Kecepatan IS Cepat AND Jarak IS Dekat AND Kemiringan IS Naik THEN TingkatPengereman IS Sedang"); IS.NewRule("Rule 21", "IF Kecepatan IS Cepat AND Jarak IS Dekat AND Kemiringan IS Turun THEN TingkatPengereman IS Kuat"); IS.NewRule("Rule 22", "IF Kecepatan IS Cepat AND Jarak IS Jauh AND Kemiringan IS Datar THEN TingkatPengereman IS Kuat"); IS.NewRule("Rule 23", "IF Kecepatan IS Cepat AND Jarak IS Jauh AND Kemiringan IS Naik THEN TingkatPengereman IS Sedang"); IS.NewRule("Rule 24", "IF Kecepatan IS Cepat AND Jarak IS Jauh AND Kemiringan IS Turun THEN TingkatPengereman IS Kuat"); IS.NewRule("Rule 25", "IF Kecepatan IS Cepat AND Jarak IS SangatJauh AND Kemiringan IS Datar THEN TingkatPengereman IS Sedang"); IS.NewRule("Rule 26", "IF Kecepatan IS Cepat AND Jarak IS SangatJauh AND Kemiringan IS Naik THEN TingkatPengereman IS Lemah"); IS.NewRule("Rule 27", "IF Kecepatan IS Cepat AND Jarak IS SangatJauh AND Kemiringan IS Turun THEN TingkatPengereman IS Kuat"); // Set Input IS.SetInput("Kecepatan", (float)speed); IS.SetInput("Jarak", (float)distance); IS.SetInput("Kemiringan", (float)angle); // Get output double BreakLevel = IS.Evaluate("TingkatPengereman"); // Tampilkan Hasil System.Console.WriteLine(BreakLevel); }
private static InferenceSystem CreateInferenceSystem() { var objectiveVariable = new LinguisticVariable("OBJECTIVE", 0, 1); var subjectiveVariable = new LinguisticVariable("SUBJECTIVE", 0, 1); var algorithmScoreVariable = new LinguisticVariable("ALGORITHM_SCORE", 0, 1); var objectiveLow = new ZTypeMembership(0, 0.5); var objectiveMedium = new PITypeMembership(0, 0.5, 0.75); var objectiveHigh = new STypeMembership(0.25, 1); var subjectiveLow = new ZTypeMembership(0, 0.5); var subjectiveMedium = new PITypeMembership(0.25, 0.5, 0.75); var subjectiveHigh = new STypeMembership(0.5, 1); var algorithmScoreVeryLow = new VeryHedge(new ZTypeMembership(0, 0.5)); var algorithmScoreLow = new ZTypeMembership(0, 0.5); var algorithmScoreMedium = new PITypeMembership(0.25, 0.5, 0.75); var algorithmScoreHigh = new STypeMembership(0.5, 1); var algorithmScoreVeryHigh = new VeryHedge(new STypeMembership(0.5, 1)); var objectiveLowSet = new FuzzySet("LOW", objectiveLow); var objectiveMediumSet = new FuzzySet("MEDIUM", objectiveMedium); var objectiveHighSet = new FuzzySet("HIGH", objectiveHigh); var subjectiveLowSet = new FuzzySet("LOW", subjectiveLow); var subjectiveMediumSet = new FuzzySet("MEDIUM", subjectiveMedium); var subjectiveHighSet = new FuzzySet("HIGH", subjectiveHigh); var resultVeryLowSet = new FuzzySet("VERY_LOW", algorithmScoreVeryLow); var resultLowSet = new FuzzySet("LOW", algorithmScoreLow); var resultMediumSet = new FuzzySet("MEDIUM", algorithmScoreMedium); var resultHighSet = new FuzzySet("HIGH", algorithmScoreHigh); var resultVeryHighSet = new FuzzySet("VERY_HIGH", algorithmScoreVeryHigh); objectiveVariable.AddLabel(objectiveLowSet); objectiveVariable.AddLabel(objectiveMediumSet); objectiveVariable.AddLabel(objectiveHighSet); subjectiveVariable.AddLabel(subjectiveLowSet); subjectiveVariable.AddLabel(subjectiveMediumSet); subjectiveVariable.AddLabel(subjectiveHighSet); algorithmScoreVariable.AddLabel(resultVeryLowSet); algorithmScoreVariable.AddLabel(resultLowSet); algorithmScoreVariable.AddLabel(resultMediumSet); algorithmScoreVariable.AddLabel(resultHighSet); algorithmScoreVariable.AddLabel(resultVeryHighSet); var rulebase = new Database(); rulebase.AddVariable(objectiveVariable); rulebase.AddVariable(subjectiveVariable); rulebase.AddVariable(algorithmScoreVariable); var inferenceSystem = new InferenceSystem(rulebase, new CentroidDefuzzifier(30)); inferenceSystem.NewRule("Rule1", "if OBJECTIVE is LOW and SUBJECTIVE is LOW then ALGORITHM_SCORE is VERY_LOW "); inferenceSystem.NewRule("Rule2", "if OBJECTIVE is LOW and SUBJECTIVE is MEDIUM then ALGORITHM_SCORE is LOW"); inferenceSystem.NewRule("Rule3", "if OBJECTIVE is LOW and SUBJECTIVE is HIGH then ALGORITHM_SCORE is MEDIUM"); inferenceSystem.NewRule("Rule4", "if OBJECTIVE is MEDIUM and SUBJECTIVE is LOW then ALGORITHM_SCORE is LOW"); inferenceSystem.NewRule("Rule5", "if OBJECTIVE is MEDIUM and SUBJECTIVE is MEDIUM then ALGORITHM_SCORE is MEDIUM"); inferenceSystem.NewRule("Rule6", "if OBJECTIVE is MEDIUM and SUBJECTIVE is HIGH then ALGORITHM_SCORE is HIGH"); inferenceSystem.NewRule("Rule7", "if OBJECTIVE is HIGH and SUBJECTIVE is LOW then ALGORITHM_SCORE is MEDIUM"); inferenceSystem.NewRule("Rule8", "if OBJECTIVE is HIGH and SUBJECTIVE is MEDIUM then ALGORITHM_SCORE is HIGH"); inferenceSystem.NewRule("Rule9", "if OBJECTIVE is HIGH and SUBJECTIVE is HIGH then ALGORITHM_SCORE is VERY_HIGH"); return(inferenceSystem); }
void InitCuckoo() { openFileDialog1.InitialDirectory = @"D:\YandexDisk\Программирование\GestureRecognize\GestureRecognize\bin\Debug"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { BinaryFormatter bf = new BinaryFormatter(); using (FileStream fs = File.OpenRead(openFileDialog1.FileName)) { scopes = (Scope[])bf.Deserialize(fs); } } deltaRMS = new LingVar(0, "deltarms", -4f, 23, scopes, nums); Turns0 = new LingVar(1, "turns0", 0, 175, scopes, nums); Zeros0 = new LingVar(2, "zeros0", 14, 80, scopes, nums); Turns1 = new LingVar(3, "turns1", 0, 175, scopes, nums); Zeros1 = new LingVar(4, "zeros1", 14, 80, scopes, nums); const int numOutTerms = 10; output = new LingVar("out", numOutTerms, 1, numOutTerms, 1f); fuzzyDB = new Database(); fuzzyDB.AddVariable(deltaRMS.lv); fuzzyDB.AddVariable(Turns0.lv); fuzzyDB.AddVariable(Turns1.lv); fuzzyDB.AddVariable(Zeros0.lv); fuzzyDB.AddVariable(Zeros1.lv); fuzzyDB.AddVariable(output.lv); loadDataOnly(); IS = new InferenceSystem(fuzzyDB, new CentroidDefuzzifier(CentrDefu)); string rule = ""; for (int i = 0; i < nums.Length; i++) { rule = "IF deltarms IS " + deltaRMS.terms[i].Name; rule += " AND turns0 IS " + Turns0.terms[i].Name; rule += " AND turns1 IS " + Turns1.terms[i].Name; rule += " AND zeros0 IS " + Zeros0.terms[i].Name; rule += " AND zeros1 IS " + Zeros1.terms[i].Name; rule += " THEN out IS " + output.terms[i].Name; IS.NewRule(i.ToString(), rule); } //chart.RangeX = new AForge.Range(14, 80); //chart.AddDataSeries("0", Color.Black, AForge.Controls.Chart.SeriesType.Line, 3, true); //chart.AddDataSeries("1", Color.Red, AForge.Controls.Chart.SeriesType.Line, 3, true); //chart.AddDataSeries("2", Color.Blue, AForge.Controls.Chart.SeriesType.Line, 3, true); //chart.AddDataSeries("3", Color.Green, AForge.Controls.Chart.SeriesType.Line, 3, true); //chart.AddDataSeries("4", Color.Pink, AForge.Controls.Chart.SeriesType.Line, 3, true); //chart.AddDataSeries("5", Color.Purple, AForge.Controls.Chart.SeriesType.Line, 3, true); //chart.AddDataSeries("6", Color.Orange, AForge.Controls.Chart.SeriesType.Line, 3, true); //chart.AddDataSeries("7", Color.Yellow, AForge.Controls.Chart.SeriesType.Line, 3, true); //chart.AddDataSeries("8", Color.Lime, AForge.Controls.Chart.SeriesType.Line, 3, true); //chart.AddDataSeries("9", Color.Coral, AForge.Controls.Chart.SeriesType.Line, 3, true); //// get membership of some points to the cool fuzzy set //double[][,] chartValues = new double[10][,]; //for (int i = 0; i < 10; i++) // chartValues[i] = new double[140, 2]; //// showing the shape of the linguistic variable - the shape of its labels memberships from start to end //int j = 0; //for (float x = 14; x < 80; x += 0.5f, j++) //{ // double[] ys = new double[10]; // for (int m = 0; m < 10; m++) // { // //ys[m] = deltaRMS.lv.GetLabelMembership("deltarms" + (m*6).ToString(), x); // ys[m] = Zeros0.lv.GetLabelMembership("zeros0" + (m * 6).ToString(), x); // } // for (int m = 0; m < 10; m++) // { // chartValues[m][j, 0] = x; // chartValues[m][j, 1] = ys[m]; // } //} //// plot membership to a chart //for (int m = 0; m < 10; m++) //{ // chart.UpdateDataSeries(m.ToString(), chartValues[m]); //} }
// Hardcode initializing the Fuzzy Inference System void InitFuzzyEngine( ) { // Linguistic labels (fuzzy sets) that compose the distances FuzzySet fsNear = new FuzzySet("Near", new TrapezoidalFunction(15, 50, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsMedium = new FuzzySet("Medium", new TrapezoidalFunction(15, 50, 60, 100)); FuzzySet fsFar = new FuzzySet("Far", new TrapezoidalFunction(60, 100, TrapezoidalFunction.EdgeType.Left)); // Right Distance (Input) LinguisticVariable lvRight = new LinguisticVariable("RightDistance", 0, 120); lvRight.AddLabel(fsNear); lvRight.AddLabel(fsMedium); lvRight.AddLabel(fsFar); // Left Distance (Input) LinguisticVariable lvLeft = new LinguisticVariable("LeftDistance", 0, 120); lvLeft.AddLabel(fsNear); lvLeft.AddLabel(fsMedium); lvLeft.AddLabel(fsFar); // Front Distance (Input) LinguisticVariable lvFront = new LinguisticVariable("FrontalDistance", 0, 120); lvFront.AddLabel(fsNear); lvFront.AddLabel(fsMedium); lvFront.AddLabel(fsFar); // Linguistic labels (fuzzy sets) that compose the angle FuzzySet fsVN = new FuzzySet("VeryNegative", new TrapezoidalFunction(-40, -35, TrapezoidalFunction.EdgeType.Right)); FuzzySet fsN = new FuzzySet("Negative", new TrapezoidalFunction(-40, -35, -25, -20)); FuzzySet fsLN = new FuzzySet("LittleNegative", new TrapezoidalFunction(-25, -20, -10, -5)); FuzzySet fsZero = new FuzzySet("Zero", new TrapezoidalFunction(-10, 5, 5, 10)); FuzzySet fsLP = new FuzzySet("LittlePositive", new TrapezoidalFunction(5, 10, 20, 25)); FuzzySet fsP = new FuzzySet("Positive", new TrapezoidalFunction(20, 25, 35, 40)); FuzzySet fsVP = new FuzzySet("VeryPositive", new TrapezoidalFunction(35, 40, TrapezoidalFunction.EdgeType.Left)); // Angle LinguisticVariable lvAngle = new LinguisticVariable("Angle", -50, 50); lvAngle.AddLabel(fsVN); lvAngle.AddLabel(fsN); lvAngle.AddLabel(fsLN); lvAngle.AddLabel(fsZero); lvAngle.AddLabel(fsLP); lvAngle.AddLabel(fsP); lvAngle.AddLabel(fsVP); // The database Database fuzzyDB = new Database( ); fuzzyDB.AddVariable(lvFront); fuzzyDB.AddVariable(lvLeft); fuzzyDB.AddVariable(lvRight); fuzzyDB.AddVariable(lvAngle); // Creating the inference system IS = new InferenceSystem(fuzzyDB, new CentroidDefuzzifier(1000)); // Going Straight IS.NewRule("Rule 1", "IF FrontalDistance IS Far THEN Angle IS Zero"); // Going Straight (if can go anywhere) IS.NewRule("Rule 2", "IF FrontalDistance IS Far AND RightDistance IS Far AND LeftDistance IS Far THEN Angle IS Zero"); // Near right wall IS.NewRule("Rule 3", "IF RightDistance IS Near AND LeftDistance IS Not Near THEN Angle IS LittleNegative"); // Near left wall IS.NewRule("Rule 4", "IF RightDistance IS Not Near AND LeftDistance IS Near THEN Angle IS LittlePositive"); // Near front wall - room at right IS.NewRule("Rule 5", "IF RightDistance IS Far AND FrontalDistance IS Near THEN Angle IS Positive"); // Near front wall - room at left IS.NewRule("Rule 6", "IF LeftDistance IS Far AND FrontalDistance IS Near THEN Angle IS Negative"); // Near front wall - room at both sides - go right IS.NewRule("Rule 7", "IF RightDistance IS Far AND LeftDistance IS Far AND FrontalDistance IS Near THEN Angle IS Positive"); }
public void FromValenceArousalToEmotions(List <double> Valence, List <double> Arousal) { int meanValence = Mean(Valence); int meanArousal = Mean(Arousal); int sValence = (int)Math.Sqrt(Var(Valence)) + 1; int sArousal = (int)Math.Sqrt(Var(Arousal)) + 1; ResLogFlou2Label.Text = "Traitement en cours"; ResLogFlou2Label.Refresh(); //OK ne pas toucher #region Input (Valence) var lvValence = new LinguisticVariable("Valence", meanValence - 10 * sValence, meanValence + 10 * sValence); var fsVeryLow = new FuzzySet("VeryLow", new TrapezoidalFunction(0, 3, 6)); var fsLow = new FuzzySet("Low", new TrapezoidalFunction(6, 7, 8)); var fsMidLow = new FuzzySet("MidLow", new TrapezoidalFunction(8, 9, 10)); var fsMid = new FuzzySet("Mid", new TrapezoidalFunction(10, 15, 20)); var fsMidHigh = new FuzzySet("MidHigh", new TrapezoidalFunction(20, 22, 24)); var fsHigh = new FuzzySet("High", new TrapezoidalFunction(24, 25, 26)); var fsVeryHigh = new FuzzySet("VeryHigh", new TrapezoidalFunction(26, 28, 30)); var fsVeryLowValence = new FuzzySet("VeryLow", new TrapezoidalFunction(meanValence - 10 * sValence, meanValence - 8 * sValence, meanValence - 5 * sValence)); var fsLowValence = new FuzzySet("Low", new TrapezoidalFunction(meanValence - 5 * sValence, meanValence - 4 * sValence, meanValence - 3 * sValence)); var fsMidLowValence = new FuzzySet("MidLow", new TrapezoidalFunction(meanValence - 3 * sValence, meanValence - 2 * sValence, meanValence)); var fsMidHighValence = new FuzzySet("MidHigh", new TrapezoidalFunction(meanValence, meanValence + 2 * sValence, meanValence + 3 * sValence)); var fsHighValence = new FuzzySet("High", new TrapezoidalFunction(meanValence + 3 * sValence, meanValence + 4 * sValence, meanValence + 5 * sValence)); var fsVeryHighValence = new FuzzySet("VeryHigh", new TrapezoidalFunction(meanValence + 5 * sValence, meanValence + 8 * sValence, meanValence + 10 * sValence)); lvValence.AddLabel(fsVeryLowValence); lvValence.AddLabel(fsLowValence); lvValence.AddLabel(fsMidLowValence); lvValence.AddLabel(fsMidHighValence); lvValence.AddLabel(fsHighValence); lvValence.AddLabel(fsVeryHighValence); #endregion #region Input (Arousal) var lvArousal = new LinguisticVariable("Arousal", meanArousal - 10 * sArousal, meanArousal + 10 * sArousal); var fsVeryLowArousal = new FuzzySet("VeryLow", new TrapezoidalFunction(meanArousal - 10 * sArousal, meanArousal - 8 * sArousal, meanArousal - 5 * sArousal)); var fsLowArousal = new FuzzySet("Low", new TrapezoidalFunction(meanArousal - 5 * sArousal, meanArousal - 4 * sArousal, meanArousal - 3 * sArousal)); var fsMidLowArousal = new FuzzySet("MidLow", new TrapezoidalFunction(meanArousal - 3 * sArousal, meanArousal - 2 * sArousal, meanArousal)); var fsMidHighArousal = new FuzzySet("MidHigh", new TrapezoidalFunction(meanArousal, meanArousal + 2 * sArousal, meanArousal + 3 * sArousal)); var fsHighArousal = new FuzzySet("High", new TrapezoidalFunction(meanArousal + 3 * sArousal, meanArousal + 4 * sArousal, meanArousal + 5 * sArousal)); var fsVeryHighArousal = new FuzzySet("VeryHigh", new TrapezoidalFunction(meanArousal + 5 * sArousal, meanArousal + 8 * sArousal, meanArousal + 10 * sArousal)); lvArousal.AddLabel(fsVeryLowArousal); lvArousal.AddLabel(fsLowArousal); lvArousal.AddLabel(fsMidLowArousal); lvArousal.AddLabel(fsMidHighArousal); lvArousal.AddLabel(fsHighArousal); lvArousal.AddLabel(fsVeryHighArousal); #endregion //il y a l'output à faire, je sais pas exactement comment il faut faire, à checker en dessous c'est peut etre de la connerie que j'ai fait #region Output (Boredom) var lvBoredom = new LinguisticVariable("Boredom", 0, 30); //var fsVeryLow = new FuzzySet("VeryLow", new TrapezoidalFunction(0, 3, 6)); //var fsLow = new FuzzySet("Low", new TrapezoidalFunction(6, 7, 8)); //var fsMidLow = new FuzzySet("MidLow", new TrapezoidalFunction(8, 9, 10)); //var fsMid = new FuzzySet("Mid", new TrapezoidalFunction(10, 15, 20)); //var fsMidHigh = new FuzzySet("MidHigh", new TrapezoidalFunction(20, 22, 24)); //var fsHigh = new FuzzySet("High", new TrapezoidalFunction(24, 25, 26)); //var fsVeryHigh = new FuzzySet("VeryHigh", new TrapezoidalFunction(26, 28, 30)); lvBoredom.AddLabel(fsVeryLow); lvBoredom.AddLabel(fsLow); lvBoredom.AddLabel(fsMidLow); lvBoredom.AddLabel(fsMid); lvBoredom.AddLabel(fsMidHigh); lvBoredom.AddLabel(fsHigh); lvBoredom.AddLabel(fsVeryHigh); #endregion #region Output (Challenge) var lvChallenge = new LinguisticVariable("Challenge", 0, 30); //var fsVeryLow = new FuzzySet("VeryLow", new TrapezoidalFunction(0, 3, 6)); //var fsLow = new FuzzySet("Low", new TrapezoidalFunction(6, 7, 8)); //var fsMidLow = new FuzzySet("MidLow", new TrapezoidalFunction(8, 9, 10)); //var fsMid = new FuzzySet("Mid", new TrapezoidalFunction(10, 15, 20)); //var fsMidHigh = new FuzzySet("MidHigh", new TrapezoidalFunction(20, 22, 24)); //var fsHigh = new FuzzySet("High", new TrapezoidalFunction(24, 25, 26)); //var fsVeryHigh = new FuzzySet("VeryHigh", new TrapezoidalFunction(26, 28, 30)); lvChallenge.AddLabel(fsVeryLow); lvChallenge.AddLabel(fsLow); lvChallenge.AddLabel(fsMidLow); lvChallenge.AddLabel(fsMid); lvChallenge.AddLabel(fsMidHigh); lvChallenge.AddLabel(fsHigh); lvChallenge.AddLabel(fsVeryHigh); #endregion #region Output (Excitement) var lvExcitement = new LinguisticVariable("Excitement", 0, 30); //var fsVeryLow = new FuzzySet("VeryLow", new TrapezoidalFunction(0, 3, 6)); //var fsLow = new FuzzySet("Low", new TrapezoidalFunction(6, 7, 8)); //var fsMidLow = new FuzzySet("MidLow", new TrapezoidalFunction(8, 9, 10)); //var fsMid = new FuzzySet("Mid", new TrapezoidalFunction(10, 15, 20)); //var fsMidHigh = new FuzzySet("MidHigh", new TrapezoidalFunction(20, 22, 24)); //var fsHigh = new FuzzySet("High", new TrapezoidalFunction(24, 25, 26)); //var fsVeryHigh = new FuzzySet("VeryHigh", new TrapezoidalFunction(26, 28, 30)); lvExcitement.AddLabel(fsVeryLow); lvExcitement.AddLabel(fsLow); lvExcitement.AddLabel(fsMidLow); lvExcitement.AddLabel(fsMid); lvExcitement.AddLabel(fsMidHigh); lvExcitement.AddLabel(fsHigh); lvExcitement.AddLabel(fsVeryHigh); #endregion #region Output (Frustration) var lvFrustration = new LinguisticVariable("Frustration", 0, 30); //var fsVeryLow = new FuzzySet("VeryLow", new TrapezoidalFunction(0, 3, 6)); //var fsLow = new FuzzySet("Low", new TrapezoidalFunction(6, 7, 8)); //var fsMidLow = new FuzzySet("MidLow", new TrapezoidalFunction(8, 9, 10)); //var fsMid = new FuzzySet("Mid", new TrapezoidalFunction(10, 15, 20)); //var fsMidHigh = new FuzzySet("MidHigh", new TrapezoidalFunction(20, 22, 24)); //var fsHigh = new FuzzySet("High", new TrapezoidalFunction(24, 25, 26)); //var fsVeryHigh = new FuzzySet("VeryHigh", new TrapezoidalFunction(26, 28, 30)); lvFrustration.AddLabel(fsVeryLow); lvFrustration.AddLabel(fsLow); lvFrustration.AddLabel(fsMidLow); lvFrustration.AddLabel(fsMid); lvFrustration.AddLabel(fsMidHigh); lvFrustration.AddLabel(fsHigh); lvFrustration.AddLabel(fsVeryHigh); #endregion #region Output (Fun) var lvFun = new LinguisticVariable("Fun", 0, 30); //var fsVeryLow = new FuzzySet("VeryLow", new TrapezoidalFunction(0, 3, 6)); //var fsLow = new FuzzySet("Low", new TrapezoidalFunction(6, 7, 8)); //var fsMidLow = new FuzzySet("MidLow", new TrapezoidalFunction(8, 9, 10)); //var fsMid = new FuzzySet("Mid", new TrapezoidalFunction(10, 15, 20)); //var fsMidHigh = new FuzzySet("MidHigh", new TrapezoidalFunction(20, 22, 24)); //var fsHigh = new FuzzySet("High", new TrapezoidalFunction(24, 25, 26)); //var fsVeryHigh = new FuzzySet("VeryHigh", new TrapezoidalFunction(26, 28, 30)); lvFun.AddLabel(fsVeryLow); lvFun.AddLabel(fsLow); lvFun.AddLabel(fsMidLow); lvFun.AddLabel(fsMid); lvFun.AddLabel(fsMidHigh); lvFun.AddLabel(fsHigh); lvFun.AddLabel(fsVeryHigh); #endregion #region Système Inference var fuzzyDb = new Database(); //2 inputs : valence et arousal fuzzyDb.AddVariable(lvValence); fuzzyDb.AddVariable(lvArousal); //5 outputs : boredom, challenge, excitement, frustration, and fun fuzzyDb.AddVariable(lvBoredom); fuzzyDb.AddVariable(lvChallenge); fuzzyDb.AddVariable(lvExcitement); fuzzyDb.AddVariable(lvFrustration); fuzzyDb.AddVariable(lvFun); // Creation system inference // Initialise la methode de défuzzification : centre de gravité var inferenceSys = new InferenceSystem(fuzzyDb, new CentroidDefuzzifier(1000)); // Ajout des regles inferenceSys.NewRule("Rule 23", "IF Arousal IS NOT VeryLow AND Valence IS MidHigh THEN Fun IS Low"); inferenceSys.NewRule("Rule 24", "IF Arousal IS NOT Low AND Valence IS MidHigh THEN Fun IS Low"); inferenceSys.NewRule("Rule 25", "IF Arousal IS NOT VeryLow AND Valence IS High THEN Fun IS Mid"); inferenceSys.NewRule("Rule 26", "IF Valence IS VeryHigh THEN Fun IS High"); inferenceSys.NewRule("Rule 27", "IF Arousal IS MidHigh AND Valence IS MidLow THEN Challenge IS Low"); inferenceSys.NewRule("Rule 28", "IF Arousal IS MidHigh AND Valence IS MidHigh THEN Challenge IS Low"); inferenceSys.NewRule("Rule 29", "IF Arousal IS High AND Valence IS MidLow THEN Challenge IS Mid"); inferenceSys.NewRule("Rule 30", "IF Arousal IS High AND Valence IS MidHigh THEN Challenge IS Mid"); inferenceSys.NewRule("Rule 31", "IF Arousal IS VeryHigh AND Valence IS MidLow THEN Challenge IS High"); inferenceSys.NewRule("Rule 32", "IF Arousal IS VeryHigh AND Valence IS MidHigh THEN Challenge IS High"); inferenceSys.NewRule("Rule 33", "IF Arousal IS MidLow AND Valence IS MidLow THEN Boredom IS Low"); inferenceSys.NewRule("Rule 34", "IF Arousal IS MidLow AND Valence IS Low THEN Boredom IS Mid"); inferenceSys.NewRule("Rule 35", "IF Arousal IS Low AND Valence IS Low THEN Boredom IS Mid"); inferenceSys.NewRule("Rule 36", "IF Arousal IS Low AND Valence IS MidLow THEN Boredom IS Mid"); inferenceSys.NewRule("Rule 37", "IF Arousal IS MidLow AND Valence IS VeryLow THEN Boredom IS High"); inferenceSys.NewRule("Rule 38", "IF Arousal IS Low AND Valence IS VeryLow THEN Boredom IS High"); inferenceSys.NewRule("Rule 39", "IF Arousal IS VeryLow AND Valence IS VeryLow THEN Boredom IS High"); inferenceSys.NewRule("Rule 40", "IF Arousal IS VeryLow AND Valence IS Low THEN Boredom IS High"); inferenceSys.NewRule("Rule 41", "IF Arousal IS VeryLow AND Valence IS MidLow THEN Boredom IS High"); inferenceSys.NewRule("Rule 42", "IF Arousal IS VeryHigh AND Valence IS MidLow THEN Frustration IS High"); inferenceSys.NewRule("Rule 43", "IF Arousal IS MidHigh AND Valence IS Low THEN Frustration IS Mid"); inferenceSys.NewRule("Rule 44", "IF Arousal IS High AND Valence IS Low THEN Frustration IS Mid"); inferenceSys.NewRule("Rule 45", "IF Arousal IS High AND Valence IS MidLow THEN Frustration IS Mid"); inferenceSys.NewRule("Rule 46", "IF Arousal IS MidHigh AND Valence IS VeryLow THEN Frustration IS High"); inferenceSys.NewRule("Rule 47", "IF Arousal IS High AND Valence IS VeryLow THEN Frustration IS High"); inferenceSys.NewRule("Rule 48", "IF Arousal IS VeryHigh AND Valence IS VeryLow THEN Frustration IS High"); inferenceSys.NewRule("Rule 49", "IF Arousal IS VeryHigh AND Valence IS Low THEN Frustration IS High"); inferenceSys.NewRule("Rule 50", "IF Arousal IS VeryHigh AND Valence IS MidLow THEN Frustration IS High"); inferenceSys.NewRule("Rule 51", "IF Arousal IS VeryLow AND Valence IS VeryLow THEN Challenge IS VeryLow"); inferenceSys.NewRule("Rule 52", "IF Arousal IS Low AND Valence IS VeryLow THEN Challenge IS VeryLow"); inferenceSys.NewRule("Rule 53", "IF Valence IS High THEN Challenge IS VeryLow"); inferenceSys.NewRule("Rule 53b", "IF Valence IS High THEN Boredom IS VeryLow"); inferenceSys.NewRule("Rule 53c", "IF Valence IS High THEN Frustration IS VeryLow"); inferenceSys.NewRule("Rule 54", "IF Valence IS VeryHigh THEN Challenge IS VeryLow"); inferenceSys.NewRule("Rule 54b", "IF Valence IS VeryHigh THEN Boredom IS VeryLow "); inferenceSys.NewRule("Rule 54c", "IF Valence IS VeryHigh THEN Frustration IS VeryLow "); inferenceSys.NewRule("Rule 55", "IF Valence IS MidHigh THEN Boredom IS VeryLow"); inferenceSys.NewRule("Rule 55b", "IF Valence IS MidHigh THEN Frustration IS VeryLow"); inferenceSys.NewRule("Rule 56", "IF Arousal IS VeryLow THEN Challenge IS VeryLow"); inferenceSys.NewRule("Rule 56b", "IF Arousal IS VeryLow THEN Frustration IS VeryLow"); inferenceSys.NewRule("Rule 57", "IF Arousal IS Low THEN Challenge IS VeryLow"); inferenceSys.NewRule("Rule 57b", "IF Arousal IS Low THEN Frustration IS VeryLow"); inferenceSys.NewRule("Rule 58", "IF Arousal IS MidLow THEN Challenge IS VeryLow"); inferenceSys.NewRule("Rule 58b", "IF Arousal IS MidLow THEN Frustration IS VeryLow"); inferenceSys.NewRule("Rule 59", "IF Arousal IS MidHigh THEN Boredom IS VeryLow"); inferenceSys.NewRule("Rule 60", "IF Arousal IS High THEN Boredom IS VeryLow"); inferenceSys.NewRule("Rule 61", "IF Arousal IS VeryHigh THEN Boredom IS VeryLow"); inferenceSys.NewRule("Rule 62", "IF Arousal IS VeryLow AND Valence IS MidHigh THEN Fun IS VeryLow"); inferenceSys.NewRule("Rule 63", "IF Arousal IS Low AND Valence IS MidHigh THEN Fun IS VeryLow"); inferenceSys.NewRule("Rule 64", "IF Arousal IS VeryLow AND Valence IS High THEN Fun IS Low"); inferenceSys.NewRule("Rule 65", "IF Valence IS MidLow THEN Fun IS VeryLow"); inferenceSys.NewRule("Rule 66", "IF Arousal IS VeryLow AND Valence IS High THEN Boredom IS Low"); inferenceSys.NewRule("Rule 67", "IF Arousal IS Low AND Valence IS MidHigh THEN Boredom IS Low"); inferenceSys.NewRule("Rule 68", "IF Arousal IS VeryLow AND Valence IS MidHigh THEN Boredom IS Mid"); inferenceSys.NewRule("Rule 69", "IF Arousal IS VeryHigh AND Valence IS VeryLow THEN Challenge IS Mid"); inferenceSys.NewRule("Rule 70", "IF Arousal IS VeryHigh AND Valence IS VeryHigh THEN Challenge IS Mid"); inferenceSys.NewRule("Rule 71", "IF Arousal IS High AND Valence IS Low THEN Challenge IS Low"); inferenceSys.NewRule("Rule 72", "IF Arousal IS High AND Valence IS High THEN Challenge IS Low"); inferenceSys.NewRule("Rule 73", "IF Arousal IS VeryHigh AND Valence IS Low THEN Challenge IS High"); inferenceSys.NewRule("Rule 74", "IF Arousal IS VeryHigh AND Valence IS High THEN Challenge IS High"); inferenceSys.NewRule("Rule 75", "IF Arousal IS MidHigh AND Valence IS MidHigh THEN Excitement IS Low"); inferenceSys.NewRule("Rule 76", "IF Arousal IS High AND Valence IS MidHigh THEN Excitement IS Mid"); inferenceSys.NewRule("Rule 77", "IF Arousal IS High AND Valence IS High THEN Excitement IS Mid"); inferenceSys.NewRule("Rule 78", "IF Arousal IS MidHigh AND Valence IS High THEN Excitement IS Mid"); inferenceSys.NewRule("Rule 79", "IF Arousal IS VeryHigh AND Valence IS MidHigh THEN Excitement IS High"); inferenceSys.NewRule("Rule 80", "IF Arousal IS VeryHigh AND Valence IS High THEN Excitement IS High"); inferenceSys.NewRule("Rule 81", "IF Arousal IS VeryHigh AND Valence IS VeryHigh THEN Excitement IS High"); inferenceSys.NewRule("Rule 82", "IF Arousal IS High AND Valence IS VeryHigh THEN Excitement IS High"); inferenceSys.NewRule("Rule 83", "IF Arousal IS MidHigh AND Valence IS VeryHigh THEN Excitement IS High"); inferenceSys.NewRule("Rule 83b", "IF Arousal IS MidLow THEN Excitement IS VeryLow"); inferenceSys.NewRule("Rule 84", "IF Arousal IS MidLow THEN Excitement IS VeryLow"); inferenceSys.NewRule("Rule 85", "IF Arousal IS Low THEN Excitement IS VeryLow"); inferenceSys.NewRule("Rule 86", "IF Arousal IS VeryLow THEN Excitement IS VeryLow"); inferenceSys.NewRule("Rule 87", "IF Valence IS VeryLow THEN Excitement IS VeryLow"); inferenceSys.NewRule("Rule 88", "IF Valence IS Low THEN Excitement IS VeryLow"); inferenceSys.NewRule("Rule 89", "IF Valence IS MidLow THEN Excitement IS VeryLow"); #endregion #region Exemple //Pour toutes les valeurs des listes float valValence = -1f, valArousal = -1f; for (int i = 0; i < Valence.Count; i++) { valValence = (float)Valence[i]; valArousal = (float)Arousal[i]; // Initialise les données d'entrées inferenceSys.SetInput("Valence", valValence); inferenceSys.SetInput("Arousal", valArousal); // Evalue les données de sortie : Boredom, Challenge, Excitement, Frustration, Fun var resBoredom = -1f; var resChallenge = -1f; var resExcitement = -1f; var resFrustration = -1f; var resFun = -1f; try { resBoredom = inferenceSys.Evaluate("Boredom"); resChallenge = inferenceSys.Evaluate("Challenge"); resExcitement = inferenceSys.Evaluate("Excitement"); resFrustration = inferenceSys.Evaluate("Frustration"); resFun = inferenceSys.Evaluate("Fun"); ResLogFlou2Label.Text = "" + i + "/" + Valence.Count + "_ Valence: " + valValence + " + Arousal: " + valArousal + " = Boredom: " + resBoredom + ", Challenge: " + resChallenge + ", Excitement: " + resExcitement + ", Frustration: " + resFrustration + ", Fun: " + resFun; //resFun = inferenceSys.Evaluate("Fun"); //ResLogFlou2Label.Text = ""+i+"/"+Valence.Count+"_ Valence: " + valValence + " + Arousal: " + // valArousal + " = Boredom: " + resBoredom + ", Challenge: " + resChallenge + ", Excitement: " + // resExcitement + ", Frustration: " + resFrustration + ", Fun: " + resFun; ResLogFlou2Label.Refresh(); //Stockage des résultats dans la liste adéquate au résultat Boredom.Add((double)resBoredom); Challenge.Add((double)resChallenge); Excitement.Add((double)resExcitement); Frustration.Add((double)resFrustration); Fun.Add((double)resFun); } catch (Exception ex) { ResLogFlou2Label.Text = "" + i; ResLogFlou2Label.Refresh(); throw new Exception(string.Format("Erreur : {0}", ex.Message)); Boredom.Add(0); Challenge.Add(0); Excitement.Add(0); Frustration.Add(0); Fun.Add(0); } DataWriter(Boredom, Challenge, Excitement, Frustration, Fun); } #endregion }