diff --git a/ax/storage/sqa_store/decoder.py b/ax/storage/sqa_store/decoder.py index 5a5dd97e892..2089b3b1cf2 100644 --- a/ax/storage/sqa_store/decoder.py +++ b/ax/storage/sqa_store/decoder.py @@ -1408,7 +1408,7 @@ def _outcome_constraint_from_sqa( def _scalarized_outcome_constraint_from_sqa( self, metric: Metric, metric_sqa: SQAMetric - ) -> ScalarizedOutcomeConstraint: + ) -> OutcomeConstraint: if ( metric_sqa.bound is None or metric_sqa.op is None @@ -1426,6 +1426,17 @@ def _scalarized_outcome_constraint_from_sqa( metric_type_name="Scalarized OutcomeConstraint", ) + expression = (metric_sqa.properties or {}).get("expression") + if isinstance(expression, str): + constraint = OutcomeConstraint( + expression=expression, + metric_name_to_signature={ + child.name: child.signature for child in metrics_sqa_children + }, + ) + constraint.db_id = metric_sqa.id + return constraint + # Extracting metric and weight for each child metrics, weights = zip( *[ diff --git a/ax/storage/sqa_store/encoder.py b/ax/storage/sqa_store/encoder.py index 8e9ee15a1d8..1d1ccde02d2 100644 --- a/ax/storage/sqa_store/encoder.py +++ b/ax/storage/sqa_store/encoder.py @@ -696,9 +696,13 @@ def outcome_constraint_to_sqa( experiment_metrics: dict[str, Metric] | None = None, ) -> SQAMetric: """Convert Ax OutcomeConstraint to SQLAlchemy.""" - if isinstance(outcome_constraint, ScalarizedOutcomeConstraint): + if ( + isinstance(outcome_constraint, ScalarizedOutcomeConstraint) + or len(outcome_constraint.metric_names) > 1 + ): return self.scalarized_outcome_constraint_to_sqa( - outcome_constraint, experiment_metrics=experiment_metrics + outcome_constraint=outcome_constraint, + experiment_metrics=experiment_metrics, ) metric_name = outcome_constraint.metric_names[0] @@ -728,7 +732,7 @@ def outcome_constraint_to_sqa( def scalarized_outcome_constraint_to_sqa( self, - outcome_constraint: ScalarizedOutcomeConstraint, + outcome_constraint: OutcomeConstraint, experiment_metrics: dict[str, Metric] | None = None, ) -> SQAMetric: """Convert Ax Scalarized OutcomeConstraint to SQLAlchemy.""" @@ -739,34 +743,37 @@ def scalarized_outcome_constraint_to_sqa( "must be lists of equal length." ) - # Constructing children SQAMetric classes (these are the real metrics in - # the `ScalarizedObjective`). + # Pair names with weights by position: metric_weights uses signatures. children_metrics = [] - for metric_name, w in metric_weights: - metric = ( - experiment_metrics.get(metric_name, Metric(name=metric_name)) - if experiment_metrics - else Metric(name=metric_name) - ) + for metric_name, (signature, weight) in zip( + outcome_constraint.metric_names, metric_weights, strict=True + ): + metric = (experiment_metrics or {}).get(metric_name) + if metric is None: + metric = Metric(name=metric_name, signature_override=signature) metric_cls = cast(SQAMetric, self.config.class_to_sqa_class[Metric]) - type_and_properties = self.get_metric_type_and_properties(metric=metric) + metric_type, properties = self.get_metric_type_and_properties(metric=metric) children_metrics.append( metric_cls( # pyre-ignore[29]: `SQAMetric` is not a function. id=None, name=metric_name, signature=metric.signature, - metric_type=type_and_properties[0], + metric_type=metric_type, intent=MetricIntent.OUTCOME_CONSTRAINT, - properties=type_and_properties[1], + properties=properties, lower_is_better=metric.lower_is_better, - scalarized_outcome_constraint_weight=w, + scalarized_outcome_constraint_weight=weight, bound=outcome_constraint.bound, op=outcome_constraint.op, relative=outcome_constraint.relative, ) ) - # Constructing a parent SQAMetric class + parent_properties = ( + {} + if isinstance(outcome_constraint, ScalarizedOutcomeConstraint) + else {"expression": outcome_constraint.expression} + ) parent_metric_cls = cast(SQAMetric, self.config.class_to_sqa_class[Metric]) parent_metric = parent_metric_cls( # pyre-ignore[29]: `SQAMetric` not a func. id=outcome_constraint.db_id, @@ -777,6 +784,7 @@ def scalarized_outcome_constraint_to_sqa( bound=outcome_constraint.bound, op=outcome_constraint.op, relative=outcome_constraint.relative, + properties=parent_properties, scalarized_outcome_constraint_children_metrics=children_metrics, ) return parent_metric diff --git a/ax/storage/sqa_store/tests/test_sqa_store.py b/ax/storage/sqa_store/tests/test_sqa_store.py index fd5362dc352..3b0c4b089c4 100644 --- a/ax/storage/sqa_store/tests/test_sqa_store.py +++ b/ax/storage/sqa_store/tests/test_sqa_store.py @@ -1374,6 +1374,31 @@ def test_experiment_outcome_constraint_updates(self) -> None: loaded_experiment = load_experiment(experiment.name) self.assertEqual(experiment, loaded_experiment) + def test_weighted_outcome_constraint_sql_roundtrip(self) -> None: + """SQL preserves modern constraint expressions, weights, and signatures.""" + expression = "2 * cost - latency <= 10" + experiment = get_experiment_with_batch_trial() + experiment.add_tracking_metric( + Metric(name="cost", signature_override="canonical_cost") + ) + experiment.add_tracking_metric(Metric(name="latency")) + constraint = OutcomeConstraint( + expression=expression, + metric_name_to_signature={"cost": "canonical_cost", "latency": "latency"}, + ) + config = none_throws(experiment.optimization_config) + config.outcome_constraints = [constraint] + save_experiment(experiment) + + loaded = load_experiment(experiment.name) + (loaded_constraint,) = none_throws( + loaded.optimization_config + ).outcome_constraints + self.assertEqual(loaded_constraint.expression, expression) + self.assertEqual(loaded_constraint.metric_weights, constraint.metric_weights) + self.assertEqual(loaded_constraint.op, constraint.op) + self.assertEqual(loaded_constraint.bound, constraint.bound) + def test_experiment_pruning_target_arm(self) -> None: experiment = get_experiment_with_batch_trial() pruning_target_parameterization = next(