예제 #1
0
        private void solveProblem_Click(object sender, EventArgs e)
        {
            try
            {
                var inputData = GetInputData();

                if (inputData.Length != _variables.Count - 1)
                {
                    throw new ArgumentException($"Некорректное число входных параметров. {inputData.Length} вместо ожидаемых {_variables.Count - 1}");
                }

                ProblemConditions conditions = new ProblemConditions();
                foreach (var lingVariable in _variables)
                {
                    conditions.AddVariable(lingVariable);
                }

                foreach (var rule in _rules)
                {
                    conditions.AddRule(rule);
                }

                var problem = new Problem()
                {
                    InputData         = inputData,
                    ProblemConditions = conditions
                };

                var solveResult = _mamdaniService.SolveProblem(problem);
                var result      = solveResult.Data;

                if (!solveResult.Success || double.IsNaN(result) || double.IsInfinity(result))
                {
                    resultTextBox.Text     = "Не удалось вычислить значение.";
                    lingResultTextBox.Text = "Не удалось вычислить значение.";
                }
                else
                {
                    resultTextBox.Text = result.ToString("F");
                    var resultVariable = _variables.FirstOrDefault(x => x.IsResult);
                    var lingResultText = string.Format("{0} - {1}",
                                                       resultVariable.LingName,
                                                       resultVariable.Terms.OrderByDescending(x => x.AccessoryFunc.GetValue(result)).FirstOrDefault().Name
                                                       );
                    lingResultTextBox.Text = lingResultText;
                }

                if (!solveResult.Success)
                {
                    MessageBox.Show(solveResult.ErrorMessage, "Ошибка расчетов", MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }
            }
            catch (ArgumentException exc)
            {
                MessageBox.Show(exc.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// Решение задачи нечеткой логики
        /// </summary>
        public ExecutionResult <double> SolveProblem(ProblemConditions conditions, double[] input)
        {
            var problem = new Problem
            {
                InputData         = input,
                ProblemConditions = conditions
            };

            return(SolveProblem(problem));
        }
예제 #3
0
        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var sfd = new SaveFileDialog();

            sfd.Filter = "Файлы xml|*.xml";
            var dialogResult = sfd.ShowDialog();

            if (dialogResult == DialogResult.OK)
            {
                var filePath   = sfd.FileName;
                var conditions = new ProblemConditions(_variables, _rules);
                ProblemConditionsHelper.WriteToFile(conditions, filePath);
            }
        }
        public void MamdaniServiceTestMethod1()
        {
            var solveResultPattern       = 0.34375;
            var result                   = ProblemConditionsHelper.ReadConditionsFromXmlString(Resources.input);
            var mamdaniService           = new MamdaniService();
            ProblemConditions conditions = new ProblemConditions()
            {
                Rules = result.Data.Rules, Variables = result.Data.Variables
            };

            var problem = new Problem()
            {
                InputData         = new double[] { 0.6, 0.8 },
                ProblemConditions = conditions
            };

            var solveResult = mamdaniService.SolveProblem(problem);

            Assert.IsTrue(Math.Abs(solveResult.Data - solveResultPattern) < 1e-3);
        }
        /// <summary>
        /// Проверка ограничений
        /// </summary>
        private ExecutionResult CheckConditions(ProblemConditions problemConditions)
        {
            if (problemConditions.Variables.Count(x => x.IsResult) != 1)
            {
                return(ExecutionResult.Error("В задаче должна быть задана одна и только одна выходная переменная."));
            }

            foreach (var rule in problemConditions.Rules)
            {
                if (!rule.Conclusion.FuzzyVariable.IsResult)
                {
                    return
                        (ExecutionResult.Error(
                             "Ошибка в правилах вывода. Заключение должно быть выражением относительно результирующей переменной. Правило с ошибкой: " +
                             string.Join(" => ", rule.ToStringArray()) + ". Результирующая переменная - " +
                             problemConditions.Variables.FirstOrDefault(x => x.IsResult).Name));
                }
            }

            return(ExecutionResult.Succeded());
        }