Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test #2

Open
wants to merge 51 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
2b6018c
Switched to radians
Whitebowfr Apr 10, 2024
5a72654
Parallelized ants
Whitebowfr Apr 10, 2024
319eee1
Switched to radians
Whitebowfr Apr 10, 2024
1249d1f
Parallelized ants
Whitebowfr Apr 10, 2024
7440470
Merge branch 'taichi' of https://github.com/Whitebowfr/projet-fourmis…
Whitebowfr Apr 10, 2024
aadc5d3
Optimised ants with taichi
Whitebowfr Apr 10, 2024
65b6845
Added spread
Whitebowfr Apr 10, 2024
8c6a6fa
Removed junk
Whitebowfr Apr 13, 2024
666ddd4
Updated factors
Whitebowfr Apr 13, 2024
3739d16
Removed junk
Whitebowfr Apr 13, 2024
a548b4a
Fixed blur
Whitebowfr Apr 13, 2024
d9af1c6
Added multiple pheromones
Whitebowfr Apr 13, 2024
19a23a0
Added food and home positions
Whitebowfr Apr 13, 2024
bce9a66
Updated to use external constants file
Whitebowfr Apr 13, 2024
34bdbee
Fixed startup lag
Whitebowfr Apr 13, 2024
cebde2e
tweaking
Guguitino May 7, 2024
4fab92a
Create fourmi.png
lchardin May 7, 2024
bb0fa3c
Create display_set.py
lchardin May 7, 2024
4cc9142
tweaking
Guguitino May 7, 2024
1a792f6
tweaking
Guguitino May 7, 2024
f074592
tweaking
Guguitino May 7, 2024
e795780
Tweaked everything
Whitebowfr May 7, 2024
a596c50
Merge branch 'taichi' of https://github.com/Whitebowfr/projet-fourmis…
Whitebowfr May 7, 2024
2a1a290
Fixed error
Whitebowfr May 7, 2024
eedd510
Removed imgui
Whitebowfr May 7, 2024
d10edda
animation du refresh
lchardin May 7, 2024
dced26f
Merge branch 'taichi' of https://github.com/Whitebowfr/projet-fourmis…
lchardin May 7, 2024
3dd2c00
Fixed issue with duplicate line and ant hidden
Whitebowfr May 7, 2024
c021397
tweaking
Guguitino May 22, 2024
2576a21
Merge remote-tracking branch 'origin/taichi' into taichi
Guguitino May 22, 2024
dd78439
Added treshold for pheromones
Whitebowfr May 22, 2024
f3e5513
tweaking
Guguitino May 22, 2024
ce5bd0b
Merge remote-tracking branch 'origin/taichi' into taichi
Guguitino May 22, 2024
32666da
Updated treshold
Whitebowfr May 22, 2024
74808b9
Merge branch 'taichi' of https://github.com/Whitebowfr/projet-fourmis…
Whitebowfr May 22, 2024
8e27e78
nouveaux parametres
lchardin May 22, 2024
e734b47
Merge branches 'taichi' and 'taichi' of https://github.com/Whitebowfr…
lchardin May 22, 2024
239240b
added the paint feature
Guguitino May 23, 2024
7a58651
Update display_set.py
lchardin May 26, 2024
b02a0e2
finition du double panneau d'affichage
lchardin May 26, 2024
941e8c2
Create fourmi2.png
lchardin May 28, 2024
7ef2eb3
affichage de nouveau paramtres fenetre de droite
lchardin May 28, 2024
015ea91
Update display_set.py
lchardin May 26, 2024
f43a37b
finition du double panneau d'affichage
lchardin May 26, 2024
e78f48d
Create fourmi2.png
lchardin May 28, 2024
789a878
affichage de nouveau paramtres fenetre de droite
lchardin May 28, 2024
1c67e2a
Merge branch 'taichi' of https://github.com/Whitebowfr/projet-fourmis…
Whitebowfr May 29, 2024
05eafa8
Update display_set.py
lchardin May 29, 2024
e38f37d
Merge branch 'taichi' of https://github.com/Whitebowfr/projet-fourmis…
lchardin May 29, 2024
948cc4c
enregistrement dans un CSV
E-Riviere Jun 4, 2024
4b8573f
kjqgcq
lchardin Jun 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode/
__pycache__/
imgui.ini
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/projet-fourmis.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 34 additions & 39 deletions Environnement.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,47 @@
import numpy as np
DECAY_RATE = 0.1

class Environment():
import taichi as ti
import constants

ti.init(arch=ti.vulkan)

@ti.data_oriented
class Environment():
def __init__(self, width, height):
self.width = width - 1
self.height = height - 1
self.grid = np.array([[0.0 for _ in range(width)] for _ in range(height)])
self.fourmis = []


def addFourmi(self, fourmi):
self.fourmis.append(fourmi)
self.grid = ti.field(dtype=ti.f16, shape=(self.height, self.width, constants.NUMBER_OF_PHEROMONES))
self.grid_blurred = ti.field(dtype=ti.f16, shape=(self.height, self.width, constants.NUMBER_OF_PHEROMONES))
self.food = ti.Vector.field(2, dtype=ti.f32, shape=constants.FOOD_COUNT)
self.home = ti.Vector([int(height//2), int(width//2)])
self.init_food()

@ti.kernel
def init_food(self):
for i in range(ti.static(self.food.shape[0])):
self.food[i] = ti.Vector((ti.random() * self.height, ti.random() * self.width))

def decay(self, deltaT):
self.grid -= DECAY_RATE * deltaT
self.grid = np.maximum(self.grid, 0)
@ti.kernel
def decay(self, deltaT: ti.f16):
for (i, j, k) in self.grid:
self.grid[i, j, k] *= ti.f16(1 - ti.static(constants.DECAY_RATE) * deltaT)

def __str__(self):
return str(self.grid)

def pschittt(self, x, y):
self.grid[y][x] += 200
if self.grid[y][x] > 255:
self.grid[y][x] = 255

def spread_here(self, x, y):
value = 0
for i in range(-1, 2):
for j in range(-1, 2):
if x+i >= 0 and x+i < self.width and y+j >= 0 and y+j < self.height:
value += self.grid[y+j][x+i]
return value / 9

@ti.kernel
def box_blur(self):
for i, j, k in self.grid:
blurred_value = ti.f16(0.0)
count = 0
for dx in ti.static(range(-1, 2)):
for dy in ti.static(range(-1, 2)):
if i + dx >= 0 and i + dx < self.height and j + dy >= 0 and j + dy < self.width:
blurred_value += self.grid[i + dx, j + dy, k]
count += 1
self.grid_blurred[i, j, k] = ti.f16(blurred_value / count)
for i, j, k in self.grid:
self.grid[i, j, k] = self.grid_blurred[i, j, k]

def spread(self):
grid_copy = []
for y in range(len(self.grid)):
grid_copy.append([])
for x in range(len(self.grid[y])):
self.grid[y][x] = self.spread_here(x, y)




if __name__ == "__main__":
env = Environment(15,15)
env.pschittt(10, 10)
env.spread()
env = Environment(15, 15)
env.box_blur()
print(env)
60 changes: 0 additions & 60 deletions Vectors.py

This file was deleted.

149 changes: 149 additions & 0 deletions ants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import taichi as ti
import constants

ti.init(arch=ti.vulkan)

@ti.data_oriented
class Ants :
def __init__(self, N, grid, home, foodgrid):
self.n = ti.static(N)
self.positions = ti.Vector.field(2, dtype=ti.f32, shape=self.n)
self.angles = ti.field(dtype=ti.f32, shape=self.n)
self.grid = grid
self.foodgrid = foodgrid
self.home = home
self.has_food = ti.field(dtype=bool, shape=self.n)
self.lol = ti.field(dtype=ti.f16, shape=self.n)
self.place_ants_home()



@ti.kernel
def place_ants_random(self):
for i in range(self.n):
angle = ti.f32(ti.random() * 2 * 3.14)
radius = ti.sqrt(ti.random()) * (self.grid.shape[0] / 2)
x = ti.f32(radius * ti.cos(angle) + self.grid.shape[0] / 2)
y = ti.f32(radius * ti.sin(angle) + self.grid.shape[1] / 2)
self.lol[i] = 0
self.positions[i] = ti.Vector([x, y])
self.angles[i] = angle



@ti.kernel
def place_ants_home(self):
for i in range(self.n):
angle = ti.random() * 2 * 3.14
self.positions[i] = self.home
self.angles[i] = ti.f32(angle)
self.lol[i] = 0

@ti.kernel
def update(self, deltaT: ti.f32):
for i in range(ti.static(self.n)):
self.update_fourmi(i, deltaT)
self.lol[i] = self.lol[i] + ti.f16(deltaT * constants.LOST_SPEED)

@ti.func
def update_fourmi(self, i: int, deltaT: ti.f32):
pheromone = 0
phb = 0
if ti.static(constants.NUMBER_OF_PHEROMONES) > 1:
pheromone = 1 if self.has_food[i] else 0
phb = abs(pheromone - 1)

forward_value = self.getSensorValue(i, 0, phb)
left_value = self.getSensorValue(i, ti.static(constants.SENSOR_ANGLE_RAD), phb) * 0.8
right_value = self.getSensorValue(i, -ti.static(constants.SENSOR_ANGLE_RAD), phb) * 0.8

randomSteering = ti.random() * ti.static(constants.RANDOM_FACT)

if left_value - right_value > constants.TRESHOLD :
self.angles[i] += randomSteering * ti.static(constants.TURN_SPEED) * deltaT
elif right_value - left_value > constants.TRESHOLD :
self.angles[i] -= randomSteering * ti.static(constants.TURN_SPEED) * deltaT
elif forward_value >= right_value and forward_value >= left_value :
self.angles[i] += (randomSteering - ti.static(constants.RANDOM_FACT) / 2) * 2 * ti.static(constants.TURN_SPEED) * deltaT

direction = ti.Vector((ti.cos(self.angles[i]), ti.sin(self.angles[i])), dt=ti.f32)
newPos = self.positions[i] + direction * deltaT * ti.static(constants.MOVE_SPEED)

shape = self.grid.shape

if newPos[0] < 0 or newPos[0] >= shape[0] or newPos[1] < 0 or newPos[1] >= shape[1] :
if newPos[0] < 0 :
newPos[0] = shape[0] - 1
elif newPos[0] >= shape[0] :
newPos[0] = 0
if newPos[1] < 0 :
newPos[1] = shape[1] - 1
elif newPos[1] >= shape[1] :
newPos[1] = 0
else :
previousTrail = self.grid[int(self.positions[i]), pheromone]
self.grid[int(self.positions[i]), pheromone] = min(1, previousTrail + ti.exp(-self.lol[i]))

if ti.static(constants.NUMBER_OF_PHEROMONES) > 1:
if self.isInRectangle(self.positions[i], self.home, ti.static(constants.HOME_SIZE)) and self.has_food[i]:
self.has_food[i] = False
self.angles[i] -= ti.f32(3.14)
self.lol[i] = 0
if not self.has_food[i]:
for f in range(self.foodgrid.shape[0]) :
if self.isInRectangle(self.positions[i], self.foodgrid[f], ti.static(constants.FOOD_SIZE)) :
self.has_food[i] = True
self.angles[i] -= ti.f32(3.14)
self.lol[i] = 0

self.positions[i] = newPos

@ti.func
def isEqual(self, vec1: ti.template(), vec2: ti.template()) -> bool : # type: ignore
return int(vec1[0]) == int(vec2[0]) and int(vec1[1]) == int(vec2[1])

@ti.func
def isInRectangle(self, point: ti.template(), rectangle_center: ti.template(), rectangle_radius: int) -> bool : # type: ignore
x = point[0]
y = point[1]
rect_x = rectangle_center[0]
rect_y = rectangle_center[1]

return x > rect_x - rectangle_radius and x < rect_x + rectangle_radius and y > rect_y - rectangle_radius and y < rect_y + rectangle_radius


@ti.func
def getSensorValue(self, i: int, sensorAngleOffset: float, pheromone: int) -> float:
sensorAngle = self.angles[i] + sensorAngleOffset
sensorDir = ti.Vector([ti.cos(sensorAngle), ti.sin(sensorAngle)])
sensorPos = self.positions[i] + sensorDir * ti.static(constants.SENSOR_OFFSET_DISTANCE)

value = 0.0

for offsetX in range(-ti.static(constants.SENSOR_SIZE), ti.static(constants.SENSOR_SIZE)):
for offsetY in range(-ti.static(constants.SENSOR_SIZE), ti.static(constants.SENSOR_SIZE)):
gridX = int(sensorPos[0] + offsetX)
gridY = int(sensorPos[1] + offsetY)

if gridX < 0 or gridX >= self.grid.shape[0] or gridY < 0 or gridY >= self.grid.shape[1]:
if gridX < 0 :
gridX = self.grid.shape[0] - 1
elif gridX >= self.grid.shape[0] :
gridX = 0
if gridY < 0 :
gridY = self.grid.shape[1] - 1
elif gridY >= self.grid.shape[1] :
gridY = 0
if ti.static(constants.NUMBER_OF_PHEROMONES) > 1 :
if self.has_food[i]:
if self.isInRectangle(ti.Vector([gridX, gridY]), self.home, ti.static(constants.HOME_SIZE)) :
value += 1
else :
for k in range(self.foodgrid.shape[0]) :
if self.isInRectangle(ti.Vector([gridX, gridY]), self.foodgrid[k], ti.static(constants.FOOD_SIZE)) :
value += 1
value += self.grid[gridX, gridY, pheromone]

if value < 0.01 :
value = 0.0
return value
22 changes: 22 additions & 0 deletions constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
DECAY_RATE = 0.5
SPREAD_RATE: int = 10

NUMBER_OF_PHEROMONES: int = 2

SENSOR_OFFSET_DISTANCE: int = 2
SENSOR_SIZE: int = 45
SENSOR_ANGLE_RAD: float = 45 * 3.14 / 180
LOST_SPEED: float = 0.3


TURN_SPEED: float = 300 * 3.14 / 180
MOVE_SPEED: int = 300
RANDOM_FACT: int = 5

HOME_SIZE: int = 30
FOOD_SIZE: int = 5
FOOD_COUNT: int = 10

HIDE_MARKERS: bool = False

TRESHOLD: float = 0.1
Empty file added create_gpx.py
Empty file.
Loading