Changeset 1153
- Timestamp:
- 07/22/08 01:58:30 (4 months ago)
- Files:
-
- trunk/openopt/scikits/openopt/Kernel/Function.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/openopt/scikits/openopt/Kernel/Function.py
r1136 r1153 2 2 # created by Dmitrey 3 3 #from numpy import copy, isnan, array, argmax, abs, zeros 4 from numpy import inf, asfarray, copy, all 4 from numpy import inf, asfarray, copy, all, any 5 5 from oologfcn import OpenOptException 6 6 … … 8 8 #initialized = False 9 9 #__allowedFields__ = ['d', 'dep'] 10 input = None # if None then x will be used 10 11 11 12 def __init__(self, fun, *args, **kwargs): … … 16 17 setattr(self, key, item) 17 18 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] 24 45 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) 30 53 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) 33 56 return copy(asfarray(self.f_val_prev)) 34 57 35 __call__ = lambda self, x: asfarray(self.__getFunc__(x))58 __call__ = lambda self, *args: self.__getFunc__(*args) 36 59 37 D = lambda self, x: self.__d(x)60 D = lambda self, *args: self.__d(*args) 38 61 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 40 76 if hasattr(self, 'd'): 41 77 ########################## 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) 56 81 return copy(asfarray(self.d_val_prev)) 57 # TODO: add args58 ##########################59 60 61 82 else: 62 83 #TODO: add numerical derivative here … … 80 101 self.fun = fun 81 102 82 def __getFunc__(self, x):83 return self.fun( x)103 def __getFunc__(self, *args, **kwargs): 104 return self.fun(*args, **kwargs) 84 105 85 __call__ = lambda self, x: asfarray(self.__getFunc__(x))106 __call__ = lambda self, *args, **kwargs: asfarray(self.__getFunc__(*args, **kwargs)) 86 107 87 108 D = lambda self, x: self.__d(x) 88 109 89 def __d(self, x):110 def __d(self, *args, **kwargs): 90 111 if hasattr(self, 'd'): 91 r = asfarray(self.d( x))112 r = asfarray(self.d(*args, **kwargs)) 92 113 # TODO: add args 93 114 else:
