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

style minor improvements

parent 4ea9e608
No related branches found
No related tags found
No related merge requests found
...@@ -18,9 +18,9 @@ MODEL_FILE = f"{MODEL_NAME}.mo" ...@@ -18,9 +18,9 @@ MODEL_FILE = f"{MODEL_NAME}.mo"
MODEL_PATH = os.path.join(os.getcwd(), MODEL_FILE) MODEL_PATH = os.path.join(os.getcwd(), MODEL_FILE)
SIMULATION_STOP_TIME = 3000 # in seconds SIMULATION_STOP_TIME = 3000 # in seconds
# Parameters and result variables # Parameters to be varied and objectives to be optimized
PARAMETERS = ["Q_max", "T_set"] PARAMETERS = ["Q_max", "T_set"]
RESULTS = ["energy", "comfort"] OBJECTIVES = ["energy", "comfort"]
MAXIMIZE = ["comfort"] # List of objectives to maximize MAXIMIZE = ["comfort"] # List of objectives to maximize
# Parameter range # Parameter range
...@@ -32,6 +32,13 @@ PARAM_BOUNDS = { ...@@ -32,6 +32,13 @@ PARAM_BOUNDS = {
# Results precision # Results precision
PRECISION = 2 # decimal places PRECISION = 2 # decimal places
# Optimization settings
OPTIMIZATION_CONFIG = {
"ALGORITHM_NAME": 'NSGA2', # Algorithm selection
"POP_SIZE": 100, # Population size for algorithm
"N_GEN": 50 # Number of generations
}
# Plot configurations # Plot configurations
PLOT_CONFIG = { PLOT_CONFIG = {
"PLOT_X": "Energy Consumption", "PLOT_X": "Energy Consumption",
...@@ -39,15 +46,6 @@ PLOT_CONFIG = { ...@@ -39,15 +46,6 @@ PLOT_CONFIG = {
"PLOT_TITLE": "Pareto Front of Energy Consumption vs 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'
ALGORITHM_NAME = 'NSGA2'
# Optimization settings
POP_SIZE = 50 # Population size for NSGA2
N_GEN = 100 # Number of generations
# Parallel processing # Parallel processing
N_JOBS = -1 # Options: '-1', '1', 'n', 'None' N_JOBS = -1 # Options: '-1', '1', 'n', 'None'
# ==================================================================== # ====================================================================
......
...@@ -14,21 +14,21 @@ from pymoo.core.problem import Problem ...@@ -14,21 +14,21 @@ from pymoo.core.problem import Problem
from pymoo.optimize import minimize from pymoo.optimize import minimize
from optimization_libraries import initialize_algorithm from optimization_libraries import initialize_algorithm
from parallel_computing import optimization_function, cleanup_temp_dirs from parallel_computing import optimization_function, cleanup_temp_dirs
from config import (PARAMETERS, RESULTS, PARAM_BOUNDS, PRECISION, PLOT_CONFIG, from config import (PARAMETERS, OBJECTIVES, MAXIMIZE, PARAM_BOUNDS, PRECISION, PLOT_CONFIG,
OPTIMIZATION_LIBRARY, ALGORITHM_NAME, POP_SIZE, N_GEN, N_JOBS, MAXIMIZE) # Import all configuration variables OPTIMIZATION_CONFIG, N_JOBS) # Import all configuration variables
class OptimizationProblem(Problem): class OptimizationProblem(Problem):
def __init__(self): def __init__(self):
self.param_names = list(PARAM_BOUNDS.keys()) self.param_names = list(PARAM_BOUNDS.keys())
self.result_names = RESULTS self.objective_names = OBJECTIVES
self.maximize_indices = [self.result_names.index(res) for res in MAXIMIZE] self.maximize_indices = [self.objective_names.index(res) for res in MAXIMIZE]
n_var = len(self.param_names) n_var = len(self.param_names)
xl = np.array([PARAM_BOUNDS[param][0] for param in self.param_names]) xl = np.array([PARAM_BOUNDS[param][0] for param in self.param_names])
xu = np.array([PARAM_BOUNDS[param][1] for param in self.param_names]) xu = np.array([PARAM_BOUNDS[param][1] for param in self.param_names])
print(f"Number of variables: {n_var}") print(f"Number of variables: {n_var}")
print(f"Lower bounds: {xl}") print(f"Lower bounds: {xl}")
print(f"Upper bounds: {xu}") print(f"Upper bounds: {xu}")
super().__init__(n_var=n_var, n_obj=len(RESULTS), n_constr=0, xl=xl, xu=xu) super().__init__(n_var=n_var, n_obj=len(OBJECTIVES), n_constr=0, xl=xl, xu=xu)
def _evaluate(self, X, out, *args, **kwargs): def _evaluate(self, X, out, *args, **kwargs):
param_values_list = [dict(zip(self.param_names, x)) for x in X] param_values_list = [dict(zip(self.param_names, x)) for x in X]
...@@ -43,14 +43,17 @@ class OptimizationProblem(Problem): ...@@ -43,14 +43,17 @@ class OptimizationProblem(Problem):
out["F"] = np.array(results) # Ensure results are a 2D array out["F"] = np.array(results) # Ensure results are a 2D array
# Initialize the optimization algorithm # Initialize the optimization algorithm
algorithm = initialize_algorithm(OPTIMIZATION_LIBRARY, ALGORITHM_NAME, POP_SIZE) algorithm = initialize_algorithm(
OPTIMIZATION_CONFIG['ALGORITHM_NAME'],
OPTIMIZATION_CONFIG.get('POP_SIZE')
)
# Define the optimization problem # Define the optimization problem
problem = OptimizationProblem() problem = OptimizationProblem()
try: try:
# Run the optimization # Run the optimization
res = minimize(problem, algorithm, ("n_gen", N_GEN), verbose=True) res = minimize(problem, algorithm, ("n_gen", OPTIMIZATION_CONFIG['N_GEN']), verbose=True)
finally: finally:
# Cleanup temporary directories # Cleanup temporary directories
cleanup_temp_dirs() cleanup_temp_dirs()
...@@ -65,7 +68,7 @@ for i, result in enumerate(res.F): ...@@ -65,7 +68,7 @@ for i, result in enumerate(res.F):
result = tuple(result) result = tuple(result)
print(f"Solution {i}: ", end="") print(f"Solution {i}: ", end="")
for name, value in zip(RESULTS, result): for name, value in zip(OBJECTIVES, result):
print(f"{name.capitalize()} = {value:.{PRECISION}f}", end=", ") print(f"{name.capitalize()} = {value:.{PRECISION}f}", end=", ")
print() print()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment