diff --git a/src/constants.py b/src/constants.py index 0a8b653..b2ae9e7 100644 --- a/src/constants.py +++ b/src/constants.py @@ -29,4 +29,4 @@ class Constants: # Genetic algorithm points average - POINTS_AVERAGE = range(5, 8) + POINTS_AVERAGE = 6.33 diff --git a/src/utils/GeneticAlgorithm.py b/src/utils/GeneticAlgorithm.py index 6c5cc17..7827f47 100644 --- a/src/utils/GeneticAlgorithm.py +++ b/src/utils/GeneticAlgorithm.py @@ -1,5 +1,4 @@ -from Plants import * - +from src.utils.Plants import * from random import choice, random @@ -19,9 +18,9 @@ class GeneticAlgorithm: return [plant_selector(plant_name) for plant_name in plant_names] def _generate_first_population(self): - return [BaseField(self._generate_random_plants()) for _ in range(100)] + return [BaseField(self._generate_random_plants()) for _ in range(15)] - def run(self): + def run(self) -> BaseField: first_population = self._generate_first_population() first_population.sort(key=lambda x: x.evaluation) population_length = len(first_population) @@ -31,7 +30,8 @@ class GeneticAlgorithm: new_population = selected.copy() while len(new_population) != population_length: child = choice(first_population).crossover(choice(first_population)) - if random() <= self.mutation_probability: + propability = random() + if propability <= self.mutation_probability: child.mutate() new_population.append(child) @@ -40,11 +40,22 @@ class GeneticAlgorithm: i += 1 if self.stop_condition(float(best_match)): break - print(best_match) + print(f'Best match is {best_match} with {i} iterations') + return best_match + + def get_plants(self) -> list: + result_array = [] + for i in range(4): + result_array = result_array + self.run().plants + return result_array def main(): - GeneticAlgorithm().run() + result_array = [] + genetic_algorithm = GeneticAlgorithm() + for i in range(4): + result_array = result_array + genetic_algorithm.run().plants + print(result_array) if __name__ == '__main__': diff --git a/src/utils/Plants.py b/src/utils/Plants.py index 0ce7cc1..bb37d0f 100644 --- a/src/utils/Plants.py +++ b/src/utils/Plants.py @@ -20,7 +20,7 @@ class BasePlant: def stop_condition(average): - return average in Constants.POINTS_AVERAGE + return round(average, 2) == Constants.POINTS_AVERAGE def plant_selector(plant_name: str) -> BasePlant: @@ -56,12 +56,7 @@ class BaseField: def evaluate_function(self) -> float: current_fields_average = self.__float__() - if current_fields_average not in Constants.POINTS_AVERAGE: - if current_fields_average < Constants.POINTS_AVERAGE[0]: - return abs(current_fields_average - Constants.POINTS_AVERAGE[0]) - elif current_fields_average > Constants.POINTS_AVERAGE[-1]: - return abs(current_fields_average - Constants.POINTS_AVERAGE[1]) - return 0 + return abs(current_fields_average - Constants.POINTS_AVERAGE) def __str__(self): return ''.join([str(plant) + ' ' for plant in self.plants]) @@ -88,7 +83,7 @@ class Cactus(BasePlant): def __init__(self): super(Cactus, self).__init__() - self.appearance_points = 6 + self.appearance_points = 4 self.difficulty_points = 3 self.profit_points = 2 diff --git a/src/world.py b/src/world.py index 5aa1fda..0a58f3c 100644 --- a/src/world.py +++ b/src/world.py @@ -2,6 +2,7 @@ import pygame from constants import Constants from src.tile import Tile +from utils.GeneticAlgorithm import GeneticAlgorithm class World: @@ -30,6 +31,7 @@ class World: self.farmland_wheat = pygame.image.load('assets/images/farmland_wheat.jpg') self.farmland_potato = pygame.image.load('assets/images/farmland_potato.jpg') self.tiles = pygame.sprite.Group() # mamy tiles jako Sprite Group, to sie przyda potem do kolizji itp. + self.plants = GeneticAlgorithm().get_plants() self.create_tiles() def create_tiles(self): @@ -45,7 +47,7 @@ class World: rodzaj_gleby = self.model.df.iloc[df_idx][Constants.SOIL_TYPE] stan_nawiezienia = self.model.df.iloc[df_idx][Constants.FERTILIZATION_STATUS] stopien_rozwoju = self.model.df.iloc[df_idx][Constants.GROWTH_LEVEL] - rodzaj_rosliny = self.model.df.iloc[df_idx][Constants.PLANT_TYPE] + rodzaj_rosliny = self.plants[df_idx].__str__() rodzaj_nawozu = self.model.df.iloc[df_idx][Constants.FERTILISER_TYPE] to_water = self.model.df.iloc[df_idx][Constants.TO_WATER]