[wip] more stuff

master
michael 2022-11-30 18:07:09 +08:00
parent c31c8308d8
commit 263bde65fb
11 changed files with 244 additions and 82 deletions

22
.gitignore vendored
View File

@ -1,16 +1,6 @@
# output output
build/
# cmake general
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
Footer
*
!CMakeLists.txt
!*.cpp
!*.hpp
!include/
!.gitignore

62
.vscode/settings.json vendored
View File

@ -1,6 +1,64 @@
{
"cmake.sourceDirectory": "${workspaceFolder}/v1",
"cmake.sourceDirectory": "${workspaceFolder}/.",
"files.associations": {
"vector": "cpp"
"vector": "cpp",
"algorithm": "cpp",
"chrono": "cpp",
"limits": "cpp",
"random": "cpp",
"cstdlib": "cpp",
"__bit_reference": "cpp",
"__split_buffer": "cpp",
"initializer_list": "cpp",
"iterator": "cpp",
"string": "cpp",
"string_view": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__errc": "cpp",
"__functional_base": "cpp",
"__locale": "cpp",
"__mutex_base": "cpp",
"__nullptr": "cpp",
"__string": "cpp",
"__threading_support": "cpp",
"__tuple": "cpp",
"atomic": "cpp",
"bit": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"exception": "cpp",
"functional": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"locale": "cpp",
"memory": "cpp",
"mutex": "cpp",
"new": "cpp",
"ostream": "cpp",
"ratio": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"utility": "cpp",
"deque": "cpp",
"fstream": "cpp",
"iomanip": "cpp",
"stack": "cpp"
}
}

10
CMakeLists.txt Normal file
View File

@ -0,0 +1,10 @@
project(test_econ_model)
# set(models "fixed-simple, lending, pop-simple")
# foreach(model models)
# add_executable(bin/${model} ${model}.cpp)
# endforeach()
make_directory(bin)
include_directories(include/)
add_executable(./bin/fixed-simple fixed-simple.cpp)
add_executable(./bin/wealth-flow wealth-flow.cpp)
add_executable(./bin/pop-simple pop-simple.cpp)

17
fixed-simple.cpp Normal file
View File

@ -0,0 +1,17 @@
#include<iostream>
#include<vector>
std::vector<int> balances;
const int N = 10000;
const int DURATION = 1000;
void frame_year() {
for (int i = 0; i < balances.size(); i++) {
// actions
}
}
int main() {
return 0;
}

12
include/econ.hpp Normal file
View File

@ -0,0 +1,12 @@
#include<iostream>
#include <vector>
void economic_decision_simple(std::vector<int> &balances, int id, float spend_ratio) {
// assert(spend_ratio > 0 && spend_ratio <= 1);
std::cout << "test" << std::endl;
int total_bal = balances.at(id);
int spendable = (random() * spend_ratio) * total_bal;
int recipient_id = random() * (balances.size());
balances.at(id) -= spendable;
balances.at(recipient_id) += spendable;
}

32
include/io.hpp Normal file
View File

@ -0,0 +1,32 @@
#include <vector>
#include <string>
#include <cstdio>
#include <iostream>
#include <fstream>
void dump_balances_csv(std::vector<int> balances, std::string name) {
std::string csv_data = "Id,Balance\n";
for (int i = 0; i < balances.size(); i++) {
csv_data += std::to_string(i+1)+ "," +
std::to_string(balances[i]) + "\n";
}
std::ofstream file;
file.open(name + ".csv");
file << csv_data;
file.close();
}
void dump_balances_and_ages_csv(std::vector<int> balances, std::vector<int> ages, std::string name) {
assert(balances.size() == ages.size());
std::string csv_data = "Id,Balance,Age\n";
for (int i = 0; i < balances.size(); i++) {
csv_data += std::to_string(i+1)+ "," +
std::to_string(ages[i]) + "," +
std::to_string(balances[i]) + "\n";
}
std::ofstream file;
file.open(name + ".csv");
file << csv_data;
file.close();
}

87
pop-simple.cpp Normal file
View File

@ -0,0 +1,87 @@
#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;
}

5
v1/.gitignore vendored
View File

@ -1,5 +0,0 @@
*
!CMakeLists.txt
!*.cpp
!*.hpp
!.gitignore

View File

@ -1,2 +0,0 @@
project(test_econ_model)
add_executable(main main.cpp)

View File

@ -1,57 +0,0 @@
#include <iostream>
#include <vector>
#include <cmath>
#include <random>
std::vector<float> ages;
std::vector<int> balances;
const int N = 10000;
const int DURATION = 1000;
const int MAX_AGE = 100;
const int LOGISTIC_CARRYING_CAPACITY = 100000;
const int LOGISTIC_GROWTH_RATE = 1;
const int LOGISTIC_MIDPOINT = DURATION / 2;
// state mutation funcs
void transfer_balance(int from, int to, int balance) {
balances[from] -= balance;
balances[to] += balance;
}
void kill_even_redist(int id) {
ages.erase(ages.begin()+id);
balances.erase(balances.begin()+id);
}
void kill_inheritance(int id) {
ages.erase(ages.begin()+id);
balances.erase(balances.begin()+id);
}
void frame_quarter() {
for (int i = 0; i < ages.size(); i++) {
ages[i] += 1;
if (ages[i] > MAX_AGE) {
kill_inheritance(i);
}
}
}
// modeling functions
float logistic_func(int year) {
// https://en.wikipedia.org//wiki/Logistic_function
return LOGISTIC_CARRYING_CAPACITY / (1 + (-LOGISTIC_GROWTH_RATE * exp(year - LOGISTIC_MIDPOINT)));
}
int main() {
// init state
for (int i = 0; i < 1000; i++) {
std::cout << logistic_func(i) << std::endl;
}
return 0;
}

20
wealth-flow.cpp Normal file
View File

@ -0,0 +1,20 @@
#include<iostream>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<algorithm>
std::vector<int> balances;
const int N = 10000;
const int DURATION = 1000;
void frame_year() {
for (int i = 0; i < balances.size(); i++) {
}
}
int main() {
return 0;
}