def testExperimentTrackingMetricUpdates(self): experiment = get_experiment_with_batch_trial() save_experiment(experiment) self.assertEqual(get_session().query(SQAMetric).count(), len(experiment.metrics)) # update tracking metric # (should perform update in place) metric = Metric(name="tracking", lower_is_better=True) experiment.update_tracking_metric(metric) save_experiment(experiment) self.assertEqual(get_session().query(SQAMetric).count(), len(experiment.metrics)) # add tracking metric metric = Metric(name="tracking2") experiment.add_tracking_metric(metric) save_experiment(experiment) self.assertEqual(get_session().query(SQAMetric).count(), len(experiment.metrics)) # remove tracking metric # (old one should be deleted) experiment.remove_tracking_metric("tracking2") save_experiment(experiment) self.assertEqual(get_session().query(SQAMetric).count(), len(experiment.metrics)) loaded_experiment = load_experiment(experiment.name) self.assertEqual(experiment, loaded_experiment)
def testExperimentRunnerUpdates(self): experiment = get_experiment_with_batch_trial() save_experiment(experiment) # one runner on the batch self.assertEqual(get_session().query(SQARunner).count(), 1) # add runner to experiment runner = get_synthetic_runner() experiment.runner = runner save_experiment(experiment) self.assertEqual(get_session().query(SQARunner).count(), 2) # update runner # (should perform update in place) runner = get_synthetic_runner() runner.dummy_metadata = {"foo": "bar"} experiment.runner = runner save_experiment(experiment) self.assertEqual(get_session().query(SQARunner).count(), 2) # remove runner # (old one should be deleted) experiment.runner = None save_experiment(experiment) self.assertEqual(get_session().query(SQARunner).count(), 1) loaded_experiment = load_experiment(experiment.name) self.assertEqual(experiment, loaded_experiment)
def testExperimentGeneratorRunUpdates(self): experiment = get_experiment_with_batch_trial() save_experiment(experiment) # one main generator run, one for the status quo self.assertEqual(get_session().query(SQAGeneratorRun).count(), 2) # add a arm # this will create one wrapper generator run # this will also replace the status quo generator run, # since the weight of the status quo will have changed trial = experiment.trials[0] trial.add_arm(get_arm()) save_experiment(experiment) self.assertEqual(get_session().query(SQAGeneratorRun).count(), 3) generator_run = get_generator_run() # TODO[Lena, T46190605]: remove generator_run._model_key = None generator_run._model_kwargs = None generator_run._bridge_kwargs = None trial.add_generator_run(generator_run=generator_run, multiplier=0.5) save_experiment(experiment) self.assertEqual(get_session().query(SQAGeneratorRun).count(), 4) loaded_experiment = load_experiment(experiment.name) self.assertEqual(experiment, loaded_experiment)
def testExperimentObjectiveUpdates(self): experiment = get_experiment_with_batch_trial() save_experiment(experiment) self.assertEqual(get_session().query(SQAMetric).count(), len(experiment.metrics)) # update objective # (should perform update in place) optimization_config = get_optimization_config() objective = get_objective() objective.minimize = True optimization_config.objective = objective experiment.optimization_config = optimization_config save_experiment(experiment) self.assertEqual(get_session().query(SQAMetric).count(), len(experiment.metrics)) # replace objective # (old one should become tracking metric) optimization_config.objective = Objective(metric=Metric( name="objective")) experiment.optimization_config = optimization_config save_experiment(experiment) self.assertEqual(get_session().query(SQAMetric).count(), len(experiment.metrics)) loaded_experiment = load_experiment(experiment.name) self.assertEqual(experiment, loaded_experiment)
def testExperimentAbandonedArmUpdates(self): experiment = get_experiment_with_batch_trial() save_experiment(experiment) # one arm is already abandoned self.assertEqual(get_session().query(SQAAbandonedArm).count(), 1) trial = experiment.trials[0] trial.mark_arm_abandoned(trial.arms[1].name) save_experiment(experiment) self.assertEqual(get_session().query(SQAAbandonedArm).count(), 2) loaded_experiment = load_experiment(experiment.name) self.assertEqual(experiment, loaded_experiment)
def testExperimentParameterUpdates(self): experiment = get_experiment_with_batch_trial() save_experiment(experiment) self.assertEqual( get_session().query(SQAParameter).count(), len(experiment.search_space.parameters), ) # update a parameter # (should perform update in place) search_space = get_search_space() parameter = get_choice_parameter() parameter.add_values(["foobar"]) search_space.update_parameter(parameter) experiment.search_space = search_space save_experiment(experiment) self.assertEqual( get_session().query(SQAParameter).count(), len(experiment.search_space.parameters), ) # add a parameter parameter = RangeParameter(name="x1", parameter_type=ParameterType.FLOAT, lower=-5, upper=10) search_space.add_parameter(parameter) experiment.search_space = search_space save_experiment(experiment) self.assertEqual( get_session().query(SQAParameter).count(), len(experiment.search_space.parameters), ) # remove a parameter # (old one should be deleted) del search_space._parameters["x1"] experiment.search_space = search_space save_experiment(experiment) self.assertEqual( get_session().query(SQAParameter).count(), len(experiment.search_space.parameters), ) loaded_experiment = load_experiment(experiment.name) self.assertEqual(experiment, loaded_experiment)
def testExperimentUpdates(self): experiment = get_experiment_with_batch_trial() save_experiment(experiment) self.assertEqual(get_session().query(SQAExperiment).count(), 1) # update experiment # (should perform update in place) experiment.description = "foobar" save_experiment(experiment) self.assertEqual(get_session().query(SQAExperiment).count(), 1) experiment.status_quo = Arm(parameters={"w": 0.0, "x": 1, "y": "y", "z": True}) save_experiment(experiment) self.assertEqual(get_session().query(SQAExperiment).count(), 1) loaded_experiment = load_experiment(experiment.name) self.assertEqual(experiment, loaded_experiment)
def testExperimentTrialUpdates(self): experiment = get_experiment_with_batch_trial() save_experiment(experiment) self.assertEqual(get_session().query(SQATrial).count(), 1) self.assertEqual(get_session().query(SQARunner).count(), 1) # add trial trial = experiment.new_batch_trial() runner = get_synthetic_runner() trial.runner = runner save_experiment(experiment) self.assertEqual(get_session().query(SQATrial).count(), 2) self.assertEqual(get_session().query(SQARunner).count(), 2) # update trial's runner runner.dummy_metadata = "dummy metadata" trial.runner = runner save_experiment(experiment) self.assertEqual(get_session().query(SQATrial).count(), 2) self.assertEqual(get_session().query(SQARunner).count(), 2) trial.run() save_experiment(experiment) self.assertEqual(get_session().query(SQATrial).count(), 2) loaded_experiment = load_experiment(experiment.name) self.assertEqual(experiment, loaded_experiment)
def testExperimentParameterConstraintUpdates(self): experiment = get_experiment_with_batch_trial() save_experiment(experiment) self.assertEqual( get_session().query(SQAParameterConstraint).count(), # 3 len(experiment.search_space.parameter_constraints), # 3 ) # add a parameter constraint search_space = experiment.search_space existing_constraint = experiment.search_space.parameter_constraints[0] new_constraint = get_sum_constraint2() search_space.add_parameter_constraints([new_constraint]) experiment.search_space = search_space save_experiment(experiment) self.assertEqual( get_session().query(SQAParameterConstraint).count(), len(experiment.search_space.parameter_constraints), ) # update a parameter constraint # (since we don't have UIDs for these, we throw out the old one # and create a new one) new_constraint.bound = 5.0 search_space.set_parameter_constraints( [existing_constraint, new_constraint]) experiment.search_space = search_space save_experiment(experiment) self.assertEqual( get_session().query(SQAParameterConstraint).count(), len(experiment.search_space.parameter_constraints), ) # remove a parameter constraint # (old one should be deleted) search_space.set_parameter_constraints([new_constraint]) experiment.search_space = search_space save_experiment(experiment) self.assertEqual( get_session().query(SQAParameterConstraint).count(), len(experiment.search_space.parameter_constraints), ) loaded_experiment = load_experiment(experiment.name) self.assertEqual(experiment, loaded_experiment)
def testExperimentObjectiveThresholdUpdates(self): experiment = get_experiment_with_batch_trial() save_experiment(experiment) self.assertEqual(get_session().query(SQAMetric).count(), len(experiment.metrics)) # update objective threshold # (should perform update in place) optimization_config = get_multi_objective_optimization_config() objective_threshold = get_objective_threshold() optimization_config.objective_thresholds = [objective_threshold] experiment.optimization_config = optimization_config save_experiment(experiment) self.assertEqual(get_session().query(SQAMetric).count(), 6) # add outcome constraint outcome_constraint2 = OutcomeConstraint(metric=Metric(name="outcome"), op=ComparisonOp.GEQ, bound=-0.5) optimization_config.outcome_constraints = [ optimization_config.outcome_constraints[0], outcome_constraint2, ] experiment.optimization_config = optimization_config save_experiment(experiment) self.assertEqual(get_session().query(SQAMetric).count(), 7) # remove outcome constraint # (old one should become tracking metric) optimization_config.outcome_constraints = [] experiment.optimization_config = optimization_config save_experiment(experiment) self.assertEqual(get_session().query(SQAMetric).count(), 5) loaded_experiment = load_experiment(experiment.name) self.assertEqual(experiment, loaded_experiment) # Optimization config should correctly reload even with no # objective_thresholds optimization_config.objective_thresholds = [] save_experiment(experiment) self.assertEqual(get_session().query(SQAMetric).count(), 4) loaded_experiment = load_experiment(experiment.name) self.assertEqual(experiment, loaded_experiment)
def testExperimentOutcomeConstraintUpdates(self): experiment = get_experiment_with_batch_trial() save_experiment(experiment) self.assertEqual( get_session().query(SQAMetric).count(), len(experiment.metrics) ) # update outcome constraint # (should perform update in place) optimization_config = get_optimization_config() outcome_constraint = get_outcome_constraint() outcome_constraint.bound = -1.0 optimization_config.outcome_constraints = [outcome_constraint] experiment.optimization_config = optimization_config save_experiment(experiment) self.assertEqual( get_session().query(SQAMetric).count(), len(experiment.metrics) ) # add outcome constraint outcome_constraint2 = OutcomeConstraint( metric=Metric(name="outcome"), op=ComparisonOp.GEQ, bound=-0.5 ) optimization_config.outcome_constraints = [ outcome_constraint, outcome_constraint2, ] experiment.optimization_config = optimization_config save_experiment(experiment) self.assertEqual( get_session().query(SQAMetric).count(), len(experiment.metrics) ) # remove outcome constraint # (old one should become tracking metric) optimization_config.outcome_constraints = [outcome_constraint] experiment.optimization_config = optimization_config save_experiment(experiment) self.assertEqual( get_session().query(SQAMetric).count(), len(experiment.metrics) ) loaded_experiment = load_experiment(experiment.name) self.assertEqual(experiment, loaded_experiment)