From f4a57b548d4e0d0a5ebe8b34a57438ffc7d18bed Mon Sep 17 00:00:00 2001 From: Zastian Pretorius Date: Wed, 27 Jul 2022 17:38:18 +0100 Subject: [PATCH] some refactoring --- Program.cs | 319 +---------------------------------------------------- file.cs | 81 ++++++++++++++ simplex.cs | 249 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 335 insertions(+), 314 deletions(-) create mode 100644 file.cs create mode 100644 simplex.cs diff --git a/Program.cs b/Program.cs index 97baaa1..cc7f847 100644 --- a/Program.cs +++ b/Program.cs @@ -1,321 +1,12 @@ -using ConsoleTables; + class lpr381 { static void Main() { - List zConstraintsList = new List(); - List constraintsList = new List(); - //read file "input.txt" line by line - string line; - StreamReader file = new StreamReader("input.txt"); - while ((line = file.ReadLine()) != null) - { - // if line contains "min" or "max" - if (line.Contains("min") || line.Contains("max")) - { - zConstraints constraints = new zConstraints(); - if (line.Contains("min")) - { - constraints.minMax = "min"; - } - else - { - constraints.minMax = "max"; - } - // remove "min" or "max" from line - line = line.Remove(0, line.IndexOf(constraints.minMax) + constraints.minMax.Length); - // split line by " " - string[] lineArray = line.Split(' '); - //remove empty elements from array - lineArray = lineArray.Where(x => !string.IsNullOrEmpty(x)).ToArray(); - - // add variables to constraints.variables after parsing them to float - List variables = new List(); - foreach (string variable in lineArray) - { - variables.Add(float.Parse(variable)); - - } - constraints.values = variables; - // add constraints to list - zConstraintsList.Add(constraints); - } - else - { - // split line by " " - string[] lineArray = line.Split(' '); - //remove empty elements from array - lineArray = lineArray.Where(x => !string.IsNullOrEmpty(x)).ToArray(); - // for each element in array - Constraints constraints = new Constraints(); - List variables = new List(); - foreach (string variable in lineArray) - { - if (variable.Contains("=") || variable.Contains(">") || variable.Contains("<")) - { - constraints.sign = variable; - } - else - { - variables.Add(float.Parse(variable)); - } - } - constraints.values = variables; - constraintsList.Add(constraints); - - } - } - file.Close(); - // print constraintsList - foreach (zConstraints constraints in zConstraintsList) - { - Console.WriteLine(constraints.minMax + " " + string.Join(" ", constraints.values)); - } - foreach (Constraints constraints in constraintsList) - { - Console.WriteLine(constraints.sign + " " + string.Join(" ", constraints.values)); - } - List> table = new List>(); - table.Add(zConstraintsList[0].values); - // add all constraints in constraintsList to table - foreach (Constraints constraints in constraintsList) - { - table.Add(constraints.values); - } - List> newTable = new List>(); - // add all rows in table to newTable - foreach (List row in table) - { - // add all columns in row to newTable without reference to table - List newRow = new List(); - for (int i = 0; i < row.Count; i++) - { - newRow.Add(row[i]); - - } - newTable.Add(newRow); - } - - // add s variable to table - for (int i = 0; i < table.Count; i++) - { - if (i == 0) - { - for (int j = 0; j < table[i].Count + 1; j++) - { - newTable[i].Add(0); - } - } - else - { - for (int j = 0; j < table[i].Count-1; j++) - { - //save the last value in newTable in to lastVal and remove it from newTanle - float lastVal = newTable[i][newTable[i].Count - 1]; - newTable[i].RemoveAt(newTable[i].Count - 1); - if (i == j+1) - { - newTable[i].Add(1); - } - else - { - newTable[i].Add(0); - } - newTable[i].Add(lastVal); - } - } - } - // print table - printTable(newTable); - bool optimal = false; - newTable = flipZ(newTable); - while (!optimal) - { - int pivotCol = findPivotCol(newTable); - List ratio = ratioTest(newTable, pivotCol); - int pivotRow = findPivotRow(ratio); - printTable(newTable); - newTable = pivotTable(newTable, pivotRow, pivotCol); - Console.WriteLine(""); - if (checkOptimal(newTable)) - { - printTable(newTable); - optimal = true; - } - } - + //simplex simplex = new simplex(); + //simplex.simplexAlgo(); + twoPhaseSimplex twoPhaseSimplex = new twoPhaseSimplex(); + twoPhaseSimplex.twoPhaseSimplexAlgo(); } - - - static void printTable(List> newTable) - { - string[] headers = new string[newTable[0].Count]; - double half = newTable[0].Count / 2; - int halfInt = (int)Math.Floor(half); - for (int i = 0; i < newTable[0].Count; i++) - { - if (i != newTable[0].Count - 1 && i < halfInt) - { - headers[i] = "x" + (i + 1); - } - else if (i != newTable[0].Count - 1 && i >= halfInt) - { - // add "s" to headers starting at 1 - headers[i] = "s" + ((i - newTable[0].Count / 2)+1); - //headers[i] = "s" + (i + 1); - } - else - { - headers[i] = "rhs"; - } - } - var conTable = new ConsoleTable(headers); - // for each row in table add row to conTable - foreach (List row in newTable) - { - // convert row to object array - object[] rowArray = new object[row.Count]; - for (int i = 0; i < row.Count; i++) - { - rowArray[i] = row[i]; - } - conTable.AddRow(rowArray); - } - conTable.Write(Format.Alternative); - } - - - - static bool checkOptimal(List> table) - { - int zIndex = 0; - for (int i = 0; i < table[0].Count; i++) - { - if (table[0][i] < 0) - { - zIndex = i; - } - } - if (zIndex == 0) - { - return true; - } - else - { - return false; - } - } - - - static List> flipZ(List> table) - { - for (int i = 0; i < table[0].Count; i++) - { - if (table[0][i] > 0) - { - table[0][i] = -table[0][i]; - } - } - return table; - } - - static int findPivotCol(List> table) - { - float largest = 0; - int largestIndex = 0; - for (int i = 0; i < table[0].Count; i++) - { - if (table[0][i] < largest) - { - largest = table[0][i]; - largestIndex = i; - } - } - return largestIndex; - } - - static List ratioTest(List> table, int testCol) - { - List ratios = new List(); - for (int i = 1; i < table.Count; i++) - { - if (table[i][testCol] != 0) - { - ratios.Add(table[i][table[i].Count - 1] / table[i][testCol]); - } - else - { - ratios.Add(float.MaxValue); - } - } - return ratios; - } - - - static int findPivotRow(List ratios) - { - float smallest = float.MaxValue; - int smallestIndex = 0; - for (int i = 0; i < ratios.Count; i++) - { - if (ratios[i] < smallest && ratios[i] > 0.0) - { - smallest = ratios[i]; - smallestIndex = i; - } - } - return smallestIndex; - } - - - static List> pivotTable(List> table, int pivotRow, int pivotCol) - { - //clone table in to newTable - List> newTable = new List>(); - foreach (List row in table) - { - List newRow = new List(); - for (int i = 0; i < row.Count; i++) - { - newRow.Add(row[i]); - } - newTable.Add(newRow); - } - var pivotPoint = newTable[pivotRow + 1][pivotCol]; - // divide pivot row by pivot point - for (int i = 0; i < newTable[pivotRow + 1].Count; i++) - { - newTable[pivotRow + 1][i] = newTable[pivotRow + 1][i] / pivotPoint; - } - // current possition-(pivot_row*new_table pivot_point) - for (int i = 0; i < newTable.Count; i++) - { - if (i != pivotRow + 1) - { - var currentPossition = newTable[i][pivotCol]; - for (int j = 0; j < newTable[i].Count; j++) - { - newTable[i][j] = newTable[i][j] - (currentPossition * newTable[pivotRow + 1][j]); - } - } - } - return newTable; - - } - - - } -class zConstraints -{ - public string minMax; - public List values; -} - -class Constraints -{ - public List values; - - public string sign; -} \ No newline at end of file diff --git a/file.cs b/file.cs new file mode 100644 index 0000000..fc52588 --- /dev/null +++ b/file.cs @@ -0,0 +1,81 @@ +class file +{ + public Tuple,List> readFile() + { + List zConstraintsList = new List(); + List constraintsList = new List(); + //read file "input.txt" line by line + string line; + StreamReader file = new StreamReader("input.txt"); + while ((line = file.ReadLine()) != null) + { + // if line contains "min" or "max" + if (line.Contains("min") || line.Contains("max")) + { + zConstraints constraints = new zConstraints(); + if (line.Contains("min")) + { + constraints.minMax = "min"; + } + else + { + constraints.minMax = "max"; + } + // remove "min" or "max" from line + line = line.Remove(0, line.IndexOf(constraints.minMax) + constraints.minMax.Length); + // split line by " " + string[] lineArray = line.Split(' '); + //remove empty elements from array + lineArray = lineArray.Where(x => !string.IsNullOrEmpty(x)).ToArray(); + + // add variables to constraints.variables after parsing them to float + List variables = new List(); + foreach (string variable in lineArray) + { + variables.Add(float.Parse(variable)); + + } + constraints.values = variables; + // add constraints to list + zConstraintsList.Add(constraints); + } + else + { + // split line by " " + string[] lineArray = line.Split(' '); + //remove empty elements from array + lineArray = lineArray.Where(x => !string.IsNullOrEmpty(x)).ToArray(); + // for each element in array + Constraints constraints = new Constraints(); + List variables = new List(); + foreach (string variable in lineArray) + { + if (variable.Contains("=") || variable.Contains(">") || variable.Contains("<")) + { + constraints.sign = variable; + } + else + { + variables.Add(float.Parse(variable)); + } + } + constraints.values = variables; + constraintsList.Add(constraints); + } + } + file.Close(); + return new Tuple, List>(zConstraintsList, constraintsList); + } +} +class zConstraints +{ + public string minMax; + public List values; +} + +class Constraints +{ + public List values; + + public string sign; +} \ No newline at end of file diff --git a/simplex.cs b/simplex.cs new file mode 100644 index 0000000..c102b21 --- /dev/null +++ b/simplex.cs @@ -0,0 +1,249 @@ +using ConsoleTables; +class simplex +{ + public void simplexAlgo() + { + file file = new file(); + Tuple, List> content = file.readFile(); + List zConstraintsList = content.Item1; + List constraintsList = content.Item2; + + // print constraintsList + foreach (zConstraints constraints in zConstraintsList) + { + Console.WriteLine(constraints.minMax + " " + string.Join(" ", constraints.values)); + } + foreach (Constraints constraints in constraintsList) + { + Console.WriteLine(constraints.sign + " " + string.Join(" ", constraints.values)); + } + List> table = new List>(); + table.Add(zConstraintsList[0].values); + // add all constraints in constraintsList to table + foreach (Constraints constraints in constraintsList) + { + table.Add(constraints.values); + } + List> newTable = new List>(); + // add all rows in table to newTable + foreach (List row in table) + { + // add all columns in row to newTable without reference to table + List newRow = new List(); + for (int i = 0; i < row.Count; i++) + { + newRow.Add(row[i]); + + } + newTable.Add(newRow); + } + + // add s variable to table + for (int i = 0; i < table.Count; i++) + { + if (i == 0) + { + for (int j = 0; j < table[i].Count + 1; j++) + { + newTable[i].Add(0); + } + } + else + { + for (int j = 0; j < table[i].Count-1; j++) + { + //save the last value in newTable in to lastVal and remove it from newTanle + float lastVal = newTable[i][newTable[i].Count - 1]; + newTable[i].RemoveAt(newTable[i].Count - 1); + if (i == j+1) + { + newTable[i].Add(1); + } + else + { + newTable[i].Add(0); + } + newTable[i].Add(lastVal); + } + } + } + // print table + printTable(newTable); + bool optimal = false; + newTable = flipZ(newTable); + while (!optimal) + { + int pivotCol = findPivotCol(newTable); + List ratio = ratioTest(newTable, pivotCol); + int pivotRow = findPivotRow(ratio); + printTable(newTable); + newTable = pivotTable(newTable, pivotRow, pivotCol); + Console.WriteLine(""); + if (checkOptimal(newTable)) + { + printTable(newTable); + optimal = true; + } + } + + } + + + static void printTable(List> newTable) + { + string[] headers = new string[newTable[0].Count]; + double half = newTable[0].Count / 2; + int halfInt = (int)Math.Floor(half); + for (int i = 0; i < newTable[0].Count; i++) + { + if (i != newTable[0].Count - 1 && i < halfInt) + { + headers[i] = "x" + (i + 1); + } + else if (i != newTable[0].Count - 1 && i >= halfInt) + { + // add "s" to headers starting at 1 + headers[i] = "s" + ((i - newTable[0].Count / 2)+1); + //headers[i] = "s" + (i + 1); + } + else + { + headers[i] = "rhs"; + } + } + var conTable = new ConsoleTable(headers); + // for each row in table add row to conTable + foreach (List row in newTable) + { + // convert row to object array + object[] rowArray = new object[row.Count]; + for (int i = 0; i < row.Count; i++) + { + rowArray[i] = row[i]; + } + conTable.AddRow(rowArray); + } + conTable.Write(Format.Alternative); + } + + + + static bool checkOptimal(List> table) + { + int zIndex = 0; + for (int i = 0; i < table[0].Count; i++) + { + if (table[0][i] < 0) + { + zIndex = i; + } + } + if (zIndex == 0) + { + return true; + } + else + { + return false; + } + } + + + static List> flipZ(List> table) + { + for (int i = 0; i < table[0].Count; i++) + { + if (table[0][i] > 0) + { + table[0][i] = -table[0][i]; + } + } + return table; + } + + static int findPivotCol(List> table) + { + float largest = 0; + int largestIndex = 0; + for (int i = 0; i < table[0].Count; i++) + { + if (table[0][i] < largest) + { + largest = table[0][i]; + largestIndex = i; + } + } + return largestIndex; + } + + static List ratioTest(List> table, int testCol) + { + List ratios = new List(); + for (int i = 1; i < table.Count; i++) + { + if (table[i][testCol] != 0) + { + ratios.Add(table[i][table[i].Count - 1] / table[i][testCol]); + } + else + { + ratios.Add(float.MaxValue); + } + } + return ratios; + } + + + static int findPivotRow(List ratios) + { + float smallest = float.MaxValue; + int smallestIndex = 0; + for (int i = 0; i < ratios.Count; i++) + { + if (ratios[i] < smallest && ratios[i] > 0.0) + { + smallest = ratios[i]; + smallestIndex = i; + } + } + return smallestIndex; + } + + + static List> pivotTable(List> table, int pivotRow, int pivotCol) + { + //clone table in to newTable + List> newTable = new List>(); + foreach (List row in table) + { + List newRow = new List(); + for (int i = 0; i < row.Count; i++) + { + newRow.Add(row[i]); + } + newTable.Add(newRow); + } + var pivotPoint = newTable[pivotRow + 1][pivotCol]; + // divide pivot row by pivot point + for (int i = 0; i < newTable[pivotRow + 1].Count; i++) + { + newTable[pivotRow + 1][i] = newTable[pivotRow + 1][i] / pivotPoint; + } + // current possition-(pivot_row*new_table pivot_point) + for (int i = 0; i < newTable.Count; i++) + { + if (i != pivotRow + 1) + { + var currentPossition = newTable[i][pivotCol]; + for (int j = 0; j < newTable[i].Count; j++) + { + newTable[i][j] = newTable[i][j] - (currentPossition * newTable[pivotRow + 1][j]); + } + } + } + return newTable; + + } + +} +