File: corana.py

package info (click to toggle)
python-bumps 1.0.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,200 kB
  • sloc: python: 24,517; xml: 493; ansic: 373; makefile: 211; javascript: 99; sh: 94
file content (70 lines) | stat: -rw-r--r-- 1,707 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
"""
Ccorana'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 math import pow

from numpy import sign, floor, inf


def corana(coeffs):
    """
    evaluates the Corana function for a list of coeffs

    minimum is f(x)=0.0 at xi=0.0
    """
    d = [1.0, 1000.0, 10.0, 100.0]
    x = coeffs
    r = 0
    for xj, dj in zip(x, d):
        zj = floor(abs(xj / 0.2) + 0.49999) * sign(xj) * 0.2
        if abs(xj - zj) < 0.05:
            r += 0.15 * pow(zj - 0.05 * sign(zj), 2) * dj
        else:
            r += dj * xj**2
        return r


def corana1d(x):
    """Corana in 1D; coeffs = (x,0,0,0)"""
    return corana([x[0], 0, 0, 0])


def corana2d(x):
    """Corana in 2D; coeffs = (x,0,y,0)"""
    return corana([x[0], 0, x[1], 0])


def corana3d(x):
    """Corana in 3D; coeffs = (x,0,y,z)"""
    return corana([x[0], 0, x[1], x[2]])


Po = [1, 1, 1, 1]
Plo = [-inf, -inf, -inf, -inf]
Phi = [inf, inf, inf, inf]


def s(V, *args):
    retval = []
    for i in args:
        retval.append(V[i])
    return retval


from .model import Function

corana1d = Function(f=corana1d, limits=(s(Plo, 0), s(Phi, 0)), start=s(Po, 0))
corana2d = Function(f=corana2d, limits=(s(Plo, 0, 2), s(Phi, 0, 2)), start=s(Po, 0, 2))
corana3d = Function(f=corana3d, limits=(s(Plo, 0, 2, 3), s(Phi, 0, 2, 3)), start=s(Po, 0, 2, 3))
corana4d = Function(f=corana, limits=(Plo, Phi), start=Po)
# End of file