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
|
# coding: UTF8
# Copyright 2009 Thomas Jourdan
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import random
import ka_random
import model_allele
class FlipMerger(model_allele.Allele):
"""FlipMerger: Reverse the layer horizontally or vertically.
inv: self.xFlip == -1.0 or self.xFlip == 1.0
inv: self.yFlip == -1.0 or self.yFlip == 1.0
"""
def __init__(self, trunk):
"""Constructor for a flip merger."""
super(FlipMerger, self).__init__(trunk)
self.xFlip = 1.0
self.yFlip = 1.0
def __eq__(self, other):
"""Equality based on fliping horizontally or vertically."""
equal = isinstance(other, FlipMerger) \
and self.xFlip == other.xFlip \
and self.yFlip == other.yFlip
return equal
def randomize(self):
"""Randomize the layers components."""
self.xFlip = 1.0 if random.randint(0, 1) > 0 else -1.0
self.yFlip = 1.0 if random.randint(0, 1) > 0 else -1.0
def mutate(self):
"""Make small random changes to the layers components."""
if ka_random.is_mutating():
self.xFlip = 1.0 if random.randint(0, 1) > 0 else -1.0
if ka_random.is_mutating():
self.yFlip = 1.0 if random.randint(0, 1) > 0 else -1.0
def shuffle(self):
"""Shuffle similar componets."""
pass
def crossingover(self, other):
"""
pre: isinstance(other, FlipMerger)
pre: isinstance(self, FlipMerger)
# check for distinct references, needs to copy content, not references
post: __return__ is not self
post: __return__ is not other
"""
new_one = FlipMerger(self.get_trunk())
cross_sequence = ka_random.crossing_sequence(2)
new_one.xFlip = self.xFlip if cross_sequence[0] else other.xFlip
new_one.yFlip = self.yFlip if cross_sequence[1] else other.yFlip
return new_one
def draw_single_layer(self, single_layer, mask_layer, ctx, width, height):
"""
pre: single_layer is not None
pre: ctx is not None
pre: width > 0
pre: height > 0
pre: width == height
"""
# paint one layer
ctx.scale(self.xFlip, self.yFlip)
ctx.save()
single_layer.draw(ctx, width, height)
ctx.restore()
def explain(self):
"""
post len(__return__) == 3
"""
if self.xFlip and self.yFlip:
text = u'Flip merger: flip horizontally and vertically.'
elif self.xFlip:
text = u'Flip merger: flip horizontally.'
elif self.yFlip:
text = u'Flip merger: flip vertically.'
else:
text = u'Flip merger: did not flip.'
return text, None, None
def copy(self):
"""A copy constructor.
post: isinstance(__return__, FlipMerger)
# check for distinct references, needs to copy content, not references
post: __return__ is not self
"""
new_one = FlipMerger(self.get_trunk())
new_one.xFlip = self.xFlip
new_one.yFlip = self.yFlip
return new_one
|