-
Notifications
You must be signed in to change notification settings - Fork 0
/
genetic-algorithm.js
214 lines (163 loc) · 7.18 KB
/
genetic-algorithm.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
const { mean, subtract } = require('mathjs');
const { data } = require('./data-loader');
const { ask } = require('./question-ask');
const { tfidfDistance } = require('./tf-idf');
class Person {
constructor(config) {
this.learningRate = config.learningRate;
this.credibility = config.credibility;
this.knowledge = config.knowledge;
}
computeCredibility(target) {
let questionMeanError = 0;
let answerMeanError = 0;
const targetQuestionTfidf = tfidfDistance(target.question);
const targetAnswerTfidf = tfidfDistance(target.answer);
for (const topic in this.knowledge) {
const questionTfidf = tfidfDistance(topic);
const answerTfidf = tfidfDistance(this.knowledge[topic]);
let questionError = 0;
if(target.question.length >= topic) {
questionError = subtract(targetQuestionTfidf, questionTfidf.resize(targetQuestionTfidf.size()));
} else {
questionError = subtract(targetQuestionTfidf.resize(questionTfidf.size()), questionTfidf);
}
let answerError = 0;
if(target.answer.length >= this.knowledge[topic].length) {
answerError = subtract(targetAnswerTfidf, answerTfidf.resize(targetAnswerTfidf.size()));
} else {
answerError = subtract(targetAnswerTfidf.resize(answerTfidf.size()), answerTfidf);
}
questionMeanError += mean(questionError);
answerMeanError += mean(answerError);
}
const knowledgeLength = ((Object.keys(this.knowledge).length) + 1);
if(target.answer.length <= 0) answerMeanError = knowledgeLength;
this.credibility -= (questionMeanError + answerMeanError) / knowledgeLength;
}
ask(question) {
return ask(question, this.knowledge);
}
learn(target) {
const learningChance = 0.8 || Math.random();
if (this.learningRate < learningChance) return;
let selector = Math.floor(Math.random() * data.length);
this.knowledge = {
...this.knowledge,
...data[selector].knowledge
};
}
crossover(partner) {
const parentKnowledge = {
...this.knowledge,
...partner.knowledge
};
const parentKnowledgeLength = (Object.keys(parentKnowledge).length) + 1;
const parentsLearningRate = (this.learningRate + partner.learningRate) / 2;
const offspring1LearningRate = (Math.random() + parentsLearningRate) / 2;
const offspring2LearningRate = (Math.random() + parentsLearningRate) / 2;
let offspring1Knowledge = {};
let offspring2Knowledge = {};
let offspring1PivotLimit = Math.floor(Math.random() * parentKnowledgeLength);
offspring1PivotLimit = offspring1PivotLimit > 0 ? offspring1PivotLimit : (parentKnowledgeLength / 2);
let offspring2PivotLimit = Math.floor(Math.random() * parentKnowledgeLength);
offspring2PivotLimit = offspring2PivotLimit > 0 ? offspring2PivotLimit : (parentKnowledgeLength / 2);
let pivot = 0;
for (const topic in parentKnowledge) {
if (pivot < offspring1PivotLimit) {
offspring1Knowledge[topic] = parentKnowledge[topic];
}
if (pivot < offspring2PivotLimit) {
offspring2Knowledge[topic] = parentKnowledge[topic];
}
pivot++;
}
const parentsCredibility = Math.abs(this.credibility + partner.credibility) / 2;
let offspring1Credibility = Math.floor(Math.random() * (Object.keys(offspring1Knowledge).length) + 1);
offspring1Credibility = (offspring1Credibility + parentsCredibility) / 2;
let offspring2Credibility = Math.floor(Math.random() * (Object.keys(offspring2Knowledge).length) + 1);
offspring2Credibility = (offspring2Credibility + parentsCredibility) / 2;
const offspring3LearningRate = (offspring1LearningRate + offspring2LearningRate);
const offspring3Credibility = (offspring1Credibility + offspring2Credibility);
const offspring3Knowledge = {
...offspring1Knowledge,
...offspring2Knowledge
};
const offspring1 = {
learningRate: offspring1LearningRate,
credibility: offspring1Credibility,
knowledge: offspring1Knowledge
};
const offspring2 = {
learningRate: offspring2LearningRate,
credibility: offspring2Credibility,
knowledge: offspring2Knowledge
}
const offspring3 = {
learningRate: offspring3LearningRate,
credibility: offspring3Credibility,
knowledge: offspring3Knowledge
}
return [new Person(offspring1), new Person(offspring2), new Person(offspring3)];
}
}
class Population {
constructor(target, populationSize) {
this.people = [];
this.target = target;
this.generationNo = 0;
while (populationSize--) {
let selector = Math.floor(Math.random() * data.length);
const person = new Person({
learningRate: Math.random(),
credibility: Math.floor(Math.random() * (Object.keys(data[selector].knowledge).length) + 1),
knowledge: data[selector].knowledge
});
this.people.push(person);
}
}
sort() {
this.people.sort((a, b) => {
return b.credibility - a.credibility;
})
}
showGeneration() {
console.log("Generation: " + this.generationNo);
this.people.map(person => {
console.log("Credibility: " + person.credibility + " | Knowledge: " + JSON.stringify(person.knowledge));
})
}
populate() {
for (let i = 0; i < this.people.length; i++) {
this.people[i].computeCredibility(this.target);
}
this.sort();
this.showGeneration();
const crossoverChance = 0.5 || Math.random();
const bestParent = 0;
let randomParent = Math.floor(Math.random() * this.people.length);
randomParent = randomParent > 0 && Math.random() > crossoverChance ? randomParent : 1;
const offsprings = this.people[bestParent].crossover(this.people[randomParent]);
this.people.splice(this.people.length - 3, 3, offsprings[0], offsprings[1], offsprings[2]);
let perfectGeneration = true;
for (let i = 0; i < this.people.length; i++) {
this.people[i].learn(this.target);
this.people[i].computeCredibility(this.target);
// if(!this.people[i].knowledge.hasOwnProperty(this.target.question)
if (this.people[i].credibility < 1) {
perfectGeneration = false;
}
}
if (perfectGeneration) {
this.sort();
this.showGeneration();
} else {
this.generationNo++;
const self = this;
setTimeout(function() {
self.populate();
}, 200);
}
}
}
module.exports.Population = Population;