swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Swarm-Support] Perl


From: Paul Johnson
Subject: Re: [Swarm-Support] Perl
Date: Tue, 10 Feb 2004 13:29:28 -0600
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5) Gecko/20031018

I attach a note that another user submitted to me, along with his Perl program.

Since the UmExpTools syntax is rather similar to the javaswarm syntax, I expect 
it might work.

I wish I had a version of the replicator script that built in these different styles as 
options.  In my original version, I had the long form parameter --X=x and the short form 
-Xx, and if the "rparameter form" were a third option, like -Dx=x, it would be 
good.  Luke Premo's adaption replaces the old Swarm style shortform with the new 
rparameter form.  He says it works in Windows.

Dr. Johnson,

Thanks for your tips about my 'replicator problems'.  With a bit of
perseverance I was able to edit your replicator.pl code so that it
works with a (windows) swarm model that makes use of the RParameters
object from the UM-ExpTools lib. I've attached the resulting file
(replicatorR.pl) so that you can look over the changes if you so
desire.  if you think it would be of use to anyone else, feel free to
post it on your site with the other two versions.  Thanks again for
your help, it guided me in the right direction.  Now I am free to move
on to data collection!

Luke Premo



Li An wrote:

Hi,
Paul gave a perl example in http://lark.cc.ukans.edu/~pauljohn/Swarm/MySwarmCode/replicator.pl, which is for Obj-C Swarm, where the command-line without using perl is like:
./ -b --run=1 --seed=1234 --numPPL=110 --vision=2 --aRebConstPOM=0.1
In this case, it can be adapted to the perl running.How about a java-swarm (in batch mode) where the command line is like:
javaswarm -Dpara1=90 -Dpara2=0.2 StartHeatbugs -b?
How to modify the script and run the java-swarm? Thanks

Li An


_______________________________________________
Support mailing list
address@hidden
http://www.swarm.org/mailman/listinfo/support



--
Paul E. Johnson                       email: address@hidden
Dept. of Political Science            http://lark.cc.ku.edu/~pauljohn
1541 Lilac Lane, Rm 504 University of Kansas Office: (785) 864-9086
Lawrence, Kansas 66044-3177           FAX: (785) 864-5700

#!/usr/bin/perl -w
use strict;
use Getopt::Long;  # command line processing

#Paul Johnson
# June 16, 2001
# modified by Luke Premo to work with Swarm models that make use of RParameters
# January 21, 2004 
# replicatorR.pl has been tested in a Windows XP environment only

# WHAT IS THIS FOR?

# I just learned Perl, and because people having no Perl knowledge
# have difficulty writing their own scripts to manage parameter sweeps
# and repetition of simulations, I offer this!  It
# runs a program over and over and passes various command line
# options to it.  You can sweep through AS MANY PARAMETERS AS YOU WANT
# with AS MANY RUNS AS YOU WANT per setting.

# IMPORTANT NOTE: replicatorR.pl will work ONLY for Swarm models that make use 
of 
# the object 'RParameters' from the UM-ExpTools-4 library.  You will need to 
use prior versions,
# replicator.pl or replicator2.pl, if your model does NOT make use of 
RParameters

# HOW DO I USE IT?

# You can give command line arguments or edit the CONFIGURATION
# section below.  Either way should end up the same.  Either way, you
# give the name of your program, the number of runs you want for each
# setting, and the parameters you want to sweep.  If you enter the
# values in the CONFIGURATION section below, you just get into a swarm
# terminal and type

# perl replicatorR.pl 

# Otherwise, you pass command line arguments, as in (type all this on
# one line!)

# perl replicatorR.pl --program=share --directory='/Documents\ and\ 
Settings/Anthropology/Desktop/foodSHARE_3' 
# --NRUNS=1 --sweep gapSize=1,2 --sweep patchSize=1,2  --sweep 
foodShareType=0,1,2

# And the thing should start working.

# This sets the gapSize variable at one of two possible values (1 or 2), and 
then two possible
# values for our variable patchSize, and then one of 3 values for 
# foodShareType.  That means this script runs a total of 12 experiments,
# corresponding to these values:

# -foodShareType=0 -gapSize=1 -patchSize=1
# -foodShareType=0 -gapSize=1 -patchSize=2
# -foodShareType=0 -gapSize=2 -patchSize=1
# -foodShareType=0 -gapSize=2 -patchSize=2
# -foodShareType=1 -gapSize=1 -patchSize=1
# -foodShareType=1 -gapSize=1 -patchSize=2
# -foodShareType=1 -gapSize=2 -patchSize=1
# -foodShareType=1 -gapSize=2 -patchSize=2
# -foodShareType=2 -gapSize=1 -patchSize=1
# -foodShareType=2 -gapSize=1 -patchSize=2 
# -foodShareType=2 -gapSize=2 -patchSize=1
# -foodShareType=2 -gapSize=2 -patchSize=2

# Or if you use the "shortform" arguments to your program (in RParameters, 
short means
# one dash and an equal sign, as in -GS=1, note that replicatorR.pl will insert 
the required 'D')
# so the shortform version of the example above would look like this:

# perl replicatorR.pl --program=share --directory='/Documents\ and\ 
Settings/Anthropology/Desktop/foodSHARE_3'
#    --NRUNS=1 --shortform --sweep GS=1,2  --sweep PS=1,2  --sweep FST=0,1,2

# In short or long form, the key is the "sweep" argument, which you
# repeat for each parameter you want to examine. You can put one or
# more comma-separated arguments to sweep various combinations.
# If you don't specify --directory, it looks in the current working
# directory.  If you don't specify NRUNS, it runs the program 1 time,
# or whatever number is set in the CONFIGURATION section below.


# WHY NOT USE DRONE?
# I love Drone for this kind of work, but Windows users around here
# have a hell of a time compiling expect, and we don't need all the drone
# features for networking and such, so we needed an alternative.  

# WHAT IS THE MEANING OF "EXPERIMENT" AND "RUN"

# I use the drone terminology here.  An "experiment" is a set of runs
# for a given set of parameters.  Every time you run this script, it
# runs at least one experiment.  A run is an execution of your program
# with a certain set of parameters.  You can have several runs of a
# program at a certain set of parameters, the only thing that changes
# is the random number seed that is fed into the program.

# Each time you run this script, by typing "perl runRB.pl", it should
# create a subdirectory exp-1, or if that exists, exp-2, and so forth.
# Inside there, you should see one directory for each parameter
# setting you select (see below).  You have the responsibility of
# creating output files with the run number in them if you want
# separate records for each run of the program.  It will create new 
# experiment directories every time it runs, but if your program
# writes on top of its old files when it repeats itself, it is 
# your own fault.

# HOW TO PREPARE YOUR SWARM PROGRAM
# The assumption is that you already have a swarm batch program that runs with
# command line arguments at the terminal prompt. Suppose you can run a program 
by typing its
# name and a bunch of command line options, such as 

# ./share -b -Drun=1 -Dseed=1234 -DgapSize=1 -DpatchSize=2 -DfoodShareType=0 

# or the short argument form

# ./rb -b -DR=1 -DS=1234 -DGS=1 -DPS=2 -DFST=0

# -b is for batch, Swarm creates that flag. You create the rest within
# your Swarm arguments code. The 'D' is required by RParameters.

# Any program you use with this script MUST have at least 2 command
# line arguments:

# 1. Run number.  Short form -R=x     (which becomes -DR=x for RParameters)
#                  Long form -run=x   (which becomes -Drun=x for RParameters)

# 2. Seed Value.  Short form -S=x     (which becomes -DS=x for RParameters)
#                  Long form -seed=x  (which becomes -Dseed=x for RParameters)

# This script will take care of the seeds, replication, and so forth,
# but your program must be able to accept the input.  The script will
# create directories, and any output generated by your runs will be
# sorted into them.  You are rendered almost superfluous by its mighty
# power!  (not really...)

# Now, about the random number seeds.  I started the Perl random
# number generator with the number 1234321, you can change the seed
# number there if you want.  Then the script chooses random numbers
# from that stream as the seeds for following runs.


# *************CONFIGURATION*****************

# If you don't use command line optinon, HERE IS THE PART YOU EDIT:

# Give a full path to the directory that holds your program. 
# If you don't specify anything, or leave this blank, then the script
# assumes the directory is your current working directory. If 
# you are in MS-Windows, you must figure out 
# what your bash environment needs for paths by typing "pwd" in your
# terminal.  You will need to use "\" if you have spaces in your directories
# (see example directory name below).

# Edit the following settings if you care to:

# Lets assume your program is in the current directory, the one where
# you plan to run this script:
#my $dir_name = ".";

# That's not necessary, you can put your program wherever you want
# as long as you tell the script, so:
my $dir_name = '/Documents\ and\ Settings/Anthropology/Desktop/foodSHARE_3';

# What is the name of your program:
my $prog_name= 'share';

# How many runs do you want for each set of parameters.
my $NRUNS=1;

# Put in your command line parameters of interest here!  Here is an
# example, assuming you want to pass through the "long form" command
# line arguments.  Those are the ones like --argument=value. Observe the
# structure of a line below. You need a command line parameter in long form
# (the kind that goes with -- in the command), the => symbol, and
# bracketed parameter values to be swept.  If you put just one value,
# it just uses that one value.  
#my %parameters = (
#              gapSize =>   [1, 2],
#              patchSize  =>   [1, 2],
#              foodShareType  =>   [0, 1, 2]
#              );


# I personally like longform, but you may like shortform arguments.
# If you want shortform arguments, change this variable $shortform to 1:

#my $shortform = 0;
my $shortform = 1;

# and then give a %parameters statement like:
my %parameters = (
               GS  =>    [1, 2],
               PS  =>    [1, 2],
               FST  =>   [0, 1, 2]
               );



# You don't really need to mess around below here. I think
# you should leave it alone, as a matter of fact.
###################################################




my @command_strings = "";

&main ();

sub main {
    &processCLI();
    
    foreach my $parameter ( keys %parameters ) {
        @command_strings = &updateCommandStrings( $parameters{$parameter}, 
$parameter ); 
    }

    my $expSuperDir = "";
    $expSuperDir = &createSuperDirectoryName();

 

    foreach my $string (@command_strings) {
        #beautify the string for directory creation purposes
        my $stringNoSpaces = $string;
        $stringNoSpaces =~ s/\ //g;
        $stringNoSpaces =~ s/--/-/g;
        my $fullPath = "$expSuperDir/$prog_name$stringNoSpaces";
        mkdir ($fullPath,0777);
        chdir ($fullPath);
        
        if ($dir_name eq ".") { $dir_name = $ENV{PWD};}
        my $program = "$dir_name/$prog_name";

        open(COMMANDS, ">>experiment_summary");
        srand 1234321;
        my $seedValue = 0;

        for (my $i=0; $i < $NRUNS; $i++) {
             my $progString;
             $seedValue += int (rand 100000) + 1;
             if ($shortform ==1) {
                 $progString = join("",$program," -b"," -DR=",$i," 
-DS=",$seedValue," ", $string);
             }
             else {
                 $progString = join("",$program," -b"," -Drun=",$i," 
-Dseed=",$seedValue," ", $string);
             }
                 
               print COMMANDS "$progString \n";

             print "Now trying to run = $progString \n";
             my $outputFile = "stdout$i";
             #diverts stderr to a file
             my $output = `$progString 2>&1 >> $outputFile`;
             print $output;
            
         }

        close(COMMANDS);
        chdir ($ENV{PWD});
    }
}


sub processCLI {
    my %clinput = ();

    GetOptions ('NRUNS:i' => \$NRUNS, 'directory:s' => \$dir_name, 'program:s' 
=> \$prog_name , 'shortform' => \$shortform, "sweep=s" => \%clinput) || die 
"Invalid arguments \n";

    print "Your program is in directory: $dir_name \n";
    print "Your program names is: $prog_name \n";
    print "Your desired number of runs is: $NRUNS \n";
    print "Your command line input parameters: \n";
    if ($shortform == 1){print "You said you were using short form parameters 
\n"};

    foreach (keys %clinput) { print "$_ $clinput{$_} \n";}

    if (%clinput) {
        print "We are using your command line input for parameters \n";
        %parameters = ();
        foreach (keys %clinput) { 
            my @arr = split(",",$clinput{$_});
            address@hidden;
        }
    }
}


sub updateCommandStrings {
  
    my @valArray = @{$_[0]};
    my $key = $_[1];
 

   # defined or $_ = " " foreach @{$local_strings};
   # @local_strings = @{$local_strings};

    
    my @oldStringArray = @command_strings;
    
    @command_strings=();
    
    foreach my $st (@oldStringArray){
        foreach my $val (@valArray){
            my $newString;
            if ($shortform == 0) {
                $newString = join("",$st," -D",$key,"=",$val);
            }
            else {
                $newString = join("",$st," -D",$key,"=",$val);
            }
            push (@command_strings, $newString);
        }
    }
    return @command_strings;
}

sub createSuperDirectoryName {
    my $dirName = "exp-1";
    my $i=1;
  
    while (-e $dirName ){
        $i++;
        $dirName = join ("-","exp",$i);
    }
    mkdir ($dirName,0777);
    return $dirName
}


exit;

reply via email to

[Prev in Thread] Current Thread [Next in Thread]