-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsketch.js
More file actions
executable file
·136 lines (117 loc) · 3.85 KB
/
sketch.js
File metadata and controls
executable file
·136 lines (117 loc) · 3.85 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
let person;
let boundary;
let canvas;
let creatures = []
const Render = Matter.Render
const engine = Matter.Engine.create();
const world = engine.world;
let generationPeriod = 15;
let population_size = 25;
let mutation_rate = 0.5;
let generation = new Generation(population_size);
let settled = false;
function setup() {
canvas = createCanvas(windowWidth * 0.73, windowHeight * 0.95);
canvas.parent("evolution-canvas");
frameRate(60);
rectMode(CENTER);
textSize(18)
fill(255);
// Initialize Generation
generation.initialize(Person);
generation.species.forEach((creature) => { creature.add_to_world(world) });
// Boundary
boundary = new SimpleBoundary();
boundary.add_to_world();
// Mouse Constraint
let canvasMouse = Matter.Mouse.create(canvas.elt);
canvasMouse.pixelRatio = pixelDensity();
let m = Matter.MouseConstraint.create(engine, { mouse: canvasMouse });
Matter.World.add(world, m);
// Restart Generation after certain seconds
setInterval(() => {
generation.evolve();
settled = false;
}, generationPeriod * 1000);
/** Input fields & Button to save and load models */
const input_field = document.getElementById('model-input-field');
const save_button = document.getElementById('save_button');
const load_button = document.getElementById('load_button');
const download_button = document.getElementById('download_button');
save_button.addEventListener("click", () => {
// Get the best creature
let best_creature = generation.getBest();
// Show its data in the textarea
let json_data = best_creature.brain.toJson();
input_field.innerHTML = json_data
});
load_button.addEventListener("click", () => {
let data = input_field.value;
let json_data = JSON.parse(data);
generation.species.forEach((creature) => {
creature.brain.loadFromJson(json_data);
})
})
download_button.addEventListener("click", () => {
let best_creature = generation.getBest();
let json_data = best_creature.brain.toJson();
saveJSON(JSON.parse(json_data), 'best.json')
})
// Run the renderer
// let render = Render.create({
// element: document.body,
// engine: engine,
// options: {
// height, width
// }
// })
// Render.run(render);
// let renderMouse = Matter.Mouse.create(render.canvas);
// renderMouse.pixelRatio = pixelDensity();
// Matter.World.add(world, Matter.MouseConstraint.create(engine, {
// mouse: renderMouse
// }));
}
let counter = 1;
function draw() {
if (counter >= 60) {
counter = 0;
settled = true;
}
counter++;
background(color(15, 15, 19));
// Display Boundary
boundary.display();
// Display Creatures
generation.species.forEach((creature) => {
creature.show();
creature.adjust_score();
if (counter % 4 === 0 && settled) {
creature.think(boundary);
}
});
// Display Stats
textSize(18)
fill("red");
text("Generation: " + generation.generation, 40, 50);
text("HighScore: " + generation.high_score.toFixed(2), 40, 70);
text("Average Score " + generation.avg_score.toFixed(2), 40, 90);
text("Population: " + generation.population, 40, 110);
text("Generation Period: " + generationPeriod + " seconds", 40, 130);
text("Mutation Rate: " + mutation_rate , 40, 150);
text("Progress: " + generation.progress.toFixed(2), 40, 170);
// Display Inheritance
textSize(14);
fill('green');
text("Creature\tParentA\t\tParentB", width * 0.73, 40)
generation.species.forEach((creature, index) => {
let txt = '';
if (creature.parents.length !== 0)
txt = `${creature.id} \t\t\t ${creature.parents[0].id} (${creature.parents[0].score.toFixed(0)}) \t\t\t ${creature.parents[1].id}(${creature.parents[1].score.toFixed(0)})`;
else
txt = `${creature.id} \t\t\t ------ \t\t\t ------`
text(txt, width * 0.75, 60 + (15 * index));
})
// Run Matter-JS Engine
Matter.Engine.update(engine);
}