Skip to content

Commit f2ba022

Browse files
committed
Minimizer test fixed.
1 parent ed8811a commit f2ba022

File tree

4 files changed

+34
-23
lines changed

4 files changed

+34
-23
lines changed

SConstruct

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ vars.AddVariables(
136136
EnumVariable('build_trilinos', 'Instructs scons to build the trilinos library.', "make", allowed_values = build_trilinos_flavours),
137137
('trilinos_prefix', 'Prefix/Paths to Trilinos installation (need to be set if build_trilinos = False).', default_prefix),
138138
('trilinos_libs', 'Trilinos libraries to link with', []),
139-
PathVariable('trilinos_src', 'Top-level source directory for trilinos.', Dir('trilinos_source15').abspath, PathVariable.PathIsDir),
139+
PathVariable('trilinos_src', 'Top-level source directory for trilinos.', Dir('trilinos_source16').abspath, PathVariable.PathIsDir),
140140
PathVariable('trilinos_build', 'Top-level build directory for trilinos.', Dir('#/build_trilinos').abspath, PathVariable.PathIsDirCreate),
141141
PathVariable('trilinos_install', 'Top-level install directory for trilinos when built', Dir('#/esys.trilinos').abspath, PathVariable.PathIsDirCreate),
142142
#('trilinos_install', 'path to install trilinos libs, default is <prefix>/lib/esys', 'default'),

escript/py_src/minimizer.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -812,8 +812,8 @@ def setOptions(self, **opts):
812812
:key m_tol: relative tolerance for solution `m` for termination of iteration
813813
:type m_tol: `float`
814814
:default m_tol: 1e-4
815-
:key grad_tol: tolerance for gradient relative to initial costfunction value for termination of iteration
816-
:type grad_tol: `float`
815+
:key grad_tol: tolerance for gradient relative to initial cost function value for termination of iteration
816+
:type grad_tol: `float` or None
817817
:default grad_tol: 1e-4
818818
:key truncation: sets the number of previous LBFGS iterations to keep
819819
:type truncation : `int`
@@ -854,7 +854,10 @@ def setOptions(self, **opts):
854854
elif o == "m_tol":
855855
self._m_tol = max(float(opts[o]), EPSILON)
856856
elif o == "grad_tol":
857-
self._grad_tol = max(float(opts[o]), EPSILON)
857+
if opts[o] is None:
858+
self._grad_tol = None
859+
else:
860+
self._grad_tol = max(float(opts[o]), EPSILON)
858861
elif o == 'historySize' or o == 'truncation':
859862
assert opts[o] > 2, "Trancation must be greater than 2."
860863
self._truncation = max(0, int(opts[o]))
@@ -946,7 +949,8 @@ def run(self, m):
946949
:rtype: m-type
947950
"""
948951
assert self._m_tol > 0.
949-
assert self._grad_tol > 0.
952+
if not self._grad_tol is None:
953+
assert self._grad_tol > 0.
950954
assert self._iterMax > 1
951955
assert self._relAlphaMin > 0.
952956
assert self._truncation > 0
@@ -981,7 +985,6 @@ def run(self, m):
981985
while not converged and not break_down and k < self._restart and iterCount < self._iterMax:
982986
self.logger.info("********** iteration %3d **********" % iterCount)
983987
self.logger.info("\tF(m) = %g" % Fm)
984-
print(Fm)
985988
# determine search direction
986989
p = -self._twoLoop(H_scale, grad_Fm, s_and_y, m, args_m)
987990
# Now we call the line search with F(m+alpha*p)
@@ -1040,16 +1043,17 @@ def run(self, m):
10401043
args_new = self.getCostFunction().getArgumentsAndCount(m_new)
10411044
grad_Fm_new = self.getCostFunction().getGradientAndCount(m_new, *args_new)
10421045

1043-
Ftol_abs = self._grad_tol * abs(max(abs(Fm), abs(Fm_new)))
1044-
dFm = abs(Fm - Fm_new)
1045-
flag = dFm <= Ftol_abs
1046-
if flag:
1047-
converged = True
1048-
self.logger.info("F(m) = %g" % Fm_new)
1049-
self.logger.info("Gradient has converged: |F-Fold|=%g < g_tol*max(|F|,|Fold|)=%g" % (dFm, Ftol_abs))
1050-
break
1051-
else:
1052-
self.logger.info("Gradient checked: |F-Fold|=%g, g_tol*max(|F|,|Fold|)=%g" % (dFm, Ftol_abs))
1046+
if not self._grad_tol is None:
1047+
Ftol_abs = self._grad_tol * abs(max(abs(Fm), abs(Fm_new)))
1048+
dFm = abs(Fm - Fm_new)
1049+
flag = dFm <= Ftol_abs
1050+
if flag:
1051+
converged = True
1052+
self.logger.info("F(m) = %g" % Fm_new)
1053+
self.logger.info("Gradient has converged: |F-Fold|=%g < g_tol*max(|F|,|Fold|)=%g" % (dFm, Ftol_abs))
1054+
break
1055+
else:
1056+
self.logger.info("Gradient checked: |F-Fold|=%g, g_tol*max(|F|,|Fold|)=%g" % (dFm, Ftol_abs))
10531057

10541058
delta_g = grad_Fm_new - grad_Fm
10551059
rho = self.getCostFunction().getDualProductAndCount(delta_m, delta_g)

escript/test/python/run_minimizer.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,31 +112,37 @@ class Test_MinimizerBFGS(unittest.TestCase):
112112
'm_true': [0.756394642236015, -0.210685240100182, 1.315069006925737] }
113113
def testTEST1(self):
114114
F = MinDist(**self.TEST1)
115-
solve = MinimizerLBFGS(F, m_tol=1e-8, grad_tol=1e-10, iterMax=30)
115+
solve = MinimizerLBFGS(F, m_tol=1e-7, grad_tol=None, iterMax=100)
116116
m0 = np.zeros((F.dimension,))
117117
solve.run(m0)
118118
m = solve.getResult()
119-
print(m, F.m_true)
120119
self.assertTrue(np.allclose(m, F.m_true))
121120

122121
def testTEST2(self):
123122
F = MinDist(**self.TEST2)
124-
solve = MinimizerLBFGS(F, m_tol=1e-8, grad_tol=1e-10, iterMax=100)
123+
solve = MinimizerLBFGS(F, m_tol=1e-7, grad_tol=None, iterMax=100)
125124
m0 = np.zeros((F.dimension,))
126125
solve.run(m0)
127126
m = solve.getResult()
128-
print(m, F.m_true)
129127
self.assertTrue(np.allclose(m, F.m_true))
130128

131129
def testTEST3(self):
132130
F = MinDist(**self.TEST3)
133-
solve = MinimizerLBFGS(F, m_tol=1e-8, grad_tol=1e-10, iterMax=100)
131+
solve = MinimizerLBFGS(F, m_tol=1e-7, grad_tol=None, iterMax=100)
134132
m0 = np.zeros((F.dimension,))
135133
solve.run(m0)
136134
m = solve.getResult()
137-
print(m, F.m_true)
138135
self.assertTrue(np.allclose(m, F.m_true))
139136

137+
def testTEST4(self):
138+
F = MinDist(**self.TEST1)
139+
solve = MinimizerLBFGS(F, m_tol=1e-10, grad_tol=1e-8, iterMax=100)
140+
m0 = np.zeros((F.dimension,))
141+
solve.run(m0)
142+
m = solve.getResult()
143+
#print((m-F.m_true)/F.m_true)
144+
self.assertTrue(np.allclose(m, F.m_true, rtol=1e-3))
145+
140146
def testOptions(self):
141147
"""
142148
test for setting options

escriptcore/test/python/test_objects.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,9 @@ def test_DumpAndLoad_Constant(self):
614614
]:
615615

616616
for rank in range(5):
617+
print(spacename, rank)
617618
filename=os.path.join(self.filename_base,
618-
"constant_{0}_rank{1}.nc".format(spacename, rank))
619+
"constant_{0}_rank{1}.h5".format(spacename, rank))
619620
d=Data(self.args[rank], functionspace(self.domain))
620621
self._diffDataObjects(d,filename)
621622

0 commit comments

Comments
 (0)