AI-Pathfinding-OptimizeMultiple
view release on metacpan or search on metacpan
lib/AI/Pathfinding/OptimizeMultiple/App/CmdLine.pm view on Meta::CPAN
sub _map_all_but_last
{
my $self = shift;
my ( $cb, $arr_ref ) = (@_);
return [
( map { $cb->($_) } @$arr_ref[ 0 .. $#$arr_ref - 1 ] ),
$arr_ref->[-1]
];
}
sub _get_quotas
{
my $self = shift;
if ( $self->_quotas_are_cb() )
{
return scalar( eval( $self->_quotas_expr() ) );
}
elsif ( defined( $self->_quotas_expr() ) )
{
return [ eval $self->_quotas_expr() ];
}
else
{
return $self->_get_default_quotas();
}
}
sub _get_default_quotas
{
return [ (350) x 5000 ];
}
sub _get_script_fh
{
my $self = shift;
return IO::File->new(
( $self->_output_filename() eq "-" )
? ">&STDOUT"
: ( $self->_output_filename(), "w" )
);
}
sub _get_script_terminator
{
return "\n\n\n";
}
sub _out_script
{
my $self = shift;
my $cmd_line_string = shift;
$self->_get_script_fh()
->print( $cmd_line_string,
$self->_get_script_terminator($cmd_line_string) );
}
sub _get_line_of_command
{
my $self = shift;
my $args_string = join( " ",
$self->_start_board(),
$self->_start_board() + $self->_num_boards() - 1, 1 );
return "freecell-solver-range-parallel-solve $args_string";
}
sub _line_ends_mapping
{
my $self = shift;
return $self->_map_all_but_last( sub { "$_[0] \\\n" }, shift );
}
sub _get_used_scans
{
my $self = shift;
return [ grep { $_->is_used() } @{ $self->_selected_scans() } ];
}
sub _get_scan_line
{
my ( $self, $line ) = @_;
return
$line->{'cmd_line'}
. " -step 500 "
. join( " ",
map { $_, $line->{'id'} }
( "--st-name", ( $self->_is_flares() ? "--flare-name" : () ) ) );
}
sub _get_lines_of_scan_defs
{
my $self = shift;
return [ map { $self->_get_scan_line($_) } @{ $self->_get_used_scans() } ];
}
sub _scan_def_line_mapping
{
my ( $self, $lines_aref ) = @_;
return $self->_map_all_but_last(
sub {
my ($line) = @_;
return $line . ' ' . ( $self->_is_flares() ? "-nf" : "-nst" );
},
[
map {
my $line = $_;
# Add the -sp r:tf flag to each scan if specified - it enhances
# performance, but timing the scans with it makes the total
# scan sub-optimal.
if ( $self->_add_horne_prune() )
{
$line =~ s/( --st-name)/ -sp r:tf$1/;
}
$line;
} @$lines_aref
],
);
}
sub _calc_iter_quota
{
my $self = shift;
my $quota = shift;
if ( $self->_offset_quotas() )
{
return $quota + 1;
}
else
{
return $quota;
}
}
sub _map_scan_idx_to_id
{
my $self = shift;
my $index = shift;
return $self->_selected_scans()->[$index]->id();
}
sub _format_prelude_iter
{
my $self = shift;
my $iter = shift;
return
( $self->_is_flares() ? "Run:" : "" )
. $iter->iters() . '@'
. $self->_map_scan_idx_to_id( $iter->scan_idx() );
}
sub _get_line_of_prelude
{
my $self = shift;
return
+( $self->_is_flares() ? "--flares-plan" : "--prelude" ) . qq{ "}
. join( ",",
map { $self->_format_prelude_iter($_) } @{ $self->_chosen_scans() } )
. "\"";
}
sub _calc_script_lines
{
my $self = shift;
return [
$self->_get_line_of_command(),
@{
$self->_scan_def_line_mapping( $self->_get_lines_of_scan_defs() )
},
$self->_get_line_of_prelude()
];
}
sub _calc_script_text
{
my $self = shift;
return join( "",
@{ $self->_line_ends_mapping( $self->_calc_script_lines() ) } );
}
sub _write_script
{
my $self = shift;
$self->_out_script( $self->_calc_script_text() );
}
sub _calc_scans_iters_pdls
{
my $self = shift;
my $method = (
( $self->_optimize_for() =~ m{len} )
? "get_scans_lens_iters_pdls"
: "get_scans_iters_pdls"
);
return $self->_input_obj->$method();
}
sub _arbitrator_trace_cb
{
my $args = shift;
printf( "%s \@ %s (%s solved)\n",
@$args{qw(iters_quota selected_scan_idx total_boards_solved)} );
}
sub _init_arbitrator
{
my $self = shift;
return $self->_arbitrator(
AI::Pathfinding::OptimizeMultiple->new(
{
'scans' => [
map { +{ name => $_->id() } }
@{ $self->_input_obj->_suitable_scans_list() },
],
'quotas' => $self->_get_quotas(),
'selected_scans' => $self->_selected_scans(),
'num_boards' => $self->_num_boards(),
'scans_iters_pdls' => $self->_calc_scans_iters_pdls(),
'trace_cb' => \&_arbitrator_trace_cb,
'optimize_for' => $self->_optimize_for(),
'stats_factors' => $self->_stats_factors(),
}
lib/AI/Pathfinding/OptimizeMultiple/App/CmdLine.pm view on Meta::CPAN
if ( defined( $self->_simulate_to() ) )
{
$self->_real_do_simulation();
}
return;
}
sub run
{
my $self = shift;
if ( $self->_should_exit_immediately() )
{
return 0;
}
$self->_init_arbitrator();
$self->_arbitrator_process();
$self->_report_total_iters();
$self->_write_script();
$self->_do_trace();
$self->_do_simulation();
return 0;
}
sub run_flares
{
my $self = shift;
$self->_optimize_for("len");
$self->_is_flares(1);
$self->_init_arbitrator();
$self->_arbitrator()->calc_flares_meta_scan();
my $scans =
$self->_post_processor->process( $self->_arbitrator->chosen_scans() );
$self->_chosen_scans($scans);
$self->_report_total_iters();
$self->_write_script();
$self->_do_trace();
$self->_do_simulation();
return 0;
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
AI::Pathfinding::OptimizeMultiple::App::CmdLine - the command line application class.
=head1 VERSION
version 0.0.17
=head1 SUBROUTINES/METHODS
=head2 $self->run()
For internal use.
=head2 $self->run_flares()
For internal use.
=head2 $self->argv()
An array ref of command line arguments.
=head2 $self->input_obj_class()
The class to handle the input data - by default -
L<AI::Pathfinding::OptimizeMultiple::DataInputObj>.
=head2 BUILD()
Moo leftover. B<INTERNAL USE>.
=for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
=head1 SUPPORT
=head2 Websites
The following websites have more information about this module, and may be of help to you. As always,
in addition to those websites please use your favorite search engine to discover more resources.
=over 4
=item *
MetaCPAN
A modern, open-source CPAN search engine, useful to view POD in HTML format.
L<https://metacpan.org/release/AI-Pathfinding-OptimizeMultiple>
=item *
RT: CPAN's Bug Tracker
The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
L<https://rt.cpan.org/Public/Dist/Display.html?Name=AI-Pathfinding-OptimizeMultiple>
=item *
CPANTS
The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
L<http://cpants.cpanauthors.org/dist/AI-Pathfinding-OptimizeMultiple>
=item *
CPAN Testers
The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
L<http://www.cpantesters.org/distro/A/AI-Pathfinding-OptimizeMultiple>
=item *
CPAN Testers Matrix
The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
L<http://matrix.cpantesters.org/?dist=AI-Pathfinding-OptimizeMultiple>
( run in 0.769 second using v1.01-cache-2.11-cpan-2e0ccfb7a10 )