-
Notifications
You must be signed in to change notification settings - Fork 3
/
GeneLisa.js
390 lines (308 loc) · 9.8 KB
/
GeneLisa.js
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/**
# Copyright (C) 2016 Prabod Rathnayaka <[email protected]>
#
# This file is part of GeneLisa.
#
# GeneLisa 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.
#
# Copyright Header 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 Copyright Header. If not, see <http://www.gnu.org/licenses/>.
#
*/
/**
* Population for a Generation
* */
var POPULATION = 50;
/**
* Number of Vertices for a Polygon
* */
var VERTICES = 4;
/**
* Size of a Gene
* */
var GENE_SIZE = 4 + (2 * VERTICES);
/**
* Number of Polygons
* */
var POLYGONS = 125;
/**
* Length of a Chromosome
* */
var CHROME_SIZE = GENE_SIZE * POLYGONS;
/**
* Percentage selected for crossover for next generation
* */
var CROSSOVER_RATE = 0.15;
/**
* Percentage that a Chromosome can be mutated
* */
var MUTATION_RATE = 0.01;
/**
* Percentage that a Chromosome selected for mutation will be mutated
* */
var MUTATE_AMOUNT = 0.1;
var population = [];
var generation = 0; // Generation of the population
var goal;
var workingData = [];
/**
* Uses ECMASCRIPT6*/
/**
* Class that represent a Chromosome
* */
class Chromosome{
constructor(bitString,geneSize,chromoSize){
this.valueString = bitString;
this.geneSize = geneSize;
this.chromoSize = chromoSize;
this.c=document.getElementById("workingCanvas");
this.ctx=this.c.getContext("2d");
this.vertices = VERTICES;
this.fitnessValue = 0;
}
/**
* Generate random DNA for the Initial Generation
* Value Encoding is used for DNA encoding
* DNA = [RED, GREEN, BLUE, ALPHA, X1, Y1, X2, Y2, ...]
* */
randomGenes(){
var bString = [];
for (var i = 0; i < this.chromoSize; i+= this.geneSize){
/**
* Generate RGBA
* */
bString.push(
Math.random(), // R
Math.random(), // G
Math.random(), // B
Math.max(Math.random() * Math.random(), 0.2) //A
);
/**
* Generate random (x,y) for vertices
* */
var X = Math.random();
var Y = Math.random();
for (var j = 0; j < this.vertices ; j++){
bString.push(
X + Math.random() - 0.5,
Y + Math.random() - 0.5
);
}
}
this.valueString = bString;
}
/**
* Draw the Chromosome in the canvas
* */
draw(context,width,height){
/**
* Paint the canvas black to make sure nothing left from the previous draw
* */
context.fillStyle = '#000';
context.fillRect(0, 0, width, height);
/**
* Draw the Starting Point -> lines to vertices
* */
for(var i = 0; i < this.chromoSize ; i += this.geneSize){
context.beginPath();
context.moveTo(
this.valueString[i + 4] * width,
this.valueString[i + 5] * height
);
for(var j = 0; j < this.vertices - 1; j++){
/**
* Draw Lines*/
context.lineTo(
this.valueString[i + j * 2 + 6] * width,
this.valueString[i + j * 2 + 7] * height
);
}
context.closePath();
context.fillStyle = 'rgba(' +
((this.valueString[i ] * 255) >> 0) + ',' + // R - int [0,255]
((this.valueString[i + 1] * 255) >> 0) + ',' + // G - int [0,255]
((this.valueString[i + 2] * 255) >> 0) + ',' + // B - int [0,255]
this.valueString[i + 3] + ')';
//Fill the polygon
context.fill();
}
}
/**
* Fitness Function
* fitness = 1 - (Square of pixel difference between chromosome and reference Image)
* ____________________________________________________________________
* ( Resolution of the Image * count(RGBA) * Number of Possible Values)
*
* This fitness function stays inside [0,1]
* */
fitness(width,height){
//Draw the Chromosome First
this.draw(this.ctx,width,height);
var fit = 0;
var imagedata = this.ctx.getImageData(0,0,width,height).data;
for (var i = 0; i < workingData.length;i++){
var dist = workingData[i] - imagedata[i];
fit += dist * dist;
}
this.fitnessValue = 1 - fit / (75 * 75 * 4 * 256 * 256);
return this.fitnessValue;
}
}
/**
* Cross two Chromosomes and produce a child Chromosome
* */
function crossover(chromosome1,chromosome2,rate,geneSize,chromoSize) {
var rand = Math.random(); // Random value for check crossover chance
if (rand < rate){
var vString = [];
for (var i =0; i< chromoSize; i += geneSize){
for(var j = 0; j < geneSize; j++){
/**
* Evenly Choose a Parent for breeding*/
var inheritedGene = (Math.random() < 0.5) ? chromosome1 : chromosome2;
var dna = inheritedGene.valueString[i+j];
var randnum = Math.random(); // Random Number for Mutation chance
if (randnum < MUTATION_RATE){
/**
* Mutate by some Amount*/
dna += Math.random() * MUTATE_AMOUNT * 2 - MUTATE_AMOUNT;
}
if (dna < 0)
dna = 0;
if (dna > 1)
dna = 1;
vString.push(dna);
}
}
return new Chromosome(vString,geneSize,chromoSize);
}
/**
* If No chance for crossover. Return one of the parent Chromosome*/
return (Math.random() < 0.5) ? chromosome1 : chromosome2;
}
/**
* Roulette Wheel Function to select two parent for breeding
* NEVER USED
* a GREEDY METHOD used for selection*/
function roulette(totalFitness, population) {
var slice = Math.random() * totalFitness;
var fitnessSoFar = 0;
for(var i = 0 ; i < POPULATION; i++){
fitnessSoFar += population[i].fitnessValue;
if (fitnessSoFar >= slice){
return population[i];
}
}
return population[0];
}
/**
* Initiate GeneLisa.js
* */
function init() {
/**
* Get Reference Canvas and Context*/
var goalCanvas=document.getElementById("goal");
var goalctx=goalCanvas.getContext("2d");
var base_image = new Image();
/**
* Draw Reference image on canvas*/
base_image.onload = function(){
goalctx.drawImage(base_image,0,0);
goalCanvas.width = 75;
goalCanvas.height = 75;
goalctx.drawImage(base_image,
0, 0, 350, 350, 0, 0,
75, 75);
var imageData = goalctx.getImageData(0, 0,
75 ,
75).data;
workingData = [];
var p = 75 * 75 * 4;
for (var i = 0; i < p; i++) {
workingData[i] = imageData[i];
}
goalCanvas.width = 350;
goalCanvas.height = 350;
goalctx.drawImage(base_image, 0, 0);
};
base_image.src = './mona.png';
/**
* Generate Random Chromosomes for the initial generation
* */
for(var i = 0 ; i < POPULATION; i++){
var cr = new Chromosome([],GENE_SIZE,CHROME_SIZE);
cr.randomGenes();
population.push(cr);
}
}
/**
* Breed a New Generation of Chromosomes
* */
function breed() {
var totalFitness = 0;
var fittest;
var fit=0;
/**
* Calculate fitness of each and every chromosome
* */
for(var j = 0 ; j < population.length ; j++){
var temp = population[j].fitness(75,75,goal);
if (temp >= fit){
fit = temp;
fittest = population[j];// Fittest Chromosome in a Generation
}
totalFitness += temp;
}
/**
* Sort the Generation
* */
population = population.sort(function(a, b) {
return b.fitnessValue - a.fitnessValue;
});
var newPopulation = [];
/**
* Select the Chromosomes with best fitnesses
* */
var selectCount = Math.floor(population.length * CROSSOVER_RATE);
/**
* Number of Chromosomes that needed to be crossed with the each of the Chosen Chromosome
* */
var randCount = Math.ceil(1 / CROSSOVER_RATE);
/**
* Select two parents and breed
* */
for (var i = 0; i < selectCount; i++) {
for (var h = 0; h < randCount; h++) {
var parent = i;
while (parent == i) {
parent = (Math.random() * selectCount) >> 0;
}
/**
* Breed
* */
var crossed = crossover(population[i], population[parent], CROSSOVER_RATE, GENE_SIZE, CHROME_SIZE);
newPopulation.push(crossed);
}
}
/**
* Draw the fittest Chromosome to Output Canvas
* */
var myCanvas = document.getElementById("myCanvas");
var ct = myCanvas.getContext("2d");
fittest.draw(ct,350,350);
population = newPopulation;
generation ++;
/**
* update generation and fitness
* */
document.getElementById("gen").innerHTML = "Generation = " + generation;
document.getElementById("fit").innerHTML = "Fitness = " + fit * 100 + "%";
}