Algorytm_genetyczny #3

Merged
s481832 merged 18 commits from Algorytm_genetyczny into master 2024-06-10 13:30:20 +02:00
14 changed files with 25 additions and 21 deletions
Showing only changes of commit cda769872b - Show all commits

View File

@ -13,11 +13,12 @@ class Animal:
path = f'images/{name}2.png'
return path
def __init__(self, x, y,name, image, food_image, food, environment, activity, ill=False, adult=False,):
def __init__(self, x, y,name, image_path, food_image, food, environment, activity, ill=False, adult=False,):
self.x = x - 1
self.y = y - 1
self.name = name
self.image = image
self.image_path = image_path
self.image = pygame.image.load(image_path)
self.adult = adult
self.food = food
self.food_image = food_image

View File

@ -5,12 +5,12 @@ from datetime import datetime
class Bat(Animal):
def __init__(self, x, y, adult=False):
name = 'bat'
Bat_image = pygame.image.load(self.choose_picture(name))
image_path = self.choose_picture(name)
environment = "medium"
food_image = 'images/grains.png'
parrot_food = 'grains'
activity = 'nocturnal'
super().__init__(x, y,name, Bat_image, food_image,parrot_food, environment, adult)
super().__init__(x, y,name, image_path, food_image,parrot_food, environment, adult)
self._starttime = datetime.now()
def getting_hungry(self, const):

View File

@ -5,13 +5,13 @@ from datetime import datetime
class Bear(Animal):
def __init__(self, x, y, adult=False):
name = 'bear'
Bear_image = pygame.image.load(self.choose_picture(name))
image_path = self.choose_picture(name)
environment = "cold"
activity = 'nocturnal'
ill = self.is_ill()
bear_food = 'meat'
food_image = 'images/meat.png'
super().__init__(x, y,name, Bear_image, food_image,bear_food,environment, activity, ill, adult)
super().__init__(x, y,name, image_path, food_image,bear_food,environment, activity, ill, adult)
self._starttime = datetime.now()
def getting_hungry(self, const):

View File

@ -5,7 +5,7 @@ from datetime import datetime
class Elephant(Animal):
def __init__(self, x, y, adult=False):
name = 'elephant'
Elephant_image = pygame.image.load(self.choose_picture(name))
image_path = self.choose_picture(name)
environment = "hot"
activity = 'diurnal'
ill = self.is_ill()
@ -16,7 +16,7 @@ class Elephant(Animal):
elephant_food = 'milk'
food_image = 'images/milk.png'
super().__init__(x, y,name, Elephant_image, food_image,elephant_food, environment, activity, ill, adult)
super().__init__(x, y,name, image_path, food_image,elephant_food, environment, activity, ill, adult)
self._starttime = datetime.now()
def getting_hungry(self, const):

View File

@ -5,13 +5,13 @@ from datetime import datetime
class Giraffe(Animal):
def __init__(self, x, y, adult=False):
name = 'giraffe'
Giraffe_image = pygame.image.load(self.choose_picture(name))
image_path = self.choose_picture(name)
environment = "hot"
activity = 'diurnal'
ill = self.is_ill()
food_image = 'images/leaves.png'
giraffe_food = 'leaves'
super().__init__(x, y, name, Giraffe_image, food_image,giraffe_food, environment, activity, ill, adult)
super().__init__(x, y, name, image_path, food_image,giraffe_food, environment, activity, ill, adult)
self._starttime = datetime.now()
def getting_hungry(self, const):

View File

@ -5,12 +5,12 @@ from datetime import datetime
class Owl(Animal):
def __init__(self, x, y, adult=False):
name = 'owl'
Owl_image = pygame.image.load(self.choose_picture(name))
image_path = self.choose_picture(name)
environment = "medium"
food_image = 'images/grains.png'
parrot_food = 'grains'
activity = 'nocturnal'
super().__init__(x, y,name, Owl_image, food_image,parrot_food, environment, adult)
super().__init__(x, y,name, image_path, food_image,parrot_food, environment, adult)
self._starttime = datetime.now()
def getting_hungry(self, const):

View File

@ -5,13 +5,13 @@ from datetime import datetime
class Parrot(Animal):
def __init__(self, x, y, adult=False):
name = 'parrot'
Parrot_image = pygame.image.load(self.choose_picture(name))
image_path = self.choose_picture(name)
environment = "medium"
activity = 'diurnal'
ill = self.is_ill()
food_image = 'images/grains.png'
parrot_food = 'grains'
super().__init__(x, y, name, Parrot_image, food_image, parrot_food, environment, activity, ill, adult)
super().__init__(x, y, name, image_path, food_image, parrot_food, environment, activity, ill, adult)
self._starttime = datetime.now()
def getting_hungry(self, const):

View File

@ -5,13 +5,13 @@ from datetime import datetime
class Penguin(Animal):
def __init__(self, x, y, adult=False):
name = 'penguin'
Penguin_image = pygame.image.load(self.choose_picture(name))
image_path = self.choose_picture(name)
environment = "cold"
activity = 'diurnal'
ill = self.is_ill()
food_image = 'images/fish.png'
penguin_food = 'fish'
super().__init__(x, y, name, Penguin_image, food_image, penguin_food, environment, activity, ill, adult)
super().__init__(x, y, name, image_path, food_image, penguin_food, environment, activity, ill, adult)
self._starttime = datetime.now()
def getting_hungry(self, const):

View File

@ -80,7 +80,7 @@ class Agent:
def feed_animal(self, animals, goal,const):
goal_x, goal_y = goal
guess = AnimalClassifier('./model/best_model.pth', classes)
neuron = AnimalClassifier('./model/best_model.pth', classes)
if self.x == goal_x and self.y == goal_y:
for animal in animals:
if animal.x == goal_x and animal.y == goal_y:
@ -89,7 +89,11 @@ def feed_animal(self, animals, goal,const):
else:
activity_time = False
guests = random.randint(1, 15)
guess.classify(animal.image)
guess = neuron.classify(animal.image_path)
if guess == animal.name:
print(f"I'm sure this is {guess}")
else:
print(f"I was wrong, this is not a {guess} but a {animal.name}")
decision = feed_decision(animal.adult, activity_time, animal.ill, const.season, guests, animal._feed, self._dryfood, self._wetfood)
if decision != [1]:
if decision == [2]:

View File

@ -6,7 +6,7 @@ class AnimalClassifier:
def __init__(self, model_path, classes, image_size=224, mean=None, std=None):
self.classes = classes
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
self.model = torch.load(model_path)
self.model = torch.load(model_path, map_location=torch.device('cpu'))
self.model = self.model.to(self.device)
self.model = self.model.eval()
self.image_size = image_size
@ -34,7 +34,6 @@ class AnimalClassifier:
return self.classes[predicted.item()]
# Define the classes
classes = [
"bat",
"bear",

View File

@ -6,7 +6,7 @@ class Constants:
def __init__(self):
self.BLACK = (0, 0, 0)
self.RED = (255, 0, 0)
self.GRID_SIZE = 50
self.GRID_SIZE = 70
self.GRID_WIDTH = 30
self.GRID_HEIGHT = 15
self.WINDOW_SIZE = (self.GRID_WIDTH * self.GRID_SIZE, self.GRID_HEIGHT * self.GRID_SIZE)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 642 KiB

After

Width:  |  Height:  |  Size: 373 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 444 KiB

After

Width:  |  Height:  |  Size: 161 KiB

BIN
tree.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 603 KiB