为什么在我的传染病模拟中实施旅行时没有任何反应?
Why is nothing happening when I implement travel in my infectious disease simulation?
我的传染病模拟有问题。这是相关代码:
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());
}
Person class 中的旅行功能如下:(确定某个人是否要旅行,并将他们传送到其他城市的位置)
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;
}
这里是城市 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);
};
这是人 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();
};
在 main.cpp 中,我只有以下内容:(遍历所有城市对)
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
if (i != j) {
cities[i].updateTravel(cities[j]);
}
}
}
问题是 updateTravel 函数什么都不做。如果我将其注释掉,感染数量将保持不变,这显然不可能。我似乎已经尝试了所有方法,包括删除 updateTravel 中的布尔数组,处理 Person class 中的所有旅行,以及包括直接在城市 class 中旅行,而 main.cpp 中没有代码。我不知道为什么它不起作用。
好像是怎么定义这些循环的计数器的问题:
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
if (i != j) {
cities[i].updateTravel(cities[j]);
}
}
}
试试这个:
for (int i = 0; i < 2; i++) {
for (int j = i+1; j < 2; j++) {
if (i != j) {
cities[i].updateTravel(cities[j]);
}
}
}
是否解决了问题?
我的传染病模拟有问题。这是相关代码:
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());
}
Person class 中的旅行功能如下:(确定某个人是否要旅行,并将他们传送到其他城市的位置)
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;
}
这里是城市 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);
};
这是人 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();
};
在 main.cpp 中,我只有以下内容:(遍历所有城市对)
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
if (i != j) {
cities[i].updateTravel(cities[j]);
}
}
}
问题是 updateTravel 函数什么都不做。如果我将其注释掉,感染数量将保持不变,这显然不可能。我似乎已经尝试了所有方法,包括删除 updateTravel 中的布尔数组,处理 Person class 中的所有旅行,以及包括直接在城市 class 中旅行,而 main.cpp 中没有代码。我不知道为什么它不起作用。
好像是怎么定义这些循环的计数器的问题:
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
if (i != j) {
cities[i].updateTravel(cities[j]);
}
}
}
试试这个:
for (int i = 0; i < 2; i++) {
for (int j = i+1; j < 2; j++) {
if (i != j) {
cities[i].updateTravel(cities[j]);
}
}
}
是否解决了问题?