Commit 546c2439 authored by Chris Dancy's avatar Chris Dancy

Some small changes

parent c38585d3
This diff is collapsed.
'''
By Christopher L. Dancy,
christopher.dancy@bucknell.edu,
Assistant Professor @ Bucknell University
Class to run python math models in parallel (e.g., for use w/ a personal
multi-core machine)
'''
from DeckMathModel import *
import IGTSim
from itertools import product
from multiprocessing import Pool
import time
from UtilityFuncs import *
class PSim(object):
''' class used to run a parallel IGT Simulations
'''
#Number of trials in each block
MAX_NUM_PROC = 50
def __init__(self, proc_func, params):
'''
Arguments:
proc_fun -- Processing function to be called in parallel
params -- A dictionary (key-value pairs) containing parameter names (key)
and a string consisting of "starting value,ending value,step size"
(value)
'''
self._proc_func = proc_func
self._params=params
assert (not (self._proc_func is None or self._params is None)), \
"Issue creating your PSim object! Did you give a function and parameters?"
def run_sims(self, num_trials, num_runs):
''' runs parallel simulations w/ numTrials, numRuns times
Arguments:
num_trials -- Total number of trials per run
(a trial is one card selected from a deck)
num_runs -- Total number of runs per simulation
Output:
Dictionary of Dictionaries of deck selection proportions for each block
each key is in the form "param-val_param-val_..."
s.t., param contained in {parameter keys} &
s.t., val conatined in {startingvalue, ..., endingvalue}
for example - a-0.3_alpha-0.5 is simulation w/ the a parameter at 0.3
and the alpha parameter at 0.5
'''
#Dictionary that holds the simulation results
sim_results_save = {}
params = []
#Get Parameter=Value strings that we can use
for key in self._params.keys():
params.extend([key+"="+str(x)+"," for x in eval("frange("+self._params[key]+")")])
params.append(params)
#Run paralellized simulations using Pool & product from itertools
sim_pool = Pool()
sim_pool.map(self.run_sim_function, self._proc_func, product(*params))
return (sim_results_save)
def run_sim_function(self, proc_fun, params):
#Last character should be unwanted comma
params = params[0:-1]
#Make string args into a dictionary so we can use splat operator w/
# our simulation function
args = dict(x.split("=") for x in params.split(","))
#Run function
results = proc_fun(**args)
#Write results to file
out_file = open(params.replace("=","-").replace(",","_") + ".txt", "w")
for key in results.keys():
out_file.write(key+","+str(results[key])+"\n")
#Need to FINISH THIS, NEED TO ADD PARAMETER DICTIONARY AND TEST FUNCTION ITSELF
# BEFORE RUNNING ON Big machine
full_param_dict = {"update_choice":"0,4,1", "c":"-10,10.25,0.25", "a":"0,1.01,0.01",\
"weight":"0,1.01,0.01"}
test_param_dict = {"update_choice":"0,4,1", "c":"-10,10,0.5", "a":"0,1,0.1",\
"weight":"0,1,0.1"}
test_time = time.time()
p_sim_obj = PSim(IGTSim.run_ev, test_param_dict)
p_sim_obj.run_sims(100,2)
print(str(time.time() - test_time))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment