diff --git a/Program.cs b/Program.cs index 80a681e..7b24a41 100644 --- a/Program.cs +++ b/Program.cs @@ -5,10 +5,12 @@ class lpr381 { //simplex simplex = new simplex(); //simplex.simplexAlgo(); - twoPhaseSimplex twoPhaseSimplex = new twoPhaseSimplex(); - twoPhaseSimplex.twoPhaseSimplexAlgo(); + //twoPhaseSimplex twoPhaseSimplex = new twoPhaseSimplex(); + //twoPhaseSimplex.twoPhaseSimplexAlgo(); //chesssolver chesssolver = new chesssolver(); //chesssolver.chessAlgo(); + dualSimplex dualSimplex = new dualSimplex(); + dualSimplex.dualSimplexAlgo(); } } diff --git a/dualSimplex.cs b/dualSimplex.cs new file mode 100644 index 0000000..6423669 --- /dev/null +++ b/dualSimplex.cs @@ -0,0 +1,223 @@ +using ConsoleTables; + +class dualSimplex +{ + public void dualSimplexAlgo() + { + 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); + } + int varCount = newTable[0].Count; + table = prepareTable(newTable, constraintsList); + // print every row in table + printTable(table, varCount); + + } + + + List> prepareTable(List> table, List constraints) + { + List> newTable = new List>(); + foreach (List row in table) + { + List newRow = new List(); + foreach (float value in row) + { + newRow.Add(value); + } + newTable.Add(newRow); + } + List signs = new List(); + foreach (Constraints constraint in constraints) + { + signs.Add(constraint.sign); + } + for (int i = 0; i < signs.Count ; i++) + { + table[0].Add(0); + table[0].Add(0); + + // if sign[i] == "=" copy table[i] to table[i+1] without overwriting table[i+1] + if (signs[i] == "=") + { + List tempRow = new List(); + foreach (float value in table[i+1]) + { + tempRow.Add(value); + } + table.Add(tempRow); + // move last row of table to table[i+2] + for (int j = table.Count - 1; j > i+2; j--) + { + List row = table[j]; + table[j] = table[j-1]; + table[j-1] = row; + } + signs.Add("2="); + for (int j = signs.Count - 1; j > i + 1; j--) + { + string sign = signs[j]; + signs[j] = signs[j-1]; + signs[j-1] = sign; + } + newTable.Insert(i+1, newTable[i+1]); + } + + if (signs[i] == ">=") + { + // multiply table[i+1] by -1 + for (int j = 0; j < table[i+1].Count; j++) + { + table[i+1][j] = table[i+1][j] * -1; + } + } + + // add 3 0s to each row for every column in table + for (int j = 0; j < signs.Count ; j++) + { + if (signs[i] == ">=") + { + // make s = 1 + if (i == j) + { + table[i+1].Add(0); + table[i+1].Add(1); + } + else + { + table[i+1].Add(0); + table[i+1].Add(0); + } + } + else if (signs[i] == "<=") + { + //make e = 1 + if (i == j) + { + table[i+1].Add(-1); + table[i+1].Add(0); + } + else + { + table[i+1].Add(0); + table[i+1].Add(0); + } + } + else if (signs[i] == "=") + { + // make a = 1 + if (i == j) + { + table[i+1].Add(-1); + table[i+1].Add(0); + } + else + { + table[i+1].Add(0); + table[i+1].Add(0); + } + } + else if (signs[i] == "2=") + { + // make a = 1 + if (i == j) + { + table[i+1].Add(0); + table[i+1].Add(1); + } + else + { + table[i+1].Add(0); + table[i+1].Add(0); + } + } + } + } + table[0].Add(0); + + // remove intex newTable.Count-1 from table and place it in the back of table + for (int i = 1; i < table.Count; i++) + { + table[i].RemoveAt(newTable[i].Count-1); + if (signs[i-1] == ">=" || signs[i-1] == "2=") + { + table[i].Add(newTable[i][newTable[i].Count-1] * -1); + } + else + { + table[i].Add(newTable[i][newTable[i].Count-1]); + } + + } + // remove the value of newTable.Count-1 from table[1] and place it in the back of table[1] + return table; + } + + void printTable(List> table, int varCount) + { + int count = 1; + string[] headers = new string[table[0].Count]; + for (int i = 0; i < table[0].Count; i++) + { + if (i < varCount) + { + headers[i] = "x" + (i + 1); + } + else if (i < table[0].Count - 1) + { + headers[i] = "e" + (count); + headers[i+1] = "s" + (count); + i = i + 1; + count++; + } + else + { + headers[i] = "rhs"; + } + } + var conTable = new ConsoleTable(headers); + foreach (List row in table) + { + // 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); + } +} \ No newline at end of file diff --git a/input.txt b/input.txt index 8840b15..26dcc6e 100644 --- a/input.txt +++ b/input.txt @@ -1,5 +1,5 @@ max +1 +1 -+8 +4 <= 160 ++8 +4 = 160 +4 +4 <= 100 +1 +0 <= 17 +1 +0 >= 5 diff --git a/obj/Debug/net6.0/lpr381.csproj.CoreCompileInputs.cache b/obj/Debug/net6.0/lpr381.csproj.CoreCompileInputs.cache index 393397b..dfdc1a2 100644 --- a/obj/Debug/net6.0/lpr381.csproj.CoreCompileInputs.cache +++ b/obj/Debug/net6.0/lpr381.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -42cc27e0497d325b5a06d599f420fe32fe1e67ba +8c13023fa8aa1aee00b6cf6a6819800d58bb1521 diff --git a/obj/Debug/net6.0/lpr381.dll b/obj/Debug/net6.0/lpr381.dll index c929f15..804d884 100644 Binary files a/obj/Debug/net6.0/lpr381.dll and b/obj/Debug/net6.0/lpr381.dll differ diff --git a/obj/Debug/net6.0/lpr381.pdb b/obj/Debug/net6.0/lpr381.pdb index c28b416..b6147dc 100644 Binary files a/obj/Debug/net6.0/lpr381.pdb and b/obj/Debug/net6.0/lpr381.pdb differ diff --git a/obj/Debug/net6.0/ref/lpr381.dll b/obj/Debug/net6.0/ref/lpr381.dll index 32e9914..5fbb2f8 100644 Binary files a/obj/Debug/net6.0/ref/lpr381.dll and b/obj/Debug/net6.0/ref/lpr381.dll differ diff --git a/twoPhaseSimplex.cs b/twoPhaseSimplex.cs index 411935b..8f2bd01 100644 --- a/twoPhaseSimplex.cs +++ b/twoPhaseSimplex.cs @@ -286,7 +286,7 @@ class twoPhaseSimplex return table; } - static int findPivotCol(List> table) + static int findPivotCol(List> table) { float largest = 0; int largestIndex = 0; @@ -428,8 +428,5 @@ class twoPhaseSimplex } } return table; - } - - } \ No newline at end of file