Search code examples
c++sfml

Why is nothing happening when I implement travel in my infectious disease simulation?


I have an issue with my infectious disease simulation. Here is the relevant code:

void City::updateTravel(City &cityDestination) {

    vector<bool> travelArray;

    for (int i = 0; i < population; i++) {

        if (people[i].travel(cityDestination.getCity())) {
            travelArray.push_back(true);
        }
        else {
            travelArray.push_back(false);
        }
        
    }
    vector<Person> newArray;
    for (int i = 0; i < travelArray.size(); i++) {
        if (travelArray[i] == true) {
            cityDestination.add(people[i]);
            cityDestination.setPopulation(cityDestination.getPeople().size());
        }
        else {
            newArray.push_back(people[i]);
        }
    }
    people = newArray;
    this->setPopulation(people.size());
    
}

Here's the travel function in the Person class: (determines if a particular person is going to travel, and teleports them to the location of the other city)

bool Person::travel(RectangleShape city) {
    int randNumber = rand() % 50 + 1;
    if (randNumber == 20) {
        Vector2f randomPosition = Vector2f(float(rand() % int(2 * city.getOrigin().x + 1 - 4 * radius)) + 2 * radius + city.getPosition().x - city.getOrigin().x, float(rand() % int(2 * city.getOrigin().y + 1 - 4 * radius)) + 2 * radius + city.getPosition().y - city.getOrigin().y);
        this->setPosition(randomPosition);
        return true;
    }
    return false;
}

Here's the City class:

class City
{
private:
    std::vector<Person> people;
    float populationDensity;
    int population;
    int activeCases;
    int totalCases;
    int cellSize;
    Vector2i citySize;
    RectangleShape city;


public:
    City(Vector2f position, int startingInfection);
    ~City();
    void updateInfection();
    void updateTravel(City &cityDestination);
    int getActiveCases();
    int getTotalCases();
    int getPopulation();
    std::vector<Person> &getPeople();
    void add(Person newPeople);
    RectangleShape getCity();
    int getCellSize();
    void setPosition(Vector2f position);
    void setPopulation(int newPopulation);
    
};

Here's the Person class:

class Person
{
private:
    float infectivity;
    CircleShape personSprite;
    int status;
    float radius;
    float pointCount;
    Vector2f personPosition;
    Vector2f personVelocity;
    int maxVelocity = 100;
    int maxRadius = 5;
    float expirationTime;
    int incubationTime;


public:
    void update(RectangleShape box);
    Vector2f getPosition();
    void setPosition(Vector2f position);
    float getRadius();
    void setVelocity(Vector2f velocity);
    void setRecoveryTime(float recoveryTime);
    Vector2f getVelocity();
    void bouncex();
    void bouncey();
    CircleShape getSprite();
    void setStatus(int stat);
    int getStatus();
    void infection();
    void nullinfection();
    bool travel(RectangleShape city);
    
    Person();
    Person(RectangleShape box);
    ~Person();
};

In main.cpp, I just have the following: (iterates over all pairs of cities)

for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
        if (i != j) {
            cities[i].updateTravel(cities[j]);
        }
    }
}

The issue is that the updateTravel function does nothing. If I comment it out, the number of infections stays the same, which clearly cannot be. I have seemingly tried everything, including removing the boolean array in updateTravel, handling all the travel in the Person class, and including travel directly in the City class with no code in main.cpp. I don't know why it's not working.


Solution

  • It seems to be a problem with how to define the counters of these loops:

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            if (i != j) {
                cities[i].updateTravel(cities[j]);
            }
        }
    }
    

    Try this:

    for (int i = 0; i < 2; i++) {
        for (int j = i+1; j < 2; j++) {
            if (i != j) {
                cities[i].updateTravel(cities[j]);
            }
        }
    }
    

    Does it solve the problem?