/usr/lib/python2.7/dist-packages/pyevolve/G1DBinaryString.py is in python-pyevolve 0.6~rc1+svn398+dfsg-7.
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 | """
:mod:`G1DBinaryString` -- the classical binary string chromosome
=====================================================================
This is the classical chromosome representation on GAs, it is the 1D
Binary String. This string looks like "00011101010".
Default Parameters
-------------------------------------------------------------
*Initializator*
:func:`Initializators.G1DBinaryStringInitializator`
The Binatry String Initializator for G1DBinaryString
*Mutator*
:func:`Mutators.G1DBinaryStringMutatorFlip`
The Flip Mutator for G1DBinaryString
*Crossover*
:func:`Crossovers.G1DBinaryStringXSinglePoint`
The Single Point Crossover for G1DBinaryString
Class
-------------------------------------------------------------
"""
from GenomeBase import GenomeBase, G1DBase
import Consts
import Util
class G1DBinaryString(GenomeBase, G1DBase):
""" G1DBinaryString Class - The 1D Binary String chromosome
Inheritance diagram for :class:`G1DBinaryString.G1DBinaryString`:
.. inheritance-diagram:: G1DBinaryString.G1DBinaryString
This chromosome class extends the :class:`GenomeBase.GenomeBase`
and :class:`GenomeBase.G1DBase` classes.
Example:
>>> genome = G1DBinaryString.G1DBinaryString(5)
:param length: the 1D Binary String size
"""
evaluator = None
""" This is the :term:`evaluation function` slot, you can add
a function with the *set* method: ::
genome.evaluator.set(eval_func)
"""
initializator = None
""" This is the initialization function of the genome, you
can change the default initializator using the function slot: ::
genome.initializator.set(Initializators.G1DBinaryStringInitializator)
In this example, the initializator :func:`Initializators.G1DBinaryStringInitializator`
will be used to create the initial population.
"""
mutator = None
""" This is the mutator function slot, you can change the default
mutator using the slot *set* function: ::
genome.mutator.set(Mutators.G1DBinaryStringMutatorSwap)
"""
crossover = None
""" This is the reproduction function slot, the crossover. You
can change the default crossover method using: ::
genome.crossover.set(Crossovers.G1DBinaryStringXUniform)
"""
def __init__(self, length=10):
""" The initializator of G1DList representation """
GenomeBase.__init__(self)
G1DBase.__init__(self, length)
self.genomeList = []
self.stringLength = length
self.initializator.set(Consts.CDefG1DBinaryStringInit)
self.mutator.set(Consts.CDefG1DBinaryStringMutator)
self.crossover.set(Consts.CDefG1DBinaryStringCrossover)
def __setitem__(self, key, value):
""" Set the specified value for an gene of List
>>> g = G1DBinaryString(5)
>>> for i in xrange(len(g)):
... g.append(1)
>>> g[4] = 0
>>> g[4]
0
"""
if value not in (0, 1):
Util.raiseException("The value must be zero (0) or one (1), used (%s)" % value, ValueError)
G1DBase.__setitem__(self, key, value)
def __repr__(self):
""" Return a string representation of Genome """
ret = GenomeBase.__repr__(self)
ret += "- G1DBinaryString\n"
ret += "\tString length:\t %s\n" % (self.getListSize(),)
ret += "\tString:\t\t %s\n\n" % (self.getBinary(),)
return ret
def getDecimal(self):
""" Converts the binary string to decimal representation
Example:
>>> g = G1DBinaryString(5)
>>> for i in xrange(len(g)):
... g.append(0)
>>> g[3] = 1
>>> g.getDecimal()
2
:rtype: decimal value
"""
return int(self.getBinary(), 2)
def getBinary(self):
""" Returns the binary string representation
Example:
>>> g = G1DBinaryString(2)
>>> g.append(0)
>>> g.append(1)
>>> g.getBinary()
'01'
:rtype: the binary string
"""
return "".join(map(str, self))
def append(self, value):
""" Appends an item to the list
Example:
>>> g = G1DBinaryString(2)
>>> g.append(0)
:param value: value to be added, 0 or 1
"""
if value not in [0, 1]:
Util.raiseException("The value must be 0 or 1", ValueError)
G1DBase.append(self, value)
def copy(self, g):
""" Copy genome to 'g'
Example:
>>> g1 = G1DBinaryString(2)
>>> g1.append(0)
>>> g1.append(1)
>>> g2 = G1DBinaryString(2)
>>> g1.copy(g2)
>>> g2[1]
1
:param g: the destination genome
"""
GenomeBase.copy(self, g)
G1DBase.copy(self, g)
def clone(self):
""" Return a new instace copy of the genome
Example:
>>> g = G1DBinaryString(5)
>>> for i in xrange(len(g)):
... g.append(1)
>>> clone = g.clone()
>>> clone[0]
1
:rtype: the G1DBinaryString instance clone
"""
newcopy = G1DBinaryString(self.getListSize())
self.copy(newcopy)
return newcopy
|