|
| 1 | +""" |
| 2 | +:mod:`FunctionSlot` -- function slots module |
| 3 | +================================================================== |
| 4 | +
|
| 5 | +The *function slot* concept is large used by Pyevolve, the idea |
| 6 | +is simple, each genetic operator or any operator, can be assigned |
| 7 | +to a slot, by this way, we can add more than simple one operator, |
| 8 | +we can have for example, two or more mutator operators at same time, |
| 9 | +two or more evaluation functions, etc. In this :mod:`FunctionSlot` module, |
| 10 | +you'll find the class :class:`FunctionSlot.FunctionSlot`, which is the slot class. |
| 11 | +
|
| 12 | +""" |
| 13 | + |
| 14 | +from random import uniform as rand_uniform |
| 15 | + |
| 16 | +from . import Util |
| 17 | +import collections |
| 18 | + |
| 19 | +class FunctionSlot(object): |
| 20 | + """ FunctionSlot Class - The function slot |
| 21 | +
|
| 22 | + Example: |
| 23 | + >>> genome.evaluator.set(eval_func) |
| 24 | + >>> genome.evaluator[0] |
| 25 | + <function eval_func at 0x018C8930> |
| 26 | + >>> genome.evaluator |
| 27 | + Slot [Evaluation Function] (Count: 1) |
| 28 | + Name: eval_func |
| 29 | + >>> genome.evaluator.clear() |
| 30 | + >>> genome.evaluator |
| 31 | + Slot [Evaluation Function] (Count: 0) |
| 32 | + No function |
| 33 | +
|
| 34 | + You can add weight to functions when using the `rand_apply` paramter: |
| 35 | + >>> genome.evaluator.set(eval_main, 0.9) |
| 36 | + >>> genome.evaluator.add(eval_sec, 0.3) |
| 37 | + >>> genome.evaluator.setRandomApply() |
| 38 | +
|
| 39 | + In the above example, the function *eval_main* will be called with 90% of |
| 40 | + probability and the *eval_sec* will be called with 30% of probability. |
| 41 | +
|
| 42 | + There are another way to add functions too: |
| 43 | + >>> genome.evaluator += eval_func |
| 44 | +
|
| 45 | + :param name: the slot name |
| 46 | + :param rand_apply: if True, just one of the functions in the slot |
| 47 | + will be applied, this function is randomly picked based |
| 48 | + on the weight of the function added. |
| 49 | +
|
| 50 | + """ |
| 51 | + |
| 52 | + def __init__(self, name="Anonymous Function", rand_apply=False): |
| 53 | + """ The creator of the FunctionSlot Class """ |
| 54 | + self.funcList = [] |
| 55 | + self.funcWeights = [] |
| 56 | + self.slotName = name |
| 57 | + self.rand_apply = rand_apply |
| 58 | + |
| 59 | + def __typeCheck(self, func): |
| 60 | + """ Used internally to check if a function passed to the |
| 61 | + function slot is callable. Otherwise raises a TypeError exception. |
| 62 | +
|
| 63 | + :param func: the function object |
| 64 | + """ |
| 65 | + if not isinstance(func, collections.Callable): |
| 66 | + Util.raiseException("The function must be a method or function", TypeError) |
| 67 | + |
| 68 | + def __iadd__(self, func): |
| 69 | + """ To add more functions using the += operator |
| 70 | +
|
| 71 | + .. versionadded:: 0.6 |
| 72 | + The __iadd__ method. |
| 73 | + """ |
| 74 | + self.__typeCheck(func) |
| 75 | + self.funcList.append(func) |
| 76 | + return self |
| 77 | + |
| 78 | + def __getitem__(self, index): |
| 79 | + """ Used to retrieve some slot function index """ |
| 80 | + return self.funcList[index] |
| 81 | + |
| 82 | + def __setitem__(self, index, value): |
| 83 | + """ Used to set the index slot function """ |
| 84 | + self.__typeCheck(value) |
| 85 | + self.funcList[index] = value |
| 86 | + |
| 87 | + def __iter__(self): |
| 88 | + """ Return the function list iterator """ |
| 89 | + return iter(self.funcList) |
| 90 | + |
| 91 | + def __len__(self): |
| 92 | + """ Return the number of functions on the slot |
| 93 | +
|
| 94 | + .. versionadded:: 0.6 |
| 95 | + The *__len__* method |
| 96 | + """ |
| 97 | + return len(self.funcList) |
| 98 | + |
| 99 | + def setRandomApply(self, flag=True): |
| 100 | + """ Sets the random function application, in this mode, the |
| 101 | + function will randomly choose one slot to apply |
| 102 | +
|
| 103 | + :param flag: True or False |
| 104 | +
|
| 105 | + """ |
| 106 | + if not isinstance(flag, bool): |
| 107 | + Util.raiseException("Random option must be True or False", TypeError) |
| 108 | + |
| 109 | + self.rand_apply = flag |
| 110 | + |
| 111 | + def clear(self): |
| 112 | + """ Used to clear the functions in the slot """ |
| 113 | + if len(self.funcList) > 0: |
| 114 | + del self.funcList[:] |
| 115 | + del self.funcWeights[:] |
| 116 | + |
| 117 | + def add(self, func, weight=0.5): |
| 118 | + """ Used to add a function to the slot |
| 119 | +
|
| 120 | + :param func: the function to be added in the slot |
| 121 | + :param weight: used when you enable the *random apply*, it's the weight |
| 122 | + of the function for the random selection |
| 123 | +
|
| 124 | + .. versionadded:: 0.6 |
| 125 | + The `weight` parameter. |
| 126 | +
|
| 127 | + """ |
| 128 | + self.__typeCheck(func) |
| 129 | + self.funcList.append(func) |
| 130 | + self.funcWeights.append(weight) |
| 131 | + |
| 132 | + def isEmpty(self): |
| 133 | + """ Return true if the function slot is empy """ |
| 134 | + return (len(self.funcList) == 0) |
| 135 | + |
| 136 | + def set(self, func, weight=0.5): |
| 137 | + """ Used to clear all functions in the slot and add one |
| 138 | +
|
| 139 | + :param func: the function to be added in the slot |
| 140 | + :param weight: used when you enable the *random apply*, it's the weight |
| 141 | + of the function for the random selection |
| 142 | +
|
| 143 | + .. versionadded:: 0.6 |
| 144 | + The `weight` parameter. |
| 145 | +
|
| 146 | + .. note:: the method *set* of the function slot remove all previous |
| 147 | + functions added to the slot. |
| 148 | + """ |
| 149 | + self.clear() |
| 150 | + self.__typeCheck(func) |
| 151 | + self.add(func, weight) |
| 152 | + |
| 153 | + def apply(self, index, obj, **args): |
| 154 | + """ Apply the index function |
| 155 | +
|
| 156 | + :param index: the index of the function |
| 157 | + :param obj: this object is passes as parameter to the function |
| 158 | + :param args: this args dictionary is passed to the function |
| 159 | +
|
| 160 | + """ |
| 161 | + if len(self.funcList) <= 0: |
| 162 | + raise Exception("No function defined: " + self.slotName) |
| 163 | + return self.funcList[index](obj, **args) |
| 164 | + |
| 165 | + def applyFunctions(self, obj=None, **args): |
| 166 | + """ Generator to apply all function slots in obj |
| 167 | +
|
| 168 | + :param obj: this object is passes as parameter to the function |
| 169 | + :param args: this args dictionary is passed to the function |
| 170 | +
|
| 171 | + """ |
| 172 | + if len(self.funcList) <= 0: |
| 173 | + Util.raiseException("No function defined: " + self.slotName) |
| 174 | + |
| 175 | + if not self.rand_apply: |
| 176 | + for f in self.funcList: |
| 177 | + yield f(obj, **args) |
| 178 | + else: |
| 179 | + v = rand_uniform(0, 1) |
| 180 | + fobj = None |
| 181 | + for func, weight in zip(self.funcList, self.funcWeights): |
| 182 | + fobj = func |
| 183 | + if v < weight: |
| 184 | + break |
| 185 | + v = v - weight |
| 186 | + |
| 187 | + yield fobj(obj, **args) |
| 188 | + |
| 189 | + def __repr__(self): |
| 190 | + """ String representation of FunctionSlot """ |
| 191 | + strRet = "Slot [%s] (Count: %d)\n" % (self.slotName, len(self.funcList)) |
| 192 | + |
| 193 | + if len(self.funcList) <= 0: |
| 194 | + strRet += "\t\tNo function\n" |
| 195 | + return strRet |
| 196 | + |
| 197 | + for f, w in zip(self.funcList, self.funcWeights): |
| 198 | + strRet += "\t\tName: %s - Weight: %.2f\n" % (f.__name__, w) |
| 199 | + if f.__doc__: |
| 200 | + strRet += "\t\tDoc: " + f.__doc__ + "\n" |
| 201 | + |
| 202 | + return strRet |
0 commit comments