File: example.py

package info (click to toggle)
python-scipy 0.14.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 52,228 kB
  • ctags: 63,719
  • sloc: python: 112,726; fortran: 88,685; cpp: 86,979; ansic: 85,860; makefile: 530; sh: 236
file content (28 lines) | stat: -rw-r--r-- 709 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
#!/usr/bin/env python
# Python TNC example
# @(#) $Jeannot: example.py,v 1.4 2004/04/02 18:51:04 js Exp $

from __future__ import division, print_function, absolute_import

import tnc

# A function to minimize
# Must return a tuple with the function value and the gradient (as a list)
# or None to abort the minimization


def function(x):
    f = pow(x[0],2.0)+pow(abs(x[1]),3.0)
    g = [0,0]
    g[0] = 2.0*x[0]
    g[1] = 3.0*pow(abs(x[1]),2.0)
    if x[1] < 0:
        g[1] = -g[1]
    return f, g

# Optimizer call
rc, nf, x = tnc.minimize(function, [-7, 3], [-10, 1], [10, 10])

print("After", nf, "function evaluations, TNC returned:", tnc.RCSTRINGS[rc])
print("x =", x)
print("exact value = [0, 1]")