diff --git a/pyevolve/Consts.py b/pyevolve/Consts.py index b6ab473..674db17 100644 --- a/pyevolve/Consts.py +++ b/pyevolve/Consts.py @@ -36,10 +36,6 @@ >>> minmax = Consts.minimaxType["minimize"] >>> minmax = Consts.minimaxType["maximize] -.. attribute:: CDefESCKey - - The ESC key ASCII code. Used to start Interactive Mode. - .. attribute:: CDefRangeMin Minimum range. This constant is used as integer and real max/min. @@ -395,7 +391,6 @@ "maximize" : 1 } -CDefESCKey = 27 CDefImportList = {"visual.graph": "you must install VPython !", "csv" : "csv module not found !", diff --git a/pyevolve/GSimpleGA.py b/pyevolve/GSimpleGA.py index b6288e8..1bba8f5 100644 --- a/pyevolve/GSimpleGA.py +++ b/pyevolve/GSimpleGA.py @@ -79,9 +79,8 @@ import code import pyevolve -# Platform dependant code for the Interactive Mode -if sys_platform[:3] == "win": - import msvcrt + + def RawScoreCriteria(ga_engine): """ Terminate the evolution using the **bestrawscore** and **rounddecimal** @@ -351,6 +350,8 @@ def setElitismReplacement(self, numreplace): def setInteractiveMode(self, flag=True): """ Enable/disable the interactive mode + If True, press CTRL-C to enter Interactive Mode during the evolution + If False, CTRL-C will exit the evolution normally :param flag: True or False @@ -750,8 +751,8 @@ def evolve(self, freq_stats=0): self.internalPop.sort() logging.debug("Starting loop over evolutionary algorithm.") - try: - while True: + while True: + try: if self.migrationAdapter: logging.debug("Migration adapter: exchange") self.migrationAdapter.exchange() @@ -787,39 +788,41 @@ def evolve(self, freq_stats=0): break if self.interactiveMode: - if sys_platform[:3] == "win": - if msvcrt.kbhit(): - if ord(msvcrt.getch()) == Consts.CDefESCKey: - print "Loading modules for Interactive Mode...", - logging.debug("Windows Interactive Mode key detected ! generation=%d", self.getCurrentGeneration()) - from pyevolve import Interaction - print " done !" - interact_banner = "## Pyevolve v.%s - Interactive Mode ##\nPress CTRL-Z to quit interactive mode." % (pyevolve.__version__,) - session_locals = { "ga_engine" : self, - "population" : self.getPopulation(), - "pyevolve" : pyevolve, - "it" : Interaction} - print - code.interact(interact_banner, local=session_locals) - if (self.getInteractiveGeneration() >= 0) and (self.getInteractiveGeneration() == self.getCurrentGeneration()): - print "Loading modules for Interactive Mode...", - logging.debug("Manual Interactive Mode key detected ! generation=%d", self.getCurrentGeneration()) - from pyevolve import Interaction - print " done !" - interact_banner = "## Pyevolve v.%s - Interactive Mode ##" % (pyevolve.__version__,) - session_locals = { "ga_engine" : self, - "population" : self.getPopulation(), - "pyevolve" : pyevolve, - "it" : Interaction} - print - code.interact(interact_banner, local=session_locals) + print "Loading modules for Interactive Mode...", + logging.debug("Manual Interactive Mode key detected ! generation=%d", self.getCurrentGeneration()) + from pyevolve import Interaction + print " done !" + interact_banner = "## Pyevolve v.%s - Interactive Mode ##" % (pyevolve.__version__,) + session_locals = { "ga_engine" : self, + "population" : self.getPopulation(), + "pyevolve" : pyevolve, + "it" : Interaction} + print + code.interact(interact_banner, local=session_locals) if self.step(): break - except KeyboardInterrupt: - logging.debug("CTRL-C detected, finishing evolution.") - if freq_stats: print "\n\tA break was detected, you have interrupted the evolution !\n" + except KeyboardInterrupt: + if self.interactiveMode and self.interactiveGen == -1: + print "Loading modules for Interactive Mode...", + logging.debug("CTRL-C detected, continuing in Interactive Mode ! generation=%d", self.getCurrentGeneration()) + from pyevolve import Interaction + print " done !" + if sys_platform[:3] == "win": + interact_banner = "## Pyevolve v.%s - Interactive Mode ##\nPress CTRL-Z to quit interactive mode." % (pyevolve.__version__,) + else: + interact_banner = "## Pyevolve v.%s - Interactive Mode ##\nPress CTRL-D to quit interactive mode." % (pyevolve.__version__,) + session_locals = { "ga_engine" : self, + "population" : self.getPopulation(), + "pyevolve" : pyevolve, + "it" : Interaction} + print + code.interact(interact_banner, local=session_locals) + else: + logging.debug("CTRL-C detected, finishing evolution.") + if freq_stats: print "\n\tA break was detected, you have interrupted the evolution !\n" + break if freq_stats != 0: self.printStats() diff --git a/pyevolve/Interaction.py b/pyevolve/Interaction.py index d08b5e4..5cf864e 100644 --- a/pyevolve/Interaction.py +++ b/pyevolve/Interaction.py @@ -13,6 +13,12 @@ You can use the manual method to enter in the Interactive Mode at specific generation using the :meth:`GSimpleGA.GSimpleGA.setInteractiveGeneration` method. +If the *interactiveGen* in :class:`GSimpleGA.GSimpleGA` is not set, pressing +CTRL-C during the evolution will start interactive mode. + +If you want exit the evolution completely when *interactiveMode* is enabled, you +need to enter interactive mode by pressing CTRL-C and then type *exit()* at the prompt. + """ import logging