-
Notifications
You must be signed in to change notification settings - Fork 6
/
head.js
347 lines (300 loc) · 9.52 KB
/
head.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
(function(modules) { // webpackBootstrap
// The module cache
var installedModules = {};
// The require function
function __webpack_require__(moduleId) {
// Check if module is in cache
if(installedModules[moduleId])
return installedModules[moduleId].exports;
// Create a new module (and put it into the cache)
var module = installedModules[moduleId] = {
exports: {},
id: moduleId,
loaded: false
};
// Execute the module function
modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
// Flag the module as loaded
module.loaded = true;
// Return the exports of the module
return module.exports;
}
// expose the modules object (__webpack_modules__)
__webpack_require__.m = modules;
// expose the module cache
__webpack_require__.c = installedModules;
// __webpack_public_path__
__webpack_require__.p = "";
// Load entry module and return exports
return __webpack_require__(0);
})
/************************************************************************/
([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var _gen = __webpack_require__(1);
var _gen2 = _interopRequireDefault(_gen);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var isRunning = false;
var init = function init() {
redirectPosts();
listen();
play();
};
function redirectPosts() {
var capture = window.location.search.match(/\?post=(\d+)/);
var postid = capture ? capture[1] : null;
if (postid) {
window.location.href = 'https://github.com/livoras/blog/issues/' + postid;
}
}
function listen() {
document.getElementById('head').addEventListener('click', function () {
if (isRunning) return;
play();
});
}
function play() {
var head = document.getElementById('head');
var history = (0, _gen2.default)('Stay simple, stay naive.').history;
isRunning = true;
var i = 0;
history.forEach(function (text, i) {
setTimeout(function () {
head.innerText = text;
if (++i === history.length) isRunning = false;
}, i * 30);
});
}
init();
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
/**
* Using Genetic Algorithm to generate lots of random strings
* and make them evolve towards the target string.
*
*/
var Genea = __webpack_require__(2);
var alphabetArr = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ., '.split('');
var alphabet = function () {
var alphabet = {};
alphabetArr.forEach(function (ch, i) {
alphabet[ch] = i;
});
return alphabet;
}();
function getTargetStr(targetStr) {
var binaryStr = '';
for (var i = 0, len = targetStr.length; i < len; i++) {
var ch = targetStr[i];
var chIndex = alphabet[ch];
binaryStr += paddingWith0(Number(chIndex).toString(2));
}
return binaryStr;
}
function paddingWith0(num) {
while (num.length < 6) {
num = '0' + num;
}
return num;
}
function run(str) {
var tar = getTargetStr(str);
var ga = new Genea({
geneLength: tar.length,
mutateProbability: 0.5,
doneFitness: 1,
populationSize: 20,
generationsSize: 400,
getFitness: function getFitness(gene) {
var count = 0;
for (var i = 0, len = gene.length; i < len; i++) {
if (gene[i] === tar[i]) count++;
}
var likeness = count / tar.length;
return likeness;
},
onGeneration: function onGeneration(generation, genes) {
var max = 0,
index = 0;
this.fitnesses.forEach(function (fitness, i) {
if (fitness > max) {
max = fitness;
index = i;
}
});
this.history.push(toChars(genes[index]));
}
});
ga.history = [];
ga.start();
return ga;
}
function toChars(gene) {
var str = '';
while (gene.length) {
var ch = '00' + gene.substr(0, 6);
gene = gene.substr(6);
var chIndex = parseInt(ch, 2);
if (chIndex >= alphabetArr.length) {
chIndex = Math.floor(Math.random() * (alphabetArr.length - 1));
}
if (!alphabetArr[chIndex]) console.log(chIndex, parseInt(ch, 2));
str += alphabetArr[chIndex];
}
return str;
}
module.exports = run;
/***/ },
/* 2 */
/***/ function(module, exports) {
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Genea = function () {
function Genea(config) {
_classCallCheck(this, Genea);
this.currentGeneration = 0;
this.populations = [];
this.fitnesses = [];
this.mutateProbability = config.mutateProbability || 0.5; // 0 ~ 1
this.generationsSize = config.generationsSize || 100;
this.populationSize = config.populationSize || 100;
this.doneFitness = config.doneFitness || 1; // 0 ~ 1
this.geneLength = config.geneLength;
this.getFitness = config.getFitness;
this.outOfGenerationsSize = config.outOfGenerationsSize || this.outOfGenerationsSize;
this.onGeneration = config.onGeneration || this.onGeneration;
this.done = config.done || this.done;
}
_createClass(Genea, [{
key: "start",
value: function start() {
this.initPopulation();
this.makeFitnesses();
this.select();
}
}, {
key: "initPopulation",
value: function initPopulation() {
this.currentGeneration = 1;
this.populations = [];
for (var i = 0, len = this.populationSize; i < len; i++) {
var gene = getRandomGene(this.geneLength);
this.populations.push(gene);
}
this.onGeneration(this.currentGeneration, this.populations);
}
}, {
key: "select",
value: function select() {
if (this.currentGeneration >= this.generationsSize) {
return this.outOfGenerationsSize(this.populations, this.fitnesses);
}
var matches = this.getMatches();
if (matches.length > 0) return this.done(matches);
this.generateNextGeneration();
}
}, {
key: "makeFitnesses",
value: function makeFitnesses() {
var _this = this;
this.fitnesses = [];
this.totalFitness = 0;
this.populations.forEach(function (individual, i) {
var fitness = _this.getFitness(individual, _this.populations);
_this.fitnesses[i] = fitness;
_this.totalFitness += fitness;
});
}
}, {
key: "getMatches",
value: function getMatches() {
var _this2 = this;
var bests = [];
this.populations.forEach(function (individual, i) {
var fitness = _this2.fitnesses[i];
if (fitness >= _this2.doneFitness) {
bests.push({
gene: individual,
fitness: fitness,
pos: i
});
}
});
return bests;
}
}, {
key: "generateNextGeneration",
value: function generateNextGeneration() {
this.currentGeneration++;
var oldPopulations = this.populations;
var newPopulations = [];
for (var i = 0, len = oldPopulations.length; i < len; i++) {
var father = this.rotate();
var mother = this.rotate();
var child = this.crossOver(father, mother);
child = this.mutate(child);
newPopulations.push(child);
}
this.populations = newPopulations;
this.makeFitnesses();
this.onGeneration(this.currentGeneration, this.populations);
this.select();
}
}, {
key: "crossOver",
value: function crossOver(father, mother) {
var pos = Math.floor(father.length * Math.random());
var child1 = father.substring(0, pos) + mother.substring(pos);
var child2 = mother.substring(0, pos) + father.substring(pos);
return this.getFitness(child1) > this.getFitness(child2) ? child1 : child2;
}
}, {
key: "mutate",
value: function mutate(child) {
var mutateProbability = Math.random();
if (mutateProbability < this.mutateProbability) return child;
var pos = Math.floor(Math.random() * this.geneLength);
var arr = child.split("");
arr[pos] = +child[pos] ^ 1;
return arr.join("");
}
}, {
key: "rotate",
value: function rotate() {
var pos = Math.random(); // let's roll!
var soFar = 0;
for (var i = 0, len = this.fitnesses.length; i < len; i++) {
var fitness = this.fitnesses[i];
soFar += fitness;
if (soFar / this.totalFitness >= pos) {
return this.populations[i];
}
}
}
}, {
key: "done",
value: function done() {}
}, {
key: "onGeneration",
value: function onGeneration() {}
}, {
key: "outOfGenerationsSize",
value: function outOfGenerationsSize() {}
}]);
return Genea;
}();
function getRandomGene(len) {
var gene = "";
for (var i = 0; i < len; i++) {
gene += Math.floor(Math.random() * 100) % 2 === 0 ? "1" : "0";
}
return gene;
}
module.exports = Genea;
/***/ }
]);