def test_from_array_with_scale_positive(self): array = numpy.array([[0, 0, 0], [10, 0, 0], [0, 10, 0], [10, 10, 10]]) bounds = Bounds.from_array(array, scale=1.1) assert (bounds.low == numpy.array([0, 0, 0])).all(), (bounds.low, array.min(axis=0)) assert (bounds.high == numpy.array([11, 11, 11])).all(), (bounds.high, array.max(axis=0)) assert bounds.shape == (3, ) array = numpy.array([[-10, 0, 0], [-10, 0, 0], [0, -10, 0], [-10, -10, -10]]) bounds = Bounds.from_array(array, scale=1.1) assert (bounds.high == numpy.array([0, 0, 0])).all(), (bounds.high, array.max(axis=0)) assert (bounds.low == numpy.array([-11, -11, -11])).all(), (bounds.low, array.min(axis=0)) assert bounds.shape == (3, ) array = numpy.array([[10, 10, 10], [100, 10, 10], [10, 100, 10], [100, 100, 100]]) bounds = Bounds.from_array(array, scale=1.1) assert numpy.allclose(bounds.low, numpy.array([9.0, 9.0, 9])), ( bounds.low, array.min(axis=0), ) assert numpy.allclose(bounds.high, numpy.array([110, 110, 110])), ( bounds.high, array.max(axis=0), ) assert bounds.shape == (3, )
def test_from_array_with_scale_negative(self): # high +, low +, scale > 1 array = numpy.array([[-10, 0, 0], [-10, 0, 0], [0, -10, 0], [-10, -10, -10]]) bounds = Bounds.from_array(array, scale=0.9) assert (bounds.high == numpy.array([0, 0, 0])).all(), (bounds.high, array.max(axis=0)) assert (bounds.low == numpy.array([-9, -9, -9])).all(), (bounds.low, array.min(axis=0)) assert bounds.shape == (3, ) array = numpy.array([[0, 0, 0], [10, 0, 0], [0, 10, 0], [10, 10, 10]]) bounds = Bounds.from_array(array, scale=0.9) assert (bounds.low == numpy.array([0, 0, 0])).all(), (bounds, array) assert (bounds.high == numpy.array([9, 9, 9])).all() assert bounds.shape == (3, ) # high +, low +, scale < 1 array = numpy.array([[10, 10, 10], [100, 10, 10], [10, 100, 10], [100, 100, 100]]) bounds = Bounds.from_array(array, scale=0.9) assert numpy.allclose(bounds.low, numpy.array([9, 9, 9])), (bounds.low, array.min(axis=0)) assert numpy.allclose(bounds.high, numpy.array([90, 90, 90])), ( bounds.high, array.max(axis=0), ) assert bounds.shape == (3, ) # high -, low -, scale > 1 array = numpy.array([[-100, -10, -10], [-100, -10, -10], [-10, -100, -10], [-100, -100, -100]]) bounds = Bounds.from_array(array, scale=1.1) assert numpy.allclose(bounds.high, numpy.array([-9, -9, -9])), ( bounds.high, array.max(axis=0), ) assert numpy.allclose(bounds.low, numpy.array([-110, -110, -110])), ( bounds.low, array.min(axis=0), ) assert bounds.shape == (3, ) # high -, low -, scale < 1 array = numpy.array([[-100, -10, -10], [-100, -10, -10], [-10, -100, -10], [-100, -100, -100]]) bounds = Bounds.from_array(array, scale=0.9) assert numpy.allclose(bounds.high, numpy.array([-11, -11, -11])), ( bounds.high, array.max(axis=0), ) assert numpy.allclose(bounds.low, numpy.array([-90, -90, -90])), ( bounds.low, array.min(axis=0), ) assert bounds.shape == (3, )
def test_safe_margin(self, bounds_fixture: Bounds): new_bounds = bounds_fixture.safe_margin() assert numpy.allclose(new_bounds.low, bounds_fixture.low) assert numpy.allclose(new_bounds.high, bounds_fixture.high) low = numpy.full_like(bounds_fixture.low, -10) new_bounds = bounds_fixture.safe_margin(low=low) assert numpy.allclose(new_bounds.high, bounds_fixture.high) assert numpy.allclose(new_bounds.low, low) new_bounds = bounds_fixture.safe_margin(low=low, scale=2) assert numpy.allclose(new_bounds.high, bounds_fixture.high * 2) assert numpy.allclose(new_bounds.low, low * 2)
def create_bounds(name): if name == "scalars": return lambda: Bounds(high=5, low=-5, shape=(3, )) elif name == "high_array": return lambda: Bounds(high=numpy.array([1, 2, 5]), low=-5) elif name == "low_array": return lambda: Bounds(low=numpy.array([-1, -5, -3]), high=5) elif name == "both_array": array = numpy.array([1, 2, 5]) return lambda: Bounds(high=array, low=-array) elif name == "high_list": return lambda: Bounds(low=numpy.array([-5, -2, -3]), high=[5, 5, 5])
def _update_lims(self, swarm, X: numpy.ndarray): backup_bounds = swarm.env.bounds if swarm is not None else Bounds.from_array( X) bounds = (swarm.critic.bounds if has_embedding(swarm) and self.use_embeddings else backup_bounds) self.xlim, self.ylim = bounds.safe_margin(low=self.low, high=self.high).to_lims()
def test_clip(self): tup = ((-1, 10), (-3, 4), (2, 5)) array = numpy.array([[-10, 0, 0], [11, 0, 0], [0, 11, 0], [11, 11, 11]]) bounds = Bounds.from_tuples(tup) clipped = bounds.clip(array) target = numpy.array([[-1, 0, 2], [10, 0, 2], [0, 4, 2], [10, 4, 5]]) assert numpy.allclose(clipped, target), (clipped, target)
def function_env(): bounds = Bounds(shape=(2, ), high=1, low=1, dtype=int) env = Function(function=lambda x: np.ones(N_WALKERS), bounds=bounds) params = { "actions": { "dtype": np.int64, "size": (2, ) }, "dt": { "dtype": np.float32 } } states = States(state_dict=params, batch_size=N_WALKERS) return env, states
def get_z_coords(self, swarm: Swarm, X: numpy.ndarray = None): if swarm is None: return numpy.ones(self.n_points**self.n_points) if swarm.critic.bounds is None: swarm.critic.bounds = Bounds.from_array(X, scale=1.1) # target grid to interpolate to xi = numpy.linspace(swarm.critic.bounds.low[0], swarm.critic.bounds.high[0], self.n_points) yi = numpy.linspace(swarm.critic.bounds.low[1], swarm.critic.bounds.high[1], self.n_points) xx, yy = numpy.meshgrid(xi, yi) grid = numpy.c_[xx.ravel(), yy.ravel()] if swarm.swarm.critic.warmed: memory_values = swarm.swarm.critic.predict(grid) memory_values = relativize(-memory_values) else: memory_values = numpy.arange(grid.shape[0]) return memory_values
def get_z_coords(self, swarm: Swarm, X: numpy.ndarray = None): if swarm is None: return numpy.ones(self.n_points**self.n_points) if swarm.critic.bounds is None: swarm.critic.bounds = Bounds.from_array(X, scale=1.1) # target grid to interpolate to xi = numpy.linspace(swarm.critic.bounds.low[0], swarm.critic.bounds.high[0], self.n_points) yi = numpy.linspace(swarm.critic.bounds.low[1], swarm.critic.bounds.high[1], self.n_points) xx, yy = numpy.meshgrid(xi, yi) grid = numpy.c_[xx.ravel(), yy.ravel()] if swarm.swarm.critic.warmed: memory_values = swarm.swarm.critic.model.transform(grid) memory_values = numpy.array([ swarm.swarm.critic.memory[ix[0], ix[1]].astype(numpy.float32) for ix in memory_values.astype(int) ]) else: memory_values = numpy.arange(grid.shape[0]) return memory_values
def test_from_array(self): array = numpy.array([[0, 0, 0], [11, 0, 0], [0, 11, 0], [11, 11, 11]]) bounds = Bounds.from_array(array) assert (bounds.low == numpy.array([0, 0, 0])).all() assert (bounds.high == numpy.array([11, 11, 11])).all() assert bounds.shape == (3, )
def test_from_tuples(self): tup = ((-1, 2), (-3, 4), (2, 5)) bounds = Bounds.from_tuples(tup) assert (bounds.low == numpy.array([-1, -3, 2])).all() assert (bounds.high == numpy.array([2, 4, 5])).all()
def _update_lims(self, swarm, X: numpy.ndarray): backup_bounds = swarm.env.bounds if swarm is not None else Bounds.from_array( X) bounds = (swarm.critic.bounds if has_embedding(swarm) and self.use_embeddings else backup_bounds) self.xlim, self.ylim = bounds.to_tuples()[:2]