cavalab / brush

An interpretable machine learning library
http://cavalab.org/brush/
GNU General Public License v3.0
2 stars 0 forks source link

Bug fix - Segmentation fault caused by insert mutation #34

Closed gAldeia closed 1 year ago

gAldeia commented 1 year ago

While I was learning how to adjust the weights of the mutations, my Python kernel was dying due to a segmentation fault. This pull request fixes that and also has some minor fixes and comments as well.


The segmentation fault was occurring in get_op_with_arg. The function takes the argument and return type to match, and returns a compatible operator to be inserted in the tree.

https://github.com/cavalab/brush/blob/546c2a792c9bdfc8358dbb84e6bb2f90a3f71ee6/src/search_space.h#L382-L430

However, in the previous implementation (code above), the max_size restriction that I added would ignore all operators in rare cases, making the select_random iterate over an empty collection (this was hard to find :sweat_smile: ). For example, if max_arg_count=1, and the compatible nodes have node.get_arg_count()>1, then no node would be selected, and select_randomly would create a segmentation fault.

To fix that, I made the insert mutation behave like PTC2 by relaxing the size limit control of the programs. Now the get_op_with_arg will avoid creating an empty collection, and the insert_mutation can create a program that exceeds the maximum size by the highest arity among the operators.

Other improvements:

gAldeia commented 1 year ago

I created another pull request from a proper branch. Sorry about this.