def interpret_chromosome(sim_parameters, populations, pop_name, world): ''' Function to call Ragaraja interpreter to express / execute the genome for each organism in a population. The Turing tape (array) after execution will be logged as "blood" of each organism. Ragaraja interpreter will use the temporary_input and temporary_output lists from each ecological cell as the input data and output data respectively for genome execution - this will resemble consumption of environmental resources and replenishing of environmental resources or dumping of wastes respectively. @param sim_parameters: simulation parameters dictionary (see Examples) @param populations: dictionary of population objects @param pop_name: population name @param world: dose_world.World object @return: none ''' chromosome = None for i in range(len(populations[pop_name].agents)): individual = populations[pop_name].agents[i] location = individual.status['location'] (x, y, z) = coordinates(location) if sim_parameters["clean_cell"]: chromosome = [0] * sim_parameters["max_tape_length"] else: chromosome = populations[pop_name].agents[i].status['blood'] if chromosome is None: chromosome = [0] * sim_parameters["max_tape_length"] for chromosome_count in range(len(individual.genome)): inputdata = world.ecosystem[x][y][z]['local_input'] output = world.ecosystem[x][y][z]['local_output'] source = ''.join(individual.genome[chromosome_count].sequence) chromosome = populations[pop_name].agents[i].status['blood'] try: (chromosome, apointer, inputdata, output, source, spointer) = \ register_machine.interpret(source, ragaraja.ragaraja, 3, inputdata, chromosome, sim_parameters["max_tape_length"], sim_parameters["max_codon"]) except Exception as e: error_msg = '|'.join(['Error at Chromosome_' + str(chromosome_count), str(e)]) populations[pop_name].agents[i].status['chromosome_error'] = error_msg populations[pop_name].agents[i].status['blood'] = chromosome populations[pop_name].agents[i].status['blood'] = chromosome world.ecosystem[x][y][z]['temporary_input'] = inputdata world.ecosystem[x][y][z]['temporary_output'] = output
def comparator(data, result): if data == result: return 'Passed' else: return 'FAILED' for t in tests: isource = source + testdata[t]['in_source'] oarray = testdata[t]['array'] oapointer = testdata[t]['apointer'] oinputdata = testdata[t]['inputdata'] ooutput = testdata[t]['output'] osource = testdata[t]['out_source'] ospointer = testdata[t]['spointer'] array = [0]*10 (array, apointer, inputdata, output, source, spointer) = r.interpret(isource, N.nBF, 1, [], array, 10) print(' '.join(['Test number:', str(t), ', Original source code:', str(source)])) print(' '.join([' ', str(comparator(oarray, array)), 'array.', 'Expected array:', str(oarray), 'Actual array:', str(array)])) print(' '.join([' ', str(comparator(oapointer, apointer)), 'array pointer.', 'Expected array pointer:', str(oapointer), 'Actual array pointer:', str(apointer)])) print(' '.join([' ', str(comparator(oinputdata, inputdata)), 'input data list.', 'Expected input data list:', str(oinputdata), 'Actual input data list:', str(inputdata)])) print(' '.join([' ', str(comparator(ooutput, output)), 'output list.',
return forward(array, apointer, inputdata, output, source, spointer) elif source[spointer] == "N" and r >= 0.75: if apointer == 0: return (array, len(array) - 1, inputdata, output, source, spointer) else: return backward(array, apointer, inputdata, output, source, spointer) nBF = { "A": increment, "T": decrement, "G": forward, "C": backward, "R": random_op, "Y": random_op, "S": random_op, "W": random_op, "K": random_op, "M": random_op, "B": random_op, "D": random_op, "H": random_op, "V": random_op, "N": random_op, ".": call_out, } if __name__ == "__main__": print(r.interpret("AAAAGGTTTCAAA", nBF)) print(r.interpret("AAAAGGTTTCAAARRYYSKVDVDBBHVNVH", nBF))
def simulate(entity_module): ''' Simulate the entities in DOSE. @param entity_module: python module of entities to execute. Required classes in entity_module are Chromosome (inherited from genetic.Chromosome), Organism (inherited from genetic.Organism), Population (inherited from genetic.Population) and World (inherited from dose_world.World). @since: version 0.4.1 ''' exec('from %s import World, Population' % entity_module) populations = {} world = World() for i in range(len(population_names)): populations[population_names[i]] = Population() L = population_locations[i] world.ecosystem[L[0]][L[1]][L[2]]['organisms'] = \ len(populations[population_names[i]].agents) for x in range(len(populations[population_names[i]].agents)): populations[population_names[i]].agents[x].status['location'] = L ######################################################################## # Default Simulation Driver # # (do not change anything above this line) # ######################################################################## generation_count = 0 while generation_count < maximum_generations: generation_count = generation_count + 1 ''' Run World.ecoregulate function ''' world.ecoregulate() ''' For each ecological cell, run World.update_ecology and World.update_local functions ''' for x in range(world.world_x): for y in range(world.world_y): for z in range(world.world_z): world.update_ecology(x, y, z) world.update_local(x, y, z) ''' For each organism Execute genome by Ragaraja interpreter using existing cytoplasm, local conditions as input Update cytoplasm (Organism.cytoplasm) Add input/output from organism intermediate condition of local cell ''' for name in population_names: for i in range(len(populations[name].agents)): source = populations[name].agents[i].genome[0].sequence source = ''.join(source) if clean_cytoplasm: array = [0] * cytoplasm_size else: array = populations[name].agents[i].cytoplasm L = populations[name].agents[i].status['location'] inputdata = world.ecosystem[L[0]][L[1]][L[2]]['local_input'] try: (array, apointer, inputdata, output, source, spointer) = \ r.interpret(source, N.ragaraja, 3, inputdata, array, max_cytoplasm_size, max_codon) except IndexError: pass except ZeroDivisionError: pass except OverflowError: pass except ValueError: pass populations[name].agents[i].cytoplasm = array world.ecosystem[L[0]][L[1]][ L[2]]['temporary_input'] = inputdata world.ecosystem[L[0]][L[1]][L[2]]['temporary_output'] = output ''' For each population Run Population.prepopulation_control function Run Population.mating function and add new organisms to cell For each organism, run Organism.mutation_scheme function Run Population.generation_events function Add 1 to generation count Run Population.report function Fossilize population if needed ''' for name in population_names: report = populations[name].generation_step() if generation_count % int(fossilized_frequency) == 0: ffile = fossil_files[name] + '_' populations[name].freeze(ffile, fossilized_ratio) if generation_count % int(print_frequency) == 0: print(str(generation_count), str(report)) f = open(result_files[name] + '.result.txt', 'a') dtstamp = str(datetime.utcnow()) f.write('\t'.join( [dtstamp, str(generation_count), str(report)])) f.write('\n') f.close() ''' For each ecological cell Run World.organism_movement function Run World.organism_location function Run World.report function ''' for x in range(world.world_x): for y in range(world.world_y): for z in range(world.world_z): world.organism_movement(x, y, z) world.organism_location(x, y, z) world.report() ''' Bury ecosystem if needed ''' if generation_count % int(eco_buried_frequency) == 0: filename = eco_burial_file + '_' + str(generation_count) + '.eco' world.eco_burial(filename)
def simulate(entity_module): exec('from %s import World, Population' % entity_module) populations = {} world = World() for i in range(len(population_names)): populations[population_names[i]] = Population() L = population_locations[i] world.ecosystem[L[0]][L[1]][L[2]]['organisms'] = \ len(populations[population_names[i]].agents) for x in range(len(populations[population_names[i]].agents)): populations[population_names[i]].agents[x].status['location'] = L ######################################################################## # Default Simulation Driver # # (do not change anything above this line) # ######################################################################## generation_count = 0 while generation_count < maximum_generations: generation_count = generation_count + 1 ''' Run World.ecoregulate function ''' world.ecoregulate() ''' For each ecological cell, run World.update_ecology and World.update_local functions ''' for x in range(world.world_x): for y in range(world.world_y): for z in range(world.world_z): world.update_ecology(x, y, z) world.update_local(x, y, z) ''' For each organism Execute genome by Ragaraja interpreter using existing cytoplasm, local conditions as input Update cytoplasm (Organism.cytoplasm) Add input/output from organism intermediate condition of local cell ''' for name in population_names: for i in range(len(populations[name].agents)): source = populations[name].agents[i].genome[0].sequence source = ''.join(source) if clean_cytoplasm: array = [0]*cytoplasm_size else: array = populations[name].agents[i].cytoplasm L = populations[name].agents[i].status['location'] inputdata = world.ecosystem[L[0]][L[1]][L[2]]['local_input'] try: (array, apointer, inputdata, output, source, spointer) = \ r.interpret(source, N.ragaraja, 3, inputdata, array, max_cytoplasm_size, max_codon) except IndexError: pass except ZeroDivisionError: pass except OverflowError: pass except ValueError: pass populations[name].agents[i].cytoplasm = array world.ecosystem[L[0]][L[1]][L[2]]['temporary_input'] = inputdata world.ecosystem[L[0]][L[1]][L[2]]['temporary_output'] = output ''' For each population Run Population.prepopulation_control function Run Population.mating function and add new organisms to cell For each organism, run Organism.mutation_scheme function Run Population.generation_events function Add 1 to generation count Run Population.report function Fossilize population if needed ''' for name in population_names: report = populations[name].generation_step() if generation_count % int(fossilized_frequency) == 0: ffile = fossil_files[name] + '_' populations[name].freeze(ffile, fossilized_ratio) if generation_count % int(print_frequency) == 0: print(str(generation_count), str(report)) f = open(result_files[name] + '.result.txt', 'a') dtstamp = str(datetime.utcnow()) f.write('\t'.join([dtstamp, str(generation_count), str(report)])) f.write('\n') f.close() ''' For each ecological cell Run World.organism_movement function Run World.organism_location function Run World.report function ''' for x in range(world.world_x): for y in range(world.world_y): for z in range(world.world_z): world.organism_movement(x, y, z) world.organism_location(x, y, z) world.report() ''' Bury ecosystem if needed ''' if generation_count % int(eco_buried_frequency) == 0: filename = eco_burial_file + '_' + str(generation_count) + '.eco' world.eco_burial(filename)
try: while count > 0: spointer = spointer - 1 if source[spointer] == ']': count = count + 1 if source[spointer] == '[': count = count - 1 except IndexError: spointer = temp return (array, apointer, inputdata, output, source, spointer) LCBF = { '+': increment, '-': decrement, '>': forward, '<': backward, '.': call_out, ',': accept_predefined, '[': cbf_start_loop, ']': cbf_end_loop, } if __name__ == '__main__': print(r.interpret('++++++++++[>+++++<.-]', LCBF)) print(r.interpret('++[>+++++<.-]>>>+++.', LCBF)) print(r.interpret('++>+++++<.-]>>>+++.', LCBF)) print(r.interpret('++>[+++++<.->>>+++.', LCBF)) print(r.interpret('+++++[>++++[>+++.<-].<-]', LCBF)) print(r.interpret('>>>>>>++', LCBF, 1, [], None, 5))
def comparator(data, result): if data == result: return 'Passed' else: return 'FAILED' for t in tests: isource = source + testdata[t]['in_source'] oarray = testdata[t]['array'] oapointer = testdata[t]['apointer'] oinputdata = testdata[t]['inputdata'] ooutput = testdata[t]['output'] osource = testdata[t]['out_source'] ospointer = testdata[t]['spointer'] array = [0] * 10 (array, apointer, inputdata, output, source, spointer) = r.interpret(isource, N.nBF, 1, [], array, 10) print(' '.join( ['Test number:', str(t), ', Original source code:', str(source)])) print(' '.join([ ' ', str(comparator(oarray, array)), 'array.', 'Expected array:', str(oarray), 'Actual array:', str(array) ])) print(' '.join([ ' ', str(comparator(oapointer, apointer)), 'array pointer.', 'Expected array pointer:', str(oapointer), 'Actual array pointer:',
try: while count > 0: spointer = spointer - 1 if source[spointer] == "]": count = count + 1 if source[spointer] == "[": count = count - 1 except IndexError: spointer = temp return (array, apointer, inputdata, output, source, spointer) LCBF = { "+": increment, "-": decrement, ">": forward, "<": backward, ".": call_out, ",": accept_predefined, "[": cbf_start_loop, "]": cbf_end_loop, } if __name__ == "__main__": print(r.interpret("++++++++++[>+++++<.-]", LCBF)) print(r.interpret("++[>+++++<.-]>>>+++.", LCBF)) print(r.interpret("++>+++++<.-]>>>+++.", LCBF)) print(r.interpret("++>[+++++<.->>>+++.", LCBF)) print(r.interpret("+++++[>++++[>+++.<-].<-]", LCBF)) print(r.interpret(">>>>>>++", LCBF, 1, [], None, 5))
elif source[spointer] == 'N' and r >= 0.75: if apointer == 0: return (array, len(array) - 1, inputdata, output, source, spointer) else: return backward(array, apointer, inputdata, output, source, spointer) nBF = { 'A': increment, 'T': decrement, 'G': forward, 'C': backward, 'R': random_op, 'Y': random_op, 'S': random_op, 'W': random_op, 'K': random_op, 'M': random_op, 'B': random_op, 'D': random_op, 'H': random_op, 'V': random_op, 'N': random_op, '.': call_out } if __name__ == '__main__': print r.interpret('AAAAGGTTTCAAA', nBF) print r.interpret('AAAAGGTTTCAAARRYYSKVDVDBBHVNVH', nBF)
else: return forward(array, apointer, inputdata, output, source, spointer) elif source[spointer] == 'N' and r >= 0.75: if apointer == 0: return (array, len(array) - 1, inputdata, output, source, spointer) else: return backward(array, apointer, inputdata, output, source, spointer) nBF = {'A': increment, 'T': decrement, 'G': forward, 'C': backward, 'R': random_op, 'Y': random_op, 'S': random_op, 'W': random_op, 'K': random_op, 'M': random_op, 'B': random_op, 'D': random_op, 'H': random_op, 'V': random_op, 'N': random_op, '.': call_out } if __name__ == '__main__': print(r.interpret('AAAAGGTTTCAAA', nBF)) print(r.interpret('AAAAGGTTTCAAARRYYSKVDVDBBHVNVH', nBF))
else: count = 1 try: while count > 0: spointer = spointer - 1 if source[spointer] == ']': count = count + 1 if source[spointer] == '[': count = count - 1 except IndexError: spointer = temp return (array, apointer, inputdata, output, source, spointer) LCBF = {'+': increment, '-': decrement, '>': forward, '<': backward, '.': call_out, ',': accept_predefined, '[': cbf_start_loop, ']': cbf_end_loop, } if __name__ == '__main__': print(r.interpret('++++++++++[>+++++<.-]', LCBF)) print(r.interpret('++[>+++++<.-]>>>+++.', LCBF)) print(r.interpret('++>+++++<.-]>>>+++.', LCBF)) print(r.interpret('++>[+++++<.->>>+++.', LCBF)) print(r.interpret('+++++[>++++[>+++.<-].<-]', LCBF)) print(r.interpret('>>>>>>++', LCBF, 1, [], None, 5))
except KeyError: array = [0]*10 # Step 4: Get expected results after execution oarray = testdata[t]['array'] # tape (array) after execution oapointer = testdata[t]['apointer'] # tape pointer oinputdata = testdata[t]['inputdata'] # input data list ooutput = testdata[t]['output'] # output list osource = testdata[t]['out_source'] # source ospointer = testdata[t]['spointer'] # source pointer # ---------------------------- # ------- EXECUTE TEST ------- # ---------------------------- (array, apointer, inputdata, output, source, spointer) = \ r.interpret(isource, N.ragaraja, 3, inputdata, array, 10) # --------------------------------------------------- # ------- COMPARE EXPECTED RESULTS AFTER TEST ------- # --------------------------------------------------- print ' '.join(['Test number:', str(t), ', Original source code:', str(isource)]) print ' '.join([' ', str(comparator(oarray, array)), 'array.', 'Expected array:', str(oarray), 'Actual array:', str(array)]) print ' '.join([' ', str(comparator(oapointer, apointer)), 'array pointer.', 'Expected array pointer:', str(oapointer), 'Actual array pointer:', str(apointer)]) print ' '.join([' ', str(comparator(oinputdata, inputdata)),