Source code for multi_evolve
1#! /bin/python3
2# This program calls evolve.py multiple times in order to run multiple evolutionary experiments.
3
4
5import argparse
6from Evolution import Evolution
7from evolve import BUILT_CONFIG_PATH, program_description as evolve_program_description
8from arg_parse_utils import add_bool_argument
9from functools import partial
10
11program_name="multi_evolve"
12program_description=f"""This function runs multiple evolution simulations specified by multiple config files.
13All files will presume the main directory of BitStreamEvolution unless absolute path given.
14
15The Evolution Simulator works as follows:
16---
17{evolve_program_description}
18---
19"""
20program_epilog="""
21Exit Status:
220 - No issues
231 - Issue while running
24130 - Keyboard Interrupt"""
25
26default_config_array = None #Use none if want specified
27default_base_config = None
28default_output_directory = None #If not changed, information only saved internally.
29default_experiment_description = "multi_evolve.py for config file: '{config}' itteration number: {config_num}" #If not changed, requires user to enter.
30
31parser = argparse.ArgumentParser(prog=program_name,
32 description=program_description,
33 epilog=program_epilog)
34parser.add_argument('-c','--configs',type=str,nargs='*',default=default_config_array,
35 help=f"")
36parser.add_argument('-bc','--base-config',type=str,default=default_base_config,
37 help= f"The config any unspecified values in the main config is pulled from. " +\
38 f"This overpowers the main config specified in the file if provided. Default: {default_base_config}")
39parser.add_argument('-o','--output-directory', type=str,default=default_output_directory,
40 help=f"The directory output from the simulation is copied to after a successful simulation. Default: {default_output_directory}")
41parser.add_argument('-d','--description', type=str,default=default_experiment_description,
42 help="The description of this simulation. Requires manual entry if not an argument.")
43flags = {'enable':["-p","--print_only", "--no-action","--test"],
44 'disable':['-np',"--no-print-only",'--act']}
45add_bool_argument(parser,"print_only",flag_names=flags,default=False)
46# --help is added by default
47
48## need to add a way to create custom experiment descriptions.
49## This allows us to pass in an evolution object to test functionality.
[docs]
50def evolve_list_of_configs_selecting_evolution(*configs:str,
51 base_config:str,
52 output_directory:str,
53 experiment_description:str,
54 print_action_only:bool=False,
55 evolution_object:Evolution = Evolution()
56 ):
57 """This functin evolves a list of configs.
58 In experiment desctiption, the strings '{config}' for the current config's file path and
59 '{config_num}' for the itterastion number of the experiment."""
60 #"multi_evolve.py for config file: '{config}' itteration number: {config_num}"
61
62 if (evolution_object == None):
63 evolution_object = Evolution()
64
65 config_num = 1
66 for config in configs:
67
68 formatted_name = experiment_description.format(config=config,config_num=config_num)
69
70 if (print_action_only):
71 print(f"multi-evolve:{{config:{config},output:{output_directory},base_config:{base_config},description:{experiment_description}}}")
72
73 evolution_object.evolve(
74 primary_config_path= config,
75 output_directory= output_directory,
76 base_config_path= base_config,
77 built_config_path= BUILT_CONFIG_PATH,
78 experiment_description= formatted_name,
79 print_action_only= print_action_only
80 )
81
82 config_num += 1
83
84# create origional function by partially completing the function, relying on None to make the function create it's own evolution_object
85evolve_list_of_configs = partial(evolve_list_of_configs_selecting_evolution,evolution_object=None)
86
87def run():
88 args=parser.parse_args()
89
90 evolve_list_of_configs(
91 configs=args.configs,
92 base_config=args.base_config,
93 output_directory=args.output_directory,
94 experiment_description=args.description,
95 print_action_only=args.print_only
96 )
97
98if __name__ == "__main__":
99 run()