AI-Genetic-Pro

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

NAME

    AI::Genetic::Pro - Efficient genetic algorithms for professional
    purpose with support for multiprocessing.

SYNOPSIS

        use AI::Genetic::Pro;
        
        sub fitness {
            my ($ga, $chromosome) = @_;
            return oct('0b' . $ga->as_string($chromosome)); 
        }
        
        sub terminate {
            my ($ga) = @_;
            my $result = oct('0b' . $ga->as_string($ga->getFittest));
            return $result == 4294967295 ? 1 : 0;
        }
        
        my $ga = AI::Genetic::Pro->new(        
            -fitness         => \&fitness,        # fitness function
            -terminate       => \&terminate,      # terminate function
            -type            => 'bitvector',      # type of chromosomes
            -population      => 1000,             # population
            -crossover       => 0.9,              # probab. of crossover
            -mutation        => 0.01,             # probab. of mutation
            -parents         => 2,                # number  of parents
            -selection       => [ 'Roulette' ],   # selection strategy
            -strategy        => [ 'Points', 2 ],  # crossover strategy
            -cache           => 0,                # cache results
            -history         => 1,                # remember best results
            -preserve        => 3,                # remember the bests
            -variable_length => 1,                # turn variable length ON
            -mce             => 1,                # optional MCE support
            -workers         => 3,                # number of workers (MCE)
        );
            
        # init population of 32-bit vectors
        $ga->init(32);
            
        # evolve 10 generations
        $ga->evolve(10);
        
        # best score
        print "SCORE: ", $ga->as_value($ga->getFittest), ".\n";
        
        # save evolution path as a chart
        $ga->chart(-filename => 'evolution.png');
         
        # save state of GA
        $ga->save('genetic.sga');
        
        # load state of GA
        $ga->load('genetic.sga');

DESCRIPTION

    This module provides efficient implementation of a genetic algorithm
    for professional purpose with support for multiprocessing. It was
    designed to operate as fast as possible even on very large populations
    and big individuals/chromosomes. AI::Genetic::Pro was inspired by
    AI::Genetic, so it is in most cases compatible (there are some
    changes). Additionally AI::Genetic::Pro isn't a pure Perl solution, so
    it doesn't have limitations of its ancestor (such as slow-down in the
    case of big populations ( >10000 ) or vectors with more than 33
    fields).

    If You are looking for a pure Perl solution, consider AI::Genetic.

    Speed

      To increase speed XS code is used, however with portability in mind.
      This distribution was tested on Windows and Linux platforms (and
      should work on any other).

      Multicore support is available through Many-Core Engine (MCE). You
      can gain the most speed up for big populations or time/CPU consuming
      fitness functions, however for small populations and/or simple
      fitness function better choice will be single-process version.

      You can get even more speed up if you turn on use of native arrays
      (parameter: native) instead of packing chromosomes into single
      scalar. However you have to remember about expensive memory use in
      that case.

    Memory

      This module was designed to use as little memory as possible. A
      population of size 10000 consisting of 92-bit vectors uses only ~24MB
      (AI::Genetic would use about 78MB). However - if you use MCE - there
      will be bigger memory consumption. This is consequence of necessity
      of synchronization between many processes.

    Advanced options

      To provide more flexibility AI::Genetic::Pro supports many
      statistical distributions, such as uniform, natural, chi_square and
      others. This feature can be used in selection and/or crossover. See
      the documentation below.

METHODS

    $ga->new( %options )

      Constructor. It accepts options in hash-value style. See options and

README  view on Meta::CPAN

      returned and if the returned chromosomes should be unique. See
      example below.

          # only one - the best
          my ($best) = $ga->getFittest;
      
          # or 5 bests chromosomes, NOT unique
          my @bests = $ga->getFittest(5);
      
          # or 7 bests and UNIQUE chromosomes
          my @bests = $ga->getFittest(7, 1);

      If you want to get a large number of chromosomes, try to use the
      getFittest_as_arrayref function instead (for efficiency).

    $ga->getFittest_as_arrayref($n, $unique)

      This function is very similar to getFittest, but it returns a
      reference to an array instead of a list.

    $ga->generation()

      Get the number of the current generation.

    $ga->people()

      Returns an anonymous list of individuals/chromosomes of the current
      population.

      IMPORTANT: the actual array reference used by the AI::Genetic::Pro
      object is returned, so any changes to it will be reflected in $ga.

    $ga->chromosomes()

      Alias for people.

    $ga->chart(%options)

      Generate a chart describing changes of min, mean, and max scores in
      your population. To satisfy your needs, you can pass the following
      options:

      -filename

	File to save a chart in (obligatory).

      -title

	Title of a chart (default: Evolution).

      -x_label

	X label (default: Generations).

      -y_label

	Y label (default: Value).

      -format

	Format of values, like sprintf (default: '%.2f').

      -legend1

	Description of min line (default: Min value).

      -legend2

	Description of min line (default: Mean value).

      -legend3

	Description of min line (default: Max value).

      -width

	Width of a chart (default: 640).

      -height

	Height of a chart (default: 480).

      -font

	Path to font (in *.ttf format) to be used (default: none).

      -logo

	Path to logo (png/jpg image) to embed in a chart (default: none).

      For example:

                $ga->chart(-width => 480, height => 320, -filename => 'chart.png');

    $ga->save($file)

      Save the current state of the genetic algorithm to the specified
      file.

    $ga->load($file)

      Load a state of the genetic algorithm from the specified file.

    $ga->as_array($chromosome)

      In list context return an array representing the specified
      chromosome. In scalar context return an reference to an array
      representing the specified chromosome. If variable_length is turned
      on and is set to level 2, an array can have some undef values. To get
      only not undef values use as_array_def_only instead of as_array.

    $ga->as_array_def_only($chromosome)

      In list context return an array representing the specified
      chromosome. In scalar context return an reference to an array
      representing the specified chromosome. If variable_length is turned
      off, this function is just an alias for as_array. If variable_length
      is turned on and is set to level 2, this function will return only
      not undef values from chromosome. See example below:

          # -variable_length => 2, -type => 'bitvector'



( run in 1.000 second using v1.01-cache-2.11-cpan-437f7b0c052 )