das-research/study.py

69 lines
2.3 KiB
Python
Raw Normal View History

2022-11-30 14:28:27 +00:00
#! /bin/python3
2023-01-25 20:51:59 +00:00
import time, sys, random, copy
import importlib
2023-03-15 11:37:23 +00:00
from joblib import Parallel, delayed
2022-11-30 14:28:27 +00:00
from DAS import *
2023-03-15 11:37:23 +00:00
# Parallel execution:
# The code currently uses 'joblib' to execute on multiple cores. For other options such as 'ray', see
# https://stackoverflow.com/questions/9786102/how-do-i-parallelize-a-simple-python-loop
# For fixing logging issues in parallel execution, see
# https://stackoverflow.com/questions/58026381/logging-nested-functions-using-joblib-parallel-and-delayed-calls
# and https://github.com/joblib/joblib/issues/1017
def runOnce(sim, config, shape):
if config.deterministic:
shape.setSeed(config.randomSeed+"-"+str(shape))
random.seed(shape.randomSeed)
2023-03-15 11:37:23 +00:00
sim.initLogger()
sim.resetShape(shape)
sim.initValidators()
sim.initNetwork()
result = sim.run()
sim.logger.info("Shape: %s ... Block Available: %d in %d steps" % (str(sim.shape.__dict__), result.blockAvailable, len(result.missingVector)), extra=sim.format)
return result
2022-11-30 14:28:27 +00:00
def study():
if len(sys.argv) < 2:
print("You need to pass a configuration file in parameter")
exit(1)
try:
config = importlib.import_module(sys.argv[1])
except ModuleNotFoundError as e:
try:
config = importlib.import_module(str(sys.argv[1]).replace(".py", ""))
except ModuleNotFoundError as e:
print(e)
print("You need to pass a configuration file in parameter")
exit(1)
shape = Shape(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
sim = Simulator(shape, config)
2022-11-30 14:28:27 +00:00
sim.initLogger()
results = []
2023-01-25 20:51:59 +00:00
now = datetime.now()
execID = now.strftime("%Y-%m-%d_%H-%M-%S_")+str(random.randint(100,999))
2022-11-30 14:28:27 +00:00
sim.logger.info("Starting simulations:", extra=sim.format)
start = time.time()
2023-03-15 11:37:23 +00:00
results = Parallel(config.numJobs)(delayed(runOnce)(sim, config, shape) for shape in config.nextShape())
2022-11-30 14:28:27 +00:00
end = time.time()
2023-03-15 11:37:23 +00:00
sim.logger.info("A total of %d simulations ran in %d seconds" % (len(results), end-start), extra=sim.format)
2023-01-25 20:51:59 +00:00
if config.dumpXML:
for res in results:
res.dump(execID)
sim.logger.info("Results dumped into results/%s/" % (execID), extra=sim.format)
if config.visualization:
2023-02-22 15:45:39 +00:00
vis = Visualizer(execID)
vis.plotHeatmaps()
2022-11-30 14:28:27 +00:00
study()