From 263bde65fb98251513d0d7931781ccfe52c49c50 Mon Sep 17 00:00:00 2001 From: turtlebasket Date: Wed, 30 Nov 2022 18:07:09 +0800 Subject: [PATCH] [wip] more stuff --- .gitignore | 22 +++-------- .vscode/settings.json | 62 +++++++++++++++++++++++++++++- CMakeLists.txt | 10 +++++ fixed-simple.cpp | 17 +++++++++ include/econ.hpp | 12 ++++++ include/io.hpp | 32 ++++++++++++++++ pop-simple.cpp | 87 +++++++++++++++++++++++++++++++++++++++++++ v1/.gitignore | 5 --- v1/CMakeLists.txt | 2 - v1/main.cpp | 57 ---------------------------- wealth-flow.cpp | 20 ++++++++++ 11 files changed, 244 insertions(+), 82 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 fixed-simple.cpp create mode 100644 include/econ.hpp create mode 100644 include/io.hpp create mode 100644 pop-simple.cpp delete mode 100644 v1/.gitignore delete mode 100644 v1/CMakeLists.txt delete mode 100644 v1/main.cpp create mode 100644 wealth-flow.cpp diff --git a/.gitignore b/.gitignore index 07c6b6f..4e7d853 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/.vscode/settings.json b/.vscode/settings.json index 304dc73..ac00b4c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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" } } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..287b8c2 --- /dev/null +++ b/CMakeLists.txt @@ -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) diff --git a/fixed-simple.cpp b/fixed-simple.cpp new file mode 100644 index 0000000..3194b99 --- /dev/null +++ b/fixed-simple.cpp @@ -0,0 +1,17 @@ +#include +#include + +std::vector 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; +} diff --git a/include/econ.hpp b/include/econ.hpp new file mode 100644 index 0000000..cd65df7 --- /dev/null +++ b/include/econ.hpp @@ -0,0 +1,12 @@ +#include +#include + +void economic_decision_simple(std::vector &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; +} diff --git a/include/io.hpp b/include/io.hpp new file mode 100644 index 0000000..344bbb2 --- /dev/null +++ b/include/io.hpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include + +void dump_balances_csv(std::vector 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 balances, std::vector 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(); +} diff --git a/pop-simple.cpp b/pop-simple.cpp new file mode 100644 index 0000000..e485612 --- /dev/null +++ b/pop-simple.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +#include +#include +#include "econ.hpp" +#include "io.hpp" + +std::vector ages; +std::vector 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()); + dump_balances_and_ages_csv(balances, ages, "pop-balances-out"); + std::cout << "Finished with " << balances.size() << " balances\n"; + + return 0; +} diff --git a/v1/.gitignore b/v1/.gitignore deleted file mode 100644 index 095be9b..0000000 --- a/v1/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -* -!CMakeLists.txt -!*.cpp -!*.hpp -!.gitignore diff --git a/v1/CMakeLists.txt b/v1/CMakeLists.txt deleted file mode 100644 index a77de4d..0000000 --- a/v1/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -project(test_econ_model) -add_executable(main main.cpp) diff --git a/v1/main.cpp b/v1/main.cpp deleted file mode 100644 index 1107fbc..0000000 --- a/v1/main.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include -#include -#include -#include - -std::vector ages; -std::vector 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; -} diff --git a/wealth-flow.cpp b/wealth-flow.cpp new file mode 100644 index 0000000..101421b --- /dev/null +++ b/wealth-flow.cpp @@ -0,0 +1,20 @@ +#include +#include +#include +#include +#include + +std::vector balances; + +const int N = 10000; +const int DURATION = 1000; + +void frame_year() { + for (int i = 0; i < balances.size(); i++) { + } +} + +int main() { + + return 0; +}