def setUp(self): self.ind_1 = Individual.Individual("01") self.ind_2 = Individual.Individual("02") self.ind_3 = Individual.Individual("03") self.fam_1 = Family.Family("01") self.fam_2 = Family.Family("02")
def tearDown(self): self.ind_1 = Individual.Individual("01") self.ind_2 = Individual.Individual("02") self.ind_3 = Individual.Individual("03") self.fam_1 = Family.Family("01") self.fam_2 = Family.Family("02")
def run_algorithm(sched, n=NUM_GENERATIONS, m=POP_SIZE): """ Runs the genetic algorithm over n generations with a population of size m using the provided schedule constraint and returns the found solution. """ # generate an initial population of individuals population = [] for i in range(m): soln = gen_rand_solution(sched.assignments) population.append(Individual(soln, fitness(soln, sched))) # evolve over n generations! for i in range(n): new_gen = [] for i in range(m): # select 2 parents via tournament selection and breed them to # create a child for the new generation parents = tournament(population) child_soln = crossover(parents[0].todos, parents[1].todos) child = Individual(child_soln, fitness(child_soln, sched)) new_gen.append(child) population = new_gen # sort the final population by fitness and return the best one population.sort(key=lambda indiv: indiv.fitness, reverse=True) return population[0]
def create_model(cpr, tmm, poi, dmn, dmx): model_map = collections.defaultdict(list) infected = poi #Creates young people for element in range(round(int(cpr) * 0.9045)): x = random.randint(0, tmm - 1) y = random.randint(0, tmm - 1) element = Individual((x, y), True) if infected > poi - (poi * 0.9045): element.infect(dmn, dmx) infected -= 1 key = element.get_destination() if key in model_map: model_map[key].append(element) else: model_map[key] = [element] #Creates old people for element in range(round(int(cpr) * 0.0955)): x = random.randint(0, tmm - 1) y = random.randint(0, tmm - 1) element = Individual((x, y), False) if infected > 0: element.infect(dmn, dmx) infected -= 1 key = element.get_destination() if key in model_map: model_map[key].append(element) else: model_map[key] = [element] return model_map
def test_were_parents_over_14(self): """ test were_parents_over_14 method """ # husband is 20 and wife is 14 at the marriage date -> Both are over 14 -> True husband: Individual = Individual(_id="I0", birt={'date': "19 SEP 1995"}) wife: Individual = Individual(_id="I1", birt={'date': "3 JAN 2000"}) individuals: List[Individual] = [husband, wife] family: Family = Family(_id="F0", husb=husband.id, wife=wife.id, marr={'date': "11 FEB 2015"}) self.assertTrue(us.were_parents_over_14(family, individuals)) # husband 11, wife 20 -> Only wife is over 14 -> False husband: Individual = Individual(_id="I2", birt={'date': "2 MAR 2007"}) wife: Individual = Individual(_id="I3", birt={'date': "11 FEB 2000"}) individuals: List[Individual] = [husband, wife] family: Family = Family(_id="F1", husb=husband.id, wife=wife.id, marr={'date': "11 FEB 2019"}) self.assertFalse(us.were_parents_over_14(family, individuals)) # husband 17, wife 10 -> Only husband is over 14 -> False husband: Individual = Individual(_id="I4", birt={'date': "22 AUG 2000"}) wife: Individual = Individual(_id="I5", birt={'date': "5 DEC 2007"}) individuals: List[Individual] = [husband, wife] family: Family = Family(_id="F2", husb=husband.id, wife=wife.id, marr={'date': "11 FEB 2018"}) self.assertFalse(us.were_parents_over_14(family, individuals)) # husband 12, wife 12 -> Both are under 14 -> False husband: Individual = Individual(_id="I6", birt={'date': "19 SEP 2007"}) wife: Individual = Individual(_id="I7", birt={'date': "3 JAN 2008"}) individuals: List[Individual] = [husband, wife] family: Family = Family(_id="F3", husb=husband.id, wife=wife.id, marr={'date': "11 FEB 2020"}) self.assertFalse(us.were_parents_over_14(family, individuals)) # husband 18, wife 16 -> Both are over 14 -> True husband: Individual = Individual(_id="I8", birt={'date': "7 FEB 1980"}) wife: Individual = Individual(_id="I9", birt={'date': "8 FEB 1982"}) individuals: List[Individual] = [husband, wife] family: Family = Family(_id="F4", husb=husband.id, wife=wife.id, marr={'date': "11 FEB 1998"}) self.assertTrue(us.were_parents_over_14(family, individuals))
def parse_single_individual(gedlist, index, xref): """ Parses a single individual from a GEDCOM giving the starting index of an 'INDI' tag. Returns an Individual class """ indiv = Individual(xref) date_type = None for gedline in gedlist[index + 1:]: if gedline.level == 0: break if gedline.tag == "NAME": indiv.name = gedline.args if gedline.tag == "SEX": indiv.sex = gedline.args[0] if gedline.tag == "BIRT": date_type = "BIRT" if gedline.tag == "DEAT": date_type = "DEAT" if gedline.tag == "FAMC": indiv.famc.append(gedline.args[0]) if gedline.tag == "FAMS": indiv.fams.append(gedline.args[0]) # This assumes the following date tag corresponds to prev tag if gedline.tag == "DATE": if date_type == "BIRT": # Store birthdate as datetime object indiv.birthdate = datetime( int(gedline.args[2]), datetime.strptime(gedline.args[1], '%b').month, int(gedline.args[0])) date_type = None elif date_type == "DEAT": # Store death as datetime object indiv.death = datetime( int(gedline.args[2]), datetime.strptime(gedline.args[1], '%b').month, int(gedline.args[0])) date_type = None else: print "ERROR" return indiv
def generate_classes( lines: List[str]) -> Tuple[List[Individual], List[Family]]: """ get lines read from a .ged file """ individuals: List[Individual] = [] families: List[Family] = [] current_record: Optional[Individual, Family] = None current_tag: Optional[str] = None for line in lines: row_fields: List[str] = line.rstrip("\n").split(' ', 2) pattern_type = pattern_finder(line) if pattern_type == 'ZERO_1': current_record = Individual( ) if row_fields[2] == 'INDI' else Family() (individuals if isinstance(current_record, Individual) else families) \ .append(current_record) current_record.id = row_fields[1] elif pattern_type == 'ZERO_2': pass # nothing to do with this elif pattern_type == 'NO_ARGUMENT': if row_fields[0] == '1': setattr(current_record, row_fields[1].lower(), {}) current_tag = row_fields[1].lower() elif pattern_type == 'ARGUMENT': if row_fields[0] == '1': if isinstance(getattr(current_record, row_fields[1].lower()), list): current_list = getattr( current_record, row_fields[1].lower()) + [row_fields[2]] setattr(current_record, row_fields[1].lower(), current_list) else: setattr(current_record, row_fields[1].lower(), row_fields[2]) elif row_fields[0] == '2': setattr(current_record, current_tag, {row_fields[1].lower(): row_fields[2]}) return individuals, families
def mate(individual1: Individual, individual2: Individual) -> Individual: mutation_probability = random.choice( (individual1.mutation_probability, individual2.mutation_probability)) genome = set() for genes in zip_longest(individual1.genome, individual2.genome): if None in genes: genes = [g for g in genes if g] gene: Gene = random.choice(genes) if random.random() <= mutation_probability: gene = mutate(gene) genome.add(gene) if random.random() < mutation_probability: genome.add(Gene()) return Individual( mutate_float(mutation_probability, mutation_probability), random.choice( (individual1.consumption_rate, individual2.consumption_rate)), genome, )
def indi_stmt(self, *ts): (args, ) = ts individual = Individual.from_args(args) self.individuals[individual.id_] = individual return individual
i = 1 for key in err_list: err_chart.add_row([i, err_list[key]]) i += 1 write_file.write("Individual\n") write_file.write(indi_chart.get_string() + "\n") write_file.write("Family\n") write_file.write(fam_chart.get_string() + "\n") write_file.write("Error\n") if (i == 1): write_file.write("\nNo Errors found!") else: write_file.write(err_chart.get_string()) write_file.close() if __name__ == "__main__": from models import Individual from models import Family indi_1 = Individual.Individual("01") indi_2 = Individual.Individual("02") fam_1 = Family.Family("10") fam_2 = Family.Family("20") indi_1.add_to_family(fam_1) indi_2.add_to_family(fam_2) fam_1.set_husband(indi_1) fam_1.set_wife(indi_2) fam_2.set_husband(indi_2) fam_2.set_wife(indi_1) print_pretty_table([indi_1, indi_2], [fam_1, fam_2], [])
return len(subject.genome.intersection(object_.genome)) * fitness def choose_partner(individual: Individual, population: Population, fitnesses: Dict[Individual, float]): return max(population, key=lambda i: evaluate_attractiveness( individual, i, fitnesses.get(individual, 0))) def consume_resources(cosmos: Cosmos) -> Tuple[Population, float]: survivors = [] resources = cosmos.resources for individual in sorted(cosmos.population, key=lambda i: evaluate(cosmos.environment, i)): if resources >= individual.consumption_rate: resources -= individual.consumption_rate survivors.append(individual) else: break return survivors, resources if __name__ == '__main__': i1 = Individual() i2 = Individual() i3 = mate(i1, i2) print(i1, i2, i3, sep='\n') env = [Constraint() for _ in range(10)] print(evaluate(env, i3))
def test_birth_before_death_of_parents(self): """ test birth_before_death_of_parents method """ # mother and father are alive (no death date) husband: Individual = Individual(_id="I0") wife: Individual = Individual(_id="I1") child: Individual = Individual(_id="I2", birt={'date': "4 OCT 2000"}) individuals: List[Individual] = [husband, wife, child] family: Family = Family(_id="F0", husb=husband.id, wife=wife.id) family.chil.append(child.id) self.assertTrue(us.birth_before_death_of_parents(family, individuals)) # child born on: mother death, 270 day after father death husband: Individual = Individual(_id="I0", deat={'date': "8 JAN 2000"}) wife: Individual = Individual(_id="I1", deat={'date': "4 OCT 2000"}) child: Individual = Individual(_id="I2", birt={'date': "4 OCT 2000"}) individuals: List[Individual] = [husband, wife, child] family: Family = Family(_id="F1", husb=husband.id, wife=wife.id) family.chil.append(child.id) self.assertTrue(us.birth_before_death_of_parents(family, individuals)) # child born on: 1 day before mother death, 1 day after father death husband: Individual = Individual(_id="I0", deat={'date': "3 OCT 2000"}) wife: Individual = Individual(_id="I1", deat={'date': "5 OCT 2000"}) child: Individual = Individual(_id="I2", birt={'date': "4 OCT 2000"}) individuals: List[Individual] = [husband, wife, child] family: Family = Family(_id="F2", husb=husband.id, wife=wife.id) family.chil.append(child.id) self.assertTrue(us.birth_before_death_of_parents(family, individuals)) # child born on: after mother death, 10 day after father death husband: Individual = Individual(_id="I0", deat={'date': "3 OCT 2000"}) wife: Individual = Individual(_id="I1", deat={'date': "12 OCT 2000"}) child: Individual = Individual(_id="I2", birt={'date': "13 OCT 2000"}) individuals: List[Individual] = [husband, wife, child] family: Family = Family(_id="F3", husb=husband.id, wife=wife.id) family.chil.append(child.id) self.assertFalse(us.birth_before_death_of_parents(family, individuals)) # child born on: before mother death, 1 year after father death husband: Individual = Individual(_id="I0", deat={'date': "11 OCT 1999"}) wife: Individual = Individual(_id="I1", deat={'date': "12 OCT 2000"}) child: Individual = Individual(_id="I2", birt={'date': "11 OCT 2000"}) individuals: List[Individual] = [husband, wife, child] family: Family = Family(_id="F4", husb=husband.id, wife=wife.id) family.chil.append(child.id) self.assertFalse(us.birth_before_death_of_parents(family, individuals))
def test_less_than_150(self): individual = Individual(birt={'date': "20 Mar 1985"}) individual.deat = {'date': "15 Aug 2008"} self.assertTrue(us.less_than_150(individual)) individual = Individual(birt={'date': "15 JAN 2000"}) self.assertTrue(us.less_than_150(individual)) individual = Individual(birt={'date': "15 Feb 2012"}) individual.deat = {'date': "21 JAN 2000"} self.assertFalse(us.less_than_150(individual)) individual = Individual(birt={'date': "15 JAN 1500"}) self.assertFalse(us.less_than_150(individual)) individual = Individual(birt={'date': "15 JAN 2006"}) individual.deat = {'date': "15 JAN 1200"} self.assertFalse(us.less_than_150(individual))
def test_divorce_before_death(self): husband: Individual = Individual(_id="I0", deat={'date': "1 JAN 2020"}) wife: Individual = Individual(_id="I1", deat={'date': "2 JAN 2019"}) individuals: List[Individual] = [husband, wife] family: Family = Family(husb=husband.id, wife=wife.id, div={'date': "11 FEB 1999"}) self.assertTrue(us.divorce_before_death(family, individuals)) """Husband and wife they both die before their divorce so False (not possible)""" husband: Individual = Individual(_id="I2", deat={'date': "1 JAN 2000"}) wife: Individual = Individual(_id="I3", deat={'date': "2 JAN 2000"}) individuals: List[Individual] = [husband, wife] family: Family = Family(husb=husband.id, wife=wife.id, div={'date': "11 FEB 2001"}) self.assertFalse(us.divorce_before_death(family, individuals)) """ Husband dies before divorce wife is alive so False (not possible)""" husband: Individual = Individual(_id="I4", deat={'date': "1 JAN 2000"}) wife: Individual = Individual(_id="I5") individuals: List[Individual] = [husband, wife] family: Family = Family(husb=husband.id, wife=wife.id, div={'date': "11 FEB 2001"}) self.assertFalse(us.divorce_before_death(family, individuals)) """ Husband dies after divorce and wife is alive so true""" husband: Individual = Individual(_id="I4", deat={'date': "1 JAN 2002"}) wife: Individual = Individual(_id="I5") individuals: List[Individual] = [husband, wife] family: Family = Family(husb=husband.id, wife=wife.id, div={'date': "11 FEB 2001"}) self.assertTrue(us.divorce_before_death(family, individuals)) """Wife dies before divorce and Husband is alive so False (not possible)""" husband: Individual = Individual(_id="I6") wife: Individual = Individual(_id="I7", deat={'date': "1 JAN 2000"}) individuals: List[Individual] = [husband, wife] family: Family = Family(husb=husband.id, wife=wife.id, div={'date': "11 FEB 2001"}) self.assertFalse(us.divorce_before_death(family, individuals)) """Wife dies after divorce and Husband is alive so true""" husband: Individual = Individual(_id="I8") wife: Individual = Individual(_id="I9", deat={'date': "1 JAN 2002"}) individuals: List[Individual] = [husband, wife] family: Family = Family(husb=husband.id, wife=wife.id, div={'date': "11 FEB 2001"}) self.assertTrue(us.divorce_before_death(family, individuals)) """ Husband and wife they both are alive no divorce date is found so false""" husband: Individual = Individual(_id="I10") wife: Individual = Individual(_id="I11") individuals: List[Individual] = [husband, wife] family: Family = Family(husb=husband.id, wife=wife.id, div={'date': "11 FEB 2002"}) self.assertFalse(us.divorce_before_death(family, individuals))
def test_male_last_names(self): husband: Individual = Individual(_id="I0", name="Pablo /Escobar/", sex='M') wife: Individual = Individual(_id="I1", name="Veronika /Esco/", sex='F') child1: Individual = Individual(_id="I2", name="Terry /Escobart/", sex='M') child2: Individual = Individual(_id="I3", name="Maria /Escobar/", sex='F') family = Family(husb=husband.id, wife=wife.id) family.chil = [child1.id, child2.id] individuals = [husband, wife, child1, child2] self.assertFalse(us.male_last_names(family, individuals)) husband: Individual = Individual(_id="I112", name="Naal /Wagas/", sex='M') wife: Individual = Individual(_id="I22", name="Veron /Wagadi/", sex='F') child1: Individual = Individual(_id="I33", name="Ter /Wagada/", sex='M') child2: Individual = Individual(_id="I44", name="Mara /Wagadi/", sex='F') family = Family(husb=husband.id, wife=wife.id) family.chil = [child1.id, child2.id] individuals = [husband, wife, child1, child2] self.assertFalse(us.male_last_names(family, individuals)) husband: Individual = Individual(_id="I9", name="Eden /Hazard/", sex='M') wife: Individual = Individual(_id="I99", name="Veva /Hazard/", sex='F') child1: Individual = Individual(_id="I999", name="JR /Hazard/", sex='M') child2: Individual = Individual(_id="I9999", name="SR /Hazard/", sex='M') family = Family(husb=husband.id, wife=wife.id) family.chil = [child1.id, child2.id] individuals = [husband, wife, child1, child2] self.assertTrue(us.male_last_names(family, individuals)) husband: Individual = Individual(_id="I07", name="Reese /Walter/", sex='M') wife: Individual = Individual(_id="I177", name="Monica /Walter/", sex='F') child1: Individual = Individual(_id="I277", name="Malcom /Walter/", sex='M') child2: Individual = Individual(_id="I377", name="Hal /Walters/", sex='M') family = Family(husb=husband.id, wife=wife.id) family.chil = [child1.id, child2.id] individuals = [husband, wife, child1, child2] self.assertFalse(us.male_last_names(family, individuals)) husband: Individual = Individual(_id="I007", name="Elon /Drogba/", sex='M') wife: Individual = Individual(_id="I1008", name="Emma /Drogba/", sex='F') child1: Individual = Individual(_id="I2009", name="Agua /Drogba/", sex='F') child2: Individual = Individual(_id="I3000", name="Win /Drogbaaa/", sex='F') family = Family(husb=husband.id, wife=wife.id) family.chil = [child1.id, child2.id] individuals = [husband, wife, child1, child2] self.assertTrue(us.male_last_names(family, individuals))
def test_fewer_than_15_siblings(self): husband: Individual = Individual(_id="I0") wife: Individual = Individual(_id="I1") child1: Individual = Individual(_id="I2") child2: Individual = Individual(_id="I3") child3: Individual = Individual(_id="I4") child4: Individual = Individual(_id="I5") child5: Individual = Individual(_id="I6") child6: Individual = Individual(_id="I7") child7: Individual = Individual(_id="I8") child8: Individual = Individual(_id="I9") child9: Individual = Individual(_id="I10") child10: Individual = Individual(_id="I12") child11: Individual = Individual(_id="I13") child12: Individual = Individual(_id="I14") child13: Individual = Individual(_id="I15") child14: Individual = Individual(_id="I16") child15: Individual = Individual(_id="I17") family: Family = Family(husb=husband.id, wife=wife.id) family.chil.extend([ child1.id, child2.id, child3.id, child4.id, child5.id, child6.id, child7.id, child8.id, child9.id, child10.id, child11.id, child12.id, child13.id, child14.id, child15.id ]) self.assertFalse(us.fewer_than_15_siblings(family)) family: Family = Family(husb=husband.id, wife=wife.id) family.chil.extend( [child1.id, child2.id, child3.id, child4.id, child5.id, child6.id]) self.assertTrue(us.fewer_than_15_siblings(family))