|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from sklearn.gaussian_process import ( |
| 4 | + GaussianProcessRegressor, GaussianProcessClassifier) |
| 5 | +from sklearn.base import BaseEstimator |
| 6 | + |
| 7 | +from CADETProcess.optimization import ( |
| 8 | + Population, OptimizationProblem, OptimizationResults) |
| 9 | + |
| 10 | +class Surrogate: |
| 11 | + def __init__( |
| 12 | + self, |
| 13 | + optimization_problem: OptimizationProblem, |
| 14 | + population: Population |
| 15 | + ): |
| 16 | + self.optimization_problem = optimization_problem |
| 17 | + self.surrogate_model_F: BaseEstimator = None |
| 18 | + self.surrogate_model_G: BaseEstimator = None |
| 19 | + self.surrogate_model_M: BaseEstimator = None |
| 20 | + self.surrogate_model_CV: BaseEstimator = None |
| 21 | + self.fit_gaussian_process(population) |
| 22 | + |
| 23 | + # save a backup of bounds |
| 24 | + self.lower_bounds_copy = optimization_problem.lower_bounds.copy() |
| 25 | + self.upper_bounds_copy = optimization_problem.upper_bounds.copy() |
| 26 | + |
| 27 | + def _reset_bounds_on_variables(self): |
| 28 | + for var, lb, ub in zip( |
| 29 | + self.optimization_problem.variables, |
| 30 | + self.lower_bounds_copy, |
| 31 | + self.upper_bounds_copy |
| 32 | + ): |
| 33 | + var.lb = lb |
| 34 | + var.ub = ub |
| 35 | + |
| 36 | + def fit_gaussian_process(self, population: Population): |
| 37 | + X = population.x |
| 38 | + F = population.f |
| 39 | + G = population.g |
| 40 | + M = population.m |
| 41 | + CV = population.cv |
| 42 | + |
| 43 | + gp_f = GaussianProcessRegressor() |
| 44 | + gp_f.fit(X, F) |
| 45 | + self.surrogate_model_F = gp_f |
| 46 | + |
| 47 | + if G is not None: |
| 48 | + gp_g = GaussianProcessRegressor() |
| 49 | + gp_g.fit(X, G) |
| 50 | + self.surrogate_model_G = gp_g |
| 51 | + |
| 52 | + if M is not None: |
| 53 | + gp_m = GaussianProcessRegressor() |
| 54 | + gp_m.fit(X, M) |
| 55 | + self.surrogate_model_M = gp_m |
| 56 | + |
| 57 | + if CV is not None: |
| 58 | + gp_cv = GaussianProcessClassifier() |
| 59 | + gp_cv.fit(X, CV) |
| 60 | + self.surrogate_model_CV = gp_cv |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + def estimate_objectives(self, X): |
| 65 | + objectives = [] |
| 66 | + F_est = self.surrogate_model_F.predict(X) |
| 67 | + objectives.append(F_est) |
| 68 | + |
| 69 | + if self.surrogate_model_G is not None: |
| 70 | + G_est = self.surrogate_model_G.predict(X) |
| 71 | + objectives.append(G_est) |
| 72 | + |
| 73 | + if self.surrogate_model_M is not None: |
| 74 | + M_est = self.surrogate_model_M.predict(X) |
| 75 | + objectives.append(M_est) |
| 76 | + |
| 77 | + if self.surrogate_model_CV is not None: |
| 78 | + CV_est = self.surrogate_model_CV.predict(X) |
| 79 | + objectives.append(CV_est) |
| 80 | + |
| 81 | + return np.array(objectives).T |
| 82 | + |
| 83 | + |
| 84 | + def estimate_feasible_objectives_space(self, n_samples=1000): |
| 85 | + X = self.optimization_problem.create_initial_values( |
| 86 | + n_samples=n_samples, |
| 87 | + method="random", |
| 88 | + ) |
| 89 | + F = self.estimate_objectives(X) |
| 90 | + |
| 91 | + return X, F |
| 92 | + |
| 93 | + |
| 94 | + def condition_objectives( |
| 95 | + self, |
| 96 | + conditional_vars: dict = {}, |
| 97 | + n_samples=1000, |
| 98 | + eps=1e-5 |
| 99 | + ): |
| 100 | + |
| 101 | + # TODO: should check if the condition is inside the constriants |
| 102 | + # otherwise Hopsy throws an error |
| 103 | + |
| 104 | + free_vars = {} |
| 105 | + for var in self.optimization_problem.variables: |
| 106 | + var_index = self.optimization_problem.get_variable_index(var.name) |
| 107 | + if var.name in conditional_vars: |
| 108 | + conditioning_value = conditional_vars[var.name] |
| 109 | + var.lb = conditioning_value - eps |
| 110 | + var.ub = conditioning_value + eps |
| 111 | + |
| 112 | + else: |
| 113 | + free_vars.update({var.name: var_index}) |
| 114 | + |
| 115 | + X, F = self.approximate_objectives(n_samples=n_samples) |
| 116 | + |
| 117 | + self._reset_bounds_on_variables() |
| 118 | + |
| 119 | + return X, F, free_vars |
0 commit comments