diff --git a/src/config.json b/src/config.json index d30634fff58f766fe9d73343c1f29af67214df3b..da762d25cf8f3e65e91c8dc3e70088ff0def7686 100644 --- a/src/config.json +++ b/src/config.json @@ -30,5 +30,11 @@ "PLOT_TITLE": "Pareto Front of Energy Consumption vs Comfort", "ENABLE_PLOT": false }, + "LIBRARY_CONFIG": { + "LOAD_LIBRARIES": true, + "LIBRARIES": [ + {"name": "", "path": ""} + ] + }, "N_JOBS": -1 } \ No newline at end of file diff --git a/src/config.py b/src/config.py index 129e5cb4b756755f6c15ab52ba5e753b443a4e05..f8413b8e6da6e2c63e986963f538068e98566403 100644 --- a/src/config.py +++ b/src/config.py @@ -28,9 +28,13 @@ PARAM_BOUNDS = config['PARAM_BOUNDS'] # Parameter range OPTIMIZATION_CONFIG = config['OPTIMIZATION_CONFIG'] PLOT_CONFIG = config['PLOT_CONFIG'] N_JOBS = config['N_JOBS'] +LIBRARY_CONFIG = config.get('LIBRARY_CONFIG', {}) +LOAD_LIBRARIES = LIBRARY_CONFIG.get('LOAD_LIBRARIES', False) +LIBRARIES = LIBRARY_CONFIG.get('LIBRARIES', []) # Derived configuration -MODEL_PATH = os.path.join(os.getcwd(), MODEL_FILE) +BASE_DIR = os.getcwd() +MODEL_PATH = os.path.join(BASE_DIR, 'models', MODEL_FILE) # Helper functions to get bounds and types def get_bounds(param_bounds): diff --git a/src/parallel_computing.py b/src/parallel_computing.py index 83e7302934c2f2c9f1987257d0279fc2248f5989..ce12e65428a1fe00b7aecbd7c39fa133dc3918ad 100644 --- a/src/parallel_computing.py +++ b/src/parallel_computing.py @@ -14,10 +14,23 @@ import numpy as np from time import sleep from joblib import Parallel, delayed from OMPython import OMCSessionZMQ -from config import MODEL_FILE, MODEL_NAME, SIMULATION_STOP_TIME, PARAMETERS, OBJECTIVE_NAMES, PARAM_BOUNDS, PARAM_TYPES, MODEL_PATH, PRECISION, N_JOBS +from config import MODEL_FILE, MODEL_NAME, SIMULATION_STOP_TIME, PARAMETERS, OBJECTIVE_NAMES, PARAM_BOUNDS, PARAM_TYPES, MODEL_PATH, PRECISION, N_JOBS, LOAD_LIBRARIES, LIBRARIES temp_dirs = [] # List to store paths of temporary directories +def load_libraries(omc): + """ + Function to load Modelica libraries. + """ + for library in LIBRARIES: + library_path = os.path.join(os.getcwd(), 'models', library['path']).replace('\\', '/') + load_library_result = omc.sendExpression(f'loadFile("{library_path}")') + print(f"Load library result: {load_library_result}") + if load_library_result is not True: + messages = omc.sendExpression("getErrorString()") + print(f"OpenModelica error messages: {messages}") + raise RuntimeError(f"Failed to load library: {library['name']}") + def optimization_function(param_values, retries=3, delay=2): """ Function to optimize the Modelica model given specific parameter values. @@ -36,6 +49,9 @@ def optimization_function(param_values, retries=3, delay=2): temp_model_path = os.path.join(temp_dir, MODEL_FILE).replace('\\', '/') shutil.copy(MODEL_PATH, temp_model_path) + if LOAD_LIBRARIES: + load_libraries(omc) + # Load the model in the worker session load_temp_model_result = omc.sendExpression(f"loadFile(\"{temp_model_path}\")") print(f"Load model result in worker: {load_temp_model_result}")