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

How can I add functions such as SQRT, EXP, X^2, POW, INV, LN in my code of Gene Expression Programming #28

Closed Afifa-Tamanna closed 4 years ago

Afifa-Tamanna commented 4 years ago

Hi, I cannot add the above mentioned functions functions. If I add function such as pset.add_function(operator.pow,2)

then the following things return;

:1: RuntimeWarning: overflow encountered in double_scalars Traceback (most recent call last): File "G:/My Drive/Afifa Tamanna/PhD/AI/GEP/Diffn_Analysis_GEP_model/Analysis/testviscosity.py", line 198, in pop, log = gep.gep_simple(pop, toolbox, n_generations=n_gen, n_elites=1, File "C:\Program Files (x86)\Python38-32\lib\site-packages\geppy\algorithms\basic.py", line 100, in gep_simple for ind, fit in zip(invalid_individuals, fitnesses): File "<__array_function__ internals>", line 5, in lstsq File "C:\Users\atam0001\AppData\Roaming\Python\Python38\site-packages\numpy\linalg\linalg.py", line 2259, in lstsq x, resids, rank, s = gufunc(a, b, rcond, signature=signature, extobj=extobj) File "C:\Users\atam0001\AppData\Roaming\Python\Python38\site-packages\numpy\linalg\linalg.py", line 109, in _raise_linalgerror_lstsq raise LinAlgError("SVD did not converge in Linear Least Squares") numpy.linalg.LinAlgError: SVD did not converge in Linear Least Squares Please assist me in solving the issue. Many Thanks.
ShuhuaGao commented 4 years ago

The main reason is that many functions you mentioned are some kind of dangerous. That is, it is easy to generate unreasonably large (such as pow(13, 50)) and even nan or errors (like sqrt(-1.2)).

Just like we usually define a protected version for div, you should also do that for these functions. A common way is to

Since evolution is always random, you cannot assume reasonable inputs or outputs for a given function. Instead, protect it. See this example for protected_div.

Afifa-Tamanna commented 4 years ago

Thank you for your quick response and the clarification!

adnanhama commented 3 years ago

Hi Shuhua; Thank you for the useful examples you provided in your page. in regard to the example:

(Numerical expression inference problem: numerical constant finding with ephemeral numerical constant (ENC))

There is only the single input 'x' and random numerical constants (RNC). I just wondering if I have three inputs as the following:

x ( date from 2.3 to 6.5) y (from 0.25 to 3) z (from 1 to 99)

How to implement the code and do I need to change the code to be like this:

n_cases = 100 X1 = np.random.uniform(-10, 10, size=n_cases) # random numbers in range [-10, 10) X2 = np.random.uniform(-10, 10, size=n_cases) X3 = np.random.uniform(-10, 10, size=n_cases)

Y = f(X1,X2,X3) + np.random.normal(size=n_cases) # Gaussian noise

Also, do I need to change the random range to be (2.3 to 6.5), (0.25 to 3) and (1 to 99)

regards