-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
executable file
·106 lines (79 loc) · 3.61 KB
/
main.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
window.onload=function(){
// Collection, a place for holding values you insert in the first place.
let collection = [];
document.getElementById("confirm").addEventListener("click", createkHTMLBlock);
let inputElement = document.getElementById("inputElement");
let hasCalculateButton = 0;
function createkHTMLBlock(){
// check for input value
if (inputElement && inputElement.value) {
// setup for some dynamic generated HTML
let container = document.getElementById("container");
var div = document.createElement("div");
div.setAttribute("class","block");
var textnode = document.createTextNode(inputElement.value);
collection.push(inputElement.value);
div.appendChild(textnode);
container.appendChild(div);
hasCalculateButton++;
if (hasCalculateButton == 1) {
generateCalculateButton();
document.getElementById("calculateIt").addEventListener("click", thinkInSolutions);
}
// clear input
inputElement.value= '';
}
}
// Button to calculate buttons associated with the number of items in the collection.
function generateCalculateButton(){
var btn = document.createElement("button");
btn.setAttribute("id", "calculateIt");
btn.innerHTML = "calculate!";
document.body.appendChild(btn);
}
// Genetic algorithm doing the thinking.
function thinkInSolutions(){
let targetElement = document.getElementById("max");
if (targetElement && targetElement.value) {
let population = new Population(collection.length, collection.length * 4);
population.calcFitness(collection);
population.evaluate(parseInt(targetElement.value, 10));
for (let i = 0; i < collection.length * 4; i++) {
population.createNextGeneration(20, collection);
population.calcFitness(collection);
population.evaluate(parseInt(targetElement.value, 10));
}
let bestOnes = population.getBestSolutions();
// A moment for verfication. To see all solutions generated.
// This moment only one solution is shown visible (by the yellow color).
// Logs all found solutions in console.
bestOnes.forEach(lid => {
let collectedItems = [];
for (let i = 0; i < lid.length; i++) {
if (lid[i] == "1") {
collectedItems.push(collection[i]);
}
}
console.log(collectedItems);
});
console.log("-----");
let blocks = document.getElementsByClassName("block");
// Clear the yellow ones.
for (let i = 0; i < blocks.length; i++) {
blocks[i].style.backgroundColor = "white";;
}
// Paint the blocks containing a part of the solutions yellow.
if (bestOnes.length > 0) {
for (let i = 0; i < bestOnes[0].length; i++) {
if (bestOnes[0][i] == "1") {
blocks[i].style.backgroundColor = "yellow";
}
}
} else {
console.log("No solutions available!");
}
} else {
alert("Please, insert a max value! (Yes, it is necessary).");
}
}
}