DEAP / deap

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

Getting "trading_system takes exactly 16 arguments (17 given)" #382

Open cenouralm opened 5 years ago

cenouralm commented 5 years ago

So basically my evaluation function takes 16 arguments, and when I register the function as: toolbox.register("evaluate", Trading_Simulator.trading_system,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16) It says that "fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) TypeError: trading_system() takes exactly 16 arguments (17 given)"

Am I missing something? (the numbers are only to not put every single argument here)

chaltik commented 5 years ago

looks like Trading_Simulator is passed in as "self" argument into the trading_system function. Try writing a wrapper and see if it works.

On Mon, Jul 1, 2019, 11:46 Rodrigo Almeida notifications@github.com wrote:

So basically my evaluation function takes 16 arguments, and when I register the function as: toolbox.register("evaluate", Trading_Simulator.trading_system,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16) It says that "fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) TypeError: trading_system() takes exactly 16 arguments (17 given)"

Am I missing something? (the numbers are only to not put every single argument here)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/DEAP/deap/issues/382?email_source=notifications&email_token=AD755K4YDDTP6HRZOUSJFTDP5JGJBA5CNFSM4H4VABAKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4G4WUS6Q, or mute the thread https://github.com/notifications/unsubscribe-auth/AD755K6NWOU24P4R6JNIZEDP5JGJBANCNFSM4H4VABAA .

cenouralm commented 5 years ago

How do I do the wrapper?

Rodrigo Almeida

No dia 01/07/2019, às 20:55, chaltik notifications@github.com escreveu:

looks like Trading_Simulator is passed in as "self" argument into the trading_system function. Try writing a wrapper and see if it works.

On Mon, Jul 1, 2019, 11:46 Rodrigo Almeida notifications@github.com wrote:

So basically my evaluation function takes 16 arguments, and when I register the function as: toolbox.register("evaluate", Trading_Simulator.trading_system,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16) It says that "fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) TypeError: trading_system() takes exactly 16 arguments (17 given)"

Am I missing something? (the numbers are only to not put every single argument here)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/DEAP/deap/issues/382?email_source=notifications&email_token=AD755K4YDDTP6HRZOUSJFTDP5JGJBA5CNFSM4H4VABAKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4G4WUS6Q, or mute the thread https://github.com/notifications/unsubscribe-auth/AD755K6NWOU24P4R6JNIZEDP5JGJBANCNFSM4H4VABAA .

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

chaltik commented 5 years ago

What I said above assumed that in your code "Trading_Simulator" is an instance of a class where you have defined the function "trading_system" like this:

def trading_system(self, x1,....,x16):

do stuff

If that is indeed the case, then you can define a wrapper in two ways. If you only need to define one instance of Trading_Simulator, then in your main() you just define

def trading_system_wrapper(x1,....,x16): return Trading_Simulator.trading_system(x1,...,x16)

and then do the toolbox.register("evaluate", trading_system_wrapper, 1,2,..., 16)

Alternatively, if you might need to create different instances of Trading_Simulator, you could do this:

def trading_system_wrapper(TS_Instance, x1,...,x16): return TS_Instance.trading_system(x1,....,x16)

and then use the toolbox as follows: TS_Instance = Trading_Simulator(...) toolbox.register("evaluate", trading_system_wrapper, TS_Instance,x1,x2,...,x16)

BUT in this second method you need to make sure that the Trading_Simulator class can be pickled (you will get an error to that effect if it cannot, but you will worry about it when and if it happens)

On Mon, Jul 1, 2019 at 1:05 PM Rodrigo Almeida notifications@github.com wrote:

How do I do the wrapper?

Rodrigo Almeida

No dia 01/07/2019, às 20:55, chaltik notifications@github.com escreveu:

looks like Trading_Simulator is passed in as "self" argument into the trading_system function. Try writing a wrapper and see if it works.

On Mon, Jul 1, 2019, 11:46 Rodrigo Almeida notifications@github.com wrote:

So basically my evaluation function takes 16 arguments, and when I register the function as: toolbox.register("evaluate",

Trading_Simulator.trading_system,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16) It says that "fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) TypeError: trading_system() takes exactly 16 arguments (17 given)"

Am I missing something? (the numbers are only to not put every single argument here)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub < https://github.com/DEAP/deap/issues/382?email_source=notifications&email_token=AD755K4YDDTP6HRZOUSJFTDP5JGJBA5CNFSM4H4VABAKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4G4WUS6Q , or mute the thread < https://github.com/notifications/unsubscribe-auth/AD755K6NWOU24P4R6JNIZEDP5JGJBANCNFSM4H4VABAA

.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/DEAP/deap/issues/382?email_source=notifications&email_token=AD755KZGJ5M2BSXUWQEGMZDP5JPSFA5CNFSM4H4VABAKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODY7GK7Y#issuecomment-507405695, or mute the thread https://github.com/notifications/unsubscribe-auth/AD755K4NDGWBRZ3O3LDG3BDP5JPSFANCNFSM4H4VABAA .

cenouralm commented 5 years ago

Hey! Thanks in advance for your answer! I tried the first solution for the wrapper and still did not work and gave the same error :/

No dia 01/07/2019, às 21:36, chaltik notifications@github.com escreveu:

trading_system_wrapper

fmder commented 5 years ago

The toolbox requires keyword arguments.

toolbox.register("evaluate", func, a=1, b=2, c=3, ...)