-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | A library for formulating and solving math programs. -- -- Please see the README on GitHub. @package math-programming @version 0.5.1 -- | Linear expressions of variables. module Math.Programming.LinExpr -- | A linear expression. -- -- Linear expressions contain symbolic variables of type b and -- numeric coefficients of type a. Often a will be -- Double, and b will be whatever variable type your -- linear program uses. data LinExpr a b LinExpr :: ![(a, b)] -> !a -> LinExpr a b -- | Construct a term in a linear expression by multiplying a constant by a -- variable. (*.) :: Num a => a -> b -> LinExpr a b infixl 7 *. -- | Construct a term in a linear expression by multiplying a variable by a -- constant. (.*) :: Num a => b -> a -> LinExpr a b infixl 7 .* -- | Construct a term in a linear expression by dividing a variable by a -- constant. (./) :: Fractional a => b -> a -> LinExpr a b infixl 7 ./ -- | Multiplication of linear expressions by a constant. scale :: Num a => a -> LinExpr a b -> LinExpr a b -- | Addition of linear expressions. (.+.) :: Num a => LinExpr a b -> LinExpr a b -> LinExpr a b infixl 6 .+. -- | The difference of linear expressions. (.-.) :: Num a => LinExpr a b -> LinExpr a b -> LinExpr a b infixl 6 .-. -- | A linear expression with a single variable term. var :: Num a => b -> LinExpr a b -- | A linear expression with only a constant term. con :: a -> LinExpr a b -- | The sum of variable terms with coefficients of unity. vsum :: Num a => [b] -> LinExpr a b -- | The sum of linear expressions. esum :: Num a => Foldable t => t (LinExpr a b) -> LinExpr a b -- | Reduce an expression to its value. eval :: Num a => LinExpr a a -> a -- | Simplify an expression by grouping like terms. simplify :: (Num a, Ord b) => LinExpr a b -> LinExpr a b instance Data.Traversable.Traversable (Math.Programming.LinExpr.LinExpr a) instance Data.Foldable.Foldable (Math.Programming.LinExpr.LinExpr a) instance GHC.Base.Functor (Math.Programming.LinExpr.LinExpr a) instance (GHC.Show.Show a, GHC.Show.Show b) => GHC.Show.Show (Math.Programming.LinExpr.LinExpr a b) instance (GHC.Read.Read a, GHC.Read.Read b) => GHC.Read.Read (Math.Programming.LinExpr.LinExpr a b) instance (GHC.Classes.Eq a, GHC.Classes.Eq b) => GHC.Classes.Eq (Math.Programming.LinExpr.LinExpr a b) instance GHC.Num.Num a => GHC.Base.Semigroup (Math.Programming.LinExpr.LinExpr a b) instance GHC.Num.Num a => GHC.Base.Monoid (Math.Programming.LinExpr.LinExpr a b) -- | Data and class definitions for the core math programming interface. module Math.Programming.Types -- | A linear program. -- -- This is a monadic context for formulating and solving linear programs. -- The types v, c, and o refer to the types of -- variables, constraints, and objectives, respectively, used by a -- particular solver backend. class Monad m => MonadLP v c o m | m -> v c o -- | Add a new (free) variable to the model. -- -- See free, bounded, nonNeg, and nonPos as -- higher-level alternatives. addVariable :: MonadLP v c o m => m v -- | Remove a variable from the model. deleteVariable :: MonadLP v c o m => v -> m () -- | Get the name of a variable. getVariableName :: MonadLP v c o m => v -> m Text -- | Set a name for a variable. setVariableName :: MonadLP v c o m => v -> Text -> m () -- | Retrieve the current bounds associated with a variable. getVariableBounds :: MonadLP v c o m => v -> m Bounds -- | Apply bounds to a variable. -- -- See within as a higher-level alternative. setVariableBounds :: MonadLP v c o m => v -> Bounds -> m () -- | Get the value of a variable in the current solution. -- -- This value could be arbitrary if no solve has been completed, or a -- solve produced an infeasible or unbounded solution. getVariableValue :: MonadLP v c o m => v -> m Double -- | Add a constraint representing the given inequality to the model. -- -- See the .==., .==#, ==., .>=., -- .>=, >=., .<=., .<=, and -- <=. functions as higher-level alternatives. addConstraint :: MonadLP v c o m => Inequality (Expr v) -> m c -- | Remove a constraint from the model. deleteConstraint :: MonadLP v c o m => c -> m () -- | Get the name of a constraint. getConstraintName :: MonadLP v c o m => c -> m Text -- | Set a name for a constraint. setConstraintName :: MonadLP v c o m => c -> Text -> m () -- | Get the dual value associated with a constraint. getConstraintValue :: MonadLP v c o m => c -> m Double -- | Add an objective to the problem. -- -- Depending on the solver backend, this might replace an existing -- objective. addObjective :: MonadLP v c o m => Expr v -> m o -- | Remove an objective from the model. deleteObjective :: MonadLP v c o m => o -> m () -- | Get the name of a objective. getObjectiveName :: MonadLP v c o m => o -> m Text -- | Set a name for a objective. setObjectiveName :: MonadLP v c o m => o -> Text -> m () -- | Get the sense of an objective. getObjectiveSense :: MonadLP v c o m => o -> m Sense -- | Set the sense of an objective. setObjectiveSense :: MonadLP v c o m => o -> Sense -> m () -- | Get the value of an objective. getObjectiveValue :: MonadLP v c o m => o -> m Double -- | Get the timeout associated with a problem. getTimeout :: MonadLP v c o m => m Double -- | Set the timeout associated with a problem. setTimeout :: MonadLP v c o m => Double -> m () -- | Compute an LP-optimal solution. optimizeLP :: MonadLP v c o m => m SolutionStatus -- | Function composition involving a 2-argument function. compose2 :: (c -> d) -> (a -> b -> c) -> a -> b -> d -- | Monadic lifting involving a 2-argument function. lift2 :: (MonadTrans t, Monad m) => (a -> b -> m c) -> a -> b -> t m c -- | A (mixed) integer program. -- -- In addition to the methods of the MonadLP class, this monad -- supports constraining variables to be either continuous or discrete. class MonadLP v c o m => MonadIP v c o m | m -> v c o getVariableDomain :: MonadIP v c o m => v -> m Domain setVariableDomain :: MonadIP v c o m => v -> Domain -> m () getRelativeMIPGap :: MonadIP v c o m => m Double setRelativeMIPGap :: MonadIP v c o m => Double -> m () optimizeIP :: MonadIP v c o m => m SolutionStatus -- | Whether a math program is minimizing or maximizing its objective. data Sense Minimization :: Sense Maximization :: Sense -- | The outcome of an optimization. data SolutionStatus -- | An optimal solution has been found. Optimal :: SolutionStatus -- | A feasible solution has been found. The result may or may not be -- optimal. Feasible :: SolutionStatus -- | The model has been proven to be infeasible. Infeasible :: SolutionStatus -- | The model has been proven to be unbounded. Unbounded :: SolutionStatus -- | An error was encountered during the solve. Instance-specific methods -- should be used to determine what occurred. Error :: SolutionStatus -- | An interval of the real numbers. data Bounds -- | The non-negative reals. NonNegativeReals :: Bounds -- | The non-positive reals. NonPositiveReals :: Bounds -- | Any closed interval of the reals. Interval :: Double -> Double -> Bounds -- | Any real number. Free :: Bounds -- | The type of values that a variable can take on. -- -- Note that the Integer constructor does not interfere with the -- Integer type, as the Integer type does not define a -- constuctor of the same name. The ambiguity is unfortunate, but other -- natural nomenclature such as Integral are similarly -- conflicted. data Domain -- | The variable lies in the real numbers Continuous :: Domain -- | The variable lies in the integers Integer :: Domain -- | The variable lies in the set {0, 1}. Binary :: Domain -- | Non-strict inequalities. data Inequality a Inequality :: Ordering -> a -> a -> Inequality a -- | A convient shorthand for the type of linear expressions used in -- models. type Expr = LinExpr Double instance GHC.Show.Show Math.Programming.Types.Sense instance GHC.Read.Read Math.Programming.Types.Sense instance GHC.Classes.Ord Math.Programming.Types.Sense instance GHC.Classes.Eq Math.Programming.Types.Sense instance GHC.Show.Show Math.Programming.Types.SolutionStatus instance GHC.Read.Read Math.Programming.Types.SolutionStatus instance GHC.Classes.Ord Math.Programming.Types.SolutionStatus instance GHC.Classes.Eq Math.Programming.Types.SolutionStatus instance GHC.Show.Show Math.Programming.Types.Bounds instance GHC.Read.Read Math.Programming.Types.Bounds instance GHC.Show.Show Math.Programming.Types.Domain instance GHC.Read.Read Math.Programming.Types.Domain instance Data.Traversable.Traversable Math.Programming.Types.Inequality instance Data.Foldable.Foldable Math.Programming.Types.Inequality instance GHC.Base.Functor Math.Programming.Types.Inequality instance GHC.Show.Show a => GHC.Show.Show (Math.Programming.Types.Inequality a) instance GHC.Read.Read a => GHC.Read.Read (Math.Programming.Types.Inequality a) instance Math.Programming.Types.MonadIP v c o m => Math.Programming.Types.MonadIP v c o (Control.Monad.Trans.Reader.ReaderT r m) instance Math.Programming.Types.MonadIP v c o m => Math.Programming.Types.MonadIP v c o (Control.Monad.Trans.State.Lazy.StateT s m) instance Math.Programming.Types.MonadLP v c o m => Math.Programming.Types.MonadLP v c o (Control.Monad.Trans.Reader.ReaderT r m) instance (Math.Programming.Types.MonadLP v c o m, GHC.Base.Monoid w) => Math.Programming.Types.MonadLP v c o (Control.Monad.Trans.Writer.Lazy.WriterT w m) instance (Math.Programming.Types.MonadLP v c o m, GHC.Base.Monoid w) => Math.Programming.Types.MonadLP v c o (Control.Monad.Trans.RWS.Lazy.RWST r w s m) instance Math.Programming.Types.MonadLP v c o m => Math.Programming.Types.MonadLP v c o (Control.Monad.Trans.State.Lazy.StateT s m) module Math.Programming.Dsl -- | Create an objective to be minimized. minimize :: MonadLP v c o m => Expr v -> m o -- | Create an objective to be maximized. maximize :: MonadLP v c o m => Expr v -> m o -- | Get the value of a linear expression in the current solution. evalExpr :: MonadLP v c o m => Expr v -> m Double -- | Create a new free variable. free :: MonadLP v c o m => m v -- | Create a new non-negative variable. nonNeg :: MonadLP v c o m => m v -- | Create a new non-positive variable. nonPos :: MonadLP v c o m => m v -- | Create a new variable bounded between two values. bounded :: MonadLP v c o m => Double -> Double -> m v -- | Constrain a variable to take on certain values. -- -- This function is designed to be used as an infix operator, e.g. -- --
--   integer `within` 'Interval 3 7'
--   
-- -- creates an integer variable that can take on values 3, 4, 5, 6, or 7. within :: MonadLP v c o m => m v -> Bounds -> m v -- | Create an integer-valued variable. integer :: MonadIP v c o m => m v -- | Create a binary variable. binary :: MonadIP v c o m => m v -- | Create an integer-value variable that takes on non-negative values. nonNegInteger :: MonadIP v c o m => m v -- | Create an integer-value variable that takes on non-positive values. nonPosInteger :: MonadIP v c o m => m v -- | Set the type of a variable. -- -- This function is designed to be used as an infix operator, e.g. -- --
--   free `asKind` Binary
--   
asKind :: MonadIP v c o m => m v -> Domain -> m v -- | A less-than or equal-to constraint (.<=.) :: MonadLP v c o m => Expr v -> Expr v -> m c infix 4 .<=. -- | A less-than or equal-to constraint with a numeric left-hand side (<=.) :: MonadLP v c o m => Double -> Expr v -> m c infix 4 <=. -- | A less-than or equal-to constraint with a numeric right-hand side (.<=) :: MonadLP v c o m => Expr v -> Double -> m c infix 4 .<= -- | A greater-than or equal-to constraint (.>=.) :: MonadLP v c o m => Expr v -> Expr v -> m c infix 4 .>=. -- | A greater-than or equal-to constraint with a numeric left-hand side (>=.) :: MonadLP v c o m => Double -> Expr v -> m c infix 4 >=. -- | A greater-than or equal-to constraint with a numeric right-hand side (.>=) :: MonadLP v c o m => Expr v -> Double -> m c infix 4 .>= -- | An equality constraint (.==.) :: MonadLP v c o m => Expr v -> Expr v -> m c infix 4 .==. -- | An equality constraint with a numeric left-hand side (==.) :: MonadLP v c o m => Double -> Expr v -> m c infix 4 ==. -- | An equality constraint with a numeric right-hand side (.==) :: MonadLP v c o m => Expr v -> Double -> m c infix 4 .== formatExpr :: MonadLP v c o m => Expr v -> m Text formatExpr' :: Monad m => (v -> m Text) -> Expr v -> m Text withVariableName :: MonadLP v c o m => m v -> Text -> m v withConstraintName :: MonadLP v c o m => m c -> Text -> m c withObjectiveName :: MonadLP v c o m => m o -> Text -> m o -- | A library for modeling and solving linear and integer programs. -- -- This library is merely a frontend to various solver backends. At the -- time this was written, the only known supported backend is -- GLPK. -- -- This page includes a high-level overview of the model building DSL, as -- well as a deeper dive into the core monadic interface. module Math.Programming -- | Create a new free variable. free :: MonadLP v c o m => m v -- | Create a new variable bounded between two values. bounded :: MonadLP v c o m => Double -> Double -> m v -- | Create a new non-negative variable. nonNeg :: MonadLP v c o m => m v -- | Create a new non-positive variable. nonPos :: MonadLP v c o m => m v -- | Create an integer-valued variable. integer :: MonadIP v c o m => m v -- | Create a binary variable. binary :: MonadIP v c o m => m v -- | Create an integer-value variable that takes on non-negative values. nonNegInteger :: MonadIP v c o m => m v -- | Create an integer-value variable that takes on non-positive values. nonPosInteger :: MonadIP v c o m => m v -- | Constrain a variable to take on certain values. -- -- This function is designed to be used as an infix operator, e.g. -- --
--   integer `within` 'Interval 3 7'
--   
-- -- creates an integer variable that can take on values 3, 4, 5, 6, or 7. within :: MonadLP v c o m => m v -> Bounds -> m v -- | Set the type of a variable. -- -- This function is designed to be used as an infix operator, e.g. -- --
--   free `asKind` Binary
--   
asKind :: MonadIP v c o m => m v -> Domain -> m v -- | An equality constraint (.==.) :: MonadLP v c o m => Expr v -> Expr v -> m c infix 4 .==. -- | An equality constraint with a numeric left-hand side (==.) :: MonadLP v c o m => Double -> Expr v -> m c infix 4 ==. -- | An equality constraint with a numeric right-hand side (.==) :: MonadLP v c o m => Expr v -> Double -> m c infix 4 .== -- | A less-than or equal-to constraint (.<=.) :: MonadLP v c o m => Expr v -> Expr v -> m c infix 4 .<=. -- | A less-than or equal-to constraint with a numeric left-hand side (<=.) :: MonadLP v c o m => Double -> Expr v -> m c infix 4 <=. -- | A less-than or equal-to constraint with a numeric right-hand side (.<=) :: MonadLP v c o m => Expr v -> Double -> m c infix 4 .<= -- | A greater-than or equal-to constraint (.>=.) :: MonadLP v c o m => Expr v -> Expr v -> m c infix 4 .>=. -- | A greater-than or equal-to constraint with a numeric left-hand side (>=.) :: MonadLP v c o m => Double -> Expr v -> m c infix 4 >=. -- | A greater-than or equal-to constraint with a numeric right-hand side (.>=) :: MonadLP v c o m => Expr v -> Double -> m c infix 4 .>= -- | Create an objective to be minimized. minimize :: MonadLP v c o m => Expr v -> m o -- | Create an objective to be maximized. maximize :: MonadLP v c o m => Expr v -> m o -- | A linear expression with a single variable term. var :: Num a => b -> LinExpr a b -- | A linear expression with only a constant term. con :: a -> LinExpr a b -- | Construct a term in a linear expression by multiplying a variable by a -- constant. (.*) :: Num a => b -> a -> LinExpr a b infixl 7 .* -- | Construct a term in a linear expression by multiplying a constant by a -- variable. (*.) :: Num a => a -> b -> LinExpr a b infixl 7 *. -- | Addition of linear expressions. (.+.) :: Num a => LinExpr a b -> LinExpr a b -> LinExpr a b infixl 6 .+. -- | The difference of linear expressions. (.-.) :: Num a => LinExpr a b -> LinExpr a b -> LinExpr a b infixl 6 .-. -- | Construct a term in a linear expression by dividing a variable by a -- constant. (./) :: Fractional a => b -> a -> LinExpr a b infixl 7 ./ -- | Reduce an expression to its value. eval :: Num a => LinExpr a a -> a -- | Simplify an expression by grouping like terms. simplify :: (Num a, Ord b) => LinExpr a b -> LinExpr a b -- | The sum of variable terms with coefficients of unity. vsum :: Num a => [b] -> LinExpr a b -- | The sum of linear expressions. esum :: Num a => Foldable t => t (LinExpr a b) -> LinExpr a b -- | Multiplication of linear expressions by a constant. scale :: Num a => a -> LinExpr a b -> LinExpr a b -- | A linear program. -- -- This is a monadic context for formulating and solving linear programs. -- The types v, c, and o refer to the types of -- variables, constraints, and objectives, respectively, used by a -- particular solver backend. class Monad m => MonadLP v c o m | m -> v c o -- | Add a new (free) variable to the model. -- -- See free, bounded, nonNeg, and nonPos as -- higher-level alternatives. addVariable :: MonadLP v c o m => m v -- | Remove a variable from the model. deleteVariable :: MonadLP v c o m => v -> m () -- | Get the name of a variable. getVariableName :: MonadLP v c o m => v -> m Text -- | Set a name for a variable. setVariableName :: MonadLP v c o m => v -> Text -> m () -- | Retrieve the current bounds associated with a variable. getVariableBounds :: MonadLP v c o m => v -> m Bounds -- | Apply bounds to a variable. -- -- See within as a higher-level alternative. setVariableBounds :: MonadLP v c o m => v -> Bounds -> m () -- | Get the value of a variable in the current solution. -- -- This value could be arbitrary if no solve has been completed, or a -- solve produced an infeasible or unbounded solution. getVariableValue :: MonadLP v c o m => v -> m Double -- | Add a constraint representing the given inequality to the model. -- -- See the .==., .==#, ==., .>=., -- .>=, >=., .<=., .<=, and -- <=. functions as higher-level alternatives. addConstraint :: MonadLP v c o m => Inequality (Expr v) -> m c -- | Remove a constraint from the model. deleteConstraint :: MonadLP v c o m => c -> m () -- | Get the name of a constraint. getConstraintName :: MonadLP v c o m => c -> m Text -- | Set a name for a constraint. setConstraintName :: MonadLP v c o m => c -> Text -> m () -- | Get the dual value associated with a constraint. getConstraintValue :: MonadLP v c o m => c -> m Double -- | Add an objective to the problem. -- -- Depending on the solver backend, this might replace an existing -- objective. addObjective :: MonadLP v c o m => Expr v -> m o -- | Remove an objective from the model. deleteObjective :: MonadLP v c o m => o -> m () -- | Get the name of a objective. getObjectiveName :: MonadLP v c o m => o -> m Text -- | Set a name for a objective. setObjectiveName :: MonadLP v c o m => o -> Text -> m () -- | Get the sense of an objective. getObjectiveSense :: MonadLP v c o m => o -> m Sense -- | Set the sense of an objective. setObjectiveSense :: MonadLP v c o m => o -> Sense -> m () -- | Get the value of an objective. getObjectiveValue :: MonadLP v c o m => o -> m Double -- | Get the timeout associated with a problem. getTimeout :: MonadLP v c o m => m Double -- | Set the timeout associated with a problem. setTimeout :: MonadLP v c o m => Double -> m () -- | Compute an LP-optimal solution. optimizeLP :: MonadLP v c o m => m SolutionStatus -- | A (mixed) integer program. -- -- In addition to the methods of the MonadLP class, this monad -- supports constraining variables to be either continuous or discrete. class MonadLP v c o m => MonadIP v c o m | m -> v c o getVariableDomain :: MonadIP v c o m => v -> m Domain setVariableDomain :: MonadIP v c o m => v -> Domain -> m () getRelativeMIPGap :: MonadIP v c o m => m Double setRelativeMIPGap :: MonadIP v c o m => Double -> m () optimizeIP :: MonadIP v c o m => m SolutionStatus -- | The type of values that a variable can take on. -- -- Note that the Integer constructor does not interfere with the -- Integer type, as the Integer type does not define a -- constuctor of the same name. The ambiguity is unfortunate, but other -- natural nomenclature such as Integral are similarly -- conflicted. data Domain -- | The variable lies in the real numbers Continuous :: Domain -- | The variable lies in the integers Integer :: Domain -- | The variable lies in the set {0, 1}. Binary :: Domain -- | Get the value of a linear expression in the current solution. evalExpr :: MonadLP v c o m => Expr v -> m Double formatExpr :: MonadLP v c o m => Expr v -> m Text -- | A convient shorthand for the type of linear expressions used in -- models. type Expr = LinExpr Double -- | An interval of the real numbers. data Bounds -- | The non-negative reals. NonNegativeReals :: Bounds -- | The non-positive reals. NonPositiveReals :: Bounds -- | Any closed interval of the reals. Interval :: Double -> Double -> Bounds -- | Any real number. Free :: Bounds -- | The outcome of an optimization. data SolutionStatus -- | An optimal solution has been found. Optimal :: SolutionStatus -- | A feasible solution has been found. The result may or may not be -- optimal. Feasible :: SolutionStatus -- | The model has been proven to be infeasible. Infeasible :: SolutionStatus -- | The model has been proven to be unbounded. Unbounded :: SolutionStatus -- | An error was encountered during the solve. Instance-specific methods -- should be used to determine what occurred. Error :: SolutionStatus -- | Whether a math program is minimizing or maximizing its objective. data Sense Minimization :: Sense Maximization :: Sense -- | Non-strict inequalities. data Inequality a Inequality :: Ordering -> a -> a -> Inequality a -- | A linear expression. -- -- Linear expressions contain symbolic variables of type b and -- numeric coefficients of type a. Often a will be -- Double, and b will be whatever variable type your -- linear program uses. data LinExpr a b LinExpr :: ![(a, b)] -> !a -> LinExpr a b withVariableName :: MonadLP v c o m => m v -> Text -> m v withConstraintName :: MonadLP v c o m => m c -> Text -> m c withObjectiveName :: MonadLP v c o m => m o -> Text -> m o