Example #1
0
        public static BigTableau F22c(double[][] A, double[] b, double[] c,
            int[] ik, double[] beta, Action<string, object> notify)
        {
            int m = A.Length;
            int n = A[0].Length;
            int k = ik.Length;
            int m2 = m + k;
            int n2 = n + m + k;

            double[] b2 = new double[m2];
            Array.Copy(b, b2, m);
            double[] c2 = new double[n2];
            Array.Copy(c, c2, n);

            double[][] A2 = new double[m2][];
            for (int i = 0; i < m2; i++) {
                A2[i] = new double[n2];
            }

            for (int i = 0; i < m; i++) {
                Array.Copy(A[i], A2[i], n);
                A2[i][n + i] = 1;
            }

            for (int i = 0; i < k; i++) {
                A2[m + i][ik[i]] = 1;
                A2[m + i][n + m + i] = -1;
                b2[m + i] = beta[i];
            }

            EqSimplexProblem p = new EqSimplexProblem(true, A2, b2, c2);
            if (notify != null) {
                notify("Without artificial variables:", p);
            }
            return SolveArtificial(p, notify);
        }
Example #2
0
        public static BigTableau SolveArtificial(EqSimplexProblem p,
            Action<string, object> notify)
        {
            ArtEqSimplexProblem p2 = CreateWithArtificialVariables(p);
            BigTableau bt = CreateBigTableau(p2, notify);
            SolveBig(bt);

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

            if (Math.Abs(bt.t[bt.m][bt.n]) > 0) {
                throw new InfeasibleProblemException();
            }

            BigTableau bt2 = EliminateArtificials(bt, p2, p);

            if (notify != null) {
                notify("After eliminating:", bt2);
            }

            SolveBig(bt2);

            return bt2;
        }
Example #3
0
        public static BigTableau EliminateArtificials(BigTableau bt,
            ArtEqSimplexProblem p, EqSimplexProblem pOrig)
        {
            int nVars = bt.n - bt.m;
            int n2 = bt.n - p.art.Length;
            double[][] t = new double[bt.m + 1][];
            for (int i = 0; i <= bt.m; i++) {
                t[i] = new double[n2 + 1];
            }

            int k = 0;
            for (int j = 0; j < bt.n; j++) {
                if (j >= nVars && p.baseVar[j - nVars] == -1) {
                    continue;
                }
                for (int i = 0; i < bt.m; i++) {
                    t[i][k] = bt.t[i][j];
                }

                k++;
            }

            // Copy RHS.
            for (int i = 0; i < bt.m; i++) {
                t[i][n2] = bt.t[i][bt.n];
            }

            // Restoring the indices.
            int[] ind = (int[])bt.ind.Clone();
            for (int i = 0; i < ind.Length; i++) {
                if (ind[i] >= nVars) {
                    ind[i] = p.baseVar[ind[i] - nVars];
                }
            }

            bool[] nonBase = NonBase2(ind, n2);

            // Solving the new objective function.
            for (int j = 0; j <= n2; j++) {
                if (!nonBase[j]) {
                    continue;
                }

                for (int i = 0; i < bt.m; i++) {
                    Console.WriteLine(j + " " + i + " ::: " + ind[i] + " " + t[i][j] + " " + pOrig.c[ind[i]]);
                    t[bt.m][j] += t[i][j] * pOrig.c[ind[i]];
                }
            }

            return new BigTableau(bt.m, n2, t, ind);
        }
Example #4
0
        public static ArtEqSimplexProblem CreateWithArtificialVariables(
            EqSimplexProblem p)
        {
            int m = p.A.Length;
            int[] baseVar = new int[m]; // -1 == needs an artificial one.
            int nArtificial = 0;

            // Find the base variable for each constraint.
            for (int i = 0; i < m; i++) {
                baseVar[i] = CalcBaseVar(p.A, p.c, i);
                if (baseVar[i] == -1) {
                    nArtificial++;
                }
            }

            bool[] nonBase = CalcNonBase(baseVar, m, p.c.Length);

            int n = p.c.Length + nArtificial;
            int nVar = n - m;

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

            int k = 0;
            for (int j = 0; j < p.c.Length; j++) {
                if (nonBase[j]) {
                    for (int i = 0; i < m; i++) {
                        A[i][k] = p.A[i][j];
                    }
                    k++;
                }
            }

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

                if (baseVar[i] == -1) {
                    c[nVar + i] = -1;
                }
            }

            foreach (var item in c) {
                Console.Write(item.ToString() + " ");
            }
            Console.WriteLine();

            int[] art = new int[nArtificial];
            k = 0;
            for (int i = 0; i < m; i++) {
                if (baseVar[i] == -1) {
                    art[k] = nVar + i;
                    k++;
                }
            }

            return new ArtEqSimplexProblem(p.max, A, p.b, c, art, baseVar);
        }
Example #5
0
        // TODO: All these printing things need to be simplified. And these
        // functions especially need to be removed because of all the
        // duplication.
        private string Print(EqSimplexProblem 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 = "=";
            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>=</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 #6
0
 public ArtEqSimplexProblem simplexArtEq(EqSimplexProblem p)
 {
     return Simplex.CreateWithArtificialVariables(p);
 }
Example #7
0
 public double[] simplex3(EqSimplexProblem p)
 {
     BigTableau bt = Simplex.SolveArtificial(p, notify);
     return bt.MakeSolutionAll();
 }