""" The script for running the stigmergy_game.py model. It takes the model parameters from the command line, sets up the model, runs it, and outputs the data. An example start up command is: python stigmergy_game_runner.py N=4 E=4 S=16 R="test_run" The stigmergy_game.py module is the python implementation of the "Stigmergy Game" appearing in: Savit, Riolo, Riolo (2013) "Co-adaptation and the Emergence of Structure." PLoS ONE [http://journals.plos.org/plosone/article?id=10.1371/journal.pone.0071828] The original implementation was done by Maria Riolo and this refactorization was written by Jon Atwell. Necessary Files: stigmergy_game.py stigmergy_game_runner.py (this file) Environment: Python 2.7.x - packages: math, random, sys, itertools Required Input Parameters: N = the number of agents E = the number of environmental states S = the number of strategies per agent Optional Input Parameters and default values: T = run length as steps per Agent. Default = 10^8 D1 = setupseed. Default = random D2 = runseed. Default = random Z = window size. Default = 10^4 R = run name. Default = 'trial' """ import sys import random import stigmergy_game as game # Default values for run parameters num_steps = None write_after = None run_name = "trial" setupseed = random.randint(0, sys.maxint) runseed = random.randint(0, sys.maxint) setupRNG = random.Random(setupseed) runRNG = random.Random(runseed) window_size = 10000 report = False # rarely varied parameters tie = 'stay' order_type = 'random' # Process command line arguments for arg in sys.argv[1:]: # Split about the equal sign, into name and value # of the argument. name, value = arg.strip().split('=') # Check for number of agents if name == 'N': num_agents = int(value) # Check for number of strategies per agent elif name == 'S': num_strats = int(value) # Check for number of environments elif name == 'E': num_environs = int(value) # Check for number of steps elif name == 'T': num_steps = int(value)* num_agents # Check for rng seed arguments # Use to reproduce initial conditions elif name == 'D1': setupseed = int(value) setupRNG = random.Random(setupseed) # Use to reproduce whole runs in conjunction with setupRNG elif name == "D2": runseed = int(value) runRNG = random.Random(runseed) elif name == 'Z': window_size = int(value) # Check for run number (which can just be a string, since it isn't for # doing calculaions, just for labeling files) elif name == 'R': run_name = value elif report == "RPT": if value != "False": report= True # Default number of steps is 10^8 *num_agents if num_steps == None: num_steps = 10**8 * num_agents # When to start recording detailed run data, if report is true write_after = num_steps - (.01 * num_steps) """At this point, we have all of the parameters and can start the run. We create the world and use a simple for-loop to 'step though' the model. We also collect data at various points in the run. The Parameter file saves all the information necessary to reconstruct the run in its entirety. The CM file tracks the data necessary to construct a 'center of mass' for the group in 'switch-wealth' space time series later. The Report file, when being produced, gives 'narrative' account of every step: who moved, what they saw, what they produced, what strategy they used and their reward. The Group file outputs information about the state of each agents after their turn. The information is a rolling average of environments, wealth, and switching data. The Reward file summarizes the individual average wealth, the group average wealth, the standard deviation, and the strategies played. """ print "starting run named ' %s ' " % run_name my_str = ['run_name = ' + str(run_name), 'num_environs = ' + str(num_environs), 'num_agents = ' + str(num_agents), 'num_strats = ' + str(num_strats), 'num_steps = ' + str(num_steps),'tie_behavior = ' + str(tie), 'order_type = ' + str(order_type), 'setup_seed = ' +str(setupseed), 'run_seed = ' + str(runseed)] # Write the parameters of this run to the parameter file ParameterFile = open("parameters-"+str(run_name)+".txt", 'w') ParameterFile.write('\n'.join(my_str) + '\n') ParameterFile.close() # Create a World world = game.World(num_environs, num_agents, num_strats, window_size*num_agents, write_after, setupRNG, runRNG, tie, order_type) if report: ReportFile = open("report-"+str(run_name)+".txt", 'w') # Run the World model for the number of steps specified # and accumulate data, updating report file when the data # types that need to be calculated over multiple steps have # a period's worth of data. time = 0 while time < num_steps: world.doStep() time += 1 if time %1000 == 0: print "time step", time # If in the time range to write, we start writing out a bunch of data. if report and time >= write_after: ReportFile.write(",".join([str(i) for i in world.reportData()]) +'\n') ReportFile.close() ResultsFile = open("basic_results-"+str(run_name)+".txt", 'w') world.finalReport(ResultsFile) ResultsFile.close()