예제 #1
0
 def test_sampler_good_starting_point(self):
     """Test running sampler with valid starting point"""
     input_path = os.path.join(TEST_DIR, '2d.txt')
     constraint = Constraint(input_path)
     sampler = Sampler(constraint)
     results = sampler.sample(100)
     self.assertEqual(len(results), 100)
     self.assertTrue(all(constraint.apply(pt) for pt in results))
예제 #2
0
 def test_sampler_bad_starting_point(self):
     """Test running sampler with invalid starting point, where
     the sampler has to find an edge first"""
     input_path = os.path.join(TEST_DIR, '2d-badstart.txt')
     constraint = Constraint(input_path)
     sampler = Sampler(constraint)
     results = sampler.sample(100)
     self.assertEqual(len(results), 100)
     self.assertTrue(all(constraint.apply(pt) for pt in results))
예제 #3
0
 def test_apply_constraint_func(self):
     """Test applying constraint function to a point to retrieve the value
     of the constraint function.
     """
     input_path = os.path.join(TEST_DIR, '2d.txt')
     constraint = Constraint(input_path)
     self.assertAlmostEqual(
         Sampler._apply_constraint_func(
             constraint.get_constraint_funcs()[1], [0, 1]), -0.2)
     self.assertAlmostEqual(
         Sampler._apply_constraint_func(
             constraint.get_constraint_funcs()[1], [0, 0.5]), 0.3)
예제 #4
0
    def test_apply_constraint_to_step_candidate(self):
        """Test applying constraint function to a point that is stepped to
        in order to retrieve the value of the constraint function."""
        input_path = os.path.join(TEST_DIR, '2d.txt')
        constraint = Constraint(input_path)

        current_pt = np.array([0.5, 0.5])
        step_vector = np.array([0, 1])
        step_vector = step_vector / np.linalg.norm(step_vector)
        mag = 0.2

        value = Sampler._apply_constraint_to_step_candidate(
            constraint.get_constraint_funcs()[1], current_pt, step_vector, mag)
        self.assertAlmostEqual(value, 0.1)
예제 #5
0
    def from_constraint_file(cls, input_file: str, step_tol: float = 1e-8):
        """
        Create sampler directly from constraint input file

        :param input_file: path to constraint definition file
        :param step_tol: magnitude tolerance for step size. If step is smaller
            than the magnitude, will be considered the same point.
        :return: Sampler object built from constraint definition
        """
        return cls(Constraint(input_file), step_tol=step_tol)
예제 #6
0
    def __init__(self, constraint: Constraint, step_tol: float = 1e-8):
        """
        Build sampler object.

        :param constraint: constraint object to define constraints
        :param step_tol: magnitude tolerance for step size. If step is smaller
            than the magnitude, will be considered the same point.
        """
        self.constraint = constraint
        self.step_tol = step_tol
        self._constraint_funcs = self.constraint.get_constraint_funcs()
        self._current_pt = np.array(constraint.get_example())
예제 #7
0
def main(input_file, output_file, n_results):
    """Runs the sampling algorithm on the problem defined in the
    constraint input file INPUT_FILE and outputs N_RESULTS number
    of sampled points to OUTPUT_FILE.
    """
    constraint = Constraint(input_file)
    sampler = Sampler(constraint)
    samples = sampler.sample(int(n_results))

    with open(output_file, 'w') as f:
        for point in samples:
            vector = " ".join(str(v) for v in point)
            f.write(vector + '\n')
예제 #8
0
    def test_is_valid_point(self):
        """Test if points lie in the unit hypercube and satisfy constraints"""
        input_path = os.path.join(TEST_DIR, '2d.txt')
        constraint = Constraint(input_path)
        sampler = Sampler(constraint)

        # Satisfies constraints
        self.assertTrue(sampler._is_valid_point(np.array([0.5, 0.5])))
        # Does not satisfy constraints
        self.assertFalse(sampler._is_valid_point(np.array([0.1, 0.1])))
        # Outside cube
        self.assertFalse(sampler._is_valid_point(np.array([-0.1, 0.1])))
        self.assertFalse(sampler._is_valid_point(np.array([0.1, -0.1])))
        self.assertFalse(sampler._is_valid_point(np.array([1.1, 0.1])))
        self.assertFalse(sampler._is_valid_point(np.array([0.1, 1.1])))