예제 #1
0
    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, )
예제 #2
0
 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, )
예제 #3
0
 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()
예제 #4
0
 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
예제 #5
0
 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
예제 #6
0
 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, )
예제 #7
0
 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]