Commit 9327b752 authored by Martin Reinecke's avatar Martin Reinecke

cleaner interface for get_descent_direction

parent e9284196
Pipeline #61058 passed with stages
in 8 minutes and 30 seconds
...@@ -85,7 +85,8 @@ class DescentMinimizer(Minimizer): ...@@ -85,7 +85,8 @@ class DescentMinimizer(Minimizer):
# compute a step length that reduces energy.value sufficiently # compute a step length that reduces energy.value sufficiently
new_energy, success = self.line_searcher.perform_line_search( new_energy, success = self.line_searcher.perform_line_search(
energy=energy, pk=self.get_descent_direction(energy, f_k_minus_1), energy=energy,
pk=self.get_descent_direction(energy, f_k_minus_1),
f_k_minus_1=f_k_minus_1) f_k_minus_1=f_k_minus_1)
if not success: if not success:
self.reset() self.reset()
...@@ -109,7 +110,7 @@ class DescentMinimizer(Minimizer): ...@@ -109,7 +110,7 @@ class DescentMinimizer(Minimizer):
def reset(self): def reset(self):
pass pass
def get_descent_direction(self, energy): def get_descent_direction(self, energy, old_value=None):
"""Calculates the next descent direction. """Calculates the next descent direction.
Parameters Parameters
...@@ -118,6 +119,10 @@ class DescentMinimizer(Minimizer): ...@@ -118,6 +119,10 @@ class DescentMinimizer(Minimizer):
An instance of the Energy class which shall be minimized. The An instance of the Energy class which shall be minimized. The
position of `energy` is used as the starting point of minimization. position of `energy` is used as the starting point of minimization.
old_value : float
if provided, this must be the value of the energy in the previous
step.
Returns Returns
------- -------
Field Field
...@@ -133,7 +138,7 @@ class SteepestDescent(DescentMinimizer): ...@@ -133,7 +138,7 @@ class SteepestDescent(DescentMinimizer):
functional's gradient for minimization. functional's gradient for minimization.
""" """
def get_descent_direction(self, energy, _): def get_descent_direction(self, energy, _=None):
return -energy.gradient return -energy.gradient
...@@ -150,7 +155,7 @@ class RelaxedNewton(DescentMinimizer): ...@@ -150,7 +155,7 @@ class RelaxedNewton(DescentMinimizer):
super(RelaxedNewton, self).__init__(controller=controller, super(RelaxedNewton, self).__init__(controller=controller,
line_searcher=line_searcher) line_searcher=line_searcher)
def get_descent_direction(self, energy, _): def get_descent_direction(self, energy, _=None):
return -energy.metric.inverse_times(energy.gradient) return -energy.metric.inverse_times(energy.gradient)
...@@ -171,12 +176,12 @@ class NewtonCG(DescentMinimizer): ...@@ -171,12 +176,12 @@ class NewtonCG(DescentMinimizer):
self._nreset = nreset self._nreset = nreset
self._file_name = file_name self._file_name = file_name
def get_descent_direction(self, energy, f_k_minus_1): def get_descent_direction(self, energy, old_value=None):
if f_k_minus_1 is None: if old_value is None:
ic = GradientNormController(iteration_limit=5) ic = GradientNormController(iteration_limit=5)
else: else:
alpha = 0.1 alpha = 0.1
ediff = alpha*(f_k_minus_1 - energy.value) ediff = alpha*(old_value-energy.value)
ic = AbsDeltaEnergyController( ic = AbsDeltaEnergyController(
ediff, iteration_limit=200, name=self._name, ediff, iteration_limit=200, name=self._name,
file_name=self._file_name) file_name=self._file_name)
...@@ -205,7 +210,7 @@ class L_BFGS(DescentMinimizer): ...@@ -205,7 +210,7 @@ class L_BFGS(DescentMinimizer):
self._s = [None]*self.max_history_length self._s = [None]*self.max_history_length
self._y = [None]*self.max_history_length self._y = [None]*self.max_history_length
def get_descent_direction(self, energy, _): def get_descent_direction(self, energy, _=None):
x = energy.position x = energy.position
s = self._s s = self._s
y = self._y y = self._y
...@@ -268,7 +273,7 @@ class VL_BFGS(DescentMinimizer): ...@@ -268,7 +273,7 @@ class VL_BFGS(DescentMinimizer):
def reset(self): def reset(self):
self._information_store = None self._information_store = None
def get_descent_direction(self, energy, _): def get_descent_direction(self, energy, _=None):
x = energy.position x = energy.position
gradient = energy.gradient gradient = energy.gradient
# initialize the information store if it doesn't already exist # initialize the information store if it doesn't already exist
......
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