def get_organism(self): """ :return: The organism information as Organism object. """ if self.organism: organism = Organism(self.organism['ScientificName']) organism.load_entry(self.organism) return organism
def __init__(self, startingEnergy, senseOfSmell, smellRatio, endurance): '''Creates a new critter with the specified energy level, sense of smell (as a radius), and smell strength.''' self.__type__ = "Critter" #Initialize all the parameters contained in the Organism superclass Organism.__init__(self, startingEnergy, int(startingEnergy/smellRatio), METABOLISM) #Set sense of smell radius, hunger and mating thresholds, and energy-to-smell ratio self.smellRatio = smellRatio self.sense = senseOfSmell self.hungerThreshold = HUNGER_THRESHOLD self.matingThreshold = HUNGER_THRESHOLD + endurance #Sets self.hungry based on energy self.hungry = True self.checkHunger()
def spawn_organisms(self, number): """ number: amount of such organisms to add """ for _ in range(number): # want to randomly configure these components self.organisms.append(Organism(self)) return
def convert_to_object(data, type): """ This function creates either a Gene or Organism object and adds extra information from NCBI (if possible). :param data: A gene or species name. :param type: The type of the data, this could be Gene or Organism. :return: An Organism of Gene object (depending on the input type). """ if data is not None: if type == "Gene": gen = Gene(data) gen = gather_extra_data(gen, data, "gene") return gen if type == "Species": organism = Organism(data) organism = gather_extra_data(organism, data, "taxonomy") return organism return None
def evolution(gen_size, num_of_gens, num_of_parents, num_of_bests, epochs, to_print): start_time = time.time() cur_gen = [] prev_gen = [] n_gen = 0 best_org_fit = 0 best_org_weights = [] # initializing generation 0 for i in range(0, gen_size): cur_gen.append(Organism()) # start evolution while n_gen < num_of_gens: n_gen += 1 prev_gen.clear() # compute fitness for each organism on current generation for org in cur_gen: org.forward_propagation(X_train, Y_train) prev_gen.append(org) cur_gen.clear() prev_gen = sorted(prev_gen, key=lambda x: x.fitness) prev_gen.reverse() # save the fitness of the best organism of current generation cur_best_fit = prev_gen[0].fitness # save the fitness and weights of the best organism until now if cur_best_fit > best_org_fit: best_org_fit = cur_best_fit best_org_weights.clear() for layer in prev_gen[0].layers: best_org_weights.append(layer.get_weights()[0]) if to_print: print('Generation: %1.f' % n_gen, '\tBest Fitness: %.4f' % cur_best_fit) # crossover current generation cur_gen = crossover(gen_size, prev_gen, num_of_bests, num_of_parents) best_organism = Organism(best_org_weights) best_organism.compile_train(epochs, X_train, Y_train, to_print) # test Y_hat = best_organism.predict(X_test) Y_hat = Y_hat.argmax(axis=1) end_time = time.time() return accuracy_score(Y_test, Y_hat), end_time - start_time
def dynamic_crossover(nn1, nn2=None, nn3=None): nn1_weights = [] nn2_weights = [] nn3_weights = [] child_weights = [] for layer in nn1.layers: nn1_weights.append(layer.get_weights()[0]) if nn2 is not None: for layer in nn2.layers: nn2_weights.append(layer.get_weights()[0]) if nn3 is not None: for layer in nn3.layers: nn3_weights.append(layer.get_weights()[0]) for i in range(0, len(nn1_weights)): net_len = np.shape(nn1_weights[i])[1] - 1 if nn2 is not None: split1 = random.randint(0, net_len) if nn3: split2 = random.randint(split1, net_len) else: split2 = net_len for j in range(split1, split2): nn1_weights[i][:, j] = nn2_weights[i][:, j] for j in range(split2, net_len): nn1_weights[i][:, j] = nn3_weights[i][:, j] child_weights.append(nn1_weights[i]) mutation(child_weights) child = Organism(child_weights) return child
def __init__(self): #create the map that defines this Universe self.map = Map("simple.map") #self.map = Map("medium.map") print "Initializing map and first generation...\n" print str(self.map) self.pickleFile = open('pickles.txt', 'w') self.pickler = Pickler(self.pickleFile, 0) #generate first set of parents self.parents = [ Organism(NUM_STATES, MEMORY_SIZE, NUM_VALUES) for i in range(ORIGINAL_POOL_SIZE) ] for i in range(ORIGINAL_POOL_SIZE): self.parents[i].birthday = 0 #for each generation, for i in range(NUM_GENERATIONS): self.currentGeneration = i + 1 self.testParents() self.selectParents() self.nextGeneration()
## Create simpy env and start pygame env = simpy.RealtimeEnvironment(initial_time=0, factor=0.1, strict=False) pygame.init() print("Starting simulation") ecosystem = Ecosystem() size = width, height = 1000, 800 screen = pygame.display.set_mode(size) display = pygame.display image = pygame.image event = pygame.event for org in range(START_NUM_OF_ORGANISMS): new_org = Organism("O-" + str(org), ecosystem, env) ecosystem.add_being(new_org) for food in range(NUM_OF_FOOD): new_food = Food("F-" + str(food), ecosystem, env) ecosystem.add_being(new_food) Emerge = World(ecosystem, env, screen, display, image, event) ##TODO: Attempt to minimilize code by combining this part with above when initializing the organisms/food for being in ecosystem.all_beings(): env.process(being.run()) env.process(Emerge.run()) env.run(until=100)
def __init__(self, s, i, a, w, t): Organism.__init__(self, s, i, a, w, t)
def Kill(self, s): Organism.Kill(self, self.string + " has been slain by " + s)
def __init__(self, world, position, power): Organism.__init__(self, world, position, power, 0)
def __init__(self, s, a, w, p, t): Organism.__init__(self, s, 0, a, w, t) self.__probability = p
def Kill(self, s): Organism.Kill(self, self.string + " has been eaten by " + s)
def loseEnergy(self): '''Decreases the critter's energy based on its metabolic rate, returns the amount of energy lost, and updates whether or not it is hungry.''' #Superclass method! Organism.loseEnergy(self) self.checkHunger() self.updateSmell()
def loseEnergy(self): '''Decreases the fruit's energy by its decay rate, returns the amount of energy lost in case the World needs to refer to it.''' return Organism.loseEnergy(self) self.updateEnergy()
def __init__(self, startingEnergy): '''Creates a new Fruit with the specified amount of energy.''' self.__type__ = "Fruit" Organism.__init__(self, startingEnergy, startingEnergy, DECAY_RATE)