def iterate(self, f, search_area): x_new = generate_vector_in_sphere(self.x, self.radius) x_new = bound_vector(x_new, search_area) f_x_new = f(x_new) if f_x_new < self.f_x: self.x = x_new self.f_x = f_x_new
def _move_stars(self, f, search_area): black_hole = self.stars[self.black_hole_id] self.stars = [ s + np.random.rand() * (black_hole - s) for i, s in enumerate(self.stars) if i != self.black_hole_id ] self.stars = [bound_vector(s, search_area) for s in self.stars] + [black_hole] self._calculate_quality_levels(f)
def iterate(self, f, search_area): center_of_mass = self._get_center_of_mass() self.points = [center_of_mass + self._generate_delta(x) for x in self.points] self.points = [bound_vector(x, search_area) for x in self.points] self.points.append(center_of_mass) self._calculate_values(f) self.iteration_number += 1
def iterate(self, f, search_area): gradient = approx_fprime(self.x, f, epsilon=self.eps) gradient_length = np.linalg.norm(gradient) if gradient_length > 0: gradient /= gradient_length x_new = self.x - np.random.uniform(0.0, self.radius) * gradient x_new = bound_vector(x_new, search_area) f_x_new = f(x_new) if f_x_new < self.f_x: self.x = x_new self.f_x = f_x_new
def test_bound_vector_1(): v = np.array([1, 2, 3]) bounds = np.array([[-5, 5] * len(v)]) npt.assert_equal(v, bound_vector(v, bounds))
def test_bound_vector_N(_): search_area = np.array([[-10, 10], [-10, 10], [-10, 10]]) bounds = np.array([[-5, 5], [-5, 5], [-5, 5]]) vector = generate_vector_in_area(search_area) vector = bound_vector(vector, bounds) assert ((bounds[:, 0] <= vector) & (vector <= bounds[:, 1])).all()
def test_bound_vector_3(): v = np.array([5, -5, 0]) bounds = np.array([[-2, 2] * len(v)]) npt.assert_equal(np.array([2, -2, 0]), bound_vector(v, bounds))
def test_bound_vector_2(): v = np.array([1, 2, 3]) bounds = np.array([[-2, 2] * len(v)]) npt.assert_equal(np.array([1, 2, 2]), bound_vector(v, bounds))