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

refactor definition of parameters to be maximized

parent 71d959df
No related branches found
No related tags found
No related merge requests found
...@@ -2,9 +2,12 @@ ...@@ -2,9 +2,12 @@
"MODEL_NAME": "SimpleHeatingSystem", "MODEL_NAME": "SimpleHeatingSystem",
"MODEL_FILE": "SimpleHeatingSystem.mo", "MODEL_FILE": "SimpleHeatingSystem.mo",
"SIMULATION_STOP_TIME": 3000, "SIMULATION_STOP_TIME": 3000,
"PRECISION": 2,
"PARAMETERS": ["Q_max", "T_set"], "PARAMETERS": ["Q_max", "T_set"],
"OBJECTIVES": ["energy", "comfort"], "OBJECTIVES": [
"MAXIMIZE": ["comfort"], {"name": "energy", "maximize": false},
{"name": "comfort", "maximize": true}
],
"PARAM_BOUNDS": { "PARAM_BOUNDS": {
"Q_max": { "Q_max": {
"bounds": [1000, 5000], "bounds": [1000, 5000],
...@@ -15,12 +18,13 @@ ...@@ -15,12 +18,13 @@
"type": "int" "type": "int"
} }
}, },
"PRECISION": 2,
"OPTIMIZATION_CONFIG": { "OPTIMIZATION_CONFIG": {
"USE_ADAPTIVE_INSTANCE_SELECTION": true, "USE_ADAPTIVE_INSTANCE_SELECTION": true,
"ADAPTIVE_INSTANCE_SELECTION_FREQUENCY": 5,
"ALGORITHM_NAME": "NSGA2", "ALGORITHM_NAME": "NSGA2",
"POP_SIZE": 1, "POP_SIZE": 10,
"N_GEN": 1 "MIN_POP_SIZE": 1,
"N_GEN": 10
}, },
"PLOT_CONFIG": { "PLOT_CONFIG": {
"PLOT_X": "Energy Consumption", "PLOT_X": "Energy Consumption",
......
...@@ -18,12 +18,13 @@ with open(CONFIG_FILE, 'r') as f: ...@@ -18,12 +18,13 @@ with open(CONFIG_FILE, 'r') as f:
# Assign configuration variables # Assign configuration variables
MODEL_NAME = config['MODEL_NAME'] MODEL_NAME = config['MODEL_NAME']
MODEL_FILE = config['MODEL_FILE'] MODEL_FILE = config['MODEL_FILE']
SIMULATION_STOP_TIME = config['SIMULATION_STOP_TIME'] SIMULATION_STOP_TIME = config['SIMULATION_STOP_TIME'] # in seconds
PARAMETERS = config['PARAMETERS'] PRECISION = config['PRECISION'] # Results precision (decimal places)
OBJECTIVES = config['OBJECTIVES'] PARAMETERS = config['PARAMETERS'] # Parameters to be varied
MAXIMIZE = config['MAXIMIZE'] OBJECTIVES = config['OBJECTIVES'] # Objectives to be optimize
PARAM_BOUNDS = config['PARAM_BOUNDS'] OBJECTIVE_NAMES = [obj['name'] for obj in OBJECTIVES]
PRECISION = config['PRECISION'] MAXIMIZE = [obj['maximize'] for obj in OBJECTIVES]
PARAM_BOUNDS = config['PARAM_BOUNDS'] # Parameter range
OPTIMIZATION_CONFIG = config['OPTIMIZATION_CONFIG'] OPTIMIZATION_CONFIG = config['OPTIMIZATION_CONFIG']
PLOT_CONFIG = config['PLOT_CONFIG'] PLOT_CONFIG = config['PLOT_CONFIG']
N_JOBS = config['N_JOBS'] N_JOBS = config['N_JOBS']
...@@ -41,11 +42,18 @@ def get_types(param_bounds): ...@@ -41,11 +42,18 @@ def get_types(param_bounds):
PARAM_BOUND_VALUES = get_bounds(PARAM_BOUNDS) PARAM_BOUND_VALUES = get_bounds(PARAM_BOUNDS)
PARAM_TYPES = get_types(PARAM_BOUNDS) PARAM_TYPES = get_types(PARAM_BOUNDS)
# "PARAMETERS": Parameters to be varied # "OPTIMIZATION_CONFIG": Configuration parameters for optimization
# "OBJECTIVES": Objectives to be optimized # "USE_ADAPTIVE_INSTANCE_SELECTION": Flag to enable or disable adaptive instance selection
# "MAXIMIZE": Objectives to be maximized # "ADAPTIVE_INSTANCE_SELECTION_FREQUENCY":
# "PARAM_BOUNDS": Parameter range # Frequency (in generations) at which adaptive instance selection is applied
# "PRECISION": Results precision (decimal places) # For example, a value of 5 means adaptive instance selection will be applied every 5 generations
# "ALGORITHM_NAME": Optimization algorithm
# "POP_SIZE": Initial population size for the optimization algorithm
# "MIN_POP_SIZE":
# Minimum population size to maintain diversity and prevent premature convergence
# The population size will not be reduced below this value
# "N_GEN": Total number of generations (iterations) for the optimization process
# "N_JOBS": Parallel processing # "N_JOBS": Parallel processing
# Options: '-1', '1', 'n', 'None' # Options: '-1', '1', 'n', 'None'
# ==================================================================== # ====================================================================
......
...@@ -14,7 +14,7 @@ import numpy as np ...@@ -14,7 +14,7 @@ import numpy as np
from time import sleep from time import sleep
from joblib import Parallel, delayed from joblib import Parallel, delayed
from OMPython import OMCSessionZMQ from OMPython import OMCSessionZMQ
from config import MODEL_FILE, MODEL_NAME, SIMULATION_STOP_TIME, PARAMETERS, OBJECTIVES, PARAM_BOUNDS, PARAM_TYPES, MODEL_PATH, PRECISION, OPTIMIZATION_CONFIG, N_JOBS from config import MODEL_FILE, MODEL_NAME, SIMULATION_STOP_TIME, PARAMETERS, OBJECTIVE_NAMES, PARAM_BOUNDS, PARAM_TYPES, MODEL_PATH, PRECISION, OPTIMIZATION_CONFIG, N_JOBS
from adaptive_instance_selection import initial_sampling, evaluate_samples, advanced_clustering_samples, adaptive_select_informative_instances, iterative_refinement from adaptive_instance_selection import initial_sampling, evaluate_samples, advanced_clustering_samples, adaptive_select_informative_instances, iterative_refinement
temp_dirs = [] # List to store paths of temporary directories temp_dirs = [] # List to store paths of temporary directories
...@@ -77,15 +77,15 @@ def optimization_function(param_values, retries=3, delay=2): ...@@ -77,15 +77,15 @@ def optimization_function(param_values, retries=3, delay=2):
simulate_result = omc.sendExpression(f"simulate({MODEL_NAME}, stopTime={SIMULATION_STOP_TIME})") simulate_result = omc.sendExpression(f"simulate({MODEL_NAME}, stopTime={SIMULATION_STOP_TIME})")
# Retrieve simulation results # Retrieve simulation results
result_values = {} result_values = {}
for objective in OBJECTIVES: for objective_name in OBJECTIVE_NAMES:
value_command = f"val({objective}, {SIMULATION_STOP_TIME})" value_command = f"val({objective_name}, {SIMULATION_STOP_TIME})"
value = omc.sendExpression(value_command) value = omc.sendExpression(value_command)
print(f"Value for {objective} at {SIMULATION_STOP_TIME}: {value}") print(f"Value for {objective_name} at {SIMULATION_STOP_TIME}: {value}")
if value is None: if value is None:
raise ValueError(f"Simulation result returned None for {objective}") raise ValueError(f"Simulation result returned None for {objective_name}")
if not isinstance(value, (float, int)): if not isinstance(value, (float, int)):
raise ValueError(f"Simulation result for {objective} is not in expected format (float or int)") raise ValueError(f"Simulation result for {objective_name} is not in expected format (float or int)")
result_values[objective] = round(value, PRECISION) result_values[objective_name] = round(value, PRECISION)
print(f"Simulation results: {result_values}") print(f"Simulation results: {result_values}")
return list(result_values.values()) return list(result_values.values())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment