~ubuntu-branches/ubuntu/karmic/python-scipy/karmic

« back to all changes in this revision

Viewing changes to Lib/optimize/cobyla.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T. Chen (new)
  • Date: 2005-03-16 02:15:29 UTC
  • Revision ID: james.westby@ubuntu.com-20050316021529-xrjlowsejs0cijig
Tags: upstream-0.3.2
ImportĀ upstreamĀ versionĀ 0.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Interface to Constrained Optimization By Linear Approximation
 
2
 
 
3
from __future__ import nested_scopes
 
4
import _cobyla
 
5
 
 
6
def fmin_cobyla(func, x0, cons, args=(), consargs=None, rhobeg=1.0, rhoend=1e-4,
 
7
                iprint=1, maxfun=1000):
 
8
    """
 
9
    Minimize a function using the Contrained Optimization BY Linear Approximation
 
10
    (COBYLA) method
 
11
 
 
12
    Arguments:
 
13
 
 
14
    func     -- function to minimize. Called as func(x, *args)
 
15
 
 
16
    x0       -- initial guess to minimum
 
17
 
 
18
    cons     -- a list of functions that all must be >=0 (a single function
 
19
                if only 1 constraint)
 
20
 
 
21
    args     -- extra arguments to pass to function
 
22
 
 
23
    consargs -- extra arguments to pass to constraints (default of None means
 
24
                use same extra arguments as those passed to func).  Use () for no
 
25
                extra arguments.
 
26
 
 
27
    rhobeg --  reasonable initial changes to the variables
 
28
 
 
29
    rhoend --  final accuracy in the optimization (not precisely guaranteed)
 
30
 
 
31
    iprint  -- controls the frequency of output: 0 (no output),1,2,3
 
32
 
 
33
    maxfun  -- maximum number of function evaluations.
 
34
 
 
35
 
 
36
    Returns:
 
37
 
 
38
    x -- the minimum
 
39
    
 
40
    """
 
41
    err = "cons must be a list of callable functions or a single"\
 
42
              " callable function."
 
43
    n = len(x0)
 
44
    if isinstance(cons, list):
 
45
        m = len(cons)
 
46
        for thisfunc in cons:
 
47
            if not callable(thisfunc):
 
48
                raise TypeError, err
 
49
    elif callable(cons):
 
50
        m = 1
 
51
        cons = [cons]
 
52
    else:
 
53
        raise TypeError, "cons must be a list of callable functions or a single"\
 
54
              " callable function."
 
55
 
 
56
    if consargs is None:
 
57
        consargs = args
 
58
        
 
59
    def calcfc(x, con):
 
60
        f = func(x, *args)
 
61
        k = 0
 
62
        for constraints in cons:
 
63
            con[k] = constraints(x,*consargs)
 
64
            k += 1
 
65
        return f
 
66
 
 
67
    xopt = _cobyla.minimize(calcfc, m=m, x=x0,rhobeg=rhobeg,rhoend=rhoend,iprint=iprint,
 
68
                            maxfun=maxfun)
 
69
    
 
70
    return xopt