AI-SimulatedAnnealing
view release on metacpan or search on metacpan
t/annealing_tests.t view on Meta::CPAN
#!/usr/bin/perl
####
# annealing_tests.t: Test the AI::SimulatedAnnealing module.
#
# Usage:
#
# perl -w annealing_tests.t market_distances.csv
####
use 5.010001;
use strict;
use warnings;
use utf8;
use English "-no_match_vars";
use List::Util ("max", "min");
use AI::SimulatedAnnealing;
use Text::BSV::BsvFileReader;
# Probability enumeration:
package Probability;
our $ONE_FIFTH = 5;
our $ONE_FOURTH = 4;
our $ONE_THIRD = 3;
our $ONE_HALF = 2;
# Redeclaration of the main package:
package main;
# Constants:
my $POUND = "#";
my $SQ = "'";
my $DQ = "\"";
my $SEMICOLON = ";";
my $CR = "\r";
my $LF = "\n";
my $SPACE = " ";
my $EMPTY = "";
my $TRUE = 1;
my $FALSE = 0;
my $CYCLES_PER_TEMPERATURE = 1_250;
# Main script:
# Get the input file path:
my $bsv_file_path = $ARGV[0];
unless (scalar @ARGV) {
die "ERROR: No command-line argumment. Please provide the path to a "
. "valid BSV (or simple CSV) file containing market distances.\n";
} # end unless
# Create a reader for the BSV file:
my $bsv_file_reader;
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
for my $p (2..5) {
$mapped_distances[$p] = [];
} # next $p
unless ($field_names->[0] eq "Time"
&& $field_names->[1] =~ /$Probability::ONE_FIFTH\z/s
&& $field_names->[2] =~ /$Probability::ONE_FOURTH\z/s
&& $field_names->[3] =~ /$Probability::ONE_THIRD\z/s
&& $field_names->[4] =~ /$Probability::ONE_HALF\z/s) {
die "ERROR: The input file does not contain market-distance data in "
. "the expected format.\n";
} # end unless
while ($bsv_file_reader->has_next()) {
my $record;
my $dex;
eval {
$record = $bsv_file_reader->get_record();
};
( run in 2.484 seconds using v1.01-cache-2.11-cpan-d8267643d1d )