Skip to content

Commit 8ffe70c

Browse files
committed
0.5.10 release; improvements to line search
1 parent aaec854 commit 8ffe70c

3 files changed

Lines changed: 37 additions & 37 deletions

File tree

flexsolve/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@
3535
from .problem import *
3636
from .profiler import *
3737

38-
__version__ = '0.5.9'
38+
__version__ = '0.5.10'

flexsolve/line_search.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,77 +3,77 @@
33
Line search utilities for simultaneous correction methods.
44
55
"""
6-
from scipy.optimize import fminbound
6+
import numpy as np
7+
from typing import NamedTuple
78

8-
__all__ = ('exact_line_search', 'inexact_line_search')
9+
__all__ = ('inexact_line_search', 'LineSearchResult')
910

10-
def exact_line_search(f, x, correction, t0=1e-9, t1=2, ttol=1e-6, maxiter=20,
11-
full_output=True):
12-
return fminbound(
13-
lambda t: f(x + t * correction),
14-
x1=t0,
15-
x2=t1,
16-
xtol=ttol,
17-
maxfun=maxiter,
18-
full_output=full_output,
19-
)
11+
12+
class LineSearchResult(NamedTuple):
13+
t: float
14+
x: np.ndarray
15+
f: float
16+
17+
18+
# def exact_line_search(f, x, correction, t0=1e-9, t1=2, ttol=1e-6, maxiter=20,
19+
# full_output=True):
20+
# return fminbound(
21+
# lambda t: f(x + t * correction),
22+
# x1=t0,
23+
# x2=t1,
24+
# xtol=ttol,
25+
# maxfun=maxiter,
26+
# full_output=full_output,
27+
# )
2028

2129
def inexact_line_search(
22-
f, x, correction, t0=1e-6, t1=2, ttol=1e-3, maxiter=100,
30+
f, x, correction, t0=1e-6, t1=1.1, ttol=1e-3, maxiter=10,
2331
fx=None, tguess=None, rho=0.618,
2432
):
2533
# Find t such that f(x + t * correction) - fx < 0
26-
if tguess is None or not t0 < tguess < t1:
27-
tguess = 0.5 * (t1 + t0)
28-
fx = None
29-
if fx is None:
30-
fx = f(x)
34+
if tguess is None or not t0 < tguess < t1: tguess = 0.5 * (t1 + t0)
35+
if fx is None: fx = f(x)
36+
x0 = x + t0 * correction
37+
ft0 = f(x0)
3138
x1 = x + t1 * correction
3239
ft1 = f(x1)
3340
if ft1 > fx:
3441
xguess = x + tguess * correction
3542
ftguess = f(xguess)
36-
if ftguess > fx:
43+
if ft1 > ft0:
44+
best = LineSearchResult(t0, x0, ft0)
3745
for i in range(maxiter):
3846
t = rho * tguess + (1 - rho) * t0
3947
xt = x + t * correction
4048
ft = f(xt)
41-
if ft < fx: return t, xt, ft
49+
if ft < best.f: best = LineSearchResult(t, xt, ft)
4250
if abs(t - tguess) < ttol: break
4351
tguess = t
44-
x0 = x + t0 * correction
45-
ft0 = f(x0)
46-
if ft0 >= fx:
47-
raise ValueError(
48-
'line search could not find improvement over reference point'
49-
)
50-
return t0, x0, ft0
5152
else:
53+
best = LineSearchResult(tguess, xguess, ftguess)
5254
for i in range(maxiter):
5355
t = (1 - rho) * tguess + rho * t1
5456
xt = x + t * correction
5557
ft = f(xt)
56-
if ft < ftguess: return t, xt, ft
58+
if ft < ftguess: best = LineSearchResult(t, xt, ft)
5759
if abs(t - tguess) < ttol: break
5860
t1 = t
59-
xguess = x + tguess * correction
60-
ftguess = f(xguess)
61-
return tguess, xguess, ftguess
6261
else:
6362
xguess = x + tguess * correction
6463
ftguess = f(xguess)
6564
if ftguess < ft1:
6665
tnext = tguess
66+
best = LineSearchResult(tguess, xguess, ftguess)
6767
for i in range(maxiter):
6868
t = (1 - rho) * tnext + rho * t1
6969
xt = x + t * correction
7070
ft = f(xt)
71-
if ft < ftguess: return t, xt, ft
71+
if ft < best.f: best = LineSearchResult(t, xt, ft)
7272
if abs(t - tguess) < ttol: break
73-
tnext = t
74-
return tguess, xguess, ftguess
73+
t1 = t
7574
else:
76-
return t1, x1, ft1
75+
best = LineSearchResult(t1, x1, ft1)
76+
return best
7777

7878

7979

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
name='flexsolve',
1111
packages=['flexsolve'],
1212
license='MIT',
13-
version='0.5.9',
13+
version='0.5.10',
1414
description='Flexible function solvers',
1515
long_description=open('README.rst').read(),
1616
author='Yoel Cortes-Pena',

0 commit comments

Comments
 (0)