ShuhuaGao / geppy

A framework for gene expression programming (an evolutionary algorithm) in Python
https://geppy.readthedocs.io/en/latest/
GNU Lesser General Public License v3.0
208 stars 76 forks source link

Please provide the symbolic function mapping for 'tan' in symbolic_function_map. #25

Closed ZhiYu-blast closed 4 years ago

ZhiYu-blast commented 4 years ago

Hi, Shuhua, Thanks for providing the gene expression programming. When using the provided example (Use the GEP-RNC algorithm with the UCI Power Plant dataset), I tried to use some functions such as sin, cos, and tan to increase the precision of developed formula. However, some errors were found. pset.add_function(math.sin, 1): Please provide the symbolic function mapping for 'sin' in symbolic_function_map. pset.add_function(math.tan, 1): Please provide the symbolic function mapping for 'tan' in symbolic_function_map. pset.add_function(math.cos, 1): Please provide the symbolic function mapping for 'cos' in symbolic_function_map. pset.add_function(math.log, 1): ValueError Traceback (most recent call last) ValueError: math domain error pset.add_function(sp.log, 1): TypeError Traceback (most recent call last) TypeError: No loop matching the specified signature and casting was found for ufunc lstsq_n Could you please give me some suggestions? Thanks again.

ShuhuaGao commented 4 years ago

Hi, Zhiyu, First, symbolic computation is needed only if you want to simplify the final individuals you have evolved for presentation or visualization purposes. Second, the package sympy does not recognize the functions in the math module. For instance, math.cos can only accept a float argument, while sympy.cos will process a symbol argument. That's why a second parameter (a dict) may be provided to gep.simplify to map math.cos to sympy.cos, which looks like {math.cos.__name__: sympy.cos} (because a function itself cannot be the key of a dict). Finally, for some common operators, geppy has a built-in mapping DEFAULT_SYMBOLIC_FUNCTION_MAP , which unfortunately does not contain trigonometric functions like sin or cos. However, as stated above, you can add any function you like as long as you could provide a proper mapping for symbolic computation. As a side note, such symbolic simplification is not used during evolution automatically, but only when you call the gep.simplify function generally after the evolution has finished. Please try it again. Your feedback is welcome.

ShuhuaGao commented 4 years ago

By the way, I just added the trigonometric functions including sin, cos, and tan into the default mapping. Thus, you may just pull the latest code.

ZhiYu-blast commented 4 years ago

Thank you very much for your kind reply, and It do works well. Hope more and more people can use your program for calculation and research. Thanks again.