1
+ # -*- coding: utf-8 -*-
1
2
"""
2
3
PyMSWolfe: Jorge Nocedal's modified More and Thuente linesearch
3
4
guaranteeing satisfaction of the strong Wolfe conditions.
6
7
import numpy as np
7
8
from nlp .ls ._modified_strong_wolfe_linesearch import mcsrch
8
9
10
+
9
11
class StrongWolfeLineSearch :
10
12
"""
11
13
A general-purpose linesearch procedure enforcing the strong
@@ -54,6 +56,7 @@ class StrongWolfeLineSearch:
54
56
computed satisfies the Armijo condition and SWLS.curvature will
55
57
be set to True if the step satisfies the curvature condition.
56
58
"""
59
+
57
60
def __init__ (self , f , x , g , d , obj , grad , ** kwargs ):
58
61
59
62
# Mandatory arguments
@@ -63,14 +66,14 @@ def __init__(self, f, x, g, d, obj, grad, **kwargs):
63
66
self .d = d # Direction along which to search
64
67
self .n = self .g .shape [0 ]
65
68
66
- self .obj = obj # To evaluate function value
69
+ self .obj = obj # To evaluate function value
67
70
self .grad = grad # To evaluate function gradient
68
71
69
72
# Optional arguments
70
- self .ftol = kwargs .get ('ftol' , 1.0e-4 )
71
- self .gtol = kwargs .get ('gtol' , 0.9 )
72
- self .xtol = kwargs .get ('xtol' , 1.0e-16 )
73
- self .stp = kwargs .get ('stp' , 1.0 )
73
+ self .ftol = kwargs .get ('ftol' , 1.0e-4 )
74
+ self .gtol = kwargs .get ('gtol' , 0.9 )
75
+ self .xtol = kwargs .get ('xtol' , 1.0e-16 )
76
+ self .stp = kwargs .get ('stp' , 1.0 )
74
77
self .stpmin = kwargs .get ('stpmin' , 1.0e-20 )
75
78
self .stpmax = kwargs .get ('stpmax' , 1.0e+20 )
76
79
self .maxfev = kwargs .get ('maxfev' , 20 )
@@ -86,19 +89,19 @@ def search(self):
86
89
isave = np .empty (2 , dtype = np .int32 )
87
90
dsave = np .empty (21 , dtype = np .double )
88
91
self .x , self .f , self .g , self .stp , self .info , self .nfev , self .wa \
89
- = mcsrch (self .n , self .x , self .f , self .g , self .d , self .stp ,
90
- self .ftol , self .gtol , self .xtol , self .stpmin ,
91
- self .stpmax , self .maxfev , self .info , self .nfev ,
92
- isave , dsave , self .wa )
92
+ = mcsrch (self .n , self .x , self .f , self .g , self .d , self .stp ,
93
+ self .ftol , self .gtol , self .xtol , self .stpmin ,
94
+ self .stpmax , self .maxfev , self .info , self .nfev ,
95
+ isave , dsave , self .wa )
93
96
94
97
while self .info == - 1 :
95
98
self .f = self .obj (self .x )
96
99
self .g = self .grad (self .x )
97
100
self .x , self .f , self .g , self .stp , self .info , self .nfev , self .wa \
98
- = mcsrch (self .n , self .x , self .f , self .g , self .d , self .stp ,
99
- self .ftol , self .gtol , self .xtol , self .stpmin ,
100
- self .stpmax , self .maxfev , self .info , self .nfev ,
101
- isave , dsave , self .wa )
101
+ = mcsrch (self .n , self .x , self .f , self .g , self .d , self .stp ,
102
+ self .ftol , self .gtol , self.xtol , self .stpmin ,
103
+ self .stpmax , self .maxfev , self .info , self .nfev ,
104
+ isave , dsave , self .wa )
102
105
103
106
if self .info == 1 : # Strong Wolfe conditions satisfied
104
107
self .armijo = True
@@ -128,12 +131,12 @@ def search(self):
128
131
g = model .grad (model .x0 )
129
132
d = - g
130
133
SWLS = StrongWolfeLineSearch (f ,
131
- model .x0 ,
132
- g ,
133
- d ,
134
- lambda z : model .obj (z ),
135
- lambda z : model .grad (z ),
136
- stp = 1.0 / sqrt (np .dot (g ,g )))
134
+ model .x0 ,
135
+ g ,
136
+ d ,
137
+ lambda z : model .obj (z ),
138
+ lambda z : model .grad (z ),
139
+ <
8000
span class="pl-s1">stp= 1.0 / sqrt (np .dot (g , g )))
137
140
print ' Before search'
138
141
print ' f = ' , f
139
142
print ' stpmax = ' , SWLS .stpmax
0 commit comments