/usr/lib/python2.7/dist-packages/pyevolve/Initializators.py is in python-pyevolve 0.6~rc1+svn398+dfsg-9.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | """
:mod:`Initializators` -- initialization methods module
===================================================================
In this module we have the genetic operators of initialization for each
chromosome representation, the most part of initialization is done by
choosing random data.
.. note:: In Pyevolve, the Initializator defines the data type that will
be used on the chromosome, for example, the :func:`G1DListInitializatorInteger`
will initialize the G1DList with Integers.
"""
from random import randint as rand_randint, uniform as rand_uniform, choice as rand_choice
import GTree
import Util
#############################
## 1D Binary String ##
#############################
def G1DBinaryStringInitializator(genome, **args):
""" 1D Binary String initializator """
genome.genomeList = [ rand_choice((0,1)) for i in xrange(genome.getListSize()) ]
#############################
## 2D Binary String ##
#############################
def G2DBinaryStringInitializator(genome, **args):
""" Integer initialization function of 2D Binary String
.. versionadded:: 0.6
The *G2DBinaryStringInitializator* function
"""
genome.clearString()
for i in xrange(genome.getHeight()):
for j in xrange(genome.getWidth()):
random_gene = rand_choice((0,1))
genome.setItem(i, j, random_gene)
####################
## 1D List ##
####################
def G1DListInitializatorAllele(genome, **args):
""" Allele initialization function of G1DList
To use this initializator, you must specify the *allele* genome parameter with the
:class:`GAllele.GAlleles` instance.
"""
allele = genome.getParam("allele", None)
if allele is None:
Util.raiseException("to use the G1DListInitializatorAllele, you must specify the 'allele' parameter")
genome.genomeList = [ allele[i].getRandomAllele() for i in xrange(genome.getListSize()) ]
def G1DListInitializatorInteger(genome, **args):
""" Integer initialization function of G1DList
This initializator accepts the *rangemin* and *rangemax* genome parameters.
"""
range_min = genome.getParam("rangemin", 0)
range_max = genome.getParam("rangemax", 100)
genome.genomeList = [rand_randint(range_min, range_max) for i in xrange(genome.getListSize())]
def G1DListInitializatorReal(genome, **args):
""" Real initialization function of G1DList
This initializator accepts the *rangemin* and *rangemax* genome parameters.
"""
range_min = genome.getParam("rangemin", 0)
range_max = genome.getParam("rangemax", 100)
genome.genomeList = [rand_uniform(range_min, range_max) for i in xrange(genome.getListSize())]
####################
## 2D List ##
####################
def G2DListInitializatorInteger(genome, **args):
""" Integer initialization function of G2DList
This initializator accepts the *rangemin* and *rangemax* genome parameters.
"""
genome.clearList()
for i in xrange(genome.getHeight()):
for j in xrange(genome.getWidth()):
randomInteger = rand_randint(genome.getParam("rangemin", 0),
genome.getParam("rangemax", 100))
genome.setItem(i, j, randomInteger)
def G2DListInitializatorReal(genome, **args):
""" Integer initialization function of G2DList
This initializator accepts the *rangemin* and *rangemax* genome parameters.
"""
genome.clearList()
for i in xrange(genome.getHeight()):
for j in xrange(genome.getWidth()):
randomReal = rand_uniform(genome.getParam("rangemin", 0),
genome.getParam("rangemax", 100))
genome.setItem(i, j, randomReal)
def G2DListInitializatorAllele(genome, **args):
""" Allele initialization function of G2DList
To use this initializator, you must specify the *allele* genome parameter with the
:class:`GAllele.GAlleles` instance.
.. warning:: the :class:`GAllele.GAlleles` instance must have the homogeneous flag enabled
"""
allele = genome.getParam("allele", None)
if allele is None:
Util.raiseException("to use the G2DListInitializatorAllele, you must specify the 'allele' parameter")
if allele.homogeneous == False:
Util.raiseException("to use the G2DListInitializatorAllele, the 'allele' must be homogeneous")
genome.clearList()
for i in xrange(genome.getHeight()):
for j in xrange(genome.getWidth()):
random_allele = allele[0].getRandomAllele()
genome.setItem(i, j, random_allele)
####################
## Tree ##
####################
def GTreeInitializatorInteger(genome, **args):
""" Integer initialization function of GTree
This initializator accepts the *rangemin* and *rangemax* genome parameters.
It accepts the following parameters too:
*max_depth*
The max depth of the tree
*max_siblings*
The number of maximum siblings of an node
*method*
The method, accepts "grow", "full" or "ramped".
.. versionadded:: 0.6
The *GTreeInitializatorInteger* function.
"""
max_depth = genome.getParam("max_depth", 5)
max_siblings = genome.getParam("max_siblings", 2)
range_min = genome.getParam("rangemin", 0)
range_max = genome.getParam("rangemax", 100)
lambda_generator = lambda: rand_randint(range_min, range_max)
method = genome.getParam("method", "grow")
if method == "grow":
root = GTree.buildGTreeGrow(0, lambda_generator, max_siblings, max_depth)
elif method == "full":
root = GTree.buildGTreeFull(0, lambda_generator, max_siblings, max_depth)
elif method == "ramped":
if Util.randomFlipCoin(0.5):
root = GTree.buildGTreeGrow(0, lambda_generator, max_siblings, max_depth)
else:
root = GTree.buildGTreeFull(0, lambda_generator, max_siblings, max_depth)
else:
Util.raiseException("Unknown tree initialization method [%s] !" % method)
genome.setRoot(root)
genome.processNodes()
assert genome.getHeight() <= max_depth
def GTreeInitializatorAllele(genome, **args):
""" Allele initialization function of GTree
To use this initializator, you must specify the *allele* genome parameter with the
:class:`GAllele.GAlleles` instance.
.. warning:: the :class:`GAllele.GAlleles` instance **must** have the homogeneous flag enabled
.. versionadded:: 0.6
The *GTreeInitializatorAllele* function.
"""
max_depth = genome.getParam("max_depth", 5)
max_siblings = genome.getParam("max_siblings", 2)
method = genome.getParam("method", "grow")
allele = genome.getParam("allele", None)
if allele is None:
Util.raiseException("to use the GTreeInitializatorAllele, you must specify the 'allele' parameter")
if allele.homogeneous == False:
Util.raiseException("to use the GTreeInitializatorAllele, the 'allele' must be homogeneous")
if method == "grow":
root = GTree.buildGTreeGrow(0, allele[0].getRandomAllele, max_siblings, max_depth)
elif method == "full":
root = GTree.buildGTreeFull(0, allele[0].getRandomAllele, max_siblings, max_depth)
elif method == "ramped":
if Util.randomFlipCoin(0.5):
root = GTree.buildGTreeGrow(0, allele[0].getRandomAllele, max_siblings, max_depth)
else:
root = GTree.buildGTreeFull(0, allele[0].getRandomAllele, max_siblings, max_depth)
else:
Util.raiseException("Unknown tree initialization method [%s] !" % method)
genome.setRoot(root)
genome.processNodes()
assert genome.getHeight() <= max_depth
####################
## Tree GP ##
####################
def GTreeGPInitializator(genome, **args):
"""This initializator accepts the follow parameters:
*max_depth*
The max depth of the tree
*method*
The method, accepts "grow", "full" or "ramped"
.. versionadded:: 0.6
The *GTreeGPInitializator* function.
"""
max_depth = genome.getParam("max_depth", 5)
method = genome.getParam("method", "grow")
ga_engine = args["ga_engine"]
if method == "grow":
root = GTree.buildGTreeGPGrow(ga_engine, 0, max_depth)
elif method == "full":
root = GTree.buildGTreeGPFull(ga_engine, 0, max_depth)
elif method == "ramped":
if Util.randomFlipCoin(0.5):
root = GTree.buildGTreeGPFull(ga_engine, 0, max_depth)
else:
root = GTree.buildGTreeGPGrow(ga_engine, 0, max_depth)
else:
Util.raiseException("Unknown tree initialization method [%s] !" % method)
genome.setRoot(root)
genome.processNodes()
assert genome.getHeight() <= max_depth
|