BEGIN { plan
'no_plan'
};
use
lib
qw( lib ../lib ../../lib )
;
Individual::Vector Individual::Tree
Fitness::ONEMAX)
;
print
"Testing Individual objects...String \n"
;
is(
ref
Algorithm::Evolutionary::Individual::String->new([
'a'
..
'z'
],10),
"Algorithm::Evolutionary::Individual::String"
,
"Good ref"
);
is(
ref
Algorithm::Evolutionary::Individual::Base::create(
'String'
, {
chars
=> [
'a'
..
'e'
],
length
=> 10 }),
"Algorithm::Evolutionary::Individual::String"
,
"Good ref"
);
print
"BitString...\n"
;
my
$bs
= Algorithm::Evolutionary::Individual::BitString->new(100);
is(
ref
$bs
,
"Algorithm::Evolutionary::Individual::BitString"
, ,
"Good ref"
);
is(
ref
Algorithm::Evolutionary::Individual::Base::create(
'BitString'
, {
length
=> 10 }),
"Algorithm::Evolutionary::Individual::BitString"
,
"Good ref"
);
print
"Vector...\n"
;
is(
ref
Algorithm::Evolutionary::Individual::Vector->new(10),
"Algorithm::Evolutionary::Individual::Vector"
,
"Good ref"
);
is(
ref
Algorithm::Evolutionary::Individual::Base::create(
'Vector'
,
{
length
=> 20,
rangestart
=> -5,
rangeend
=> 5 }),
"Algorithm::Evolutionary::Individual::Vector"
,
"Good ref"
);
my
$primitives
= {
sum
=> [2, -1, 1],
multiply
=> [2, -1, 1],
substract
=> [2, -1, 1],
divide
=> [2, -1, 1],
x
=> [0, -10, 10],
y
=> [0, -10, 10] };
is(
ref
Algorithm::Evolutionary::Individual::Tree->new(
$primitives
, 3 ),
"Algorithm::Evolutionary::Individual::Tree"
,
"Good ref"
);
my
$fitness
=
sub
{
my
$indi
=
shift
;
return
unpack
(
"N"
,
pack
(
"B32"
,
substr
(
"0"
x 32 .
$indi
->{
'_str'
}, -32)));
};
is(
$bs
->evaluate(
$fitness
) > 0, 1,
"Evaluation correct"
);
my
$fitness_obj
= new Algorithm::Evolutionary::Fitness::ONEMAX;
is(
$bs
->evaluate(
$fitness_obj
) > 0, 1,
"Evaluation object correct"
);
my
$bprime
= new Algorithm::Evolutionary::Individual::String [
'a'
..
'z'
], 64;
print
"Testing algorithms\n"
;
my
$m
= new Algorithm::Evolutionary::Op::Bitflip;
my
$initTemp
= 2;
my
$minTemp
= 0.1;
my
$freezer
= new Algorithm::Evolutionary::Op::LinearFreezer(
$initTemp
);
my
$numChanges
= 7;
my
$eval
=
sub
{
my
$indi
=
shift
;
my
(
$x
,
$y
) = @{
$indi
->{_array}};
my
$sqrt
=
sqrt
(
$x
*$x
+
$y
*$y
);
return
sin
(
$sqrt
)/
$sqrt
;
};
my
$sa
= new Algorithm::Evolutionary::Op::SimulatedAnnealing(
$eval
,
$m
,
$freezer
,
$initTemp
,
$minTemp
, );
is(
ref
$sa
,
'Algorithm::Evolutionary::Op::SimulatedAnnealing'
,
"Good class"
);
my
$c
= new Algorithm::Evolutionary::Op::Crossover;
my
$replacementRate
= 0.3;
my
$popSize
= 20;
my
$selector
= new Algorithm::Evolutionary::Op::RouletteWheel
$popSize
;
my
$onemax
=
sub
{
my
$indi
=
shift
;
my
$total
= 0;
my
$len
=
$indi
->size();
my
$i
= 0;
while
(
$i
<
$len
) {
$total
+=
substr
(
$indi
->{
'_str'
},
$i
, 1);
$i
++;
}
return
$total
;
};
my
@pop
;
my
$numBits
= 20;
for
( 0..
$popSize
) {
my
$indi
= new Algorithm::Evolutionary::Individual::BitString
$numBits
;
my
$fitness
=
$onemax
->(
$indi
);
$indi
->Fitness(
$fitness
);
push
(
@pop
,
$indi
);
}
my
$generation
=
new Algorithm::Evolutionary::Op::GeneralGeneration(
$onemax
,
$selector
, [
$m
,
$c
],
$replacementRate
);
my
@sortPop
=
sort
{
$b
->Fitness() <=>
$a
->Fitness() }
@pop
;
my
$bestIndi
=
$sortPop
[0];
$generation
->apply( \
@sortPop
);
is(
$bestIndi
->Fitness() <=
$sortPop
[0]->Fitness(), 1,
"Fitness improvement"
);
my
$ggxml
=
$generation
->asXML();
my
$gprime
= Algorithm::Evolutionary::Op::Base->fromXML(
$ggxml
);
is(
$gprime
->{_eval}(
$pop
[0] ) eq
$generation
->{_eval}(
$pop
[0] ) , 1,
"XML"
);
my
$ez
= new Algorithm::Evolutionary::Op::Easy
$onemax
;
my
$ezxml
=
$ez
->asXML();
my
$ezprime
= Algorithm::Evolutionary::Op::Base->fromXML(
$ezxml
);
is(
$ezprime
->{_eval}(
$pop
[0] ) eq
$ez
->{_eval}(
$pop
[0] ) , 1,
"Code snippets"
);
my
$oldBestFitness
=
$bestIndi
->Fitness();
$ez
->apply( \
@sortPop
);
is(
$sortPop
[0]->Fitness() >=
$oldBestFitness
, 1,
"Fitness improving"
);
my
$g100
= new Algorithm::Evolutionary::Op::GenerationalTerm 10;
my
$f
= new Algorithm::Evolutionary::Op::FullAlgorithm
$generation
,
$g100
;
my
$fxml
=
$f
->asXML();
my
$txml
=
$f
->{_terminator}->asXML();
my
$fprime
= Algorithm::Evolutionary::Op::Base->fromXML(
$fxml
);
is(
$txml
eq
$fprime
->{_terminator}->asXML() , 1,
"from XML"
);
$oldBestFitness
=
$bestIndi
->Fitness();
for
(
@sortPop
) {
if
( !
defined
$_
->Fitness() ) {
my
$fitness
=
$onemax
->(
$_
);
$_
->Fitness(
$fitness
);
}
}
$f
->apply( \
@sortPop
);
is(
$sortPop
[0]->Fitness() >=
$oldBestFitness
, 1,
"Improving fitness"
);