def f(self, x: np.ndarray) -> float: x1, x2, t = x if self.rate is not None: weight = 1.0 + np.exp(-not_none(self.rate) * t) else: weight = 1.0 return checked_cast(float, branin(x1=x1, x2=x2)) * weight
def get_branin_data( trial_indices: Optional[Iterable[int]] = None, trials: Optional[Iterable[Trial]] = None, ) -> Data: if trial_indices and trials: raise ValueError("Expected `trial_indices` or `trials`, not both.") if trials: df_dicts = [ { "trial_index": trial.index, "metric_name": "branin", "arm_name": not_none(checked_cast(Trial, trial).arm).name, "mean": branin( float(not_none( trial.arm).parameters["x1"]), # pyre-ignore[6] float(not_none( trial.arm).parameters["x2"]), # pyre-ignore[6] ), "sem": 0.0, } for trial in trials ] else: df_dicts = [{ "trial_index": trial_index, "metric_name": "branin", "arm_name": f"{trial_index}_0", "mean": 5.0, "sem": 0.0, } for trial_index in (trial_indices or [0])] return Data(df=pd.DataFrame.from_records(df_dicts))
def testEncodeDecodeSimpleBenchmarkProblem(self): branin_problem = get_branin_simple_benchmark_problem() sum_problem = get_sum_simple_benchmark_problem() new_branin_problem = object_from_json(object_to_json(branin_problem)) new_sum_problem = object_from_json(object_to_json(sum_problem)) self.assertEqual(branin_problem.f(1, 2), new_branin_problem.f(1, 2), branin(1, 2)) self.assertEqual(sum_problem.f([1, 2]), new_sum_problem.f([1, 2]), 3)
def branin_centered(X): # Evaluate branin problem, scaled to X \in [-1, 1]^2 # Map from [-1, 1]^2 to [[-5, 10], [0, 15]] assert X.min() >= -1 assert X.max() <= 1 Xu = (X + 1) / 2. Xu *= 15 Xu[:, 0] -= 5 return branin(Xu)
def f(self, x: np.ndarray) -> Mapping[str, Any]: if self.index < len(FIDELITY): self.index += 1 x1, x2 = x fidelity = FIDELITY[self.index] fidelity_penalty = random() * math.pow(1.0 - fidelity, 2.0) mean = checked_cast(float, branin(x1=x1, x2=x2)) - fidelity_penalty return {"mean": mean, "fidelity": fidelity}
def f(self, x: np.ndarray, timestamp: int) -> Mapping[str, Any]: x1, x2 = x if self.rate is not None: weight = 1.0 + np.exp(-not_none(self.rate) * timestamp) else: weight = 1.0 mean = checked_cast(float, branin(x1=x1, x2=x2)) * weight return {"mean": mean, "timestamp": timestamp}
def get_branin_data_batch(batch: BatchTrial) -> Data: return Data( pd.DataFrame({ "arm_name": [arm.name for arm in batch.arms], "metric_name": "branin", "mean": [ # pyre-ignore[6]: This function can fail if a parameter value # does not support conversion to float. branin(float(arm.parameters["x1"]), float(arm.parameters["x2"])) for arm in batch.arms ], "sem": 0.1, }))
def testEncodeDecodeSimpleBenchmarkProblem(self): branin_problem = get_branin_simple_benchmark_problem() sum_problem = get_sum_simple_benchmark_problem() new_branin_problem = object_from_json(object_to_json(branin_problem)) new_sum_problem = object_from_json(object_to_json(sum_problem)) self.assertEqual(branin_problem.f(1, 2), new_branin_problem.f(1, 2), branin(1, 2)) self.assertEqual(sum_problem.f([1, 2]), new_sum_problem.f([1, 2]), 3) # Test using `from_botorch`. ackley_problem = SimpleBenchmarkProblem(f=from_botorch(Ackley()), noise_sd=0.0, minimize=True) new_ackley_problem = object_from_json(object_to_json(ackley_problem)) self.assertEqual(ackley_problem.f(1, 2), new_ackley_problem.f(1, 2), ackley(1, 2))
def test_branin(self): self.assertEqual(branin.name, "Branin") self.assertAlmostEqual(branin(1, 2), 21.62763539206238) self.assertAlmostEqual(branin(x1=1, x2=2), 21.62763539206238) self.assertAlmostEqual(branin(np.array([1, 2])), 21.62763539206238) self.assertAlmostEqual(branin(np.array([[1, 2], [1, 2]]))[0], 21.62763539206238) self.assertAlmostEqual(branin.minimums[0][0], -np.pi) self.assertAlmostEqual(branin.fmin, 0.397887, places=6) self.assertAlmostEqual(branin.fmax, 308.129, places=3) self.assertAlmostEqual(branin.fmax, branin(-5, 0), places=3) self.assertEqual(branin.domain[0], (-5, 10)) self.assertEqual(branin.required_dimensionality, 2) with self.assertRaisesRegex(NotImplementedError, "Branin does not specify"): branin.maximums with self.assertRaisesRegex(ValueError, "Synthetic function call"): branin(np.array([[[1, 3]]]))
def testEncodeDecodeSimpleBenchmarkProblem(self): branin_problem = get_branin_simple_benchmark_problem() sum_problem = get_sum_simple_benchmark_problem() new_branin_problem = object_from_json( object_to_json( branin_problem, encoder_registry=DEPRECATED_ENCODER_REGISTRY, class_encoder_registry=DEPRECATED_CLASS_ENCODER_REGISTRY, ), decoder_registry=DEPRECATED_DECODER_REGISTRY, class_decoder_registry=DEPRECATED_CLASS_DECODER_REGISTRY, ) new_sum_problem = object_from_json( object_to_json( sum_problem, encoder_registry=DEPRECATED_ENCODER_REGISTRY, class_encoder_registry=DEPRECATED_CLASS_ENCODER_REGISTRY, ), decoder_registry=DEPRECATED_DECODER_REGISTRY, class_decoder_registry=DEPRECATED_CLASS_DECODER_REGISTRY, ) self.assertEqual( branin_problem.f(1, 2), new_branin_problem.f(1, 2), branin(1, 2) ) self.assertEqual(sum_problem.f([1, 2]), new_sum_problem.f([1, 2]), 3) # Test using `from_botorch`. ackley_problem = SimpleBenchmarkProblem( f=from_botorch(Ackley()), noise_sd=0.0, minimize=True ) new_ackley_problem = object_from_json( object_to_json( ackley_problem, encoder_registry=DEPRECATED_ENCODER_REGISTRY, class_encoder_registry=DEPRECATED_CLASS_ENCODER_REGISTRY, ), decoder_registry=DEPRECATED_DECODER_REGISTRY, class_decoder_registry=DEPRECATED_CLASS_DECODER_REGISTRY, ) self.assertEqual( ackley_problem.f(1, 2), new_ackley_problem.f(1, 2), ackley(1, 2) )
def f(self, x: np.ndarray) -> float: x1, x2 = x return checked_cast(float, branin(x1=x1, x2=x2))
def f(self, x: np.ndarray) -> float: x1, x2, fidelity = x fidelity_penalty = random() * math.pow(1.0 - fidelity, 2.0) return checked_cast(float, branin(x1=x1, x2=x2)) - fidelity_penalty