-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathG1DList.py
More file actions
165 lines (124 loc) · 3.79 KB
/
G1DList.py
File metadata and controls
165 lines (124 loc) · 3.79 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
"""
:mod:`G1DList` -- the 1D list chromosome
=============================================================
This is the 1D List representation, this list can carry real
numbers or integers or any kind of object, by default, we have
genetic operators for integer and real lists, which can be found
on the respective modules.
Default Parameters
-------------------------------------------------------------
*Initializator*
:func:`Initializators.G1DListInitializatorInteger`
The Integer Initializator for G1DList
*Mutator*
:func:`Mutators.G1DListMutatorSwap`
The Swap Mutator for G1DList
*Crossover*
:func:`Crossovers.G1DListCrossoverSinglePoint`
The Single Point Crossover for G1DList
Class
-------------------------------------------------------------
"""
from GenomeBase import GenomeBase, G1DBase
import Consts
class G1DList(G1DBase):
""" G1DList Class - The 1D List chromosome representation
Inheritance diagram for :class:`G1DList.G1DList`:
.. inheritance-diagram:: G1DList.G1DList
This chromosome class extends the :class:`GenomeBase.G1DBase` classes.
**Examples**
The instantiation
>>> g = G1DList(10)
Compare
>>> genome2 = genome1.clone()
>>> genome2 == genome1
True
Multiply
>>> genome = population[0]
>>> genome
(...)
[1, 2, 3, 4]
>>> genome_result = genome * 2
>>> genome_result
(...)
[2, 2, 6, 8]
Add
>>> genome
(...)
[1, 2, 3, 4]
>>> genome_result = genome + 2
(...)
[3, 4, 5, 6]
Iteration
>>> for i in genome:
>>> print i
1
2
3
4
Size, slice, get/set, append
>>> len(genome)
4
>>> genome
(...)
[1, 2, 3, 4]
>>> genome[0:1]
[1, 2]
>>> genome[1] = 666
>>> genome
(...)
[1, 666, 3, 4]
>>> genome.append(99)
>>> genome
(...)
[1, 666, 3, 4, 99]
:param size: the 1D list size
"""
def __init__(self, size=10, cloning=False):
""" The initializator of G1DList representation,
size parameter must be specified """
super(G1DList, self).__init__(size)
if not cloning:
self.initializator.set(Consts.CDefG1DListInit)
self.mutator.set(Consts.CDefG1DListMutator)
self.crossover.set(Consts.CDefG1DListCrossover)
def __mul__(self, other):
""" Multiply every element of G1DList by "other" """
newObj = self.clone()
for i in xrange(len(newObj)):
newObj[i] *= other
return newObj
def __add__(self, other):
""" Plus every element of G1DList by "other" """
newObj = self.clone()
for i in xrange(len(newObj)):
newObj[i] += other
return newObj
def __sub__(self, other):
""" Plus every element of G1DList by "other" """
newObj = self.clone()
for i in xrange(len(newObj)):
newObj[i] -= other
return newObj
def __repr__(self):
""" Return a string representation of Genome """
ret = GenomeBase.__repr__(self)
ret += "- G1DList\n"
ret += "\tList size:\t %s\n" % (self.getListSize(),)
ret += "\tList:\t\t %s\n\n" % (self.genomeList,)
return ret
def copy(self, g):
""" Copy genome to 'g'
Example:
>>> genome_origin.copy(genome_destination)
:param g: the destination G1DList instance
"""
GenomeBase.copy(self, g)
G1DBase.copy(self, g)
def clone(self):
""" Return a new instace copy of the genome
:rtype: the G1DList clone instance
"""
newcopy = G1DList(self.genomeSize, True)
self.copy(newcopy)
return newcopy