From 1807a973fd81c2d6487201195a6a4c6d5a3e454e Mon Sep 17 00:00:00 2001 From: Cole Blanchard Date: Mon, 30 Sep 2024 16:25:52 -0400 Subject: [PATCH 1/6] weighted interventions --- service/models/operations/optimize.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/service/models/operations/optimize.py b/service/models/operations/optimize.py index 1f6bc2c..3acdfce 100644 --- a/service/models/operations/optimize.py +++ b/service/models/operations/optimize.py @@ -59,13 +59,19 @@ def gen_risk_bound(self): return -self.risk_bound -def objfun(x, initial_guess, objective_function_option): - if objective_function_option == "lower_bound": - return np.sum(np.abs(x)) - if objective_function_option == "upper_bound": - return -np.sum(np.abs(x)) - if objective_function_option == "initial_guess": - return np.sum(np.abs(x - initial_guess)) +def objfun(x, initial_guess, objective_function_option, relative_importance): + total_sum = 0 + sum_of_all_weights = sum(relative_importance) + + for i in range(len(x)): + weight = relative_importance[i] / sum_of_all_weights + if objective_function_option[i] == "lower_bound": + total_sum += weight * np.abs(x[i]) + elif objective_function_option[i] == "upper_bound": + total_sum += weight * -np.abs(x[i]) + elif objective_function_option[i] == "initial_guess": + total_sum += weight * np.abs(x[i] - initial_guess[i]) + return total_sum class InterventionObjective(BaseModel): @@ -79,6 +85,7 @@ class InterventionObjective(BaseModel): start_time: Optional[list[float]] = None objective_function_option: Optional[list[str]] = None initial_guess: Optional[list[float]] = None + relative_importance: Optional[list[float]] = None class OptimizeExtra(BaseModel): @@ -198,8 +205,9 @@ def progress_hook(current_results): "end_time": self.timespan.end, "objfun": lambda x: objfun( x, - self.optimize_interventions.initial_guess[0], - self.optimize_interventions.objective_function_option[0], + self.optimize_interventions.initial_guess, + self.optimize_interventions.objective_function_option, + self.optimize_interventions.relative_importance, ), "qoi": qoi_methods, "risk_bound": risk_bounds, From ba71a34c4960b087bafbd96d9dc084c0d77459f3 Mon Sep 17 00:00:00 2001 From: Cole Blanchard Date: Tue, 1 Oct 2024 09:18:51 -0400 Subject: [PATCH 2/6] np --- service/models/operations/optimize.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/models/operations/optimize.py b/service/models/operations/optimize.py index 3acdfce..3ca21c0 100644 --- a/service/models/operations/optimize.py +++ b/service/models/operations/optimize.py @@ -61,7 +61,7 @@ def gen_risk_bound(self): def objfun(x, initial_guess, objective_function_option, relative_importance): total_sum = 0 - sum_of_all_weights = sum(relative_importance) + sum_of_all_weights = np.sum(relative_importance) for i in range(len(x)): weight = relative_importance[i] / sum_of_all_weights From efa15b16a1b4d208383cc8244c0c4b2a51cda84b Mon Sep 17 00:00:00 2001 From: Cole Blanchard Date: Tue, 1 Oct 2024 09:23:02 -0400 Subject: [PATCH 3/6] fallback to 1 --- service/models/operations/optimize.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/models/operations/optimize.py b/service/models/operations/optimize.py index 3ca21c0..af1b7b7 100644 --- a/service/models/operations/optimize.py +++ b/service/models/operations/optimize.py @@ -61,7 +61,7 @@ def gen_risk_bound(self): def objfun(x, initial_guess, objective_function_option, relative_importance): total_sum = 0 - sum_of_all_weights = np.sum(relative_importance) + sum_of_all_weights = np.sum(relative_importance) or 1 # fallback to 1 if sum is 0 for i in range(len(x)): weight = relative_importance[i] / sum_of_all_weights From ba7b5363105e269c4556369374ee063718313bbe Mon Sep 17 00:00:00 2001 From: Cole Blanchard Date: Tue, 1 Oct 2024 09:52:16 -0400 Subject: [PATCH 4/6] check values exist --- service/models/operations/optimize.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/service/models/operations/optimize.py b/service/models/operations/optimize.py index af1b7b7..d449daa 100644 --- a/service/models/operations/optimize.py +++ b/service/models/operations/optimize.py @@ -62,6 +62,13 @@ def gen_risk_bound(self): def objfun(x, initial_guess, objective_function_option, relative_importance): total_sum = 0 sum_of_all_weights = np.sum(relative_importance) or 1 # fallback to 1 if sum is 0 + if ( + x is None + or initial_guess is None + or objective_function_option is None + or relative_importance is None + ): + return total_sum for i in range(len(x)): weight = relative_importance[i] / sum_of_all_weights From b3af58db1bb9295212dbc57756bc7812158d61ad Mon Sep 17 00:00:00 2001 From: Cole Blanchard Date: Tue, 1 Oct 2024 11:30:37 -0400 Subject: [PATCH 5/6] update test + address comments --- service/models/operations/optimize.py | 30 ++++++++++++++++++++-- tests/examples/optimize/input/request.json | 3 ++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/service/models/operations/optimize.py b/service/models/operations/optimize.py index d449daa..ebd7f89 100644 --- a/service/models/operations/optimize.py +++ b/service/models/operations/optimize.py @@ -60,24 +60,50 @@ def gen_risk_bound(self): def objfun(x, initial_guess, objective_function_option, relative_importance): + """ + Calculate the weighted sum of objective functions based on the given parameters. + + Parameters: + x (list or array): The current values of the variables. + initial_guess (list or array): The initial guess values of the variables. + objective_function_option (list): List of options specifying the type of objective function for each variable. + relative_importance (list): List of weights indicating the relative importance of each variable. + + Returns: + float: The weighted sum of the objective functions. + """ + + # Initialize the total sum to zero total_sum = 0 - sum_of_all_weights = np.sum(relative_importance) or 1 # fallback to 1 if sum is 0 + + # Calculate the sum of all weights, fallback to 1 if the sum is 0 + sum_of_all_weights = np.sum(relative_importance) or 1 + + # Check if any of the required parameters is None and raise an error if so if ( x is None or initial_guess is None or objective_function_option is None or relative_importance is None ): - return total_sum + raise ValueError( + "There was an issue creating the objective function. None of the parameters x, initial_guess, objective_function_option, or relative_importance can be None" + ) + # Iterate over each variable for i in range(len(x)): + # Calculate the weight for the current variable weight = relative_importance[i] / sum_of_all_weights + + # Apply the corresponding objective function based on the option provided if objective_function_option[i] == "lower_bound": total_sum += weight * np.abs(x[i]) elif objective_function_option[i] == "upper_bound": total_sum += weight * -np.abs(x[i]) elif objective_function_option[i] == "initial_guess": total_sum += weight * np.abs(x[i] - initial_guess[i]) + + # Return the total weighted sum of the objective functions return total_sum diff --git a/tests/examples/optimize/input/request.json b/tests/examples/optimize/input/request.json index f3e1d71..9d6be1e 100644 --- a/tests/examples/optimize/input/request.json +++ b/tests/examples/optimize/input/request.json @@ -8,7 +8,8 @@ "start_time": [2], "param_names": ["beta"], "param_values": [0.02], - "initial_guess": [0] + "initial_guess": [0], + "relative_importance": [1] }, "timespan": { "start": 0, From 5a1626d958021a6951fa8138484d733e6b085796 Mon Sep 17 00:00:00 2001 From: Cole Blanchard <33158416+blanchco@users.noreply.github.com> Date: Wed, 2 Oct 2024 09:52:57 -0400 Subject: [PATCH 6/6] Update optimize.py --- service/models/operations/optimize.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/service/models/operations/optimize.py b/service/models/operations/optimize.py index ebd7f89..476e657 100644 --- a/service/models/operations/optimize.py +++ b/service/models/operations/optimize.py @@ -64,8 +64,8 @@ def objfun(x, initial_guess, objective_function_option, relative_importance): Calculate the weighted sum of objective functions based on the given parameters. Parameters: - x (list or array): The current values of the variables. - initial_guess (list or array): The initial guess values of the variables. + x (list): The current values of the variables. + initial_guess (list): The initial guess values of the variables. objective_function_option (list): List of options specifying the type of objective function for each variable. relative_importance (list): List of weights indicating the relative importance of each variable.