private bool PotentialsIteration(IReadOnlyList <Point[]> teachingPoints, ref PotentialsFunction result)
        {
            var nextIteration = false;

            if (teachingPoints.Count != ClassCount)
            {
                throw new Exception();
            }

            for (var classNumber = 0; classNumber < ClassCount; classNumber++)
            {
                for (var i = 0; i < teachingPoints[classNumber].Length; i++)
                {
                    result += _correction * PartFunction(teachingPoints[classNumber][i]);

                    var index           = (i + 1) % teachingPoints[classNumber].Length;
                    var nextClassNumber = index == 0 ? (classNumber + 1) % ClassCount : classNumber;
                    var nextPoint       = teachingPoints[nextClassNumber][index];

                    _correction = GetNewCorrection(nextPoint, result, nextClassNumber);

                    if (_correction != 0)
                    {
                        nextIteration = true;
                    }
                }
            }

            return(nextIteration);
        }
Exemple #2
0
        private void teachingButton_Click(object sender, EventArgs e)
        {
            _step = pictureBox.Height / 20;

            var potentials = new PotentialsAlgorithm();
            var teaching   = new Point[2][];

            teaching[0] = new Point[2];
            teaching[1] = new Point[2];

            teaching[0][0] = new Point((int)x11NumericUpDown.Value, (int)y11NumericUpDown.Value);
            teaching[0][1] = new Point((int)x12NumericUpDown.Value, (int)y12NumericUpDown.Value);
            teaching[1][0] = new Point((int)x21NumericUpDown.Value, (int)y21NumericUpDown.Value);
            teaching[1][1] = new Point((int)x22NumericUpDown.Value, (int)y22NumericUpDown.Value);

            _points[0].Add(teaching[0][0]);
            _points[0].Add(teaching[0][1]);
            _points[1].Add(teaching[1][0]);
            _points[1].Add(teaching[1][1]);

            var potentialsResult = potentials.GetResult(teaching);

            _separetFunction     = potentialsResult.Function;
            functionTextBox.Text = String.Empty;

            if (!potentialsResult.HasWarning)
            {
                functionTextBox.Text = "Разделяющая функция: " + _separetFunction.ToString();
            }
            else
            {
                MessageBox.Show("Невозможно построить разделяющую функцию", "POTENTIALS", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            var bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);

            using (var graphics = Graphics.FromImage(bitmap))
            {
                graphics.Clear(Color.White);
                if (!potentialsResult.HasWarning)
                {
                    DrawGraph(graphics);
                }

                graphics.DrawLine(Pens.Black, 0, pictureBox.Height / 2, pictureBox.Width, pictureBox.Height / 2);
                graphics.DrawLine(Pens.Black, pictureBox.Width / 2, 0, pictureBox.Width / 2, pictureBox.Height);

                for (var i = 0; i < 2; i++)
                {
                    for (var j = 0; j < 2; j++)
                    {
                        DrawPoint(graphics, teaching[i][j], i);
                    }
                }
            }

            pictureBox.Image     = bitmap;
            testGroupBox.Enabled = !potentialsResult.HasWarning;
        }
        private static int GetNewCorrection(Point nextPoint, PotentialsFunction result, int nextClassNumber)
        {
            var functionValue = result.GetValue(nextPoint);

            if (functionValue <= 0 && nextClassNumber == 0)
            {
                return(1);
            }

            return(functionValue > 0 && nextClassNumber == 1 ? -1 : 0);
        }
        public PotentialsResult GetResult(Point[][] teachingPoints)
        {
            var function        = new PotentialsFunction(0, 0, 0, 0);
            var result          = new PotentialsResult();
            var nextIteration   = true;
            var iterationNumber = 0;

            _correction = 1;

            while (nextIteration && iterationNumber < IterationsCount)
            {
                iterationNumber++;
                nextIteration = PotentialsIteration(teachingPoints, ref function);
            }

            if (iterationNumber == IterationsCount)
            {
                result.HasWarning = true;
            }

            result.Function = function;

            return(result);
        }