Esempio n. 1
0
        private void CalculateButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                FuzzySystem fuzzy = FuzzyReader.GetFuzzySystemFromFile(FuzzySystemText.Text);
                fuzzy.LoadInputsAndSaveOutputs(InputValuesText.Text, "FuzzySystemTemporaryTestOutputFile.temp.del");
                float MSE = FuzzySystem.GetOutputFilesMSE(OutputValuesText.Text, "FuzzySystemTemporaryTestOutputFile.temp.del");
                if (File.Exists("FuzzySystemTemporaryTestOutputFile.temp.del"))
                {
                    File.Delete("FuzzySystemTemporaryTestOutputFile.temp.del");
                }

                MSEText.Text   = MSE.ToString(CultureInfo.InvariantCulture);
                RMSEText.Text  = Math.Sqrt(MSE).ToString(CultureInfo.InvariantCulture);
                RMSEPText.Text = (Math.Sqrt(MSE) / (fuzzy.Outputs[0].Range[1] - fuzzy.Outputs[0].Range[0]) * 100.0f).ToString(CultureInfo.InvariantCulture);
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
                if (File.Exists("FuzzySystemTemporaryTestOutputFile.temp.del"))
                {
                    File.Delete("FuzzySystemTemporaryTestOutputFile.temp.del");
                }
                return;
            }
        }
Esempio n. 2
0
        private double FitnessFunctionMSE(ArrayList ActualParameters)
        {
            FuzzySystem tempFuz = GetModifiedFuzzySystem(ActualParameters);

            float MSE        = 0.0f;
            bool  readFailed = false;

            Dispatcher.Invoke(() =>
            {
                try
                {
                    tempFuz.LoadInputsAndSaveOutputs(InputFileTextBox.Text, OutputFileTextBox.Text.Replace(".txt", ".generated.txt"));
                    MSE = FuzzySystem.GetOutputFilesMSE(OutputFileTextBox.Text, OutputFileTextBox.Text.Replace(".txt", ".generated.txt"));
                }
                catch (Exception exc)
                {
                    readFailed     = true;
                    Optimizer.Stop = true;
                    MessageBox.Show(exc.Message, "Error", MessageBoxButton.OK);
                }
            });

            if (readFailed)
            {
                return(double.NaN);
            }

            //return (double)Math.Sqrt((double)MSE);
            return((double)MSE);
        }
Esempio n. 3
0
        private void GenerateButton_Click(object sender, RoutedEventArgs e)
        {
            InitializeFuzzySystem();

            // Make sure the selected values are compatible
            {
                bool invalid = true;

                if (fuzzySystem.Type == FuzzyType.Mamdani || fuzzySystem.Type == FuzzyType.Larsen)
                {
                    if (fuzzySystem.DefuzzMethod == DefuzzMethodType.COA || fuzzySystem.DefuzzMethod == DefuzzMethodType.COG || fuzzySystem.DefuzzMethod == DefuzzMethodType.MOM || fuzzySystem.DefuzzMethod == DefuzzMethodType.LOM || fuzzySystem.DefuzzMethod == DefuzzMethodType.SOM)
                    {
                        if (fuzzySystem.ImpMethod == ImpMethodType.min || fuzzySystem.ImpMethod == ImpMethodType.max)
                        {
                            invalid = false;
                        }
                    }
                }

                if (fuzzySystem.Type == FuzzyType.Sugeno)
                {
                    if (fuzzySystem.DefuzzMethod == DefuzzMethodType.wtaver || fuzzySystem.DefuzzMethod == DefuzzMethodType.wtsum)
                    {
                        if (fuzzySystem.ImpMethod == ImpMethodType.prod)
                        {
                            invalid = false;
                        }
                    }
                }

                if (invalid)
                {
                    MessageBox.Show("Incompatible fuzzy system type and defuzzification method.", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }

            var watch = System.Diagnostics.Stopwatch.StartNew();

            List <Rule> rules = new List <Rule>();

            // Generate the rules
            if (fuzzySystem.Type == FuzzyType.Sugeno)
            {
                // Get a list of inputMF-combinations
                int ruleCount = 1;
                for (int inputId = 0; inputId < fuzzySystem.Inputs.Length; inputId++)
                {
                    ruleCount *= fuzzySystem.Inputs[inputId].NumMFs;
                }

                for (int i = 0; i < ruleCount; i++)
                {
                    rules.Add(new Rule());
                    rules[rules.Count - 1].Inputs     = new int[fuzzySystem.Inputs.Length];
                    rules[rules.Count - 1].Inputs[0]  = -1; // Uninitialized rule indicator
                    rules[rules.Count - 1].Outputs    = new int[fuzzySystem.Outputs.Length];
                    rules[rules.Count - 1].Outputs[0] = i + 1;
                }

                // Prepare outputs
                fuzzySystem.Outputs[0].NumMFs = ruleCount;
                for (int mfId = 0; mfId < fuzzySystem.Outputs[0].NumMFs; mfId++)
                {
                    fuzzySystem.Outputs[0].MFs[mfId]      = new MembershipFunction();
                    fuzzySystem.Outputs[0].MFs[mfId].Type = MFtype.constant;
                }

                Random RNG = new Random();
                for (int ruleId = 0; ruleId < rules.Count; ruleId++)
                {
                    bool newRuleFound = true;
                    do
                    {
                        for (int i = 0; i < rules[ruleId].Inputs.Length; i++)
                        {
                            rules[ruleId].Inputs[i] = RNG.Next(fuzzySystem.Inputs[i].NumMFs) + 1;
                        }
                        newRuleFound = true;
                        for (int r = 0; r < ruleId; r++)
                        {
                            if (rules[r].SameInput(rules[ruleId]))
                            {
                                newRuleFound = false;
                            }
                        }
                    }while (!newRuleFound);

                    // For each input combination we calculate their weights at each of the given coordinates

                    List <int>   itrValues = new List <int>();
                    List <float> weights   = new List <float>();

                    for (int itr = 0; itr < InputCoords[0].Count; itr++)
                    {
                        float weight = 1.0f;
                        for (int inputId = 0; inputId < fuzzySystem.NumInputs; inputId++)
                        {
                            weight = Math.Min(weight, fuzzySystem.Inputs[inputId].MFs[rules[ruleId].Inputs[inputId] - 1].GetMembershipValue(InputCoords[inputId][itr]));
                        }

                        // If both weights are >0, we store these weigths (or their min/avg) and the itr value for this combination
                        if (weight > 0.0f)
                        {
                            weights.Add(weight);
                            itrValues.Add(itr);
                        }
                    }

                    // We get the weighed average of the outputs for each combination
                    // Add a new MF to the output with this value, and set the rule to it
                    float result = 0.0f;
                    for (int i = 0; i < weights.Count; i++)
                    {
                        result += weights[i] * OutputCoords[0][itrValues[i]];
                    }
                    result /= weights.Sum();

                    fuzzySystem.Outputs[0].MFs[ruleId].Params[0] = result;
                }

                // Remove invalid rules/outputs (NaN output -> no matching input-output values were found)
                // Make new lists of the valid entries
                List <Rule> validRules = new List <Rule>();
                List <MembershipFunction> validOutputMFs = new List <MembershipFunction>();
                for (int i = 0; i < ruleCount; i++)
                {
                    if (!float.IsNaN(fuzzySystem.Outputs[0].MFs[i].Params[0]))
                    {
                        validRules.Add(rules[i]);
                        validRules[validRules.Count - 1].Outputs[0] = validRules.Count;
                        validOutputMFs.Add(fuzzySystem.Outputs[0].MFs[i]);
                    }
                }
                // Overwrite original arrays with the new ones
                rules = validRules;
                fuzzySystem.Outputs[0].NumMFs = validOutputMFs.Count;
                fuzzySystem.Outputs[0].MFs    = validOutputMFs.ToArray();
            }
            else if (fuzzySystem.Type == FuzzyType.Mamdani || fuzzySystem.Type == FuzzyType.Larsen)
            {
                // for each input-output data we have (rows in the txt)
                for (int itr = 0; itr < InputCoords[0].Count; itr++)
                {
                    // Get the value of each input's MFs at the specified points
                    List <List <float> > MFvalues = new List <List <float> >();   //MFvalues[input id][MF id]
                    // Add a list foreach ioput
                    for (int i = 0; i < fuzzySystem.NumInputs + fuzzySystem.NumOutputs; i++)
                    {
                        MFvalues.Add(new List <float>());
                    }

                    // for each input
                    for (int inputId = 0; inputId < InputCoords.Count; inputId++)
                    {
                        // for each MF
                        for (int mfID = 0; mfID < fuzzySystem.Inputs[inputId].NumMFs; mfID++)
                        {
                            // Get the membership value at the given coordinate
                            MFvalues[inputId].Add(fuzzySystem.Inputs[inputId].MFs[mfID].GetMembershipValue(InputCoords[inputId][itr]));
                        }
                    }

                    // for each output
                    for (int outputId = 0; outputId < OutputCoords.Count; outputId++)
                    {
                        // for each MF
                        for (int mfID = 0; mfID < fuzzySystem.Outputs[outputId].NumMFs; mfID++)
                        {
                            // Get the membership value at the given coordinate
                            MFvalues[fuzzySystem.NumInputs + outputId].Add(fuzzySystem.Outputs[outputId].MFs[mfID].GetMembershipValue(OutputCoords[outputId][itr]));
                        }
                    }

                    // Check if we have a NaN
                    bool hasNaN = false;
                    foreach (List <float> MFlist in MFvalues)
                    {
                        foreach (float f in MFlist)
                        {
                            if (float.IsNaN(f))
                            {
                                hasNaN = true;
                            }
                        }
                    }
                    if (hasNaN)
                    {
                        continue;
                    }

                    // Get each input's MF where the membership was the highest (randomly chosen if equal)
                    int[] highestMF = new int[fuzzySystem.NumInputs + fuzzySystem.NumOutputs];
                    for (int k = 0; k < fuzzySystem.NumInputs; k++)
                    {
                        // Find highest membership value
                        float highestVal = MFvalues[k][0];
                        for (int l = 1; l < fuzzySystem.Inputs[k].NumMFs; l++)
                        {
                            highestVal = Math.Max(MFvalues[k][l], highestVal);
                        }
                        // Find MFs that have the highest membership value we just found
                        List <int> MFsWithHighestVal = new List <int>();
                        for (int l = 0; l < fuzzySystem.Inputs[k].NumMFs; l++)
                        {
                            if (MFvalues[k][l] == highestVal)
                            {
                                MFsWithHighestVal.Add(l);
                            }
                        }
                        // Choose a random one of them
                        highestMF[k] = MFsWithHighestVal[(new Random()).Next(MFsWithHighestVal.Count)];
                    }
                    // Get each output's highest MF index
                    for (int k = 0; k < fuzzySystem.NumOutputs; k++)
                    {
                        // Find highest membership value
                        float highestVal = MFvalues[fuzzySystem.NumInputs + k][0];
                        for (int l = 1; l < fuzzySystem.Outputs[k].NumMFs; l++)
                        {
                            highestVal = Math.Max(MFvalues[fuzzySystem.NumInputs + k][l], highestVal);
                        }
                        // Find MFs that have the highest membership value we just found
                        List <int> MFsWithHighestVal = new List <int>();
                        for (int l = 0; l < fuzzySystem.Outputs[0].NumMFs; l++)
                        {
                            if (MFvalues[fuzzySystem.NumInputs + k][l] == highestVal)
                            {
                                MFsWithHighestVal.Add(l);
                            }
                        }
                        // Choose a random one of them
                        highestMF[fuzzySystem.NumInputs + k] = MFsWithHighestVal[(new Random()).Next(MFsWithHighestVal.Count)];
                    }
                    // Create rule
                    Rule newRule = new Rule()
                    {
                        ConnectionType = 1,
                        Inputs         = new int[fuzzySystem.NumInputs],
                        Outputs        = new int[fuzzySystem.NumOutputs],
                        Weight         = 1.0f
                    };
                    for (int i = 0; i < fuzzySystem.NumInputs; i++)
                    {
                        newRule.Inputs[i] = highestMF[i] + 1;
                    }
                    for (int i = 0; i < fuzzySystem.NumOutputs; i++)
                    {
                        newRule.Outputs[i] = highestMF[fuzzySystem.NumInputs + i] + 1;
                    }

                    // Add to rule list if the inputs don't match any existing rule
                    bool isRuleNew = true;
                    foreach (Rule rule in rules)
                    {
                        if (rule.SameInput(newRule))
                        {
                            isRuleNew = false;
                            break;
                        }
                    }
                    if (isRuleNew)
                    {
                        rules.Add(newRule);
                    }
                }
            }

            foreach (Rule rule in rules)
            {
                rule.ConnectionType = 1;
                rule.Weight         = 1.0f;
            }
            fuzzySystem.Rules = rules.ToArray();

            watch.Stop();

            GeneratedFuzzyBox.Text = fuzzySystem.ToString(IncludeValuesCheckBox.IsChecked == true);

            SaveButton.IsEnabled     = true;
            OptimizeButton.IsEnabled = true;
            UseButton.IsEnabled      = true;

            float MSE;

            try
            {
                fuzzySystem.LoadInputsAndSaveOutputs(InputFileTextBox.Text, OutputFileTextBox.Text.Replace(".txt", ".generated.txt"));
                MSE = FuzzySystem.GetOutputFilesMSE(OutputFileTextBox.Text, OutputFileTextBox.Text.Replace(".txt", ".generated.txt"));
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "Error", MessageBoxButton.OK);
                return;
            }

            MessageBox.Show("MSE: " + MSE.ToString(CultureInfo.InvariantCulture) + "\r\nRMSE: " + Math.Sqrt(MSE).ToString(CultureInfo.InvariantCulture) + "\r\nRMSEP: " + (Math.Sqrt(MSE) / (fuzzySystem.Outputs[0].Range[1] - fuzzySystem.Outputs[0].Range[0]) * 100.0f).ToString(CultureInfo.InvariantCulture) + "%\r\nGeneration time in milliseconds: " + watch.ElapsedMilliseconds, "Generation successful!", MessageBoxButton.OK);
        }