module Nlopt.Raw where
import Ipopt.AnyRF
import Foreign.C.Types
import Foreign.Ptr
import Foreign.Marshal
import Foreign.Storable
import Data.Int
import C2HS
import qualified Data.Vector.Storable.Mutable as VM
import qualified Data.Vector as V
import qualified Data.Vector.Storable as VS
import qualified Data.Vector.Generic as VG
import Control.Exception
import Data.Typeable
import Numeric.AD (grad, jacobian, hessian)
import Control.Monad
toPrecondAD :: (forall a. AnyRFCxt a => V.Vector a -> a) -> Precond
toPrecondAD f n xs vs r _usrData =
toPrecondG (\x v -> return (hessian f x `mXv` v)) n xs vs r _usrData
toPrecondG :: (VG.Vector vec Double, v ~ vec Double)
=> (v -> v -> IO v)
-> Precond
toPrecondG f n xs vs r _usrData = do
xs' <- ptrToV n xs
vs' <- ptrToV n vs
copyInto n r =<< f xs' vs'
toFuncM :: VG.Vector v Double => (v Double -> IO (v Double) ) -> MFunc
toFuncM f m r n xs _g _usrData = do
copyInto m r =<< f =<< ptrToV n xs
toFuncMG :: (VG.Vector n Double, VG.Vector n (m Double), n ~ m)
=> (n Double -> IO (m Double) )
-> (n Double -> IO (n (m Double)))
-> MFunc
toFuncMG f g m r n xs g' _usrData = do
xs' <- ptrToV n xs
copyInto m r =<< f xs'
copyInto (n*m) g' . VG.concat . VG.toList =<< g xs'
toFuncMAD :: (forall a. AnyRFCxt a => V.Vector a -> V.Vector a) -> MFunc
toFuncMAD f m r n xs g _usrData = do
xs' <- ptrToV n xs
copyInto m r (f xs')
copyInto (n*m) g (V.concat (V.toList (jacobian f xs')))
toFunc :: (VG.Vector v Double) => (v Double -> IO Double) -> Func
toFunc f n xs _g _usrData = fmap CDouble $ f =<< ptrToV n xs
toFuncAD :: (forall a. AnyRFCxt a => V.Vector a -> a) -> Func
toFuncAD f n xs g _usrData = do
xs' <- ptrToV n xs
copyInto n g (grad f xs')
return (CDouble (f xs'))
toFuncG :: (VG.Vector v Double) => (v Double -> IO Double)
-> (v Double -> IO (v Double))
-> Func
toFuncG f g n xs g' _usrData = do
xs' <- ptrToV n xs
copyInto n g' =<< g xs'
CDouble `fmap` f xs'
type family UnFunPtr a
type instance UnFunPtr (FunPtr a) = a
type Func = UnFunPtr FunPtrFunc
type MFunc = UnFunPtr FunPtrMFunc
type Precond = UnFunPtr FunPtrPrecond
type FunPtrFunc = ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO CDouble)))))))
type FunPtrMFunc = ((FunPtr (CUInt -> ((Ptr CDouble) -> (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO ())))))))))
type FunPtrPrecond = ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO ()))))))))
newtype NLOpt = NLOpt (ForeignPtr (NLOpt))
withNLOpt (NLOpt fptr) = withForeignPtr fptr
data NloptAlgorithm = NLOPT_GN_DIRECT
| NLOPT_GN_DIRECT_L
| NLOPT_GN_DIRECT_L_RAND
| NLOPT_GN_DIRECT_NOSCAL
| NLOPT_GN_DIRECT_L_NOSCAL
| NLOPT_GN_DIRECT_L_RAND_NOSCAL
| NLOPT_GN_ORIG_DIRECT
| NLOPT_GN_ORIG_DIRECT_L
| NLOPT_GD_STOGO
| NLOPT_GD_STOGO_RAND
| NLOPT_LD_LBFGS_NOCEDAL
| NLOPT_LD_LBFGS
| NLOPT_LN_PRAXIS
| NLOPT_LD_VAR1
| NLOPT_LD_VAR2
| NLOPT_LD_TNEWTON
| NLOPT_LD_TNEWTON_RESTART
| NLOPT_LD_TNEWTON_PRECOND
| NLOPT_LD_TNEWTON_PRECOND_RESTART
| NLOPT_GN_CRS2_LM
| NLOPT_GN_MLSL
| NLOPT_GD_MLSL
| NLOPT_GN_MLSL_LDS
| NLOPT_GD_MLSL_LDS
| NLOPT_LD_MMA
| NLOPT_LN_COBYLA
| NLOPT_LN_NEWUOA
| NLOPT_LN_NEWUOA_BOUND
| NLOPT_LN_NELDERMEAD
| NLOPT_LN_SBPLX
| NLOPT_LN_AUGLAG
| NLOPT_LD_AUGLAG
| NLOPT_LN_AUGLAG_EQ
| NLOPT_LD_AUGLAG_EQ
| NLOPT_LN_BOBYQA
| NLOPT_GN_ISRES
| NLOPT_AUGLAG
| NLOPT_AUGLAG_EQ
| NLOPT_G_MLSL
| NLOPT_G_MLSL_LDS
| NLOPT_LD_SLSQP
| NLOPT_LD_CCSAQ
| NLOPT_GN_ESCH
| NLOPT_NUM_ALGORITHMS
deriving (Show,Bounded,Ord,Eq)
instance Enum NloptAlgorithm where
fromEnum NLOPT_GN_DIRECT = 0
fromEnum NLOPT_GN_DIRECT_L = 1
fromEnum NLOPT_GN_DIRECT_L_RAND = 2
fromEnum NLOPT_GN_DIRECT_NOSCAL = 3
fromEnum NLOPT_GN_DIRECT_L_NOSCAL = 4
fromEnum NLOPT_GN_DIRECT_L_RAND_NOSCAL = 5
fromEnum NLOPT_GN_ORIG_DIRECT = 6
fromEnum NLOPT_GN_ORIG_DIRECT_L = 7
fromEnum NLOPT_GD_STOGO = 8
fromEnum NLOPT_GD_STOGO_RAND = 9
fromEnum NLOPT_LD_LBFGS_NOCEDAL = 10
fromEnum NLOPT_LD_LBFGS = 11
fromEnum NLOPT_LN_PRAXIS = 12
fromEnum NLOPT_LD_VAR1 = 13
fromEnum NLOPT_LD_VAR2 = 14
fromEnum NLOPT_LD_TNEWTON = 15
fromEnum NLOPT_LD_TNEWTON_RESTART = 16
fromEnum NLOPT_LD_TNEWTON_PRECOND = 17
fromEnum NLOPT_LD_TNEWTON_PRECOND_RESTART = 18
fromEnum NLOPT_GN_CRS2_LM = 19
fromEnum NLOPT_GN_MLSL = 20
fromEnum NLOPT_GD_MLSL = 21
fromEnum NLOPT_GN_MLSL_LDS = 22
fromEnum NLOPT_GD_MLSL_LDS = 23
fromEnum NLOPT_LD_MMA = 24
fromEnum NLOPT_LN_COBYLA = 25
fromEnum NLOPT_LN_NEWUOA = 26
fromEnum NLOPT_LN_NEWUOA_BOUND = 27
fromEnum NLOPT_LN_NELDERMEAD = 28
fromEnum NLOPT_LN_SBPLX = 29
fromEnum NLOPT_LN_AUGLAG = 30
fromEnum NLOPT_LD_AUGLAG = 31
fromEnum NLOPT_LN_AUGLAG_EQ = 32
fromEnum NLOPT_LD_AUGLAG_EQ = 33
fromEnum NLOPT_LN_BOBYQA = 34
fromEnum NLOPT_GN_ISRES = 35
fromEnum NLOPT_AUGLAG = 36
fromEnum NLOPT_AUGLAG_EQ = 37
fromEnum NLOPT_G_MLSL = 38
fromEnum NLOPT_G_MLSL_LDS = 39
fromEnum NLOPT_LD_SLSQP = 40
fromEnum NLOPT_LD_CCSAQ = 41
fromEnum NLOPT_GN_ESCH = 42
fromEnum NLOPT_NUM_ALGORITHMS = 43
toEnum 0 = NLOPT_GN_DIRECT
toEnum 1 = NLOPT_GN_DIRECT_L
toEnum 2 = NLOPT_GN_DIRECT_L_RAND
toEnum 3 = NLOPT_GN_DIRECT_NOSCAL
toEnum 4 = NLOPT_GN_DIRECT_L_NOSCAL
toEnum 5 = NLOPT_GN_DIRECT_L_RAND_NOSCAL
toEnum 6 = NLOPT_GN_ORIG_DIRECT
toEnum 7 = NLOPT_GN_ORIG_DIRECT_L
toEnum 8 = NLOPT_GD_STOGO
toEnum 9 = NLOPT_GD_STOGO_RAND
toEnum 10 = NLOPT_LD_LBFGS_NOCEDAL
toEnum 11 = NLOPT_LD_LBFGS
toEnum 12 = NLOPT_LN_PRAXIS
toEnum 13 = NLOPT_LD_VAR1
toEnum 14 = NLOPT_LD_VAR2
toEnum 15 = NLOPT_LD_TNEWTON
toEnum 16 = NLOPT_LD_TNEWTON_RESTART
toEnum 17 = NLOPT_LD_TNEWTON_PRECOND
toEnum 18 = NLOPT_LD_TNEWTON_PRECOND_RESTART
toEnum 19 = NLOPT_GN_CRS2_LM
toEnum 20 = NLOPT_GN_MLSL
toEnum 21 = NLOPT_GD_MLSL
toEnum 22 = NLOPT_GN_MLSL_LDS
toEnum 23 = NLOPT_GD_MLSL_LDS
toEnum 24 = NLOPT_LD_MMA
toEnum 25 = NLOPT_LN_COBYLA
toEnum 26 = NLOPT_LN_NEWUOA
toEnum 27 = NLOPT_LN_NEWUOA_BOUND
toEnum 28 = NLOPT_LN_NELDERMEAD
toEnum 29 = NLOPT_LN_SBPLX
toEnum 30 = NLOPT_LN_AUGLAG
toEnum 31 = NLOPT_LD_AUGLAG
toEnum 32 = NLOPT_LN_AUGLAG_EQ
toEnum 33 = NLOPT_LD_AUGLAG_EQ
toEnum 34 = NLOPT_LN_BOBYQA
toEnum 35 = NLOPT_GN_ISRES
toEnum 36 = NLOPT_AUGLAG
toEnum 37 = NLOPT_AUGLAG_EQ
toEnum 38 = NLOPT_G_MLSL
toEnum 39 = NLOPT_G_MLSL_LDS
toEnum 40 = NLOPT_LD_SLSQP
toEnum 41 = NLOPT_LD_CCSAQ
toEnum 42 = NLOPT_GN_ESCH
toEnum 43 = NLOPT_NUM_ALGORITHMS
toEnum unmatched = error ("NloptAlgorithm.toEnum: Cannot match " ++ show unmatched)
data NloptResult = NLOPT_FAILURE
| NLOPT_INVALID_ARGS
| NLOPT_OUT_OF_MEMORY
| NLOPT_ROUNDOFF_LIMITED
| NLOPT_FORCED_STOP
| NLOPT_SUCCESS
| NLOPT_STOPVAL_REACHED
| NLOPT_FTOL_REACHED
| NLOPT_XTOL_REACHED
| NLOPT_MAXEVAL_REACHED
| NLOPT_MAXTIME_REACHED
deriving (Show,Typeable)
instance Enum NloptResult where
fromEnum NLOPT_FAILURE = (1)
fromEnum NLOPT_INVALID_ARGS = (2)
fromEnum NLOPT_OUT_OF_MEMORY = (3)
fromEnum NLOPT_ROUNDOFF_LIMITED = (4)
fromEnum NLOPT_FORCED_STOP = (5)
fromEnum NLOPT_SUCCESS = 1
fromEnum NLOPT_STOPVAL_REACHED = 2
fromEnum NLOPT_FTOL_REACHED = 3
fromEnum NLOPT_XTOL_REACHED = 4
fromEnum NLOPT_MAXEVAL_REACHED = 5
fromEnum NLOPT_MAXTIME_REACHED = 6
toEnum (1) = NLOPT_FAILURE
toEnum (2) = NLOPT_INVALID_ARGS
toEnum (3) = NLOPT_OUT_OF_MEMORY
toEnum (4) = NLOPT_ROUNDOFF_LIMITED
toEnum (5) = NLOPT_FORCED_STOP
toEnum 1 = NLOPT_SUCCESS
toEnum 2 = NLOPT_STOPVAL_REACHED
toEnum 3 = NLOPT_FTOL_REACHED
toEnum 4 = NLOPT_XTOL_REACHED
toEnum 5 = NLOPT_MAXEVAL_REACHED
toEnum 6 = NLOPT_MAXTIME_REACHED
toEnum unmatched = error ("NloptResult.toEnum: Cannot match " ++ show unmatched)
instance Exception NloptResult
checkEC :: CInt -> IO NloptResult
checkEC n | n < 0 = throwIO e
| otherwise = return e
where e = fromCInt n :: NloptResult
nloptSrand :: Int -> IO ()
nloptSrand a1 =
let {a1' = fromIntegral a1} in
nloptSrand'_ a1' >>= \res ->
return ()
nloptSrandTime :: IO ()
nloptSrandTime =
nloptSrandTime'_ >>= \res ->
return ()
nloptVersion :: IO (Int, Int, Int)
nloptVersion =
alloca $ \a1' ->
alloca $ \a2' ->
alloca $ \a3' ->
nloptVersion'_ a1' a2' a3' >>= \res ->
peekInt a1'>>= \a1'' ->
peekInt a2'>>= \a2'' ->
peekInt a3'>>= \a3'' ->
return (a1'', a2'', a3'')
nloptCreate :: NloptAlgorithm -> Int -> IO (NLOpt)
nloptCreate a1 a2 =
let {a1' = toCInt a1} in
let {a2' = fromIntegral a2} in
nloptCreate'_ a1' a2' >>= \res ->
ptrToNLOpt res >>= \res' ->
return (res')
nloptCopy :: NLOpt -> IO (NLOpt)
nloptCopy a1 =
withNLOpt_ a1 $ \a1' ->
nloptCopy'_ a1' >>= \res ->
ptrToNLOpt res >>= \res' ->
return (res')
nloptDestroy :: Ptr () -> IO ()
nloptDestroy a1 =
let {a1' = id a1} in
nloptDestroy'_ a1' >>= \res ->
return ()
nloptOptimize :: NLOpt -> Vec -> IO (NloptResult, Double)
nloptOptimize a1 a2 =
withNLOpt_ a1 $ \a1' ->
vmUnsafeWith a2 $ \a2' ->
alloca $ \a3' ->
nloptOptimize'_ a1' a2' a3' >>= \res ->
peekDouble a3'>>= \a3'' ->
checkEC res >>= \res' ->
return (res', a3'')
nloptSetMinObjective :: NLOpt -> Func -> IO (NloptResult)
nloptSetMinObjective a1 a2 =
withNLOpt_ a1 $ \a1' ->
withFunc a2 $ \a2' ->
withNull $ \a3' ->
nloptSetMinObjective'_ a1' a2' a3' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptSetMaxObjective :: NLOpt -> Func -> IO (NloptResult)
nloptSetMaxObjective a1 a2 =
withNLOpt_ a1 $ \a1' ->
withFunc a2 $ \a2' ->
withNull $ \a3' ->
nloptSetMaxObjective'_ a1' a2' a3' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptSetPrecondMinObjective :: NLOpt -> Func -> Precond -> IO (NloptResult)
nloptSetPrecondMinObjective a1 a2 a3 =
withNLOpt_ a1 $ \a1' ->
withFunc a2 $ \a2' ->
withPrecond a3 $ \a3' ->
withNull $ \a4' ->
nloptSetPrecondMinObjective'_ a1' a2' a3' a4' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptSetPrecondMaxObjective :: NLOpt -> Func -> Precond -> IO (NloptResult)
nloptSetPrecondMaxObjective a1 a2 a3 =
withNLOpt_ a1 $ \a1' ->
withFunc a2 $ \a2' ->
withPrecond a3 $ \a3' ->
withNull $ \a4' ->
nloptSetPrecondMaxObjective'_ a1' a2' a3' a4' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptGetAlgorithm :: NLOpt -> IO (NloptAlgorithm)
nloptGetAlgorithm a1 =
withNLOpt_ a1 $ \a1' ->
nloptGetAlgorithm'_ a1' >>= \res ->
let {res' = fromCInt res} in
return (res')
nloptGetDimension :: NLOpt -> IO (Int)
nloptGetDimension a1 =
withNLOpt_ a1 $ \a1' ->
nloptGetDimension'_ a1' >>= \res ->
let {res' = fromIntegral res} in
return (res')
nloptSetLowerBounds :: NLOpt -> Vec -> IO (NloptResult)
nloptSetLowerBounds a1 a2 =
withNLOpt_ a1 $ \a1' ->
vmUnsafeWith a2 $ \a2' ->
nloptSetLowerBounds'_ a1' a2' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptSetUpperBounds :: NLOpt -> Vec -> IO (NloptResult)
nloptSetUpperBounds a1 a2 =
withNLOpt_ a1 $ \a1' ->
vmUnsafeWith a2 $ \a2' ->
nloptSetUpperBounds'_ a1' a2' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptGetLowerBounds :: NLOpt -> Vec -> IO (NloptResult)
nloptGetLowerBounds a1 a2 =
withNLOpt_ a1 $ \a1' ->
vmUnsafeWith a2 $ \a2' ->
nloptGetLowerBounds'_ a1' a2' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptGetUpperBounds :: NLOpt -> Vec -> IO (NloptResult)
nloptGetUpperBounds a1 a2 =
withNLOpt_ a1 $ \a1' ->
vmUnsafeWith a2 $ \a2' ->
nloptGetUpperBounds'_ a1' a2' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptSetLowerBounds1 :: NLOpt -> Double -> IO (NloptResult)
nloptSetLowerBounds1 a1 a2 =
withNLOpt_ a1 $ \a1' ->
let {a2' = realToFrac a2} in
nloptSetLowerBounds1'_ a1' a2' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptSetUpperBounds1 :: NLOpt -> Double -> IO (NloptResult)
nloptSetUpperBounds1 a1 a2 =
withNLOpt_ a1 $ \a1' ->
let {a2' = realToFrac a2} in
nloptSetUpperBounds1'_ a1' a2' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptRemoveInequalityConstraints :: NLOpt -> IO (NloptResult)
nloptRemoveInequalityConstraints a1 =
withNLOpt_ a1 $ \a1' ->
nloptRemoveInequalityConstraints'_ a1' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptAddInequalityConstraint :: NLOpt -> Func -> Double -> IO (NloptResult)
nloptAddInequalityConstraint a1 a2 a4 =
withNLOpt_ a1 $ \a1' ->
withFunc a2 $ \a2' ->
withNull $ \a3' ->
let {a4' = realToFrac a4} in
nloptAddInequalityConstraint'_ a1' a2' a3' a4' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptAddPrecondInequalityConstraint :: NLOpt -> Func -> Precond -> Double -> IO (NloptResult)
nloptAddPrecondInequalityConstraint a1 a2 a3 a5 =
withNLOpt_ a1 $ \a1' ->
withFunc a2 $ \a2' ->
withPrecond a3 $ \a3' ->
withNull $ \a4' ->
let {a5' = realToFrac a5} in
nloptAddPrecondInequalityConstraint'_ a1' a2' a3' a4' a5' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptAddInequalityMconstraint :: NLOpt -> Int -> MFunc -> Vec -> IO (NloptResult)
nloptAddInequalityMconstraint a1 a2 a3 a5 =
withNLOpt_ a1 $ \a1' ->
let {a2' = fromIntegral a2} in
withMFunc a3 $ \a3' ->
withNull $ \a4' ->
vmUnsafeWith a5 $ \a5' ->
nloptAddInequalityMconstraint'_ a1' a2' a3' a4' a5' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptRemoveEqualityConstraints :: NLOpt -> IO (NloptResult)
nloptRemoveEqualityConstraints a1 =
withNLOpt_ a1 $ \a1' ->
nloptRemoveEqualityConstraints'_ a1' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptAddEqualityConstraint :: NLOpt -> Func -> Double -> IO (NloptResult)
nloptAddEqualityConstraint a1 a2 a4 =
withNLOpt_ a1 $ \a1' ->
withFunc a2 $ \a2' ->
withNull $ \a3' ->
let {a4' = realToFrac a4} in
nloptAddEqualityConstraint'_ a1' a2' a3' a4' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptAddPrecondEqualityConstraint :: NLOpt -> Func -> Precond -> Double -> IO (NloptResult)
nloptAddPrecondEqualityConstraint a1 a2 a3 a5 =
withNLOpt_ a1 $ \a1' ->
withFunc a2 $ \a2' ->
withPrecond a3 $ \a3' ->
withNull $ \a4' ->
let {a5' = realToFrac a5} in
nloptAddPrecondEqualityConstraint'_ a1' a2' a3' a4' a5' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptAddEqualityMconstraint :: NLOpt -> Int -> MFunc -> Vec -> IO (NloptResult)
nloptAddEqualityMconstraint a1 a2 a3 a5 =
withNLOpt_ a1 $ \a1' ->
let {a2' = fromIntegral a2} in
withMFunc a3 $ \a3' ->
withNull $ \a4' ->
vmUnsafeWith a5 $ \a5' ->
nloptAddEqualityMconstraint'_ a1' a2' a3' a4' a5' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptSetStopval :: NLOpt -> Double -> IO (NloptResult)
nloptSetStopval a1 a2 =
withNLOpt_ a1 $ \a1' ->
let {a2' = realToFrac a2} in
nloptSetStopval'_ a1' a2' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptGetStopval :: NLOpt -> IO (Double)
nloptGetStopval a1 =
withNLOpt_ a1 $ \a1' ->
nloptGetStopval'_ a1' >>= \res ->
let {res' = realToFrac res} in
return (res')
nloptSetFtolRel :: NLOpt -> Double -> IO (NloptResult)
nloptSetFtolRel a1 a2 =
withNLOpt_ a1 $ \a1' ->
let {a2' = realToFrac a2} in
nloptSetFtolRel'_ a1' a2' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptGetFtolRel :: NLOpt -> IO (Double)
nloptGetFtolRel a1 =
withNLOpt_ a1 $ \a1' ->
nloptGetFtolRel'_ a1' >>= \res ->
let {res' = realToFrac res} in
return (res')
nloptSetFtolAbs :: NLOpt -> Double -> IO (NloptResult)
nloptSetFtolAbs a1 a2 =
withNLOpt_ a1 $ \a1' ->
let {a2' = realToFrac a2} in
nloptSetFtolAbs'_ a1' a2' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptGetFtolAbs :: NLOpt -> IO (Double)
nloptGetFtolAbs a1 =
withNLOpt_ a1 $ \a1' ->
nloptGetFtolAbs'_ a1' >>= \res ->
let {res' = realToFrac res} in
return (res')
nloptSetXtolRel :: NLOpt -> Double -> IO (NloptResult)
nloptSetXtolRel a1 a2 =
withNLOpt_ a1 $ \a1' ->
let {a2' = realToFrac a2} in
nloptSetXtolRel'_ a1' a2' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptGetXtolRel :: NLOpt -> IO (Double)
nloptGetXtolRel a1 =
withNLOpt_ a1 $ \a1' ->
nloptGetXtolRel'_ a1' >>= \res ->
let {res' = realToFrac res} in
return (res')
nloptSetXtolAbs1 :: NLOpt -> Double -> IO (NloptResult)
nloptSetXtolAbs1 a1 a2 =
withNLOpt_ a1 $ \a1' ->
let {a2' = realToFrac a2} in
nloptSetXtolAbs1'_ a1' a2' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptSetXtolAbs :: NLOpt -> Vec -> IO (NloptResult)
nloptSetXtolAbs a1 a2 =
withNLOpt_ a1 $ \a1' ->
vmUnsafeWith a2 $ \a2' ->
nloptSetXtolAbs'_ a1' a2' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptGetXtolAbs :: NLOpt -> Vec -> IO (NloptResult)
nloptGetXtolAbs a1 a2 =
withNLOpt_ a1 $ \a1' ->
vmUnsafeWith a2 $ \a2' ->
nloptGetXtolAbs'_ a1' a2' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptSetMaxeval :: NLOpt -> Int -> IO (NloptResult)
nloptSetMaxeval a1 a2 =
withNLOpt_ a1 $ \a1' ->
let {a2' = fromIntegral a2} in
nloptSetMaxeval'_ a1' a2' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptGetMaxeval :: NLOpt -> IO (Int)
nloptGetMaxeval a1 =
withNLOpt_ a1 $ \a1' ->
nloptGetMaxeval'_ a1' >>= \res ->
let {res' = fromIntegral res} in
return (res')
nloptSetMaxtime :: NLOpt -> Double -> IO (NloptResult)
nloptSetMaxtime a1 a2 =
withNLOpt_ a1 $ \a1' ->
let {a2' = realToFrac a2} in
nloptSetMaxtime'_ a1' a2' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptGetMaxtime :: NLOpt -> IO (Double)
nloptGetMaxtime a1 =
withNLOpt_ a1 $ \a1' ->
nloptGetMaxtime'_ a1' >>= \res ->
let {res' = realToFrac res} in
return (res')
nloptForceStop :: NLOpt -> IO (NloptResult)
nloptForceStop a1 =
withNLOpt_ a1 $ \a1' ->
nloptForceStop'_ a1' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptSetForceStop :: NLOpt -> Int -> IO (NloptResult)
nloptSetForceStop a1 a2 =
withNLOpt_ a1 $ \a1' ->
let {a2' = fromIntegral a2} in
nloptSetForceStop'_ a1' a2' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptGetForceStop :: NLOpt -> IO (Int)
nloptGetForceStop a1 =
withNLOpt_ a1 $ \a1' ->
nloptGetForceStop'_ a1' >>= \res ->
let {res' = fromIntegral res} in
return (res')
nloptSetLocalOptimizer :: NLOpt -> NLOpt -> IO (NloptResult)
nloptSetLocalOptimizer a1 a2 =
withNLOpt_ a1 $ \a1' ->
withNLOpt_ a2 $ \a2' ->
nloptSetLocalOptimizer'_ a1' a2' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptSetPopulation :: NLOpt -> Int -> IO (NloptResult)
nloptSetPopulation a1 a2 =
withNLOpt_ a1 $ \a1' ->
let {a2' = fromIntegral a2} in
nloptSetPopulation'_ a1' a2' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptGetPopulation :: NLOpt -> IO (Int)
nloptGetPopulation a1 =
withNLOpt_ a1 $ \a1' ->
nloptGetPopulation'_ a1' >>= \res ->
let {res' = fromIntegral res} in
return (res')
nloptSetVectorStorage :: NLOpt -> Int -> IO (NloptResult)
nloptSetVectorStorage a1 a2 =
withNLOpt_ a1 $ \a1' ->
let {a2' = fromIntegral a2} in
nloptSetVectorStorage'_ a1' a2' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptGetVectorStorage :: NLOpt -> IO (Int)
nloptGetVectorStorage a1 =
withNLOpt_ a1 $ \a1' ->
nloptGetVectorStorage'_ a1' >>= \res ->
let {res' = fromIntegral res} in
return (res')
nloptSetInitialStep :: NLOpt -> Vec -> IO (NloptResult)
nloptSetInitialStep a1 a2 =
withNLOpt_ a1 $ \a1' ->
vmUnsafeWith a2 $ \a2' ->
nloptSetInitialStep'_ a1' a2' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptGetInitialStep :: NLOpt -> Vec -> Vec -> IO (NloptResult)
nloptGetInitialStep a1 a2 a3 =
withNLOpt_ a1 $ \a1' ->
vmUnsafeWith a2 $ \a2' ->
vmUnsafeWith a3 $ \a3' ->
nloptGetInitialStep'_ a1' a2' a3' >>= \res ->
checkEC res >>= \res' ->
return (res')
nloptSetInitialStep1 :: NLOpt -> Double -> IO (NloptResult)
nloptSetInitialStep1 a1 a2 =
withNLOpt_ a1 $ \a1' ->
let {a2' = realToFrac a2} in
nloptSetInitialStep1'_ a1' a2' >>= \res ->
checkEC res >>= \res' ->
return (res')
withNLOpt_ x f = withNLOpt x (f . castPtr)
vmUnsafeWith :: VM.IOVector Double -> (Ptr CDouble -> IO b) -> IO b
vmUnsafeWith v f = VM.unsafeWith v (f . castPtr)
ptrToVS :: Integral n => n -> Ptr CDouble -> IO Vec
ptrToVS n p = do
fp <- newForeignPtr_ (castPtr p)
return (VM.unsafeFromForeignPtr0 fp (fromIntegral n))
ptrToV :: (VG.Vector v Double, Integral n) => n -> Ptr CDouble -> IO (v Double)
ptrToV n p = fmap V.convert $ VS.unsafeFreeze =<< ptrToVS n p
copyInto _ p _ | p == nullPtr = return ()
copyInto n dest src = do
to <- ptrToVS n dest
VM.copy to =<< VS.unsafeThaw (V.convert src)
type Vec = VM.IOVector Double
toCInt x = fromIntegral (fromEnum x)
fromCInt x = toEnum (fromIntegral x)
peekInt ptr = fmap fromIntegral (peek ptr)
ptrToNLOpt :: Ptr () -> IO NLOpt
ptrToNLOpt p = do
fp <- newForeignPtr nloptDestroyFP p
return (NLOpt (castForeignPtr fp))
foreign import ccall "wrapper" mkNloptFinalizer :: (Ptr () -> IO ()) -> IO (FunPtr (Ptr () -> IO ()))
foreign import ccall unsafe "& nlopt_destroy" nloptDestroyFP :: FunPtr (Ptr () -> IO ())
foreign import ccall "wrapper" mkFunc :: Func -> IO (FunPtr Func)
foreign import ccall "wrapper" mkPrecond :: Precond -> IO (FunPtr Precond)
foreign import ccall "wrapper" mkMFunc :: MFunc -> IO (FunPtr MFunc)
withFunc :: Func -> (FunPtr Func -> IO b) -> IO b
withFunc f g = do
f' <- mkFunc f
r <- g f'
return r
withPrecond :: Precond -> (FunPtr Precond -> IO b) -> IO b
withPrecond f g = do
f' <- mkPrecond f
r <- g f'
return r
withMFunc :: MFunc -> (FunPtr MFunc -> IO b) -> IO b
withMFunc f g = do
f' <- mkMFunc f
r <- g f'
return r
withNull f = f nullPtr
peekDouble :: Ptr CDouble -> IO Double
peekDouble p = peek (castPtr p)
mXv :: Num a => V.Vector (V.Vector a) -> V.Vector a -> V.Vector a
mXv m v = V.map (V.sum . V.zipWith (*) v) m
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_srand"
nloptSrand'_ :: (CULong -> (IO ()))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_srand_time"
nloptSrandTime'_ :: (IO ())
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_version"
nloptVersion'_ :: ((Ptr CInt) -> ((Ptr CInt) -> ((Ptr CInt) -> (IO ()))))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_create"
nloptCreate'_ :: (CInt -> (CUInt -> (IO (Ptr ()))))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_copy"
nloptCopy'_ :: ((Ptr ()) -> (IO (Ptr ())))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_destroy"
nloptDestroy'_ :: ((Ptr ()) -> (IO ()))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_optimize"
nloptOptimize'_ :: ((Ptr ()) -> ((Ptr CDouble) -> ((Ptr CDouble) -> (IO CInt))))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_min_objective"
nloptSetMinObjective'_ :: ((Ptr ()) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO CDouble)))))) -> ((Ptr ()) -> (IO CInt))))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_max_objective"
nloptSetMaxObjective'_ :: ((Ptr ()) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO CDouble)))))) -> ((Ptr ()) -> (IO CInt))))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_precond_min_objective"
nloptSetPrecondMinObjective'_ :: ((Ptr ()) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO CDouble)))))) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO ()))))))) -> ((Ptr ()) -> (IO CInt)))))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_precond_max_objective"
nloptSetPrecondMaxObjective'_ :: ((Ptr ()) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO CDouble)))))) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO ()))))))) -> ((Ptr ()) -> (IO CInt)))))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_algorithm"
nloptGetAlgorithm'_ :: ((Ptr ()) -> (IO CInt))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_dimension"
nloptGetDimension'_ :: ((Ptr ()) -> (IO CUInt))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_lower_bounds"
nloptSetLowerBounds'_ :: ((Ptr ()) -> ((Ptr CDouble) -> (IO CInt)))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_upper_bounds"
nloptSetUpperBounds'_ :: ((Ptr ()) -> ((Ptr CDouble) -> (IO CInt)))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_lower_bounds"
nloptGetLowerBounds'_ :: ((Ptr ()) -> ((Ptr CDouble) -> (IO CInt)))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_upper_bounds"
nloptGetUpperBounds'_ :: ((Ptr ()) -> ((Ptr CDouble) -> (IO CInt)))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_lower_bounds1"
nloptSetLowerBounds1'_ :: ((Ptr ()) -> (CDouble -> (IO CInt)))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_upper_bounds1"
nloptSetUpperBounds1'_ :: ((Ptr ()) -> (CDouble -> (IO CInt)))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_remove_inequality_constraints"
nloptRemoveInequalityConstraints'_ :: ((Ptr ()) -> (IO CInt))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_add_inequality_constraint"
nloptAddInequalityConstraint'_ :: ((Ptr ()) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO CDouble)))))) -> ((Ptr ()) -> (CDouble -> (IO CInt)))))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_add_precond_inequality_constraint"
nloptAddPrecondInequalityConstraint'_ :: ((Ptr ()) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO CDouble)))))) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO ()))))))) -> ((Ptr ()) -> (CDouble -> (IO CInt))))))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_add_inequality_mconstraint"
nloptAddInequalityMconstraint'_ :: ((Ptr ()) -> (CUInt -> ((FunPtr (CUInt -> ((Ptr CDouble) -> (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO ())))))))) -> ((Ptr ()) -> ((Ptr CDouble) -> (IO CInt))))))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_remove_equality_constraints"
nloptRemoveEqualityConstraints'_ :: ((Ptr ()) -> (IO CInt))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_add_equality_constraint"
nloptAddEqualityConstraint'_ :: ((Ptr ()) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO CDouble)))))) -> ((Ptr ()) -> (CDouble -> (IO CInt)))))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_add_precond_equality_constraint"
nloptAddPrecondEqualityConstraint'_ :: ((Ptr ()) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO CDouble)))))) -> ((FunPtr (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO ()))))))) -> ((Ptr ()) -> (CDouble -> (IO CInt))))))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_add_equality_mconstraint"
nloptAddEqualityMconstraint'_ :: ((Ptr ()) -> (CUInt -> ((FunPtr (CUInt -> ((Ptr CDouble) -> (CUInt -> ((Ptr CDouble) -> ((Ptr CDouble) -> ((Ptr ()) -> (IO ())))))))) -> ((Ptr ()) -> ((Ptr CDouble) -> (IO CInt))))))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_stopval"
nloptSetStopval'_ :: ((Ptr ()) -> (CDouble -> (IO CInt)))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_stopval"
nloptGetStopval'_ :: ((Ptr ()) -> (IO CDouble))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_ftol_rel"
nloptSetFtolRel'_ :: ((Ptr ()) -> (CDouble -> (IO CInt)))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_ftol_rel"
nloptGetFtolRel'_ :: ((Ptr ()) -> (IO CDouble))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_ftol_abs"
nloptSetFtolAbs'_ :: ((Ptr ()) -> (CDouble -> (IO CInt)))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_ftol_abs"
nloptGetFtolAbs'_ :: ((Ptr ()) -> (IO CDouble))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_xtol_rel"
nloptSetXtolRel'_ :: ((Ptr ()) -> (CDouble -> (IO CInt)))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_xtol_rel"
nloptGetXtolRel'_ :: ((Ptr ()) -> (IO CDouble))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_xtol_abs1"
nloptSetXtolAbs1'_ :: ((Ptr ()) -> (CDouble -> (IO CInt)))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_xtol_abs"
nloptSetXtolAbs'_ :: ((Ptr ()) -> ((Ptr CDouble) -> (IO CInt)))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_xtol_abs"
nloptGetXtolAbs'_ :: ((Ptr ()) -> ((Ptr CDouble) -> (IO CInt)))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_maxeval"
nloptSetMaxeval'_ :: ((Ptr ()) -> (CInt -> (IO CInt)))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_maxeval"
nloptGetMaxeval'_ :: ((Ptr ()) -> (IO CInt))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_maxtime"
nloptSetMaxtime'_ :: ((Ptr ()) -> (CDouble -> (IO CInt)))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_maxtime"
nloptGetMaxtime'_ :: ((Ptr ()) -> (IO CDouble))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_force_stop"
nloptForceStop'_ :: ((Ptr ()) -> (IO CInt))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_force_stop"
nloptSetForceStop'_ :: ((Ptr ()) -> (CInt -> (IO CInt)))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_force_stop"
nloptGetForceStop'_ :: ((Ptr ()) -> (IO CInt))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_local_optimizer"
nloptSetLocalOptimizer'_ :: ((Ptr ()) -> ((Ptr ()) -> (IO CInt)))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_population"
nloptSetPopulation'_ :: ((Ptr ()) -> (CUInt -> (IO CInt)))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_population"
nloptGetPopulation'_ :: ((Ptr ()) -> (IO CUInt))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_vector_storage"
nloptSetVectorStorage'_ :: ((Ptr ()) -> (CUInt -> (IO CInt)))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_vector_storage"
nloptGetVectorStorage'_ :: ((Ptr ()) -> (IO CUInt))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_initial_step"
nloptSetInitialStep'_ :: ((Ptr ()) -> ((Ptr CDouble) -> (IO CInt)))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_get_initial_step"
nloptGetInitialStep'_ :: ((Ptr ()) -> ((Ptr CDouble) -> ((Ptr CDouble) -> (IO CInt))))
foreign import ccall safe "Nlopt/Raw.chs.h nlopt_set_initial_step1"
nloptSetInitialStep1'_ :: ((Ptr ()) -> (CDouble -> (IO CInt)))