Skip to content

Commit

Permalink
Fix min delegation violation + rename id
Browse files Browse the repository at this point in the history
  • Loading branch information
LadyChristina committed Nov 22, 2021
1 parent 7f8f9f3 commit 20f4fcb
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 38 deletions.
4 changes: 2 additions & 2 deletions batch-run.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def main():
help='The percentage of players that will abstain from the game in this run. Default is 10%%.')

fixed_params = {
"simulation_id": "temp",
"execution_id": "temp",
"player_activation_order": "Random",
"relative_utility_threshold": 0,
"myopic_fraction": 0,
Expand Down Expand Up @@ -94,7 +94,7 @@ def main():
run_data_MP = batch_run_MP.get_model_vars_dataframe()
# print(run_data_MP.head())

output_dir = "output/19-11-21"
output_dir = "output/22-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)
Expand Down
28 changes: 15 additions & 13 deletions experiments.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
#!/bin/bash

python3.9 main.py --simulation_id=baseline-100-10 --n=100 --k=10 --alpha=0.3
python3.9 main.py --execution_id=single-run-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 --execution_id=single-run-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 --execution_id=single-run-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 --execution_id=single-run-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 --execution_id=single-run-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 --execution_id=single-run-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 --execution_id=single-run-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 --execution_id=single-run-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 main.py --execution_id=single-run-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=batch-run-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=batch-run-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=batch-run-alpha-zoom-in-1000-100 --n=1000 --k=100 --alpha 0 1 0.01

python3.9 batch-run.py --execution_id=varying-k-1000 --n=1000 --k 1 251 2 --alpha=0.3
python3.9 batch-run.py --execution_id=batch-run-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=batch-run-k-1000 --n=1000 --k 1 251 2 --alpha=0.3



Expand Down
14 changes: 7 additions & 7 deletions logic/sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(
relative_utility_threshold=0, absolute_utility_threshold=1e-9,
min_steps_to_keep_pool=5, pool_splitting=True, seed=42, pareto_param=2.0, max_iterations=1000,
common_cost=1e-4, cost_min=0.001, cost_max=0.002, player_activation_order="Random", total_stake=1,
ms=10, simulation_id=''
ms=10, execution_id=''
):
# todo make sure that the input is valid? n > 0, 0 < k <= n

Expand Down Expand Up @@ -90,7 +90,7 @@ def __init__(

self.perceived_active_stake = total_stake
self.beta = total_stake / self.k
self.simulation_id = simulation_id if simulation_id != '' else self.generate_simulation_id()
self.execution_id = execution_id if execution_id != '' else self.generate_execution_id()

self.running = True # for batch running and visualisation purposes
self.schedule = self.player_activation_orders[player_activation_order](self)
Expand Down Expand Up @@ -245,18 +245,18 @@ def dump_state_to_csv(self):
"Private" if pool.is_private else "Public"]
for pool in pools])

output_dir = "output/19-11-21/"
output_dir = "output/22-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')) \
if self.has_converged() else (path / (self.simulation_id + '-intermediate-configuration.csv'))
filename = (path / (self.execution_id + '-final_configuration.csv')) \
if self.has_converged() else (path / (self.execution_id + '-intermediate-configuration.csv'))
with open(filename, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(row_list)

# temporary, used to extract results in latex format for easier reporting
latex_dir = output_dir + "latex/"
hlp.to_latex(row_list, self.simulation_id, latex_dir)
hlp.to_latex(row_list, self.execution_id, latex_dir)

def get_pools_list(self):
return list(self.pools.values())
Expand All @@ -270,7 +270,7 @@ def get_players_list(self):
def get_status(self):
print("Step {}: {} pools".format(self.schedule.steps, len(self.pools)))

def generate_simulation_id(self):
def generate_execution_id(self):
return "".join(['-' + str(key) + '=' + str(value) for key, value in self.arguments.items()
if type(value) == bool or type(value) == int or type(value) == float])[:147]

Expand Down
2 changes: 1 addition & 1 deletion logic/stakeholder.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ def find_delegation_move_desirability(self, stake_to_delegate=None):
stake_to_saturation)
stake_to_delegate -= allocation
allocations[pool_id] = allocation
if stake_to_delegate == 0:
if stake_to_delegate <= MIN_STAKE_UNIT:
break
# there were not enough non-saturated pools for the player to delegate their stake to
# so they have to choose a saturated pool
Expand Down
30 changes: 15 additions & 15 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def main():
parser.add_argument('--ms', type=int, default=10,
help='The minimum consecutive idle steps that are required to declare convergence. '
'Default is 10. But if min_steps_to_keep_pool > ms then ms = min_steps_to_keep_pool + 1. ')
parser.add_argument('--simulation_id', type=str, default='unnamed-simulation',
parser.add_argument('--execution_id', type=str, default='unnamed-simulation',
help='An optional identifier for the specific simulation run, '
'which will be included in the output.')

Expand All @@ -74,53 +74,53 @@ def main():
pool_splitting=args.pool_splitting,
max_iterations=args.max_iterations,
ms=args.ms,
simulation_id=args.simulation_id
execution_id=args.execution_id
)

sim.run_model()

sim_df = sim.datacollector.get_model_vars_dataframe()

simulation_id = args.simulation_id
if simulation_id == '':
execution_id = args.execution_id
if execution_id == '':
# No identifier was provided by the user, so we construct one based on the simulation's parameter values
simulation_id = "".join(['-' + str(key) + '=' + str(value) for key, value in sim.arguments.items()
execution_id = "".join(['-' + str(key) + '=' + str(value) for key, value in sim.arguments.items()
if type(value) == bool or type(value) == int or type(value) == float])[:180]

pickled_simulation_filename = "output/simulation-object-" + simulation_id + ".pkl"
pickled_simulation_filename = "output/simulation-object-" + execution_id + ".pkl"
with open(pickled_simulation_filename, "wb") as pkl_file:
pkl.dump(sim, pkl_file)

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

margin_changes = sim_df["MarginChanges"]
'''plot_line(simulation_id, sim_df["MarginChanges"], 'C0', "Number of pools over time", "Round",
'''plot_line(execution_id, sim_df["MarginChanges"], 'C0', "Number of pools over time", "Round",
"#Pools", "poolCount", equilibrium_steps=[], pivot_steps=[])'''

pool_nums = sim_df["#Pools"]
if sim.schedule.steps >= sim.max_iterations:
# If the max number of iterations was reached, then we save the data about the pool numbers
# in order to later analyse the statistic properties of the execution
filename = output_dir + simulation_id + "-poolCount" + ".pkl"
filename = output_dir + execution_id + "-poolCount" + ".pkl"
with open(filename, "wb") as pkl_file:
pkl.dump(pool_nums, pkl_file)

equilibrium_steps = sim.equilibrium_steps
pivot_steps = sim.pivot_steps

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

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

plot_line(simulation_id, sim_df["TotalPledge"], 'purple', "Total pledge over time", "Round",
plot_line(execution_id, sim_df["TotalPledge"], 'purple', "Total pledge over time", "Round",
"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",
plot_line(execution_id, sim_df["MeanAbsDiff"], 'green', "Mean Absolute Difference of Controlled Stake", "Round",
"Mean abs diff", "meanAbsDiff", equilibrium_steps, pivot_steps, figures_dir, False)

'''pool_sizes_by_step = sim_df["PoolSizes"] # todo fix
Expand All @@ -135,7 +135,7 @@ def main():
plt.savefig(figures_dir + "poolDynamics.png", bbox_inches='tight')'''


def plot_line(simulation_id, data, color, title, x_label, y_label, filename, equilibrium_steps, pivot_steps,
def plot_line(execution_id, data, color, title, x_label, y_label, filename, equilibrium_steps, pivot_steps,
figures_dir, show_equilibrium=False):
path = pathlib.Path.cwd() / figures_dir
pathlib.Path(path).mkdir(parents=True, exist_ok=True)
Expand All @@ -156,7 +156,7 @@ def plot_line(simulation_id, data, color, title, x_label, y_label, filename, equ
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.legend()
plt.savefig(figures_dir + simulation_id + "-" + filename + ".png", bbox_inches='tight')
plt.savefig(figures_dir + execution_id + "-" + filename + ".png", bbox_inches='tight')

# plt.show()

Expand Down

0 comments on commit 20f4fcb

Please sign in to comment.