示例#1
0
 public GraphicMethodForm(SimplexMethod sm)
 {
     InitializeComponent();
     this.sm = sm;
     start   = new Point(20, panel1.Height - 40);
     this.panel1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseWheel);
 }
        private SimplexMethod parseInput()
        {
            int vars_amount   = dataGridView1.ColumnCount - 2;
            int restrs_amount = dataGridView1.RowCount - 3;

            Rational[] f = paseFunction();
            if (f == null)
            {
                return(null);
            }
            Rational[,] restrs = new Rational[restrs_amount, vars_amount + 1];

            for (int i = 3; i < dataGridView1.Rows.Count; i++)
            {
                for (int j = 1; j < dataGridView1.Columns.Count; j++)
                {
                    string s = (string)dataGridView1.Rows[i].Cells[j].Value;

                    try
                    {
                        restrs[i - 3, j - 1] = s.ParseRational();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Неверный формат ввода ", s);
                        return(null);
                    }
                }
            }

            SimplexMethod sm = new SimplexMethod(f, restrs);

            return(sm);
        }
示例#3
0
        public SimplexMethod GenerateArtificialBasisTask()
        {
            int nVars   = this.nVars + this.nRestrs;
            int nRestrs = this.nRestrs;

            Rational[] f = new Rational[nVars + 1];
            Rational[,] r = new Rational[nRestrs, nVars + 1];

            for (int i = 0; i < this.nRestrs; i++)
            {
                if (restrs[i, this.nVars] < 0)
                {
                    for (int j = 0; j <= this.nVars; j++)
                    {
                        restrs[i, j] = -restrs[i, j];
                    }//меняем знак строки если в конце строки стоит минус
                }
            }
            for (int i = 0; i < nVars; i++)
            {
                if (i >= this.nVars)
                {
                    f[i] = 1;
                }
            }
            for (int i = 0; i < this.nRestrs; i++)
            {
                for (int j = 0; j < this.nVars; j++)
                {
                    r[i, j] = restrs[i, j];
                }
            }

            for (int i = 0; i < nRestrs; i++)
            {
                r[i, i + this.nVars] = 1;
            }

            for (int i = 0; i < nRestrs; i++)
            {
                r[i, nVars] = restrs[i, this.nVars];
            }

            SimplexMethod sm = new SimplexMethod(f, r);
            Rational[] sol   = new Rational[nVars];
            for (int i = 0; i < nRestrs; i++)
            {
                sol[this.nVars + i] = r[i, nVars];
            }

            sm.SetBasicSolution(sol, nRestrs);
            return(sm);
        }
示例#4
0
 public SimplexMethod(SimplexMethod sm)
 {
     table          = sm.table.Clone() as Rational[, ];
     basisVariables = new List <int>(sm.basisVariables);
     freeVariables  = new List <int>(sm.freeVariables);
     NStep          = sm.NStep;
     f        = sm.f.Clone() as Rational[];
     restrs   = sm.restrs.Clone() as Rational[, ];
     solution = sm.solution.Clone() as Rational[];
     nVars    = sm.nVars;
     nRestrs  = sm.nRestrs;
     isMax    = sm.isMax;
 }
示例#5
0
        private void DrawCurStep()
        {
            SimplexTable.Columns.Clear();
            SimplexTable.Rows.Clear();

            lblCurStep.Text = $"Текущий шаг {curStep} из {nSteps}";

            SimplexMethod sm = steps[curStep];

            for (int i = 0; i < sm.freeVariables.Count + 2; i++)
            {
                SimplexTable.Columns.Add("", "");
            }
            List <string> r1 = new List <string>();

            r1.Add($"X({sm.NStep})");
            for (int i = 0; i < sm.freeVariables.Count; i++)
            {
                r1.Add("X" + sm.freeVariables[i]);
            }
            r1.Add("");

            SimplexTable.Rows.Add(r1.ToArray());
            for (int i = 0; i <= sm.basisVariables.Count; i++)
            {
                List <string> r = new List <string>();
                if (i != sm.basisVariables.Count)
                {
                    r.Add("X" + sm.basisVariables[i]);
                }
                else
                {
                    r.Add("");
                }
                for (int j = 0; j < sm.freeVariables.Count + 1; j++)
                {
                    r.Add(sm.table[i, j].ToString());
                }

                SimplexTable.Rows.Add(r.ToArray());
            }

            SimplexTable.ClearSelection();
            DrawOporniyElements();
            setStyle();
            WriteSolution();
        }
示例#6
0
        public SimplexMethodForm(SimplexMethod sm, bool autoSteps, bool basisMethod = false)
        {
            InitializeComponent();
            nSteps  = 0;
            curStep = 0;
            steps   = new List <SimplexMethod>();
            steps.Add(sm);
            this.basisMethod = basisMethod;
            if (basisMethod)
            {
                btnFinish.Text = "Продолжить решение с данным базисным решением";
            }

            this.autoSteps = autoSteps;

            if (autoSteps)
            {
                doAuto();
            }
        }
示例#7
0
        public SimplexMethod GeterateRestrsAfterBasis(Rational[] f)
        {
            int nVars   = this.nVars - this.nRestrs;
            int nRestrs = this.nRestrs;

            Rational[,] r = new Rational[nRestrs, nVars + 1];
            for (int j = 0; j < freeVariables.Count; j++)
            {
                if (freeVariables[j] <= nVars)
                {
                    for (int i = 0; i < nRestrs; i++)
                    {
                        r[i, freeVariables[j] - 1] = table[i, j];
                    }
                }
            }

            for (int i = 0; i < basisVariables.Count; i++)
            {
                if (basisVariables[i] <= nVars)
                {
                    r[i, basisVariables[i] - 1] = 1;
                }
            }

            Rational[] sol = new Rational[nVars];
            for (int i = 0; i < nRestrs; i++)
            {
                sol[basisVariables[i] - 1] = table[i, nVars];
                r[i, nVars] = table[i, nVars];
            }

            SimplexMethod sm = new SimplexMethod(f, r);

            sm.SetBasicSolution(sol, nRestrs);

            return(sm);
        }
        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);
            }
        }