The Pyevolve Interactive Mode on Mac OS

The Interactive Mode of Pyevolve is the only part of Pyevolve which is platform dependant, I’ve created the platform dependant code for Win and Linux, but not yet for Mac, I’m working on that issue for the next release 0.6 of Pyevolve, but there is a way to enter in the Interactive Mode on the Mac, it’s a workaround for a while, this is the code:

from pyevolve import G1DList
from pyevolve import GSimpleGA
from pyevolve import Selectors
import pyevolve
import code

# The generation that Pyevolve will enter on
# the interactive mode

def evolve_callback(ga_engine):
   """ The callback function to enter on interactive mode"""
   generation = ga_engine.getCurrentGeneration()

   if generation == INTERACTIVE_STOP:
      from pyevolve import Interaction
      interact_banner = "## Pyevolve v.%s - Interactive Mode ##" \
                        % (pyevolve.__version__,)
      session_locals = { "ga_engine"  : ga_engine,
                         "population" : ga_engine.getPopulation(),
                         "pyevolve"   : pyevolve,
                         "it"         : Interaction}
      code.interact(interact_banner, local=session_locals)
   return False

def eval_func(chromosome):
   """ The evaluation function """
   score = 0.0
   for value in chromosome:
      if value==0:
         score += 0.1
   return score

genome = G1DList.G1DList(30)
genome.setParams(rangemin=0, rangemax=10)
ga = GSimpleGA.GSimpleGA(genome)
print ga.bestIndividual()

This workaround will force the Pyevolve to enter on the Interactive Mode when the generation is equal to INTERACTIVE_STOP variable, on your example, it will enter in the Interactive Mode in the generation 50.

Thanks to Jay for reporting this issue.

1 Comment

  1. Thanks Christian, that worked great!
    You could also maybe do something like this;

    import sys, tty, termios
    from select import select
    def kbhit():
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
            ch =
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        if ch == 'x1b':
            return ch
            return None

    Based on this post:

    It ‘eats’ every keystroke though (including Ctrl-C) so it would need some tweaking!

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.