-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathG1DBinaryString.py
More file actions
169 lines (120 loc) · 4.04 KB
/
G1DBinaryString.py
File metadata and controls
169 lines (120 loc) · 4.04 KB
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
"""
: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(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.G1DBase` class.
Example:
>>> genome = G1DBinaryString.G1DBinaryString(5)
:param length: the 1D Binary String size
"""
__slots__ = ["stringLength"]
def __init__(self, length=10):
""" The initializator of G1DList representation """
super(G1DBinaryString, self).__init__(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 instance 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