public SimplexMethod(MultiCriteriaProblem prob, int criteriaInd)
 {
     Prob         = prob;
     CriteriaInd  = criteriaInd;
     SimplexTable = new double[Prob.CountConstraint + 3,
                               Prob.CountVariables + CountTotalNewVars() + 1];
     varInd         = Prob.CountVariables + 1;
     costMRow       = SimplexTable.GetLength(0) - 1;
     costRow        = costMRow - 1;
     Basis          = new int[Prob.CountConstraint];
     additionalVars = new Dictionary <int, int>();
     for (int i = 0; i < Prob.CountConstraint + 1; i++)
     {
         for (int j = 0; j < Prob.CountVariables + 1; j++)
         {
             if (i == 0 && j != 0)
             {
                 SimplexTable[0, j] = prob.CriteriaCoefficients[CriteriaInd, j - 1];
             }
             else if (i != 0 && j == 0)
             {
                 SimplexTable[i, 0] = prob.Constants[i - 1];
             }
             else if (i == 0 && j == 0)
             {
                 continue;
             }
             else
             {
                 SimplexTable[i, j] = prob.ConstraintCoefficients[i - 1, j - 1];
             }
         }
     }
 }
예제 #2
0
 public PenaltyMethod(MultiCriteriaProblem prob, double[] idealF, double[] simplexX, double epsilon, double epsilonGrad, double alphaK, double step)
 {
     Prob                  = prob;
     IdealF                = idealF;
     this.epsilon          = epsilon;
     this.epsilonGrad      = epsilonGrad;
     this.simplexX         = simplexX;
     AlphaK                = alphaK;
     stepPenalty           = step;
     PenaltyIterations     = new List <double[]>();
     gradDescentIterations = new List <double[]>();
 }
 private void buttonGenerate_Click(object sender, EventArgs e)
 {
     panel1.Controls.Clear();
     prob        = null;
     countCrit   = Convert.ToInt32(textBoxCountCriteria.Text);
     countVar    = Convert.ToInt32(textBoxCountVar.Text);
     countConstr = Convert.ToInt32(textBoxCountConstraint.Text);
     createUIProblem(countCrit, countVar, countConstr, true);
     buttonComputeF.Visible = true;
     buttonSaveProb.Visible = true;
     labelOptF.Visible      = false;
     labelProblem.Visible   = false;
     textBoxProb.Visible    = false;
 }
        private void buttonCompute_Click(object sender, EventArgs e)
        {
            bool[] minimize = new bool[countCrit];
            double[,] criteriaCoefficients   = new double[countCrit, countVar];
            double[,] constraintCoefficients = new double[countConstr, countVar];
            double[]      constants       = new double[countConstr];
            MathSign[]    constraintSigns = new MathSign[countConstr];
            int[]         NotNonNegativeVarInd;
            List <string> comboboxText = new List <string>();

            foreach (Control c in panel1.Controls)
            {
                if (c is ComboBox)
                {
                    comboboxText.Add(((ComboBox)c).SelectedItem.ToString());
                }
            }
            for (int i = 0; i < countCrit; i++)
            {
                minimize[i] = comboboxText[i] == "MIN";
            }
            for (int i = countCrit; i < countCrit + countConstr; i++)
            {
                switch (comboboxText[i])
                {
                case "<=": constraintSigns[i - countCrit] = MathSign.LessThan; break;

                case ">=": constraintSigns[i - countCrit] = MathSign.GreaterThan; break;

                case "=": constraintSigns[i - countCrit] = MathSign.Equal; break;
                }
            }
            List <int> tempNotNonNeg = new List <int>();

            for (int i = countCrit + countConstr; i < countCrit + countConstr + countVar; i++)
            {
                if (comboboxText[i] == " ")
                {
                    tempNotNonNeg.Add(i - countCrit - countConstr);
                }
            }
            NotNonNegativeVarInd = tempNotNonNeg.ToArray();
            for (int i = 0; i < criteriaTable.RowCount; i++)
            {
                for (int j = 0; j < criteriaTable.ColumnCount; j++)
                {
                    criteriaCoefficients[i, j] = Convert.ToDouble(criteriaTable[j, i].Value);
                }
            }
            for (int i = 0; i < coeffTable.RowCount; i++)
            {
                for (int j = 0; j < coeffTable.ColumnCount; j++)
                {
                    constraintCoefficients[i, j] = Convert.ToDouble(coeffTable[j, i].Value);
                }
            }
            for (int i = 0; i < constTable.RowCount; i++)
            {
                constants[i] = Convert.ToDouble(constTable[0, i].Value);
            }
            prob       = new MultiCriteriaProblem(minimize, criteriaCoefficients, constraintCoefficients, constants, constraintSigns, NotNonNegativeVarInd);
            solutionsF = new double[prob.Minimize.Length];
            SimplexMethod sm;

            labelOptF.Text = "(";
            simplexXOpt    = new List <double[]>();
            double[] x = new double[prob.CountVariables];
            for (int i = 0; i < prob.CountCriteria; i++)
            {
                sm = new SimplexMethod(prob, i);
                Tuple <double, double[]> res;
                res = sm.Calculate();
                if (res != null)
                {
                    solutionsF[i] = res.Item1;
                    simplexXOpt.Add(res.Item2);
                }
                else
                {
                    solutionsF  = null;
                    simplexXOpt = null;
                    break;
                }
                labelOptF.Text += solutionsF[i] + " ";
            }
            if (solutionsF != null)
            {
                labelOptF.Text   += ")";
                labelOptF.Visible = true;
                labelProblem.Text = "Решается следующая задача на заданном множестве ограничений:";
                textBoxProb.Text  = "min ";
                for (int i = 0; i < prob.CriteriaCoefficients.GetLength(0); i++)
                {
                    textBoxProb.Text += "(";
                    for (int j = 0; j < prob.CriteriaCoefficients.GetLength(1); j++)
                    {
                        textBoxProb.Text += prob.CriteriaCoefficients[i, j] + "X" + (j + 1);
                        if (j != prob.CriteriaCoefficients.GetLength(1) - 1)
                        {
                            textBoxProb.Text += (prob.CriteriaCoefficients[i, j + 1] >= 0) ? "+" : "";
                        }
                    }
                    textBoxProb.Text += (solutionsF[i] > 0) ? "-" : "+";
                    textBoxProb.Text += Math.Abs(Math.Round(solutionsF[i], 1));
                    textBoxProb.Text += ")^2";
                    if (i != prob.CriteriaCoefficients.GetLength(0) - 1)
                    {
                        textBoxProb.Text += " + ";
                    }
                }
                labelProblem.Visible         = true;
                textBoxProb.Visible          = true;
                buttonComputePenalty.Visible = true;
            }
            else
            {
                labelOptF.Text    = "Система ограничений задачи несовместна";
                labelOptF.Visible = true;
            }
        }
        private void buttonOpenFromFile_Click(object sender, EventArgs e)
        {
            prob = null;
            panel1.Controls.Clear();
            labelOptF.Visible    = false;
            labelProblem.Visible = false;
            textBoxProb.Visible  = false;
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.DefaultExt = ".txt";
            dlg.Filter     = "Текстовый документ (.txt)| *.txt";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string filename  = dlg.FileName;
                string extension = Path.GetExtension(filename);
                try
                {
                    if (extension == ".txt")
                    {
                        string[] fileLines = File.ReadAllLines(filename);
                        if (fileLines.Length == 0)
                        {
                            throw new Exception("Пустой файл!");
                        }
                        string[] split = fileLines[0].Split(' ');
                        if (!Int32.TryParse(split[0], out countCrit))
                        {
                            throw new Exception(wrongFileFormatMessage);
                        }
                        if (!Int32.TryParse(split[1], out countConstr))
                        {
                            throw new Exception(wrongFileFormatMessage);
                        }
                        if (!Int32.TryParse(split[2], out countVar))
                        {
                            throw new Exception(wrongFileFormatMessage);
                        }

                        bool[] minimize = new bool[countCrit];
                        double[,] criteriaCoefficients   = new double[countCrit, countVar];
                        double[,] constraintCoefficients = new double[countConstr, countVar];
                        double[]   constants       = new double[countConstr];
                        MathSign[] constraintSigns = new MathSign[countConstr];
                        int[]      NotNonNegativeVarInd;
                        for (int i = 1; i <= countCrit; i++)
                        {
                            string   line     = fileLines[i];
                            string[] splitStr = line.Split(' ');
                            minimize[i - 1] = splitStr[0] == "1";
                            for (int j = 1; j <= countVar; j++)
                            {
                                if (!Double.TryParse(splitStr[j], out criteriaCoefficients[i - 1, j - 1]))
                                {
                                    throw new Exception(wrongFileFormatMessage);
                                }
                            }
                        }
                        for (int i = countCrit + 1; i < countConstr + countCrit + 1; i++)
                        {
                            string   line     = fileLines[i];
                            string[] splitStr = line.Split(' ');
                            for (int j = 0; j < countVar; j++)
                            {
                                if (!Double.TryParse(splitStr[j], out constraintCoefficients[i - countCrit - 1, j]))
                                {
                                    throw new Exception(wrongFileFormatMessage);
                                }
                            }
                            if (!Double.TryParse(splitStr[countVar + 1], out constants[i - countCrit - 1]))
                            {
                                throw new Exception(wrongFileFormatMessage);
                            }
                            switch (splitStr[countVar])
                            {
                            case "<=": constraintSigns[i - countCrit - 1] = MathSign.LessThan; break;

                            case ">=": constraintSigns[i - countCrit - 1] = MathSign.GreaterThan; break;

                            case "=": constraintSigns[i - countCrit - 1] = MathSign.Equal; break;

                            default: throw new Exception(wrongFileFormatMessage);
                            }
                        }
                        if (fileLines.Length == countConstr + countCrit + 2)
                        {
                            string[] splitStr = fileLines[countConstr + countCrit + 1].Split(' ');
                            NotNonNegativeVarInd = new int[splitStr.Length];
                            for (int i = 0; i < splitStr.Length; i++)
                            {
                                if (!Int32.TryParse(splitStr[i], out NotNonNegativeVarInd[i]))
                                {
                                    throw new Exception(wrongFileFormatMessage);
                                }
                            }
                        }
                        else
                        {
                            NotNonNegativeVarInd = null;
                        }
                        try
                        {
                            prob = new MultiCriteriaProblem(minimize, criteriaCoefficients, constraintCoefficients, constants, constraintSigns, NotNonNegativeVarInd);
                            createUIProblem(countCrit, countVar, countConstr, false);
                        }
                        catch (Exception exc)
                        {
                            MessageBox.Show(exc.Message);
                        }
                    }
                    buttonComputeF.Visible = true;
                    buttonSaveProb.Visible = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }