Source code for evolve
1#! /bin/python3
2#Genetic Algorithm for Intrinsic Analog Hardware Evolution
3# FOR USE WITH LATTICE iCE40 FPGAs ONLY
4#
5# This code can be used to evolve an analog oscillator
6#
7# Author: Derek Whitley
8
9"""
10evolve.py
11---------
12
13This file is run to initiate a round of evolution. There are command line arguments to help initiate an evolution run.
14
15This file has been reviewed, and should be at a satisfacroty level.
16
17"""
18
19
20from Evolution import Evolution
21from arg_parse_utils import add_bool_argument
22import argparse
23import signal
24import sys
25
26## Command Line Argument And Help information for this file.
27
28#Program info for --help output
29program_name="evolve"
30program_description="""This program evolves a population of FPGA layouts (or simulations of them) acording to a predefined fitness function.
31All files will presume the main directory of BitStreamEvolution unless absolute path given."""
32program_epilog="""For non-simulations, and Arduino and the Lattice iCE40 FPGA are also needed.
33Exit Status:
340 - No issues
351 - Issue while running
36130 - KeyboardInterrupt (ctrl+c)"""
37#Check exit codes by running `echo $?` after running the command.
38
39## Setting Default Values for Function Call
40default_config = "data/config.ini" #Use none if want specified
41# By default, we don't want to use a base config.
42# We also don't want to override by default, as that would defeat the purpose of the parameter
43# in config files to specify the base config to use
44# 'default_config' is provided as a sample file, not totally meant to be used - but, might need a rename
45default_base_config = None #"data/default_config.ini"
46default_output_directory = None #If not changed, information only saved internally.
47default_experiment_description = None #If not changed, requires user to enter.
48
49# This is where the completed config from Config Builder is stored while evolution is active for easy access.
50BUILT_CONFIG_PATH = "./workspace/builtconfig.ini"
51
52## Creating an Argument Parser to Parse Arguments
53parser = argparse.ArgumentParser(prog=program_name,
54 description=program_description,
55 epilog=program_epilog)
56parser.add_argument('-c','--config',type=str,default=default_config,
57 help=f"The file this simulation is generated from. Default: {default_config}")
58parser.add_argument('-bc','--base-config',type=str,default=default_base_config,
59 help= f"The config any unspecified values in the main config is pulled from. " +\
60 f"This overpowers the main config specified in the file if provided. Default: {default_base_config}")
61parser.add_argument('-o','--output-directory', type=str,default=default_output_directory,
62 help=f"The directory output from the simulation is copied to after a successful simulation. Default: {default_output_directory}")
63parser.add_argument('-d','--description', type=str,default=default_experiment_description,
64 help="The description of this simulation. Requires manual entry if not an argument.")
65print_flags = {'enable':['-p','--print-only','--test','--no-action'],
66 'disable':['-np', '--no-print-only','--normal','--act']}
67add_bool_argument(parser,"print_only",flag_names=print_flags,default=False)
68# --help is added by default
69
[docs]
70def run():
71 """Perform evolution according to the provided config."""
72 ## Parsing Args and configuring Variables
73 __args=parser.parse_args()
74
75 ## TODO: DELETE ME WHEN args are Logged
76 if (__args.print_only):
77 print(f"Arguments Detected: {__args}") # probably should log this instead. not sure if with logger directly or through config.
78
79 # Run Evolution class, which actually executes an experiment
80 evolution = Evolution()
81 def sig_int_handler(sig, frame):
82 print('User Interrupt')
83 evolution.clean_up()
84 # Still exit, but make sure we can save everything first
85 # Denote with exit that a user interrupt occoured
86 sys.exit(130)
87
88 signal.signal(signal.SIGINT, sig_int_handler)
89
90 evolution.evolve(
91 primary_config_path = __args.config,
92 base_config_path = __args.base_config,
93 output_directory = __args.output_directory,
94 experiment_description = __args.description,
95 built_config_path= BUILT_CONFIG_PATH,
96 print_action_only= __args.print_only
97 )
98
99if __name__ == "__main__":
100 run()