Function with excessive arguments - Python -
,i creating genetic fuzzy learning system , it's corresponding training simulation environment-> collection of functions , classes controlled master script user defines such things as: simulation scenario, controller characteristics, etc.
the result 50 different numpy arrays , lists arguments particular problem. these arguments must given controller generating functions(s), , simulation determines effectiveness of each controller. process using multiprocess.starmap_async method parallelize fitness of each controller. master script calls controller generation 50 arguments, multiprocess calls it's workers 55 processes, , workers call simulation files 57 inputs. (it current understanding using many input arguments multiprocesses not increase overhead names pointing data, rather copying or re-initializing it... if wrong please let me know!)
i understand can replace 50 arguments 1 list contains of arguments, , utilize global variables in master script avoid having of book-keeping. lot of these variables not change, large data structures don't want calculate more once. there other approaches, , 1 considered acceptable? wish avoid having 10 lines of args every call of in project.
be brutal need be, runs perfectly, simulations going more , more complex, number of (non-optional) arguments growing. i've removed of more specific var names, here call main script runs:
(opt_str,opt_fit) = trainer(map_size,targets,sams,sams_stat,ais,ais_stat, b_mpammo,b_sdammo,route,vel,b_range,a_range, s_range,b_flight, a_flight, s_flight,... lots more) inside ga have:
step = np.int8(pop_size/8) pol = pool(processes=8) res = pol.starmap_async(simworker, ((i, i+step, map_size,targets, sams,sams_stat,ais,ais_stat, b_mpammo,b_sdammo,route,vel,b_range, a_range,s_range,b_flight, a_flight, s_flight,fitness, ttr,ttb,ttcr,ttcb,pos,times,pop,... lots more args) in range(0, pop_size, step))) and simworker:
for p in range(start, stop): fitness[p] = sim_t(map_size,targets,sams,sams_stat,ais,ais_stat, b_mpammo,b_sdammo,route,vel,b_range,a_range,s_range,b_flight, a_flight, s_flight, ttr,ttb,ttcr,ttcb,pos,times,pop[p],... lots more)
if use keyword arguments, aka **kwargs can pass in many need, , not require ones don't need. function can check , take ones wants.
alternatively, create object (i suggest class) represents state, , build , pass function.
Comments
Post a Comment