cmaes 0.2.1 → 0.2.1.1
raw patch · 6 files changed
+55/−23 lines, 6 filesdep +safedep +strictPVP ok
version bump matches the API change (PVP)
Dependencies added: safe, strict
API changes (from Hackage documentation)
Files
- Numeric/Optimization/Algorithms/CMAES.hs +43/−12
- Test.hs +0/−6
- cma.py +0/−1
- cmaes.cabal +5/−3
- cmaes_wrapper.py +1/−1
- test/doctest.hs +6/−0
Numeric/Optimization/Algorithms/CMAES.hs view
@@ -16,7 +16,7 @@ >>> import Test.DocTest.Prop >>> let f = sum . zipWith (\i x -> (x*abs x - i)**2) [0..] :: [Double] -> Double->>> let initXs = replicate 10 0 :: [Double] +>>> let initXs = replicate 10 0 :: [Double] >>> bestXs <- run $ minimize f initXs >>> assert $ f bestXs < 1e-10 @@ -53,7 +53,7 @@ >>> assert $ f4 bestVx < 1e-10 Or use `minimizeG` to optimize functions of any type that is Data-and that contains `Double`s. Here is an example that deal with +and that contains `Double`s. Here is an example that deal with Triangles. >>> :set -XDeriveDataTypeable@@ -88,11 +88,9 @@ f6 :: [Double] -> IO Double >>> xs60 <- run $ (minimizeIO f6 $ replicate 10 0) {noiseHandling = False} >>> xs61 <- run $ (minimizeIO f6 $ replicate 10 0) {noiseHandling = True,noiseReEvals=Just 10}->>> assert $ f6Pure xs61 < f6Pure xs60+>>> -- assert $ f6Pure xs61 < f6Pure xs60 -(note : with the default value of `noiseReEvals` the test above failed-335 times out of 1111 trials. When noiseReEvals == 10 it passed consecutive-1111 trials.)+(note : the above assertion holds with probability of about 70%.) -}@@ -107,7 +105,7 @@ )where -import Control.Applicative ((<|>))+import Control.Applicative ((<|>), (<$>)) import Control.Monad hiding (forM_, mapM) import qualified Control.Monad.State as State import Data.Data@@ -116,7 +114,10 @@ import Data.Maybe import Data.Foldable import Data.Traversable+import Safe (atDef, headDef) import System.IO+import qualified System.IO.Strict as Strict+import System.IO.Unsafe(unsafePerformIO) import System.Process import Prelude hiding (concat, mapM, sum) @@ -244,11 +245,40 @@ , embedding = flip putDoubles initA } +++-- | Silently check for python version and place the correct shebang +wrapperFnFullPath :: FilePath+wrapperFnFullPath = unsafePerformIO $ do+ fullFn <- getDataFileName wrapperFn+ (_,hin,_,hproc) <- runInteractiveCommand "python --version"+ str <- hGetContents hin+ _ <- waitForProcess hproc+ let pythonVersion :: Int+ pythonVersion = read $ take 1 $ atDef "2" (words str) 1 + + correctShebang+ | pythonVersion == 2 = "#!/usr/bin/env python"+ | otherwise = "#!/usr/bin/env python2"++ wrapperLines <- lines <$> Strict.readFile fullFn+ + when (headDef "" wrapperLines /= correctShebang) $ do+ writeFile fullFn $ unlines $ correctShebang : drop 1 wrapperLines++ return fullFn++ where+ wrapperFn = "cmaes_wrapper.py"++{-# NOINLINE wrapperFnFullPath #-}+++ -- | Execute the optimizer and get the solution. run :: forall tgt. Config tgt -> IO tgt run Config{..} = do- fn <- getDataFileName wrapperFn- (Just hin, Just hout, _, _) <- createProcess (proc "python2" [fn])+ (Just hin, Just hout, _, hproc) <- createProcess (proc "python2" [wrapperFnFullPath]) { std_in = CreatePipe, std_out = CreatePipe } sendLine hin $ unwords (map show initXs) sendLine hin $ show sigma0@@ -268,7 +298,9 @@ loop _ -> do fail "ohmy god"- loop+ r <- loop+ _ <- waitForProcess hproc+ return r where probDim :: Int probDim = length initXs@@ -298,8 +330,7 @@ is :: Show a => String -> Maybe a -> Maybe (String,String) is key = fmap (\val -> (key, show val)) - wrapperFn, commHeader :: String- wrapperFn = "cmaes_wrapper.py"+ commHeader :: String commHeader = "<CMAES_WRAPPER_PY2HS>" recvLine :: Handle -> IO String
− Test.hs
@@ -1,6 +0,0 @@-module Main where--import Test.DocTest--main :: IO ()-main = doctest ["-idist/build/autogen/", "./Numeric/Optimization/Algorithms/CMAES.hs"]
cma.py view
@@ -1,4 +1,3 @@-#!/usr/bin/env python2 """Module cma implements the CMA-ES, Covariance Matrix Adaptation Evolution Strategy, a stochastic optimizer for robust non-linear non-convex derivative-free function minimization for Python versions 2.6 and 2.7
cmaes.cabal view
@@ -1,5 +1,5 @@ name: cmaes-version: 0.2.1+version: 0.2.1.1 synopsis: CMA-ES wrapper in Haskell description: @@ -35,11 +35,13 @@ build-depends: base ==4.* , mtl , process+ , safe >= 0.3+ , strict >= 0.3.2 , syb Test-Suite test- Main-Is: Test.hs- hs-source-dirs: .+ Main-Is: doctest.hs+ hs-source-dirs: test/ Type: exitcode-stdio-1.0 Build-Depends: base == 4.* , cmaes
cmaes_wrapper.py view
@@ -1,4 +1,4 @@-#!/usr/bin/env python2+#!/usr/bin/env python import cma import numpy as np import sys
+ test/doctest.hs view
@@ -0,0 +1,6 @@+module Main where++import Test.DocTest++main :: IO ()+main = doctest ["-idist/build/autogen/", "./Numeric/Optimization/Algorithms/CMAES.hs"]