-
Notifications
You must be signed in to change notification settings - Fork 1
/
ants.py
183 lines (141 loc) · 6.61 KB
/
ants.py
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import numpy as np
from p5 import *
def load_image_rgb(path):
img = load_image(path)
img.pixels[:, :, [0, 1, 2]] = img.pixels[:, :, [2, 1, 0]] # Convert from BGR to RGB
return img
def setup():
global car_img, charger_img
global cars, chargers, distances, pheromones, shortest_path, shortest_distance
global ants, num_ants, dst_power, pheromone_power, evaporation_rate, pheromone_intensity
shortest_path = None
shortest_distance = float('inf')
cars, chargers, distances, pheromones = np.empty((0, 2)), np.empty((0, 2)), np.empty((0, 0, 1)), np.empty((0, 0, 1))
ants = []
num_ants = 1000
dst_power, pheromone_power, evaporation_rate, pheromone_intensity = 8.0, 4.0, 0.2, 2.0
car_img = load_image_rgb("img/car.png")
charger_img = load_image_rgb("img/charger.png")
size(1280, 720)
set_frame_rate(1000)
def spawn_ants():
global num_ants, ants, chargers, cars, shortest_path, shortest_distance
ants = []
if len(chargers) > 0 and len(cars) > 0:
charger_slots = {i: 2 for i in range(len(chargers))}
for _ in range(num_ants):
spawn_location = {'type': 'car', 'id': np.random.randint(0, len(cars))}
ant = {
'distanceTraveled': 0.0,
'carsVisited': set(),
'chargerSlots': charger_slots.copy(),
'location': spawn_location,
'nodesVisited': [spawn_location.copy()]
}
ants.append(ant)
def reset_shortest_path():
global shortest_path, shortest_distance
shortest_path = None
shortest_distance = float('inf')
def save_shortest_path():
global ants, shortest_path, shortest_distance
for ant in ants:
if ant['distanceTraveled'] < shortest_distance:
shortest_path = ant['nodesVisited']
shortest_distance = ant['distanceTraveled']
def calculate_distances():
global distances, cars, chargers
if len(chargers) > 0 and len(cars) > 0:
distances = abs(np.expand_dims(cars, axis=1) - chargers).sum(axis=2)
def initialize_pheromones():
global pheromones
pheromones = np.ones((len(cars), len(chargers)))
def mouse_pressed():
global mouse_button, mouse_x, mouse_y, cars, chargers
if mouse_button == LEFT:
cars = np.vstack([cars, [(mouse_x, mouse_y)]])
elif mouse_button == RIGHT:
chargers = np.vstack([chargers, [(mouse_x, mouse_y)]])
reset_shortest_path()
calculate_distances()
initialize_pheromones()
spawn_ants()
def calculate_probabilites(neighbor_distances, neighbor_pheromones):
global dst_power, pheromone_power
desirabilities = ((1 / neighbor_distances)**dst_power) * (neighbor_pheromones**pheromone_power)
desirabilities[desirabilities == 0] = 1e-10 # Avoid division by zero
return desirabilities / sum(desirabilities) # Normalize to probabilities
def update_pheromones():
global ants, pheromones, evaporation_rate, pheromone_intensity
pheromones *= 1 - evaporation_rate
for ant in ants:
for idx in range(len(ant['nodesVisited']) - 1):
curr = ant['nodesVisited'][idx]
next = ant['nodesVisited'][idx + 1]
if curr['type'] == 'car':
carId, chargerId = curr['id'], next['id']
else:
carId, chargerId = next['id'], curr['id']
pheromones[carId][chargerId] += pheromone_intensity / ant['distanceTraveled']
def step():
global ants, pheromones, chargers, cars, distances
for ant in ants:
if len(ant['carsVisited']) != len(cars) and sum(ant['chargerSlots'].values()) > 0.0:
if ant['location']['type'] == 'car':
possibleChargers = [i for i in range(len(chargers)) if ant['chargerSlots'][i] > 0]
chargerDistances = distances[ant['location']['id']][possibleChargers]
chargerPheromones = pheromones[ant['location']['id']][possibleChargers]
probabilites = calculate_probabilites(chargerDistances, chargerPheromones)
nextCharger = np.random.choice(range(len(possibleChargers)), p=probabilites)
nextChargerId = possibleChargers[nextCharger]
ant['carsVisited'].add(ant['location']['id'])
ant['chargerSlots'][nextChargerId] -= 1
ant['location']['type'] = 'charger'
ant['location']['id'] = nextChargerId
ant['distanceTraveled'] += chargerDistances[nextCharger]
elif ant['location']['type'] == 'charger':
possibleCars = [i for i in range(len(cars)) if i not in ant['carsVisited']]
carDistances = distances[:, ant['location']['id']][possibleCars]
carPheromones = pheromones[:, ant['location']['id']][possibleCars]
probabilites = calculate_probabilites(carDistances, carPheromones)
nextCar = np.random.choice(range(len(possibleCars)), p=probabilites)
ant['location']['type'] = 'car'
ant['location']['id'] = possibleCars[nextCar]
ant['distanceTraveled'] += carDistances[nextCar]
ant['nodesVisited'].append(ant['location'].copy())
generation_done = True
for ant in ants:
if len(ant['carsVisited']) != len(cars) and sum(ant['chargerSlots'].values()) > 0.0:
generation_done = False
break
if ants and generation_done:
update_pheromones()
save_shortest_path()
spawn_ants()
def draw():
global cars, chargers, pheromones, distances, shortest_path
background(34, 39, 46)
for car_id, (car_x, car_y) in enumerate(cars):
for charger_id, (charger_x, charger_y) in enumerate(chargers):
strokeWeight(pheromones[car_id][charger_id])
stroke(1, 4, 9)
line(car_x, car_y, charger_x, charger_y)
if shortest_path:
for idx in range(len(shortest_path) - 1):
curr, next = shortest_path[idx], shortest_path[idx + 1]
if curr['type'] == 'car':
car_x, car_y = cars[curr['id']]
charger_x, charger_y = chargers[next['id']]
else:
car_x, car_y = cars[next['id']]
charger_x, charger_y = chargers[curr['id']]
strokeWeight(8)
stroke(218, 54, 51)
line(car_x, car_y, charger_x, charger_y)
for car_x, car_y in cars:
image(car_img, car_x - car_img.width() // 2, car_y - car_img.height() // 2)
for charger_x, charger_y in chargers:
image(charger_img, charger_x - charger_img.width() // 2, charger_y - charger_img.height() // 2)
step()
if __name__ == '__main__':
run(renderer='skia')