diff --git a/mphys/network/remote_component.py b/mphys/network/remote_component.py index 77fcbec7..b0468b4a 100644 --- a/mphys/network/remote_component.py +++ b/mphys/network/remote_component.py @@ -446,6 +446,18 @@ def _add_objectives_from_baseline_model(self, output_dict): ) self.derivative_coloring_num += 1 + def _lower_bound_used(self, bound): + if hasattr(bound, "__len__"): + return (np.array(bound) > -1e20).any() + else: + return bound + + def _upper_bound_used(self, bound): + if hasattr(bound, "__len__"): + return (np.array(bound) < 1e20).any() + else: + return bound + def _add_constraints_from_baseline_model(self, output_dict): for con in output_dict["constraints"].keys(): self.add_output( @@ -472,9 +484,10 @@ def _add_constraints_from_baseline_model(self, output_dict): ), ) else: - if ( - output_dict["constraints"][con]["lower"] > -1e20 - and output_dict["constraints"][con]["upper"] < 1e20 + if self._lower_bound_used( + output_dict["constraints"][con]["lower"] + ) and self._upper_bound_used( + output_dict["constraints"][con]["upper"] ): # enforce lower and upper bounds self.add_constraint( con.replace(".", self.var_naming_dot_replacement), @@ -491,8 +504,8 @@ def _add_constraints_from_baseline_model(self, output_dict): else None ), ) - elif ( - output_dict["constraints"][con]["lower"] > -1e20 + elif self._lower_bound_used( + output_dict["constraints"][con]["lower"] ): # enforce lower bound self.add_constraint( con.replace(".", self.var_naming_dot_replacement), diff --git a/mphys/network/server.py b/mphys/network/server.py index 866fb857..472823ba 100644 --- a/mphys/network/server.py +++ b/mphys/network/server.py @@ -276,6 +276,18 @@ def _apply_reference_vals_to_desvar_bounds(self, desvar_dict): ) return desvar_dict + def _lower_bound_used(self, bound): + if hasattr(bound, "__len__"): + return (bound > -1e20).any() + else: + return bound + + def _upper_bound_used(self, bound): + if hasattr(bound, "__len__"): + return (bound < 1e20).any() + else: + return bound + def _apply_reference_vals_to_constraint_bounds(self, constraint_dict): if ( constraint_dict["adder"] is None and constraint_dict["scaler"] is None @@ -287,13 +299,13 @@ def _apply_reference_vals_to_constraint_bounds(self, constraint_dict): + constraint_dict["ref0"] ) else: - if constraint_dict["lower"] > -1e20: + if self._lower_bound_used(constraint_dict["lower"]): constraint_dict["lower"] = ( constraint_dict["lower"] * (constraint_dict["ref"] - constraint_dict["ref0"]) + constraint_dict["ref0"] ) - if constraint_dict["upper"] < 1e20: + if self._upper_bound_used(constraint_dict["upper"]): constraint_dict["upper"] = ( constraint_dict["upper"] * (constraint_dict["ref"] - constraint_dict["ref0"]) @@ -306,12 +318,12 @@ def _apply_reference_vals_to_constraint_bounds(self, constraint_dict): - constraint_dict["adder"] ) else: - if constraint_dict["lower"] > -1e20: + if self._lower_bound_used(constraint_dict["lower"]): constraint_dict["lower"] = ( constraint_dict["lower"] / constraint_dict["scaler"] - constraint_dict["adder"] ) - if constraint_dict["upper"] < 1e20: + if self._upper_bound_used(constraint_dict["upper"]): constraint_dict["upper"] = ( constraint_dict["upper"] / constraint_dict["scaler"] - constraint_dict["adder"]