-
Notifications
You must be signed in to change notification settings - Fork 0
/
AntSimulator.ts
165 lines (147 loc) · 4.79 KB
/
AntSimulator.ts
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { Ant } from './Ant';
import { FoodSource } from './FoodSource';
import { Pheromone } from './Pheromone';
import * as ULID from "ulid";
export class AntSimulator {
ants: Ant[] = []
foodSources: FoodSource[] = []
pheromones: Pheromone[] = []
gatheredFood = 0
stepCount = 0
constructor(numAnts: number, numFoodSources: number) {
this.ants = [];
this.foodSources = [];
this.pheromones = [];
this.gatheredFood = 0;
for (let i = 0; i < numAnts; i++) {
this.ants.push(new Ant(ULID.ulid(), 0, 0));
}
for (let i = 0; i < numFoodSources; i++) {
// For simplicity, place food sources at random positions between -10 and 10.
this.foodSources.push(
new FoodSource(20 * Math.random() - 10, 20 * Math.random() - 10, 100)
);
}
}
getState() {
const antStates = this.ants.map((ant) => ant.getState());
const foodSourceStates = this.foodSources.map((foodSource) =>
foodSource.getState()
);
const pheromoneStates = this.pheromones.map((pheromone) => ({
x: pheromone.x,
y: pheromone.y,
strength: pheromone.strength,
}));
return {
ants: antStates,
foodSources: foodSourceStates,
pheromones: pheromoneStates,
gatheredFood: this.gatheredFood,
};
}
log() {
let antsWithFood = this.ants.filter((ant) => ant.hasFood).length;
let totalFood = this.foodSources.reduce(
(total, foodSource) => total + foodSource.foodAmount,
0
);
console.log(`Step: ${this.stepCount}`);
console.log(`Ants with food: ${antsWithFood}`);
console.log(`Total food remaining: ${totalFood}`);
// A simple visualization of the world as a 21x21 grid.
// Each cell represents an area from -10 to 10 in x and y.
// let world = Array(21)
// .fill()
// .map(() => Array(21).fill(" "));
// // Mark food sources with 'F'
// for (let foodSource of this.foodSources) {
// let x = Math.round(foodSource.x) + 10;
// let y = Math.round(foodSource.y) + 10;
// world[y][x] = "F";
// }
// // Mark ants with 'A'
// for (let ant of this.ants) {
// let x = Math.round(ant.x) + 10;
// let y = Math.round(ant.y) + 10;
// if (0 <= x && x < 21 && 0 <= y && y < 21) {
// world[y][x] = "A";
// }
// }
// // Print the world
// for (let row of world) {
// console.log(row.join(""));
// }
}
step() {
for (let ant of this.ants) {
ant.move();
if (!ant.hasFood) {
ant.findFood(this.foodSources);
} else {
if (ant.bringFoodHome()) {
this.gatheredFood++;
if (this.gatheredFood > this.ants.length) {
for (let i = 0; i < 10; i++) {
this.ants.push(new Ant(ULID.ulid(), 0, 0));
}
this.foodSources.push(
new FoodSource(2 * Math.random() - 10, 20 * Math.random() - 10, 100)
);
}
}
}
ant.decrementLifeSpan();
if (ant.lifeSpan <= 0) {
this.ants = this.ants.filter(x=>x.id != ant.id)
}
}
// Reduce the strength of all pheromones.
// for (let ant of this.ants) {
// if (ant.pheromone) {
// ant.pheromone.strength--;
// if (ant.pheromone.strength <= 0) {
// ant.pheromone = null;
// }
// }
// }
for (let pheromone of this.pheromones) {
pheromone.strength--;
if (pheromone.strength <= 0) {
const index = this.pheromones.indexOf(pheromone);
if (index > -1) {
this.pheromones.splice(index, 1);
}
}
}
// If an ant comes within a certain distance of a pheromone, move towards the pheromone.
for (let ant of this.ants) {
if (!ant.hasFood) {
for (const otherAnt of this.ants) {
if (otherAnt.pheromone) {
let dx = ant.x - otherAnt.pheromone.x;
let dy = ant.y - otherAnt.pheromone.y;
let distance = Math.sqrt(dx * dx + dy * dy);
if (distance <= 1) {
ant.moveToward(otherAnt.pheromone.x, otherAnt.pheromone.y);
break;
}
}
}
}
}
// If an ant drops a pheromone, add it to the list of pheromones.
for (let ant of this.ants) {
if (ant.pheromone && !this.pheromones.includes(ant.pheromone)) {
this.pheromones.push(ant.pheromone);
}
}
this.log();
}
run(steps: any[]) {
for (let i = 0; i < steps.length; i++) {
this.stepCount = i + 1;
this.step();
}
}
}