def testFastGallery(self): tf = Hartman3() kernel = tf.createKernel(GaussianKernel_ard) X = lhcSample(tf.bounds, 10, seed=23) Y = [tf.f(x) for x in X] prefs = query2prefs(X, tf.f) GP = PrefGaussianProcess(kernel) GP.addPreferences(prefs) gallery = fastUCBGallery(GP, tf.bounds, 4) print('gallery returned:') for x in gallery: print('\t', x) GP.addPreferences(query2prefs(gallery, tf.f)) # make sure we don't return anything out of bounds bounds = copy(tf.bounds) bounds[0] = [0., 0.] gallery = fastUCBGallery(GP, bounds, 4) print('gallery returned:') for x in gallery: print('\t', x) for v, b in zip(x, bounds): self.failUnless(v >= b[0] and v <= b[1])
def testPriorAndPrefs(self): S5 = Shekel5() pX = lhcSample(S5.bounds, 100, seed=13) pY = [S5.f(x) for x in pX] prior = RBFNMeanPrior() prior.train(pX, pY, S5.bounds, k=10) hv = .1 hyper = [hv, hv, hv, hv] gkernel = GaussianKernel_ard(hyper) GP = PrefGaussianProcess(gkernel, prior=prior) X = [array([i + .5] * 4) for i in range(5)] valX = [x.copy() for x in X] prefs = [] for i in range(len(X)): for j in range(i): if S5.f(X[i]) > S5.f(X[j]): prefs.append((X[i], X[j], 0)) else: prefs.append((X[j], X[i], 0)) GP.addPreferences(prefs) opt, optx = maximizeEI(GP, S5.bounds)
def demoPrefGallery(): """ A simpler demo, showing how to use a preference gallery. This demo is not interactive -- it uses the 6D Hartman test function to generate the preferences. """ N = 3 # gallery size # use the Hartman6 test function, which has a kernel and bounds predefined tf = Hartman6() bounds = tf.bounds kernel = tf.createKernel(GaussianKernel_ard) # set up a Preference Gaussian Process, in which the observations are # preferences, rather than scalars GP = PrefGaussianProcess(kernel) # initial preferences -- since we have no informative prior on the space, # the gallery will be a set of points that maximize variance gallery = fastUCBGallery(GP, bounds, N) # this is a utility function for automtically testing the preferences -- # given a test functions and some sample points, it will return a list of # preferences prefs = query2prefs(gallery, tf.f) # preferences have the form [r, c, degree], where r is preferred to c. # degree is degree of preference. Just leave degree at 0 for now. for r, c, _ in prefs: print '%s preferred to %s' % (r, c) # add preferences to the model GP.addPreferences(prefs) # get another gallery, but with the first three dimensions fixed to .5 nbounds = deepcopy(bounds) nbounds[:3] = [[.5, .5]] * 3 gallery = fastUCBGallery(GP, nbounds, N) prefs = query2prefs(gallery, tf.f) for r, c, _ in prefs: print '%s preferred to %s' % (r, c) # get another gallery, but with the *last* three dimensions fixed to .5 nbounds = deepcopy(bounds) nbounds[3:] = [[.5, .5]] * 3 gallery = fastUCBGallery(GP, nbounds, N) prefs = query2prefs(gallery, tf.f) for r, c, _ in prefs: print '%s preferred to %s' % (r, c) # preferences don't have to come from the gallery r = array([0, 0, .5, 0, 1, .25]) c = array([1, 1, .75, 1, 0, .5]) pref = (r, c, 0) GP.addPreferences([pref])