Skip to content

Commit

Permalink
Add experiments script
Browse files Browse the repository at this point in the history
  • Loading branch information
LadyChristina committed Nov 19, 2021
1 parent 058f9ab commit 3061720
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 21 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
**.csv
**.spec
**.tex
**.sh
**.pkl
**/dist
**/build
Expand Down
13 changes: 7 additions & 6 deletions batch-run.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ def main():
help='The identifier of this execution, to be used for naming the output files.')
parser.add_argument('--seed', default=42,
help='Seed for reproducibility (set seed=None if reproducibility is not required). Default is 42.')
parser.add_argument('--max_iterations', type=int, default=1000,
parser.add_argument('--max_iterations', type=int, default=5000,
help='The maximum number of iterations of the system. Default is 1000.')
parser.add_argument('--n', nargs="+", type=int, default=1000,
help='The number of players (natural number). Default is 100.')
parser.add_argument('--k', nargs="+", type=int, default=[95, 110, 2],
parser.add_argument('--k', nargs="+", type=int, default=[100, 200, 2],
help='The k value of the system (natural number). Default is 10.')
parser.add_argument('--alpha', nargs="+", type=float, default=0.3,
help='The alpha value of the system (decimal number between 0 and 1). Default is 0.3')
Expand Down Expand Up @@ -66,6 +66,7 @@ def main():
"median_pools_per_operator": sim.get_median_pools_per_operator,
"avgSatRate": sim.get_avg_sat_rate,
"nakamotoCoeff": sim.get_nakamoto_coefficient,
"StatisticalDistance": sim.get_controlled_stake_distr_stat_dist,
# "NCR": sim.get_NCR,
#"MinAggregatePledge": sim.get_min_aggregate_pledge,
# "pledge_rate": sim.get_pledge_rate,
Expand Down Expand Up @@ -93,15 +94,15 @@ def main():
run_data_MP = batch_run_MP.get_model_vars_dataframe()
# print(run_data_MP.head())

output_dir = "output"
output_dir = "output/19-11-21"
pickled_batch_run_data = output_dir + "/batch-run-data.pkl"
with open(pickled_batch_run_data, "wb") as pkl_file:
pkl.dump(run_data_MP, pkl_file)

variable_param = list(variable_params.keys())[0]
colours = [np.random.rand(3, ) for i in range(len(model_reporters))]
for i, model_reporter in enumerate(model_reporters):
plot_aggregate_data(run_data_MP, variable_param, model_reporter, colours[i], args_dict["execution_id"])
plot_aggregate_data(run_data_MP, variable_param, model_reporter, colours[i], args_dict["execution_id"], output_dir)

# ordered dicts with data from each step of each run (the combinations of variable params act as the keys)
# for example data_collector_model[(0.1, 0.02, 1)] shows the values of the parameters collected at model level
Expand All @@ -112,8 +113,8 @@ def main():
plt.show()


def plot_aggregate_data(df, variable_param, model_reporter, color, exec_id):
figures_dir = "output/figures/"
def plot_aggregate_data(df, variable_param, model_reporter, color, exec_id, output_dir):
figures_dir = output_dir + "/figures/"
plt.figure()
plt.scatter(df[variable_param], df[model_reporter], color=color)
plt.xlabel(variable_param)
Expand Down
31 changes: 31 additions & 0 deletions experiments.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

python3.9 main.py --simulation_id=baseline-100-10 --n=100 --k=10 --alpha=0.3

python3.9 main.py --simulation_id=baseline-1000-100 --n=1000 --k=100 --alpha=0.3

python3.9 main.py --simulation_id=abstention-30-100-10 --n=100 --k=10 --alpha=0.3 --abstention_rate=0.3

python3.9 main.py --simulation_id=parameter-change-k-100 --n=100 --k 10 20 --alpha=0.3

python3.9 main.py --simulation_id=parameter-change-alpha-100-10 --n=100 --k=10 --alpha 0.03 0.3

python3.9 main.py --simulation_id=parameter-change-k-1000 --n=1000 --k 100 200 --alpha=0.3

python3.9 main.py --simulation_id=parameter-change-alpha-1000-100 --n=1000 --k=100 --alpha 0.03 0.3

python3.9 main.py --simulation_id=alpha-zero-100-10 --n=100 --k=10 --alpha=0

python3.9 main.py --simulation_id=alpha-zero-1000-100 --n=1000 --k=100 --alpha=0

python3.9 batch-run.py --execution_id=varying-k-100 --n=100 --k 1 51 1 --alpha=0.3

python3.9 batch-run.py --execution_id=varying-alpha-1000-100 --n=1000 --k=100 --alpha 0 10 0.25

python3.9 batch-run.py --execution_id=varying-abstention-rate-1000-100 --n=1000 --k=100 --alpha=0.3 --abstention_rate 0 0.91 0.01

python3.9 batch-run.py --execution_id=varying-k-1000 --n=1000 --k 1 251 2 --alpha=0.3




4 changes: 3 additions & 1 deletion logic/model_reporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,11 @@ def get_controlled_stake_mean_abs_diff(model):
def get_controlled_stake_distr_stat_dist(model):
"""
:param model:
:return: the statistical difference of the distributions of the stake that players control
:return: the statistical distance of the distributions of the stake that players control
(how they started vs how they ended up)
"""
if not model.has_converged():
return -1
active_players = {player_id: player for player_id, player in model.get_players_dict().items() if
not player.abstains}
pools = model.get_pools_list()
Expand Down
2 changes: 1 addition & 1 deletion logic/sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def dump_state_to_csv(self):
"Private" if pool.is_private else "Public"]
for pool in pools])

output_dir = "output/"
output_dir = "output/19-11-21/"
path = pathlib.Path.cwd() / output_dir
pathlib.Path(path).mkdir(parents=True, exist_ok=True)
filename = (path / (self.simulation_id + '-final_configuration.csv')) \
Expand Down
2 changes: 1 addition & 1 deletion logic/stakeholder.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def calculate_delegator_utility(self, pool, stake_allocation):
m_factor = (1 - pool.margin) * q
u_0 = (r - pool.cost)
u = m_factor * u_0
utility = max(0, u)
utility = u if u > 0 else 0
return utility

# how does a myopic player decide whether to open a pool or not? -> for now we assume that all players play non-myopically when it comes to pool moves
Expand Down
21 changes: 10 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ def main():
print("Let the Pooling Games begin!")

parser = argparse.ArgumentParser(description='Pooling Games')
parser.add_argument('--n', type=int, default=1000,
parser.add_argument('--n', type=int, default=100,
help='The number of players (natural number). Default is 100.')
parser.add_argument('--k', nargs="+", type=int, default=40,
parser.add_argument('--k', nargs="+", type=int, default=10,
help='The k value of the system (natural number). Default is 10.')
parser.add_argument('--alpha', nargs="+", type=float, default=0.3,
help='The alpha value of the system (decimal number between 0 and 1). Default is 0.3')
Expand Down Expand Up @@ -91,8 +91,8 @@ def main():
with open(pickled_simulation_filename, "wb") as pkl_file:
pkl.dump(sim, pkl_file)

output_dir = "output/"
figures_dir = "output/figures/"
output_dir = "output/19-11-21/"
figures_dir = output_dir + "figures/"
path = pathlib.Path.cwd() / figures_dir
pathlib.Path(path).mkdir(parents=True, exist_ok=True)

Expand All @@ -112,16 +112,16 @@ def main():
pivot_steps = sim.pivot_steps

plot_line(simulation_id, sim_df["#Pools"], 'C0', "Number of pools over time", "Round",
"#Pools", "poolCount", equilibrium_steps, pivot_steps, True)
"#Pools", "poolCount", equilibrium_steps, pivot_steps, figures_dir, True)

plot_line(simulation_id, sim_df["AvgPledge"], 'red', "Average pledge over time", "Round",
"Average pledge", "avgPledge", equilibrium_steps, pivot_steps, True)
"Average pledge", "avgPledge", equilibrium_steps, pivot_steps, figures_dir, True)

plot_line(simulation_id, sim_df["TotalPledge"], 'purple', "Total pledge over time", "Round",
"Total pledge", "totalPledge", equilibrium_steps, pivot_steps, True)
"Total pledge", "totalPledge", equilibrium_steps, pivot_steps, figures_dir, True)

plot_line(simulation_id, sim_df["MeanAbsDiff"], 'green', "Mean Absolute Difference of Controlled Stake", "Round",
"Mean abs diff", "meanAbsDiff", equilibrium_steps, pivot_steps, False)
"Mean abs diff", "meanAbsDiff", equilibrium_steps, pivot_steps, figures_dir, False)

'''pool_sizes_by_step = sim_df["PoolSizes"] # todo fix
# print(pool_sizes_by_step)
Expand All @@ -136,8 +136,7 @@ def main():


def plot_line(simulation_id, data, color, title, x_label, y_label, filename, equilibrium_steps, pivot_steps,
show_equilibrium=False):
figures_dir = "output/figures/"
figures_dir, show_equilibrium=False):
path = pathlib.Path.cwd() / figures_dir
pathlib.Path(path).mkdir(parents=True, exist_ok=True)

Expand All @@ -153,7 +152,7 @@ def plot_line(simulation_id, data, color, title, x_label, y_label, filename, equ
for i, step in enumerate(pivot_steps):
label = "Parameter change" if i == 0 else ""
plt.plot(step, data[step], 'x', label=label, c=pivot_colour)
plt.title(title)
#plt.title(title)
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.legend()
Expand Down

0 comments on commit 3061720

Please sign in to comment.