test-econ-model/pop-simple.cpp

88 lines
2.2 KiB
C++
Raw Normal View History

2022-11-30 02:07:09 -08:00
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include "econ.hpp"
#include "io.hpp"
std::vector<int> ages;
std::vector<int> balances;
const int N_o = 10;
const int N = 10000;
const int DURATION = 1000;
const int MAX_AGE = 100;
const int STARTING_BALANCE=50000;
const int LOGISTIC_CARRYING_CAPACITY = 100000;
const float LOGISTIC_GROWTH_RATE = 0.04;
const int LOGISTIC_MIDPOINT = DURATION / 2;
// pop modeling functions
void kill_even_redist(int id) {
int bal = balances.at(id);
ages.erase(ages.begin()+id);
balances.erase(balances.begin()+id);
int alloc_each = bal / balances.size();
for (int i = 0; i < balances.size(); i++) {
balances.at(i) += alloc_each;
}
}
void kill_inheritance(int id) {
int bal = balances.at(id);
ages.erase(ages.begin()+id);
balances.erase(balances.begin()+id);
int recipient_id = (balances.size() - 1) * random();
balances.at(recipient_id) += bal;
}
void age_all() {
for (int i = 0; i < ages.size(); i++) {
ages.at(i) += 1;
if (ages.at(i) > MAX_AGE) {
// kill_inheritance(i);
kill_even_redist(i);
}
}
}
int logistic_population_func(int year) {
// https://en.wikipedia.org//wiki/Logistic_function
return (LOGISTIC_CARRYING_CAPACITY /
(1 + exp(-LOGISTIC_GROWTH_RATE * (year - LOGISTIC_MIDPOINT))));
}
void adjust_population(int year) {
int diff = logistic_population_func(year) - balances.size();
if (diff > 0) {
for (int i = 0; i < diff; i++) {
ages.push_back(1);
balances.push_back(0);
}
}
}
int main() {
// init state
ages.assign(N_o, 1);
balances.assign(N_o, STARTING_BALANCE);
for (int year = 0; year < DURATION; year++) {
age_all();
adjust_population(year);
// for (int j = 0; j < balances.size(); j++) {
// economic_decision_simple(balances, j, 0.4);
// }
}
// std::sort(balances.begin(), balances.end(), std::greater<int>());
dump_balances_and_ages_csv(balances, ages, "pop-balances-out");
std::cout << "Finished with " << balances.size() << " balances\n";
return 0;
}