Project: Stage 3.5
Courage… let's run away!


Disclaimer: the automatically generated English translation is provided only for convenience and it may contain wording flaws. The original French document must be taken as reference!


Goals: Lizards learn to flee from their predators.

Until now, an animal has analyzed the entities in its field of vision to detect food sources or potential mates.

To develop a fleeing behavior, it must also evaluate which of these organic entities could be predators (which could eat it!). If there is at least one predator in its field of vision, it must enter the "running away" state (RUNNING_AWAY).

Switching to the RUNNING_AWAY state takes precedence over all other possible states: the presence of a predator determines the state before the presence of food or a mate.

The animal must flee from any predator in its field of vision and should not return to the WANDERING state as long as there is a predator nearby.

The distance at which the animal feels safe enough to stop fleeing can be configured as you see fit (for example, using a ratio based on the size of the world).

Force Controlling Movement

The force governing movement must be adapted to the fleeing state.

Let the difference between the predator's position i and that of the animal be:

The force controlling the animal’s movement in the presence of predators is calculated using the following formula:

where k is the number of predators, δ1 is a flight amplification coefficient (for example, 500), and δ2 is a coefficient greater than 1 (for example, 1.2) that accounts for the impact of distance (the farther the predator, the weaker the repulsion effect).

[Question Q3.16] Do you think it would be useful to have an attribute to remember an animal's potential predators? Answer this question in your REPONSES file, justifying your response.

Test 22: Escape

To test the fleeing behavior, you will continue to use PPSTest.cpp.

Follow the same procedure as previous tests by configuring your simulation to make observation easier.

Create configurations that normally trigger a fleeing response:

Lizards flee and then resume a WANDERING behavior once the scorpion is far enough away. Fleeing does not occur when the predator is outside the field of vision.

At this stage, you now have a relatively complete tool to simulate a predator-prey model. Well done!

If you like, you can experiment with your configuration files (parameters related to reproduction, longevity, etc.) to see if you can create situations where both populations remain stable for a long time. For example, using the provided configuration file appPPS.json, you should be able to observe cyclical population dynamics: with enough cacti (10) and sufficient humidity (rain enabled), a reasonable population of lizards (~20) will proliferate, then decline (due to food shortages), then proliferate again if you reintroduce cacti, etc. Observing population trends quantitatively can be challenging. We will address this at the end of the project by adding evolution curve displays.


Graphics Improvements

If you are bothered by the fact that animals sometimes appear drawn under the cactus, the Drawable class provides a method getDepth() that allows defining the drawing priority order, resolving this issue.

To take advantage of this, animals as drawable objects should redefine getDepth to return their drawing priority level (DrawingPriority::ANIMAL_PRIORITY), and cacti should do the same (with the value DrawingPriority::CACTUS_PRIORITY). When the environment’s drawing method renders OrganicEntity objects, it can now do so in the correct priority order:

    // entities is the vector of OrganicEntity objects in the environment; we create a copy in a list:
      list<Entity*> sorted( entities.begin(), entities.end());
     // Define an ordering relation based on getDepth(): 
    auto comp([](Entity* a, Entity* b)->bool{ return int(a->getDepth()) < int(b->getDepth()); }); 
   // Sort the entities based on this criterion
    sorted.sort(comp); 
   // Now, draw the sorted entities instead of the original entities vector
    
Back to project statement (part 3)