2022-11-30 14:28:27 +00:00
|
|
|
#! /bin/python3
|
|
|
|
|
2023-01-25 20:51:59 +00:00
|
|
|
import time, sys, random, copy
|
2023-02-24 18:08:41 +00:00
|
|
|
import importlib
|
2023-04-13 04:01:04 +00:00
|
|
|
import subprocess
|
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
|
|
|
|
|
2023-03-14 09:10:13 +00:00
|
|
|
def initLogger(config):
|
|
|
|
"""It initializes the logger."""
|
|
|
|
logger = logging.getLogger("Study")
|
|
|
|
logger.setLevel(config.logLevel)
|
|
|
|
ch = logging.StreamHandler()
|
|
|
|
ch.setLevel(config.logLevel)
|
|
|
|
ch.setFormatter(CustomFormatter())
|
|
|
|
logger.addHandler(ch)
|
|
|
|
return logger
|
|
|
|
|
2023-03-21 13:53:48 +00:00
|
|
|
def runOnce(config, shape, execID):
|
2023-03-14 09:10:13 +00:00
|
|
|
|
2023-03-15 11:37:23 +00:00
|
|
|
if config.deterministic:
|
2023-03-15 12:18:02 +00:00
|
|
|
shape.setSeed(config.randomSeed+"-"+str(shape))
|
|
|
|
random.seed(shape.randomSeed)
|
2023-03-15 11:37:23 +00:00
|
|
|
|
2023-03-27 13:29:39 +00:00
|
|
|
sim = Simulator(shape, config, execID)
|
2023-03-15 11:37:23 +00:00
|
|
|
sim.initLogger()
|
|
|
|
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)
|
2023-03-21 13:53:48 +00:00
|
|
|
|
|
|
|
if config.dumpXML:
|
2023-03-27 13:29:39 +00:00
|
|
|
result.dump()
|
2023-03-21 13:53:48 +00:00
|
|
|
|
2023-07-12 19:28:21 +00:00
|
|
|
if config.visualization:
|
|
|
|
visual = Visualizor(execID, config, [result])
|
|
|
|
visual.plotAll()
|
|
|
|
|
2023-03-15 11:37:23 +00:00
|
|
|
return result
|
2022-11-30 14:28:27 +00:00
|
|
|
|
|
|
|
def study():
|
2023-01-23 17:04:54 +00:00
|
|
|
if len(sys.argv) < 2:
|
|
|
|
print("You need to pass a configuration file in parameter")
|
|
|
|
exit(1)
|
|
|
|
|
2023-03-02 00:01:52 +00:00
|
|
|
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)
|
|
|
|
|
2023-03-14 09:10:13 +00:00
|
|
|
logger = initLogger(config)
|
|
|
|
format = {"entity": "Study"}
|
|
|
|
|
2023-01-16 21:43:52 +00:00
|
|
|
results = []
|
2023-01-23 17:04:54 +00:00
|
|
|
|
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))
|
|
|
|
|
2023-04-13 04:01:04 +00:00
|
|
|
# save config and code state for reproducibility
|
|
|
|
if not os.path.exists("results"):
|
|
|
|
os.makedirs("results")
|
|
|
|
dir = "results/"+execID
|
|
|
|
if not os.path.exists(dir):
|
|
|
|
os.makedirs(dir)
|
2023-04-17 10:14:39 +00:00
|
|
|
if config.saveGit:
|
|
|
|
with open(dir+"/git.diff", 'w') as f:
|
|
|
|
subprocess.run(["git", "diff"], stdout=f)
|
|
|
|
with open(dir+"/git.describe", 'w') as f:
|
|
|
|
subprocess.run(["git", "describe", "--always"], stdout=f)
|
2023-04-13 04:01:04 +00:00
|
|
|
subprocess.run(["cp", sys.argv[1], dir+"/"])
|
|
|
|
|
2023-03-14 09:10:13 +00:00
|
|
|
logger.info("Starting simulations:", extra=format)
|
2022-11-30 14:28:27 +00:00
|
|
|
start = time.time()
|
2023-03-21 13:53:48 +00:00
|
|
|
results = Parallel(config.numJobs)(delayed(runOnce)(config, shape ,execID) for shape in config.nextShape())
|
2022-11-30 14:28:27 +00:00
|
|
|
end = time.time()
|
2023-03-14 09:10:13 +00:00
|
|
|
logger.info("A total of %d simulations ran in %d seconds" % (len(results), end-start), extra=format)
|
2023-01-25 20:51:59 +00:00
|
|
|
|
2023-03-15 12:18:02 +00:00
|
|
|
if config.visualization:
|
2023-04-20 16:15:02 +00:00
|
|
|
vis = Visualizer(execID, config)
|
2023-02-22 15:45:39 +00:00
|
|
|
vis.plotHeatmaps()
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2023-05-13 09:25:09 +00:00
|
|
|
visual = Visualizor(execID, config, results)
|
2023-06-11 17:26:44 +00:00
|
|
|
visual.plotHeatmaps("nn", "fr")
|
2023-05-13 09:25:09 +00:00
|
|
|
|
2023-03-14 09:09:04 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
study()
|