-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
104 lines (90 loc) · 2.7 KB
/
sketch.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
let person;
let boundary;
let creatures = []
const Render = Matter.Render
const engine = Matter.Engine.create();
const world = engine.world;
const generationPeriod = 20;
let generation = new Generation(25);
let settled = false;
function setup() {
let canvas = createCanvas(windowWidth * 0.95, windowHeight * 0.95);
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 5 seconds
setInterval(() => {
generation.evolve();
console.log(generation.avg_score);
settled = false;
}, generationPeriod * 1000);
// 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: " + 5 + "%", 40, 150);
text("Progress: " + generation.progress.toFixed(2), 40, 170);
// Display Inheritance
textSize(14);
fill('green');
text("Creature\tParentA\t\tParentB", width * 0.78, 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.80, 60 + (15 * index));
})
// Run Matter-JS Engine
Matter.Engine.update(engine);
}