def test_constraint_from_str(self): with self.assertRaisesRegex(ValueError, "Bound for sum constraint"): constraint_from_str( "x1 + x2 <= not_numerical_bound", {"x1": None, "x2": None} ) with self.assertRaisesRegex(ValueError, "Outcome constraint bound"): outcome_constraint_from_str("m1 <= not_numerical_bound")
def with_evaluation_function( parameters: List[TParameterRepresentation], evaluation_function: TEvaluationFunction, experiment_name: Optional[str] = None, objective_name: Optional[str] = None, minimize: bool = False, parameter_constraints: Optional[List[str]] = None, outcome_constraints: Optional[List[str]] = None, total_trials: int = 20, arms_per_trial: int = 1, wait_time: int = 0, random_seed: Optional[int] = None, generation_strategy: Optional[GenerationStrategy] = None, ) -> "OptimizationLoop": """Constructs a synchronous `OptimizationLoop` using an evaluation function.""" exp_parameters = [parameter_from_json(p) for p in parameters] parameter_map = {p.name: p for p in exp_parameters} experiment = SimpleExperiment( name=experiment_name, search_space=SearchSpace( parameters=exp_parameters, parameter_constraints=None if parameter_constraints is None else [ constraint_from_str(c, parameter_map) for c in parameter_constraints ], ), objective_name=objective_name, evaluation_function=evaluation_function, minimize=minimize, outcome_constraints=[ outcome_constraint_from_str(c) for c in (outcome_constraints or []) ], ) return OptimizationLoop( experiment=experiment, total_trials=total_trials, arms_per_trial=arms_per_trial, random_seed=random_seed, wait_time=wait_time, generation_strategy=generation_strategy, )
def test_constraint_from_str(self): with self.assertRaisesRegex(ValueError, "Bound for sum constraint"): constraint_from_str("x1 + x2 <= not_numerical_bound", { "x1": None, "x2": None }) with self.assertRaisesRegex(ValueError, "Outcome constraint bound"): outcome_constraint_from_str("m1 <= not_numerical_bound") three_val_constaint = constraint_from_str( "x1 + x2 + x3 <= 3", { "x1": RangeParameter(name="x1", parameter_type=ParameterType.FLOAT, lower=0.1, upper=2.0), "x2": RangeParameter(name="x2", parameter_type=ParameterType.FLOAT, lower=0.1, upper=2.0), "x3": RangeParameter(name="x3", parameter_type=ParameterType.FLOAT, lower=0.1, upper=2.0), }, ) self.assertEqual([p.name for p in three_val_constaint.parameters], ["x1", "x2", "x3"]) self.assertTrue(three_val_constaint._is_upper_bound) self.assertEqual(three_val_constaint.bound, 3.0) with self.assertRaisesRegex(ValueError, "Parameter constraint should"): constraint_from_str("x1 + x2 + <= 3", { "x1": None, "x2": None, "x3": None }) with self.assertRaisesRegex(ValueError, "Parameter constraint should"): constraint_from_str("x1 + x2 + x3 = 3", { "x1": None, "x2": None, "x3": None })
def test_constraint_from_str(self): with self.assertRaisesRegex(ValueError, "Bound for the constraint"): constraint_from_str("x1 + x2 <= not_numerical_bound", { "x1": None, "x2": None }) with self.assertRaisesRegex(ValueError, "Outcome constraint bound"): outcome_constraint_from_str("m1 <= not_numerical_bound") three_val_constaint = constraint_from_str( "x1 + x2 + x3 <= 3", { "x1": RangeParameter(name="x1", parameter_type=ParameterType.FLOAT, lower=0.1, upper=2.0), "x2": RangeParameter(name="x2", parameter_type=ParameterType.FLOAT, lower=0.1, upper=2.0), "x3": RangeParameter(name="x3", parameter_type=ParameterType.FLOAT, lower=0.1, upper=2.0), }, ) self.assertEqual(three_val_constaint.bound, 3.0) with self.assertRaisesRegex(ValueError, "Parameter constraint should"): constraint_from_str("x1 + x2 + <= 3", { "x1": None, "x2": None, "x3": None }) with self.assertRaisesRegex(ValueError, "Parameter constraint should"): constraint_from_str("x1 + x2 + x3 = 3", { "x1": None, "x2": None, "x3": None }) three_val_constaint2 = constraint_from_str( "-x1 + 2.1*x2 - 4*x3 <= 3", { "x1": RangeParameter(name="x1", parameter_type=ParameterType.FLOAT, lower=0.1, upper=4.0), "x2": RangeParameter(name="x2", parameter_type=ParameterType.FLOAT, lower=0.1, upper=4.0), "x3": RangeParameter(name="x3", parameter_type=ParameterType.FLOAT, lower=0.1, upper=4.0), }, ) self.assertEqual(three_val_constaint2.bound, 3.0) self.assertEqual(three_val_constaint2.constraint_dict, { "x1": -1.0, "x2": 2.1, "x3": -4.0 }) with self.assertRaisesRegex(ValueError, "Multiplier should be float"): constraint_from_str("x1 - e*x2 + x3 <= 3", { "x1": None, "x2": None, "x3": None }) with self.assertRaisesRegex(ValueError, "A linear constraint should be"): constraint_from_str("x1 - 2 *x2 + 3 *x3 <= 3", { "x1": None, "x2": None, "x3": None }) with self.assertRaisesRegex(ValueError, "A linear constraint should be"): constraint_from_str("x1 - 2* x2 + 3* x3 <= 3", { "x1": None, "x2": None, "x3": None }) with self.assertRaisesRegex(ValueError, "A linear constraint should be"): constraint_from_str("x1 - 2 * x2 + 3*x3 <= 3", { "x1": None, "x2": None, "x3": None })