AI-ANN
view release on metacpan or search on metacpan
t/03_evolver_basic.t view on Meta::CPAN
# change 'tests => 1' to 'tests => last_test_to_print';
use Test::More tests => 60;
BEGIN { use_ok('AI::ANN');
use_ok('AI::ANN::Evolver'); };
#########################
# Insert your test code below, the Test::More module is use()ed here so read
# its man page ( perldoc Test::More ) for help writing this test script.
# This test basically works by crossing a network with itself and making sure nothing changes
$network1=new AI::ANN ('inputs'=>1, 'data'=>[{ iamanoutput => 1, inputs => {0 => 1}, neurons => {}}]);
$network2=new AI::ANN ('inputs'=>1, 'data'=>[{ iamanoutput => 1, inputs => {0 => 1}, neurons => {}}]);
ok(defined $network1, "new() works");
ok($network1->isa("AI::ANN"), "Right class");
ok(defined $network2, "new() works");
ok($network2->isa("AI::ANN"), "Right class");
ok($out=$network1->execute([1]), "executed and still alive");
is($#{$out}, 0, "execute() output for a single neuron is the right length before crossover");
is($out->[0], 1, "execute() output for a single neuron has the correct value before crossover");
ok($out=$network2->execute([1]), "executed and still alive");
is($#{$out}, 0, "execute() output for a single neuron is the right length before crossover");
is($out->[0], 1, "execute() output for a single neuron has the correct value before crossover");
$evolver=new AI::ANN::Evolver ({});
ok(defined $evolver, "new() works");
ok($evolver->isa("AI::ANN::Evolver"), "Right class");
$network3=$evolver->crossover($network1, $network2);
ok(defined $network3, "crossover() works");
ok($network3->isa("AI::ANN"), "Right class");
ok($out=$network3->execute([1]), "executed and still alive");
is($#{$out}, 0, "execute() output for a single neuron is the right length after crossover");
is($out->[0], 1, "execute() output for a single neuron has the correct value after crossover");
($inputs, $neurons, $outputs) = $network3->get_state();
is($#{$inputs}, 0, "get_state inputs is correct length");
is($inputs->[0], 1, "get_state inputs returns correct element 0");
is($#{$neurons}, 0, "get_state neurons is correct length");
is($neurons->[0], 1, "get_state neurons returns correct element 0");
is($#{$outputs}, 0, "get_state outputs is correct length");
is($outputs->[0], 1, "get_state outputs returns correct element 0");
# Now we'll do it bigger!
$evolver=new AI::ANN::Evolver ({mutation_chance => 0.5,
mutation_amount => 0.2, add_link_chance => 0.2,
kill_link_chance => 0.2, sub_crossover_chance =>
0.2, min_value => 0, max_value => 4});
$network1=new AI::ANN ('inputs'=>1,
'data'=>[{ iamanoutput => 0, inputs => {0 => 2}, neurons => {}, eta_inputs => {0 => 0.5}, eta_neurons => {}},
{ iamanoutput => 0, inputs => {0 => 1}, neurons => {0 => 1}, eta_inputs => {0 => 0.3}, eta_neurons => {0 => 0.7}},
{ iamanoutput => 1, inputs => {}, neurons => {0 => 2, 1 => 1}, eta_inputs => {}, eta_neurons => {0 => 0.4, 1 => 0.6}},
{ iamanoutput => 1, inputs => {}, neurons => {0 => 3}, eta_inputs => {}, eta_neurons => {0 => 0.8}}]);
$network2=new AI::ANN ('inputs'=>1,
'data'=>[{ iamanoutput => 0, inputs => {0 => 1}, neurons => {}},
{ iamanoutput => 0, inputs => {0 => 2}, neurons => {0 => 2}},
( run in 0.924 second using v1.01-cache-2.11-cpan-39bf76dae61 )