Mercurial > hg > jslab
comparison src/samer/maths/opt/CubicLineSearch.java @ 0:bf79fb79ee13
Initial Mercurial check in.
author | samer |
---|---|
date | Tue, 17 Jan 2012 17:50:20 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:bf79fb79ee13 |
---|---|
1 /* | |
2 * Copyright (c) 2000, Samer Abdallah, King's College London. | |
3 * All rights reserved. | |
4 * | |
5 * This software is provided AS iS and WITHOUT ANY WARRANTY; | |
6 * without even the implied warranty of MERCHANTABILITY or | |
7 * FITNESS FOR A PARTICULAR PURPOSE. | |
8 */ | |
9 | |
10 package samer.maths.opt; | |
11 import samer.maths.*; | |
12 import samer.core.*; | |
13 | |
14 | |
15 public class CubicLineSearch | |
16 { | |
17 double ZETA=0.0625; // factor for safeguarding | |
18 State S; | |
19 | |
20 public CubicLineSearch(State s) { S=s; } | |
21 | |
22 public void setSafeguardFactor( double z) { ZETA=z; } | |
23 | |
24 protected final double extrapolate() | |
25 { | |
26 double beta=S.cubic()-S.alpha; | |
27 | |
28 if (Double.isInfinite(beta) || Double.isNaN(beta)) { | |
29 return S.alpha*1.6; | |
30 } else if (beta>0) { | |
31 if (beta<0.5*S.alpha) beta=S.alpha*0.5; | |
32 else if (beta>2*S.alpha) beta=2*S.alpha; | |
33 return beta; | |
34 } else { | |
35 return S.alpha*1.6; | |
36 // if (beta>8) beta=8; | |
37 } | |
38 } | |
39 | |
40 protected final double interpolate() | |
41 { | |
42 if (S.P2.s>0) { | |
43 double beta=S.cubic(); | |
44 double safe=ZETA*S.alpha; | |
45 if (Double.isNaN(beta) || Double.isInfinite(beta)) { | |
46 beta=S.alpha/2; // bisect | |
47 } else if (beta<=safe || beta>=(S.alpha-safe)) { | |
48 beta=S.alpha/2; // bisect | |
49 } | |
50 if (S.alpha<0.1) beta/=2; // adust | |
51 return beta; | |
52 } else { | |
53 return S.alpha/2; | |
54 } | |
55 } | |
56 | |
57 public void run(Condition convergence) | |
58 { | |
59 while (!convergence.test()) { | |
60 if (S.P2.f<S.P1.f && S.P2.s<0) { | |
61 double beta=extrapolate(); | |
62 S.move(); | |
63 S.step(beta); | |
64 } else | |
65 S.step(interpolate()); | |
66 } | |
67 } | |
68 } | |
69 |