diff --git a/.vscode/settings.json b/.vscode/settings.json index 7f4d72e..541feb7 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -62,6 +62,8 @@ "stack": "cpp", "numeric": "cpp", "array": "cpp", - "map": "cpp" + "map": "cpp", + "list": "cpp", + "sstream": "cpp" } } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 2bfd8e8..faccaab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,16 @@ execute_process(COMMAND bash ./scripts/setup-include.sh) make_directory(bin) include_directories(include/) +# fetch latest argparse +include(FetchContent) +FetchContent_Declare( + argparse + GIT_REPOSITORY https://github.com/p-ranav/argparse.git +) +FetchContent_MakeAvailable(argparse) + add_executable(pop-simple pop-simple.cpp) +target_link_libraries(pop-simple argparse) # add_executable(./bin/fixed-simple fixed-simple.cpp) # add_executable(./bin/wealth-flow wealth-flow.cpp) diff --git a/pop-simple.cpp b/pop-simple.cpp index de05068..41dc08e 100644 --- a/pop-simple.cpp +++ b/pop-simple.cpp @@ -7,17 +7,21 @@ #include #include #include + +#include + #include "econ.hpp" #include "io.hpp" #include "util.hpp" -std::map config; +// std::map config; +argparse::ArgumentParser program("pop-simple"); -#define c(a) config[a] -#define ci(a) std::stoi(config[a]) -#define cf(a) std::stof(config[a]) +#define arg(a) program[a] +#define iarg(a) std::stoi(program[a]) +#define farg(a) std::stof(program[a]) -#define csets(k, v) config[k] = v +#define cset(k, v) config[k] = v #define csetn(k, v) config[k] = std::to_string(v); std::vector ages; @@ -71,7 +75,7 @@ void kill_inheritance(int id) { void age_all(estate_mode mode) { for (int i = 0; i < ages.size(); i++) { ages.at(i) += 1; - if (ages.at(i) > ci("MAX_AGE")) { + if (ages.at(i) > iarg("MAX_AGE")) { switch (mode) { case INHERITANCE: kill_inheritance(i); @@ -86,8 +90,8 @@ void age_all(estate_mode mode) { int logistic_population_func(int year) { // https://en.wikipedia.org//wiki/Logistic_function - return ci("N_o") + (ci("N_f") - ci("N_o")) / - (1 + exp(-cf("GROWTH_RATE") * (year - ci("DURATION")/2))); + return iarg("N_o") + (iarg("N_f") - iarg("N_o")) / + (1 + exp(-farg("GROWTH_RATE") * (year - ci("DURATION")/2))); } void adjust_population(int year) { @@ -95,16 +99,31 @@ void adjust_population(int year) { if (diff > 0) { for (int i = 0; i < diff; i++) { ages.push_back(1); - balances.push_back(ci("STARTING_BALANCE_NEW")); + balances.push_back(iarg("STARTING_BALANCE_NEW")); } } } -int main() { - // set config - make configurable with params later +int main(int argc, char* argv[]) { + // set config via cli args + program.add_argument("estate", "-e") + .help("Estate distribution mode (redistribute, inheritance)") + .default_value(INHERITANCE); + + program.add_argument("time", "-t") + .help("Duration in years of simulation.") + .default_value(1000); + + try { + program.parse_args(argc, argv); + } + catch (const std::runtime_error& err) { + std::cerr << err.what() << std::endl; + std::cerr << program; + std::exit(1); + } // model params - csetn("DURATION", 1000); csetn("MAX_AGE", 81); csetn("INIT_BALANCE_EACH", 5000); csetn("STARTING_BALANCE_NEW", 1000); // effectively inflation rate @@ -158,7 +177,16 @@ int main() { } // std::sort(balances.begin(), balances.end(), std::greater()); - dump_balances_and_ages_csv(balances, ages, "pop-balances-out"); + std::string ext; + switch (emode) { + case EVEN_REDIST: + ext = "even"; + break; + case INHERITANCE: + ext = "inheritance"; + break; + } + dump_balances_and_ages_csv(balances, ages, "pop-simple-results-"+ext); std::cout << "Finished with " << balances.size() << " balances\n"; return 0; diff --git a/scripts/setup-include.sh b/scripts/setup-include.sh deleted file mode 100644 index a30f9b9..0000000 --- a/scripts/setup-include.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -if [ ! -f include/argparse/argparse.hpp ]; then -mkdir -p include/argparse -curl https://raw.githubusercontent.com/p-ranav/argparse/997da9255618311d1fcb0135ce86022729d1f1cb/include/argparse/argparse.hpp \ - -o include/argparse/argparse.hpp -fi diff --git a/scripts/visualize-pop-results.py b/scripts/visualize-pop-results.py index 0c7f60b..5bd0143 100644 --- a/scripts/visualize-pop-results.py +++ b/scripts/visualize-pop-results.py @@ -1,9 +1,20 @@ from matplotlib import pyplot as plt import pandas as pd -with open("../pop-balances-out.csv", "r") as file: - results = pd.read_csv(file) +with open("../pop-balances-out-inheritance.csv", "r") as file: + pop_inheritance_results = pd.read_csv(file) -# balance avg -plt.hist(results.loc[:,"Balance"].sort_values(), bins=30) +plt.hist(pop_inheritance_results.loc[:,"Balance"].sort_values(), bins=30) +plt.title("Wealth distribution (Inheritance)") +plt.xlabel("dollarydoos") +plt.ylabel("participants") +plt.show() + +with open("../pop-balances-out-redist.csv", "r") as file: + pop_redist_results = pd.read_csv(file) + +plt.hist(pop_redist_results.loc[:,"Balance"].sort_values(), bins=30) +plt.title("Wealth distribution (") +plt.xlabel("dollarydoos") +plt.ylabel("participants") plt.show()