Essays.club - Получите бесплатные рефераты, курсовые работы и научные статьи
Поиск

Решение симплексного метода C#

Автор:   •  Сентябрь 21, 2019  •  Задача  •  2,107 Слов (9 Страниц)  •  979 Просмотры

Страница 1 из 9

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace Simplex

{

    public class Simplex

    {

        //source - симплекс таблица без базисных переменных

        double[,] table; //симплекс таблица

        int m, n;

        List basis; //список базисных переменных

        public Simplex(double[,] source)

        {

            m = source.GetLength(0);

            n = source.GetLength(1);

            table = new double[m, n + m - 1];

            basis = new List();

            for (int i = 0; i < m; i++)

            {

                for (int j = 0; j < table.GetLength(1); j++)

                {

                    if (j < n)

                        table[i, j] = source[i, j];

                    else

                        table[i, j] = 0;

                }

                //выставляем коэффициент 1 перед базисной переменной в строке

                if ((n + i) < table.GetLength(1))

                {

                    table[i, n + i] = 1;

                    basis.Add(n + i);

                }

            }

            n = table.GetLength(1);

        }

        //result - в этот массив будут записаны полученные значения X

        public double[,] Calculate(double[] result)

        {

            int mainCol, mainRow; //ведущие столбец и строка

            while (!IsItEnd())

            {

                mainCol = findMainCol();

                mainRow = findMainRow(mainCol);

                basis[mainRow] = mainCol;

                double[,] new_table = new double[m, n];

                for (int j = 0; j < n; j++)

                    new_table[mainRow, j] = table[mainRow, j] / table[mainRow, mainCol];

                for (int i = 0; i < m; i++)

                {

                    if (i == mainRow)

                        continue;

                    for (int j = 0; j < n; j++)

                        new_table[i, j] = table[i, j] - table[i, mainCol] * new_table[mainRow, j];

                }

                table = new_table;

            }

            //заносим в result найденные значения X

            for (int i = 0; i < result.Length; i++)

            {

                int k = basis.IndexOf(i + 1);

                if (k != -1)

                    result[i] = table[k, 0];

                else

                    result[i] = 0;

            }

            return table;

        }

        private bool IsItEnd()

        {

            bool flag = true;

            for (int j = 1; j < n; j++)

            {

                if (table[m - 1, j] < 0)

                {

                    flag = false;

                    break;

                }

            }

            return flag;

...

Скачать:   txt (4.9 Kb)   pdf (111.9 Kb)   docx (8.2 Kb)  
Продолжить читать еще 8 страниц(ы) »
Доступно только на Essays.club