improved version of working algorithm; generated plants are now displayed on the map
This commit is contained in:
parent
3387d41839
commit
c42409b210
@ -29,4 +29,4 @@ class Constants:
|
|||||||
|
|
||||||
# Genetic algorithm points average
|
# Genetic algorithm points average
|
||||||
|
|
||||||
POINTS_AVERAGE = range(5, 8)
|
POINTS_AVERAGE = 6.33
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
from Plants import *
|
from src.utils.Plants import *
|
||||||
|
|
||||||
from random import choice, random
|
from random import choice, random
|
||||||
|
|
||||||
|
|
||||||
@ -19,9 +18,9 @@ class GeneticAlgorithm:
|
|||||||
return [plant_selector(plant_name) for plant_name in plant_names]
|
return [plant_selector(plant_name) for plant_name in plant_names]
|
||||||
|
|
||||||
def _generate_first_population(self):
|
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 = self._generate_first_population()
|
||||||
first_population.sort(key=lambda x: x.evaluation)
|
first_population.sort(key=lambda x: x.evaluation)
|
||||||
population_length = len(first_population)
|
population_length = len(first_population)
|
||||||
@ -31,7 +30,8 @@ class GeneticAlgorithm:
|
|||||||
new_population = selected.copy()
|
new_population = selected.copy()
|
||||||
while len(new_population) != population_length:
|
while len(new_population) != population_length:
|
||||||
child = choice(first_population).crossover(choice(first_population))
|
child = choice(first_population).crossover(choice(first_population))
|
||||||
if random() <= self.mutation_probability:
|
propability = random()
|
||||||
|
if propability <= self.mutation_probability:
|
||||||
child.mutate()
|
child.mutate()
|
||||||
new_population.append(child)
|
new_population.append(child)
|
||||||
|
|
||||||
@ -40,11 +40,22 @@ class GeneticAlgorithm:
|
|||||||
i += 1
|
i += 1
|
||||||
if self.stop_condition(float(best_match)):
|
if self.stop_condition(float(best_match)):
|
||||||
break
|
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():
|
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__':
|
if __name__ == '__main__':
|
||||||
|
@ -20,7 +20,7 @@ class BasePlant:
|
|||||||
|
|
||||||
|
|
||||||
def stop_condition(average):
|
def stop_condition(average):
|
||||||
return average in Constants.POINTS_AVERAGE
|
return round(average, 2) == Constants.POINTS_AVERAGE
|
||||||
|
|
||||||
|
|
||||||
def plant_selector(plant_name: str) -> BasePlant:
|
def plant_selector(plant_name: str) -> BasePlant:
|
||||||
@ -56,12 +56,7 @@ class BaseField:
|
|||||||
|
|
||||||
def evaluate_function(self) -> float:
|
def evaluate_function(self) -> float:
|
||||||
current_fields_average = self.__float__()
|
current_fields_average = self.__float__()
|
||||||
if current_fields_average not in Constants.POINTS_AVERAGE:
|
return abs(current_fields_average - 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
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return ''.join([str(plant) + ' ' for plant in self.plants])
|
return ''.join([str(plant) + ' ' for plant in self.plants])
|
||||||
@ -88,7 +83,7 @@ class Cactus(BasePlant):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
||||||
super(Cactus, self).__init__()
|
super(Cactus, self).__init__()
|
||||||
self.appearance_points = 6
|
self.appearance_points = 4
|
||||||
self.difficulty_points = 3
|
self.difficulty_points = 3
|
||||||
self.profit_points = 2
|
self.profit_points = 2
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ import pygame
|
|||||||
|
|
||||||
from constants import Constants
|
from constants import Constants
|
||||||
from src.tile import Tile
|
from src.tile import Tile
|
||||||
|
from utils.GeneticAlgorithm import GeneticAlgorithm
|
||||||
|
|
||||||
|
|
||||||
class World:
|
class World:
|
||||||
@ -30,6 +31,7 @@ class World:
|
|||||||
self.farmland_wheat = pygame.image.load('assets/images/farmland_wheat.jpg')
|
self.farmland_wheat = pygame.image.load('assets/images/farmland_wheat.jpg')
|
||||||
self.farmland_potato = pygame.image.load('assets/images/farmland_potato.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.tiles = pygame.sprite.Group() # mamy tiles jako Sprite Group, to sie przyda potem do kolizji itp.
|
||||||
|
self.plants = GeneticAlgorithm().get_plants()
|
||||||
self.create_tiles()
|
self.create_tiles()
|
||||||
|
|
||||||
def create_tiles(self):
|
def create_tiles(self):
|
||||||
@ -45,7 +47,7 @@ class World:
|
|||||||
rodzaj_gleby = self.model.df.iloc[df_idx][Constants.SOIL_TYPE]
|
rodzaj_gleby = self.model.df.iloc[df_idx][Constants.SOIL_TYPE]
|
||||||
stan_nawiezienia = self.model.df.iloc[df_idx][Constants.FERTILIZATION_STATUS]
|
stan_nawiezienia = self.model.df.iloc[df_idx][Constants.FERTILIZATION_STATUS]
|
||||||
stopien_rozwoju = self.model.df.iloc[df_idx][Constants.GROWTH_LEVEL]
|
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]
|
rodzaj_nawozu = self.model.df.iloc[df_idx][Constants.FERTILISER_TYPE]
|
||||||
to_water = self.model.df.iloc[df_idx][Constants.TO_WATER]
|
to_water = self.model.df.iloc[df_idx][Constants.TO_WATER]
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user