code: def test_apply_constraints(self):
      # nonnegative objective, one constraint
      samples = torch.randn(1)
      obj = ones_f(samples)
      obj = apply_constraints(
          obj=obj,
          constraints=[zeros_f],
          samples=samples,
          infeasible_cost=0.0,
      )
      self.assertTrue(torch.equal(obj, ones_f(samples) * 0.5))
      # nonnegative objective, two constraint
      samples = torch.randn(1)
      obj = ones_f(samples)
      obj = apply_constraints(
          obj=obj,
          constraints=[zeros_f, zeros_f],
          samples=samples,
          infeasible_cost=0.0,
      )
      self.assertTrue(torch.equal(obj, ones_f(samples) * 0.5 * 0.5))
      # negative objective, one constraint, infeasible_cost
      samples = torch.randn(1)
      obj = minus_one_f(samples)
      obj = apply_constraints(
          obj=obj,
          constraints=[zeros_f],
          samples=samples,
          infeasible_cost=2.0,
      )
      self.assertTrue(torch.equal(obj, ones_f(samples) * 0.5 - 2.0))
  
      # nonnegative objective, one constraint, eta = 0
      samples = torch.randn(1)
      obj = ones_f(samples)
      with self.assertRaisesRegex(ValueError, "eta must be positive."):
          apply_constraints(
              obj=obj,
              constraints=[zeros_f],
              samples=samples,
              infeasible_cost=0.0,
              eta=0.0,
          )
  
      # soft_eval_constraint is not in the path of apply_constraints, adding this test
      # for coverage.
      with self.assertRaisesRegex(ValueError, "eta must be positive."):
          soft_eval_constraint(lhs=obj, eta=0.0)
      ind = soft_eval_constraint(lhs=ones_f(samples), eta=1e-6)
      self.assertAllClose(ind, torch.zeros_like(ind))
      ind = soft_eval_constraint(lhs=-ones_f(samples), eta=1e-6)
      self.assertAllClose(ind, torch.ones_like(ind))
  
imports: import torch
  from botorch.utils import apply_constraints, get_objective_weights_transform
  from botorch.utils.objective import compute_feasibility_indicator, compute_smoothed_feasibility_indicator, soft_eval_constraint
  from botorch.utils.testing import BotorchTestCase
  from torch import Tensor
label_desc: Use image node_id 1 for calling the TestApplyConstraints obj's underlying member method code with example usage: obj.test_apply_constraints() without return types