AI-SimulatedAnnealing
view release on metacpan or search on metacpan
t/annealing_tests.t view on Meta::CPAN
eval {
$bsv_file_reader = Text::BSV::BsvFileReader->new($bsv_file_path);
};
if ($EVAL_ERROR) {
my $exception = $EVAL_ERROR;
given ($exception->get_type()) {
when ($Text::BSV::Exception::FILE_NOT_FOUND) {
say STDERR "$DQ$bsv_file_path$DQ is not a valid file path.";
exit(1);
}
when ($Text::BSV::Exception::IO_ERROR) {
say STDERR "Couldn't open $DQ$bsv_file_path$DQ for reading.";
exit(1);
}
when ($Text::BSV::Exception::INVALID_DATA_FORMAT) {
say STDERR "Invalid BSV data: " . $exception->get_message();
exit(1);
}
default {
say STDERR $exception->get_message();
exit(1);
} # end when
} # end given
} # end if
# Generate a list of distances for each probability from the data in the
# BSV file:
my $field_names = $bsv_file_reader->get_field_names();
my @mapped_distances; # indexes 2-5 = Probability constants;
# values = references to number arrays
t/annealing_tests.t view on Meta::CPAN
{"LowerBound" => 0.0, "UpperBound" => 3.0, "Precision" => 3};
push @number_specs,
{"LowerBound" => -1.0, "UpperBound" => 5.0, "Precision" => 3};
push @number_specs,
{"LowerBound" => -4.0, "UpperBound" => 0.0, "Precision" => 3};
$optimized_coefficients = anneal(
\@number_specs, $cost_function, $CYCLES_PER_TEMPERATURE);
# Print the results for this probability to the console:
say "\nProbability: 1/$p";
printf("Coefficients: a = %1.3f; b = %1.3f; c= %1.3f\n",
$optimized_coefficients->[0],
$optimized_coefficients->[1],
$optimized_coefficients->[2]);
say "Cost: " . $cost_function->($optimized_coefficients);
} # next $p
# Perform an annealing test with integers that triggers brute-force analysis
# and uses an anonymous cost function that minimizes this sum:
#
# (10 * abs(23 - val)) + (the total range of a, b, and c)
#
# where "val" is the result of following expression:
#
# (a * (x ** 2)) + bx + c
t/annealing_tests.t view on Meta::CPAN
$abc = anneal(\@number_specs,
sub {
my $nums = $_[0];
my $range = max(@{ $nums }) - min(@{ $nums });
my $val = ($nums->[0] * 9) + ($nums->[1] * 3) + $nums->[2];
my $cost = $range + (10 * abs(23 - $val));
return $cost;
}, 120);
say "\nHere are a, b, and c: " . $abc->[0] . ", "
. $abc->[1] . ", " . $abc->[2];
# Helper functions:
# The cost_function_factory() takes a reference to an array containing
# real-world market distances and returns a reference to a cost function.
# The cost function takes a reference to an array of three coefficients,
# and returns the mean absolute percentage deviation of the calculated
# results from the real-world results based on this formula:
#
( run in 1.128 second using v1.01-cache-2.11-cpan-5b529ec07f3 )