Пример #1
0
 def test_select_point_to_explore_in_two_dimensions(self):
     '''
     This test case just runs code on a multi-dimensional input dataset.
     It doesn't test any conditions, but could presumably do so in the future.
     '''
     acquire(
         x=a([
             [-0.5, -0.5],
             [-0.5, 0.5],
         ]),
         # Fake fmap and Cmap values from another set of x's
         fmap=a([
             [0.03254087],
             [-0.03254087],
         ]),
         Cmap=a([
             [0.07894662, -0.07894662],
             [-0.07894662, 0.07894662],
         ]),
         bounds=a([
             [-1.0, 1.0],
             [-1.0, 1.0],
         ]),
         kernelfunc=default_kernel
     )
Пример #2
0
 def test_select_point_to_exploit(self):
     # We attempt to force exploitation by covering most of the input
     # space and expecting that the maximization algorithm will choose
     # the point between the highest outputs, given a symmetric output function.
     next_point = acquire(
         x=a([
             [-0.75],
             [-0.25],
             [0.25],
             [0.75],
         ]),
         # I got these fmap and Cmap values from running our optimizer
         # on the input data with comparisons [1, 0], [1, 3], [2, 0], [2, 3].
         fmap=a([
             [0.08950024],
             [0.21423927],
             [0.21423927],
             [0.08950024],
         ]),
         Cmap=a([
             [0.15672336, -0.07836168, -0.07836168, 0.0],
             [-0.07836168, 0.15672336, 0.0, -0.07836168],
             [-0.07836168, 0.0, 0.15672336, -0.07836168],
             [0.0, -0.07836168, -0.07836168, 0.15672336],
         ]),
         bounds=a([
             [-1.0, 1.0],
         ]),
         kernelfunc=default_kernel
     )
     self.assertTrue(next_point[0] > -.25)
     self.assertTrue(next_point[0] < .25)
Пример #3
0
 def test_select_point_to_exploit(self):
     # We attempt to force exploitation by covering most of the input
     # space and expecting that the maximization algorithm will choose
     # the point between the highest outputs, given a symmetric output function.
     next_point = acquire(
         x=a([
             [-0.75],
             [-0.25],
             [0.25],
             [0.75],
         ]),
         # I got these fmap and Cmap values from running our optimizer
         # on the input data with comparisons [1, 0], [1, 3], [2, 0], [2, 3].
         fmap=a([
             [0.08950024],
             [0.21423927],
             [0.21423927],
             [0.08950024],
         ]),
         Cmap=a([
             [0.15672336, -0.07836168, -0.07836168, 0.0],
             [-0.07836168, 0.15672336, 0.0, -0.07836168],
             [-0.07836168, 0.0, 0.15672336, -0.07836168],
             [0.0, -0.07836168, -0.07836168, 0.15672336],
         ]),
         bounds=a([
             [-1.0, 1.0],
         ]),
         kernelfunc=default_kernel)
     self.assertTrue(next_point[0] > -.25)
     self.assertTrue(next_point[0] < .25)
Пример #4
0
 def test_select_point_to_explore_in_two_dimensions(self):
     '''
     This test case just runs code on a multi-dimensional input dataset.
     It doesn't test any conditions, but could presumably do so in the future.
     '''
     acquire(
         x=a([
             [-0.5, -0.5],
             [-0.5, 0.5],
         ]),
         # Fake fmap and Cmap values from another set of x's
         fmap=a([
             [0.03254087],
             [-0.03254087],
         ]),
         Cmap=a([
             [0.07894662, -0.07894662],
             [-0.07894662, 0.07894662],
         ]),
         bounds=a([
             [-1.0, 1.0],
             [-1.0, 1.0],
         ]),
         kernelfunc=default_kernel)
Пример #5
0
 def test_select_point_to_explore(self):
     next_point = acquire(
         x=a([
             [-0.75],
             [-0.4],
         ]),
         # I got these fmap and Cmap values from running our optimizer
         # on the input data with comparisons [0, 1]
         fmap=a([
             [0.03254087],
             [-0.03254087],
         ]),
         Cmap=a([
             [0.07894662, -0.07894662],
             [-0.07894662, 0.07894662],
         ]),
         bounds=a([
             [-1.0, 1.0],
         ]),
         kernelfunc=default_kernel)
     self.assertTrue(next_point[0] > -.4)
Пример #6
0
 def test_select_point_to_explore(self):
     next_point = acquire(
         x=a([
             [-0.75],
             [-0.4],
         ]),
         # I got these fmap and Cmap values from running our optimizer
         # on the input data with comparisons [0, 1]
         fmap=a([
             [0.03254087],
             [-0.03254087],
         ]),
         Cmap=a([
             [0.07894662, -0.07894662],
             [-0.07894662, 0.07894662],
         ]),
         bounds=a([
             [-1.0, 1.0],
         ]),
         kernelfunc=default_kernel
     )
     self.assertTrue(next_point[0] > -.4)
Пример #7
0
 def test_select_point_to_explore_within_bounds_function(self):
     next_point = acquire(
         x=a([
             [-0.75],
             [-0.4],
         ]),
         # See above for the source of fmap and Cmap values
         fmap=a([
             [0.03254087],
             [-0.03254087],
         ]),
         Cmap=a([
             [0.07894662, -0.07894662],
             [-0.07894662, 0.07894662],
         ]),
         bounds=a([
             [-2.0, 2.0],
         ]),
         kernelfunc=default_kernel,
         extrabounds=lambda p: p[0] > -1.0 and p[0] < 1.0,
     )
     self.assertTrue(next_point[0] > -.4)
     self.assertTrue(next_point[0] < 1.0)
Пример #8
0
 def test_select_point_to_explore_within_bounds_function(self):
     next_point = acquire(
         x=a([
             [-0.75],
             [-0.4],
         ]),
         # See above for the source of fmap and Cmap values
         fmap=a([
             [0.03254087],
             [-0.03254087],
         ]),
         Cmap=a([
             [0.07894662, -0.07894662],
             [-0.07894662, 0.07894662],
         ]),
         bounds=a([
             [-2.0, 2.0],
         ]),
         kernelfunc=default_kernel,
         extrabounds=lambda p: p[0] > -1.0 and p[0] < 1.0,
     )
     self.assertTrue(next_point[0] > -.4)
     self.assertTrue(next_point[0] < 1.0)
Пример #9
0
def bayesopt():

    # The caller is responsible with updating comparisons.
    # They should be able to just upload the x and f as they were provided by the last
    # call to this URL.
    f = np.array(loads(request.args.get('f', '[]')), dtype=np.float)
    x = np.array(loads(request.args.get('x', '[]')), dtype=np.float)
    comparisons = np.array(loads(request.args.get('comparisons', '[]')), dtype=np.int)

    SIGMA = 10.0
    BOUNDS = np.array([
        [0.0, 4.0],
        [0.0, 4.0],
        [0.0, 4.0],
    ])
    KERNELFUNC = partial(default_kernel, a=-.25)

    # Create extra bounds to rule out configurations that burned
    within_bounds = {}
    BURNING_CONFIGURATIONS = [
        (4, 0, 0),
        (4, 1, 0),
        (4, 0, 1),
        (4, 1, 1),
        (4, 1, 1),
        (3, 0, 2),
        (4, 0, 2),
        (4, 1, 2),
        (3, 0, 3),
        (4, 0, 3),
        (4, 1, 3),
        (3, 0, 4),
        (4, 0, 4),
        (4, 1, 4),
    ]
    keys = [_ for _ in itertools.product(range(5), range(5), range(5))]
    for k in keys:
        within_bounds[k] = (k not in BURNING_CONFIGURATIONS)

    def extrabounds(p):
        rounded = np.round(p).astype('int')
        rounded_key = tuple(rounded)
        return within_bounds[rounded_key]

    # If no parameters are given, initialize the data
    if f.shape == (0,):
        x = np.array([
            [1.0, 1.0, 1.0],
        ])
        f = np.array([
            [0.0],
        ])
        comparisons = np.array([])
        best_f_i = 0
        xbest = x[0]
        xnew = np.array([3.0, 3.0, 3.0])
    # Predict the next point for comparison
    else:
        prof = cProfile.Profile()
        f, Cmap = prof.runcall(
            newton_rhapson, x, f, comparisons, KERNELFUNC,
            compute_H, compute_g, SIGMA, maxiter=10)
        prof.dump_stats('code.profile')
        xnew = acquire(x, f, Cmap, BOUNDS, KERNELFUNC, extrabounds)
        best_f_i = np.argmax(f)
        xbest = x[best_f_i]

    # Add newest points to x and f
    x = np.array(x.tolist() + [xnew])
    f = np.array(f.tolist() + [[0.0]])

    return jsonify(**{
        'x': x.tolist(),
        'f': f.tolist(),
        'xbest': {
            'value': xbest.tolist(),
            'img': get_cut_image_name(*(xbest.tolist())),
            'index': best_f_i,
        },
        'xnew': {
            'value': xnew.tolist(),
            'img': get_cut_image_name(*(xnew.tolist())),
            'index': len(f) - 1,
        },
        'comparisons': comparisons.tolist()
    })
Пример #10
0
def bayesopt():

    # The caller is responsible with updating comparisons.
    # They should be able to just upload the x and f as they were provided by the last
    # call to this URL.
    f = np.array(loads(request.args.get('f', '[]')), dtype=np.float)
    x = np.array(loads(request.args.get('x', '[]')), dtype=np.float)
    comparisons = np.array(loads(request.args.get('comparisons', '[]')),
                           dtype=np.int)

    SIGMA = 10.0
    BOUNDS = np.array([
        [0.0, 4.0],
        [0.0, 4.0],
        [0.0, 4.0],
    ])
    KERNELFUNC = partial(default_kernel, a=-.25)

    # Create extra bounds to rule out configurations that burned
    within_bounds = {}
    BURNING_CONFIGURATIONS = [
        (4, 0, 0),
        (4, 1, 0),
        (4, 0, 1),
        (4, 1, 1),
        (4, 1, 1),
        (3, 0, 2),
        (4, 0, 2),
        (4, 1, 2),
        (3, 0, 3),
        (4, 0, 3),
        (4, 1, 3),
        (3, 0, 4),
        (4, 0, 4),
        (4, 1, 4),
    ]
    keys = [_ for _ in itertools.product(range(5), range(5), range(5))]
    for k in keys:
        within_bounds[k] = (k not in BURNING_CONFIGURATIONS)

    def extrabounds(p):
        rounded = np.round(p).astype('int')
        rounded_key = tuple(rounded)
        return within_bounds[rounded_key]

    # If no parameters are given, initialize the data
    if f.shape == (0, ):
        x = np.array([
            [1.0, 1.0, 1.0],
        ])
        f = np.array([
            [0.0],
        ])
        comparisons = np.array([])
        best_f_i = 0
        xbest = x[0]
        xnew = np.array([3.0, 3.0, 3.0])
    # Predict the next point for comparison
    else:
        prof = cProfile.Profile()
        f, Cmap = prof.runcall(newton_rhapson,
                               x,
                               f,
                               comparisons,
                               KERNELFUNC,
                               compute_H,
                               compute_g,
                               SIGMA,
                               maxiter=10)
        prof.dump_stats('code.profile')
        xnew = acquire(x, f, Cmap, BOUNDS, KERNELFUNC, extrabounds)
        best_f_i = np.argmax(f)
        xbest = x[best_f_i]

    # Add newest points to x and f
    x = np.array(x.tolist() + [xnew])
    f = np.array(f.tolist() + [[0.0]])

    return jsonify(
        **{
            'x': x.tolist(),
            'f': f.tolist(),
            'xbest': {
                'value': xbest.tolist(),
                'img': get_cut_image_name(*(xbest.tolist())),
                'index': best_f_i,
            },
            'xnew': {
                'value': xnew.tolist(),
                'img': get_cut_image_name(*(xnew.tolist())),
                'index': len(f) - 1,
            },
            'comparisons': comparisons.tolist()
        })