Skip to content

Commit

Permalink
Add tests for cgfam and lbfgsb. Fix lbfgsb example
Browse files Browse the repository at this point in the history
  • Loading branch information
gvanuxem authored and mohawk2 committed Apr 25, 2024
1 parent f59d1a0 commit 5c1c6ba
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
2 changes: 1 addition & 1 deletion nonlinear.pd
Original file line number Diff line number Diff line change
Expand Up @@ -1505,8 +1505,8 @@ It uses the limited memory BFGS method.
# The SIAM 100-Digit Challenge problem 4
# see http://www-m8.ma.tum.de/m3/bornemann/challengebook/
# result: -3.30686864747523728007611377089851565716648236
use PDL::NiceSlice;
use PDL::Opt::NonLinear;
use PDL::Stat::Distributions;

$x = (random(2,500)-0.5)*2;
$gx = zeroes(2,500);
Expand Down
58 changes: 48 additions & 10 deletions t/1.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use strict;
use warnings;
use PDL::LiteF;
use PDL::Opt::NonLinear;
use PDL::NiceSlice;
use Test::More;

sub approx_ok {
Expand All @@ -26,21 +27,21 @@ my $gtol = pdl(1e-8);
#$stepmx = pdl(0.5);
my $maxit = pdl(long, 50);
sub min_func{
my ($fx, $x) = @_;
$fx .= rosen($x);
my ($fx, $x) = @_;
$fx .= rosen($x);
}
sub grad_func{
my ($gx, $x) = @_;
$gx .= rosen_grad($x);
my ($gx, $x) = @_;
$gx .= rosen_grad($x);
}
sub hess_func{
my ($hx, $x) = @_;
$hx .= rosen_hess($x);
my ($hx, $x) = @_;
$hx .= rosen_hess($x);
}
tensoropt($fx, $gx, $hx, $x,
1,$maxit,15,1,2,1,
ones(5),0.5,$xtol,$gtol,2,6,
\&min_func, \&grad_func, \&hess_func);
tensoropt($fx, $gx, $hx, $x,
1,$maxit,15,1,2,1,
ones(5),0.5,$xtol,$gtol,2,6,
\&min_func, \&grad_func, \&hess_func);

approx_ok $x,$res,0.001,'tensoropt';

Expand Down Expand Up @@ -70,4 +71,41 @@ lbfgs($fx, $gx, $x, $diag, $diagco, $m, $maxit, $maxfc, $eps, $xtol, $gtol,
$print,$info,\&fg_func,\&fdiag);
approx_ok $x,$res,0.0001,'lbfgs';

$x = pdl '[0.49823058 0.98093641 0.63151156 0.66477157 0.60801367]';
$gx = zeroes(5);
$fx = pdl(0);

my $bounds = zeroes(5,2);
$bounds(,0).= -5;
$bounds(,1).= 5;
my $tbounds = zeroes(5);
$tbounds .= 2;
$gtol = pdl(0.9);
my $pgtol = pdl(1e-10);
my $factr = pdl(100);
$print = pdl(long, [0,0]);
$maxit = pdl(long,100);
$info = pdl(long,0);
$m = pdl(long,10);
my $iv = zeroes(long,44);
my $v = zeroes(29);
sub fg_func{

Check failure on line 92 in t/1.t

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, 5.10)

Subroutine fg_func redefined

Check failure on line 92 in t/1.t

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, 5.14)

Subroutine fg_func redefined

Check failure on line 92 in t/1.t

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, 5.20)

Subroutine fg_func redefined

Check failure on line 92 in t/1.t

View workflow job for this annotation

GitHub Actions / ci (5.30, ubuntu-latest, true, true)

Subroutine fg_func redefined

Check failure on line 92 in t/1.t

View workflow job for this annotation

GitHub Actions / ci (5.30, macos-latest)

Subroutine fg_func redefined
my ($f, $g, $x) = @_;
$f .= rosen($x);
$g .= rosen_grad($x);
return 0;
}
lbfgsb($fx, $gx, $x, $m, $bounds, $tbounds, $maxit, $factr, $pgtol, $gtol,
$print, $info,$iv, $v,\&fg_func);
approx_ok $x,$res,0.0001,'lbfgsb';

$x = pdl '[0.49823058 0.98093641 0.63151156 0.66477157 0.60801367]';
$gx = rosen_grad($x);
$fx = rosen($x);
$print = zeroes(2);
$maxit = pdl(long, 200);
$info = pdl(long,0);
cgfam($fx, $gx, $x, $maxit, $eps, $xtol, $gtol,$print,$info,1,\&fg_func);
approx_ok $x,$res,0.0001,'cgfam';

done_testing;

0 comments on commit 5c1c6ba

Please sign in to comment.