Skip to content
Open
5 changes: 0 additions & 5 deletions pyevolve/Consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -395,7 +391,6 @@
"maximize" : 1
}

CDefESCKey = 27

CDefImportList = {"visual.graph": "you must install VPython !",
"csv" : "csv module not found !",
Expand Down
71 changes: 37 additions & 34 deletions pyevolve/GSimpleGA.py
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions pyevolve/Interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down