From 8b75a6a60e3a98229a9d741569ed00f3b06157cb Mon Sep 17 00:00:00 2001 From: Anthony Miller-Rhodes Date: Sat, 10 Sep 2011 21:49:56 -0400 Subject: [PATCH 1/8] Added some code to support esc in linux --- pyevolve/GSimpleGA.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/pyevolve/GSimpleGA.py b/pyevolve/GSimpleGA.py index b6288e8..7480b8a 100644 --- a/pyevolve/GSimpleGA.py +++ b/pyevolve/GSimpleGA.py @@ -82,6 +82,28 @@ # Platform dependant code for the Interactive Mode if sys_platform[:3] == "win": import msvcrt +elif sys_platform[:3] == "lin": + import tty + import termios + from sys import stdin as sys_stdin + def linux_getch(): + try: + fd = sys_stdin.fileno() + old_settings = termios.tcgetattr(fd) + try: + tty.setraw(sys_stdin.fileno()) + ch = sys_stdin.read(1) + finally: + termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) + return ch + except: + return + + import signal + TIMEOUT = 1 + + + def RawScoreCriteria(ga_engine): """ Terminate the evolution using the **bestrawscore** and **rounddecimal** @@ -802,6 +824,20 @@ def evolve(self, freq_stats=0): print code.interact(interact_banner, local=session_locals) + if sys_platform[:3] == "lin": + 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 771eca5b707d515060bf22ef8bf41a6506e8bbc6 Mon Sep 17 00:00:00 2001 From: Anthony Miller-Rhodes Date: Sun, 11 Sep 2011 01:24:18 -0400 Subject: [PATCH 2/8] Changed CTRL-C to stop evolution and continue in Interactive Mode. Removed Windows specific code for this feature --- pyevolve/GSimpleGA.py | 78 ++++++++++++------------------------------- 1 file changed, 21 insertions(+), 57 deletions(-) diff --git a/pyevolve/GSimpleGA.py b/pyevolve/GSimpleGA.py index 7480b8a..ad7a596 100644 --- a/pyevolve/GSimpleGA.py +++ b/pyevolve/GSimpleGA.py @@ -79,29 +79,6 @@ import code import pyevolve -# Platform dependant code for the Interactive Mode -if sys_platform[:3] == "win": - import msvcrt -elif sys_platform[:3] == "lin": - import tty - import termios - from sys import stdin as sys_stdin - def linux_getch(): - try: - fd = sys_stdin.fileno() - old_settings = termios.tcgetattr(fd) - try: - tty.setraw(sys_stdin.fileno()) - ch = sys_stdin.read(1) - finally: - termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) - return ch - except: - return - - import signal - TIMEOUT = 1 - @@ -772,8 +749,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() @@ -809,35 +786,6 @@ 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 sys_platform[:3] == "lin": - 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()) @@ -853,9 +801,25 @@ def evolve(self, freq_stats=0): 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: + 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" if freq_stats != 0: self.printStats() From ec980350557a2f7683d8f1817bfd5b93047feca2 Mon Sep 17 00:00:00 2001 From: Anthony Miller-Rhodes Date: Sun, 11 Sep 2011 02:06:45 -0400 Subject: [PATCH 3/8] Added break statement so that CTRL-C quits the GA when Interactive Mode is False --- pyevolve/GSimpleGA.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/pyevolve/GSimpleGA.py b/pyevolve/GSimpleGA.py index ad7a596..ae79959 100644 --- a/pyevolve/GSimpleGA.py +++ b/pyevolve/GSimpleGA.py @@ -787,17 +787,17 @@ def evolve(self, freq_stats=0): if self.interactiveMode: 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 @@ -820,6 +820,7 @@ def evolve(self, freq_stats=0): 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() From d78564fa112a7e5fd3cdf23068f4aecb14ed7cbf Mon Sep 17 00:00:00 2001 From: Anthony Miller-Rhodes Date: Sun, 11 Sep 2011 20:17:19 -0400 Subject: [PATCH 4/8] Added code to disable CTRL-C to interactive mode when an interactiveGen is set by the user --- pyevolve/GSimpleGA.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyevolve/GSimpleGA.py b/pyevolve/GSimpleGA.py index ae79959..c96acd6 100644 --- a/pyevolve/GSimpleGA.py +++ b/pyevolve/GSimpleGA.py @@ -802,7 +802,7 @@ def evolve(self, freq_stats=0): if self.step(): break except KeyboardInterrupt: - if self.interactiveMode: + 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 From 6b0f9547bcfea8d762d06d26c6fe929f186fc639 Mon Sep 17 00:00:00 2001 From: Anthony Miller-Rhodes Date: Sat, 17 Dec 2011 23:11:27 -0500 Subject: [PATCH 5/8] Removed ESC key from Consts due to change in interactiveMode behavior. ESC is no longer needed. --- pyevolve/Consts.py | 5 ----- 1 file changed, 5 deletions(-) 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 !", From 3c8db426952f32db79d519ce8fb8492b7dc2d3a3 Mon Sep 17 00:00:00 2001 From: Anthony Miller-Rhodes Date: Sat, 17 Dec 2011 23:25:11 -0500 Subject: [PATCH 6/8] Added documentation to setInteractiveMode method of GSimpleGA to describe Interactive mode functionality --- pyevolve/GSimpleGA.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pyevolve/GSimpleGA.py b/pyevolve/GSimpleGA.py index c96acd6..1bba8f5 100644 --- a/pyevolve/GSimpleGA.py +++ b/pyevolve/GSimpleGA.py @@ -350,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 From 35b9a59ad0f7f761d4ea29805bf837545dd36d13 Mon Sep 17 00:00:00 2001 From: Anthony Miller-Rhodes Date: Sat, 17 Dec 2011 23:34:08 -0500 Subject: [PATCH 7/8] Added documentation in Interaction.py module to describe interactive mode behavior --- pyevolve/Interaction.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyevolve/Interaction.py b/pyevolve/Interaction.py index d08b5e4..c0561bf 100644 --- a/pyevolve/Interaction.py +++ b/pyevolve/Interaction.py @@ -13,6 +13,9 @@ 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. + """ import logging From da536c164cbc60f5eecd24b593fc35f1e00ba5d3 Mon Sep 17 00:00:00 2001 From: Anthony Miller-Rhodes Date: Sun, 18 Dec 2011 00:06:12 -0500 Subject: [PATCH 8/8] Added more on behavior of interactive mode to documentation in the Interaction.py module --- pyevolve/Interaction.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyevolve/Interaction.py b/pyevolve/Interaction.py index c0561bf..5cf864e 100644 --- a/pyevolve/Interaction.py +++ b/pyevolve/Interaction.py @@ -16,6 +16,9 @@ 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