AI-Pathfinding-OptimizeMultiple

 view release on metacpan or  search on metacpan

lib/AI/Pathfinding/OptimizeMultiple/App/CmdLine.pm  view on Meta::CPAN

package AI::Pathfinding::OptimizeMultiple::App::CmdLine;
$AI::Pathfinding::OptimizeMultiple::App::CmdLine::VERSION = '0.0.17';
use strict;
use warnings;

use MooX qw/late/;

use Getopt::Long qw(GetOptionsFromArray);
use IO::File     ();

use AI::Pathfinding::OptimizeMultiple                ();
use AI::Pathfinding::OptimizeMultiple::PostProcessor ();

# TODO : restore later.
# use MyInput;

use Carp ();

has argv             => ( isa => 'ArrayRef[Str]', is => 'ro', required => 1, );
has _arbitrator      => ( is  => 'rw' );
has _add_horne_prune => ( isa => 'Bool',     is => 'rw' );
has _chosen_scans    => ( isa => 'ArrayRef', is => 'rw' );
has _should_exit_immediately =>
    ( isa => 'Bool', is => 'rw', default => sub { 0; }, );
has input_obj_class  => ( isa => 'Str', is => 'rw' );
has _input_obj       => ( is  => 'rw' );
has _is_flares       => ( is  => 'rw',  isa => 'Bool', default => sub { 0; }, );
has _num_boards      => ( isa => 'Int', is  => 'rw' );
has _offset_quotas   => ( isa => 'Int', is  => 'rw' );
has _optimize_for    => ( isa => 'Str', is  => 'rw' );
has _output_filename => ( isa => 'Str', is  => 'rw' );
has _post_processor => (
    isa => 'Maybe[AI::Pathfinding::OptimizeMultiple::PostProcessor]',
    is  => 'rw'
);
has _quotas_are_cb        => ( isa => 'Bool',       is => 'rw' );
has _quotas_expr          => ( isa => 'Maybe[Str]', is => 'rw' );
has _should_rle_be_done   => ( isa => 'Bool',       is => 'rw' );
has _should_trace_be_done => ( isa => 'Bool',       is => 'rw' );
has _simulate_to          => ( isa => 'Maybe[Str]', is => 'rw' );
has _start_board          => ( isa => 'Int',        is => 'rw' );
has _stats_factors =>
    ( isa => 'HashRef', is => 'rw', default => sub { return +{}; }, );

my $_component_re = qr/[A-Za-z][A-Za-z0-9_]*/;
my $_module_re    = qr/$_component_re(?:::$_component_re)*/;

sub BUILD
{
    my $self = shift;

    # Command line parameters
    my $_start_board         = 1;
    my $num_boards           = 32000;
    my $output_filename      = "-";
    my $should_trace_be_done = 0;
    my $should_rle_be_done   = 1;
    my $_quotas_expr         = undef;
    my $quotas_are_cb        = 0;
    my $optimize_for         = "speed";
    my $offset_quotas        = 0;
    my $simulate_to          = undef;
    my $_add_horne_prune     = 0;
    my $input_obj_class = 'AI::Pathfinding::OptimizeMultiple::DataInputObj';
    my %stats_factors;

    my $help = 0;
    my $man  = 0;
    GetOptionsFromArray(
        $self->argv(),
        'help|h'          => \$help,
        man               => \$man,
        "o|output=s"      => \$output_filename,
        "num-boards=i"    => \$num_boards,
        "trace"           => \$should_trace_be_done,
        "rle!"            => \$should_rle_be_done,
        "start-board=i"   => \$_start_board,
        "quotas-expr=s"   => \$_quotas_expr,
        "quotas-are-cb"   => \$quotas_are_cb,
        "offset-quotas"   => \$offset_quotas,
        "opt-for=s"       => \$optimize_for,
        "simulate-to=s"   => \$simulate_to,
        "sprtf"           => \$_add_horne_prune,
        "input-class=s"   => \$input_obj_class,
        "stats-factors=f" => \%stats_factors,
    ) or die "Extracting options from ARGV array failed - $!";

    if ($help)
    {
        $self->_should_exit_immediately(1);
        print <<"EOF";
$0 - optimize a game AI multi-tasking configuration

--help | -h - displays this help screen
--output=[filename] | -o [filename] - output to this file instead of STDOUT.
EOF
        return;
    }

    $self->_start_board($_start_board);
    $self->_num_boards($num_boards);
    $self->_output_filename($output_filename);
    $self->_should_trace_be_done($should_trace_be_done);

lib/AI/Pathfinding/OptimizeMultiple/App/CmdLine.pm  view on Meta::CPAN

        if ($@)
        {
            die "Could not load '$class' - <<$@>>";
        }

        # TODO : Restore later.
        $self->_input_obj(
            $class->new(
                {
                    start_board => $self->_start_board(),
                    num_boards  => $self->_num_boards(),
                }
            )
        );
    }

    $self->_post_processor(
        AI::Pathfinding::OptimizeMultiple::PostProcessor->new(
            {
                do_rle        => $self->_should_rle_be_done(),
                offset_quotas => $self->_offset_quotas(),
            }
        )
    );

    return;
}

sub _selected_scans
{
    my $self = shift;

    return $self->_input_obj->selected_scans();
}

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'} }

lib/AI/Pathfinding/OptimizeMultiple/App/CmdLine.pm  view on Meta::CPAN

    $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>

=item *

CPAN Testers Dependencies

The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.

L<http://deps.cpantesters.org/?module=AI::Pathfinding::OptimizeMultiple>

=back

=head2 Bugs / Feature Requests

Please report any bugs or feature requests by email to C<bug-ai-pathfinding-optimizemultiple at rt.cpan.org>, or through
the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=AI-Pathfinding-OptimizeMultiple>. You will be automatically notified of any
progress on the request by the system.

=head2 Source Code

The code is open to the world, and available for you to hack on. Please feel free to browse it and play
with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
from your repository :)

L<http://github.com/shlomif/fc-solve>

  git clone ssh://git@github.com/shlomif/fc-solve.git

=head1 AUTHOR

Shlomi Fish <shlomif@cpan.org>

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website



( run in 1.348 second using v1.01-cache-2.11-cpan-ceb78f64989 )