-
Notifications
You must be signed in to change notification settings - Fork 0
/
ga.js
89 lines (79 loc) · 2.43 KB
/
ga.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
// var pop_size = 0;
var pool;
function newGeneration(prevPaddles, pop_size){
var paddles= [];
var maxfit = evaluate(prevPaddles,pop_size);
paddles = selection(prevPaddles, pop_size);
flag =1;
return [paddles,maxfit];
}
function duplicate(paddleA, m_rate){
var weightsA = paddleA.controller.model.getWeights();
// paddles[0].controller.model.layers[1].setWeights(paddles[1].controller.model.layers[1].getWeights());
var arr1 =[];
var child_weights = [];
for (var temp in weightsA){
arr1[temp] = weightsA[temp].dataSync();
// ------
for (var i = arr1[temp].length-1; i >= 0; i--) {
if(random(1)< m_rate){
arr1[temp][i] =random(-1,1);
}
}
// ------
var mid = floor(arr1[temp].length /2);
tf.tidy(() => {
if(temp ==0){
child_weights.push(tf.tensor(arr1[temp]).reshape([3,3]));
}
else if(temp ==2){
child_weights.push(tf.tensor(arr1[temp]).reshape([3,5]));
}
else if(temp ==4){
child_weights.push(tf.tensor(arr1[temp]).reshape([5,1]));
}
else{
child_weights.push(tf.tensor(arr1[temp]));
}
});
}
// console.log(child_weights);
return child_weights;
}
function evaluate(prevPaddles,pop_size){
var maxfit =0;
for(var i in prevPaddles){
var fit = 2*prevPaddles[i].score+ 1.5*(prevPaddles[i].fitness);
if(fit>maxfit)
{
maxfit =fit;
}
}
for (var i in prevPaddles) {
prevPaddles[i].score =(2*prevPaddles[i].score+1.5*prevPaddles[i].fitness)/ maxfit;
}
return(maxfit);
}
function selection(paddles, pop_size){
var new_paddles =[];
pool = [];
for(var temp in paddles){
for(var i=0; i<ceil(paddles[temp].score*15);i++)
{
pool.push(paddles[temp]);
}
}
for (var i =pop_size-1;i>=0;i--)
{
var parentA = random(pool);
child_genes =[];
// console.log(parentA);
var child_genes = duplicate(parentA,0.2);
var paddle = new Paddle();
// console.log(child_genes);
paddle.setWeights(child_genes,parentA);
new_paddles.push(paddle);
}
// console.log(new_paddles.length);
return new_paddles;
}