diff options
Diffstat (limited to 'examples/minimize.hs')
-rw-r--r-- | examples/minimize.hs | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/examples/minimize.hs b/examples/minimize.hs index 5beba3b..8962006 100644 --- a/examples/minimize.hs +++ b/examples/minimize.hs | |||
@@ -13,6 +13,9 @@ df [x,y] = [20*(x-1), 40*(y-2)] | |||
13 | -- the conjugate gradient method | 13 | -- the conjugate gradient method |
14 | minimizeCG = minimizeConjugateGradient 1E-2 1E-4 1E-3 30 | 14 | minimizeCG = minimizeConjugateGradient 1E-2 1E-4 1E-3 30 |
15 | 15 | ||
16 | -- the BFGS2 method | ||
17 | minimizeBFGS2 = minimizeVectorBFGS2 1E-2 1E-2 1E-3 30 | ||
18 | |||
16 | -- a minimization algorithm which does not require the gradient | 19 | -- a minimization algorithm which does not require the gradient |
17 | minimizeS f xi = minimizeNMSimplex f xi (replicate (length xi) 1) 1E-2 100 | 20 | minimizeS f xi = minimizeNMSimplex f xi (replicate (length xi) 1) 1E-2 100 |
18 | 21 | ||
@@ -24,20 +27,27 @@ partialDerivative n f v = fst (derivCentral 0.01 g (v!!n)) where | |||
24 | (a,_:b) = splitAt n v | 27 | (a,_:b) = splitAt n v |
25 | 28 | ||
26 | main = do | 29 | main = do |
27 | -- conjugate gradient with true gradient | 30 | putStrLn "BFGS2 with true gradient" |
28 | let (s,p) = minimizeCG f df [5,7] | 31 | let (s,p) = minimizeBFGS2 f df [5,7] |
29 | print s -- solution | 32 | print s -- solution |
30 | disp p -- evolution of the algorithm | 33 | disp p -- evolution of the algorithm |
31 | let [x,y] = drop 2 (toColumns p) | 34 | let [x,y] = drop 2 (toColumns p) |
32 | mplot [x,y] -- path from the starting point to the solution | 35 | mplot [x,y] -- path from the starting point to the solution |
33 | 36 | ||
34 | -- conjugate gradient with estimated gradient | 37 | putStrLn "conjugate gradient with true gradient" |
38 | let (s,p) = minimizeCG f df [5,7] | ||
39 | print s | ||
40 | disp p | ||
41 | let [x,y] = drop 2 (toColumns p) | ||
42 | mplot [x,y] | ||
43 | |||
44 | putStrLn "conjugate gradient with estimated gradient" | ||
35 | let (s,p) = minimizeCG f (gradient f) [5,7] | 45 | let (s,p) = minimizeCG f (gradient f) [5,7] |
36 | print s | 46 | print s |
37 | disp p | 47 | disp p |
38 | mplot $ drop 2 (toColumns p) | 48 | mplot $ drop 2 (toColumns p) |
39 | 49 | ||
40 | -- without gradient, using the NM Simplex method | 50 | putStrLn "without gradient, using the NM Simplex method" |
41 | let (s,p) = minimizeS f [5,7] | 51 | let (s,p) = minimizeS f [5,7] |
42 | print s | 52 | print s |
43 | disp p | 53 | disp p |