``````use strict; # -*- cperl -*-
use warnings;

use lib qw( ../../../../lib );

Algorithm::Evolutionary::Fitness::Rastrigin - Implementation of Rastrigin's function

my \$n_dimensions=2;  #Max. number of elements to choose
my \$rastrigin = Algorithm::Evolutionary::Fitness::Rastrigin->new( \$n_dimensions );

Classical Rastrigin function, used for tests of numerical optimization problems.
Check it at L<http://www-optima.amp.i.kyoto-u.ac.jp/member/student/hedar/Hedar_files/TestGO_files/Page2607.htm>

=cut

package Algorithm::Evolutionary::Fitness::Rastrigin;

our (\$VERSION) = ( '\$Revision: 3.3 \$ ' =~ / (\d+\.\d+)/ ) ;

use Carp qw( croak );
use base qw(Algorithm::Evolutionary::Fitness);
use constant PI2    => 8 * atan2(1, 1);
use constant RASTRIGIN_A => 10;

Creates a new instance of the problem, with the said number of bits and peaks

=cut

sub new {
my \$class = shift;
my ( \$n_dimensions ) = @_;

#Instantiate superclass
my \$self = \$class->SUPER::new();

#Assign stuff
\$self->{'n_dimensions'} = \$n_dimensions;
\$self->{'_base_fitness'} = RASTRIGIN_A*\$n_dimensions;
\$self->initialize();
return \$self;
}

sub _really_apply {
my \$self = shift;
return \$self->Rastrigin( @_ );
}

Applies the knapsack problem to the string, using a penalty function

=cut

sub Rastrigin {
my \$self = shift;
my @array = @_;
my \$fitness = \$self->{'_base_fitness'};
for ( my \$i = 0; \$i < \$self->{'n_dimensions'}; \$i ++ ) {
\$fitness += \$array[\$i]*\$array[\$i]- RASTRIGIN_A *cos(PI2*\$array[\$i]);
}
return \$fitness;
}