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 @@
"MODEL_NAME": "SimpleHeatingSystem",
"MODEL_FILE": "SimpleHeatingSystem.mo",
"SIMULATION_STOP_TIME": 3000,
"PRECISION": 2,
"PARAMETERS": ["Q_max", "T_set"],
"OBJECTIVES": ["energy", "comfort"],
"MAXIMIZE": ["comfort"],
"OBJECTIVES": [
{"name": "energy", "maximize": false},
{"name": "comfort", "maximize": true}
],
"PARAM_BOUNDS": {
"Q_max": {
"bounds": [1000, 5000],
......@@ -15,12 +18,13 @@
"type": "int"
}
},
"PRECISION": 2,
"OPTIMIZATION_CONFIG": {
"USE_ADAPTIVE_INSTANCE_SELECTION": true,
"ADAPTIVE_INSTANCE_SELECTION_FREQUENCY": 5,
"ALGORITHM_NAME": "NSGA2",
"POP_SIZE": 1,
"N_GEN": 1
"POP_SIZE": 10,
"MIN_POP_SIZE": 1,
"N_GEN": 10
},
"PLOT_CONFIG": {
"PLOT_X": "Energy Consumption",
......
......@@ -18,12 +18,13 @@ with open(CONFIG_FILE, 'r') as f:
# Assign configuration variables
MODEL_NAME = config['MODEL_NAME']
MODEL_FILE = config['MODEL_FILE']
SIMULATION_STOP_TIME = config['SIMULATION_STOP_TIME']
PARAMETERS = config['PARAMETERS']
OBJECTIVES = config['OBJECTIVES']
MAXIMIZE = config['MAXIMIZE']
PARAM_BOUNDS = config['PARAM_BOUNDS']
PRECISION = config['PRECISION']
SIMULATION_STOP_TIME = config['SIMULATION_STOP_TIME'] # in seconds
PRECISION = config['PRECISION'] # Results precision (decimal places)
PARAMETERS = config['PARAMETERS'] # Parameters to be varied
OBJECTIVES = config['OBJECTIVES'] # Objectives to be optimize
OBJECTIVE_NAMES = [obj['name'] for obj in OBJECTIVES]
MAXIMIZE = [obj['maximize'] for obj in OBJECTIVES]
PARAM_BOUNDS = config['PARAM_BOUNDS'] # Parameter range
OPTIMIZATION_CONFIG = config['OPTIMIZATION_CONFIG']
PLOT_CONFIG = config['PLOT_CONFIG']
N_JOBS = config['N_JOBS']
......@@ -41,16 +42,23 @@ def get_types(param_bounds):
PARAM_BOUND_VALUES = get_bounds(PARAM_BOUNDS)
PARAM_TYPES = get_types(PARAM_BOUNDS)
# "PARAMETERS": Parameters to be varied
# "OBJECTIVES": Objectives to be optimized
# "MAXIMIZE": Objectives to be maximized
# "PARAM_BOUNDS": Parameter range
# "PRECISION": Results precision (decimal places)
# "OPTIMIZATION_CONFIG": Configuration parameters for optimization
# "USE_ADAPTIVE_INSTANCE_SELECTION": Flag to enable or disable adaptive instance selection
# "ADAPTIVE_INSTANCE_SELECTION_FREQUENCY":
# Frequency (in generations) at which adaptive instance selection is applied
# 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
# Options: '-1', '1', 'n', 'None'
# ====================================================================
# -1 = use all available CPU cores
# 1 = disables parallel processing and runs the tasks sequentially
# n = specifies the exact number of CPU cores to use
# None = will run the tasks sequentially, equivalent to '1'
# ====================================================================
\ No newline at end of file
# Options: '-1', '1', 'n', 'None'
# ====================================================================
# -1 = use all available CPU cores
# 1 = disables parallel processing and runs the tasks sequentially
# n = specifies the exact number of CPU cores to use
# None = will run the tasks sequentially, equivalent to '1'
# ====================================================================
\ No newline at end of file
......@@ -14,7 +14,7 @@ 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, 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
temp_dirs = [] # List to store paths of temporary directories
......@@ -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})")
# Retrieve simulation results
result_values = {}
for objective in OBJECTIVES:
value_command = f"val({objective}, {SIMULATION_STOP_TIME})"
for objective_name in OBJECTIVE_NAMES:
value_command = f"val({objective_name}, {SIMULATION_STOP_TIME})"
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:
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)):
raise ValueError(f"Simulation result for {objective} is not in expected format (float or int)")
result_values[objective] = round(value, PRECISION)
raise ValueError(f"Simulation result for {objective_name} is not in expected format (float or int)")
result_values[objective_name] = round(value, PRECISION)
print(f"Simulation results: {result_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