Skip to content
Snippets Groups Projects
Commit fe0bc53c authored by Zizhe Wang's avatar Zizhe Wang
Browse files

feat add configuration of result precision and plot config

parent e301bc3d
Branches
No related tags found
No related merge requests found
......@@ -13,23 +13,31 @@
import os
# Basic settings
SIMULATION_STOP_TIME = 2000 # in seconds
MODEL_NAME = "SimpleHeatingSystem"
MODEL_FILE = f"{MODEL_NAME}.mo"
current_directory = os.getcwd()
model_path = os.path.join(current_directory, MODEL_FILE)
MODEL_PATH = os.path.join(os.getcwd(), MODEL_FILE)
SIMULATION_STOP_TIME = 2000 # in seconds
# Parameters and result variables
PARAMETERS = ["Q_max", "T_set"]
RESULTS = ["energy", "cost", "comfort"]
RESULTS = ["energy", "comfort"]
# Parameter bounds
# Parameter range
PARAM_BOUNDS = {
"Q_max": (1000, 5000),
"T_set": (280, 310),
}
# Results precision
PRECISION = 2 # decimal places
# Plot configurations
PLOT_CONFIG = {
"PLOT_X": "Energy Consumption",
"PLOT_Y": "Comfort",
"PLOT_TITLE": "Pareto Front of Energy Consumption vs Comfort"
}
# Algorithm selection
# Options: 'pymoo.NSGA2', 'pymoo.NSGA3', 'pymoo.CMAES', 'scipy.de', 'scipy.minimize'
OPTIMIZATION_LIBRARY = 'pymoo'
......
......@@ -14,7 +14,8 @@ from pymoo.core.problem import Problem
from pymoo.optimize import minimize
from optimization_libraries import initialize_algorithm
from parallel_computing import optimization_function, cleanup_temp_dirs
from config import PARAMETERS, RESULTS, PARAM_BOUNDS, POP_SIZE, N_GEN, N_JOBS, OPTIMIZATION_LIBRARY, ALGORITHM_NAME
from config import (PARAMETERS, RESULTS, PARAM_BOUNDS, PRECISION, PLOT_CONFIG,
OPTIMIZATION_LIBRARY, ALGORITHM_NAME, POP_SIZE, N_GEN, N_JOBS) # Import all configuration variables
class OptimizationProblem(Problem):
def __init__(self):
......@@ -49,15 +50,15 @@ print("Optimization Results:")
for i, result in enumerate(res.F):
print(f"Solution {i}: ", end="")
for name, value in zip(RESULTS, result):
print(f"{name.capitalize()} = {value:.2f}", end=", ") # two decimal places
print(f"{name.capitalize()} = {value:.{PRECISION}f}", end=", ")
print()
# Plot the results
plt.figure(figsize=(8, 6))
plt.scatter(res.F[:, 0], res.F[:, 2])
plt.xlabel('Energy Consumption')
plt.ylabel('Comfort')
plt.title('Pareto Front of Energy Consumption vs Comfort')
plt.scatter(res.F[:, 0], res.F[:, 1])
plt.xlabel(PLOT_X)
plt.ylabel(PLOT_Y)
plt.title(PLOT_TITLE)
plt.grid(True)
plt.tight_layout()
plt.show()
\ No newline at end of file
......@@ -13,7 +13,7 @@ import shutil
from time import sleep
from OMPython import OMCSessionZMQ
import numpy as np
from config import MODEL_FILE, MODEL_NAME, SIMULATION_STOP_TIME, RESULTS, model_path
from config import MODEL_FILE, MODEL_NAME, SIMULATION_STOP_TIME, RESULTS, MODEL_PATH, PRECISION
temp_dirs = [] # List to store paths of temporary directories
......@@ -31,7 +31,7 @@ def optimization_function(param_values):
# Copy model file to temporary directory
temp_model_path = os.path.join(temp_dir, MODEL_FILE).replace('\\', '/')
shutil.copy(model_path, temp_model_path)
shutil.copy(MODEL_PATH, temp_model_path)
# Load the model in the worker session
load_temp_model_result = omc.sendExpression(f"loadFile(\"{temp_model_path}\")")
......@@ -60,7 +60,7 @@ def optimization_function(param_values):
raise RuntimeError(f"Expected file not found: {file}")
# Set model parameters
rounded_param_values = {param: round(value, 2) for param, value in param_values.items()} # two decimal places
rounded_param_values = {param: round(value, PRECISION) for param, value in param_values.items()}
for param, value in rounded_param_values.items():
set_param_result = omc.sendExpression(f"setParameterValue({MODEL_NAME}, {param}, {value})")
if not set_param_result:
......@@ -79,8 +79,8 @@ def optimization_function(param_values):
# print(f"Executing command: {value_command}")
value = omc.sendExpression(value_command)
print(f"Value for {result} at {SIMULATION_STOP_TIME}: {value}")
# Round the result to 2 decimal places
result_values[result] = round(value, 2)
# Round the result
result_values[result] = round(value, PRECISION)
if value is None:
raise ValueError(f"Simulation result returned None for {result}")
if not isinstance(value, (float, int)):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment