Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Made interactive mode more platform independent #15

Open
wants to merge 8 commits into from

2 participants

@amillerrhodes

I changed the code around a little so that a Ctrl-C will stop the evolution and start interactive mode. This is only if interactive mode is enabled. If interactiveMode is set to False, then Ctrl-C will exit the evolution without going into interactive mode.

I tested this in Debian and it works fine.

@perone
Owner

Thanks ! Can you also change the documentation explaining the new behavior ? I think the documentation is still citing about the ESC key on windows, if you don't have time to do that, please open a ticket so I can track it and merge the pull request, thanks again !

@amillerrhodes

It's been a crazy semester, but I now have time to get back to this. I'll update the documentation in the next week or two.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
5 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 !",
View
71 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()
View
6 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
Something went wrong with that request. Please try again.