AI-ANN
view release on metacpan or search on metacpan
lib/AI/ANN/Evolver.pm view on Meta::CPAN
$weight += (rand() * 2 - 1) * $self->{'mutation_amount'};
if ($weight > $self->{'max_value'}) {
$weight = $self->{'max_value'};
}
if ($weight < $self->{'min_value'}) {
$weight = $self->{'min_value'} + 0.000001;
}
}
if (abs($weight) < $self->{'mutation_amount'}) {
if (rand() < $self->{'kill_link_chance'}) {
$weight = undef;
}
}
} else {
if (rand() < $self->{'add_link_chance'}) {
$weight = rand() * $self->{'mutation_amount'};
# We want to Do The Right Thing. Here, that means to
# detect whether the user is using weights in (0, x), and
# if so make sure we don't accidentally give them a
# negative weight, because that will become 0.000001.
# Instead, we'll generate a positive only value at first
lib/AI/ANN/Evolver.pm view on Meta::CPAN
$weight += (rand() * 2 - 1) * $self->{'mutation_amount'};
if ($weight > $self->{'max_value'}) {
$weight = $self->{'max_value'};
}
if ($weight < $self->{'min_value'}) {
$weight = $self->{'min_value'} + 0.000001;
}
}
if (abs($weight) < $self->{'mutation_amount'}) {
if (rand() < $self->{'kill_link_chance'}) {
$weight = undef;
}
}
} else {
if (rand() < $self->{'add_link_chance'}) {
$weight = rand() * $self->{'mutation_amount'};
# We want to Do The Right Thing. Here, that means to
# detect whether the user is using weights in (0, x), and
# if so make sure we don't accidentally give them a
# negative weight, because that will become 0.000001.
lib/AI/ANN/Neuron.pm view on Meta::CPAN
zero-indexed.
eta_inputs and eta_neurons are optional, required only if you wish to use the
Gaussian mutation in AI::ANN::Evolver.
=head2 ready
$neuron->ready( [$input0, $input1, ...], [$neuronvalue0, ...] )
All inputs must be provided or you're insane.
If a neuron is not yet available, make it undef, not zero.
Returns 1 if ready, 0 otherwise.
=head2 execute
$neuron->execute( [$input0, $input1, ...], {$neuronid => $neuronvalue, ...} )
You /must/ pass the correct number of inputs and neurons, and undefined values
/must/ be zeros, not undef.
Returns raw value (linear potential)
=head1 AUTHOR
Dan Collins <DCOLLINS@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2011 by Dan Collins.
t/01_neuron_basic.t view on Meta::CPAN
is($neuron->ready([2, 0, 1], {}), 1, "Ready - extra inputs");
is($neuron->execute([2, 0, 1], {}), 2, "Execute - extra inputs");
is($neuron->ready([1], {0 => 2, 2 => 4}), 1, "Ready - extra neurons");
is($neuron->execute([1], {0 => 2, 2 => 4}), 1, "Execute - extra neurons");
$neuron = new AI::ANN::Neuron(0, {0 => 1, 1 => 3}, {1 => 3, 2 => 4});
ok(defined $neuron, "new() works on a more complex neuron");
ok($neuron->isa("AI::ANN::Neuron"), "Right class");
is($neuron->ready([1, 2], [undef, 1, 3]), 1, "Ready - complex case 1");
is($neuron->execute([1, 2], [0, 1, 3]), 22, "Execute - complex case 1");
is($neuron->ready([1, 2], [0, 1]), 0, "Not ready - complex case 2");
is($neuron->ready([2], [0, 1, 3]), 0, "Not ready - complex case 3");
( run in 4.007 seconds using v1.01-cache-2.11-cpan-d80b1682f3f )