used in operatonal research LP(linear programming)
The Simplex Algorithm
Simplex method
Resolve using the Simplex Method the following problem:
Maximize | Z = f(x,y) = 3x + 2y |
subject to: | 2x + y ≤ 18 |
2x + 3y ≤ 42 | |
3x + y ≤ 24 | |
x ≥ 0 , y ≥ 0 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Simplex_Method
{
class simplex
{
int countslock;
int frow;
int fcol;
double[,] dt = { { 1, -8, -6, 0, 0, 0, 0 }, { 0, 2, 7, 1, 0, 100, 0 }, { 0, 1, 3, 0, 1, 50, 0 } };
double[] temprowdata1 ;
public simplex()
{
Console.WriteLine("the data is");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 7; j++)
{
Console.Write( dt[i,j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine();
temprowdata1 = new double[dt.GetLength(1)];
aditioniteration();
//operation(dt);
//double[] tempdt = {30,20,50,15,10,100};
//ratiorow(tempdt);
// Console.WriteLine(dt.GetLength(0));
}
public string check_cons(string a)
{
countslock++;
if (a.Contains("<="))
{
return a.Replace("<", "+s" + countslock);
}
else if (a.Contains(">="))
{
return a.Replace(">", "-s" + countslock);
}
return "error";
}
public string[] dataextract(string a)
{
string[] data;
int dtcounter =0;
data = new string[a.Length];
string sec = "";
for (int i = 0; i <a.Length; i++)
{
if (a[i] != 'x')
{
sec += a[i];
}
else { i++;
data[dtcounter] = sec; dtcounter++; sec = ""; }
}
data[dtcounter] = sec;
for (int i = 0; i <data.Length; i++)
{
if (data[i] != null)
{
if (data[i].Length == 1 && i > 0 )
{ data[i] += "1"; }
if (data[i].Contains("="))
{
string[] s = data[i].Split('=');
bool ass = false;
foreach (var item in s)
{
if (ass)
{ data[i + 1] = item; }
if (item.Contains('-'))
{ data[i] = "-1"; ass = true; }
else if(!item.Contains('-') && !ass) { data[i] = "1"; ass = true; }
}
}
}
}
//for (int i = 0; i < data.Length; i++)
//{
// Console.WriteLine(data[i]);
//}
return data;
}
public void problemsolve(double[,] a)
{
if (minind(a) > 0)
{
}
}
public double[] operation(double[,] a)
{
int row = 0;
int coloumn = 0;
double pivotelement = 0;
double m = 0;
double[] ratiodata = new double[a.GetLength(0)];
bool readytogo = false;
for (int i = 0; i < a.GetLength(0); i++)
{
//least data containg coloumn index
if (a[0, i] < m)
{
m = a[0, i];
coloumn = i;
}
}
if (readytogo)
{
for (int i = 1; i < a.GetLength(0); i++)
{
//least ratio calculating
ratiodata[i] = (a[i, a.GetLength(1) - 2] / a[i, Convert.ToInt32(coloumn)]);
}
//selecting row data for next operation to bring zero in data one
if (minratiocheck(ratiodata))
{
double[] temprowdata = new double[a.GetLength(1)];
row = ratiorow(ratiodata);
pivotelement = a[row, coloumn];
if (pivotelement > 0 && pivotelement == 1)
{
// if pivot = 1 take data as it
for (int i = 0; i < a.GetLength(1); i++)
{
temprowdata1[i] = (a[row, i]);
Console.WriteLine(temprowdata[i]);
}
}
else
{
//pivot element row equalizing to one and then take data
for (int i = 0; i < a.GetLength(1); i++)
{
temprowdata1[i] = (a[row, i] / a[row, coloumn]);
}
}
}
frow = row;
fcol = coloumn;
return temprowdata1;
}
}
public void aditioniteration()
{
double[] arr = operation(dt);
double[] arrtemp = new double[arr.Length];
double leastvalue = dt[0, fcol];
for (int i = 0; i <arr.Length; i++)
{
arrtemp[i] = arr[i] * (leastvalue *(-1));
Console.WriteLine(arrtemp[i]);
}
}
public bool minratiocheck(double[] ratio)
{
for (int i = 0; i < ratio.Length; i++)
{
if (ratio[i] > 0)
{
return true;
}
}
return false;
}
public int ratiorow(double[] ratio)
{
double m =500000000;
int row = 0;
for (int i = 0; i < ratio.Length; i++)
{
if (m > ratio[i] && ratio[i] != 0)
{
if (ratio[i] > 0)
{
m = ratio[i];
row = i;
}
}
}
return row;
}
public double minind(double[,] a)
{
double m = 0;
double ind = 0;
for (int i = 0; i < a.GetLength(0); i++)
{
if (a[0, i] < m)
{
m = a[0, i];
ind = i;
}
}
if (ind > 0)
{
return ind;
}
return 0.0;
}
public void ratiocheck(double[,] a)
{
double minvalue = minind(a);
double[] ratiodt = new double[a.GetLength(0)];
Console.WriteLine("minvalue " + minvalue);
for (int i = 1; i < a.GetLength(0); i++)
{
ratiodt[i] = a[i, Convert.ToInt32(minvalue)] / a[i, a.GetLength(1) - 2];
Console.WriteLine(ratiodt[i]);
Console.WriteLine(a[i, a.GetLength(1) - 1]+" world war");
}
}
}
class Program
{
static void Main(string[] args)
{
simplex sim = new simplex();
/*
int[] arr = { 0, 0,50, 50, 60 ,-90,0,0,10,10,20};
int m = 1000000;
for (int i = 1; i < arr.Length; i++)
{
if (m > arr[i] && arr[i] != 0 )
{
if (arr[i] > 0)
{
m = arr[i];
Console.WriteLine(m);
Console.WriteLine(i);
}
}
}
important min function
*/
/*
// sim.dataextract("");
string[] constraint = { "20x1-10x2+0x3>=50", "1x1+3x2+x3>=25", "-1x1+2x2+0x3<=26" };
//for (int i = 0; i < constraint.Length; i++)
//{
// string[] values = sim.dataextract(sim.check_cons(constraint[i]));
// foreach (var item in values)
// {
// if (i >= 1)
// {
// Console.Write(item + " ");
// }
// Console.WriteLine();
// }
//}
//string maxz = "Z=2x1+3x2+4x3";
//maxz = maxz.Replace("+", "-");
//maxz = maxz.Replace("=", "-");
//maxz += "=0";
//Console.WriteLine(maxz);
for (int i = 0; i < constraint.Length; i++)
{
// Console.WriteLine(sim.check_cons(constraint[i]));
string[] values = sim.dataextract(sim.check_cons(constraint[i]));
foreach (var item in values)
{
Console.Write(item + " ");
}
Console.WriteLine();
}
Console.WriteLine("refind data");
sim.dataextract("90x1-7x2+1x3-s2=25");
*/
}
}
}