Changeset 610 for branches


Ignore:
Timestamp:
12/13/12 12:43:16 (3 years ago)
Author:
mmckerns
Message:

added isbounded; minor documentation improvements; extended as_constraints test

Location:
branches/decorate
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/decorate/constraints.py

    r609 r610  
     1#!/usr/bin/env python 
     2 
    13# XXX: provide the below from measures, which from bounded?, and ...generic? 
    24# XXX: with_*** is a 'wrap' constraint, what about 'nested' constraints? 
  • branches/decorate/penalty.py

    r609 r610  
     1#!/usr/bin/env python 
     2 
     3""" 
     4References: 
     5   [1] http://en.wikipedia.org/wiki/Penalty_method 
     6   [2] Applied Optimization with MATLAB Programming, by Venkataraman. 
     7       Wiley, 2nd edition, 2009. 
     8   [3] http://www.srl.gatech.edu/education/ME6103/Penalty-Barrier.ppt 
     9   [4] "An Augmented Lagrange Multiplier Based Method for Mixed Integer 
     10       Discrete Continuous Optimization and Its Applications to Mechanical 
     11       Design", by Kannan and Kramer. 1994. 
     12""" 
    113 
    214def with_penalty(ptype, *args, **kwds): 
  • branches/decorate/test_penalty.py

    r609 r610  
    1919    return abs(sum(x) - 5.0) 
    2020 
    21   from mystic.solvers import fmin_powell 
     21  from mystic.solvers import fmin 
    2222  from numpy import array 
    2323  x = array([1,2,3,4,5]) 
    24   y = fmin_powell(cost, x, penalty=penalty, disp=False) 
     24  y = fmin(cost, x, penalty=penalty, disp=False) 
    2525 
    2626  assert round(mean(y)) == 5.0 
     
    8282 
    8383  ndim = 3 
    84   constraints = as_constraint(penalty, solver='fmin_powell') 
     84  constraints = as_constraint(penalty, solver='fmin') 
    8585  #XXX: this is expensive to evaluate, as there are nested optimizations 
    8686 
     
    9191  assert round(mean(_x)) == 5.0 
    9292  assert round(spread(_x)) == 5.0 
     93  assert round(penalty(_x)) == 0.0 
    9394 
    9495  def cost(x): 
     
    9697 
    9798  npop = ndim*3 
    98   from mystic.solvers import fmin_powell, diffev 
     99  from mystic.solvers import diffev 
    99100  y = diffev(cost, x, npop, constraints=constraints, disp=False, gtol=10) 
    100101 
     
    122123    return abs(sum(x) - 5.0) 
    123124 
    124   from mystic.solvers import fmin_powell 
    125   y = fmin_powell(cost, x, penalty=penalty, disp=False) 
     125  from mystic.solvers import fmin 
     126  y = fmin(cost, x, penalty=penalty, disp=False) 
    126127 
    127128  assert round(mean(y)) == 5.0 
     
    140141    return abs(sum(x) - 5.0) 
    141142 
    142   from mystic.solvers import fmin_powell 
     143  from mystic.solvers import fmin 
    143144  from numpy import array 
    144145  x = array([1,2,3,4,5]) 
    145   y = fmin_powell(cost, x, penalty=penalty, disp=False) 
     146  y = fmin(cost, x, penalty=penalty, disp=False) 
    146147 
    147148  assert round(mean(y)) == 5.0 
  • branches/decorate/wrapper.py

    r609 r610  
     1#!/usr/bin/env python 
     2 
    13#XXX: be mindful if the decorators restrict to functions that expect arrays 
    24#     compare against the originals for restrictions 
    35 
    46#XXX: when_decorated registers methods to 'populate up' when is decorated ? 
    5  
    67 
    78from mystic.tools import Null 
     
    8384 
    8485 
     86def isbounded(func, x, min=None, max=None): 
     87    """return False if func(x) evaluates outside the bounds, True otherwise. 
     88 
     89Inputs: 
     90    func -- a function of x. 
     91    x -- a list of parameters. 
     92 
     93Additional Inputs: 
     94    min -- list of lower bounds on parameters. 
     95    max -- list of upper bounds on parameters. 
     96""" 
     97    #from numpy import clip, array   #XXX: numpy.clip doesn't need/use func 
     98    #return all(clip(x,min,max) == array(x)) 
     99    bound = bounded(min,max) 
     100    wrapped = bound(func) 
     101    if wrapped(x) == inf: 
     102        return False     
     103    return True 
     104 
     105 
    85106def mixedin(scale=1.0, shift=0.0, mixin=lambda x:x, normalized=False): 
    86107  """build a function from the weighted sum of two functions 
Note: See TracChangeset for help on using the changeset viewer.