Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

19 Commits

Repository files navigation

ExactSimplex

An implementation of the Simplex algorithm in C# that gives exact results (in form of fractions). Based on: https://github.com/timdolenko/simplex

Available as a NuGet package: https://www.nuget.org/packages/ExactSimplex

Why?

Countless implementations of the Simplex algorithms exist online, in multiple languages, but it is hard to find an implementation in C# that gives exact, precise results.

Most of the available implementations operate on the 'double' data type, which has limited precision and makes the results look weird (and just straight-up wrong).

This code is meant to help those who seek an exact result for a Linear Programming problem. An exact result is in the form of (numerator/denominator).

Example

An example of how to use ExactSimplex is given in the ExactSimplex.Example console project.

Consider the following LP problem.

Maximize:

$$P = 20x1 + 10x2 + 15x3$$

Subject to:

$$3x1 + 2x2 + 5x3 \le 55$$$$2x1 + x2 + x3 \le 26$$$$x1 + x2 + 3x3 \le 30$$$$5x1 + 2x2 + 4x3 \le 57$$$$x1, x2, x3 \ge 0$$

Create an array of Constraints with the desired amount of variables (just their coefficients).

varconstraints=newConstraint[]{newConstraint(newFraction[]{3,2,5},newFraction(55),"<="),newConstraint(newFraction[]{2,1,1},newFraction(26),"<="),newConstraint(newFraction[]{1,1,3},newFraction(30),"<="),newConstraint(newFraction[]{5,2,4},newFraction(57),"<="),newConstraint(newFraction[]{1,0,0},newFraction(0),">="),newConstraint(newFraction[]{0,1,0},newFraction(0),">="),newConstraint(newFraction[]{0,0,1},newFraction(0),">=")};

Create a function to be minimized or maximized.

varfunction=newFunction(newFraction[]{20,10,15},newFraction(0),true);

Create an instance of Simplex and call GetResult on it.

varsimplex=newSimplex(function,constraints);varresult=simplex.GetResult();

Interpret the result.

switch(result.ResultType){caseSimplexResult.Unbounded:Console.WriteLine("Unbounded.");break;caseSimplexResult.NotYetFound:Console.WriteLine("Solution wasn't found after 100 steps.");break;caseSimplexResult.Found:Console.WriteLine("Solution was found.");Console.WriteLine("Function value: "+result.Results.Last().FValue);// Function value: 268break;}

You can use the GetVariableValues method to retrieve the final values of the variables.

varvariableValues=simplex.GetVariableValues();for(inti=0;i<variableValues.Length;i++){Console.WriteLine($"x{i+1} = {variableValues[i]}");}// Variable values:// x1 = 9/5// x2 = 104/5// x3 = 8/5

You can also retrieve the simplex tableau.

vartableau=result.Results.Last().Matrix;

Dependencies and references

The project is based on the following publicly available projects and packages:

About

An implementation of the Simplex algorithm in C# that gives exact results (in form of fractions). Based on: https://github.com/timdolenko/simplex

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages