eckity.genetic_encodings.gp.tree.functions
This module implements functions used in the function (internal) nodes of a GP tree. Note: all functions work on numpy arrays.
1""" 2This module implements functions used in the function (internal) nodes of a GP tree. 3Note: all functions work on numpy arrays. 4""" 5 6import numpy as np 7 8 9def f_add(x, y): 10 """x+y""" 11 return np.add(x, y) 12 13 14def f_sub(x, y): 15 """x-y""" 16 return np.subtract(x, y) 17 18 19def f_mul(x, y): 20 """x*y""" 21 return np.multiply(x, y) 22 23 24def f_div(x, y): 25 """protected division: if abs(y) > 0.001 return x/y else return 0""" 26 with np.errstate(divide='ignore', invalid='ignore'): 27 return np.where(np.abs(y) > 0.001, np.divide(x, y), 0.) 28 29 30def f_sqrt(x): 31 """protected square root: sqrt(abs(x))""" 32 return np.sqrt(np.absolute(x)) 33 34 35def f_log(x): 36 """protected log: if abs(x) > 0.001 return log(abs(x)) else return 0""" 37 with np.errstate(divide='ignore', invalid='ignore'): 38 return np.where(np.abs(x) > 0.001, np.log(np.abs(x)), 0.) 39 40 41def f_abs(x): 42 """absolute value of x""" 43 return np.absolute(x) 44 45 46def f_neg(x): 47 """negative of x""" 48 return np.negative(x) 49 50 51def f_inv(x): 52 """protected inverse: if abs(x) > 0.001 return 1/x else return 0""" 53 # with np.errstate(divide='ignore', invalid='ignore'): 54 # return np.where(np.abs(x) > 0.001, 1. / x, 0.) 55 return f_div(1, x) 56 57 58def f_max(x, y): 59 """maximum(x,y)""" 60 return np.maximum(x, y) 61 62 63def f_min(x, y): 64 """minimum(x,y)""" 65 return np.minimum(x, y) 66 67 68def f_sin(x): 69 """sin(x)""" 70 return np.sin(x) 71 72 73def f_cos(x): 74 """cos(x)""" 75 return np.cos(x) 76 77 78def f_tan(x): 79 """tan(x)""" 80 return np.tan(x) 81 82 83def f_iflte0(x, y, z): 84 """if x <= 0 return y else return z""" 85 return np.where(x <= 0, y, z) 86 87 88def f_ifgt0(x, y, z): 89 """if x > 0 return y else return z""" 90 return np.where(x > 0, y, z) 91 92 93def f_iflte(x, y, z, w): 94 """if x <= y return z else return w""" 95 return np.where(x <= y, z, w) 96 97 98def f_ifgt(x, y, z, w): 99 """if x > y return z else return w""" 100 return np.where(x > y, z, w) 101 102 103def f_and(x, y): 104 """x and y""" 105 return np.bitwise_and(x, y) 106 107 108def f_or(x, y): 109 """x or y""" 110 return np.bitwise_or(x, y) 111 112 113def f_not(x): 114 """not x""" 115 return np.logical_not(x).astype(int) 116 117 118def f_if_then_else(test, dit, dif): 119 """if test return dit else return dif""" 120 return np.where(test, dit, dif) 121 122 123 124full_function_set = [f_add, f_sub, f_mul, f_div, f_sqrt, f_log, f_abs, f_neg, f_inv, f_max, f_min, f_sin, f_cos, f_tan, 125 f_iflte0, f_ifgt0, f_iflte, f_ifgt, f_add, f_or, f_not, f_if_then_else]
def
f_add(x, y):
x+y
def
f_sub(x, y):
x-y
def
f_mul(x, y):
x*y
def
f_div(x, y):
25def f_div(x, y): 26 """protected division: if abs(y) > 0.001 return x/y else return 0""" 27 with np.errstate(divide='ignore', invalid='ignore'): 28 return np.where(np.abs(y) > 0.001, np.divide(x, y), 0.)
protected division: if abs(y) > 0.001 return x/y else return 0
def
f_sqrt(x):
protected square root: sqrt(abs(x))
def
f_log(x):
36def f_log(x): 37 """protected log: if abs(x) > 0.001 return log(abs(x)) else return 0""" 38 with np.errstate(divide='ignore', invalid='ignore'): 39 return np.where(np.abs(x) > 0.001, np.log(np.abs(x)), 0.)
protected log: if abs(x) > 0.001 return log(abs(x)) else return 0
def
f_abs(x):
absolute value of x
def
f_neg(x):
negative of x
def
f_inv(x):
52def f_inv(x): 53 """protected inverse: if abs(x) > 0.001 return 1/x else return 0""" 54 # with np.errstate(divide='ignore', invalid='ignore'): 55 # return np.where(np.abs(x) > 0.001, 1. / x, 0.) 56 return f_div(1, x)
protected inverse: if abs(x) > 0.001 return 1/x else return 0
def
f_max(x, y):
maximum(x,y)
def
f_min(x, y):
minimum(x,y)
def
f_sin(x):
sin(x)
def
f_cos(x):
cos(x)
def
f_tan(x):
tan(x)
def
f_iflte0(x, y, z):
if x <= 0 return y else return z
def
f_ifgt0(x, y, z):
if x > 0 return y else return z
def
f_iflte(x, y, z, w):
94def f_iflte(x, y, z, w): 95 """if x <= y return z else return w""" 96 return np.where(x <= y, z, w)
if x <= y return z else return w
def
f_ifgt(x, y, z, w):
99def f_ifgt(x, y, z, w): 100 """if x > y return z else return w""" 101 return np.where(x > y, z, w)
if x > y return z else return w
def
f_and(x, y):
x and y
def
f_or(x, y):
x or y
def
f_not(x):
not x
def
f_if_then_else(test, dit, dif):
119def f_if_then_else(test, dit, dif): 120 """if test return dit else return dif""" 121 return np.where(test, dit, dif)
if test return dit else return dif
full_function_set =
[<function f_add>, <function f_sub>, <function f_mul>, <function f_div>, <function f_sqrt>, <function f_log>, <function f_abs>, <function f_neg>, <function f_inv>, <function f_max>, <function f_min>, <function f_sin>, <function f_cos>, <function f_tan>, <function f_iflte0>, <function f_ifgt0>, <function f_iflte>, <function f_ifgt>, <function f_add>, <function f_or>, <function f_not>, <function f_if_then_else>]