Commit 6cb4d388 authored by Martin Reinecke's avatar Martin Reinecke
Browse files

make code more readable

parent cfed2bad
Pipeline #14530 passed with stage
in 6 minutes and 35 seconds
...@@ -104,21 +104,21 @@ class InformationStore(object): ...@@ -104,21 +104,21 @@ class InformationStore(object):
max_history_length : integer max_history_length : integer
Maximum number of stored past updates. Maximum number of stored past updates.
s : List s : List
List of past position differences, which are Fields. Circular buffer of past position differences, which are Fields.
y : List y : List
List of past gradient differences, which are Fields. Circular buffer of past gradient differences, which are Fields.
last_x : Field last_x : Field
Latest position in variable space. Latest position in variable space.
last_gradient : Field last_gradient : Field
Gradient at latest position. Gradient at latest position.
k : integer k : integer
Number of updates that have taken place Number of updates that have taken place
_ss_store : dictionary _ss_store : numpy.ndarray
Dictionary of scalar products between different elements of s. 2D circular buffer of scalar products between different elements of s.
_sy_store : dictionary _sy_store : numpy.ndarray
Dictionary of scalar products between elements of s and y. 2D circular buffer of scalar products between elements of s and y.
_yy_store : dictionary _yy_store : numpy.ndarray
Dictionary of scalar products between different elements of y. 2D circular buffer of scalar products between different elements of y.
""" """
def __init__(self, max_history_length, x0, gradient): def __init__(self, max_history_length, x0, gradient):
...@@ -129,10 +129,10 @@ class InformationStore(object): ...@@ -129,10 +129,10 @@ class InformationStore(object):
self.last_gradient = gradient.copy() self.last_gradient = gradient.copy()
self.k = 0 self.k = 0
hl = max_history_length mmax = max_history_length
self._ss_store = np.empty((hl, hl), dtype=np.float64) self._ss_store = np.empty((mmax, mmax), dtype=np.float64)
self._sy_store = np.empty((hl, hl), dtype=np.float64) self._sy_store = np.empty((mmax, mmax), dtype=np.float64)
self._yy_store = np.empty((hl, hl), dtype=np.float64) self._yy_store = np.empty((mmax, mmax), dtype=np.float64)
@property @property
def history_length(self): def history_length(self):
...@@ -153,15 +153,16 @@ class InformationStore(object): ...@@ -153,15 +153,16 @@ class InformationStore(object):
""" """
result = [] result = []
m = self.history_length m = self.history_length
mmax = self.max_history_length
k = self.k k = self.k
s = self.s s = self.s
for i in xrange(m): for i in xrange(m):
result.append(s[(k-m+i) % m]) result.append(s[(k-m+i) % mmax])
y = self.y y = self.y
for i in xrange(m): for i in xrange(m):
result.append(y[(k-m+i) % m]) result.append(y[(k-m+i) % mmax])
result.append(self.last_gradient) result.append(self.last_gradient)
...@@ -181,28 +182,27 @@ class InformationStore(object): ...@@ -181,28 +182,27 @@ class InformationStore(object):
""" """
m = self.history_length m = self.history_length
mmax = self.max_history_length
k = self.k k = self.k
result = np.empty((2*m+1, 2*m+1), dtype=np.float) result = np.empty((2*m+1, 2*m+1), dtype=np.float)
# update the stores # update the stores
if (m > 0): k1 = (k-1) % mmax
k1 = (k-1) % m
for i in xrange(m): for i in xrange(m):
kmi = (k-m+i) % m kmi = (k-m+i) % mmax
self._ss_store[kmi, k1] = self._ss_store[k1, kmi] \ self._ss_store[kmi, k1] = self._ss_store[k1, kmi] \
= self.s[kmi].vdot(self.s[k1]) = self.s[kmi].vdot(self.s[k1])
self._yy_store[kmi, k1] = self._yy_store[k1, kmi] \ self._yy_store[kmi, k1] = self._yy_store[k1, kmi] \
= self.y[kmi].vdot(self.y[k1]) = self.y[kmi].vdot(self.y[k1])
self._sy_store[kmi, k1] = self.s[kmi].vdot(self.y[k1]) self._sy_store[kmi, k1] = self.s[kmi].vdot(self.y[k1])
for j in xrange(m-1): for j in xrange(m-1):
kmj = (k-m+j) % m kmj = (k+-m+j) % mmax
self._sy_store[k1, kmj] = self.s[k1].vdot(self.y[kmj]) self._sy_store[k1, kmj] = self.s[k1].vdot(self.y[kmj])
for i in xrange(m): for i in xrange(m):
kmi = (k-m+i) % m kmi = (k-m+i) % mmax
for j in xrange(m): for j in xrange(m):
kmj = (k-m+j) % m kmj = (k-m+j) % mmax
result[i, j] = self._ss_store[kmi, kmj] result[i, j] = self._ss_store[kmi, kmj]
result[i, m+j] = result[m+j, i] = self._sy_store[kmi, kmj] result[i, m+j] = result[m+j, i] = self._sy_store[kmi, kmj]
result[m+i, m+j] = self._yy_store[kmi, kmj] result[m+i, m+j] = self._yy_store[kmi, kmj]
...@@ -257,9 +257,9 @@ class InformationStore(object): ...@@ -257,9 +257,9 @@ class InformationStore(object):
the respective list. the respective list.
""" """
m = self.max_history_length mmax = self.max_history_length
self.s[self.k % m] = x - self.last_x self.s[self.k % mmax] = x - self.last_x
self.y[self.k % m] = gradient - self.last_gradient self.y[self.k % mmax] = gradient - self.last_gradient
self.last_x = x.copy() self.last_x = x.copy()
self.last_gradient = gradient.copy() self.last_gradient = gradient.copy()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment