Algorithm-RandomPointGenerator

 view release on metacpan or  search on metacpan

lib/Algorithm/RandomPointGenerator.pm  view on Meta::CPAN

            print OUTFILE "\n";             
        }
        $oldfirst = $first;
    }
    print OUTFILE "\n";             
    close OUTFILE;                                                 
my $argstring = <<"END";
set hidden3d
splot "$temp_file" with lines
END
    my $plot;
    if (!defined $pause_time) {
        $plot = Graphics::GnuplotIF->new( persist => 1 );
    } else {
        $plot = Graphics::GnuplotIF->new();
    }
#    my $plot = Graphics::GnuplotIF->new(persist => 1);
    $plot->gnuplot_cmd( $argstring );
    $plot->gnuplot_pause( $pause_time ) if defined $pause_time;
}

sub DESTROY {
    my $self = shift;
    my $master_file_basename = basename($self->{_hist_file}, ('.csv', '.dat', '.txt'));
    unlink glob "__temp_$master_file_basename*";
}

sub deep_copy_AoA {
    my $ref_in = shift;
    my $ref_out;
    foreach my $i (0..@{$ref_in}-1) {
        foreach my $j (0..@{$ref_in->[$i]}-1) {
            $ref_out->[$i]->[$j] = $ref_in->[$i]->[$j];
        }
    }
    return $ref_out;
}

# from perl docs:
sub fisher_yates_shuffle {                
    my $arr =  shift;                
    my $i = @$arr;                   
    while (--$i) {                   
        my $j = int rand( $i + 1 );  
        @$arr[$i, $j] = @$arr[$j, $i]; 
    }
}

sub check_for_illegal_params {
    my @params = @_;
    my @legal_params = qw / input_histogram_file
                            bounding_box_file
                            number_of_points
                            how_many_to_discard
                            proposal_density_width
                            y_axis_pos_direction
                            output_hist_bins_along_x
                            command_line_mode
                            debug
                          /;
    my $found_match_flag;
    foreach my $param (@params) {
        foreach my $legal (@legal_params) {
            $found_match_flag = 0;
            if ($param eq $legal) {
                $found_match_flag = 1;
                last;
            }
        }
        last if $found_match_flag == 0;
    }
    return $found_match_flag;
}

1;

=pod

=head1 NAME

Algorithm::RandomPointGenerator -- This module generates a set of random points in a
2D plane according to a user-specified probability distribution that is provided to
the module in the form of a 2D histogram.


=head1 SYNOPSIS

  #  The quickest way to use the module is through the script genRand2D that you'll
  #  find in the examples subdirectory.  You can move this script to any convenient
  #  location in your directory structure.  Call this script as a command-line utility
  #  in the following manner:

  genRand2D  --histfile  your_histogram_file.csv  --bbfile  your_bounding_box_file.csv

  #  where the '--histfile' option supplies the name of the file that contains a 2D
  #  histogram and the option '--bbfile' the name of the file that defines a bounding
  #  box in the XY-plane to which the histogram applies. The module uses the
  #  Metropolis-Hastings algorithm to draw random points from a probability density
  #  function that is approximated by the 2D histogram you supply through the
  #  '--histfile' option. You can also run the command

  genRand2D  --help

  #  for further information regarding these two command-line options.  An invocation
  #  of genRand2D gives you 2000 random points that are deposited in a file whose
  #  name is printed out in the terminal window in which you invoke the genRand2D
  #  command.


  #  The rest of this Synopsis concerns using the module with greater control over
  #  the production and display of random points.  Obviously, the very first thing
  #  you would need to do would be to import the module:

  use Algorithm::RandomPointGenerator;

  #  Next, name the file that contains a 2D histogram for the desired density
  #  function for the generation of random points:

  my $input_histogram_file = "histogram.csv";

  #  Then name the file that defines the bounding box for the random points:

  my $bounding_box_file =  "bounding_box.csv";

  #  Now construct an instance of RandomPointGenerator using a call that, assuming
  #  you wish to set all the constructor options, would look like:

  my $generator = Algorithm::RandomPointGenerator->new(
                            input_histogram_file    => $input_histogram_file,
                            bounding_box_file       => $bounding_box_file,
                            number_of_points        => 2000,
                            how_many_to_discard     => 500,



( run in 2.819 seconds using v1.01-cache-2.11-cpan-d8267643d1d )