File: 0_poly.t

package info (click to toggle)
libmath-amoeba-perl 0.05-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 92 kB
  • sloc: perl: 183; makefile: 2
file content (50 lines) | stat: -rw-r--r-- 990 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use strict;

########################################################################
# This script is to test the ability of the optimizer to tune:
# 
# 	f(x) = sum_i (x_i - i) 	where i from 1 to N.
# 
########################################################################

use Test::More (tests => 11);

sub BEGIN
{
	use_ok('Math::Amoeba', 0.02, 'MinimiseND');
}

sub testPoly
{
	my $tol = 1e-5;

	my $N = 9;
	my $y = [1..$N];

	my $func = sub {
		my $x = [@_];

		my $cost = 0;
		foreach my $i (0..$N-1) {
			$cost += ($x->[$i] - $y->[$i])**2;	
		}
		return $cost;
	};
	
	my $guess = [ map { rand()*2 - 1 } (1..$N) ];
	my $scale = [ map { 1 } (1..$N) ]; 
	my ($opt, $cost) = MinimiseND($guess, $scale, \&$func, $tol, 10000);

	ok(abs($cost) < $tol, "Optimization success ($cost vs 0)");
	foreach my $i (0..$N-1) {
		ok(
			abs($opt->[$i] - $y->[$i]) < 1e-2, 
			sprintf(
				qq{The %d th parameter error okay (%.5f vs %.5f).},
				$i, $opt->[$i], $y->[$i]
			)
		);
	}
}

&testPoly();