Changeset 1153

Show
Ignore:
Timestamp:
07/22/08 01:58:30 (4 months ago)
Author:
dmitrey.kroshko
Message:

changes to Function class

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openopt/scikits/openopt/Kernel/Function.py

    r1136 r1153  
    22# created by Dmitrey 
    33#from numpy import copy, isnan, array, argmax, abs, zeros 
    4 from numpy import inf, asfarray, copy, all 
     4from numpy import inf, asfarray, copy, all, any 
    55from oologfcn import OpenOptException 
    66 
     
    88    #initialized = False 
    99    #__allowedFields__ = ['d', 'dep'] 
     10    input = None # if None then x will be used 
    1011 
    1112    def __init__(self, fun, *args, **kwargs): 
     
    1617            setattr(self, key, item) 
    1718 
    18     def __getFunc__(self, x): 
    19         if hasattr(self, 'f_key_prev'): 
    20             if hasattr(self, 'dep'): x_to_compare = x[self.dep] 
    21             else: x_to_compare = x 
    22             if all(self.f_key_prev == x_to_compare): 
    23                 pass 
     19    def __getInput__(self): 
     20        if self.input is None: 
     21            return None 
     22        elif not type(self.input) in (list, tuple): 
     23            self.input = (self.input, ) 
     24        r = [] 
     25        for item in self.input: 
     26            if not callable(item): r.append(item) 
     27            elif item.__module__ == 'scikits.openopt.Kernel.Function': 
     28                item.x = self.x 
     29                r.append(item()) 
     30            else:  r.append(item()) 
     31        return tuple(r) 
     32 
     33    def __getFunc__(self, x=None): 
     34        if x is None: x = self.x 
     35        else: self.x = x 
     36 
     37        if self.input is not None: 
     38            Input = key_to_compare = self.__getInput__() 
     39        else: 
     40            #assert x is not None #or xxxx is not None 
     41            #if x is None: x = xxxx 
     42            Input = x 
     43            if hasattr(self, 'dep'): 
     44                key_to_compare = x[self.dep] 
    2445            else: 
    25                 self.f_key_prev = copy(x) 
    26                 self.f_val_prev = self.fun(x) 
    27         else: 
    28             if hasattr(self, 'dep'): 
    29                 self.f_key_prev = copy(x[self.dep]) 
     46                key_to_compare = x 
     47 
     48        if not hasattr(self, 'f_key_prev') or any(self.f_key_prev != key_to_compare): 
     49#            if self.fun.__module__ == 'scikits.openopt.Kernel.Function': 
     50#                setattr(fun, 'x', x) 
     51            if self.input is not None: 
     52                self.f_val_prev = self.fun(*Input) 
    3053            else: 
    31                 self.f_key_prev = copy(x
    32             self.f_val_prev = self.fun(x
     54                self.f_val_prev = self.fun(Input
     55        self.f_key_prev = copy(key_to_compare
    3356        return copy(asfarray(self.f_val_prev)) 
    3457 
    35     __call__ = lambda self, x: asfarray(self.__getFunc__(x)
     58    __call__ = lambda self, *args: self.__getFunc__(*args
    3659 
    37     D = lambda self, x: self.__d(x
     60    D = lambda self, *args: self.__d(*args
    3861 
    39     def __d(self, x): 
     62    def __d(self, x=None): 
     63        if x is None: x = self.x 
     64        else: self.x = x 
     65 
     66        if self.input is not None: 
     67            Input = key_to_compare = self.__getInput__() 
     68        else: 
     69            #assert x is not None or xxxx is not None 
     70            Input = x 
     71            if hasattr(self, 'dep'): 
     72                key_to_compare = x[self.dep] 
     73            else: 
     74                key_to_compare = x 
     75 
    4076        if hasattr(self, 'd'): 
    4177            ########################## 
    42             if hasattr(self, 'd_key_prev'): 
    43                 if hasattr(self, 'dep'): x_to_compare = x[self.dep] 
    44                 else: x_to_compare = x 
    45                 if all(self.d_key_prev == x_to_compare): 
    46                     pass 
    47                 else: 
    48                     self.d_key_prev = copy(x) 
    49                     self.d_val_prev = self.d(x) 
    50             else: 
    51                 if hasattr(self, 'dep'): 
    52                     self.d_key_prev = copy(x[self.dep]) 
    53                 else: 
    54                     self.d_key_prev = copy(x) 
    55                 self.d_val_prev = self.d(x) 
     78            if not hasattr(self, 'd_key_prev') or any(self.d_key_prev != key_to_compare): 
     79                self.d_val_prev = self.d(Input) 
     80            self.d_key_prev = copy(key_to_compare) 
    5681            return copy(asfarray(self.d_val_prev)) 
    57             # TODO: add args 
    58             ########################## 
    59  
    60  
    6182        else: 
    6283            #TODO: add numerical derivative here 
     
    80101        self.fun = fun 
    81102 
    82     def __getFunc__(self, x): 
    83         return self.fun(x
     103    def __getFunc__(self, *args, **kwargs): 
     104        return self.fun(*args, **kwargs
    84105 
    85     __call__ = lambda self, x: asfarray(self.__getFunc__(x)) 
     106    __call__ = lambda self, *args, **kwargs: asfarray(self.__getFunc__(*args, **kwargs)) 
    86107 
    87108    D = lambda self, x: self.__d(x) 
    88109 
    89     def __d(self, x): 
     110    def __d(self, *args, **kwargs): 
    90111        if hasattr(self, 'd'): 
    91             r = asfarray(self.d(x)) 
     112            r = asfarray(self.d(*args, **kwargs)) 
    92113            # TODO: add args 
    93114        else: