-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnt_Colonies.pde
More file actions
109 lines (97 loc) · 3.1 KB
/
Ant_Colonies.pde
File metadata and controls
109 lines (97 loc) · 3.1 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
/**
An implementation of Kenneth Stanley's NeuroEvolution of Augmenting Topologies
(NEAT) Follows this paper:
https://nn.cs.utexas.edu/downloads/papers/stanley.ec02.pdf
CONTROLS:
Click + drag - move around
Scroll - zoom
+/- - simulation speed
o/p - number of generations skipped between rendered ones
n - skip to next generation
*/
import java.util.*;
PVector cameraPos;
float cameraZoom = 1;
float tileSize = 10;
Grid grid = new Grid(300, 375);
Population population = new Population();
TileAgent selectedAgent;
Network selectedNetwork;
Tile selectedTile;
float speed = 5;
int iterationFrames = 0, generationIteration = 0, generationElapsed = 0,
generationCount = 0, generationSpeed = 1000;
boolean skipGeneration = false;
void setup() {
// fullScreen();
size(512, 512);
windowResizable(true);
cameraPos = new PVector(0, 0);
population.firstGeneration();
}
void draw() {
background(255);
pushMatrix();
translate(-cameraPos.x * cameraZoom, -cameraPos.y * cameraZoom);
translate(width * 3 / 8, height / 2); // center of screen without ui
scale(cameraZoom);
translate(-width * 3 / 8, -height / 2); // center of screen without ui
for (int i = 0; i < floor(iterationFrames / speed) || skipGeneration; i++) {
// Keep simulating iterations without drawing anything, and only
// simulate a frame if, when the speed is above 1, some number of frames
// have passed
population.frame();
}
if (generationIteration == 0) {
// draw something now
grid.draw();
// UI elements
popMatrix();
drawUI();
if (iterationFrames < speed) {
iterationFrames++;
} else {
iterationFrames = 0;
}
} else {
// When skipping through generations
popMatrix();
skipGeneration = true;
drawProgress();
}
}
void drawUI() {
noStroke();
fill(128);
rect(width * 3 / 4, 0, width / 4, height);
if (selectedAgent != null) {
// draw ui of thing that's selected (ant or colony)
selectedAgent.drawUI(width * 3.0 / 4, 0.0);
} else {
// general simulation information
String output = "top score: " + population.topScore + " (gen " +
population.topScoreGeneration + ")\n" +
"speed: " + (1 / speed) + "\n" +
"generation: " + (generationCount + 1) + "\n" +
"generation progress: " + generationElapsed + "/" +
generationLength + "\n" +
"generations at once: " + generationSpeed + "\n" +
"species: " + population.species.size() + "\n" +
"ants: " + population.population + "\n";
fill(0);
textAlign(LEFT, TOP);
textSize(24);
text(output, width * 3 / 4 + 5, 5);
}
}
void drawProgress() {
fill(0);
textAlign(CENTER, CENTER);
textSize(32);
text("Simulating...", width / 2, height / 2 - 64);
text(
generationIteration + "/" + generationSpeed,
width / 2,
height / 2 + 64
);
}