Skip to content

Commit

Permalink
refactor: clean up Bee and Hive
Browse files Browse the repository at this point in the history
  • Loading branch information
HokageM committed Mar 25, 2024
1 parent 2aefb5b commit 116a76f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 52 deletions.
16 changes: 8 additions & 8 deletions src/ninjabees/Bee.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,6 @@ def return_home(self):
x = self.get_x()
y = self.get_y()

if x == self.hive.get_x() and y == self.hive.get_y():
if self.__found_food:
self.__is_path_set_to_hive = False
if self.__job == BeeJob.Scout:
self.hive.add_found_food_source(self.__found_food_source, list(self.__flying_path))
self.wait_for_instructions = True
return

if not self.__is_path_set_to_hive:
self.__is_path_set_to_hive = True
self.reverse_flying_path()
Expand All @@ -169,6 +161,14 @@ def return_home(self):
self.set_x(x)
self.set_y(y)

if x == self.hive.get_x() and y == self.hive.get_y():
if self.__found_food:
self.__is_path_set_to_hive = False
if self.__job == BeeJob.Scout:
self.hive.add_found_food_source(self.__found_food_source, list(self.__flying_path))
self.wait_for_instructions = True
return

def move_towards_exploration_goal(self):
"""
Move the bee towards the food goal.
Expand Down
81 changes: 37 additions & 44 deletions src/ninjabees/Hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,8 @@ def add_found_food_source(self, food_source, path_to_food):
:param path_to_food:
:return:
"""
index = 0
x, y = int(self.get_x()), int(self.get_y())
for step in path_to_food:
move = path_to_food[index]
x += move[0]
y += move[1]
index += 1
if x != food_source.get_x() or y != food_source.get_y(): # not a correct path
if not self.__check_if_path_is_valid(food_source, path_to_food):
raise Exception("Invalid path to food source")

if food_source not in self.found_food_sources:
self.found_food_sources[food_source] = list(path_to_food)
if len(path_to_food) < len(self.found_food_sources[food_source]):
Expand Down Expand Up @@ -90,47 +82,48 @@ def onlooker_bees_phase(self):
if len(self.found_food_sources) == 0:
for bee in self.bee_population:
if bee.wait_for_instructions:
bee.wait_for_instructions = False
bee.reset()
self.__reset_returned_bee(bee)
return

for bee in self.bee_population:
# For every bee in population which is currently a scout bee and has not found food,
# set its food goal random where the probability of
# selecting a food source is proportional to its quality.
if bee.get_job() == BeeJob.Scout and bee.get_x() == self.get_x() and bee.get_y() == self.get_y():
if bee.has_found_food():
self.food_at_hive += 1
if bee.get_found_food_source() not in self.found_food_sources:
self.add_found_food_source(bee.get_found_food_source(), bee.get_flying_path())
if len(bee.get_flying_path()) < len(self.found_food_sources[bee.get_found_food_source()]):
self.add_found_food_source(bee.get_found_food_source(), bee.get_flying_path())
bee.reset()
is_home = bee.get_x() == self.get_x() and bee.get_y() == self.get_y()
if bee.get_job() == BeeJob.Scout and is_home:
self.__reset_returned_bee(bee)
if self.__current_foraging < self.max_cnt_foraging_bees:
food_source_qualities = [self.calculate_food_source_quality(source) for source in
self.found_food_sources]
list_food_sources = list(self.found_food_sources)
food_goal = random.choices(list_food_sources,
weights=food_source_qualities, k=self.num_onlooker_bees)[0]
path_to_goal = self.found_food_sources[food_goal]

bee.set_food_goal(food_goal, list(path_to_goal))
self.__set_next_food_goal(bee)
bee.set_job(BeeJob.Forager)
self.__current_foraging += 1
continue
is_bee_home = bee.get_x() == self.get_x() and bee.get_y() == self.get_y()
if bee.get_job() == BeeJob.Forager and is_bee_home and bee.wait_for_instructions:
bee.wait_for_instructions = False
if bee.has_found_food():
self.food_at_hive += 1
bee.reset()
if bee.get_job() == BeeJob.Forager and is_home and bee.wait_for_instructions:
self.__reset_returned_bee(bee)
self.__set_next_food_goal(bee)
continue

food_source_qualities = [self.calculate_food_source_quality(source) for source in
self.found_food_sources]
list_food_sources = list(self.found_food_sources)
food_goal = random.choices(list_food_sources,
weights=food_source_qualities, k=self.num_onlooker_bees)[0]
path_to_goal = self.found_food_sources[food_goal]
def __check_if_path_is_valid(self, food_source, path_to_food):
index = 0
x, y = int(self.get_x()), int(self.get_y())
for _ in path_to_food:
move = path_to_food[index]
x += move[0]
y += move[1]
index += 1
if x != food_source.get_x() or y != food_source.get_y():
return False
return True

bee.set_food_goal(food_goal, list(path_to_goal))
continue
def __set_next_food_goal(self, bee):
food_source_qualities = [self.calculate_food_source_quality(source) for source in
self.found_food_sources]
list_food_sources = list(self.found_food_sources)
food_goal = random.choices(list_food_sources,
weights=food_source_qualities, k=self.num_onlooker_bees)[0]
path_to_goal = self.found_food_sources[food_goal]
bee.set_food_goal(food_goal, list(path_to_goal))

def __reset_returned_bee(self, bee):
bee.wait_for_instructions = False

if bee.has_found_food():
self.food_at_hive += 1

bee.reset()

0 comments on commit 116a76f

Please sign in to comment.