File: dejong.py

package info (click to toggle)
python-bumps 1.0.0b2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,144 kB
  • sloc: python: 23,941; xml: 493; ansic: 373; makefile: 209; sh: 91; javascript: 90
file content (87 lines) | stat: -rwxr-xr-x 2,032 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python

"""
Rosenbrock's function, De Jong's step function, De Jong's quartic function,
and Shekel's function

References::
    [1] Storn, R. and Price, K. Differential Evolution - A Simple and Efficient
    Heuristic for Global Optimization over Continuous Spaces. Journal of Global
    Optimization 11: 341-359, 1997.

    [2] Storn, R. and Price, K.
    (Same title as above, but as a technical report.)
    http://www.icsi.berkeley.edu/~storn/deshort1.ps
"""

from functools import reduce

from numpy import sum as numpysum
from numpy import asarray
from math import floor
import random
from math import pow


def rosenbrock(x):
    """
    Rosenbrock function:

    A modified second De Jong function, Equation (18) of [2]

    minimum is f(x)=0.0 at xi=1.0
    """
    # ensure that there are 2 coefficients
    assert len(x) >= 2
    x = asarray(x)
    return numpysum(100.0 * (x[1:] - x[:-1] ** 2.0) ** 2.0 + (1 - x[:-1]) ** 2.0)


def step(x):
    """
    De Jong's step function:

    The third De Jong function, Equation (19) of [2]

    minimum is f(x)=0.0 at xi=-5-n where n=[0.0,0.12]
    """
    f = 30.0
    for c in x:
        if abs(c) <= 5.12:
            f += floor(c)
        elif c > 5.12:
            f += 30 * (c - 5.12)
        else:
            f += 30 * (5.12 - c)
    return f


def quartic(x):
    """
    De Jong's quartic function:
    The modified fourth De Jong function, Equation (20) of [2]

    minimum is f(x)=random, but statistically at xi=0
    """
    f = 0.0
    for j, c in enumerate(x):
        f += pow(c, 4) * (j + 1.0) + random.random()
    return f


def shekel(x):
    """
    Shekel: The modified fifth De Jong function, Equation (21) of [2]

    minimum is f(x)=0.0 at x(-32,-32)
    """

    A = [-32.0, -16.0, 0.0, 16.0, 32.0]
    a1 = A * 5
    a2 = reduce(lambda x1, x2: x1 + x2, [[c] * 5 for c in A])

    x1, x2 = x
    r = 0.0
    for i in range(25):
        r += 1.0 / (1.0 * i + pow(x1 - a1[i], 6) + pow(x2 - a2[i], 6) + 1e-15)
    return 1.0 / (0.002 + r)