Beispiel #1
0
def perf_bench():
    sillypop = []
    for _ in range(1000):
        sillypop.append( gi.create( ( 1, 1.0, 1.0 ) ) )
    start_time = timeit.default_timer()
    for t in range(7300):
        for human in sillypop:
            gi.update( human )
    elapsed = timeit.default_timer() - start_time
    print( str( elapsed ) )
    def update(self):
        self.time += 1
        for h in self.humans:
            dgi.update(h)
            # Pull people who've changed out of human and into werewolves
            if dgi.is_infected(h) and not dgi.is_incubating(h):
                self.humans.remove(h)
                self.waiting_wolves.remove(
                    h
                )  # See above, they are in two places and need to be removed
                self.werewolves.append(h)
                if self.debug:
                    print(f"Individual {h} is a wolf!")

        if self.time % HALLOWEEN_DAY == 0:  # It is october 31
            if len(self.werewolves) == 0:  # and there are no werewolves
                found_one = False
                possible_humans = len(self.humans)
                ages = []
                min_age_exposure = self.min_age_werewolf_years * DAYS_YEAR
                while not found_one and possible_humans > 0:
                    for h in self.humans:
                        possible_humans -= 1
                        age = dgi.get_age(h)
                        ages.append(age)
                        if age > min_age_exposure:
                            if age % DAYS_YEAR == HALLOWEEN_DAY:
                                self.humans.remove(h)
                                self.werewolves.append(h)
                                found_one = True
                                break
                if found_one:
                    print("Found a new werewolf with a Halloween Birthday.")
                else:
                    print("No cool birthdays, just taking someone.")
                    future_wolf = None
                    for h in self.humans:
                        age = dgi.get_age(h)
                        if not future_wolf and age > min_age_exposure:
                            self.humans.remove(h)
                            self.werewolves.append(h)
                            future_wolf = h
                    if future_wolf:
                        print("Found someone old enough.")
                    else:
                        print("No one old enough! No outbreak!")
                        print(ages)
                        sys.exit()
        if self.enable_reporting:
            self.report_step()
 def test_update_individual(self):
     test = self.namespace_under_test
     # self.debug = True
     self.create_generic_gene_gina()
     curr_expected_gene_age = self.gene['expected']['age']
     curr_expected_gina_age = self.gina['expected']['age']
     timestep_size = 1  # TODO: figure out how to set / configure this
     gene = self.gene['observed']
     gina = self.gina['observed']
     gina_became_possible_mother = test.is_possible_mother(gina)
     gina_ceased_potential_motherhood = False
     age_of_potential_motherhood = None
     age_of_ceased_motherhood_potential = None
     self.assertFalse(gina_became_possible_mother,
                      "gina should not be a possible mother at creation.")
     for x in range(14600):
         self.check_property_of_individual('age',
                                           curr_expected_gene_age,
                                           test.get_age(gene),
                                           individual_label="gene")
         self.check_property_of_individual('age',
                                           curr_expected_gina_age,
                                           test.get_age(gina),
                                           individual_label="gina")
         if not gina_became_possible_mother:
             gina_became_possible_mother = test.is_possible_mother(gina)
             if gina_became_possible_mother:
                 age_of_potential_motherhood = curr_expected_gina_age
         elif not test.is_possible_mother(
                 gina) and not gina_ceased_potential_motherhood:
             gina_ceased_potential_motherhood = True
             age_of_ceased_motherhood_potential = curr_expected_gina_age
         test.update(gene)
         test.update(gina)
         curr_expected_gina_age += timestep_size
         curr_expected_gene_age += timestep_size
     self.assertTrue(gina_became_possible_mother,
                     "gina should have become a possible mother.")
     if self.debug:
         print(
             f"gina became a possible mother at age {age_of_potential_motherhood}.\nm"
         )
     self.assertTrue(gina_ceased_potential_motherhood,
                     "gina should have ceased being a potential mother.")
     if self.debug:
         print(
             f"gina ceased potential motherhood at age {age_of_ceased_motherhood_potential}.\n"
         )
Beispiel #4
0
 def update(self):
     self.time += 1
     for h in self.humans:
         dgi.update(h)
     if self.time % HALLOWEEN_DAY == 0:  # It is october 31
         if len(self.werewolves) == 0:  # and there are no werewolves
             found_one = False
             possible_humans = len(self.humans)
             ages = []
             min_age_exposure = self.min_age_werewolf_years * DAYS_YEAR
             while not found_one and possible_humans > 0:
                 for h in self.humans:
                     possible_humans -= 1
                     age = dgi.get_age(h)
                     ages.append(age)
                     if age > min_age_exposure:
                         if age % DAYS_YEAR == HALLOWEEN_DAY:
                             self.humans.remove(h)
                             self.werewolves.append(h)
                             found_one = True
                             break
             if found_one:
                 print("Found a new werewolf with a Halloween Birthday.")
             else:
                 print("No cool birthdays, just taking someone.")
                 future_wolf = None
                 for h in self.humans:
                     age = dgi.get_age(h)
                     if not future_wolf and age > min_age_exposure:
                         self.humans.remove(h)
                         self.werewolves.append(h)
                         future_wolf = h
                 if future_wolf:
                     print("Found someone old enough.")
                 else:
                     print("No one old enough! No outbreak!")
                     print(ages)
                     sys.exit()
     if self.enable_reporting:
         self.report_step()
for x in range(100):
    sex = x%2
    person = dgi.create((sex, 7300, 1.0))
    people.append(person)
    pass
print("Created people.")
# Now, touch each one with an infection before we spin time
for victim in people:
    dgi.force_infect(victim)
    pass

print("Forced infections.")

for x in range(25):
    for person in people:
        dgi.update(person)
        pass
    pass

incubating = 0
infected = 0
for person in people:
    if dgi.is_incubating(person):
        incubating += 1
        pass
    if dgi.is_infected(person):
        infected += 1
        pass
    pass
print(f"25 days later... incubating: {incubating}\tinfected: {infected}")