Skip to content

Commit

Permalink
Use pedometer (#6)
Browse files Browse the repository at this point in the history
* Use pedometer

* gif
  • Loading branch information
hasnainroopawalla authored Sep 3, 2023
1 parent 5ede058 commit 3f0ed1b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
Binary file modified assets/simulation.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 38 additions & 8 deletions src/aco/ant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Vector, fromAngle } from "./vector";
export enum IAntState {
ReturningHome,
SearchingForFood,
ReturningHomeUsingPedometer,
}

type IAntennas = {
Expand All @@ -23,25 +24,27 @@ export class Ant {
position: Vector;
velocity: Vector;
desiredVelocity: Vector;
acceleration: Vector;
angle: number;
wanderAngle: number;
state: IAntState;
colony: Colony;
targetFoodItem: FoodItem | null;
lastDepositedPheromone?: Pheromone;
steps: number;

constructor(p: p5, colony: Colony, world: World) {
this.p = p;
this.world = world;
this.colony = colony;
this.position = new Vector(this.colony.position.x, this.colony.position.y);
this.wanderAngle = 0;
this.setSpawnOrientation();
this.searchingForFood();
}

private setSpawnOrientation() {
this.steps = 0;
this.angle = randomFloat(0, this.p.TWO_PI);
this.velocity = fromAngle(this.angle);
this.desiredVelocity = this.velocity.copy();
this.acceleration = new Vector();
this.searchingForFood();
}

private approachTarget(target: Vector): void {
Expand Down Expand Up @@ -122,6 +125,11 @@ export class Ant {
}

private handleSearchingForFood() {
// force ant to use its pedometer to go back to the colony if max steps reached
if (this.steps >= config.antMaxSteps) {
return this.returningHomeUsingPedometer();
}

// check if food item exists within perception range
if (!this.targetFoodItem) {
this.targetFoodItem = this.world.getFoodItemInAntPerceptionRange(
Expand Down Expand Up @@ -149,11 +157,10 @@ export class Ant {
private handleReturningHome() {
if (this.colonyInPerceptionRange()) {
this.approachTarget(this.colony.position);

// check if food item is delivered to colony
if (this.colony.collide(this.position)) {
// rotate 180 degrees
this.velocity.rotate(Math.PI, true);
this.desiredVelocity.rotate(Math.PI, true);
this.targetFoodItem.delivered();
this.targetFoodItem = null;
this.colony.incrementFoodCount();
Expand All @@ -165,6 +172,15 @@ export class Ant {
}
}

private handleReturningHomeUsingPedometer() {
this.approachTarget(this.colony.position);
if (this.colony.collide(this.position)) {
// ant leaves the colony at a random angle
this.setSpawnOrientation();
this.searchingForFood();
}
}

private colonyInPerceptionRange(): boolean {
return areCirclesIntersecting(
this.position,
Expand All @@ -175,6 +191,9 @@ export class Ant {
}

private shouldPheromoneBeDeposited() {
if (this.state === IAntState.ReturningHomeUsingPedometer) {
return false;
}
if (!this.lastDepositedPheromone) {
return true;
}
Expand All @@ -191,7 +210,7 @@ export class Ant {
this.lastDepositedPheromone = new Pheromone(
this.p,
this.position.copy(),
this.isSearchingForFood() ? IPheromoneType.Home : IPheromoneType.Food
this.isReturningHome() ? IPheromoneType.Food : IPheromoneType.Home
);
this.world.depositPheromone(this.lastDepositedPheromone);
}
Expand Down Expand Up @@ -241,6 +260,10 @@ export class Ant {
this.state = IAntState.SearchingForFood;
}

public returningHomeUsingPedometer() {
this.state = IAntState.ReturningHomeUsingPedometer;
}

public isSearchingForFood() {
return this.state === IAntState.SearchingForFood;
}
Expand All @@ -249,15 +272,22 @@ export class Ant {
return this.state === IAntState.ReturningHome;
}

public isReturningHomeUsingPedometer() {
return this.state === IAntState.ReturningHomeUsingPedometer;
}

public update() {
this.handleObstacles();
this.handlePheromoneDeposit();

this.isSearchingForFood() && this.handleSearchingForFood();
this.isReturningHome() && this.handleReturningHome();
this.isReturningHomeUsingPedometer() &&
this.handleReturningHomeUsingPedometer();
this.handleWandering();

this.updatePosition();
this.steps += 1;
}

public render() {
Expand Down
3 changes: 2 additions & 1 deletion src/aco/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const config = {
colonyTextSize: 20,
// ant
antWanderStrength: 0.2,
antMaxSpeed: 2.5,
antMaxSpeed: 3,
antSteeringLimit: 0.1,
antSize: 4,
antColor: "#000000",
Expand All @@ -32,6 +32,7 @@ export const config = {
antAntennaRange: 90,
antAntennaRotation: 1.1,
antObstacleAngleRange: 0.7,
antMaxSteps: 1000,
// pheromone
pheromoneSize: 4,
pheromoneStrokeWeight: 0,
Expand Down

0 comments on commit 3f0ed1b

Please sign in to comment.