Steve Cook > Tk-Taxis-2.03 > Tk::Taxis

Download:
Tk-Taxis-2.03.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
Report a bug
Module Version: 2.03   Source  

NAME ^

Tk::Taxis - Perl extension for simulating biological taxes

SYNOPSIS ^

  use Tk::Taxis;
  my $taxis = $mw->Taxis( -width => 200, -height => 100 )->pack();
  $taxis->configure( -population => 20 );
  $taxis->taxis() while 1;

ABSTRACT ^

Simulates the biological movement called taxis

DESCRIPTION ^

Organisms such as bacteria respond to gradients in chemicals, light, etc, by a process called taxis ('movement'). This module captures some of the spirit of this model of organismal movement. Bacteria are unable to measure differential gradients of chemicals along the length of their cells. Instead, they measure the concentration at a given point, move a little, measure it again, then if they find they are running up a favourable concentration gradient, they reduce their tumbling frequency (the probability that they will randomly change direction). In this way, they effect a random walk that is biased up the gradient.

METHODS

Tk::Taxis is a composite widget, so to invoke a new instance, you need to call it in the usual way...

  my $taxis = $mw->Taxis( -option => value )->pack();
  $taxis->configure ( -option => value );
  my $number = $taxis->cget( -option );

or similar. This widget is based on Frame and implements a Canvas. Configurable options are mostly forwarded to the Canvas subwidget, which be directly accessed by the Subwidget('canvas') method. Options specific to the Tk::Taxis widget are listed below. If you try to pass in values too low or high (as specified below), the module will warn and set a default minimum or maximum instead. These options can be set in the constructor, and get/set by the standard cget and configure methods.

These options can also all be called as similarly named methods. There are also two additional public methods...

Two final methods available are image_height and image_width, which get/set the height and width of the image used as a critter. It is inadvisable to set these, but the Tk::Taxis::Critter class requires read-only access to them.

CAVEATS ^

Those used to writing...

  MainLoop();

in every Tk script should note that because the simulation requires its own event loop within the event loop of the main program, this will not work out of the box. The best solution I have found is this...

  # import some semantics 
  use Tk qw( DoOneEvent DONT_WAIT );
  use Tk::Taxis;
  use Time::HiRes;
  my $mw = new MainWindow;
  my $taxis = $mw->Taxis()->pack();

  # this tells us whether we are running the simulation or not
  my $running = 1;
  
  # minimum refresh rate
  my $refresh = 0.02; # 20 ms
  
  # home-rolled event loop
  while ( 1 )
  {
    my $finish = $refresh + Time::HiRes::time;
    $taxis->taxis() if $running;
    while ( Time::HiRes::time < $finish  )
      # take up some slack time if the loop executes too quickly
    { 
      DoOneEvent( DONT_WAIT );
    }
  }
  
  # arrange for a start/stop Button or similar to invoke this callback
  sub start_toggle
  {
        $running = $running ? 0 : 1;
  }

As every call to taxis involves iterating over the entire population, when that population is small, the iterations occur more quickly than when the population is large. This event loop ensures that small populations do not whizz around madly (as would be the case if we used a simple while loop), whilst ensuring that large populations do not cause the script to hang in deep recursion (as would be the case if we used a timed repeat callback and a default MainLoop()).

SEE ALSO ^

Tk::Taxis::Critter

AUTHOR ^

Steve Cook, <steve@steve.gb.com>

COPYRIGHT AND LICENSE ^

Copyright 2005 by Steve Cook

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.