Algorithm-Evolutionary

 view release on metacpan or  search on metacpan

t/0302-knapsack.t  view on Meta::CPAN

#-*-cperl-*-
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'

#########################

use Test::More tests => 5;

use warnings;
use strict;

use lib qw( ../../lib ../lib lib ); #Just in case we are testing it in-place
use Algorithm::Evolutionary::Individual::BitString;

use_ok( "Algorithm::Evolutionary::Fitness::Knapsack", "using A::E::Fitness::Knapsack OK" );

#Weights and profits. Created with a generator
my @profits=(13.76, 8.40465, 7.06637, 14.066, 6.92841, 13.0597, 8.08657, 9.78078, 7.69908, 11.3133, 11.5659, 9.03571, 14.9824, 9.77511, 6.96194, 12.4665, 14.5813, 9.76035, 10.4337, 11.443, 12.8197, 10.4104, 14.1428, 7.15892, 10.8211, 7.30107, 7.86628...
my @weights=(8.75995, 3.40465, 2.06637, 9.06605, 1.92841, 8.05966, 3.08657, 4.78078, 2.69908, 6.31326, 6.56593, 4.03571, 9.98242, 4.77511, 1.96194, 7.46651, 9.58128, 4.76035, 5.4337, 6.44295, 7.8197, 5.41039, 9.14283, 2.15892, 5.82114, 2.30107, 2.866...

my $Nmax=100;  #Max. number of elements to choose
my $capacity=286; #Max. Capacity of the knapsack
my $rho=5.0625; #Penalizations coeficient

my $knap = new Algorithm::Evolutionary::Fitness::Knapsack( $Nmax, $capacity, $rho, \@profits, \@weights );
isa_ok( $knap,  "Algorithm::Evolutionary::Fitness::Knapsack" );

my $indi = new Algorithm::Evolutionary::Individual::BitString $Nmax ; # Build random bitstring with length 10
ok( $knap->_apply( $indi ) > 0, "Works on indis" );
ok( $knap->knapsack( $indi->{'_str'})  > 0, "Works on strings and caches" );
ok( $knap->cached_evals() == 1, "Cached evals OK");



( run in 1.671 second using v1.01-cache-2.11-cpan-39bf76dae61 )