-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpopulation.js
More file actions
executable file
·157 lines (138 loc) · 5.59 KB
/
population.js
File metadata and controls
executable file
·157 lines (138 loc) · 5.59 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
class Population{
constructor(lengte, lengthPopulation){
this.optimalSolutionScore = 0; // Benchmark to recruit optimal solutions.
this.optimalMembers = []; // Best solutions encoded.
this.optimalSolutionValues = []; // Associated values to the best encoded solutions.
this.members = []; // Members of the population.
for (let i = 0; i < lengthPopulation; i++) {
let dna = "";
for (let j = 0; j < lengte; j++) {
dna += Math.floor(Math.random() * 2); // generate a 0 or a 1 in string format.
}
this.members.push(dna);
}
}
// Arrange all current results.
calcFitness(verzameling) {
this.values = [];
this.members.forEach(member => {
let value = 0;
for (let i = 0; i < member.length; i++) {
if (member[i] === "1") {
value += parseInt(verzameling[i], 10);
}
}
this.values.push(value);
});
}
// Decide what to do.
evaluate(doel){
this.solutions = [];
let counter = 0;
for (let i = 0; i < this.values.length; i++) {
if (this.values[i] <= doel) {
this.solutions.push(this.members[i]);
counter++;
// A check with the best (the best seen so far).
if (this.values[i] > this.optimalSolutionScore) {
this.optimalSolutionScore = this.values[i];
this.optimalSolutionValues.push(this.values[i]);
this.optimalMembers.push(this.members[i]);
} else if(this.values[i] === this.optimalSolutionScore) {
// Loop through to check if it is a unique member.
let uniqueMember = true;
for (let k = 0; k < this.optimalMembers.length; k++) {
if (this.optimalMembers[k] === this.members[i]) {
uniqueMember = false;
};
}
// Add if unique in encoding.
if (uniqueMember) {
this.optimalMembers.push(this.members[i]);
this.optimalSolutionValues.push(this.values[i]);
}
}
}
}
}
// Next round, new opportunities!
createNextGeneration(lengthPopulation){
if(this.solutions.length !== 0){
// Place to fill with new possible solutions.
let nextpool = [];
// only the best go on.
this.solutions.forEach(solution => {
// only mutation (discovery within existing solutions)
let mutatedSolution = "";
for (let i = 0; i < solution.length; i++) {
if (Math.random() < 0.15) {
mutatedSolution += solution[i] === "0" ? "1" : "0";
}else {
mutatedSolution += solution[i];
}
}
nextpool.push(mutatedSolution);
});
// if population is not complete yet
while (true) {
if (nextpool.length < lengthPopulation) {
// crossover (50/50). So half part of one solution and the other part of another solution.
let parentOne = this.solutions[Math.floor(Math.random() * this.solutions.length)];
let parentTwo = this.solutions[Math.floor(Math.random() * this.solutions.length)];
let child = this.crossover(parentOne, parentTwo);
nextpool.push(child);
} else {
break;
}
}
this.members = nextpool;
} else {
// No solutions found at all! So generate new candidates!
let newMembers = [];
for (let i = 0; i < lengthPopulation; i++) {
let dna = "";
for (let j = 0; j < this.members[0].length; j++) {
dna += Math.floor(Math.random() * 2);
}
newMembers.push(dna);
}
this.members = newMembers;
}
}
// mix some offsprings.
crossover(dnaEen, dnaTwee){
let dnaChild = "";
for (let i = 0; i < dnaEen.length; i++) {
if (i > Math.floor(dnaEen.length/2)) {
// mutate
if (Math.random() < 0.15) {
dnaChild += dnaEen[i] === "0" ? "1" : "0";
} else {
dnaChild += dnaEen[i];
}
}else {
// mutate
if (Math.random() < 0.15) {
dnaChild += dnaTwee[i] === "0" ? "1" : "0";
} else {
dnaChild += dnaTwee[i];
}
}
}
return dnaChild;
}
// Get the best solutions!
getBestSolutions(){
let besten = [];
let decrementor = this.optimalMembers.length - 1;
while (decrementor > 0) {
if (this.optimalSolutionValues[decrementor] == this.optimalSolutionScore) {
besten.push(this.optimalMembers[decrementor]);
decrementor--;
} else {
break;
}
}
return besten;
}
}