Fork me on GitHub Top

skopt.benchmarks module

A collection of benchmark problems.

# -*- coding: utf-8 -*-
"""A collection of benchmark problems."""

import numpy as np


def bench1(x):
    """A benchmark function for test purposes.

        f(x) = x ** 2

    It has a single minima with f(x*) = 0 at x* = 0.
    """
    return x[0] ** 2


def bench2(x):
    """A benchmark function for test purposes.

        f(x) = x ** 2           if x < 0
               (x-5) ** 2 - 5   otherwise.

    It has a global minima with f(x*) = -5 at x* = 5.
    """
    if x[0] < 0:
        return x[0] ** 2
    else:
        return (x[0] - 5) ** 2 - 5


def bench3(x):
    """A benchmark function for test purposes.

        f(x) = sin(5*x) * (1 - tanh(x ** 2))

    It has a global minima with f(x*) ~= -0.9 at x* ~= -0.3.
    """
    return np.sin(5 * x[0]) * (1 - np.tanh(x[0] ** 2))


def bench4(x):
    """A benchmark function for test purposes.

        f(x) = float(x) ** 2

    where x is a string. It has a single minima with f(x*) = 0 at x* = "0".
    This benchmark is used for checking support of categorical variables.
    """
    return float(x[0]) ** 2


def bench5(x):
    """A benchmark function for test purposes.

        f(x) = float(x[0]) ** 2 + x[1] ** 2

    where x is a string. It has a single minima with f(x) = 0 at x[0] = "0"
    and x[1] = "0"
    This benchmark is used for checking support of mixed spaces.
    """
    return float(x[0]) ** 2 + x[1] ** 2


def branin(x, a=1, b=5.1 / (4 * np.pi**2), c=5. / np.pi,
           r=6, s=10, t=1. / (8 * np.pi)):
    """Branin-Hoo function is defined on the square x1 ∈ [-5, 10], x2 ∈ [0, 15].

    It has three minima with f(x*) = 0.397887 at x* = (-pi, 12.275),
    (+pi, 2.275), and (9.42478, 2.475).

    More details: <http://www.sfu.ca/~ssurjano/branin.html>
    """
    return (a * (x[1] - b * x[0] ** 2 + c * x[0] - r) ** 2 +
            s * (1 - t) * np.cos(x[0]) + s)


def hart6(x,
          alpha=np.asarray([1.0, 1.2, 3.0, 3.2]),
          P=10**-4 * np.asarray([[1312, 1696, 5569, 124, 8283, 5886],
                                 [2329, 4135, 8307, 3736, 1004, 9991],
                                 [2348, 1451, 3522, 2883, 3047, 6650],
                                 [4047, 8828, 8732, 5743, 1091, 381]]),
          A=np.asarray([[10, 3, 17, 3.50, 1.7, 8],
                        [0.05, 10, 17, 0.1, 8, 14],
                        [3, 3.5, 1.7, 10, 17, 8],
                        [17, 8, 0.05, 10, 0.1, 14]])):
    """The six dimensional Hartmann function is defined on the unit hypercube.

    It has six local minima and one global minimum f(x*) = -3.32237 at
    x* = (0.20169, 0.15001, 0.476874, 0.275332, 0.311652, 0.6573).

    More details: <http://www.sfu.ca/~ssurjano/hart6.html>
    """
    return -np.sum(alpha * np.exp(-np.sum(A * (np.array(x) - P)**2, axis=1)))

Functions

def bench1(

x)

A benchmark function for test purposes.

f(x) = x ** 2

It has a single minima with f(x) = 0 at x = 0.

def bench1(x):
    """A benchmark function for test purposes.

        f(x) = x ** 2

    It has a single minima with f(x*) = 0 at x* = 0.
    """
    return x[0] ** 2

def bench2(

x)

A benchmark function for test purposes.

f(x) = x ** 2           if x < 0
       (x-5) ** 2 - 5   otherwise.

It has a global minima with f(x) = -5 at x = 5.

def bench2(x):
    """A benchmark function for test purposes.

        f(x) = x ** 2           if x < 0
               (x-5) ** 2 - 5   otherwise.

    It has a global minima with f(x*) = -5 at x* = 5.
    """
    if x[0] < 0:
        return x[0] ** 2
    else:
        return (x[0] - 5) ** 2 - 5

def bench3(

x)

A benchmark function for test purposes.

f(x) = sin(5*x) * (1 - tanh(x ** 2))

It has a global minima with f(x) ~= -0.9 at x ~= -0.3.

def bench3(x):
    """A benchmark function for test purposes.

        f(x) = sin(5*x) * (1 - tanh(x ** 2))

    It has a global minima with f(x*) ~= -0.9 at x* ~= -0.3.
    """
    return np.sin(5 * x[0]) * (1 - np.tanh(x[0] ** 2))

def bench4(

x)

A benchmark function for test purposes.

f(x) = float(x) ** 2

where x is a string. It has a single minima with f(x) = 0 at x = "0". This benchmark is used for checking support of categorical variables.

def bench4(x):
    """A benchmark function for test purposes.

        f(x) = float(x) ** 2

    where x is a string. It has a single minima with f(x*) = 0 at x* = "0".
    This benchmark is used for checking support of categorical variables.
    """
    return float(x[0]) ** 2

def bench5(

x)

A benchmark function for test purposes.

f(x) = float(x[0]) ** 2 + x[1] ** 2

where x is a string. It has a single minima with f(x) = 0 at x[0] = "0" and x[1] = "0" This benchmark is used for checking support of mixed spaces.

def bench5(x):
    """A benchmark function for test purposes.

        f(x) = float(x[0]) ** 2 + x[1] ** 2

    where x is a string. It has a single minima with f(x) = 0 at x[0] = "0"
    and x[1] = "0"
    This benchmark is used for checking support of mixed spaces.
    """
    return float(x[0]) ** 2 + x[1] ** 2

def branin(

x, a=1, b=0.12918450914398066, c=1.5915494309189535, r=6, s=10, t=0.039788735772973836)

Branin-Hoo function is defined on the square x1 ∈ [-5, 10], x2 ∈ [0, 15].

It has three minima with f(x) = 0.397887 at x = (-pi, 12.275), (+pi, 2.275), and (9.42478, 2.475).

More details: http://www.sfu.ca/~ssurjano/branin.html

def branin(x, a=1, b=5.1 / (4 * np.pi**2), c=5. / np.pi,
           r=6, s=10, t=1. / (8 * np.pi)):
    """Branin-Hoo function is defined on the square x1 ∈ [-5, 10], x2 ∈ [0, 15].

    It has three minima with f(x*) = 0.397887 at x* = (-pi, 12.275),
    (+pi, 2.275), and (9.42478, 2.475).

    More details: <http://www.sfu.ca/~ssurjano/branin.html>
    """
    return (a * (x[1] - b * x[0] ** 2 + c * x[0] - r) ** 2 +
            s * (1 - t) * np.cos(x[0]) + s)

def hart6(

x, alpha=array([ 1. , 1.2, 3. , 3.2]), P=array([[ 0.1312, 0.1696, 0.5569, 0.0124, 0.8283, 0.5886], [ 0.2329, 0.4135, 0.8307, 0.3736, 0.1004, 0.9991], [ 0.2348, 0.1451, 0.3522, 0.2883, 0.3047, 0.665 ], [ 0.4047, 0.8828, 0.8732, 0.5743, 0.1091, 0.0381]]), A=array([[ 10. , 3. , 17. , 3.5 , 1.7 , 8. ], [ 0.05, 10. , 17. , 0.1 , 8. , 14. ], [ 3. , 3.5 , 1.7 , 10. , 17. , 8. ], [ 17. , 8. , 0.05, 10. , 0.1 , 14. ]]))

The six dimensional Hartmann function is defined on the unit hypercube.

It has six local minima and one global minimum f(x) = -3.32237 at x = (0.20169, 0.15001, 0.476874, 0.275332, 0.311652, 0.6573).

More details: http://www.sfu.ca/~ssurjano/hart6.html

def hart6(x,
          alpha=np.asarray([1.0, 1.2, 3.0, 3.2]),
          P=10**-4 * np.asarray([[1312, 1696, 5569, 124, 8283, 5886],
                                 [2329, 4135, 8307, 3736, 1004, 9991],
                                 [2348, 1451, 3522, 2883, 3047, 6650],
                                 [4047, 8828, 8732, 5743, 1091, 381]]),
          A=np.asarray([[10, 3, 17, 3.50, 1.7, 8],
                        [0.05, 10, 17, 0.1, 8, 14],
                        [3, 3.5, 1.7, 10, 17, 8],
                        [17, 8, 0.05, 10, 0.1, 14]])):
    """The six dimensional Hartmann function is defined on the unit hypercube.

    It has six local minima and one global minimum f(x*) = -3.32237 at
    x* = (0.20169, 0.15001, 0.476874, 0.275332, 0.311652, 0.6573).

    More details: <http://www.sfu.ca/~ssurjano/hart6.html>
    """
    return -np.sum(alpha * np.exp(-np.sum(A * (np.array(x) - P)**2, axis=1)))