Skip to content

Commit 427c6a6

Browse files
committed
Add particle.js
1 parent 9ef14bf commit 427c6a6

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
var Particle = {
2+
// xLength: 1 + numberOfNeurons + numberOfNeurons * dimension + numberOfNeurons
3+
createNew: function () {
4+
var particle = {};
5+
particle.x = [];
6+
particle.v = [];
7+
particle.fitness = 10000;
8+
particle.rbf = RBF.createNew();
9+
particle.clone = function () {
10+
var particleNew = Particle.createNew();
11+
for (var i = 0; i < this.x.length; i++)
12+
particleNew.x[i] = this.x[i];
13+
for (var i = 0; i < this.v.length; i++)
14+
particleNew.v[i] = this.v[i];
15+
particleNew.normalization();
16+
return particleNew;
17+
};
18+
particle.move = function () {
19+
for (var i = 0; i < this.x.length; i++)
20+
x[i] += v[i];
21+
};
22+
particle.normalization = function (particle) {
23+
this.x[0] = Math.min(Math.max(this.x[0], 0), 1);
24+
this.rbf.theta = this.x[0];
25+
for (var i = 0; i < numberOfNeurons; i++) {
26+
this.x[i + 1] = Math.min(Math.max(this.x[i + 1], -40), 40);
27+
this.rbf.W[i] = this.x[i + 1];
28+
}
29+
for (var i = 1 + numberOfNeurons, j = 0; i < 1 + numberOfNeurons + numberOfNeurons * dimension; i++, j++) {
30+
this.x[i] = Math.min(Math.max(this.x[i], 0), 30);
31+
this.rbf.M[parseInt(j / dimension)][parseInt(j % dimension)] = this.x[i];
32+
}
33+
for (var i = 1 + numberOfNeurons + numberOfNeurons * dimension, j = 0; i < 1 + numberOfNeurons + numberOfNeurons * dimension + numberOfNeurons; i++, j++) {
34+
this.x[i] = Math.min(Math.max(this.x[i], 0.000001), 10);
35+
this.rbf.sigma[j] = this.x[i];
36+
}
37+
};
38+
particle.randomBuild = function () {
39+
this.x[0] = Math.random();
40+
for (var i = 1; i < 1 + numberOfNeurons; i++) {
41+
this.x[i] = Math.random();
42+
}
43+
for (var i = 1 + numberOfNeurons; i < 1 + numberOfNeurons + numberOfNeurons * dimension; i++) {
44+
this.x[i] = Math.random() * 30;
45+
}
46+
for (var i = 1 + numberOfNeurons + numberOfNeurons * dimension; i < 1 + numberOfNeurons + numberOfNeurons * dimension + numberOfNeurons; i++) {
47+
this.x[i] = Math.random() * 10;
48+
}
49+
this.normalization();
50+
};
51+
particle.getFitness = function (yD, x) {
52+
this.normalization();
53+
var value = 0;
54+
for (var i = 0; i < yD.length; i++) {
55+
var fX = this.rbf.getOutput(x[i]);
56+
value += Math.pow(yD[i] - fX, 2);
57+
}
58+
value = value / 2.0;
59+
this.fitness = value;
60+
return value;
61+
};
62+
particle.distanceTo = function(target) {
63+
var distance = 0;
64+
for (var i = 0; i < this.x.length; i++)
65+
distance += (this.x[i] - target.x[i]) * (this.x[i] - target.x[i]);
66+
return distance;
67+
}
68+
return particle;
69+
}
70+
};

0 commit comments

Comments
 (0)