-
-
Save swvitaliy/6aee55302165855c60c08dc53f67ec8b to your computer and use it in GitHub Desktop.
Generated from codex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace SimplexMethod | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Create random data | |
Random random = new Random(); | |
double[,] tableau = new double[,] { | |
{ 0, 3, 5, 4, 1, 0, 0 }, | |
{ 8, 6, 7, 2, 0, 1, 30 }, | |
{ 4, 5, 9, 3, 0, 0, 25 } | |
}; | |
// Call the Simplex method | |
SimplexMethod(tableau); | |
} | |
static void SimplexMethod(double[,] tableau) | |
{ | |
// Initialize the necessary variables | |
int rowCount = tableau.GetLength(0); | |
int colCount = tableau.GetLength(1); | |
int pivotCol = 0; | |
int pivotRow = 0; | |
// Find the pivot column | |
for (int col = 0; col < colCount - 1; col++) | |
{ | |
if (tableau[0, col] < 0) | |
{ | |
pivotCol = col; | |
break; | |
} | |
} | |
// Find the pivot row | |
double minRatio = double.MaxValue; | |
for (int row = 1; row < rowCount; row++) | |
{ | |
if (tableau[row, pivotCol] > 0) | |
{ | |
double ratio = tableau[row, colCount - 1] / tableau[row, pivotCol]; | |
if (ratio < minRatio) | |
{ | |
minRatio = ratio; | |
pivotRow = row; | |
} | |
} | |
} | |
// Calculate the new tableau | |
double pivotValue = tableau[pivotRow, pivotCol]; | |
for (int col = 0; col < colCount; col++) | |
{ | |
tableau[pivotRow, col] /= pivotValue; | |
} | |
for (int row = 0; row < rowCount; row++) | |
{ | |
if (row != pivotRow) | |
{ | |
double multiplier = tableau[row, pivotCol]; | |
for (int col = 0; col < colCount; col++) | |
{ | |
tableau[row, col] -= multiplier * tableau[pivotRow, col]; | |
} | |
} | |
} | |
// Print the new tableau | |
Console.WriteLine("New Tableau: "); | |
for (int row = 0; row < rowCount; row++) | |
{ | |
for (int col = 0; col < colCount; col++) | |
{ | |
Console.Write(tableau[row, col] + " "); | |
} | |
Console.WriteLine(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment