-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevolutionary-algorithm-demo.html
More file actions
executable file
·58 lines (48 loc) · 2.04 KB
/
evolutionary-algorithm-demo.html
File metadata and controls
executable file
·58 lines (48 loc) · 2.04 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="author" content="Martin Vyšňovský (martinvysnovsky@gmail.com)">
<title>Evolutionary algorithm</title>
</head>
<body>
<script src="./EvolutionaryAlgorithm.js"></script>
<script>
// measure time taken by algorithm
var start = new Date().getTime();
var fitness_function = function(item) {
return 2 * Math.sin(item.variables.x) + 2;
};
// initialize algorithm;
// first argument is array of names of variables used
// second argument is interval of this values [min, max]
// third argument is number coding - INT / REAL
// fourth argument is fitness funciton
var ea = new EvolutionaryAlgorithm(['x', 'y', 'z', 'm', 'n', 'p'], [-3, 3], 'INT', fitness_function);
// initialize population
// allowed methods: random
var population = ea.initializePopulation(20, 'random');
for(var i=0; i<100; i++)
{
// select parents from population
// allowed methods: random, best, roulette (rouletteMethod: with_replacement, without_replacement, remainder_with_replacement, remainder_without_replacement, univerzal)
var parents = population.getParents('roulette', 20, {rouletteMethod: 'univerzal', shuffleOrder: true});
// apply genetic operators to population
// allowed methods: uniform_mutation (number_of_mutated_values), extremal_mutation (number_of_mutated_values)
var children = population.applyGeneticOperators(parents, 'uniform_mutation');
// replace curent population with new one
// allowed methods: generational, comma_strategy (newGenerationSize), separate_competition (generationGap), plus_strategy (newGenerationSize)
population.replacement(parents, children, 'comma_strategy', {newGenerationSize: 15});
}
var result = population.individuals;
// print out parents
for(var i=0, len=result.length; i<len; i++)
{
document.write(i+1 + ": " + result[i].toString() + "<br>");
}
var end = new Date().getTime();
var time = end - start;
document.write('Execution time: ' + time + 'ms');
</script>
</body>
</html>