-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneticInvadersInvader.m
More file actions
205 lines (163 loc) · 6.9 KB
/
Copy pathGeneticInvadersInvader.m
File metadata and controls
205 lines (163 loc) · 6.9 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
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
classdef GeneticInvadersInvader < handle
properties (Constant)
X_DIR_POS = 1
X_DIR_NEG = -1
end
properties (SetAccess=private)
% objects
axesObj
handles
positionX
positionY
directionX
matrix
ind
totalDensity
color
finishedFlag
hitFlag
configurationObj
genePoolObj
end
methods
function obj = GeneticInvadersInvader(axesObj, configurationObj, initialPosition, genePoolObj)
obj.hitFlag = false;
obj.finishedFlag = false;
obj.color = [0 0 0];
obj.axesObj = axesObj;
obj.configurationObj = configurationObj;
obj.createInvaderFromGenes(genePoolObj);
obj.createInvaderGraphics(initialPosition);
if rand(1) < 0.5
obj.directionX = GeneticInvadersInvader.X_DIR_POS;
else
obj.directionX = GeneticInvadersInvader.X_DIR_NEG;
end
end
function setXPosition(obj, xpos)
obj.positionX = xpos;
end
function setYPosition(obj, ypos)
obj.positionY = ypos;
end
function x_position = getXPosition(obj)
x_position = obj.positionX;
end
function x_direction = getXDirection(obj)
x_direction = obj.directionX;
end
function setXDirection(obj, d)
obj.directionX = d;
end
function y_position = getYPosition(obj)
y_position = obj.positionY;
end
function hit(obj)
obj.hitFlag = true;
end
function finished(obj)
obj.finishedFlag = true;
end
function p = getGenePool(obj)
p = obj.genePoolObj;
end
function remove(obj)
for k = 1 : numel(obj)
ix = ishandle(obj(k).handles);
if any(ix)
delete(obj(k).handles(ix));
end
end
end
function draw(obj)
len = obj(1).configurationObj.invaderLength;
for o = 1 : length(obj)
[r,c] = ind2sub([len, len], obj(o).ind);
xdata = nan(len);
ydata = nan(len);
xdata(obj(o).ind) = c;
ydata(obj(o).ind) = r;
set(obj(o).handles, ...
'markerfacecolor', obj(o).color,...
'markeredgecolor', obj(o).color,...
'xdata', obj(o).positionX + xdata(:), ...
'ydata', obj(o).positionY + ydata(:) ...
);
end
end
end
methods (Access=private)
function createInvaderFromGenes(obj, genePoolObj)
rColorGeneObj = genePoolObj.getColorRedGene();
gColorGeneObj = genePoolObj.getColorGreenGene();
bColorGeneObj = genePoolObj.getColorBlueGene();
luminosityGeneObj = genePoolObj.getLuminosityGene();
r = rColorGeneObj.getVariedValue();
g = gColorGeneObj.getVariedValue();
b = bColorGeneObj.getVariedValue();
l = luminosityGeneObj.getVariedValue();
newColor = [r, g, b];
% adjust for luminosity
newColor = newColor + (l - sum(newColor)) / 3;
newColor = GeneticInvaderGeneColor.checkVector(newColor);
obj.color = newColor;
densityGeneObj = genePoolObj.getDensityGene();
newDensity = densityGeneObj.getVariedValue();
randFactor = randn(1)/10;
newDensity = newDensity + (newDensity * randFactor);
newDensity = fix(newDensity);
if newDensity > GeneticInvaderGeneDensity.MAX
newDensity = GeneticInvaderGeneDensity.MAX;
elseif newDensity < 1
newDensity = 1;
end
boldnessGeneObj = genePoolObj.getBoldnessGene();
boldValue = boldnessGeneObj.getVariedValue();
YspeedGeneObj = genePoolObj.getYSpeedGene();
YspeedValue = YspeedGeneObj.getVariedValue();
XspeedGeneObj = genePoolObj.getXSpeedGene();
XspeedValue = XspeedGeneObj.getVariedValue();
jitterGeneObj = genePoolObj.getJitterGene();
jitterValue = jitterGeneObj.getVariedValue();
obj.matrix = zeros(obj.configurationObj.invaderLength, obj.configurationObj.invaderLength);
candind = randperm(obj.configurationObj.invaderLength*obj.configurationObj.invaderLength);
candind = candind(1:newDensity);
obj.matrix(candind) = 1;
obj.totalDensity = sum(obj.matrix(:));
obj.ind = find(obj.matrix);
obj.genePoolObj = GeneticInvadersGenePool();
obj.genePoolObj.getColorRedGene().setValue(obj.color(1));
obj.genePoolObj.getColorGreenGene().setValue(obj.color(2));
obj.genePoolObj.getColorBlueGene().setValue(obj.color(3));
obj.genePoolObj.getLuminosityGene().setValue(sum(obj.color));
obj.genePoolObj.getDensityGene().setValue(newDensity);
obj.genePoolObj.getBoldnessGene().setValue(boldValue);
obj.genePoolObj.getYSpeedGene().setValue(YspeedValue);
obj.genePoolObj.getXSpeedGene().setValue(XspeedValue);
obj.genePoolObj.getJitterGene().setValue(jitterValue);
end
function createInvaderGraphics(obj, initialPosition)
obj.handles = [];
obj.positionX = initialPosition(1);
obj.positionY = initialPosition(2);
xdata = nan(obj.configurationObj.invaderLength);
ydata = nan(obj.configurationObj.invaderLength);
[r,c] = ind2sub([obj.configurationObj.invaderLength, obj.configurationObj.invaderLength], obj.ind);
xdata(obj.ind) = c;
ydata(obj.ind) = r;
boldness = obj.genePoolObj.getBoldnessGene().getValue();
newHandle = line(obj.axesObj, ...
'hittest', 'off', ...
'xdata', initialPosition(1) + xdata(:), ...
'ydata', initialPosition(2) + ydata(:), ...
'marker', 'o',...
'linestyle','none',...
'linewidth', boldness,...
'markersize',1,...
'markerfacecolor', obj.color,...
'markeredgecolor', obj.color,...
'visible', 'on');
obj.handles = [obj.handles, newHandle];
end
end
end