def run_one(arg): """Run one evolution. Arg is a tuple containing user ID and run number.""" user_id, run_number = arg # Note that the PID printed below is the PID in which this function is # running, which is different from the PID of the evolution. print "Launching evolution for user %d run %d (pid %d) at %s..." % \ (user_id, run_number, os.getpid(), time.asctime()) sys.stdout.flush() timer = SimpleTimer(output_stream=None) out_dir = os.path.join(SG_SIM_PATH, "id_%d" % user_id) model = os.path.join(SG_MODELS_PATH, "load_prediction.py") postfix = "run_%d" % run_number generations = 50 pop_size = 400 mutation = 0.05 crossover = 0.5 # NB Total-load sims: total = " --total-load" data_seed = 12 stdout_path = os.path.join(out_dir, "output_run_%d.txt" % run_number) os.system("test -d %s || mkdir -p %s" % (out_dir, out_dir)) os.system("python %s " % model + \ " --userid=%d" % user_id + \ " --out-dir=%s --out-postfix=%s " % (out_dir, postfix) + \ " --generations=%d --pop-size=%d " % (generations, pop_size) + \ " --mutation=%f --crossover=%f " % (mutation, crossover) + \ " --no-show-plot --save-plot " + \ total + \ " --data-seed=%d " % data_seed + \ " >%s" % stdout_path) print "Evolution completed for user %d run %d. %s" \ % (user_id, run_number, timer.end()) sys.stdout.flush()
params.run_one(arg) def make_runs(user_ids, num_runs): """Create a list of (user_id, run_number) pairs that can be sent via pool.map to the run_one function.""" return [(user, run) for user in user_ids for run in range(num_runs)] def run_simulations(runs): """Run all the simulations provided in runs by sending them on to the run_one function.""" num_parallel_processes = 12 pool = Pool(processes=num_parallel_processes) pool.map(run_one_wrapper, runs, chunksize=1) if __name__ == "__main__": # if socket.gethostname() == "tanzenmusik.idi.ntnu.no": # user_ids = tempfeeder_exp().user_ids[25:50] # else: # user_ids = tempfeeder_exp().user_ids[0:25] user_ids = [tempfeeder_exp().user_ids[0]] num_runs = 12 print "Master pid is %d " % os.getpid() timer = SimpleTimer(output_stream=None) tempfeeder_exp().close() runs = make_runs(user_ids, num_runs) run_simulations(runs) print "All simulations complete. %s" % timer.end() tempfeeder_exp().close()
def make_runs(user_ids, num_runs): """Create a list of (user_id, run_number) pairs that can be sent via pool.map to the run_one function.""" return [(user, run) for user in user_ids for run in range(num_runs)] def run_simulations(runs): """Run all the simulations provided in runs by sending them on to the run_one function.""" num_parallel_processes = 12 pool = Pool(processes=num_parallel_processes) pool.map(run_one_wrapper, runs, chunksize=1) if __name__ == "__main__": # if socket.gethostname() == "tanzenmusik.idi.ntnu.no": # user_ids = tempfeeder_exp().user_ids[25:50] # else: # user_ids = tempfeeder_exp().user_ids[0:25] user_ids = [tempfeeder_exp().user_ids[0]] num_runs = 12 print "Master pid is %d " % os.getpid() timer = SimpleTimer(output_stream=None) tempfeeder_exp().close() runs = make_runs(user_ids, num_runs) run_simulations(runs) print "All simulations complete. %s" % timer.end() tempfeeder_exp().close()