adding genetic algorithm implementation that chooses which plants to farm #3

Merged
s464891 merged 3 commits from genetic_algorithm into master 2022-06-06 18:27:42 +02:00
4 changed files with 25 additions and 17 deletions
Showing only changes of commit c42409b210 - Show all commits

View File

@ -29,4 +29,4 @@ class Constants:
# Genetic algorithm points average
POINTS_AVERAGE = range(5, 8)
POINTS_AVERAGE = 6.33

View File

@ -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__':

View File

@ -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

View File

@ -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]