{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Numeric.HiGHS.LP (
solve,
LP.Direction(..),
Term(..), (LP..*),
Constraints,
LP.free, (LP.<=.), (LP.>=.), (LP.==.), (LP.>=<.),
Method, Priv.simplex, Priv.choose, Priv.ipm,
LPEnum.ModelStatus,
Result,
solveWith,
Query, Priv.getObjectiveValue,
Priv.getOptimalVector, Priv.getSolutionVectors,
Priv.getBasisStatus, Highs.BasisStatus,
) where
import qualified Numeric.HiGHS.LP.Enumeration as LPEnum
import qualified Numeric.HiGHS.LP.FFI as Highs
import qualified Numeric.HiGHS.LP.Debug as Debug
import qualified Numeric.HiGHS.LP.Private as Priv
import Numeric.HiGHS.LP.Private
(Method, Result, Query, checkStatus, runContT, withBuffer,
storeBounds, prepareRowBoundsArrays, prepareColumnBoundsArrays,
storeConstraints, prepareConstraints,
setMethod, objectiveSense, examineStatus)
import qualified Numeric.LinearProgramming.Common as LP
import Numeric.LinearProgramming.Common
(Bounds, Term(Term), Constraints, Direction(..), Objective)
import qualified Data.Array.Comfort.Storable as Array
import qualified Data.Array.Comfort.Shape as Shape
import Control.Monad.IO.Class (liftIO)
import Control.Exception (bracket)
import System.IO.Unsafe (unsafePerformIO)
solve ::
(Shape.Indexed sh, Shape.Index sh ~ ix) =>
Method -> Bounds ix -> Constraints Double ix ->
(Direction, Objective sh) -> Result sh
solve :: forall sh ix.
(Indexed sh, Index sh ~ ix) =>
Method
-> Bounds ix
-> Constraints Double ix
-> (Direction, Objective sh)
-> Result sh
solve = Query sh (Double, Array sh Double)
-> Method
-> Bounds ix
-> Constraints Double ix
-> (Direction, Array sh Double)
-> (ModelStatus, Maybe (Double, Array sh Double))
forall sh ix result.
(Indexed sh, Index sh ~ ix) =>
Query sh result
-> Method
-> Bounds ix
-> Constraints Double ix
-> (Direction, Objective sh)
-> (ModelStatus, Maybe result)
solveWith Query sh (Double, Array sh Double)
forall sh. C sh => Query sh (Double, Array sh Double)
Priv.getResult
solveWith ::
(Shape.Indexed sh, Shape.Index sh ~ ix) =>
Query sh result -> Method -> Bounds ix -> Constraints Double ix ->
(Direction, Objective sh) -> (LPEnum.ModelStatus, Maybe result)
solveWith :: forall sh ix result.
(Indexed sh, Index sh ~ ix) =>
Query sh result
-> Method
-> Bounds ix
-> Constraints Double ix
-> (Direction, Objective sh)
-> (ModelStatus, Maybe result)
solveWith Query sh result
query Method
method Bounds ix
bounds Constraints Double ix
constrs (Direction
dir,Objective sh
obj) =
IO (ModelStatus, Maybe result) -> (ModelStatus, Maybe result)
forall a. IO a -> a
unsafePerformIO (IO (ModelStatus, Maybe result) -> (ModelStatus, Maybe result))
-> IO (ModelStatus, Maybe result) -> (ModelStatus, Maybe result)
forall a b. (a -> b) -> a -> b
$
let shape :: sh
shape = Objective sh -> sh
forall sh a. Array sh a -> sh
Array.shape Objective sh
obj in
let numCols :: Int
numCols = sh -> Int
forall sh. C sh => sh -> Int
Shape.size sh
shape in
let numRows :: Int
numRows = Constraints Double ix -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Constraints Double ix
constrs in
ContT (ModelStatus, Maybe result) IO (ModelStatus, Maybe result)
-> IO (ModelStatus, Maybe result)
forall a. ContT a IO a -> IO a
runContT (ContT (ModelStatus, Maybe result) IO (ModelStatus, Maybe result)
-> IO (ModelStatus, Maybe result))
-> ContT (ModelStatus, Maybe result) IO (ModelStatus, Maybe result)
-> IO (ModelStatus, Maybe result)
forall a b. (a -> b) -> a -> b
$ do
Ptr CDouble
objPtr <- Array sh CDouble
-> ContT (ModelStatus, Maybe result) IO (Ptr CDouble)
forall sh a r. Array sh a -> ContT r IO (Ptr a)
withBuffer (Array sh CDouble
-> ContT (ModelStatus, Maybe result) IO (Ptr CDouble))
-> Array sh CDouble
-> ContT (ModelStatus, Maybe result) IO (Ptr CDouble)
forall a b. (a -> b) -> a -> b
$ (Double -> CDouble) -> Objective sh -> Array sh CDouble
forall sh a b.
(C sh, Storable a, Storable b) =>
(a -> b) -> Array sh a -> Array sh b
Array.map Double -> CDouble
forall a b. (Real a, Fractional b) => a -> b
realToFrac Objective sh
obj
(Ptr CDouble
collbPtr,Ptr CDouble
colubPtr) <-
(Array sh CDouble, Array sh CDouble)
-> ContT (ModelStatus, Maybe result) IO (Ptr CDouble, Ptr CDouble)
forall sh r.
(Array sh CDouble, Array sh CDouble)
-> ContT r IO (Ptr CDouble, Ptr CDouble)
storeBounds ((Array sh CDouble, Array sh CDouble)
-> ContT (ModelStatus, Maybe result) IO (Ptr CDouble, Ptr CDouble))
-> (Array sh CDouble, Array sh CDouble)
-> ContT (ModelStatus, Maybe result) IO (Ptr CDouble, Ptr CDouble)
forall a b. (a -> b) -> a -> b
$ sh -> Bounds ix -> (Array sh CDouble, Array sh CDouble)
forall sh ix.
(Indexed sh, Index sh ~ ix) =>
sh -> Bounds ix -> (Array sh CDouble, Array sh CDouble)
prepareColumnBoundsArrays sh
shape Bounds ix
bounds
(Ptr CDouble
rowlbPtr,Ptr CDouble
rowubPtr) <- (Array ShapeInt CDouble, Array ShapeInt CDouble)
-> ContT (ModelStatus, Maybe result) IO (Ptr CDouble, Ptr CDouble)
forall sh r.
(Array sh CDouble, Array sh CDouble)
-> ContT r IO (Ptr CDouble, Ptr CDouble)
storeBounds ((Array ShapeInt CDouble, Array ShapeInt CDouble)
-> ContT (ModelStatus, Maybe result) IO (Ptr CDouble, Ptr CDouble))
-> (Array ShapeInt CDouble, Array ShapeInt CDouble)
-> ContT (ModelStatus, Maybe result) IO (Ptr CDouble, Ptr CDouble)
forall a b. (a -> b) -> a -> b
$ Constraints Double ix
-> (Array ShapeInt CDouble, Array ShapeInt CDouble)
forall ix.
Bounds ix -> (Array ShapeInt CDouble, Array ShapeInt CDouble)
prepareRowBoundsArrays Constraints Double ix
constrs
let (Array ShapeInt CDouble
coefficients, Array ShapeInt HighsInt
indices, Array ShapeInt HighsInt
rowStarts) = sh
-> Constraints Double ix
-> (Array ShapeInt CDouble, Array ShapeInt HighsInt,
Array ShapeInt HighsInt)
forall sh ix.
(Indexed sh, Index sh ~ ix) =>
sh
-> Constraints Double ix
-> (Array ShapeInt CDouble, Array ShapeInt HighsInt,
Array ShapeInt HighsInt)
prepareConstraints sh
shape Constraints Double ix
constrs
(Ptr CDouble
coefficientsPtr, Ptr HighsInt
indexPtr, Ptr HighsInt
startPtr)
<- (Array ShapeInt CDouble, Array ShapeInt HighsInt,
Array ShapeInt HighsInt)
-> ContT
(ModelStatus, Maybe result)
IO
(Ptr CDouble, Ptr HighsInt, Ptr HighsInt)
forall r.
(Array ShapeInt CDouble, Array ShapeInt HighsInt,
Array ShapeInt HighsInt)
-> ContT r IO (Ptr CDouble, Ptr HighsInt, Ptr HighsInt)
storeConstraints (Array ShapeInt CDouble
coefficients, Array ShapeInt HighsInt
indices, Array ShapeInt HighsInt
rowStarts)
IO (ModelStatus, Maybe result)
-> ContT (ModelStatus, Maybe result) IO (ModelStatus, Maybe result)
forall a. IO a -> ContT (ModelStatus, Maybe result) IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (ModelStatus, Maybe result)
-> ContT
(ModelStatus, Maybe result) IO (ModelStatus, Maybe result))
-> IO (ModelStatus, Maybe result)
-> ContT (ModelStatus, Maybe result) IO (ModelStatus, Maybe result)
forall a b. (a -> b) -> a -> b
$ IO (Ptr Highs)
-> (Ptr Highs -> IO ())
-> (Ptr Highs -> IO (ModelStatus, Maybe result))
-> IO (ModelStatus, Maybe result)
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket IO (Ptr Highs)
Highs.create Ptr Highs -> IO ()
Highs.destroy ((Ptr Highs -> IO (ModelStatus, Maybe result))
-> IO (ModelStatus, Maybe result))
-> (Ptr Highs -> IO (ModelStatus, Maybe result))
-> IO (ModelStatus, Maybe result)
forall a b. (a -> b) -> a -> b
$ \Ptr Highs
model -> do
Ptr Highs -> IO ()
Debug.initLog Ptr Highs
model
Ptr Highs -> Method -> IO ()
setMethod Ptr Highs
model Method
method
IO Status -> IO ()
checkStatus (IO Status -> IO ()) -> IO Status -> IO ()
forall a b. (a -> b) -> a -> b
$ Ptr Highs
-> HighsInt
-> HighsInt
-> HighsInt
-> MatrixFormat
-> ObjSense
-> CDouble
-> Ptr CDouble
-> Ptr CDouble
-> Ptr CDouble
-> Ptr CDouble
-> Ptr CDouble
-> Ptr HighsInt
-> Ptr HighsInt
-> Ptr CDouble
-> IO Status
Highs.passLp Ptr Highs
model
(Int -> HighsInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
numCols)
(Int -> HighsInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
numRows)
(Int -> HighsInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> HighsInt) -> Int -> HighsInt
forall a b. (a -> b) -> a -> b
$ ShapeInt -> Int
forall sh. C sh => sh -> Int
Shape.size (ShapeInt -> Int) -> ShapeInt -> Int
forall a b. (a -> b) -> a -> b
$ Array ShapeInt CDouble -> ShapeInt
forall sh a. Array sh a -> sh
Array.shape Array ShapeInt CDouble
coefficients)
MatrixFormat
Highs.matrixFormatRowwise
(Direction -> ObjSense
objectiveSense Direction
dir)
CDouble
0 Ptr CDouble
objPtr
Ptr CDouble
collbPtr Ptr CDouble
colubPtr
Ptr CDouble
rowlbPtr Ptr CDouble
rowubPtr
Ptr HighsInt
startPtr Ptr HighsInt
indexPtr Ptr CDouble
coefficientsPtr
Query sh result
-> sh -> Ptr Highs -> Status -> IO (ModelStatus, Maybe result)
forall sh a.
C sh =>
Query sh a
-> sh -> Ptr Highs -> Status -> IO (ModelStatus, Maybe a)
examineStatus Query sh result
query sh
shape Ptr Highs
model (Status -> IO (ModelStatus, Maybe result))
-> IO Status -> IO (ModelStatus, Maybe result)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ptr Highs -> IO Status
Highs.run Ptr Highs
model