Skip to content

Commit

Permalink
Merge pull request #72 from clarencecastillo/master
Browse files Browse the repository at this point in the history
Rewritten example 13
  • Loading branch information
mauriceling committed Jan 17, 2014
2 parents a7e90ab + cfe9eb4 commit 6490580
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 76 deletions.
2 changes: 1 addition & 1 deletion dose/simulation_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ def simulation_core(sim_functions, sim_parameters, Populations, World):
print '\nSimulation preparation complete...'
while generation_count < max:
generation_count = generation_count + 1
print 'Generation ' + str(generation_count) + ' complete...\r',
sim_functions.ecoregulate(World)
eco_cell_iterator(World, sim_parameters,
sim_functions.update_ecology)
Expand All @@ -100,6 +99,7 @@ def simulation_core(sim_functions, sim_parameters, Populations, World):
(con, cur) = db_report(con, cur, sim_functions,
sim_parameters["starting_time"],
Populations, World, generation_count)
print 'Generation ' + str(generation_count) + ' complete...\r',
print '\nClosing simulation results...'
for pop_name in Populations: close_results(sim_parameters, pop_name)
if sim_parameters.has_key("database_file") and \
Expand Down
116 changes: 41 additions & 75 deletions examples/13_no_migration_natural_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,38 @@
# Example codes starts from here
import dose, genetic, random
from collections import Counter
from copy import deepcopy

parameters = {
"simulation_name": "example_13",
"population_names": ['pop_01'],
"population_locations": [[(x,0,0) for x in xrange(5)]],
"initial_chromosome": ['1','0'] * 50,
"deployment_code": 3,
"population_locations": [[(0,0,0)]],
"initial_chromosome": ['1','0'] * 250,
"deployment_code": 1,
"chromosome_bases": ['0','1'],
"background_mutation": 0.1,
"background_mutation": 0.01,
"additional_mutation": 0.00,
"mutation_type": 'point',
"chromosome_size": 100,
"chromosome_size": 500,
"genome_size": 1,
"max_tape_length": 50,
"clean_cell": True,
"interpret_chromosome": False,
"max_codon": 2000,
"population_size": 500,
"eco_cell_capacity": 100,
"world_x": 5,
"population_size": 100,
"eco_cell_capacity": 0,
"world_x": 1,
"world_y": 1,
"world_z": 1,
"goal": 55,
"maximum_generations": 200,
"goal": 50,
"maximum_generations": 300,
"fossilized_ratio": 0.01,
"fossilized_frequency": 50,
"print_frequency": 1,
"ragaraja_version": 0,
"ragaraja_instructions": ['000', '001', '010',
'011', '100', '101'],
"eco_buried_frequency": 200,
"eco_buried_frequency": 300,
"database_file": "sim13_no_migration.db",
"database_logging_frequency": 1
}
Expand All @@ -72,6 +73,7 @@ def report(self, World): pass

def fitness(self, Populations, pop_name):
for organism in Populations[pop_name].agents:
final_fitness = []
chromosome = organism.genome[0].sequence
zero_count = []
for base_index in xrange(parameters["chromosome_size"] - 1):
Expand All @@ -81,81 +83,45 @@ def fitness(self, Populations, pop_name):
next_index += 1
if (next_index + base_index) == parameters["chromosome_size"]: break
zero_count.append(next_index - 1)
organism.status['fitness'] = max(zero_count)

def mutation_scheme(self, organism):
if organism.status['fitness'] != parameters["goal"]:
organism.genome[0].rmutate(parameters["mutation_type"],
parameters["additional_mutation"])

def prepopulation_control(self, Populations, pop_name):
for location in parameters["population_locations"][0]:
group = dose.filter_location(location, Populations[pop_name].agents)
average_fitness = sum([organism.status["fitness"] for organism in group])/len(group)
if average_fitness != parameters["goal"]:
alpha_organism_fitness = 0
for organism in group:
if abs(parameters["goal"] - organism.status['fitness']) < \
abs(parameters["goal"] - alpha_organism_fitness):
alpha_organism_fitness = int(organism.status['fitness'])
for organism in group:
if organism.status['fitness'] not in xrange(alpha_organism_fitness - 2,
alpha_organism_fitness + 2):
Populations[pop_name].agents.remove(organism)
else:
if organism.status['fitness'] != parameters["goal"]:
Populations[pop_name].agents.remove(organism)
for sequence in xrange(len(zero_count)):
if len(final_fitness) == 10: break
seq_score = sorted(zero_count, reverse = True)[sequence]
if seq_score > 5:
seq_score = 5
final_fitness.append(seq_score)
organism.status['fitness'] = sum(final_fitness)

def mutation_scheme(self, organism):
organism.genome[0].rmutate(parameters["mutation_type"],
parameters["additional_mutation"])

def prepopulation_control(self, Populations, pop_name): pass

def mating(self, Populations, pop_name):
for location in parameters["population_locations"][0]:
group = dose.filter_location(location, Populations[pop_name].agents)
for x in xrange(parameters["eco_cell_capacity"] - len(group)):
parents = []
alpha_organism = group[0]
for organism in group:
if abs(parameters["goal"] - organism.status['fitness']) < \
abs(parameters["goal"] - alpha_organism.status['fitness']):
alpha_organism = organism
parents.append(alpha_organism)
parents.append(random.choice(group))
crossover_pt = random.randint(0, len(parents[0].genome[0].sequence))
(new_chromo1, new_chromo2) = genetic.crossover(parents[0].genome[0],
parents[1].genome[0],
crossover_pt)
child = genetic.Organism([new_chromo1],
parameters["mutation_type"],
parameters["additional_mutation"])
child.status['parents'] = [parents[0].status['identity'],
parents[1].status['identity']]
child.status['location'] = location
child.generate_name()
child.status['deme'] = pop_name
Populations[pop_name].agents.append(child)
group = deepcopy(Populations[pop_name].agents)
for organism in group:
organism.generate_name()
Populations[pop_name].agents.append(organism)

def postpopulation_control(self, Populations, pop_name):
for location in parameters["population_locations"][0]:
group = dose.filter_location(location, Populations[pop_name].agents)
average_fitness = sum([organism.status["fitness"] for organism in group])/len(group)
if average_fitness != parameters["goal"]:
omega_organism_fitness = 0
for organism in group:
if abs(parameters["goal"] - organism.status['fitness']) > \
abs(parameters["goal"] - omega_organism_fitness):
omega_organism_fitness = int(organism.status['fitness'])
for organism in group:
if organism.status['fitness'] in xrange(omega_organism_fitness - 1,
omega_organism_fitness + 1):
Populations[pop_name].agents.remove(organism)
else:
if organism.status['fitness'] != parameters["goal"]:
group = deepcopy(Populations[pop_name].agents)
fitness_dict = {}
for organism in group:
fitness_dict[organism.status['identity']] = int(organism.status['fitness'])
sorted_fitness = sorted(fitness_dict.items(), key=lambda x: x[1])
for index_pair in sorted_fitness:
if len(Populations[pop_name].agents) == len(group)/2: break
for organism in Populations[pop_name].agents:
if organism.status['identity'] == index_pair[0]:
Populations[pop_name].agents.remove(organism)
break;

def generation_events(self, Populations, pop_name): pass

def population_report(self, Populations, pop_name):
report_list = []
for organism in Populations[pop_name].agents:
chromosome = ''.join(organism.genome[0].sequence)
chromosome = organism.status['identity']
fitness = str(organism.status['fitness'])
report_list.append(chromosome + ' ' + fitness)
return '\n'.join(report_list)
Expand Down

0 comments on commit 6490580

Please sign in to comment.