Created
December 3, 2009 10:21
-
-
Save JakubOboza/248047 to your computer and use it in GitHub Desktop.
This file contains hidden or 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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Windows.Forms; | |
using System.Drawing; | |
namespace Simplex | |
{ | |
class Simplex | |
{ | |
private const double M = 10000000000000; | |
public bool Minimalization{get; set;} | |
private int _conditionCount; | |
private int _xBWeightCol; | |
private int _bValuesCol; | |
private int _firstConditionCol; | |
private int _firstConditionRow; | |
private int _ZjRow; | |
private int _TjRow; | |
private DataGridView _simplexDataGrid; | |
public Simplex(DataGridView dataGrid) | |
{ | |
_simplexDataGrid = dataGrid; | |
_simplexDataGrid.Rows.Add(); | |
_conditionCount = 0; | |
_xBWeightCol = 0; | |
_firstConditionCol = 2; | |
_bValuesCol = 7; | |
_firstConditionRow = 1; | |
} | |
private void SimplexTable(Object value, int row, int cell) | |
{ | |
string textValue = value.ToString(); | |
double numericValue; | |
if (textValue == "M") | |
{ | |
_simplexDataGrid.Rows[row].Cells[cell].Value = "M"; | |
} | |
else | |
{ | |
if (double.TryParse(textValue,out numericValue)) | |
{ | |
if (numericValue >= M) | |
{ | |
_simplexDataGrid.Rows[row].Cells[cell].Value = "M"; | |
} | |
else | |
{ | |
_simplexDataGrid.Rows[row].Cells[cell].Value = numericValue; | |
} | |
} | |
else | |
{ | |
_simplexDataGrid.Rows[row].Cells[cell].Value = textValue; | |
} | |
} | |
} | |
private double SimplexTable(int row, int cell) | |
{ | |
string value; | |
value = _simplexDataGrid.Rows[row].Cells[cell].Value.ToString(); | |
if (value == "M") | |
{ | |
return M; | |
} | |
else | |
{ | |
return double.Parse(value); | |
} | |
} | |
private void SetCellColor(int row, int cell, Color color) | |
{ | |
_simplexDataGrid.Rows[row].Cells[cell].Style.BackColor = color; | |
} | |
private void RebuildTable(int centralElementRow, int centralElementCol) | |
{ | |
double temp = 0; | |
double elementCentralny = SimplexTable(centralElementRow, centralElementCol); | |
for (int i = _firstConditionRow; i < _firstConditionRow + _conditionCount; i++) | |
{ | |
for (int j = _firstConditionCol; j < _bValuesCol + 1; j++) | |
{ | |
if (i == centralElementRow) | |
{ | |
SimplexTable(SimplexTable(i, j) / elementCentralny, i, j); | |
} | |
else | |
{ | |
for (int k = _firstConditionRow; k < _firstConditionRow + _conditionCount; k++) | |
{ | |
if (k != centralElementRow) | |
{ | |
if (j != centralElementCol) | |
{ | |
temp += SimplexTable(k, centralElementCol) * SimplexTable(centralElementRow, j); | |
SimplexTable((SimplexTable(i, j) - temp), i, j); | |
} | |
} | |
temp = 0; | |
} | |
} | |
} | |
for (int k = _firstConditionRow; k < _firstConditionRow + _conditionCount; k++) | |
{ | |
if (i != centralElementRow && k != centralElementCol) | |
{ | |
SimplexTable(0, i, centralElementCol); | |
} | |
temp = 0; | |
} | |
} | |
} | |
private void CalculateZjRow() | |
{ | |
_ZjRow = _conditionCount + 2; | |
double zj, cj, tij; | |
SimplexTable("Zj", _ZjRow, 0); | |
for (int i = _firstConditionCol; i < _bValuesCol; i++) | |
{ | |
zj = 0; | |
for (int j = _firstConditionRow; j < _firstConditionRow+_conditionCount; j++) | |
{ | |
cj = SimplexTable(j, 0); | |
tij = SimplexTable(j, i); | |
zj += cj * tij; | |
} | |
SimplexTable(zj, _ZjRow, i); | |
} | |
} | |
private void CalculateTjRow() | |
{ | |
double tj, ci, zj; | |
_TjRow = _conditionCount + 3; | |
SimplexTable("Tj", _TjRow, 0); | |
for (int i = _firstConditionCol; i < _bValuesCol; i++) | |
{ | |
tj = 0; | |
ci = SimplexTable(0, i); | |
zj = SimplexTable(_ZjRow, i); | |
tj += ci - zj; | |
SimplexTable(tj, _TjRow, i); | |
} | |
} | |
private void GoalFunctionValue() | |
{ | |
double t0 = 0, ti0, cj; | |
for (int i = _firstConditionRow; i < _firstConditionRow + _conditionCount; i++) | |
{ | |
cj = SimplexTable(i, _xBWeightCol); | |
ti0 = SimplexTable(i, _bValuesCol); | |
t0 += cj * ti0; | |
} | |
SimplexTable(t0, _TjRow, _bValuesCol); | |
} | |
private bool TestSolution() | |
{ | |
if (Minimalization) | |
{ | |
for (int i = _firstConditionCol; i < _bValuesCol; i++) | |
{ | |
if (SimplexTable(_TjRow, i) < 0) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
if (!Minimalization) | |
{ | |
for (int i = _firstConditionCol; i < _bValuesCol; i++) | |
{ | |
if (SimplexTable(_TjRow, i) > 0) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
else return false; | |
} | |
private void SetBaseVariables() | |
{ | |
for (int i = _firstConditionCol; i < _bValuesCol-1; i++) | |
{ | |
if (SimplexTable(1, i ) == 1) | |
{ | |
if (SimplexTable(2, i +1) == 1) | |
{ | |
if (SimplexTable(1, i + 1) == 0) | |
{ | |
if (SimplexTable(2, i ) == 0) | |
{ | |
SimplexTable(_simplexDataGrid.Columns[i].HeaderText.ToString(), 1, 1); | |
SimplexTable(_simplexDataGrid.Columns[i + 1].HeaderText.ToString(), 2, 1); | |
SimplexTable(SimplexTable(0,i),1,_xBWeightCol); | |
SimplexTable(SimplexTable(0, i + 1), 2, _xBWeightCol); | |
SimplexTable(0, 2, 0); | |
} | |
} | |
} | |
} | |
} | |
} | |
private int FindIndexOfExtremumInRow(int wiersz) | |
{ | |
int extremum = _firstConditionCol; | |
if (Minimalization) | |
{ | |
for (int i = _firstConditionCol; i < _bValuesCol; i++) | |
{ | |
if (SimplexTable(wiersz, i) < SimplexTable(wiersz, extremum)) | |
{ | |
extremum = i; | |
} | |
} | |
} | |
if (!Minimalization) | |
{ | |
for (int i = _firstConditionCol; i < _bValuesCol; i++) | |
{ | |
if (SimplexTable(wiersz, i) > SimplexTable(wiersz, extremum)) | |
{ | |
extremum = i; | |
} | |
} | |
} | |
return extremum; | |
} | |
private void OutOfBase() | |
{ | |
int extremumCol = FindIndexOfExtremumInRow(_TjRow); | |
int extremumRow = _firstConditionRow; | |
for (int i = _firstConditionRow; i < _firstConditionRow + _conditionCount; i++) | |
{ | |
if (SimplexTable(i, _bValuesCol) / SimplexTable(i, extremumCol) < SimplexTable(extremumRow, _bValuesCol) / SimplexTable(extremumRow, extremumCol)) | |
{ | |
extremumRow = i; | |
} | |
} | |
SetCellColor(extremumRow, extremumCol, Color.Green); | |
RebuildTable(extremumRow, extremumCol); | |
SimplexTable(_simplexDataGrid.Columns[extremumCol].HeaderText, extremumRow, _xBWeightCol + 1); | |
SimplexTable(SimplexTable(0,extremumCol),extremumRow,_xBWeightCol); | |
} | |
public void InsertGoalFunction(double x1,double x2,double x3) | |
{ | |
SimplexTable(x1, 0, 2); | |
SimplexTable(x2, 0, 3); | |
SimplexTable(x3, 0, 4); | |
} | |
public void InsertCondition(double x1,double x2,double x3,double b, int type) | |
{ | |
_conditionCount++; | |
_simplexDataGrid.Rows.Insert(_conditionCount, 1); | |
SimplexTable(x1, _conditionCount, 2); | |
SimplexTable(x2, _conditionCount, 3); | |
SimplexTable(x3, _conditionCount, 4); | |
SimplexTable(b, _conditionCount, 7); | |
if (type == 0) | |
{ | |
SimplexTable(1, _conditionCount, 4 + _conditionCount); | |
} | |
if (type == 1) | |
{ | |
SimplexTable(0, _conditionCount, 4 + _conditionCount); | |
} | |
if (type == 2) | |
{ | |
SimplexTable(-1, _conditionCount, 4 + _conditionCount); | |
} | |
//akutalizaja funkcji celu | |
if (Minimalization == true) | |
{ | |
SimplexTable("M", 0, 4 + _conditionCount); | |
} | |
else | |
{ | |
SimplexTable("0", 0, 4 + _conditionCount); | |
} | |
//ustawienie wyrazów wolnych | |
for (int i = _firstConditionCol; i < 7; i++) | |
{ | |
if (_simplexDataGrid.Rows[_conditionCount].Cells[i].Value == null) | |
{ | |
SimplexTable(0, _conditionCount, i ); | |
} | |
} | |
} | |
public void Start() | |
{ | |
_simplexDataGrid.Rows.Insert(_conditionCount + 1, 2); | |
_simplexDataGrid.Rows[_conditionCount + 1].DefaultCellStyle.BackColor = Color.Gray; | |
SetBaseVariables(); | |
do | |
{ | |
OutOfBase(); | |
CalculateZjRow(); | |
CalculateTjRow(); | |
GoalFunctionValue(); | |
if (TestSolution()) | |
{ | |
MessageBox.Show("end"); | |
} | |
else MessageBox.Show("next"); | |
} | |
while (!TestSolution()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment