コード例 #1
0
        private void btnSolve_Click(object sender, EventArgs e)
        {
            SimplexMethod sm = parseInput();

            if (sm == null)
            {
                return;
            }
            if (rbGraphicMethod.Checked)
            {
                if (sm.nVars - sm.nRestrs > 2)
                {
                    MessageBox.Show("Данную задачу нельзя решить графически в 2д");
                    return;
                }
            }

            if (rbGraphicMethod.Checked)
            {
                if (sm.nVars - sm.nRestrs == 1)
                {
                    VariablesAmount.Value = VariablesAmount.Value + 1;
                    sm = parseInput();
                    if (sm == null)
                    {
                        return;
                    }
                }

                if (sm.nVars - sm.nRestrs == 0)
                {
                    VariablesAmount.Value = VariablesAmount.Value + 1;
                    VariablesAmount.Value = VariablesAmount.Value + 1;
                    sm = parseInput();
                    if (sm == null)
                    {
                        return;
                    }
                }
            }
            if (!sm.ValidateInput())
            {
                return;
            }
            bool autoSteps = cbAutoSteps.Checked;

            if (rbMax.Checked)
            {
                sm.isMax = true;
                sm.changeFunctionnSign();
            }
            try
            {
                if (rbGraphicMethod.Checked)
                {
                    if (sm.nVars - sm.nRestrs > 2)
                    {
                        MessageBox.Show("Данную задачу нельзя решить графически в 2д");
                        return;
                    }

                    var bi = GetBasicIndexes();
                    if (bi == null)
                    {
                        return;
                    }
                    sm.setBasicIndexes(bi);
                    sm.CountGauss();
                    GraphicMethodForm form = new GraphicMethodForm(sm);
                    this.Hide();
                    form.ShowDialog();
                    this.Show();
                }
                else
                {
                    if (rbSolutionMethod.Checked)
                    {
                        var sol = GetBasicSolution();
                        if (sol == null)
                        {
                            return;
                        }
                        try
                        {
                            sm.SetBasicSolution(sol, sm.nRestrs);
                            sm.CountTable();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Неверное базисное решение");
                        }
                        SimplexMethodForm form = new SimplexMethodForm(sm, autoSteps);
                        this.Hide();
                        form.ShowDialog();
                        this.Show();
                    }
                    else
                    {
                        SimplexMethod basisTask = sm.GenerateArtificialBasisTask();

                        basisTask.CountTable();
                        SimplexMethodForm form = new SimplexMethodForm(basisTask, autoSteps, true);
                        this.Hide();
                        form.ShowDialog();
                        this.Show();

                        if (form.isFinal())
                        {
                            if (form.resultF() != 0)
                            {
                                MessageBox.Show("Ограничения системы не совместны, решений нет ");
                                return;
                            }
                            var f = paseFunction();
                            if (f == null)
                            {
                                return;
                            }
                            SimplexMethod smTask = form.GetLastTable().GeterateRestrsAfterBasis(f);
                            if (rbMax.Checked)
                            {
                                smTask.isMax = true;
                                smTask.changeFunctionnSign();
                            }
                            smTask.CountTable();
                            SimplexMethodForm form2 = new SimplexMethodForm(smTask, autoSteps);
                            this.Hide();
                            form2.ShowDialog();
                            this.Show();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
コード例 #2
0
        private void GraphicMethodForm_Load(object sender, EventArgs e)
        {
            List <Panel> panels = new List <Panel>();
            List <Label> labels = new List <Label>();

            panels.Add(panelRestr1);
            labels.Add(LabelRestr1);

            for (int i = 1; i < sm.nRestrs; i++)
            {
                panels.Add(new Panel());
                labels.Add(new Label());
                this.Controls.Add(panels[i]);
                this.Controls.Add(labels[i]);
            }

            int x = panelRestr1.Left;
            int y = LabelRestr1.Top;

            for (int i = 0; i < sm.nRestrs; i++)
            {
                panels[i].BackColor = colors[i % colors.Count];
                panels[i].Size      = panels[0].Size;

                labels[i].Text     = $"{sm.table[i, 0]}*X1 { ((sm.table[i, 1] >= 0) ? "+" : "")} {sm.table[i, 1]}*X2 <= {sm.table[i, 2]}";
                labels[i].Font     = labels[0].Font;
                labels[i].AutoSize = true;

                panels[i].Left = x;
                panels[i].Top  = y;

                labels[i].Left = x + 20;
                labels[i].Top  = y;
                y += 20;
            }



            lblFunction.Text = $"{sm.f[0]}*X1 { ((sm.f[1] >= 0) ? "+" : "")}  {sm.f[1]}*X2 { ((sm.f[2] >= 0) ? "+" : "")}   {sm.f[2]} -> min";

            if (checkInfinity())
            {
                if (sm.isMax)
                {
                    lblFAns.Text = "Infinity";
                    lblXAns.Text = "Функция не ограничена сверху";


                    lblF.Text = "Infinity";
                    lblX.Text = "Функция не ограничена сверху";
                }
                else
                {
                    lblFAns.Text = "-Infinity";
                    lblXAns.Text = "Функция не ограничена снизу";


                    lblF.Text = "-Infinity";
                    lblX.Text = "Функция не ограничена снизу";
                }
                return;
            }
            if (GetAwailableVertexs().Count != 0)
            {
                var best = countBest();
                lblX.Text = $"X*=({best.Key},{best.Value})";
                lblF.Text = $"F*={sm.CountRes(new Rational[] { best.Key, best.Value })}";

                if (sm.isMax)
                {
                    sm.changeFunctionnSign();
                }

                lblFAns.Text = $"F*={sm.CountRes(new Rational[] { best.Key, best.Value })}";
                var ans = sm.GetRealAnsver(new Rational[] { best.Key, best.Value });
                lblXAns.Text = "X*=(";
                for (int i = 0; i < ans.Length; i++)
                {
                    lblXAns.Text += (i != 0 ? "," : "") + ans[i].ToString();
                }
                lblXAns.Text += ")";
                if (sm.isMax)
                {
                    sm.changeFunctionnSign();
                }
            }
            else
            {
                lblX.Text = $"";
                lblF.Text = $"Нет решений";
            }
        }