Permalink
Browse files

moved golden section search and bfgs/dfp into Optimization module

  • Loading branch information...
1 parent c763e75 commit e899e8648b553e5b0adbb55ee59be71ce8b514d3 @ghorn committed Aug 18, 2011
Showing with 77 additions and 45 deletions.
  1. +10 −0 Optimization.hs
  2. +26 −0 Optimization/LineSearch.hs
  3. +3 −43 preliminaryOptimization.hs → Optimization/QuasiNewton.hs
  4. +7 −2 hopt-o-mex.cabal
  5. +31 −0 optTest.hs
View
@@ -0,0 +1,10 @@
+-- Optimization.hs
+
+module Optimization
+ (
+ module Optimization.LineSearch
+ , module Optimization.QuasiNewton
+ ) where
+
+import Optimization.LineSearch
+import Optimization.QuasiNewton
View
@@ -0,0 +1,26 @@
+-- LineSearch.hs
+
+{-# OPTIONS_GHC -Wall #-}
+
+module Optimization.LineSearch
+ (
+ goldenSectionSearch
+ ) where
+
+tau :: Floating a => a
+tau = 2/(1+sqrt(5))
+
+goldenSectionSearch :: (Ord a, Floating b) => (b -> a) -> (b, b) -> [(b,a)]
+goldenSectionSearch f (x0,x3) = gss f (x0,x1,x2,x3)
+ where
+ x1 = x0 + (x3-x0)*(1-tau)
+ x2 = x0 + (x3-x0)*tau
+
+gss :: (Ord a, Floating b) => (b -> a) -> (b, b, b, b) -> [(b,a)]
+gss f (x0, x1, x2, x3) = xs
+ where
+ x1' = x0 + (x2-x0)*(1-tau)
+ x2' = x1 + (x3-x1)*tau
+ xs
+ | (f x1) < (f x2) = (x1, f x1):(gss f (x0,x1',x1,x2))
+ | otherwise = (x2, f x2):(gss f (x1,x2,x2',x3))
@@ -1,36 +1,13 @@
--- preliminaryOptimization.hs
+-- QuasiNewton.hs
{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -Wall #-}
-module Main where
+module Optimization.QuasiNewton(dfp, bfgs) where
-import Casadi.Utils
+import Optimization.LineSearch
import Numeric.LinearAlgebra
-import Graphics.Gnuplot.Simple
-
-rosenbrock :: Floating a => [a] -> a
-rosenbrock [x0,x1] = (1-x0)*(1-x0) + 100*(x1-x0*x0)*(x1-x0*x0)
-
-tau :: Floating a => a
-tau = 2/(1+sqrt(5))
-
-goldenSectionSearch :: (Ord a, Floating b) => (b -> a) -> (b, b) -> [(b,a)]
-goldenSectionSearch f (x0,x3) = gss f (x0,x1,x2,x3)
- where
- x1 = x0 + (x3-x0)*(1-tau)
- x2 = x0 + (x3-x0)*tau
-
-gss :: (Ord a, Floating b) => (b -> a) -> (b, b, b, b) -> [(b,a)]
-gss f (x0, x1, x2, x3) = xs
- where
- x1' = x0 + (x2-x0)*(1-tau)
- x2' = x1 + (x3-x1)*tau
- xs
- | (f x1) < (f x2) = (x1, f x1):(gss f (x0,x1',x1,x2))
- | otherwise = (x2, f x2):(gss f (x1,x2,x2',x3))
-
dfp :: (Product e, Container Vector e, Ord t, Num (Vector e), Floating e) =>
Vector e -> (Vector e -> t) -> (Vector e -> Vector e) -> [(Vector e, Matrix e)]
dfp x0 f g = iterate (\(x,v) -> oneDfp x v f g) (x0, v0)
@@ -79,20 +56,3 @@ oneBfgs xk vk f g = (xkp1, vkp1)
f' alpha = f $ xk + (scale alpha pk)
(alphakp1, _) = head $ drop 200 $ goldenSectionSearch f' (0,10)
xkp1 = xk + (scale alphakp1 pk)
-
-
-main :: IO ()
-main = do
- (rosenbrockF, rosenbrockG, _) <- getDerivs rosenbrock 2
- let x0 = fromList [-0.9,1] :: Vector Double
--- results = (take 20 $ dfp x0 rosenbrockF rosenbrockG)
- results = (take 20 $ bfgs x0 rosenbrockF rosenbrockG)
- path = map f results
- where
- f (vec,_) = (x,y)
- where
- [x,y] = toList vec
- mapM_ (\(_,v) -> print $ inv v) results
- mapM_ (\(x,_) -> print x) results
-
- plotList [] path
View
@@ -19,9 +19,13 @@ Library
Casadi.SXMatrix
Casadi.SXFunction
Casadi.Utils
+
+ Optimization
+ Optimization.LineSearch
+ Optimization.QuasiNewton
Other-modules:
- Casadi.CasadiInterfaceUtils
+ Casadi.CasadiInterfaceUtils
Build-depends: base, hmatrix
@@ -38,6 +42,7 @@ Library
Ghc-options: -pgml g++ -fPIC -O2 -dno-debug-output
--HS_PROFILE_FLAGS = -prof -auto-all -caf-all -fforce-recomp -rtsopts
+
Executable speedTest
Main-Is: speedTest.hs
Hs-Source-Dirs: .
@@ -79,7 +84,7 @@ Executable cartpoleDdp
Executable optTest
Main-Is: preliminaryOptimization.hs
Hs-Source-Dirs: .
- Other-Modules: Casadi
+ Other-Modules: Casadi, Optimization
Build-depends: hmatrix, gnuplot
Ghc-options: -pgml g++ -dno-debug-output -fPIC -O2
View
@@ -0,0 +1,31 @@
+-- preliminaryOptimization.hs
+
+{-# OPTIONS_GHC -Wall #-}
+
+module Main where
+
+import Casadi.Utils
+import Optimization.QuasiNewton
+import Numeric.LinearAlgebra
+
+import Graphics.Gnuplot.Simple
+
+rosenbrock :: Floating a => [a] -> a
+rosenbrock [x0,x1] = (1-x0)*(1-x0) + 100*(x1-x0*x0)*(x1-x0*x0)
+rosenbrock _ = error "wrong number of inputs to rosenbrock"
+
+main :: IO ()
+main = do
+ (rosenbrockF, rosenbrockG, _) <- getDerivs rosenbrock 2
+ let x0 = fromList [-0.9,1] :: Vector Double
+-- results = (take 20 $ dfp x0 rosenbrockF rosenbrockG)
+ results = (take 20 $ bfgs x0 rosenbrockF rosenbrockG)
+ path = map f results
+ where
+ f (vec,_) = (x,y)
+ where
+ [x,y] = toList vec
+ mapM_ (\(_,v) -> print $ inv v) results
+ mapM_ (\(x,_) -> print x) results
+
+ plotList [] path

0 comments on commit e899e86

Please sign in to comment.