Wednesday, 10 February 2021

Can one constrain the outcome of skopt.Lhs.generate?

Let's say I have an array like this:

from skopt.space import Space
from skopt.sampler import Lhs
import numpy as np

np.random.seed(42)

rows = 5
cols = 3

dummy = np.zeros((rows, cols))

array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])

and I now would like to use skopt.Lhs.generate to fill certain positions of this array with a 1 whereby I would like to exclude certain positions stored in ignore:

ignore = np.array([
    [3, 1],
    [4, 1]
])

How would I do this best?

I can do

space = Space([(0, rows - 1), (0, cols - 1)])

lhs = Lhs(criterion="maximin", iterations=1000)
lh = np.array(lhs.generate(space.dimensions, 3))

dummy[lh[:, 0], lh[:, 1]] = 1

which gives

array([[0., 0., 1.],
       [1., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.],
       [0., 1., 0.]])

but as one can see the position 4, 1 is occupied but it shouldn't.

One way could be to put the lhs.generate call inside a while loop and then always checks whether any element is in ignore but I am wondering whether there is a more straightforward solution to this.



from Can one constrain the outcome of skopt.Lhs.generate?

No comments:

Post a Comment