• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mcuntz / pyjams / 5248889706

pending completion
5248889706

push

github

mcuntz
Merged jams as subdirectory into pyjams

18232 of 18232 new or added lines in 194 files covered. (100.0%)

4277 of 22698 relevant lines covered (18.84%)

1.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/pyjams/jams/functions/opti_test_functions.py
1
#!/usr/bin/env python
2
"""
×
3
Module provides common test functions for parameter estimation and
4
optimisation algorithms such as Rosenbrock and Griewank functions.
5

6
Current functions are:
7
    ackley                >=2 params:     Ackley function, global optimum: 0.0 at origin
8

9
    goldstein_price       2 params:       Goldstein-Price function, global optimum: 3.0 (0.0,-1.0)
10

11
    griewank              2 or 10 params: Griewank function, global optimum: 0 at origin
12

13
    rastrigin             2 params:       Rastrigin function, global optimum: -2 (0,0)
14

15
    rosenbrock            2 params:       Rosenbrock function, global optimum: 0 (1,1)
16

17
    six_hump_camelback    2 params:       Six-hump Camelback function
18
                                          True Optima: -1.031628453489877
19
                                          (-0.08983,0.7126) and (0.08983,-0.7126)
20

21
This module was written by Matthias Cuntz while at Department of
22
Computational Hydrosystems, Helmholtz Centre for Environmental
23
Research - UFZ, Leipzig, Germany, and continued while at Institut
24
National de Recherche pour l'Agriculture, l'Alimentation et
25
l'Environnement (INRAE), Nancy, France.
26

27
Copyright (c) 2013-2020 Matthias Cuntz - mc (at) macu (dot) de
28
Released under the MIT License; see LICENSE file for details.
29

30
* Written Oct 2013 by Matthias Cuntz (mc (at) macu (dot) de)
31
* Rearrange function library, Mar 2015, Matthias Cuntz
32
* Changed to Sphinx docstring and numpydoc, May 2020, Matthias Cuntz
33

34
.. moduleauthor:: Matthias Cuntz
35

36
The following functions are provided:
37

38
.. autosummary::
39
   ackley
40
   griewank
41
   goldstein_price
42
   rastrigin
43
   rosenbrock
44
   six_hump_camelback
45
"""
46
from __future__ import division, absolute_import, print_function
×
47
from warnings import warn, filterwarnings
×
48
filterwarnings("default", category=DeprecationWarning)
×
49
import numpy as np
×
50

51

52
__all__ = ['ackley', 'griewank', 'goldstein_price', 'rastrigin', 'rosenbrock', 'six_hump_camelback']
×
53

54

55
# -----------------------------------------------------------
56

57
def ackley(x):
×
58
    """
59
    Ackley function (>= 2-D).
60

61
    Global Optimum: 0.0, at origin.
62

63
    Parameters
64
    ----------
65
    x : array
66
        multi-dimensional x-values (len(x) >= 2)
67

68
    Returns
69
    -------
70
    float
71
       Value of Ackley function.
72
    """
73
    warn('The module functions is deprecated from JAMS. Use module pyjams.',
×
74
         category=DeprecationWarning)
75
    a = 20.0
×
76
    b = 0.2
×
77
    c = 2.0*np.pi
×
78

79
    n  = np.size(x)
×
80
    s1 = np.sum(x**2)
×
81
    s2 = np.sum(np.cos(c*x))
×
82
    f  = -a * np.exp(-b*np.sqrt(1.0/n*s1)) - np.exp(1.0/n*s2) + a + np.exp(1.0)
×
83

84
    return f
×
85

86
# -----------------------------------------------------------
87

88
def griewank(x):
×
89
    """
90
    Griewank function (2-D or 10-D).
91

92
    Global Optimum: 0.0, at origin.
93

94
    Parameters
95
    ----------
96
    x : array
97
        multi-dimensional x-values.
98

99
        `len(x)=2` or `len(x)=10`.
100

101
        `x[i]` bound to [-600,600] for all i.
102

103
    Returns
104
    -------
105
    float
106
       Value of Griewank function.
107
    """
108
    warn('The module functions is deprecated from JAMS. Use module pyjams.',
×
109
         category=DeprecationWarning)
110
    nopt = np.size(x)
×
111
    #if (nopt == 2) | (nopt == 10):
112
    xx = x
×
113
    if nopt==2:
×
114
        d = 200.0
×
115
    else:
116
        d = 4000.0
×
117

118
    u1 = 0.0
×
119
    u2 = 1.0
×
120
    for j in range(nopt):
×
121
        u1 = u1 + xx[j]**2/d
×
122
        u2 = u2 * np.cos(xx[j]/np.sqrt(float(j+1)))
×
123

124
    f = u1 - u2 + 1
×
125
    return f
×
126

127
# -----------------------------------------------------------
128

129
def goldstein_price(x):
×
130
    """
131
    Goldstein-Price function (2-D).
132

133
    Global Optimum: 3.0, at (0.0,-1.0).
134

135
    Parameters
136
    ----------
137
    x : array
138
        2 x-values. `len(x)=2`.
139

140
        `x[i]` bound to [-2,2] for i=1 and 2.
141

142
    Returns
143
    -------
144
    float
145
       Value of Goldstein-Price function.
146
    """
147
    warn('The module functions is deprecated from JAMS. Use module pyjams.',
×
148
         category=DeprecationWarning)
149
    x1 = x[0]
×
150
    x2 = x[1]
×
151
    u1 = (x1 + x2 + 1.0)**2
×
152
    u2 = 19. - 14.*x1 + 3.*x1**2 - 14.*x2 + 6.*x1*x2 +3.*x2**2
×
153
    u3 = (2.*x1 - 3.*x2)**2
×
154
    u4 = 18. - 32.*x1 + 12.*x1**2 + 48.*x2 -36.*x1*x2 + 27.*x2**2
×
155
    u5 = u1 * u2
×
156
    u6 = u3 * u4
×
157
    f = (1. + u5) * (30. + u6)
×
158
    return f
×
159

160
# -----------------------------------------------------------
161

162
def rastrigin(x):
×
163
    """
164
    Rastrigin function (2-D).
165

166
    Global Optimum: -2.0, at origin.
167

168
    Parameters
169
    ----------
170
    x : array
171
        2 x-values. `len(x)=2`.
172

173
        `x[i]` bound to [-1,1] for i=1 and 2.
174

175
    Returns
176
    -------
177
    float
178
       Value of Rastrigin function.
179
    """
180
    warn('The module functions is deprecated from JAMS. Use module pyjams.',
×
181
         category=DeprecationWarning)
182
    x1 = x[0]
×
183
    x2 = x[1]
×
184
    f = x1**2 + x2**2 - np.cos(18.0*x1) - np.cos(18.0*x2)
×
185
    return f
×
186

187
# -----------------------------------------------------------
188

189
def rosenbrock(x):
×
190
    """
191
    Rosenbrock function (2-D).
192

193
    Global Optimum: 0.0, at (1.0,1.0).
194

195
    Parameters
196
    ----------
197
    x : array
198
        2 x-values. `len(x)=2`.
199

200
        `x[1]` bound to [-5,5].
201

202
        `x[2]` bound to [-2,8].
203

204
    Returns
205
    -------
206
    float
207
       Value of Rosenbrock function.
208
    """
209
    warn('The module functions is deprecated from JAMS. Use module pyjams.',
×
210
         category=DeprecationWarning)
211

212
    x1 = x[0]
×
213
    x2 = x[1]
×
214
    a = 100.0
×
215
    f = a * (x2 - x1**2)**2 + (1 - x1)**2
×
216
    return f
×
217

218
# -----------------------------------------------------------
219

220
def six_hump_camelback(x):
×
221
    """
222
    Six-hump Camelback function (2-D).
223

224
    Global Optima: -1.031628453489877, at (-0.08983,0.7126) and (0.08983,-0.7126).
225

226
    Parameters
227
    ----------
228
    x : array
229
        2 x-values. `len(x)=2`.
230

231
        `x[i]` bound to [-5,5] for i=1 and 2.
232

233
    Returns
234
    -------
235
    float
236
       Value of Six-hump Camelback function.
237
    """
238
    warn('The module functions is deprecated from JAMS. Use module pyjams.',
×
239
         category=DeprecationWarning)
240
    x1 = x[0]
×
241
    x2 = x[1]
×
242
    f = (4 - 2.1*x1**2 + x1**4/3)*x1**2 + x1*x2 + (-4 + 4*x2**2)*x2**2
×
243
    return f
×
244

245
# -----------------------------------------------------------
246

247
if __name__ == '__main__':
248
    import doctest
249
    doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc