예제 #1
0
        public static List <Series> Isolines(Optimize.BasicData input)
        {
            double x1step    = input.Bounds.X1Interval / (double)x1stepcount;
            double x2step    = input.Bounds.X2Interval / (double)x2stepcount;
            var    list      = new List <Point>();
            var    list_good = new List <Point>();

            for (int i = 0; i < x1stepcount; i++)
            {
                double x1 = input.Bounds.X1Lower + x1step * i;
                for (int j = 0; j < x2stepcount; j++)
                {
                    double x2 = input.Bounds.X2Lower + x2step * j;
                    double y  = input.Function.Calc(x1, x2);
                    list.Add(new Point(x1, x2, y));
                    if (input.CheckRestrictions(x1, x2))
                    {
                        list_good.Add(new Point(x1, x2, y));
                    }
                }
            }
            ;

            double min  = list.AsParallel().Min(p => p.Y);
            double max  = list.AsParallel().Max(p => p.Y);
            double step = (max - min) / steps;
            var    ss   = new List <Series>();

            ss.Add(PlacePoints(list_good, $"Restrictions", Color.LightGreen, 1));
            System.Threading.Tasks.Parallel.For(1, steps, (i) =>
            {
                int ey         = 1 + MathExpr.GetPrecision(min + i * step);
                double y       = MathExpr.Round(min + i * step, ey);
                double delta_y = Math.Pow(step, -ey) / 1.5;
                var list2      = list.Where(p => p.Y > y - delta_y && p.Y < y + delta_y);
                var s          = PlacePoints(list2, $"Iso{i}", SystemColors.Highlight, 1);
                int index      = new Random().Next(s.Points.Count / 3, s.Points.Count / 3 * 2);
                if (index > 0)
                {
                    s.Points[index].Label = y.ToString();
                }
                ss.Add(s);
            });
            return(ss);
        }
예제 #2
0
파일: MainForm.cs 프로젝트: a1ex2k/OPR
        Optimize.BasicData PrepareValues()
        {
            Optimize.BasicData data = null;
            try
            {
                var func = new MathExpr(funcTextBox.Text);
                logstr += $"y={funcTextBox.Text}\u2192{criteriaComboBox.Text}";
                var restr = new MathExpr[limitsGridView.Rows.Count];
                for (int i = 0; i < limitsGridView.Rows.Count; i++)
                {
                    string expr = limitsGridView[0, i].Value.ToString() + limitsGridView[1, i].Value.ToString() + limitsGridView[2, i].Value.ToString();
                    logstr  += ", " + expr;
                    restr[i] = new MathExpr(expr);
                }
                double x1min, x1max, x2min, x2max;
                x1min = Double.Parse(x1ltextBox.Text);
                x1max = Double.Parse(x1utextBox.Text);
                x2min = Double.Parse(x2ltextBox.Text);
                x2max = Double.Parse(x2utextBox.Text);
                Double.Parse(par1TextBox.Text);
                Double.Parse(textBox4.Text);
                Double.Parse(textBox3.Text);
                Double.Parse(textBox2.Text);
                uint.Parse(par2TextBox.Text);
                double par_eps = Double.Parse(textBox1.Text);

                var bounds = new Optimize.BasicData.VarBounds(x1min, x1max, x2min, x2max);
                logstr += " " + bounds;

                data = new Optimize.BasicData(func, crit, restr, bounds);
            }
            catch
            {
                MessageBox.Show("Не удалось обработать введённые данные. Процесс отменён.");
                logstr += "  -  Неудача!";
            }
            return(data);
        }
예제 #3
0
파일: MainForm.cs 프로젝트: a1ex2k/OPR
        public void Run()
        {
            Optimize.BasicData data = PrepareValues();
            if (data == null)
            {
                return;
            }
            SetChartArea(data.Bounds);
            Plotting.Isolines(data).ForEach(s => chart.Series.Add(s));

            Optimize opt;

            switch (method)
            {
            case 0:
                double par_step  = Double.Parse(par1TextBox.Text);
                double par_step2 = Double.Parse(textBox4.Text);
                opt    = new GridBruteForce(data, par_step);
                logstr = $"{methodcomboBox.Text}, Шаг: {par_step} - " + logstr; break;

            case 1:
                uint par_count = uint.Parse(par2TextBox.Text);
                opt    = new MonteCarlo(data, par_count);
                logstr = $"{methodcomboBox.Text} - " + logstr; break;

            case 2:
                double par_eps    = Double.Parse(textBox1.Text);
                Point  startPoint = null;
                if (enableStartPoint.Checked == true)
                {
                    double x1_0 = Double.Parse(textBox3.Text);
                    double x2_0 = Double.Parse(textBox2.Text);
                    startPoint = new Point(x1_0, x2_0, data.Function.Calc(x1_0, x2_0));
                }
                par_step  = Double.Parse(par1TextBox.Text);
                par_step2 = Double.Parse(textBox4.Text);
                opt       = new HookeJeeves(data, par_eps, par_step, par_step2, startPoint);
                logstr    = $"{methodcomboBox.Text}, Точность: {par_eps} " + logstr; break;

            case 3: return; break;

            default: return; break;
            }

            try
            {
                opt.RunOptimization();
                resultlabel.Text = $"{opt.Result}\r\nДлительность: {opt.Duration} мс, {opt.Iterations} итер.";
                logstr           = $"y({opt.Result.X1},{opt.Result.X2})={opt.Result.Y}, {opt.Duration} мс, {opt.Iterations} итер. - " + logstr;
                chart.Series.Add(Plotting.PlacePoint(opt.Result, Color.Crimson));

                TableForm = new TableForm();
                if (method == 2)
                {
                    chart.Series.Add(Plotting.PlacePoints(((HookeJeeves)opt).Points, "path", Color.Black, 3, MarkerStyle.Star5, SeriesChartType.Line));
                }
                Parallel.Invoke(() =>
                {
                    switch (method)
                    {
                    case 0: TableForm.Populate((GridBruteForce)opt); break;

                    case 1: TableForm.Populate(((MonteCarlo)opt).Points); break;

                    case 2: TableForm.Populate(((HookeJeeves)opt).Points); break;

                    case 3: return; break;

                    default: break;
                    }
                });
            }
            catch (OperationCanceledException)
            {
                MessageBox.Show("Не удалось найти точки, удовлетворяющие ограничениям");
                resultlabel.Text = "-";
                logstr          += "  -  Неудача!";
            }
            listBox1.Items.Add(logstr + "\t\t");
            logstr = "";
        }