DEAP / deap

Distributed Evolutionary Algorithms in Python
http://deap.readthedocs.org/
GNU Lesser General Public License v3.0
5.86k stars 1.13k forks source link

gp.compile should be called gp.eval #545

Open nbro opened 3 years ago

nbro commented 3 years ago

The function compile defined inside gp actually calls Python's eval and not compile, so the name of this function is highly misleading. In fact, you can get syntax errors if you try to evaluate an individual that cannot be converted to a string and evaluated as an expression. For example, if you had defined a terminal of the form

class MyTerminal:
    pass
a = MyTerminal()

Then use a as a terminal of your primitive set, you may get a SyntaxError: invalid syntax if you cal gp.compile to evaluate an individual that uses a as a terminal, more precisely, you may get an error message like this

...

lambda ...: ... (<__main__.MyTerminal object at ...>)

SyntaxError: invalid syntax

If you had defined a __repr__ method for MyTerminal that returns something that can be evaluated, then you would not get this error.

So, given that Python's eval and compile are different functions, it's highly misleading to have a function compile that actually calls eval. In other words, I'm proposing gp.compile to be changed to gp.eval or gp.evaluate.

naive666 commented 3 years ago

Hi, I met the same problem when I was evaluating the dataframes. So what I should do is to create a new class that includes repr ? or any better suggestions? Thank you!

fmder commented 3 years ago

I understand the confusion, but I think eval is also miss leading as it could be confuse with the evaluation function. I like compile as it creates a callable from the representation. The discussion is leading me to the more general question, could we eliminate the compile function altogheter? Or more simpler could it be anything else than compile of eval?

fmder commented 3 years ago

It looks to me that python's compile is doing a somewhat similar thing no?