示例#1
0
文件: Form1.cs 项目: sasha2567/diplom
        private void button2_Click(object sender, EventArgs e)
        {
            SecondLevel secondLevel = new SecondLevel();

            secondLevel.InitialConditions(Convert.ToInt16(numericTypeCount.Value));


            List <List <int> > A = new List <List <int> >();

            for (int i = 0; i < 5; i++)
            {
                List <int> w = new List <int>();
                A.Add(w);
            }
            A[0].Add(12); A[0].Add(12); A[0].Add(2); A[0].Add(2);
            A[1].Add(12); A[1].Add(12); A[1].Add(2); A[1].Add(2);
            A[2].Add(12); A[2].Add(13); A[2].Add(3);
            A[3].Add(12); A[3].Add(10); A[3].Add(2); A[3].Add(2); A[3].Add(2);
            A[4].Add(12); A[4].Add(8); A[4].Add(2); A[4].Add(2); A[4].Add(2); A[4].Add(2);

            secondLevel.GenerateSolution(A);
        }
示例#2
0
        /*
         * Алгоритм формирования решения по составам паритй всех типов данных
         *
         */
        public void GenerateSolution()
        {
            this.GenerateStartSolution();
            SecondLevel        secondLevel = new SecondLevel();
            List <List <int> > temp        = this.CopyMatrix(this.A);

            secondLevel.GenerateSolution(temp);
            List <List <int> > tmpMatrixA = secondLevel.ReturnAMatrix();

            this.k  = 0;
            this.f1 = this.GetCriterion(tmpMatrixA);

            /*if (this.f1 == 0)
             * {
             *  this.f1 = 99999;
             * }*/
            //this.f1 = 20;
            this.f1Buf = this.f1;
            int currentChangeType = 0;
            //Добавить вычисление значения критерия
            List <List <int> > MaxA = this.CopyMatrix(this.A);
            int    maxF1            = this.f1;
            string s = "";

            if (flag == true)
            {
                while (!this.CheckType(this.I))
                {
                    this.solutionFlag = false;
                    //1 - Копируем I в Ii
                    for (int j = 0; j < this.countType; j++)
                    {
                        this.Ii[j] = this.I[j];
                    }
                    this.ABuf = this.CopyMatrix(this.A);
                    //this.f1 = this.GetCriterion(this.ABuf);
                    //Для каждого рассматриваемого типа
                    for (int iter = 0; iter < this.Ii.Count; iter++)
                    {
                        if (this.Ii[iter] != 0)
                        {
                            this.i = iter;
                            if (this.np1i[this.i] > 0)
                            {
                                this.A1 = this.CopyMatrix(this.ABuf);
                                //MessageBox.Show("Решение по составу партий данных " + (this.i + 1) + " типа на " + (this.k + 1) + " шаге алгоритма");
                                //Получение состава партий фиксированного типа
                                List <List <int> > toBatchAlgoritm = new List <List <int> >();
                                toBatchAlgoritm.Add(new List <int>());
                                toBatchAlgoritm.Add(new List <int>());
                                toBatchAlgoritm[1] = this.A1[this.i + 1];
                                test = new BatchTypeClaims(this.f1, this.i + 1, this.countClaims[this.i], toBatchAlgoritm, this.A1);
                                test.GenerateSolution();

                                //test.PrintMatrix(2);
                                //test.PrintMatrix(3);

                                List <List <int> > tempA2 = test.ReturnMatrix(3);
                                if (tempA2.Count < 2)
                                {
                                    this.mi[this.i]++;
                                    this.A2 = new List <List <int> >();
                                    this.A2.Add(new List <int>());
                                    this.A2.Add(new List <int>());
                                    this.A2[1].Add(0);
                                    this.A2[1].Add(0);
                                    int sum = 0;
                                    for (int j = 1; j < this.mi[this.i]; j++)
                                    {
                                        this.A2[1].Add(2);
                                        sum += 2;
                                    }
                                    this.A2[1][1] = this.countClaims[this.i] - sum;
                                    if (!this.CheckingMatrix(1) && !this.CheckingMatrix(2))
                                    {
                                        this.I[this.i]  = 0;
                                        this.mi[this.i] = 0;
                                        continue;
                                    }
                                }
                                else
                                {
                                    //this.A2 = this.CopyMatrix(tempA2);
                                    this.A2 = new List <List <int> >();
                                    this.A2.Add(new List <int>());
                                    for (int ii = 1; ii < tempA2.Count; ii++)
                                    {
                                        //this.A2.Add(new List<int>());
                                        this.A2.Insert(ii, tempA2[ii]);
                                        this.A2[ii].Insert(0, 0);
                                    }
                                }

                                //Буферизируем текущее решение для сравнения
                                this.A1i = this.CopyMatrix(this.A1);

                                for (this.q2 = 1; this.q2 < this.A2.Count; this.q2++)
                                {
                                    this.A1i[this.i + 1] = this.A2[this.q2];
                                    int f1g = 0;
                                    secondLevel = new SecondLevel();
                                    List <List <int> > tempA = CopyMatrix(this.A1i);
                                    secondLevel.GenerateSolution(tempA);
                                    List <List <int> > tempMatrixA = secondLevel.ReturnAMatrix();
                                    f1g = this.GetCriterion(tempMatrixA);
                                    Random rand = new Random();
                                    int    ret  = rand.Next(5, 15);
                                    //if(ret < 10)
                                    if (f1g > this.f1Buf)
                                    //if (f1g - this.f1Buf < 0 || f1g == 0)
                                    {
                                        this.f1Buf        = f1g;
                                        this.solutionFlag = true;
                                        this.ABuf         = this.CopyMatrix(this.A1i);
                                    }
                                }
                            }
                        }
                    }
                    this.k++;
                    if (this.solutionFlag)
                    {
                        this.f1 = this.f1Buf;
                        this.A  = this.CopyMatrix(ABuf);
                        MaxA    = this.CopyMatrix(ABuf);
                        maxF1   = this.f1Buf;
                        s       = "";
                        foreach (List <int> row in MaxA)
                        {
                            foreach (int colum in row)
                            {
                                s += colum + ", ";
                            }
                            s += "\n";
                        }
                        MessageBox.Show(s);
                    }
                    else
                    {
                        /*
                         * //this.A = new List<List<int>>();
                         * //this.A.Add(new List<int>());
                         * this.A[currentChangeType + 1].Clear();
                         * //for (int ii = 0; ii < this.I.Count; ii++)
                         * //{
                         * //this.A.Add(new List<int>());
                         * this.A[currentChangeType + 1].Add(0);
                         * this.A[currentChangeType + 1].Add(0);
                         * int sum = 0;
                         * //this.mi[currentChangeType]++;
                         * for (int j = 1; j < this.mi[currentChangeType]; j++)
                         * {
                         *  this.A[currentChangeType + 1].Add(2);
                         *  sum += 2;
                         * }
                         * this.A[currentChangeType + 1][1] = this.countClaims[currentChangeType] - sum;
                         * currentChangeType++;
                         * if (currentChangeType == this.countType)
                         * {
                         *  currentChangeType = 0;
                         * }
                         * //}
                         */
                    }
                }
            }
            MessageBox.Show("На текущем " + this.k + " шаге получено решение");
            s = "";
            foreach (List <int> row in MaxA)
            {
                foreach (int colum in row)
                {
                    s += colum + ", ";
                }
                s += "\n";
            }
            MessageBox.Show(s);
            MessageBox.Show("Количество обработанных требований " + maxF1);
        }
示例#3
0
        /*
         * Основная функция работы алгоритма
         *
         */
        public bool GenerateSolution()
        {
            if (this.inMatrixFlag)
            {
                return(false);
            }
Nachalo:
            bool step5 = false;

            this.q1 = 1;
            this.h  = 2;
            this.j  = 1;
            while (this.q1 <= this.np1)
            {
                if (!step5)
                {
                    step5 = false;
                    this.q2++;
                    this.FormationDecisionPartMakeup();
                    this.np2++;
                    if (!this.CheckingMatrix(1))
                    {
                        this.A2.RemoveAt(this.q2);
                        this.q2--;
                        this.np2--;
                    }
                }
                if (this.h + this.j <= this.A1[this.q1].Count - 1)
                {
                    if (this.CheckingMatrix(2))
                    {
                        this.h += this.j;
                        continue;
                    }
                    if (this.CheckingMatrix(3))
                    {
                        this.j++;
                        step5 = true;
                        continue;
                    }
                }
                this.q1++;
                this.h = 2;
                this.j = 1;
            }
            this.np2 = this.A2.Count;
            if (this.np2 > 1)
            {
                this.A2  = this.SortedMatrix(this.A2);
                this.np2 = this.A2.Count - 1;
                this.G   = 0;
                if (this.np2 > 0)
                {
                    bool flagSolution = false;
                    for (int indexQ = 1; indexQ < this.A2.Count; indexQ++)
                    {
                        int f1g = 0;
                        List <List <int> > tempA = CopyMatrix(this.A);
                        tempA[this.i] = this.A2[indexQ];
                        SecondLevel secondLevel = new SecondLevel();
                        secondLevel.GenerateSolution(tempA);
                        List <List <int> > tempMatrixA = secondLevel.ReturnAMatrix();
                        f1g = this.GetCriterion(tempMatrixA);
                        Random rand = new Random();
                        int    ret  = rand.Next(5, 15);
                        //if (ret < 10)
                        if (f1g >= this.f1)
                        {
                            this.q2i     = indexQ;
                            this.G       = f1g - this.f1;
                            this.f1      = f1g;
                            flagSolution = true;
                        }
                    }
                    if (flagSolution == false)
                    {
                        for (int ii = 1; ii < this.A2.Count; ii++)
                        {
                            this.A2[ii].Insert(0, 0);
                        }
                        this.A1  = CopyMatrix(this.A2);
                        this.q1  = this.A1.Count - 1;
                        this.np2 = 0;
                        this.q2  = 0;
                        goto Nachalo;
                    }
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }