コード例 #1
0
ファイル: MainForm.cs プロジェクト: sunny-hell/bmstu
        private void btnStart_Click(object sender, EventArgs e)
        {
            int n  = tblCosts.Rows.Count;
            VengerMatrix m = new VengerMatrix(n);
            double[][] c = readMatrix();
            m.setMatrix(c);
            VengerAlgorithm va = new VengerAlgorithm(m);
            String s = "";
            Task t = new Task(n, c);
            BranchAndBoundAlgorithm bba = new BranchAndBoundAlgorithm(t);
            double totalCost;
            int[][] path = bba.start(out totalCost);
            txtResult.Text = pathToString(path);
            lblTotalCost.Text = String.Format("Общая стоимость проезда по маршруту: {0} руб.", totalCost);
            /* if (dbg)
                 va.startDebug(max, out opt, out f, out s);
             else
                 va.start(max, out opt, out f);*/

            //        s += printOptimalMatrix(opt, 5);
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: sunny-hell/bmstu
        private void button1_Click(object sender, EventArgs e)
        {
            VengerMatrix m;
            int n;
            double[][] c;
            try
            {
                if (rand)
                {
                    n = Int32.Parse(txtSize.Text);
                    m = new VengerMatrix(n);
                    c = createRandomMatrix(n);
                    m.setMatrix(c);
                    rtbSource.Text = m.print();
                }
                else
                {
                    n = Int32.Parse(txtSize.Text);
                    m = new VengerMatrix(n);
                    c = readMatrix(n);

                    //double[][] c = {
                    //         new double [5]{10.0, 12.0, 7.0, 11.0, 10.0},
                    //         new double [5]{12.0, 5.0, 12.0, 7.0, 12.0},
                    //         new double [5]{8.0, 6.0, 7.0, 8.0, 13.0},
                    //         new double [5]{8.0, 11.0, 5.0, 9.0, 9.0},
                    //         new double [5]{10.0, 8.0, 9.0, 11.0, 11.0}};
                    //int[][] c = {
                    //         new int [5]{0, 0, 0, 0, 0},
                    //         new int [5]{0, 0, 0, 0, 0},
                    //         new int [5]{0, 0, 0, 0, 0},
                    //         new int [5]{0, 0, 0, 0, 0},
                    //         new int [5]{0, 0, 0, 0, 0}};
                    m.setMatrix(c);
                }

                VengerAlgorithm va = new VengerAlgorithm(m);
                String s = "";
                int[][] opt;
                double f;
                Task t = new Task(n, c);
                BranchAndBoundAlgorithm bba = new BranchAndBoundAlgorithm(t);
                bba.start(dbg, out s);

               /* if (dbg)
                    va.startDebug(max, out opt, out f, out s);
                else
                    va.start(max, out opt, out f);*/

            //        s += printOptimalMatrix(opt, 5);

                rtbResult.Text = s; // m.print();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            //for (int i = 0; i < m.N; i++)
            //    rtbResult.Text += (m.getMinInRow(i) + "\t");
            //rtbResult.Text += "\n";

            //VengerMatrix m2 = m.Copy();
            //rtbResult.Text += "\n" + m2.print();
        }
コード例 #3
0
        public void start(bool dbg, out String s)
        {
            s = "";
            Task tsk;
            int[][] x;
            double f;
            int i = 1;

            if (dbg)
            {
                s += "Начальные оценки: \n";
                s += "x*: \n";
                s += printOptimalMatrix(_x, _n);
                s += String.Format("f* = {0}\n", _f);
                s += "\n";
            }
            while (_taskList.Count > 0)
            {
                if (dbg)
                {
                    s += String.Format("Число задач в списке S: {0}.\n", _taskList.Count);
                    s += String.Format("Итерация № {0}\n", i);
                    i++;
                }
                tsk = _taskList.Dequeue();
                if (dbg)
                    s += "Решается задача: \n" + tsk.ToString();

                VengerAlgorithm va = new VengerAlgorithm(tsk.toVengerMatrix());
                va.start(false, out x, out f);
                if (dbg)
                {
                    s += "x: \n";
                    s += printOptimalMatrix(x, _n);
                    s += String.Format("f = {0} \n", f);
                }

                // полученное значение целевой функции меньше текущего?
                if (f < _f)
                {
                    List<Loop> loops = getSubLoops(x);
                    if (loops.Count == 1)
                    {
                        _f = f;
                        _x = x;
                        if (dbg)
                        {
                            s += "Полученное решение является полным подциклом; \n";
                            s += String.Format("x* = x, f* = {0}\n", _f);
                            s += "\n";
                        }
                    }
                    else
                    {
                        if (dbg)
                        {
                            s += "Подциклы: ";
                            foreach (Loop l in loops)
                                s += l.ToString() + ";";
                            s += "\n\n";
                        }
                        Loop min = getMinLoops(loops);
                        addNewTasks(min, tsk);
                    }
                }
            }
            if (dbg)
                s += "Список задач S пуст.\n";
            s += "============================================\n";
            s += "Итоговое решение задачи: \n";
            s += "x*:\n";
            s += printOptimalMatrix(_x, _n);
            s += String.Format("f* = {0} \n", _f);
        }
コード例 #4
0
        public int[][] start(out double cost)
        {
            Task tsk;
            int[][] x;
            double f;
            int i = 1;

            while (_taskList.Count > 0)
            {
                tsk = _taskList.Dequeue();

                VengerAlgorithm va = new VengerAlgorithm(tsk.toVengerMatrix());
                va.start(false, out x, out f);

                // полученное значение целевой функции меньше текущего?
                if (f < _f)
                {
                    List<Loop> loops = getSubLoops(x);
                    if (loops.Count == 1)
                    {
                        _f = f;
                        _x = x;
                    }
                    else
                    {
                        Loop min = getMinLoops(loops);
                        addNewTasks(min, tsk);
                    }
                }
            }
            cost = _f;
            return _x;
        }