Example #1
0
def generation_strategy_from_json(
        generation_strategy_json: Dict[str, Any]) -> GenerationStrategy:
    """Load generation strategy from JSON."""
    steps = object_from_json(generation_strategy_json.pop("steps"))
    gs = GenerationStrategy(steps=steps,
                            name=generation_strategy_json.pop("name"))
    gs._db_id = object_from_json(generation_strategy_json.pop("db_id"))
    gs._experiment = object_from_json(
        generation_strategy_json.pop("experiment"))
    gs._generated = generation_strategy_json.pop("generated")
    gs._observed = generation_strategy_json.pop("observed")
    gs._data = object_from_json(generation_strategy_json.pop("data"))
    gs._curr = gs._steps[generation_strategy_json.pop("curr_index")]
    gs._generator_runs = object_from_json(
        generation_strategy_json.pop("generator_runs"))
    if generation_strategy_json.pop(
            "had_initialized_model"):  # pragma: no cover
        # If model in the current step was not directly from the `Models` enum,
        # pass its type to `restore_model_from_generator_run`, which will then
        # attempt to use this type to recreate the model.
        if type(gs._curr.model) != Models:
            models_enum = type(gs._curr.model)
            assert issubclass(models_enum, Models)
            # pyre-ignore[6]: `models_enum` typing hackiness
            gs._restore_model_from_generator_run(models_enum=models_enum)
            return gs

        gs._restore_model_from_generator_run()
    return gs
Example #2
0
def generation_strategy_from_json(
    generation_strategy_json: Dict[str, Any]
) -> GenerationStrategy:
    """Load generation strategy from JSON."""
    steps = object_from_json(generation_strategy_json.pop("steps"))
    gs = GenerationStrategy(steps=steps, name=generation_strategy_json.pop("name"))
    gs._experiment = object_from_json(generation_strategy_json.pop("experiment"))
    gs._generated = generation_strategy_json.pop("generated")
    gs._observed = generation_strategy_json.pop("observed")
    gs._data = object_from_json(generation_strategy_json.pop("data"))
    gs._curr = gs._steps[generation_strategy_json.pop("current_step_index")]
    gs._generator_runs = object_from_json(
        generation_strategy_json.pop("generator_runs")
    )
    if generation_strategy_json.pop("had_initialized_model"):  # pragma: no cover
        gs._restore_model_from_generator_run()
    return gs
Example #3
0
def generation_strategy_from_json(
    experiment: Experiment, generation_strategy_json: Dict[str, Any]
) -> GenerationStrategy:
    """Load generation strategy from JSON."""
    gs = GenerationStrategy(
        steps=object_from_json(generation_strategy_json.pop("steps")),
        name=generation_strategy_json.pop("name"),
    )
    gs._generated = generation_strategy_json.pop("generated")
    gs._observed = generation_strategy_json.pop("observed")
    gs._data = object_from_json(generation_strategy_json.pop("data"))
    gs._curr = object_from_json(generation_strategy_json.pop("curr"))
    gs._last_generator_run = object_from_json(
        generation_strategy_json.pop("last_generator_run")
    )
    if generation_strategy_json.pop("had_initialized_model"):  # pragma: no cover
        gs._restore_model_from_generator_run(experiment=experiment)
    return gs
Example #4
0
 def generation_strategy_from_sqa(
         self, gs_sqa: SQAGenerationStrategy) -> GenerationStrategy:
     """Convert SQALchemy generation strategy to Ax `GenerationStrategy`."""
     steps = object_from_json(gs_sqa.steps)
     gs = GenerationStrategy(name=gs_sqa.name, steps=steps)
     gs._generated = list(gs_sqa.generated)
     gs._observed = list(gs_sqa.observed)
     gs._curr = gs._steps[gs_sqa.curr_index]
     # There is just one data object on generation strategy.
     gs._data = self.data_from_sqa(gs_sqa.data)
     gs._generator_runs = [
         self.generator_run_from_sqa(gr) for gr in gs_sqa.generator_runs
     ]
     if len(gs._generator_runs) > 0:
         # Generation strategy had an initialized model.
         # pyre-ignore[16]: SQAGenerationStrategy does not have `experiment` attr.
         gs._experiment = self.experiment_from_sqa(gs_sqa.experiment)
         gs._restore_model_from_generator_run()
     gs._db_id = gs_sqa.id
     return gs
Example #5
0
 def test_equality(self):
     gs1 = GenerationStrategy(steps=[
         GenerationStep(model=Models.SOBOL, num_arms=5),
         GenerationStep(model=Models.GPEI, num_arms=-1),
     ])
     gs2 = gs1.clone_reset()
     self.assertEqual(gs1, gs2)
     gs1._data = get_data()
     self.assertNotEqual(gs1, gs2)
     gs2._data = get_data()
     self.assertEqual(gs1, gs2)
     gs1 = gs1.clone_reset()
     gs2 = gs2.clone_reset()
     gs1.gen(experiment=get_branin_experiment())
     self.assertNotEqual(gs1, gs2)
     gs2.gen(experiment=get_branin_experiment())
     # Each generation strategy generated a different arm, so they are not
     # equal even though they have the same setup and generated the same
     # number of arms.
     self.assertNotEqual(gs1, gs2)