Giter Club home page Giter Club logo

simplex-method's Introduction

simplex-method

simplex-method is a Haskell library that implements the two-phase simplex method in exact rational arithmetic.

Quick Overview

The Linear.Simplex.Solver.TwoPhase module contain both phases of the two-phase simplex method.

Phase One

Phase one is implemented by findFeasibleSolution:

findFeasibleSolution :: (MonadIO m, MonadLogger m) => [PolyConstraint] -> m (Maybe FeasibleSystem)

findFeasibleSolution takes a list of PolyConstraints. The PolyConstraint type, as well as other custom types required by this library, are defined in the Linear.Simplex.Types module. PolyConstraint is defined as:

data PolyConstraint
  = LEQ {lhs :: VarLitMapSum, rhs :: SimplexNum}
  | GEQ {lhs :: VarLitMapSum, rhs :: SimplexNum}
  | EQ {lhs :: VarLitMapSum, rhs :: SimplexNum}
  deriving (Show, Read, Eq, Generic)

SimplexNum is an alias for Rational, and VarLitMapSum is an alias for VarLitMap, which is an alias for Map Var SimplexNum. Var is an alias of Int.

A VarLitMapSum is read as Integer variables mapped to their Rational coefficients, with an implicit + between each entry. For example: Map.fromList [(1, 2), (2, (-3)), (1, 3)] is equivalent to (2x1 + (-3x2) + 3x1).

And a PolyConstraint is an inequality/equality where the LHS is a VarLitMapSum and the RHS is a Rational. For example: LEQ (Map.fromList [(1, 2), (2, (-3)), (1, 3)] 60) is equivalent to (2x1 + (-3x2) + 3x1) <= 60.

Passing a [PolyConstraint] to findFeasibleSolution will return a FeasibleSystem if a feasible solution exists:

data FeasibleSystem = FeasibleSystem
  { dict :: Dict
  , slackVars :: [Var]
  , artificialVars :: [Var]
  , objectiveVar :: Var
  }
  deriving (Show, Read, Eq, Generic)
type Dict = M.Map Var DictValue

data DictValue = DictValue
  { varMapSum :: VarLitMapSum
  , constant :: SimplexNum
  }
  deriving (Show, Read, Eq, Generic)

Dict can be thought of as a set of equations, where the key represents a basic variable on the LHS of the equation that is equal to the RHS represented as a DictValue value.

Phase Two

optimizeFeasibleSystem performs phase two of the simplex method, and has the type:

optimizeFeasibleSystem :: (MonadIO m, MonadLogger m) => ObjectiveFunction -> FeasibleSystem -> m (Maybe Result)

data ObjectiveFunction = Max {objective :: VarLitMapSum} | Min {objective :: VarLitMapSum}

data Result = Result
  { objectiveVar :: Var
  , varValMap :: VarLitMap
  }
  deriving (Show, Read, Eq, Generic)

We give optimizeFeasibleSystem an ObjectiveFunction along with a FeasibleSystem.

Two-Phase Simplex

twoPhaseSimplex performs both phases of the simplex method. It has the type:

twoPhaseSimplex :: (MonadIO m, MonadLogger m) => ObjectiveFunction -> [PolyConstraint] -> m (Maybe Result)

Extracting Results

The result of the objective function is present in the returned Result type of both twoPhaseSimplex and optimizeFeasibleSystem, but this can be difficult to grok in systems with many variables, so the following function will extract the value of the objective function for you.

dictionaryFormToTableau :: Dict -> Tableau

There are similar functions for DictionaryForm as well as other custom types in the module Linear.Simplex.Util.

Example

exampleFunction :: (ObjectiveFunction, [PolyConstraint])
exampleFunction =
  (
    Max {objective = Map.fromList [(1, 3), (2, 5)]},      -- 3x1 + 5x2
    [
      LEQ {lhs = Map.fromList [(1, 3), (2, 1)], rhs = 15}, -- 3x1 + x2 <= 15 
      LEQ {lhs = Map.fromList [(1, 1), (2, 1)], rhs = 7},  -- x1 + x2 <= 7
      LEQ {lhs = Map.fromList [(2, 1)], rhs = 4},          -- x2 <= 4
      LEQ {lhs = Map.fromList [(1, -1), (2, 2)], rhs = 6}  -- -x1 + 2x2 <= 6
    ]
  )

twoPhaseSimplex (fst exampleFunction) (snd exampleFunction)

The result of the call above is:

Just 
  (Result
    { objectiveVar = 7 -- Integer representing objective function
    , varValMap = Map.fromList  
      [ (7, 29) -- Value for variable 7, so max(3x1 + 5x2) = 29.
      , (1, 3) -- Value for variable 1, so x1 = 3 
      , (2, 4) -- Value for variable 2, so x2 = 4
      ]
    }
  )

There are many more examples in test/TestFunctions.hs. You may use prettyShowVarConstMap, prettyShowPolyConstraint, and prettyShowObjectiveFunction to convert these tests into a more human-readable format.

Issues

Please share any bugs you find here.

simplex-method's People

Contributors

rasheedja avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

simplex-method's Issues

Adding positivity constraints change the result

Hello,

I have these constraints:

[LEQ {lhs = fromList [(1,1 % 1),(2,1 % 1),(3,0 % 1)], rhs = 1 % 1},LEQ {lhs = fromList [(1,1 % 1),(2,0 % 1),(3,1 % 1)], rhs = 1 % 1}]

and the objective is to maximize the first variable.

I get this result:

Just (Result {objectiveVar = 6, varValMap = fromList [(1,1 % 1),(6,1 % 1)]})

Variables 2 and 3 do not appear in this resut.

Now I add the constraints "variable 2 positive" and "variable 3 positive":

[LEQ {lhs = fromList [(1,1 % 1),(2,1 % 1),(3,0 % 1)], rhs = 1 % 1},LEQ {lhs = fromList [(1,1 % 1),(2,(-1) % 1),(3,0 % 1)], rhs = 0 % 1},LEQ {lhs = fromList [(1,1 % 1),(2,0 % 1),(3,(-1) % 1)], rhs = 0 % 1},LEQ {lhs = fromList [(1,1 % 1),(2,0 % 1),(3,1 % 1)], rhs = 1 % 1}]

Then I get the expected results, with variables 2 and 3 present this time:

Just (Result {objectiveVar = 8, varValMap = fromList [(1,1 % 2),(2,1 % 2),(3,1 % 2),(8,1 % 2)]})

Is it normal? That sounds strange to me, because the positivity constraints should be implicit, no?

Unexpected results for simple (degenerate) cases

Thanks for this, it's quite useful and seems to work in most cases, but there are some weird results I don't quite understand. Here's some things that work as expected:

  • minimize x st x>=0, twoPhaseSimplex (Min [(1,1)]) [GEQ [(1,1)] 0): correct 0
  • maximize x st x>=0, twoPhaseSimplex (Max [(1,1)]) [GEQ [(1,1)] 0): correct Nothing
  • maximize -x st x>=0, twoPhaseSimplex (Max [(1,-1)]) [GEQ [(1,1)] 0): correct 0
  • minimize -x st x>=0, twoPhaseSimplex (Min [(1,-1)]) [GEQ [(1,1)] 0): correct Nothing

But if I flip the constraint, either by using LEQ or negating it, the unbounded cases seem to fail:

  • maximize x st x<=0, twoPhaseSimplex (Max [(1,1)]) [LEQ [(1,1)] 0): correct 0
  • minimize x st x<=0, twoPhaseSimplex (Min [(1,1)]) [LEQ [(1,1)] 0): incorrect 0
  • maximize -x st x<=0, twoPhaseSimplex (Max [(1,-1)]) [LEQ [(1,1)] 0): incorrect 0
  • minimize x st -x>=0, twoPhaseSimplex (Min [(1,1)]) [GEQ [(1,-1)] 0): incorrect 0

Am I doing something wrong or misinterpreting something? I can reproduce this with various multiple varibles as well, but it only seems to happen when I'm essentially saying to minimize variables that are bounded above (rather than below).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.