hmatrix-quadprogpp 0.1.0.0 → 0.2.0.1
raw patch · 2 files changed
+32/−17 lines, 2 filesdep ~base
Dependency ranges changed: base
Files
Numeric/Minimization/QuadProgPP.hs view
@@ -1,7 +1,10 @@ {-# LANGUAGE BangPatterns #-} {-# LANGUAGE ViewPatterns #-} {-# LANGUAGE ForeignFunctionInterface #-}-module Numeric.Minimization.QuadProgPP (solveQuadProg) where+module Numeric.Minimization.QuadProgPP+ ( solveQuadProg+ , QuadProgPPError(..)+ ) where import Control.Applicative import Control.Monad@@ -17,6 +20,18 @@ import Foreign.Storable import System.IO.Unsafe (unsafePerformIO) +-- | Errors that can happen in 'solveQuadProg'.+data QuadProgPPError+ = QuadProgInfeasible+ -- ^ The problem has no feasible solution.+ | QuadProgSizeMismatch+ -- ^ The given matrices and vectors have inconsistent+ -- dimensionalities.+ | QuadProgOtherError String+ -- ^ Other errors. Currently this is used for C++ exceptions+ -- thrown by QuadProg++.+ deriving (Show, Eq)+ -- | Solve a strictly convex quadratic program with optional linear -- constraints. It returns a pair of the optimal solution and the -- value of the objective function at that point. On error it returns@@ -35,7 +50,7 @@ -- ^ Optional inequality constraints. When given, this -- argument should be of the form @Just (E, F)@, which -- represents linear inequalities @x -> x'E + F >= 0@.- -> Either String (Vector Double, Double)+ -> Either QuadProgPPError (Vector Double, Double) solveQuadProg (g, g0) (split -> (ce, ce0)) (split -> (ci, ci0)) = unsafePerformIO $ mat' (Just g) $ \gRow gCol gPtr ->@@ -44,7 +59,7 @@ vec' ce0 $ \ce0Size ce0Ptr -> mat' (trans <$> ci) $ \ciRow ciCol ciPtr -> vec' ci0 $ \ci0Size ci0Ptr ->- fromMaybe (return $ Left sizeMismatchError) $ do+ fromMaybe (return $ Left QuadProgSizeMismatch) $ do let !nVar = gRow guard $ gCol == nVar guard $ g0Size == nVar@@ -64,22 +79,21 @@ ptrSolution ptrErrorStr errorCStr <- peek ptrErrorStr- if errorCStr == nullPtr -- success- then let- !solutionVec = VS.unsafeFromForeignPtr0 fpSolution- (fromIntegral nVar)- in return $ Right (solutionVec, best)- else do- errorStr <- peekCAString errorCStr- free errorCStr- return $ Left errorStr+ case () of+ _ | errorCStr /= nullPtr -> do -- exception+ errorStr <- peekCAString errorCStr+ free errorCStr+ return $ Left $ QuadProgOtherError errorStr+ | best == (1/0) -> return $ Left QuadProgInfeasible+ | otherwise -> let+ !solutionVec = VS.unsafeFromForeignPtr0 fpSolution+ (fromIntegral nVar)+ in return $ Right (solutionVec, best) where mat' (Just m) f = mat (cmat m) $ \church -> church $ \nrow ncol ptr -> f nrow ncol ptr mat' Nothing f = f 0 0 nullPtr vec' (Just v) f = vec v $ \church -> church $ \size ptr -> f size ptr vec' Nothing f = f 0 nullPtr- sizeMismatchError =- "Numeric.Minimization.QuadProgPP.solveQuadProg: size mismatch" split :: Maybe (a, b) -> (Maybe a, Maybe b) split x = (fst <$> x, snd <$> x)
hmatrix-quadprogpp.cabal view
@@ -2,8 +2,8 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: hmatrix-quadprogpp-version: 0.1.0.0-synopsis: Bindings to QuadProg+++version: 0.2.0.1+synopsis: Bindings to the QuadProg++ quadratic programming library description: Bindings to QuadProg++, a C++ library for quadratic programming. <http://sourceforge.net/projects/quadprog/>@@ -21,8 +21,9 @@ location: https://github.com/alang9/hmatrix-quadprogpp.git library+ ghc-options: -Wall c-sources: c++/binding.cxx exposed-modules: Numeric.Minimization.QuadProgPP -- other-modules: - build-depends: base ==4.5.*, vector >= 0.9, hmatrix >= 0.14+ build-depends: base >= 4.5 && < 4.7, vector >= 0.9, hmatrix >= 0.14 extra-libraries: QuadProgpp, stdc++