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