AI_PROJECT/GeneticAccuracy.py

139 lines
4.2 KiB
Python

import json
import random
from displayControler import NUM_Y, NUM_X
iterat = 2500
population = 120
roulette = True
plants = ['corn', 'potato', 'tomato', 'carrot']
initial_yields = {'corn': 38, 'potato': 40, 'tomato': 43, 'carrot': 45}
yield_reduction = {
'corn': {'corn': -4.5, 'potato': -3, 'tomato': -7, 'carrot': -7},
'potato': {'corn': -7, 'potato': -5, 'tomato': -10, 'carrot': -6},
'tomato': {'corn': -4, 'potato': -5, 'tomato': -7, 'carrot': -7},
'carrot': {'corn': -11, 'potato': -5, 'tomato': -4, 'carrot': -7}
}
yield_reduction2 = {
'corn': {'corn': None, 'potato': -4, 'tomato': -2, 'carrot': -4},
'potato': {'corn': None, 'potato': -5, 'tomato': -5, 'carrot': -2},
'tomato': {'corn': -5, 'potato': -3, 'tomato': -7, 'carrot': None},
'carrot': {'corn': -3, 'potato': -6, 'tomato': -4, 'carrot': -9}
}
yield_multiplier = {'corn': 1.25, 'potato': 1.17, 'tomato': 1.22, 'carrot': 1.13}
yield_multiplier2 = {'corn': 1.25, 'potato': 1.19, 'tomato': 1.22, 'carrot': 1.15}
def calculate_yields(garden):
rows = len(garden)
cols = len(garden[0])
total_yields = 0
for i in range(rows):
for j in range(cols):
plant = garden[i][j]
yield_count = initial_yields[plant]
# Sprawdzanie sąsiadów
neighbors = [
(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)
]
for ni, nj in neighbors:
if 0 <= ni < rows and 0 <= nj < cols:
neighbor_plant = garden[ni][nj]
yield_count += yield_reduction[plant][neighbor_plant]
yield_count *= yield_multiplier[plant]
total_yields += yield_count
return total_yields
def calculate_yields2(garden):
rows = len(garden)
cols = len(garden[0])
total_yields = 0
for i in range(rows):
for j in range(cols):
plant = garden[i][j]
yield_count = initial_yields[plant]
# Sprawdzanie sąsiadów
neighbors = [
(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)
]
neighbor_flag = False
for ni, nj in neighbors:
if 0 <= ni < rows and 0 <= nj < cols:
neighbor_plant = garden[ni][nj]
if yield_reduction2[plant][neighbor_plant] is not None: # jeśli jest wartość None to plony dla tej rośliny będą wyzerowane
yield_count += yield_reduction2[plant][neighbor_plant]
else:
neighbor_flag = True
if not neighbor_flag:
yield_count *= yield_multiplier2[plant]
total_yields += yield_count
return total_yields
def generate_garden(rows=20, cols=12):
return [[random.choice(plants) for _ in range(cols)] for _ in range(rows)]
def generate_garden_with_yields(t, rows=NUM_Y, cols=NUM_X):
garden = generate_garden(rows, cols)
if t == 1:
total_yields = calculate_yields(garden)
else:
total_yields = calculate_yields2(garden)
return [garden, total_yields]
def generate():
s1 = 0
s2 = 0
n = 150
for i in range(n):
x = generate_garden_with_yields(1)
s1 += x[1]
y = generate_garden_with_yields(2)
s2 += y[1]
return [s1/n, s2/n]
data = generate()
# print(data)
# Odczyt z pliku
with open(f'pole_pop{population}_iter{iterat}_{roulette}.json', 'r') as file:
garden_data = json.load(file)
# print("Odczytane dane ogrodu:")
# for row in garden_data:
# print(row)
print("Wygenerowane przy pomocy GA: ", calculate_yields(garden_data))
print(f"Przeciętny ogród wygenerowany randomowo ma {data[0]} plonów")
print("Uśredniony przyrost plonów (ile razy więcej plonów): ", calculate_yields(garden_data)/data[0])
# Odczyt z pliku
with open(f'pole2_pop{population}_iter{iterat}_{roulette}.json', 'r') as file:
garden_data2 = json.load(file)
# print("Odczytane dane ogrodu:")
# for row in garden_data2:
# print(row)
print("Wygenerowane: przy pomocy GA2", calculate_yields2(garden_data2))
print(f"Przeciętny ogród wygenerowany randomowo ma {data[1]} plonów")
print("Uśredniony przyrost plonów (ile razy więcej plonów): ", calculate_yields2(garden_data2)/data[1])