Example #1
0
        public static string SixPapFormat(SimplexProblem p)
        {
            int m = p.A.Length;
            int n = p.A[0].Length;
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("objective: MAX\nm: {0}\ns: {1}\n", m, n);
            sb.Append("C(j):");
            for (var j = 0; j < n; j++) {
                sb.Append(" " + p.c[j]);
            }
            sb.Append("\nconstraints:\n");

            for (var i = 0; i < m; i++) {
                sb.AppendFormat("({0}):", i + 1);
                for (var j = 0; j < n; j++) {
                    sb.Append(" " + p.A[i][j]);
                }
                sb.AppendFormat(" < {0}\n", p.b[i]);
            }

            return sb.ToString();
        }
Example #2
0
        public static CompactTableau CreateInfeasibleTableau(SimplexProblem p)
        {
            int m = p.A.Length;
            int nOrig = p.A[0].Length;
            int n = nOrig + 1;

            double[][] t = new double[m + 1][];

            for (int i = 0; i < m; i++) {
                t[i] = new double[n + 1];

                Array.Copy(p.A[i], t[i], nOrig);
                t[i][nOrig] = -1;
                t[i][n] = p.b[i];
            }
            t[m] = new double[n + 1];
            t[m][nOrig] = -1;

            int[] ind = new int[n + m];

            for (int i = 0; i < nOrig; i++) {
                ind[i] = i;
            }

            ind[nOrig] = ind.Length - 1;

            for (int i = 0; i < m; i++) {
                ind[n + i] = nOrig + i;
            }

            return new CompactTableau(m, n, t, ind, true);
        }
Example #3
0
        public static CompactTableau CreateTableau(SimplexProblem p,
            Action<string, object> notify)
        {
            CompactTableau ct;

            if (notify != null) {
                notify("Problem:", p);
            }

            if (p.IsInitiallyFeasible()) {
                ct = CreateFeasibleTableau(p);
                if (notify != null) {
                    notify("Created feasible tableau:", ct);
                }

                return ct;
            }

            ct = CreateInfeasibleTableau(p);
            if (notify != null) {
                notify("Created infeasible tableau:", ct);
            }

            PivotInfeasibleTableau(ct);
            if (notify != null) {
                notify("Pivoted infeasible tableau:", ct);
            }

            SolveFeasible(ct, notify);

            TransformToFeasible(ct, p.c);
            if (notify != null) {
                notify("Removed column:", ct);
            }

            return ct;
        }
Example #4
0
        public static CompactTableau CreateDualTableau(SimplexProblem p,
            Action<string, object> notify)
        {
            if (notify != null) {
                notify("Problem:", p);
            }

            int m = p.A[0].Length;
            int n = p.A.Length;

            double[][] t = new double[m + 1][];

            for (int i = 0; i < m; i++) {
                t[i] = new double[n + 1];
                for (int j = 0; j < n; j++) {
                    t[i][j] = p.A[j][i];
                }
                t[i][n] = p.c[i];
            }
            t[m] = new double[n + 1];
            Array.Copy(p.b, t[m], n);

            int[] ind = new int[n + m];
            for (int j = 0; j < n; j++) {
                ind[j] = m + j;
            }
            for (int i = 0; i < m; i++) {
                ind[n + i] = i;
            }

            CompactTableau ct = new CompactTableau(m, n, t, ind, false);
            ct.dual = true;
            if (notify != null) {
                notify("Created dual tableau:", ct);
            }

            return ct;
        }
Example #5
0
        public static CompactTableau CreateFeasibleTableau(SimplexProblem p)
        {
            int m = p.A.Length;
            int n = p.A[0].Length;

            double[][] t = new double[m + 1][];

            for (int i = 0; i < m; i++) {
                t[i] = new double[n + 1];

                Array.Copy(p.A[i], t[i], n);
                t[i][n] = p.b[i];
            }
            t[m] = new double[n + 1];
            Array.Copy(p.c, t[m], n);

            int[] ind = new int[n + m];

            for (int i = 0; i < ind.Length; i++) {
                ind[i] = i;
            }

            return new CompactTableau(m, n, t, ind, false);
        }
Example #6
0
        public static BigTableau CreateBigRestartedTableau(BigTableau bt,
            SimplexProblem p)
        {
            int m = bt.m;
            int n = bt.n;
            int nVar = n - m;
            double[][] t1 = bt.t;
            double[][] t2 = new double[m + 1][];

            // Allocating new matrix and setting the middle S* and y*T.
            for (int i = 0; i <= m; i++) {
                t2[i] = new double[n + 1];
                Array.Copy(t1[i], nVar, t2[i], nVar, m);
            }

            // Setting S*b.
            double sum;
            for (int i = 0; i < m; i++) {
                sum = 0.0;
                for (int j = 0; j < m; j++) {
                    sum += t1[i][nVar + j] * p.b[j];
                }
                t2[i][n] = sum;
            }

            // Setting y*Tb.
            sum = 0.0;
            for (int j = 0; j < m; j++) {
                sum += t1[m][nVar + j] * p.b[j];
            }
            t2[m][n] = sum;

            // Setting S*A.
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < nVar; j++) {
                    sum = 0.0;
                    for (int k = 0; k < nVar; k++) {
                        sum += t1[i][nVar + k] * p.A[k][j];
                    }
                    t2[i][j] = sum;
                }
            }

            // Setting y*TA-cT.
            for (int j = 0; j < nVar; j++) {
                sum = 0.0;
                for (int i = 0; i < m; i++) {
                    sum += t1[m][nVar + i] * p.A[i][j];
                }
                t2[m][j] = sum - p.c[j];
            }

            // Copying the labels.
            int[] ind = new int[m];
            Array.Copy(bt.ind, ind, m);

            return new BigTableau(m, n, t2, ind);
        }
Example #7
0
        public static BigTableau CreateBigTableau(SimplexProblem p,
            Action<string, object> notify)
        {
            if (notify != null) {
                notify("Problem:", p);
            }

            int m = p.A.Length;
            int nVars = p.A[0].Length;
            int n = m + nVars;

            double[][] t = new double[m + 1][];

            for (int i = 0; i < m; i++) {
                t[i] = new double[n + 1];

                Array.Copy(p.A[i], t[i], nVars);
                t[i][nVars + i] = 1;
                t[i][n] = p.b[i];
            }
            t[m] = new double[n + 1];
            for (int i = 0; i < nVars; i++) {
                t[m][i] = -p.c[i];
            }

            int[] ind = new int[m];
            for (int i = 0; i < ind.Length; i++) {
                ind[i] = nVars + i;
            }

            BigTableau bt = new BigTableau(m, n, t, ind);

            if (notify != null) {
                notify("Created tableau:", bt);
            }

            return bt;
        }
Example #8
0
 public double[] simplex2(SimplexProblem p)
 {
     return simplex2(simplex2Tableau(p));
 }
Example #9
0
        private string Print(SimplexProblem p)
        {
            int m = p.A.Length;
            int n = p.A[0].Length;

            StringBuilder sb = new StringBuilder();

            sb.Append("<table class='simplex-problem'>");

            sb.AppendFormat("<tr><td class='first'>{0} ",
                    p.max ? "max" : "min");
            string comp = p.max ? "≤" : "≥";
            string name = p.max ? "x" : "y";
            bool first = true;
            for (int j = 0; j < n; j++) {
                AddX(sb, name, p.c[j], j, ref first);
            }
            sb.Append("</td></tr>");

            for (int i = 0; i < m; i++) {
                sb.Append("<tr><td class='first'>");
                first = true;
                for (int j = 0; j < n; j++) {
                    AddX(sb, name, p.A[i][j], j, ref first);
                }
                sb.AppendFormat("</td><td>{0}</td><td>{1:0.###}</td></tr>",
                        comp, p.b[i]);
            }

            sb.Append("<tr><td class='first'>");
            for (int j = 0; j < n; j++) {
                if (j > 0) {
                    sb.Append(",");
                }
                sb.AppendFormat("{0}<sub>{1}</sub>", name, j + 1);
            }
            sb.Append("</td><td>≥</td><td>0</td></tr>");

            sb.Append("</table>");

            return sb.ToString();
        }
Example #10
0
 public CompactTableau simplexTableau(SimplexProblem p)
 {
     return Simplex.CreateTableau(p, notify);
 }
Example #11
0
 public double[] simplexDual(SimplexProblem p)
 {
     return simplexDual(simplexDualTableau(p));
 }
Example #12
0
 public BigTableau simplex2TableauSolved(SimplexProblem p)
 {
     BigTableau bt = simplex2Tableau(p);
     Simplex.SolveBig(bt, notify);
     return bt;
 }
Example #13
0
 public BigTableau simplex2Tableau(SimplexProblem p)
 {
     return Simplex.CreateBigTableau(p, notify);
 }
Example #14
0
        public double[] simplex2Restart(BigTableau old, SimplexProblem p)
        {
            BigTableau bt2 = Simplex.CreateBigRestartedTableau(old, p);

            if (notify != null) {
                notify("Before pivoting:", bt2);
            }

            Simplex.FixBigRestartedTableau(bt2, notify);
            return bt2.MakeSolution();
        }