public Dictionary(LPP lpp) { z0 = 0; this.a = new double[lpp.ObjFunc.VariablesNumber]; for (int i = 0; i < a.Length; i++) { a[i] = lpp.ObjFunc.Coefficients[i]; } this.basic = new int[lpp.ObjFunc.VariablesNumber]; for (int i = 0; i < lpp.ObjFunc.VariablesNumber; i++) { this.basic[i] = i + 1; } this.slack = new int[lpp.Constraints.Length]; for (int i = 0; i < lpp.Constraints.Length; i++) { this.slack[i] = lpp.ObjFunc.VariablesNumber + i + 1; } this.c = new double[lpp.Constraints.Length, lpp.ObjFunc.VariablesNumber + 1]; for (int i = 0; i < lpp.Constraints.Length; i++) { this.c[i, 0] = lpp.Constraints[i].Restriction; for (int j = 1; j < lpp.ObjFunc.VariablesNumber + 1; j++) { this.c[i, j] = -lpp.Constraints[i].Coefficients[j - 1]; } } }
static void Main(string[] args) { //var funk = new ObjectiveFunction(new double[]{1, 0, 1, 3}); //var constr = new [] { // new Constraint(new double[] { 2, 5, 0, 0 }, 15), // new Constraint(new double[] { 0, 2, 0, 0 }, 8), // new Constraint(new double[] {1, 1, 0, 0 }, 10), // new Constraint(new double[] {-2, -2, 1, 1 }, -3), // new Constraint(new double[] {0, -2, 1, 3}, 11) //}; var funk = new ObjectiveFunction(new double[] { 2, -1, 2, 0, 0 }); var constr = new[] { new Constraint(new double[] { 2, 1, 0, 1, 0 }, 10), new Constraint(new double[] { 1, 2, -2, 0, 0 }, 20), new Constraint(new double[] { 0, 1, 2, 0, 1 }, 5) }; LPP task = new LPP(funk, constr); task.Solve(); }
private Dictionary initialize() { Console.WriteLine("Initialization phase..."); Console.WriteLine("-------------------------------"); Console.WriteLine(); double[] auxC = new double[ObjFunc.VariablesNumber + 1]; auxC[0] = -1; for (int i = 0; i < auxC.Length - 1; i++) { auxC[i + 1] = 0; } ObjectiveFunction auxOF = new ObjectiveFunction(auxC); Constraint[] auxCS = new Constraint[this.Constraints.Length]; int leavesBasis = 0; double minB = Constraints[0].Restriction; for (int i = 0; i < auxCS.Length; i++) { double[] auxCC = new double[ObjFunc.VariablesNumber + 1]; auxCC[0] = -1; for (int j = 0; j < auxCC.Length - 1; j++) { auxCC[j + 1] = Constraints[i].Coefficients[j]; } auxCS[i] = new Constraint(auxCC, Constraints[i].Restriction); if (Constraints[i].Restriction < minB) { minB = Constraints[i].Restriction; leavesBasis = i; } } LPP auxLPP = new LPP(auxOF, auxCS); Dictionary auxD = new Dictionary(auxLPP); auxD.Print(false); auxD.Recalculate(0, leavesBasis); while (!SolutionFound(auxD)) { auxD.Print(preferToLeave: 1); auxD.Improve(preferToLeave: 1); } auxD.Print(preferToLeave: 1); int len = auxD.basic.Length; int[] bb = new int[len]; double[,] cc = new double[auxLPP.Constraints.Length, len + 1]; int[] ss = new int[auxLPP.Constraints.Length]; for (int i = 0; i < len; i++) { bb[i] = auxD.basic[i]; for (int j = 0; j < auxLPP.Constraints.Length; j++) { cc[j, i + 1] = auxD.c[j, i + 1]; } } for (int j = 0; j < auxLPP.Constraints.Length; j++) { cc[j, 0] = auxD.c[j, 0]; ss[j] = auxD.slack[j]; } auxD.a = new double[len - 1]; auxD.basic = new int[len - 1]; auxD.slack = new int[auxLPP.Constraints.Length]; auxD.c = new double[auxLPP.Constraints.Length, len]; for (int i = 0; i < auxLPP.Constraints.Length; i++) { auxD.c[i, 0] = cc[i, 0]; int j = 1; while (bb[j - 1] != 1) { auxD.c[i, j] = cc[i, j]; j++; } while (j < bb.Length) { j++; auxD.c[i, j - 1] = cc[i, j]; } auxD.slack[i] = ss[i] - 1; } int k = 0; while (bb[k] != 1) { auxD.basic[k] = bb[k] - 1; k++; } k++; while (k < bb.Length) { auxD.basic[k - 1] = bb[k] - 1; k++; } auxD.z0 = 0; for (int i = 0; i < this.ObjFunc.Coefficients.Length; i++) { for (int j = 0; j < auxD.slack.Length; j++) { if (auxD.slack[j] == i + 1) { auxD.z0 += ObjFunc.Coefficients[i] * auxD.c[j, 0]; for (int m = 0; m < ObjFunc.Coefficients.Length; m++) { auxD.a[m] += ObjFunc.Coefficients[i] * auxD.c[j, m + 1]; } } } } auxD.Print(false); Console.WriteLine(); return(auxD); }