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
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).
An example of how to use ExactSimplex is given in the ExactSimplex.Example console project.
Consider the following LP problem.
Maximize:
Subject to:
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),">=")};varfunction=newFunction(newFraction[]{20,10,15},newFraction(0),true);varsimplex=newSimplex(function,constraints);varresult=simplex.GetResult();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/5You can also retrieve the simplex tableau.
vartableau=result.Results.Last().Matrix;The project is based on the following publicly available projects and packages: