Add unit tests (trust_region algorithm).

Using mex implementation (block_trust_region).
trust-region-mex
Stéphane Adjemian (Ryûk) 2021-07-23 19:42:30 +02:00
parent e8cc23d3af
commit bdc95dd1d0
Signed by: stepan
GPG Key ID: 295C1FE89E17EB3C
2 changed files with 236 additions and 2 deletions

View File

@ -1116,7 +1116,8 @@ M_TRS_FILES += run_block_byte_tests_matlab.m.trs \
histval_initval_file_unit_tests.m.trs \
test_aggregate_routine_1_2.m.trs \
test_aggregate_routine_1_2_3.m.trs \
run_kronecker_tests.m.trs
run_kronecker_tests.m.trs \
nonlinearsolvers.m.trs
M_XFAIL_TRS_FILES = $(patsubst %.mod, %.m.trs, $(XFAIL_MODFILES))
@ -1127,7 +1128,8 @@ O_TRS_FILES += run_block_byte_tests_octave.o.trs \
run_reporting_test_octave.o.trs \
run_all_unitary_tests.o.trs \
histval_initval_file_unit_tests.o.trs \
run_kronecker_tests.o.trs
run_kronecker_tests.o.trs \
nonlinearsolvers.o.trs
O_XFAIL_TRS_FILES = $(patsubst %.mod, %.o.trs, $(XFAIL_MODFILES))

232
tests/nonlinearsolvers.m Normal file
View File

@ -0,0 +1,232 @@
debug = false;
if debug
[top_test_dir, ~, ~] = fileparts(mfilename('fullpath'));
else
top_test_dir = getenv('TOP_TEST_DIR');
end
addpath(sprintf('%s/matlab', top_test_dir(1:end-6)))
addpath(sprintf('%s/tests/solver-test-functions', top_test_dir(1:end-6)))
if ~debug
% Test Dynare Version
if ~strcmp(dynare_version(), getenv('DYNARE_VERSION'))
error('Incorrect version of Dynare is being tested')
end
end
dynare_config;
NumberOfTests = 0;
testFailed = 0;
if ~debug
skipline()
disp('*** TESTING: nonlinearsolvers.m ***');
end
tolf = 1e-6;
tolx = 1e-6;
maxit = 50;
factor = 10;
auxstruct = struct();
t0 = clock;
try
NumberOfTests = NumberOfTests+1;
x = rosenbrock();
[x, errorflag] = block_trust_region(@rosenbrock, x, tolf, tolx, maxit, factor, false, auxstruct);
if errorflag
testFailed = testFailed+1;
end
catch
testFailed = testFailed+1;
end
try
NumberOfTests = NumberOfTests+1;
x = powell1();
[x, errorflag] = block_trust_region(@powell1, x, tolf, tolx, maxit, factor, false, auxstruct);
if errorflag
testFailed = testFailed+1;
end
catch
testFailed = testFailed+1;
end
try
NumberOfTests = NumberOfTests+1;
x = powell2();
[x, errorflag] = block_trust_region(@powell2, x, tolf, tolx, maxit, factor, false, auxstruct);
if errorflag
testFailed = testFailed+1;
end
catch
testFailed = testFailed+1;
end
try
NumberOfTests = NumberOfTests+1;
x = wood();
[x, errorflag] = block_trust_region(@wood, x, tolf, tolx, maxit, factor, false, auxstruct);
if errorflag
testFailed = testFailed+1;
end
catch
testFailed = testFailed+1;
end
try
NumberOfTests = NumberOfTests+1;
% FIXME block_trust_region is diverging if x(1)<0. Note that trust_region is not finding the
% solution for the same initial conditions.
x = helicalvalley();
x(1) = 5;
[x, errorflag] = block_trust_region(@helicalvalley, x, tolf, tolx, maxit, factor, false, auxstruct);
if errorflag
testFailed = testFailed+1;
end
catch
testFailed = testFailed+1;
end
try
NumberOfTests = NumberOfTests+1;
n = 10;
x = watson(nan(n,1));
[x, errorflag] = block_trust_region(@watson, x, tolf, tolx, maxit, factor, false, auxstruct);
if errorflag
testFailed = testFailed+1;
end
catch
testFailed = testFailed+1;
end
try
NumberOfTests = NumberOfTests+1;
% FIXME block_trust_region does not work for all n.
n = 9;
x = chebyquad(nan(n,1));
[x, errorflag] = block_trust_region(@chebyquad, x, tolf, tolx, maxit, factor, false, auxstruct);
if errorflag
testFailed = testFailed+1;
end
catch
testFailed = testFailed+1;
end
try
NumberOfTests = NumberOfTests+1;
n = 10;
x = brown(nan(n,1));
[x, errorflag] = block_trust_region(@brown, x, tolf, tolx, maxit, factor, false, auxstruct);
if errorflag
testFailed = testFailed+1;
end
catch
testFailed = testFailed+1;
end
try
NumberOfTests = NumberOfTests+1;
n = 10;
x = discreteboundaryvalue(nan(n,1));
[x, errorflag] = block_trust_region(@discreteboundaryvalue, x, tolf, tolx, maxit, factor, false, auxstruct);
if errorflag
testFailed = testFailed+1;
end
catch
testFailed = testFailed+1;
end
try
NumberOfTests = NumberOfTests+1;
n = 10;
x = discreteintegralequation(nan(n,1));
[x, errorflag] = block_trust_region(@discreteintegralequation, x, tolf, tolx, maxit, factor, false, auxstruct);
if errorflag
testFailed = testFailed+1;
end
catch
testFailed = testFailed+1;
end
try
NumberOfTests = NumberOfTests+1;
n = 10;
x = trigonometric(nan(n,1));
[x, errorflag] = block_trust_region(@trigonometric, x, tolf, tolx, maxit, factor, false, auxstruct);
if errorflag
testFailed = testFailed+1;
end
catch
testFailed = testFailed+1;
end
try
NumberOfTests = NumberOfTests+1;
n = 10;
x = variablydimensioned(nan(n,1));
[x, errorflag] = block_trust_region(@variablydimensioned, x, tolf, tolx, maxit, factor, false, auxstruct);
if errorflag
testFailed = testFailed+1;
end
catch
testFailed = testFailed+1;
end
try
NumberOfTests = NumberOfTests+1;
n = 10;
x = broydentridiagonal(nan(n,1));
[x, errorflag] = block_trust_region(@broydentridiagonal, x, tolf, tolx, maxit, factor, false, auxstruct);
if errorflag
testFailed = testFailed+1;
end
catch
testFailed = testFailed+1;
end
try
NumberOfTests = NumberOfTests+1;
n = 10;
x = broydenbanded(nan(n,1));
[x, errorflag] = block_trust_region(@broydenbanded, x, tolf, tolx, maxit, factor, false, auxstruct);
if errorflag
testFailed = testFailed+1;
end
catch
testFailed = testFailed+1;
end
t1 = clock;
if ~debug
cd(getenv('TOP_TEST_DIR'));
else
dprintf('FAILED tests: %i', testFailed)
end
if isoctave
fid = fopen('nonlinearsolvers.o.trs', 'w+');
else
fid = fopen('nonlinearsolvers.m.trs', 'w+');
end
if testFailed
fprintf(fid,':test-result: FAIL\n');
else
fprintf(fid,':test-result: PASS\n');
end
fprintf(fid,':number-tests: %i\n', NumberOfTests);
fprintf(fid,':number-failed-tests: %i\n', testFailed);
fprintf(fid,':list-of-passed-tests: nonlinearsolvers.m\n');
fprintf(fid,':elapsed-time: %f\n', etime(t1, t0));
fclose(fid);
if ~debug
exit;
end