def nextOne_goods(): """生成奖励""" type = random.randint(0, 100) if type < 2: return Bee(screen, sets.beeImage[0], 1) elif type >= 2 and type < 5: return Bee(screen, sets.beeImage[1], 2) elif type >= 5 and type < 6: return Bee(screen, sets.beeImage[2], 0)
def searchArea(self): i = 0 h = 0 self.beeList = [] while ((i < self.bees_number) and (i < self.flip)): #print ("First method to generate") solution = self.refSolution.solution.get_state() k = 0 while ((self.flip * k + h) < len(solution)): solution[self.flip * k + h] = ((solution[self.flip * k + h] + 1) % 2) k += 1 newBee = Bee(i, self.data, self.locIterations, solution) self.beeList.append(newBee) i += 1 h = h + 1 h = 0 while ((i < self.bees_number) and (i < 2 * self.flip)): #print("Second method to generate") solution = self.refSolution.solution.get_state() k = 0 while ((k < int(len(solution) / self.flip)) and (self.flip * k + h < len(solution))): solution[int(self.data.nb_attribs / self.flip) * h + k] = ( (solution[int(self.data.nb_attribs / self.flip) * h + k] + 1) % 2) k += 1 newBee = Bee(i, self.data, self.locIterations, solution) self.beeList.append(newBee) i += 1 h = h + 1 while (i < self.bees_number): #print("Random method to generate") solution = self.refSolution.solution.get_state() indice = random.randint(0, len(solution) - 1) solution[indice] = ((solution[indice] + 1) % 2) newBee = Bee(i, self.data, self.locIterations, solution) self.beeList.append(newBee) i += 1 for bee in (self.beeList): lista = [ j for j, n in enumerate(bee.solution.get_state()) if n == 1 ] if (len(lista) == 0): bee.setSolution(Bee.Rand(self.data.nb_attribs))
def add_bees(self, n): for i in range(n): b = Bee( x=random.randint(self.min_x * 1000, self.max_x * 1000) / 1000, y=random.randint(self.min_y * 1000, self.max_y * 1000) / 1000, env=self) self.bees.append(b)
def bestBeeQuality(self): distance = 0 i = 0 pos = -1 while (i < self.bees_number): max_val = self.beeList[i].fitness nbUn = Solution.nbrUn(self.beeList[i].solution.get_state()) while ((i < self.bees_number) and (self.beeList[i].solution.get_accuracy( self.beeList[i].solution.get_state()) == max_val)): distanceTemp = self.distanceTabou(self.beeList[i]) nbUnTemp = Solution.nbrUn(self.beeList[i].solution.get_state()) if (distanceTemp > distance) or ((distanceTemp == distance) and (nbUnTemp < nbUn)): if ((distanceTemp == distance) and (nbUnTemp < nbUn)): print("We pick the solution with less features") nbUn = nbUnTemp distance = distanceTemp pos = i i += 1 if (pos != -1): return self.beeList[pos] bee = Bee(-1, self.data, self.locIterations, Bee.Rand(self.data.nb_attribs)) return bee
def action(self): x = self.__outer.x y = self.__outer.y bee = Bee(self.__outer._ecosystem, x, y, hive=self.__outer) self.__outer._ecosystem.animal_map[x][y].append(bee) self.__outer.bees.append(bee) self.__outer.food -= BEE_FOOD_COST self._status = bt.Status.SUCCESS
def add_bee(self, id): valied = False while not valied: x = random.randint(0, self.hive.rows) y = random.randint(0, self.hive.cols) valied = self.hive.is_valid((x, y)) color = self.new_color() self.bees.update({id: Bee((x, y), id=id, color=color)})
def create_neigh_bee(site, patch_size, problem): p = site.position[:] i = randint(0, len(p) - 1) j = randint(i, len(p)) temp = p[i:j] p = np.concatenate([p[:i], p[j:]]) r = randint(0, len(p)) p = np.insert(p, r, temp) temp = Bee(problem, p) schedule = temp.schedule_builder(list(p)) temp.schedule = schedule return temp
def bestBeeDiversity(self): max_val = 0 for i in range(len(self.beeList)): if (self.distanceTabou(self.beeList[i]) > max_val): max_val = self.distanceTabou(self.beeList[i]) if (max_val == 0): bee = Bee(-1, self.data, self.locIterations, Bee.Rand(self.data.nb_attribs)) return bee i = 0 while (i < len(self.beeList) and self.distanceTabou(self.beeList[i]) != max_val): i += 1 return self.beeList[i]
def __create_bee(self): ''' Creates an instance of Bee with name, feature_list and a config file. ''' # access default speech mode from config file default_speech_mode = self.config["Utilities"]["default_speech_mode"] virtual_assistant = Bee(name=self.name, features=self.feature_list, config=self.config, wake_word_detector=self.wake_word_detector, default_speech_mode=default_speech_mode, decision_strategy=self.decision_strategy) return virtual_assistant
def __init__(self, problem, flip, max_chance, bees_number, maxIterations, locIterations): self.data = problem self.flip = flip self.max_chance = max_chance self.nbChance = max_chance self.bees_number = bees_number self.maxIterations = maxIterations self.locIterations = locIterations self.beeList = [] self.refSolution = Bee(-1, self.data, self.locIterations, Bee.Rand(self.data.nb_attribs)) self.bestSolution = self.refSolution self.tabou = [] self.feature_count = {i: 0 for i in range(self.data.nb_attribs)} Solution.solutions.clear()
def add_bee(self, hive, type_bee, color, age=0): """ Add a bee to the model. """ bee = Bee(self, pos=hive.pos, hive=hive, type_bee=type_bee, hive_id=hive.unique_id, color=color, age=age) if type_bee == 'babee': self.birth_count += 1 self.add_agent(bee, hive.pos)
def load_from_csv(filename, bee_num=None): frame = 0 bees = {} with open(filename, 'rb') as f: f_reader = csv.reader(f) for row in f_reader: for idx in range(0, len(row), 6): id = int(float(row[idx])) if id < 0: continue if bee_num == 1: name = 'bee_0' else: name = 'bee_' + str(id) path = (float(row[idx + 1]), float(row[idx + 2])) bee_len = float(row[idx + 3]) bee_width = float(row[idx + 4]) bee_angle = float(row[idx + 5]) if name not in bees.keys(): bees[name] = Bee(name, path, bee_len, bee_width, bee_angle, frame) else: bees[name].refresh_params(path, bee_len, bee_width, bee_angle) if bee_num == 1: break frame += 1 for key, value in bees.iteritems(): value.calculate_end_frame() return bees
def generate_enemy(self, stage_num): print(stage_num) self.stage_num = stage_num - 1 enemy_dic = enemy_generation_table[stage_num] for part_num in range(len(self.enemies)): enemy_part = enemy_dic[part_num] number = 0 for enemy_type in enemy_part: enemy = None if enemy_type == BEE: enemy = Bee(enemy_position_table[part_num][number]) elif enemy_type == BFLY: enemy = Butterfly(enemy_position_table[part_num][number]) elif enemy_type == MOTH: enemy = Moth(enemy_position_table[part_num][number]) else: pass self.enemies[part_num].append(enemy) number += 1 gameworld.add_objects(self.enemies[part_num], 1)
def add_new_bee(canvas, bee_list): bee = Bee(canvas) bee.draw_bee() bee_list.append(bee)
def initialize_forest(self): """Adds initial organisms to the map.""" directions = list(Direction) # Water map for pool_size in WATER_POOLS: rand_x = random.randint(0, self.width - 1) rand_y = random.randint(0, self.height - 1) while self.water_map[rand_x][rand_y]: rand_x = random.randint(0, self.width - 1) rand_y = random.randint(0, self.height - 1) water_pools_added = 0 positions = [(rand_x, rand_y)] WATER_POOLS_POSITIONS.append((rand_x, rand_y)) while water_pools_added < pool_size and positions: # Breadth first add water pools around x, y = positions.pop(0) if not self.water_map[x][y]: water = Water(self, x, y) self.water_map[x][y] = water water_pools_added += 1 # Insert all neighbors random.shuffle( directions) # shuffle for a bit random shapes for dir in directions: new_x = x + dir.value[0] new_y = y + dir.value[1] # Check if out of bounds if new_x < 0 or new_x >= self.width or new_y < 0 or new_y >= self.height: continue if self.water_map[new_x][new_y]: continue positions.append((new_x, new_y)) # Plant map for x in range(self.width): for y in range(self.height): # check if water if self.water_map[x][y]: continue if random.random() <= TREE_PERCENTAGE: tree = Tree(self, x, y) self.plant_map[x][y] = tree if random.random() <= HIVES_PER_TREE: hive = Hive(self, x, y) self.animal_map[x][y].append(hive) bee_amount = random.randint(HIVE_BEE_MIN_AMOUNT, HIVE_BEE_MAX_AMOUNT) bee = Bee(self, x, y, hive=hive, scout=True, age=random.randint(0, 24 * 150)) hive.bees.append(bee) self.animal_map[x][y].append(bee) for _ in range(bee_amount): bee = Bee(self, x, y, hive=hive, scout=False, age=random.randint(0, 24 * 150)) self.animal_map[x][y].append(bee) hive.bees.append(bee) elif random.random() <= GRASS_INIT_PERCENTAGE: grass = Grass(self, x, y, random.randint(-80, 100), None, self.get_initial_water_level(x, y)) self.plant_map[x][y] = grass else: earth = Earth(self, x, y, self.get_initial_water_level(x, y)) self.plant_map[x][y] = earth # Flower map from organisms import Type for x in range(self.width): for y in range(self.height): if self.water_map[x][y]: continue if random.random() <= FLOWER_PERCENTAGE: if self.plant_map[x][y] and self.plant_map[x][ y].type == Type.TREE: continue for _ in range(random.randint(1, 4)): flower = Flower(self, x, y, random.randint(-50, 100), nectar=random.randint(0, 100), has_seed=random.choice([True, False])) self.flower_map[x][y].append(flower) # Animal map import numpy as np # Rabbits for _ in range(BURROW_AMOUNT): x = random.randint(0, self.width - 1) y = random.randint(0, self.height - 1) while self.water_map[x][y]: x = random.randint(0, self.width - 1) y = random.randint(0, self.height - 1) burrow = Burrow(self, x, y) self.animal_map[x][y].append(burrow) rabbit_amount = random.randint(BURROW_RABBIT_MIN_AMOUNT, BURROW_RABBIT_MAX_AMOUNT) for _ in range(rabbit_amount): dx = random.randint(-3, 3) dy = random.randint(-3, 3) if x + dx < 0 or x + dx >= self.width or y + dy < 0 or y + dy >= self.height: continue if self.water_map[x + dx][y + dy]: continue rabbit = Rabbit(self, x + dx, y + dy, random.choice([True, False]), adult=True, burrow=burrow, age=random.randint(24 * 30, 24 * 30 * 3), reproduction_timer=random.randint(0, 24 * 6), genetics_factor=np.random.normal(1, 0.1)) self.animal_map[x + dx][y + dy].append(rabbit) # Foxes for _ in range(FOX_AMOUNT): x = random.randint(0, self.width - 1) y = random.randint(0, self.height - 1) while self.water_map[x][y]: x = random.randint(0, self.width - 1) y = random.randint(0, self.height - 1) fox = Fox(self, x, y, random.choice([True, False]), adult=True, age=random.randint(24 * 30 * 2, 24 * 30 * 6), genetics_factor=np.random.normal(1, 0.1)) self.animal_map[x][y].append(fox)
background = screen.copy() for y in range(screen_rect.height): color_val = 255 - 255 * y / screen_rect.height pygame.draw.line(background, (0, color_val, 0), (0, y), (screen_rect.width, y)) Bee.loadImages() screen.blit(background, (0, 0)) pygame.display.flip() invaders = pygame.sprite.Group() projectiles = pygame.sprite.Group() monsters = pygame.sprite.Group() drawing_group = pygame.sprite.OrderedUpdates() player_bee = Bee(10, screen.get_height() - 200) for x in range(10, screen.get_width(), 100): for y in range(10, screen.get_height() - 300, 100): auto_bee = Bee(x, y) auto_bee.setAngle(180) invaders.add(auto_bee) drawing_group.add(auto_bee) drawing_group.add(player_bee) pygame.display.set_caption('Bee Invaders') pygame.mouse.set_visible(0) clock = pygame.time.Clock() angles = ((45, 0, -45), (90, 0, -90), (135, 180, -135))
g = Giraffe(4) g.say_hello() g.get_number_of_legs() g.how_do_i_move() g.how_much_do_i_weigh() g.am_i_extinct() if What_animal == "Hippo": h = Hippo(4) h.say_hello() h.get_number_of_legs() h.how_do_i_move() h.how_much_do_i_weigh() h.am_i_extinct() if What_animal == "Monkey": m = Monkey(2) m.say_hello() m.get_number_of_legs() m.how_do_i_move() m.how_much_do_i_weigh() m.am_i_extinct() if What_animal == "Bee": be = Bee(0) be.say_hello() be.get_number_of_legs() be.how_do_i_move() be.how_much_do_i_weigh() be.am_i_extinct()
def main(): pygame.init() title = 'Bee, Get the Nectar!' screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) screenRect = screen.get_rect() Bee.loadImages() Flower.loadImages() background = loadImage('clover-large.jpg') drawTitle = True if drawTitle: font = pygame.font.Font(None, 48) text = font.render(title, 1, Color('white')) textpos = text.get_rect(centerx=screenRect.width/2, centery=25) background.blit(text, textpos) screen.blit(background, (0, 0)) pygame.display.flip() bee = Bee(screenRect) flowers = pygame.sprite.Group() score = Score() drawingGroup = pygame.sprite.RenderUpdates() drawingGroup.add(bee) drawingGroup.add(score) pygame.display.set_caption(title) pygame.mouse.set_visible(0) clock = pygame.time.Clock() angles = ((45, 90, 135), (0, 0, 180), (315, 270, 225)) # game loop loop = True while loop: # get input for event in pygame.event.get(): if event.type == QUIT \ or (event.type == KEYDOWN and event.key == K_ESCAPE): loop = False keystate = pygame.key.get_pressed() xdir = keystate[K_RIGHT] - keystate[K_LEFT] # -1, 0, or 1 ydir = keystate[K_DOWN] - keystate[K_UP] bee.setAngle(angles[xdir+1][ydir+1]) bee.rect = bee.rect.move((xdir * 8, ydir * 8)).clamp(screenRect) # Detect collisions for flower in pygame.sprite.spritecollide(bee, flowers, True): score.score += 1 flower.kill() if random.randint(0, 50) == 0: flower = Flower(screenRect) drawingGroup.add(flower) flowers.add(flower) drawingGroup.clear(screen, background) drawingGroup.update() changedRects = drawingGroup.draw(screen) pygame.display.update(changedRects) # maintain frame rate clock.tick(40) pygame.quit()
pygame.init() screen = pygame.display.set_mode((700, 394), 0) screen_rect = screen.get_rect() background = screen.copy() for y in range(screen_rect.height): color_val = 255 - 255 * y / screen_rect.height pygame.draw.line(background, (0, color_val, 0), (0, y), (screen_rect.width, y)) Bee.loadImages() screen.blit(background, (0, 0)) pygame.display.flip() auto_bees = pygame.sprite.Group() player_bee = Bee(screen_rect) player_bee.rect = player_bee.rect.move((50, 50)) auto_bee = Bee(screen_rect) auto_bees.add(auto_bee) imgRect = player_bee.image.get_rect() player_bee.rect = imgRect.move(50, screen_rect.centery - imgRect.centery) drawing_group = pygame.sprite.RenderUpdates() drawing_group.add(player_bee, auto_bee) pygame.display.set_caption('One Bee') pygame.mouse.set_visible(0) clock = pygame.time.Clock() angles = ((45, 0, -45), (90, 0, -90), (135, 180, -135))
def create_rand_bee(problem): return Bee(problem)
TITLE = 'Bee, Get the Nectar!' screen = pygame.display.set_mode((1280, 720), 0) screenRect = screen.get_rect() Bee.loadImages() Flower.loadImages() background = loadImage('clover-large.jpg') font = pygame.font.Font(None, 48) text = font.render(TITLE, 1, Color('white')) textpos = text.get_rect(centerx=screenRect.width/2, centery=25) background.blit(text, textpos) screen.blit(background, (0, 0)) pygame.display.flip() bee = Bee(screenRect) flowers = pygame.sprite.Group() score = Score() drawingGroup = pygame.sprite.RenderUpdates() drawingGroup.add(bee) drawingGroup.add(score) pygame.display.set_caption(TITLE) pygame.mouse.set_visible(0) clock = pygame.time.Clock() angles = (( 45, 0, -45), ( 90, 0, -90), (135, 180, -135))
def init_pop(bees, problem): return [Bee(problem) for k in range(bees)]
flowers = [ (5, 5), (7, 3), ] positions = [Position(*coords) for coords in flowers] env = Environment(flowers=positions) expect(env.is_flower(positions[0])).to(be_true) expect(env.is_flower(Position(0, 0))).to(be_false) with describe(Bee) as self: with describe("movement"): with it("moves if no known environment"): bee = Bee("#1") result = bee.move("x", 5) expect(result).to(be_true) with it("updates postiion when moving"): bee = Bee("#2") result = bee.move("x", 5) expect(bee.position).to(equal(Position(5, 0))) with it("refuses to move outside known environment"): bee = Bee("#3", Environment(5, 5)) result = bee.move("x", 10) expect(result).to(be_false) expect(bee.position).to(equal(Position())) with it("refuses to move in an unknown axis"):
import pygame from pygame.locals import * from bee import Bee pygame.init() screen = pygame.display.set_mode((600, 600), 0) bgColor = 0, 120, 0 screen.fill(bgColor) pygame.display.flip() pygame.display.set_caption('Sprite Lesson 1') Bee.loadImage() bee = Bee(screen.get_rect()) drawingGroup = pygame.sprite.RenderUpdates() drawingGroup.add(bee) def clear_callback(surf, rect): surf.fill(bgColor, rect) loop = True while loop: for event in pygame.event.get(): if event.type == QUIT \ or (event.type == KEYDOWN and event.key == K_ESCAPE): loop = False drawingGroup.clear(screen, clear_callback) drawingGroup.update() changedRects = drawingGroup.draw(screen) pygame.display.update(changedRects)
from bee import Bee pygame.init() screen = pygame.display.set_mode((700, 394), 0) screen_rect = screen.get_rect() background = screen.copy() for y in range(screen_rect.height): color_val = 255 - 255 * y / screen_rect.height pygame.draw.line(background, (0, color_val, 0), (0, y), (screen_rect.width, y)) Bee.loadImages() screen.blit(background, (0, 0)) pygame.display.flip() bee = Bee(screen_rect) drawing_group = pygame.sprite.RenderUpdates() drawing_group.add(bee) pygame.display.set_caption('One Bee') pygame.mouse.set_visible(0) clock = pygame.time.Clock() angles = ((45, 0, -45), (90, 0, -90), (135, 180, -135)) # game loop loop = True while loop: # get input for event in pygame.event.get():
def create_bees(canvas, bee_list): for i in range(0, 3): bee = Bee(canvas) bee.draw_bee() bee_list.append(bee)
def get_random_bee(): bee_genes_len = 9 + deviation_angle_bits + search_radius_bits random_genes = [random.randint(0, 1) for _ in range(bee_genes_len)] random_bee = Bee(random_genes) random_bee.adjust_gene_values() return random_bee