File: griewangk.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 (37 lines) | stat: -rw-r--r-- 897 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
#!/usr/bin/env python

"""
Griewangk'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 math import cos, sqrt


def griewangk(coeffs):
    """
    Griewangk function: a multi-minima function, Equation (23) of [2]

    minimum is f(x)=0.0 at xi=0.0
    """

    # ensure that there are 10 coefficients
    x = [0] * 10
    x[: len(coeffs)] = coeffs

    term1 = sum([xi * xi for xi in x]) / 4000
    term2 = prod([cos(xi / sqrt(i + 1.0)) for i, xi in enumerate(x)])
    return term1 - term2 + 1


prod = lambda x: reduce(lambda a, b: a * b, x, 1.0)