def make_random_kid(env, folder, num, parent_num): org = makegene(random.randint(1, env.maxgenes+1)) # make a random gene org.folder = folder.path # set org folder (created in init of bfolder) org.logloc = "P_"+str(num+1)+"_B_"+str(parent_num+1) # set log output for location org.is_primeval = True # came from random, so its primeval folder.org = org # add the org to the folder writec(folder.path, folder.org) # write out the file if env.debug == True: call('g++ '+ folder.path + '*.cpp ./habitat/biosort.o -o ' + folder.path + 'organism.out -g -O0', shell = True) else: call('g++ '+ folder.path + '*.cpp ./habitat/biosort.o -o ' + folder.path + 'organism.out -O0', shell = True)
def make_random(env, folder, num): org = makegene(random.randint(1, env.maxgenes+1)) org.folder = folder.path org.logloc = "R_"+str(num) org.is_primeval = True folder.org = org writec(folder.path, folder.org) if env.debug == True: call('g++ '+folder.path+'*.cpp ./habitat/biosort.o -o '+folder.path+'organism.out -g -O0', shell = True) else: call('g++ '+ folder.path + '*.cpp ./habitat/biosort.o -o ' + folder.path + 'organism.out -O0', shell = True)
def make_random_breeder(env, folder, num): org = makegene(random.randint(1, env.maxgenes+1)) # Make a random gene org.is_primeval = True # Came from random, so its primeval org.folder = folder.path # set org folder (needed for mutate) org.logloc = "B_"+str(num) # set log output for location folder.org = org writec(folder.path, folder.org) # write out the file if env.debug == True: call('g++ '+ folder.path + '*.cpp ./habitat/biosort.o -o ' + folder.path + 'organism.out -g -O0', shell = True) else: call('g++ '+ folder.path + '*.cpp ./habitat/biosort.o -o ' + folder.path + 'organism.out -O0', shell = True) copy('./habitat/biosort.h', './habitat/breeder'+str(num))
def Start(env): ops = 0 #Counter for operations performed by organism before completing org = '' #Variable to hold the first organism random_counter = 1 #Counts randoms until a sorter is found #Creates an organism and runs it, repeats until an organism is found #which sorts within the given pressure threshold. while ops not in range(1, env.pressure+1): sys.stdout.write("Attempting to find first sorter: Attempt %i\r" %random_counter) sys.stdout.flush() random_counter += 1 org = makegene(random.randint(1, env.maxgenes+1)) org.folder = "breeder1/" arraylist = []#List populated with a number of random arrays of ints of the size #requested by the environment. Arraylist contains x number of these arrays #where x is the number of runs per generation. for i in range(env.runs): arraylist.append(make_list(env.arraysize)) testgene(org, arraylist, env)#Function to test a gene without logging to see if it sorts #within the given pressure threshold. ops = org.avgops #Once an organism that sorts under pressure is found, program begins generation 1. print "\nFirst successful sorter found. Beginning first generation." org.lineage_id = env.lineage_counter#Lineage ID is the number given to any organism if it was #produced randomly and earned a breeder spot. This lets the #log file trace back where a specific organism came from #initially. env.lineage_counter += 1 foldlist = Prep_First_Generation(org, env)#Foldlist holds the folders containing the organisms #to start Generation 1 with. It contains 2 indexes, #the nonrandom folders are index 0, the random folders #are index 2. folders = foldlist[0] rfolders = foldlist[1] arraylist = Run_Gen(folders, rfolders, env)#Array list used in each generation is returned for #logging purposes. Logging cannot be done until a #certain point in the Setup for the next gen. #Loop to run all generations requested try: for x in range(env.gens): Setup_Gen(folders, rfolders, arraylist, env) arraylist = Run_Gen(folders, rfolders, env) except KeyboardInterrupt: if not os.path.exists('./logs/'): os.makedirs('./logs/') log = open("./logs/log-terminated.txt", 'a') log.write(''.join(env.loglist)) log.close() except: raise #Once program is finished, all log files are placed into a new subfolder named using #the run name and a timestamp. This folder contains all the logs and the config file #used for the run. path = env.name + str(int(time.time())) + "/" try: subprocess.call("mkdir ./logs/" + path, shell = True) except: pass subprocess.call("mv ./logs/log* ./logs/" + path, shell = True) subprocess.call("cp ./config.cfg ./logs/" + path, shell = True)