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
207 stars 76 forks source link

question: limit function parameter to const integers #37

Closed welaunchcn closed 3 years ago

welaunchcn commented 3 years ago

For example, function shift(a, N) takes two parameters. The value for 'N' must be in the range of 1 to 5. The value comes from a random integer number directly, not from output of any other functions.

Is it possible to specify this kind of constraints?

ShuhuaGao commented 3 years ago

Generally, the N is not a big number, right? If so, you can simply register multiple shift but with different names. For example, shift1(a) is equivalent to shift(a, 1), shift2(a) is equivalent to shift(a, 2).

welaunchcn commented 3 years ago

Yes that is the work around I'm using. The problem is that since we are having multiple instances for the same function, the creation of individuals tends to give much more weight to it.

ShuhuaGao commented 3 years ago

Yes, you are right. I noticed this issue also for adding terminals. The reason is that each primitive is chosen according to a uniform distribution. Ideally, there should be a weight along with each primitive, but I don't have time to make it for now. You may fork this project and do it at your willingness.

A workaround is that, supposing you have added 5 versions of shift, you can also add other functions 5 times as long as the name parameter in add_function is unique (see source).

welaunchcn commented 3 years ago

Thanks for your help, I will try as your suggested.