Compare commits

...

23 Commits

Author SHA1 Message Date
8aadcfb677 Zaktualizuj 'src/dimensions.py' 2021-06-21 14:16:18 +02:00
831e599e14 Zaktualizuj 'tractor.py' 2021-06-21 14:15:45 +02:00
c21203fb72 Zaktualizuj 'node.py' 2021-06-21 14:14:44 +02:00
3fd1afdaeb Zaktualizuj 'src/colors.py' 2021-06-21 14:14:19 +02:00
4f5cf361ce Zaktualizuj 'field.py' 2021-06-21 14:14:00 +02:00
e9c11d37d2 Zaktualizuj 'plant.py' 2021-06-21 14:13:44 +02:00
e77b8d03f9 Zaktualizuj 'main.py' 2021-06-21 14:13:30 +02:00
8f0a1ed075 Zaktualizuj 'main.py' 2021-06-21 14:02:50 +02:00
bd33fa3df5 GA FIX
- END of bugfixing

with Michał Malinowski
2021-06-21 04:47:16 +02:00
5ca916873d GA FIX
- FIX bug where parents would not go to next gen

with Michał Malinowski
2021-06-21 04:43:13 +02:00
361a733102 GA END
- BUGfix
- comments
- stop var

with Michał Malinowski
2021-06-21 04:04:26 +02:00
ef5c5556ef GA implementation
- ADD comments
- ADD stop function

with Michał Malinowski
2021-06-21 03:38:21 +02:00
288d3cf30a GA implementation
- ADD crossover
- ADD mutation
- ADD next_gen preparation
- Project completed (with errors)

with Michał Malinowski
2021-06-21 03:24:07 +02:00
7a14078390 GA implementation
- ADD pretty_printer method
- crossover draft

with Michał Malinowski
2021-06-21 01:56:55 +02:00
19680a0139 GA implementation
- DONE best results
- DONE parents selection

with Michał Malinowski
2021-06-21 00:38:56 +02:00
301e05268c GA implementation
- DONE fitness function
2021-06-20 23:43:57 +02:00
6c905621ca Merge pull request 'GA implementation in env' (#1) from Paweł into master
Reviewed-on: s452664/Sztuczna_Inteligencja-projekt#1
2021-06-20 18:03:50 +02:00
aca811e228 GA implementation in env
- use ga in traktor environment on field variables
- adjusted fitness function
2021-06-20 18:02:11 +02:00
14795cdc5e Changed method for accuracy calculation: 2021-06-20 15:04:51 +02:00
3898a3bcab Update network model structure:
Changed model from FCNN to CNN
2021-06-20 15:00:34 +02:00
0b2791f961 Genetic Algorithm
- added Genetic Algorithm to adjust
2021-06-20 13:24:12 +02:00
328b171e5a file namechange
- changed name of files to represent inside
- added main to seperate processes
- changed raw code to functions
2021-06-15 01:36:18 +02:00
f993a577f4 Reorganising files:
- added AI dictionary with AI classes and functions
- added src directory with raw data or simple classes
- removed unused libraries
2021-06-14 23:54:30 +02:00
64 changed files with 1116 additions and 672 deletions

156
AI/GeneticAlgorithm.py Normal file
View File

@ -0,0 +1,156 @@
import keyboard as keyboard
import field as F
from ga_methods import *
from src import mapschema as maps
# Genetic Algorithm
def genetic_algorithm_setup(field):
population_units = ["", "w", "p", "s"]
# TODO REPREZENTACJA OSOBNIKA - MACIERZ ROZKłADU PLONÓW
population_text = []
population_text_single = []
population_size = 10
# Populate the population_text array
for k in range(population_size):
population_text_single = []
for row in range(D.GSIZE):
population_text_single.append([])
for column in range(D.GSIZE):
population_text_single[row].append(random.choice(population_units))
population_text.append(population_text_single)
"""
Genetic algorithm parameters:
Mating pool size
Population size
"""
# units per population in generation
best_outputs = []
num_generations = 100
num_parents = 4
# iterative var
generation = 0
stop = 0
# TODO WARUNEK STOPU
while generation < num_generations and stop < 3:
if keyboard.is_pressed('space'):
generation += 1
print("Generation : ", generation)
# Measuring the fitness of each chromosome in the population.
# population Fitness
fitness = []
for i in range(0, population_size):
fitness.append((i, population_fitness(population_text[i], field, population_size)))
print("Fitness")
print(fitness)
best = sorted(fitness, key=lambda tup: tup[1], reverse=True)[0:num_parents]
# Leaderboard only
best_outputs.append(best[0][1])
# The best result in the current iteration.
print("Best result : ", best[0])
# TODO METODA WYBORU OSOBNIKA - RANKING
# Selecting the best parents in the population for mating.
parents = [population_text[i[0]] for i in best]
parents_copy = copy.deepcopy(parents)
print("Parents")
for i in range(0, len(parents)):
print('\n'.join([''.join(['{:4}'.format(item) for item in row])
for row in parents[i]]))
print("")
# Generating next generation using crossover.
offspring_x = random.randint(1, D.GSIZE - 2)
offspring_y = random.randint(1, D.GSIZE - 2)
# TODO OPERATOR KRZYŻOWANIA
offspring_crossover = crossover(parents)
print("Crossover")
for i in range(0, len(offspring_crossover)):
print('\n'.join([''.join(['{:4}'.format(item) for item in row])
for row in offspring_crossover[i]]))
print("")
# TODO OPERATOR MUTACJI
offspring_mutation = mutation(population_units, offspring_crossover, population_size - num_parents,
num_mutations=10)
print("Mutation")
for i in range(0, len(offspring_mutation)):
print('\n'.join([''.join(['{:4}'.format(item) for item in row])
for row in offspring_mutation[i]]))
print("")
population_text_copy = copy.deepcopy(population_text)
unused_indexes = [i for i in range(0, population_size) if i not in [j[0] for j in best]]
# Creating next generation
population_text = []
for k in parents_copy:
population_text.append(k)
for k in range(0, len(offspring_mutation)):
population_text.append(offspring_mutation[k])
while len(population_text) < population_size:
x = random.choice(unused_indexes)
population_text.append(population_text_copy[x])
unused_indexes.remove(x)
# TODO WARUNEK STOPU
stop = 0
if generation > 10:
if best_outputs[-1] / best_outputs[-2] < 1.001:
stop += 1
if best_outputs[-1] / best_outputs[-3] < 1.001:
stop += 1
if best_outputs[-2] / best_outputs[-3] < 1.001:
stop += 1
# final Fitness
fitness = []
for i in range(0, population_size):
fitness.append((i, population_fitness(population_text[i], field, population_size)))
print("Final Fitness")
print(fitness)
best = sorted(fitness, key=lambda tup: tup[1])[0:num_parents]
print("Best solution : ", )
for i in range(0, D.GSIZE):
print(population_text[best[0][0]][i])
print("Best solution fitness : ", best[0][1])
pretty_printer(best_outputs)
# TODO REALLY return best iteration of field
return 0
if __name__ == "__main__":
# Define the map of the field
mapschema = maps.createField()
# Create field array
field = []
# Populate the field array
for row in range(D.GSIZE):
field.append([])
for column in range(D.GSIZE):
fieldbit = F.Field(row, column, mapschema[column][row])
field[row].append(fieldbit)
genetic_algorithm_setup(field)

48
AI/NN_accuracy.py Normal file
View File

@ -0,0 +1,48 @@
import torch
import torchvision
import torchvision.transforms as transforms
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import numpy as np
from matplotlib.pyplot import imshow
import os
import PIL
import numpy as np
from matplotlib.pyplot import imshow
import neural_network
from matplotlib.pyplot import imshow
# wcześniej grader.py
# Get accuracy for neural_network model 'network_model.pth'
def NN_accuracy():
# Create the model
net = neural_network.Net()
# Load state_dict
neural_network.load_network_from_structure(net)
# Set model to eval
net.eval()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
folderlist = os.listdir(os.path.dirname(__file__) + "\\test")
tested = 0
correct = 0
for folder in folderlist:
for file in os.listdir(os.path.dirname(__file__) + "\\test\\" + folder):
if neural_network.result_from_network(net, os.path.dirname(__file__) + "\\test\\" + folder + "\\" + file) == folder:
correct += 1
tested += 1
else:
tested += 1
print(correct/tested)
if __name__ == "__main__":
NN_accuracy()

87
AI/decision_tree.py Normal file
View File

@ -0,0 +1,87 @@
# used in Plant
def decision_tree(plant):
if plant.field.hydration == 4:
if plant.is_healthy == 1:
if plant.field.tractor_there == 0:
if plant.ticks == 0:
return 0
elif plant.ticks == 1:
return 1
elif plant.field.tractor_there == 1:
return 0
elif plant.is_healthy == 0:
return 0
elif plant.field.hydration == 2:
if plant.species == "sorrel":
if plant.ticks == 1:
if plant.is_healthy == 1:
return 1
elif plant.is_healthy == 0:
return 0
elif plant.ticks == 0:
return 0
elif plant.species == "potato":
return 0
elif plant.species == "wheat":
return 0
elif plant.species == "strawberry":
return 0
elif plant.field.hydration == 1:
if plant.species == "potato":
return 0
elif plant.species == "strawberry":
if plant.ticks == 1:
return -1
elif plant.ticks == 0:
return 0
elif plant.species == "wheat":
return 0
elif plant.species == "sorrel":
if plant.is_healthy == 0:
return 0
elif plant.is_healthy == 1:
if plant.field.tractor_there == 0:
if plant.ticks == 0:
return 0
elif plant.ticks == 1:
return 1
elif plant.field.tractor_there == 1:
return 0
elif plant.field.hydration == 3:
if plant.ticks == 1:
if plant.field.tractor_there == 0:
if plant.is_healthy == 1:
if plant.species == "potato":
if plant.field.fertility == 1:
return 1
elif plant.field.fertility == 0:
return 0
elif plant.species == "strawberry":
return 1
elif plant.species == "sorrel":
return 1
elif plant.species == "wheat":
return 1
elif plant.is_healthy == 0:
return 0
elif plant.field.tractor_there == 1:
return 0
elif plant.ticks == 0:
return 0
elif plant.field.hydration == 5:
if plant.field.tractor_there == 1:
return 0
elif plant.field.tractor_there == 0:
if plant.is_healthy == 0:
return 0
elif plant.is_healthy == 1:
if plant.ticks == 1:
return 1
elif plant.ticks == 0:
return 0
elif plant.field.hydration == 0:
if plant.ticks == 0:
return 0
elif plant.ticks == 1:
return -1

103
AI/ga_methods.py Normal file
View File

@ -0,0 +1,103 @@
import copy
import random
import matplotlib
import matplotlib.pyplot
import numpy
import src.dimensions as D
# Genetic Algorithm methods
def local_fitness(field, x, y, plants_case):
soil_value = 0
if field[x][y].field_type == "soil":
soil_value = 1
else:
soil_value = 0.5
if plants_case[x][y] == "":
plant_value = 0
elif plants_case[x][y] == "w":
plant_value = 1
elif plants_case[x][y] == "p":
plant_value = 2
elif plants_case[x][y] == "s":
plant_value = 3
else:
plant_value = 1
neighbour_bonus = 1
if x - 1 >= 0:
if plants_case[x][y] == plants_case[x - 1][y]:
neighbour_bonus += 1
if x + 1 < D.GSIZE:
if plants_case[x][y] == plants_case[x + 1][y]:
neighbour_bonus += 1
if y - 1 >= 0:
if plants_case[x][y] == plants_case[x][y - 1]:
neighbour_bonus += 1
if y + 1 < D.GSIZE:
if plants_case[x][y] == plants_case[x][y + 1]:
neighbour_bonus += 1
local_fitness_value = (soil_value + plant_value) * (0.5 * neighbour_bonus + 1)
return local_fitness_value
def population_fitness(population_text_local, field, population_size):
# Calculating the fitness value of each solution in the current population.
# The fitness function calulates the sum of products between each input and its corresponding weight.
fitness = []
for k in range(population_size):
population_values_single = []
population_values_single_row = []
fitness_row = []
for i in range(0, D.GSIZE):
for j in range(0, D.GSIZE):
population_values_single_row.append(local_fitness(field, i, j, population_text_local))
population_values_single.append(population_values_single_row)
for i in range(D.GSIZE):
fitness_row.append(sum(population_values_single[i]))
fitness = sum(fitness_row)
return fitness
def crossover(local_parents):
ret = []
for i in range(0, len(local_parents)):
child = copy.deepcopy(local_parents[i])
# Vertical randomization
width = random.randint(1, D.GSIZE // len(local_parents)) # width of stripes
indexes_parents = numpy.random.permutation(range(0, len(local_parents))) # sorting of stripes
beginning = random.randint(0, len(local_parents[0]) - width * len(
local_parents)) # point we start putting the stripes from
for x in indexes_parents:
child[beginning:beginning + width] = local_parents[x][beginning:beginning + width]
beginning += width
ret.append(child)
return ret
def mutation(population_units, offspring_crossover, num_mutants, num_mutations=10):
for case in range(0, len(offspring_crossover)):
for mutation in range(0, num_mutations):
mutation_x = random.randint(0, D.GSIZE - 1)
mutation_y = random.randint(0, D.GSIZE - 1)
mutation_value = random.choice(population_units)
offspring_crossover[case][mutation_x][mutation_y] = mutation_value
num_mutants -= 1
return offspring_crossover
def pretty_printer(best_outputs):
matplotlib.pyplot.plot(best_outputs)
matplotlib.pyplot.xlabel("Iteration")
matplotlib.pyplot.ylabel("Fitness")
matplotlib.pyplot.show()

View File

@ -1,30 +1,35 @@
from cases import *
from collections import Counter
import operator
from types import prepare_class
import numpy as np
import copy import copy
import operator
from collections import Counter
import numpy as np
from src.cases import *
class Node: class Node:
def __init__(self, Class, tag=None): def __init__(self, Class, tag=None):
self.Class = Class self.Class = Class
self.childs = [] self.childs = []
def classes_of_cases (cases):
def classes_of_cases(cases):
classes = [] classes = []
for case in cases: for case in cases:
if case.Class not in classes: if case.Class not in classes:
classes.append(case.Class) classes.append(case.Class)
return classes return classes
def count_classes (cases):
def count_classes(cases):
classes = [] classes = []
for case in cases: for case in cases:
classes.append(case.Class) classes.append(case.Class)
c = Counter(classes) c = Counter(classes)
return max(c.items(), key=operator.itemgetter(1))[0] return max(c.items(), key=operator.itemgetter(1))[0]
def chose_attribute (cases, attributes):
def chose_attribute(cases, attributes):
a = "" a = ""
max = float("-inf") max = float("-inf")
for attribute in attributes: for attribute in attributes:
@ -33,7 +38,8 @@ def chose_attribute (cases, attributes):
a = attribute a = attribute
return a return a
def I (cases):
def I(cases):
i = 0 i = 0
all = len(cases) all = len(cases)
classes = classes_of_cases(cases) classes = classes_of_cases(cases)
@ -42,9 +48,10 @@ def I (cases):
for case in cases: for case in cases:
if case.Class == Class: if case.Class == Class:
noc += 1 noc += 1
i -= (noc/all)*np.log2(noc/all) i -= (noc / all) * np.log2(noc / all)
return i return i
def E(cases, attribute): def E(cases, attribute):
e = 0 e = 0
values = [] values = []
@ -57,7 +64,7 @@ def E(cases, attribute):
for case in cases: for case in cases:
if case.values[index] == value: if case.values[index] == value:
ei.append(case) ei.append(case)
e += (len(ei)/len(cases))*I(ei) e += (len(ei) / len(cases)) * I(ei)
return e return e
@ -86,15 +93,16 @@ def treelearn(cases, attributes, default_class):
for case in cases: for case in cases:
if case.values[index] == value: if case.values[index] == value:
new_case = copy.deepcopy(case) new_case = copy.deepcopy(case)
new_case.values = case.values[:index] + case.values[index+1:] new_case.values = case.values[:index] + case.values[index + 1:]
new_case.attributes = case.attributes[:index] + case.attributes[index+1:] new_case.attributes = case.attributes[:index] + case.attributes[index + 1:]
new_cases.append(new_case) new_cases.append(new_case)
new_attributes = attributes[:index] + attributes[index+1 :] new_attributes = attributes[:index] + attributes[index + 1:]
child = treelearn(new_cases, new_attributes, new_default_class) child = treelearn(new_cases, new_attributes, new_default_class)
t.childs.append([child, value]) t.childs.append([child, value])
return t return t
def pretty_print(root, n): def pretty_print(root, n):
if len(root.childs) == 0: if len(root.childs) == 0:
for _ in range(n): for _ in range(n):
@ -104,19 +112,15 @@ def pretty_print(root, n):
for _ in range(n): for _ in range(n):
print(" ", end="") print(" ", end="")
if child != root.childs[0]: if child != root.childs[0]:
print("el", end= "") print("el", end="")
if len(str(child[1])) > 1: if len(str(child[1])) > 1:
print("if self." + str(root.Class) + " == \"" + str(child[1]) + "\":") print("if self." + str(root.Class) + " == \"" + str(child[1]) + "\":")
else: else:
print("if self." + str(root.Class) + " == " + str(child[1]) + ":") print("if self." + str(root.Class) + " == " + str(child[1]) + ":")
pretty_print(child[0], n+1) pretty_print(child[0], n + 1)
tree = treelearn(cases, attributes, 0)
pretty_print(tree, 0)
# Get view of decision_tree.py
if __name__ == "__main__":
tree = treelearn(cases, attributes, 0)
pretty_print(tree, 0)

View File

@ -2,7 +2,7 @@ import torch
import torchvision import torchvision
import torchvision.transforms as transforms import torchvision.transforms as transforms
import torch.nn as nn import torch.nn as nn
import torch.nn.functional as f import torch.nn.functional as F
import torch.optim as optim import torch.optim as optim
import numpy as np import numpy as np
from matplotlib.pyplot import imshow from matplotlib.pyplot import imshow
@ -22,37 +22,46 @@ class Negative(object):
def __call__(self, img): def __call__(self, img):
return to_negative(img) return to_negative(img)
def plotdigit(image):
img = np.reshape(image, (-1, 100))
imshow(img, cmap='Greys')
transform = transforms.Compose([Negative(), transforms.ToTensor()]) transform = transforms.Compose([Negative(), transforms.ToTensor()])
train_set = torchvision.datasets.ImageFolder(root='train', transform=transform) train_set = torchvision.datasets.ImageFolder(root='train', transform=transform)
classes = ("apple", "potato") classes = ("pepper", "potato", "strawberry", "tomato")
BATCH_SIZE = 2 BATCH_SIZE = 4
train_loader = torch.utils.data.DataLoader(train_set, batch_size=BATCH_SIZE, shuffle=True, num_workers=0) train_loader = torch.utils.data.DataLoader(train_set, batch_size=BATCH_SIZE, shuffle=True, num_workers=0)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
class Net(nn.Module): class Net(nn.Module):
def __init__(self): def __init__(self):
super(Net, self).__init__() super().__init__()
self.flatten = nn.Flatten() self.network = nn.Sequential(
self.linear_relu_stack = nn.Sequential( nn.Conv2d(3, 32, kernel_size=3, padding=1), #3 channels to 32 channels
nn.Linear(3*100*100, 512),
nn.ReLU(), nn.ReLU(),
nn.Linear(512, 512), nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
nn.ReLU(), nn.ReLU(),
nn.Linear(512, 2), nn.MaxPool2d(2, 2), # output: 64 channels x 50 x 50 image size - decrease
nn.ReLU()
)
self.linear_relu_stack = self.linear_relu_stack.to(device)
def forward(self, x): nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
x = self.flatten(x).to(device) nn.ReLU(),
logits = self.linear_relu_stack(x).to(device) nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1), #increase power of model
return logits nn.ReLU(),
nn.MaxPool2d(2, 2), # output: 128 x 25 x 25
nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(5, 5), # output: 256 x 5 x 5
nn.Flatten(), #a single vector 256*5*5,
nn.Linear(256*5*5, 1024),
nn.ReLU(),
nn.Linear(1024, 512),
nn.ReLU(),
nn.Linear(512, 4))
def forward(self, xb):
return self.network(xb)
def training_network(): def training_network():
net = Net() net = Net()
@ -61,7 +70,7 @@ def training_network():
criterion = nn.CrossEntropyLoss() criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
for epoch in range(4): for epoch in range(10):
running_loss = 0.0 running_loss = 0.0
for i, data in enumerate(train_loader, 0): for i, data in enumerate(train_loader, 0):
inputs, labels = data[0].to(device), data[1].to(device) inputs, labels = data[0].to(device), data[1].to(device)
@ -72,7 +81,7 @@ def training_network():
optimizer.step() optimizer.step()
running_loss += loss.item() running_loss += loss.item()
if i % 2000 == 1999: if i % 200 == 199:
print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss)) print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss))
running_loss = 0.0 running_loss = 0.0
@ -82,8 +91,8 @@ def training_network():
def result_from_network(net, loaded_image): def result_from_network(net, loaded_image):
image = PIL.Image.open(loaded_image) image = PIL.Image.open(loaded_image)
pil_to_tensor = transforms.ToTensor()(image.convert("RGB")).unsqueeze_(0) pil_to_tensor = transforms.Compose([Negative(), transforms.ToTensor()])(image.convert("RGB")).unsqueeze_(0)
outputs = net(pil_to_tensor.to(device)) outputs = net(pil_to_tensor)
return classes[torch.max(outputs, 1)[1]] return classes[torch.max(outputs, 1)[1]]
@ -100,4 +109,3 @@ def load_network_from_structure(network):
if __name__ == "__main__": if __name__ == "__main__":
print(torch.cuda.is_available()) print(torch.cuda.is_available())
training_network() training_network()

View File

@ -1,156 +0,0 @@
# Import the pygame module
import pygame
# Import pygame.locals for easier access to key coordinates
from pygame.locals import (
K_UP,
K_LEFT,
K_RIGHT,
K_ESCAPE,
KEYDOWN,
QUIT
)
# Import other files from project
import field as F
import tractor as T
import plant as P
import colors as C
import dimensions as D
import node as N
import mapschema as maps
# Initialize pygame
pygame.init()
# Name the window
pygame.display.set_caption("Inteligentny Traktor")
# Create the screen object
# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
screen = pygame.display.set_mode((D.SCREEN_WIDTH, D.SCREEN_HEIGHT))
# Define the map of the field
mapschema = maps.createField()
# Create field array
field = []
# Populate the field array
for row in range(D.GSIZE):
field.append([])
for column in range(D.GSIZE):
fieldbit = F.Field(row, column, mapschema[column][row])
field[row].append(fieldbit)
# Create Tractor object
tractor = T.Tractor(field, [0,0])
# Define the map of plants
mapschema = maps.createPlants()
# Createt plants array
plants = []
# Populate the plants array
for row in range(D.GSIZE):
plants.append([])
for column in range(D.GSIZE):
if mapschema[column][row] != 0:
plantbit = P.Plant(field[row][column], mapschema[column][row])
plants[row].append(plantbit)
# Create list for tractor instructions
path = []
# Variable to keep the main loop running
RUNNING = True
# Variable conroling timed eventes
TICKER = 0
# Initialize clock
clock = pygame.time.Clock()
# Main loop
while RUNNING:
# Look at every event in the queue
for event in pygame.event.get():
# Did the user hit a key?
if event.type == KEYDOWN:
# Was it the Escape key? If so, stop the loop.
if event.key == K_ESCAPE:
RUNNING = False
# Did the user click the window close button? If so, stop the loop.
elif event.type == QUIT:
RUNNING = False
# Create key Node that will be used to calculate tractor instructions
processor = N.Node(field, tractor.position, tractor.direction)
# If path is empty or nonexistent, create new one
if path is None or len(path) == 0:
path = processor.findPathToPlant()
# control tractor by poping instructions from path list
if path is not None:
if path[0] == "move":
tractor.move()
path.pop(0)
elif path[0] =="left":
tractor.rotate_left()
path.pop(0)
elif path[0] == "right":
tractor.rotate_right()
path.pop(0)
elif path[0] == "hydrate":
tractor.hydrate(field)
path.pop(0)
else:
path.pop(0)
# Get all keys pressed at a time CURRENTLY UNUSED
pressed_keys = pygame.key.get_pressed()
# control tractor with pressed keys CURRENTLY UNUSED
if pressed_keys[K_UP]:
tractor.move()
elif pressed_keys[K_LEFT]:
tractor.rotate_left()
elif pressed_keys[K_RIGHT]:
tractor.rotate_right()
# Set the screen background
screen.fill(C.DBROWN)
# Draw the field
for row in range(D.GSIZE):
for column in range(D.GSIZE):
screen.blit(field[row][column].surf, field[row][column].rect)
# Draw the tactor
screen.blit(tractor.surf, tractor.rect)
# Plants grow with every 10th tick, then they are drawn
for row in plants:
for plant in row:
plant.tick()
plant.grow()
screen.blit(plant.surf, plant.rect)
# Field are drying with every 100th tick
if TICKER == 0:
for row in range(D.GSIZE):
for column in range(D.GSIZE):
field[row][column].dehydrate()
# Increment ticker
TICKER = (TICKER + 1)%100
# Update the screen
pygame.display.flip()
# Ensure program maintains a stable framerate
clock.tick(8)

259
cases.py
View File

@ -1,259 +0,0 @@
class Case:
def __init__(self, values, attributes, Class):
self.values = values
self.attributes = attributes
self.Class = Class
attributes = ["field.hydration", "field.fertility", "species", "ticks", "is_healthy", "field.tractor_there"]
cases = [Case([4, 0, "potato", 0, 1, 0], attributes, 0),
Case([2, 0, "sorrel", 1, 1, 0], attributes, 1),
Case([4, 1, "wheat", 0, 0, 1], attributes, 0),
Case([1, 1, "potato", 1, 0, 1], attributes, 0),
Case([2, 1, "potato", 0, 0, 1], attributes, 0),
Case([2, 0, "potato", 0, 1, 0], attributes, 0),
Case([1, 1, "strawberry", 1, 0, 1], attributes, -1),
Case([1, 0, "wheat", 1, 1, 1], attributes, 0),
Case([2, 0, "wheat", 1, 0, 1], attributes, 0),
Case([1, 1, "strawberry", 0, 1, 1], attributes, 0),
Case([3, 1, "potato", 1, 1, 0], attributes, 1),
Case([2, 1, "strawberry", 1, 0, 0], attributes, 0),
Case([4, 0, "wheat", 1, 1, 1], attributes, 0),
Case([4, 1, "wheat", 1, 0, 1], attributes, 0),
Case([5, 1, "potato", 1, 1, 1], attributes, 0),
Case([4, 0, "strawberry", 1, 0, 0], attributes, 0),
Case([1, 1, "sorrel", 1, 0, 0], attributes, 0),
Case([0, 0, "sorrel", 0, 0, 1], attributes, 0),
Case([2, 0, "sorrel", 1, 1, 0], attributes, 1),
Case([0, 1, "sorrel", 0, 1, 1], attributes, 0),
Case([0, 1, "strawberry", 1, 1, 0], attributes, -1),
Case([2, 0, "sorrel", 0, 1, 1], attributes, 0),
Case([4, 0, "wheat", 1, 0, 1], attributes, 0),
Case([5, 0, "wheat", 0, 0, 0], attributes, 0),
Case([0, 0, "strawberry", 1, 0, 0], attributes, -1),
Case([4, 0, "sorrel", 1, 0, 0], attributes, 0),
Case([3, 0, "sorrel", 0, 0, 1], attributes, 0),
Case([3, 1, "potato", 1, 0, 1], attributes, 0),
Case([4, 1, "potato", 0, 0, 1], attributes, 0),
Case([1, 1, "wheat", 0, 1, 1], attributes, 0),
Case([3, 0, "wheat", 0, 1, 1], attributes, 0),
Case([2, 0, "wheat", 0, 0, 0], attributes, 0),
Case([5, 1, "potato", 1, 1, 1], attributes, 0),
Case([4, 1, "strawberry", 0, 0, 1], attributes, 0),
Case([1, 0, "potato", 1, 1, 0], attributes, 0),
Case([4, 1, "sorrel", 1, 1, 1], attributes, 0),
Case([0, 1, "sorrel", 0, 0, 0], attributes, 0),
Case([4, 0, "strawberry", 1, 0, 0], attributes, 0),
Case([4, 0, "sorrel", 1, 0, 0], attributes, 0),
Case([5, 0, "strawberry", 0, 1, 1], attributes, 0),
Case([3, 1, "wheat", 1, 1, 1], attributes, 0),
Case([3, 0, "strawberry", 0, 1, 1], attributes, 0),
Case([4, 0, "potato", 0, 0, 1], attributes, 0),
Case([5, 1, "wheat", 1, 1, 1], attributes, 0),
Case([3, 0, "strawberry", 0, 1, 1], attributes, 0),
Case([3, 0, "sorrel", 0, 0, 1], attributes, 0),
Case([0, 0, "potato", 1, 1, 1], attributes, -1),
Case([4, 0, "strawberry", 1, 1, 1], attributes, 0),
Case([2, 1, "strawberry", 1, 0, 1], attributes, 0),
Case([2, 1, "wheat", 0, 0, 1], attributes, 0),
Case([2, 1, "sorrel", 1, 1, 0], attributes, 1),
Case([1, 0, "potato", 1, 0, 1], attributes, 0),
Case([4, 0, "strawberry", 1, 0, 0], attributes, 0),
Case([4, 1, "potato", 1, 1, 1], attributes, 0),
Case([0, 0, "strawberry", 1, 0, 1], attributes, -1),
Case([0, 1, "wheat", 0, 0, 0], attributes, 0),
Case([1, 1, "wheat", 0, 1, 0], attributes, 0),
Case([0, 0, "sorrel", 1, 1, 0], attributes, -1),
Case([2, 0, "sorrel", 0, 0, 1], attributes, 0),
Case([5, 1, "wheat", 1, 1, 1], attributes, 0),
Case([2, 0, "strawberry", 0, 1, 0], attributes, 0),
Case([2, 1, "wheat", 0, 0, 1], attributes, 0),
Case([3, 0, "potato", 1, 0, 1], attributes, 0),
Case([5, 0, "wheat", 1, 1, 1], attributes, 0),
Case([0, 1, "strawberry", 1, 0, 0], attributes, -1),
Case([0, 0, "wheat", 0, 0, 0], attributes, 0),
Case([5, 0, "potato", 1, 1, 0], attributes, 1),
Case([2, 0, "strawberry", 0, 1, 1], attributes, 0),
Case([3, 1, "sorrel", 0, 1, 0], attributes, 0),
Case([2, 1, "potato", 1, 1, 1], attributes, 0),
Case([5, 0, "strawberry", 1, 1, 0], attributes, 1),
Case([5, 0, "wheat", 0, 0, 0], attributes, 0),
Case([5, 0, "wheat", 1, 1, 0], attributes, 1),
Case([2, 0, "potato", 1, 0, 0], attributes, 0),
Case([3, 1, "wheat", 0, 1, 0], attributes, 0),
Case([3, 0, "potato", 1, 1, 1], attributes, 0),
Case([0, 1, "sorrel", 1, 1, 1], attributes, -1),
Case([0, 0, "strawberry", 1, 1, 1], attributes, -1),
Case([2, 1, "strawberry", 0, 1, 0], attributes, 0),
Case([0, 1, "sorrel", 1, 1, 0], attributes, -1),
Case([3, 0, "wheat", 0, 1, 1], attributes, 0),
Case([4, 0, "strawberry", 1, 0, 1], attributes, 0),
Case([3, 1, "potato", 0, 0, 1], attributes, 0),
Case([1, 1, "sorrel", 0, 0, 0], attributes, 0),
Case([5, 1, "wheat", 1, 0, 1], attributes, 0),
Case([5, 0, "potato", 1, 0, 1], attributes, 0),
Case([3, 0, "potato", 0, 0, 1], attributes, 0),
Case([1, 0, "wheat", 0, 1, 0], attributes, 0),
Case([5, 0, "sorrel", 0, 1, 1], attributes, 0),
Case([4, 0, "potato", 0, 1, 0], attributes, 0),
Case([0, 0, "strawberry", 0, 0, 0], attributes, 0),
Case([5, 0, "sorrel", 1, 1, 0], attributes, 1),
Case([4, 1, "sorrel", 0, 0, 0], attributes, 0),
Case([1, 1, "strawberry", 1, 1, 0], attributes, -1),
Case([5, 0, "strawberry", 1, 0, 0], attributes, 0),
Case([5, 1, "wheat", 0, 0, 0], attributes, 0),
Case([0, 1, "sorrel", 1, 1, 0], attributes, -1),
Case([3, 1, "potato", 1, 0, 1], attributes, 0),
Case([1, 0, "sorrel", 0, 0, 0], attributes, 0),
Case([3, 0, "wheat", 0, 1, 1], attributes, 0),
Case([0, 0, "wheat", 0, 0, 1], attributes, 0),
Case([1, 0, "potato", 0, 0, 0], attributes, 0),
Case([1, 1, "sorrel", 0, 1, 0], attributes, 0),
Case([0, 1, "strawberry", 0, 1, 0], attributes, 0),
Case([5, 1, "potato", 1, 0, 1], attributes, 0),
Case([2, 0, "strawberry", 1, 1, 1], attributes, 0),
Case([4, 1, "wheat", 1, 0, 0], attributes, 0),
Case([0, 1, "sorrel", 1, 1, 0], attributes, -1),
Case([1, 0, "strawberry", 1, 1, 1], attributes, -1),
Case([4, 0, "wheat", 0, 0, 0], attributes, 0),
Case([4, 0, "strawberry", 0, 1, 0], attributes, 0),
Case([0, 0, "sorrel", 1, 0, 1], attributes, -1),
Case([1, 0, "strawberry", 0, 1, 0], attributes, 0),
Case([0, 0, "strawberry", 1, 1, 1], attributes, -1),
Case([2, 1, "potato", 0, 0, 0], attributes, 0),
Case([3, 1, "strawberry", 1, 1, 0], attributes, 1),
Case([1, 0, "sorrel", 1, 1, 1], attributes, 0),
Case([5, 1, "strawberry", 1, 1, 0], attributes, 1),
Case([2, 0, "wheat", 1, 1, 1], attributes, 0),
Case([5, 1, "strawberry", 0, 1, 0], attributes, 0),
Case([1, 1, "wheat", 0, 1, 1], attributes, 0),
Case([1, 1, "potato", 0, 1, 0], attributes, 0),
Case([4, 0, "potato", 1, 1, 0], attributes, 1),
Case([2, 1, "strawberry", 0, 0, 1], attributes, 0),
Case([0, 1, "potato", 0, 0, 1], attributes, 0),
Case([3, 0, "sorrel", 1, 0, 1], attributes, 0),
Case([4, 0, "wheat", 1, 0, 1], attributes, 0),
Case([5, 1, "sorrel", 1, 0, 0], attributes, 0),
Case([1, 0, "sorrel", 1, 0, 0], attributes, 0),
Case([5, 0, "sorrel", 1, 0, 1], attributes, 0),
Case([2, 1, "potato", 1, 0, 1], attributes, 0),
Case([1, 0, "potato", 1, 1, 1], attributes, 0),
Case([4, 0, "wheat", 1, 1, 0], attributes, 1),
Case([0, 0, "sorrel", 0, 1, 0], attributes, 0),
Case([2, 0, "wheat", 0, 0, 1], attributes, 0),
Case([0, 1, "potato", 1, 0, 1], attributes, -1),
Case([0, 1, "sorrel", 1, 0, 1], attributes, -1),
Case([1, 1, "potato", 0, 0, 1], attributes, 0),
Case([3, 0, "sorrel", 1, 1, 1], attributes, 0),
Case([0, 0, "potato", 1, 0, 1], attributes, -1),
Case([4, 0, "potato", 0, 0, 1], attributes, 0),
Case([0, 0, "strawberry", 1, 0, 0], attributes, -1),
Case([0, 0, "strawberry", 1, 1, 1], attributes, -1),
Case([5, 0, "potato", 0, 1, 1], attributes, 0),
Case([2, 0, "potato", 0, 0, 0], attributes, 0),
Case([0, 1, "potato", 1, 0, 0], attributes, -1),
Case([1, 0, "potato", 0, 0, 1], attributes, 0),
Case([4, 0, "sorrel", 1, 0, 1], attributes, 0),
Case([1, 0, "potato", 1, 0, 0], attributes, 0),
Case([5, 1, "potato", 1, 0, 1], attributes, 0),
Case([2, 1, "wheat", 1, 0, 0], attributes, 0),
Case([0, 1, "potato", 1, 1, 1], attributes, -1),
Case([5, 1, "strawberry", 0, 1, 0], attributes, 0),
Case([3, 0, "strawberry", 0, 1, 1], attributes, 0),
Case([3, 0, "strawberry", 1, 0, 0], attributes, 0),
Case([0, 1, "strawberry", 0, 0, 1], attributes, 0),
Case([0, 1, "wheat", 0, 0, 0], attributes, 0),
Case([0, 1, "strawberry", 1, 0, 0], attributes, -1),
Case([1, 0, "potato", 0, 0, 1], attributes, 0),
Case([1, 1, "wheat", 0, 0, 0], attributes, 0),
Case([0, 1, "strawberry", 1, 0, 1], attributes, -1),
Case([1, 1, "potato", 0, 0, 1], attributes, 0),
Case([0, 0, "wheat", 0, 0, 1], attributes, 0),
Case([4, 1, "sorrel", 1, 1, 1], attributes, 0),
Case([5, 1, "wheat", 0, 0, 1], attributes, 0),
Case([5, 1, "strawberry", 0, 0, 0], attributes, 0),
Case([4, 1, "wheat", 0, 0, 1], attributes, 0),
Case([1, 1, "sorrel", 0, 0, 0], attributes, 0),
Case([1, 1, "potato", 1, 1, 0], attributes, 0),
Case([0, 1, "sorrel", 1, 0, 0], attributes, -1),
Case([5, 0, "sorrel", 1, 0, 0], attributes, 0),
Case([0, 0, "strawberry", 1, 1, 1], attributes, -1),
Case([0, 0, "potato", 0, 0, 0], attributes, 0),
Case([0, 0, "strawberry", 1, 1, 1], attributes, -1),
Case([4, 0, "strawberry", 0, 0, 1], attributes, 0),
Case([2, 0, "potato", 1, 0, 0], attributes, 0),
Case([4, 0, "strawberry", 0, 0, 0], attributes, 0),
Case([0, 1, "strawberry", 0, 1, 1], attributes, 0),
Case([1, 1, "wheat", 1, 1, 0], attributes, 0),
Case([3, 0, "potato", 0, 1, 0], attributes, 0),
Case([1, 1, "wheat", 0, 1, 0], attributes, 0),
Case([1, 1, "sorrel", 0, 0, 1], attributes, 0),
Case([3, 1, "wheat", 0, 0, 0], attributes, 0),
Case([3, 1, "wheat", 0, 0, 0], attributes, 0),
Case([1, 0, "wheat", 0, 1, 1], attributes, 0),
Case([5, 1, "potato", 1, 0, 1], attributes, 0),
Case([5, 0, "wheat", 0, 0, 1], attributes, 0),
Case([2, 1, "sorrel", 0, 1, 0], attributes, 0),
Case([5, 0, "sorrel", 1, 0, 0], attributes, 0),
Case([1, 0, "potato", 1, 1, 1], attributes, 0),
Case([5, 1, "wheat", 1, 1, 0], attributes, 1),
Case([3, 1, "sorrel", 0, 0, 0], attributes, 0),
Case([5, 0, "wheat", 0, 0, 1], attributes, 0),
Case([2, 0, "strawberry", 0, 1, 1], attributes, 0),
Case([1, 0, "potato", 1, 1, 1], attributes, 0),
Case([0, 1, "sorrel", 0, 0, 0], attributes, 0),
Case([5, 1, "potato", 1, 1, 0], attributes, 1),
Case([2, 0, "wheat", 0, 1, 0], attributes, 0),
Case([3, 0, "strawberry", 0, 1, 1], attributes, 0),
Case([3, 0, "potato", 1, 1, 0], attributes, 0),
Case([1, 1, "potato", 0, 0, 0], attributes, 0),
Case([2, 1, "potato", 0, 1, 0], attributes, 0),
Case([2, 1, "wheat", 0, 1, 0], attributes, 0),
Case([2, 0, "sorrel", 1, 1, 0], attributes, 1),
Case([3, 0, "strawberry", 0, 1, 1], attributes, 0),
Case([2, 0, "wheat", 1, 1, 1], attributes, 0),
Case([5, 0, "wheat", 1, 1, 0], attributes, 1),
Case([1, 1, "sorrel", 0, 0, 0], attributes, 0),
Case([0, 1, "potato", 0, 0, 0], attributes, 0),
Case([5, 1, "strawberry", 1, 1, 1], attributes, 0),
Case([4, 1, "wheat", 1, 0, 0], attributes, 0),
Case([5, 1, "sorrel", 0, 0, 1], attributes, 0),
Case([1, 1, "wheat", 1, 0, 0], attributes, 0),
Case([5, 0, "strawberry", 1, 0, 1], attributes, 0),
Case([5, 0, "wheat", 1, 0, 1], attributes, 0),
Case([2, 0, "potato", 1, 0, 1], attributes, 0),
Case([3, 1, "wheat", 1, 0, 0], attributes, 0),
Case([0, 1, "strawberry", 1, 0, 0], attributes, -1),
Case([0, 1, "strawberry", 0, 1, 1], attributes, 0),
Case([3, 0, "wheat", 0, 1, 0], attributes, 0),
Case([4, 1, "potato", 1, 1, 1], attributes, 0),
Case([3, 0, "potato", 0, 0, 1], attributes, 0),
Case([2, 1, "strawberry", 1, 1, 0], attributes, 0),
Case([1, 1, "sorrel", 0, 0, 0], attributes, 0),
Case([4, 1, "wheat", 1, 0, 1], attributes, 0),
Case([2, 0, "potato", 0, 1, 0], attributes, 0),
Case([5, 0, "sorrel", 0, 1, 1], attributes, 0),
Case([0, 1, "wheat", 1, 1, 0], attributes, -1),
Case([5, 1, "wheat", 1, 0, 0], attributes, 0),
Case([2, 0, "potato", 0, 0, 0], attributes, 0),
Case([2, 0, "strawberry", 0, 1, 1], attributes, 0),
Case([4, 1, "potato", 0, 1, 1], attributes, 0),
Case([0, 1, "sorrel", 1, 1, 0], attributes, -1),
Case([1, 1, "strawberry", 1, 0, 1], attributes, -1),
Case([3, 0, "sorrel", 1, 1, 0], attributes, 1),
Case([5, 1, "wheat", 1, 0, 0], attributes, 0),
Case([4, 0, "sorrel", 1, 1, 0], attributes, 1),
Case([2, 1, "sorrel", 1, 0, 0], attributes, 0),
Case([0, 1, "wheat", 0, 1, 0], attributes, 0),
Case([5, 0, "potato", 1, 1, 0], attributes, 1),
Case([3, 1, "strawberry", 0, 1, 0], attributes, 0),
Case([5, 1, "strawberry", 0, 0, 0], attributes, 0),
Case([4, 1, "potato", 1, 1, 1], attributes, 0),
Case([5, 1, "potato", 1, 0, 1], attributes, 0),
Case([5, 1, "potato", 1, 1, 1], attributes, 0),
Case([0, 0, "sorrel", 0, 0, 0], attributes, 0),
Case([1, 1, "sorrel", 1, 1, 0], attributes, 1),
Case([0, 1, "potato", 0, 1, 0], attributes, 0),
Case([4, 1, "strawberry", 1, 1, 1], attributes, 0),
Case([0, 0, "wheat", 0, 1, 1], attributes, 0),
Case([3, 0, "wheat", 1, 1, 0], attributes, 1)]

View File

@ -1,6 +1,6 @@
import pygame import pygame
from colors import * from src.colors import *
from dimensions import * from src.dimensions import *
class Field(pygame.sprite.Sprite): class Field(pygame.sprite.Sprite):
def __init__(self, row, column, field_type): def __init__(self, row, column, field_type):
@ -24,12 +24,25 @@ class Field(pygame.sprite.Sprite):
self.position = [row, column] self.position = [row, column]
self.hydration = 0 self.hydration = 0
self.planted = 0 self.planted = 0
self.fertility = 1 self.fertility = 0
self.tractor_there = False self.tractor_there = False
def hydrate(self): def hydrate(self):
if self.field_type == "soil" and self.hydration <= 5: if self.field_type == "soil" and self.hydration <= 5:
self.hydration += 1 self.hydration += 1
if self.fertility == 1:
if self.hydration == 0:
self.surf.fill(REDDISH0)
self.fertility = 0
if self.hydration == 1:
self.surf.fill(REDDISH1)
if self.hydration == 2:
self.surf.fill(REDDISH2)
if self.hydration == 3:
self.surf.fill(REDDISH3)
if self.hydration == 4 or self.hydration == 5:
self.surf.fill(REDDISH4)
else:
if self.hydration == 0: if self.hydration == 0:
self.surf.fill(BROWN0) self.surf.fill(BROWN0)
if self.hydration == 1: if self.hydration == 1:
@ -44,6 +57,19 @@ class Field(pygame.sprite.Sprite):
def dehydrate(self): def dehydrate(self):
if self.field_type == "soil" and self.hydration > 0: if self.field_type == "soil" and self.hydration > 0:
self.hydration -= 1 self.hydration -= 1
if self.fertility == 1:
if self.hydration == 0:
self.surf.fill(REDDISH0)
self.fertility = 0
if self.hydration == 1:
self.surf.fill(REDDISH1)
if self.hydration == 2:
self.surf.fill(REDDISH2)
if self.hydration == 3:
self.surf.fill(REDDISH3)
if self.hydration == 4 or self.hydration == 5:
self.surf.fill(REDDISH4)
else:
if self.hydration == 0: if self.hydration == 0:
self.surf.fill(BROWN0) self.surf.fill(BROWN0)
if self.hydration == 1: if self.hydration == 1:

View File

@ -1,65 +0,0 @@
import torch
import torchvision
import torchvision.transforms as transforms
import torch.nn as nn
import torch.nn.functional as f
import torch.optim as optim
import numpy as np
from matplotlib.pyplot import imshow
import os
import PIL
import numpy as np
import neural_network
from matplotlib.pyplot import imshow
# Create the model
model = neural_network.Net()
# Load state_dict
neural_network.load_network_from_structure(model)
# Create the preprocessing transformation here
transform = transforms.Compose([neural_network.Negative(), transforms.ToTensor()])
# load your image(s)
img = PIL.Image.open('test\\0_100.jpg')
img2 = PIL.Image.open('test\\1_100.jpg')
img3 = PIL.Image.open('test\\4_100.jpg')
img4 = PIL.Image.open('test\\5_100.jpg')
# Transform
input = transform(img)
input2 = transform(img2)
input3 = transform(img3)
input4 = transform(img4)
# unsqueeze batch dimension, in case you are dealing with a single image
input = input.unsqueeze(0)
input2 = input2.unsqueeze(0)
input3 = input3.unsqueeze(0)
input4 = input4.unsqueeze(0)
# Set model to eval
model.eval()
# Get prediction
output = model(input)
output2 = model(input2)
output3 = model(input3)
output4 = model(input4)
print(output)
index = output.cpu().data.numpy().argmax()
print(index)
print(output2)
index = output2.cpu().data.numpy().argmax()
print(index)
print(output3)
index = output3.cpu().data.numpy().argmax()
print(index)
print(output4)
index = output4.cpu().data.numpy().argmax()
print(index)

160
main.py Normal file
View File

@ -0,0 +1,160 @@
# Import the pygame module
import pygame
# Import pygame.locals for easier access to key coordinates
from pygame.locals import (
K_UP,
K_LEFT,
K_RIGHT,
K_ESCAPE,
KEYDOWN,
QUIT
)
# Import other files from project
import field as F
import node as N
import plant as P
import src.colors as C
import src.dimensions as D
import AI.GeneticAlgorithm as ga
import AI.neural_network as nn
import tractor as T
from src import mapschema as maps
if __name__ == "__main__":
# Initialize pygame
pygame.init()
# Name the window
pygame.display.set_caption("Inteligentny Traktor")
# Create the screen object
# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
screen = pygame.display.set_mode((D.SCREEN_WIDTH, D.SCREEN_HEIGHT))
# Define the map of the field
mapschema = maps.createField()
# Create field array
field = []
# Populate the field array
for row in range(D.GSIZE):
field.append([])
for column in range(D.GSIZE):
fieldbit = F.Field(row, column, mapschema[column][row])
field[row].append(fieldbit)
# genetic_algorithm_setup(field)
num_of_plants = 0
plant_pops = []
best_plant_pop = []
goal_gen = 100
best_plant_pop, plant_pops, num_of_plants, fitness = ga.genetic_algorithm_setup(field, plant_pops, goal_gen)
net = nn.Net()
nn.load_network_from_structure(net)
net.eval()
# Create Tractor object
tractor = T.Tractor(field, [0, 0])
# Define the map of plants
mapschema = maps.createPlants()
# Create plants array
plants = []
# Populate the plants array
for row in range(D.GSIZE):
plants.append([])
for column in range(D.GSIZE):
if best_plant_pop[column][row] != "":
plantbit = P.Plant(field[row][column], best_plant_pop[column][row])
plants[row].append(plantbit)
else:
plants[row].append(0)
# Create list for tractor instructions
path = []
# Variable to keep the main loop running
RUNNING = True
# Variable conroling timed eventes
TICKER = 0
# Initialize clock
clock = pygame.time.Clock()
# Main loop
while RUNNING:
for event in pygame.event.get():
# Did the user hit a key?
if event.type == KEYDOWN:
# Was it the Escape key? If so, stop the loop.
if event.key == K_ESCAPE:
RUNNING = False
# Did the user click the window close button? If so, stop the loop.
elif event.type == QUIT:
RUNNING = False
# Create key Node that will be used to calculate tractor instructions
processor = N.Node(field, tractor.position, tractor.direction)
# If path is empty or nonexistent, create new one
if path is None or len(path) == 0:
path = processor.findPathToPlant()
# control tractor by poping instructions from path list
if path is not None:
if path[0] == "move":
tractor.move()
elif path[0] == "left":
tractor.rotate_left()
elif path[0] == "right":
tractor.rotate_right()
elif path[0] == "hydrate":
tractor.hydrate(field)
elif path[0] == "fertilize":
if plants[tractor.position[1]][tractor.position[0]]:
tractor.fertilize(field, plants, nn.result_from_network(net, plants[tractor.position[0]][tractor.position[1]].testimage))
path.pop(0)
# Set the screen background
screen.fill(C.DBROWN)
# Draw the field
for row in range(D.GSIZE):
for column in range(D.GSIZE):
screen.blit(field[row][column].surf, field[row][column].rect)
# Draw the tactor
screen.blit(tractor.surf, tractor.rect)
# Plants grow with every 10th tick, then they are drawn
for row in plants:
for plant in row:
if plant != 0:
plant.tick()
plant.grow()
screen.blit(plant.surf, plant.rect)
# Field are drying with every 100th tick
if TICKER == 0:
for row in range(D.GSIZE):
for column in range(D.GSIZE):
field[row][column].dehydrate()
# Increment ticker
TICKER = (TICKER + 1) % 100
# Update the screen
pygame.display.flip()
# Ensure program maintains a stable framerate
clock.tick(35)

View File

@ -1,27 +0,0 @@
def createField():
field = [["soil", "soil", "soil", "soil", "soil", "soil", "rocks", "soil", "soil", "soil"],
["soil", "soil", "soil", "soil", "soil", "soil", "rocks", "soil", "soil", "soil"],
["soil", "soil", "soil", "soil", "soil", "road", "road", "road", "road", "road"],
["rocks", "rocks", "rocks", "rocks", "soil", "road", "soil", "soil", "rocks", "soil"],
["soil", "soil", "soil", "soil", "soil", "road", "rocks", "rocks", "soil", "soil"],
["soil", "soil", "soil", "pond", "rocks", "road", "rocks", "soil", "soil", "rocks"],
["rocks", "pond", "pond", "pond", "pond", "road", "rocks", "soil", "soil", "rocks"],
["road", "road", "road", "road", "road", "road", "rocks", "soil", "soil", "soil"],
["soil", "soil", "soil", "soil", "soil", "soil", "rocks", "soil", "rocks", "rocks"],
["soil", "soil", "soil", "soil", "soil", "rocks", "soil", "rocks", "rocks", "soil"]
]
return field
def createPlants():
field = [["wheat", "wheat", "wheat", "wheat", "wheat", "wheat", 0, "strawberry", "strawberry", "strawberry"],
["wheat", "wheat", "wheat", "wheat", "wheat", "wheat", 0, "strawberry", "strawberry", "strawberry"],
["wheat", "wheat", "wheat", "wheat", 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
["wheat", "wheat", "wheat", "wheat", 0, 0, 0, 0, 0, 0],
["wheat", "wheat", "wheat", 0, 0, 0, 0, "potato", "potato", 0],
[0, 0, 0, 0, 0, 0, 0, "potato", "potato", 0],
[0, 0, 0, 0, 0, 0, 0, "potato", "potato", "potato"],
["strawberry", "strawberry", "strawberry", "strawberry", "strawberry", 0, 0, "potato", 0, 0],
["strawberry", "strawberry", "strawberry", "strawberry", "strawberry", 0, 0, 0, 0, 0]
]
return field

99
node.py
View File

@ -1,10 +1,12 @@
from dimensions import *
import heapq import heapq
from src.dimensions import *
def getTotalCost(x): def getTotalCost(x):
return x.totalCost return x.totalCost
def showPath(node, goal): def showPath(node, goal):
path = node.findPath(goal) path = node.findPath(goal)
for x in path: for x in path:
@ -14,34 +16,36 @@ def showPath(node, goal):
print("***") print("***")
def succesor (node): def succesor(node):
succesors = [] succesors = []
if node.position[0]+node.rotation[0] in range(0,GSIZE) and node.position[1]+node.rotation[1] in range(0,GSIZE): if node.position[0] + node.rotation[0] in range(0, GSIZE) and node.position[1] + node.rotation[1] in range(0,
child = Node(node.field, [node.position[0]+node.rotation[0], node.position[1]+node.rotation[1]], node.rotation) GSIZE):
child = Node(node.field, [node.position[0] + node.rotation[0], node.position[1] + node.rotation[1]],
node.rotation)
child.action = "move" child.action = "move"
succesors.append(child) succesors.append(child)
if node.rotation == [1,0]: if node.rotation == [1, 0]:
child = Node(node.field, node.position, [0,-1]) child = Node(node.field, node.position, [0, -1])
child.action = "left" child.action = "left"
succesors.append(child) succesors.append(child)
child = Node(node.field, node.position, [0,1]) child = Node(node.field, node.position, [0, 1])
child.action = "right" child.action = "right"
succesors.append(child) succesors.append(child)
if node.rotation == [0,1]: if node.rotation == [0, 1]:
child = Node(node.field, node.position, [-1, 0]) child = Node(node.field, node.position, [-1, 0])
succesors.append(child) succesors.append(child)
child.action = "right" child.action = "right"
child = Node(node.field, node.position, [1, 0]) child = Node(node.field, node.position, [1, 0])
child.action = "left" child.action = "left"
succesors.append(child) succesors.append(child)
if node.rotation == [-1,0]: if node.rotation == [-1, 0]:
child = Node(node.field, node.position, [0,-1]) child = Node(node.field, node.position, [0, -1])
succesors.append(child) succesors.append(child)
child.action = "right" child.action = "right"
child = Node(node.field, node.position, [0,1]) child = Node(node.field, node.position, [0, 1])
child.action = "left" child.action = "left"
succesors.append(child) succesors.append(child)
if node.rotation == [0,-1]: if node.rotation == [0, -1]:
child = Node(node.field, node.position, [-1, 0]) child = Node(node.field, node.position, [-1, 0])
child.action = "left" child.action = "left"
succesors.append(child) succesors.append(child)
@ -50,7 +54,8 @@ def succesor (node):
succesors.append(child) succesors.append(child)
return succesors return succesors
class Node():
class Node:
def __init__(self, field, position, rotation): def __init__(self, field, position, rotation):
self.parent = 0 self.parent = 0
self.startCost = 0 self.startCost = 0
@ -102,8 +107,8 @@ class Node():
continue continue
child.parent = currentNode child.parent = currentNode
child.startCost = currentNode.startCost + child.field[child.position[0]][child.position[1]].moveCost child.startCost = currentNode.startCost + child.field[child.position[0]][child.position[1]].moveCost
child.heuristic = abs(goal[0]-child.position[0]) + abs(goal[1]-child.position[1]) child.heuristic = abs(goal[0] - child.position[0]) + abs(goal[1] - child.position[1])
child.totalCost = child.startCost+child.heuristic child.totalCost = child.startCost + child.heuristic
for openNode in openList: for openNode in openList:
if child.position == openNode.position and child.rotation == openNode.rotation and child.action == openNode.action and child.startCost > openNode.startCost: if child.position == openNode.position and child.rotation == openNode.rotation and child.action == openNode.action and child.startCost > openNode.startCost:
@ -131,10 +136,13 @@ class Node():
closedList.append(currentNode) closedList.append(currentNode)
if currentNode.field[currentNode.position[0]][currentNode.position[1]].planted and currentNode.field[currentNode.position[0]][currentNode.position[1]].hydration < 2: if currentNode.field[currentNode.position[0]][currentNode.position[1]].planted and \
currentNode.field[currentNode.position[0]][currentNode.position[1]].field_type == "soil" and \
currentNode.field[currentNode.position[0]][currentNode.position[1]].hydration < 2:
path = [] path = []
for _ in range(currentNode.field[currentNode.position[0]][currentNode.position[1]].hydration, 4): for _ in range(currentNode.field[currentNode.position[0]][currentNode.position[1]].hydration, 4):
path.append("hydrate") path.append("hydrate")
path.append("fertilize")
current = currentNode current = currentNode
while current is not None: while current is not None:
path.append(current.action) path.append(current.action)
@ -154,8 +162,9 @@ class Node():
continue continue
child.parent = currentNode child.parent = currentNode
child.startCost = currentNode.startCost + child.field[child.position[0]][child.position[1]].moveCost child.startCost = currentNode.startCost + child.field[child.position[0]][child.position[1]].moveCost
child.heuristic = abs(startNode.position[0]-child.position[0]) + abs(startNode.position[1]-child.position[1]) child.heuristic = abs(startNode.position[0] - child.position[0]) + abs(
child.totalCost = child.startCost+child.heuristic startNode.position[1] - child.position[1])
child.totalCost = child.startCost + child.heuristic
for openNode in openList: for openNode in openList:
if child.position == openNode.position and child.rotation == openNode.rotation and child.action == openNode.action and child.startCost >= openNode.startCost: if child.position == openNode.position and child.rotation == openNode.rotation and child.action == openNode.action and child.startCost >= openNode.startCost:
@ -167,3 +176,57 @@ class Node():
continue continue
heapq.heappush(openList, child) heapq.heappush(openList, child)
def findPathToPlantSpot(self, goals):
startNode = Node(self.field, self.position, self.rotation)
openList = []
closedList = []
startNode.parent = None
heapq.heappush(openList, startNode)
while len(openList) > 0:
currentNode = heapq.heappop(openList)
closedList.append(currentNode)
if not currentNode.field[currentNode.position[0]][currentNode.position[1]].planted and \
goals[currentNode.position[0]][currentNode.position[1]] != "":
path = []
path.append("plant")
current = currentNode
while current is not None:
path.append(current.action)
current = current.parent
return path[::-1]
children = succesor(currentNode)
perm = 0
for child in children:
for closedChild in closedList:
if child.position == closedChild.position and child.rotation == closedChild.rotation and child.action == closedChild.action:
perm = 1
break
if perm == 1:
perm = 0
continue
child.parent = currentNode
child.startCost = currentNode.startCost + child.field[child.position[0]][child.position[1]].moveCost
child.heuristic = abs(startNode.position[0] - child.position[0]) + abs(
startNode.position[1] - child.position[1])
child.totalCost = child.startCost + child.heuristic
for openNode in openList:
if child.position == openNode.position and child.rotation == openNode.rotation and child.action == openNode.action and child.startCost >= openNode.startCost:
perm = 1
break
if perm == 1:
perm = 0
continue
heapq.heappush(openList, child)

View File

@ -1,29 +1,32 @@
import pygame import os
from colors import * import random
from dimensions import *
from sprites import * from AI.decision_tree import *
from src.dimensions import *
from src.sprites import *
from src.colors import *
path = os.path.dirname(__file__) + "\\src\\test\\"
class Plant(pygame.sprite.Sprite): class Plant(pygame.sprite.Sprite):
def __init__(self, field, species): def __init__(self, field, species):
super(Plant, self).__init__()
self.species = species self.species = species
if self.species == "wheat": if self.species == "tomato":
self.growth_speed = 1.5
self.humidity_needed = 2
self.img0 = wheat_img_0 self.img0 = wheat_img_0
self.img1 = wheat_img_1 self.img1 = wheat_img_1
self.img2 = wheat_img_2 self.img2 = wheat_img_2
self.img3 = wheat_img_3 self.img3 = wheat_img_3
elif self.species == "potato": elif self.species == "potato":
self.growth_speed = 1
self.humidity_needed = 1
self.img0 = potato_img_0 self.img0 = potato_img_0
self.img1 = potato_img_1 self.img1 = potato_img_1
self.img2 = potato_img_2 self.img2 = potato_img_2
self.img3 = potato_img_3 self.img3 = potato_img_3
elif self.species == "strawberry": elif self.species == "strawberry":
self.growth_speed = 0.8 self.img0 = strawberry_img_0
self.humidity_needed = 1 self.img1 = strawberry_img_1
self.img2 = strawberry_img_2
self.img3 = strawberry_img_3
elif self.species == "pepper":
self.img0 = strawberry_img_0 self.img0 = strawberry_img_0
self.img1 = strawberry_img_1 self.img1 = strawberry_img_1
self.img2 = strawberry_img_2 self.img2 = strawberry_img_2
@ -38,6 +41,8 @@ class Plant(pygame.sprite.Sprite):
field.planted = True field.planted = True
self.tickscount = 0 self.tickscount = 0
self.ticks = 0 self.ticks = 0
self.path = path + self.species + "\\"
self.testimage = self.path + random.choice(os.listdir(self.path))
def dtree(self): def dtree(self):
if self.field.hydration == 4: if self.field.hydration == 4:
@ -52,7 +57,7 @@ class Plant(pygame.sprite.Sprite):
elif self.is_healthy == 0: elif self.is_healthy == 0:
return 0 return 0
elif self.field.hydration == 2: elif self.field.hydration == 2:
if self.species == "sorrel": if self.species == "pepper":
if self.ticks == 1: if self.ticks == 1:
if self.is_healthy == 1: if self.is_healthy == 1:
return 1 return 1
@ -62,7 +67,7 @@ class Plant(pygame.sprite.Sprite):
return 0 return 0
elif self.species == "potato": elif self.species == "potato":
return 0 return 0
elif self.species == "wheat": elif self.species == "tomato":
return 0 return 0
elif self.species == "strawberry": elif self.species == "strawberry":
return 0 return 0
@ -74,9 +79,9 @@ class Plant(pygame.sprite.Sprite):
return -1 return -1
elif self.ticks == 0: elif self.ticks == 0:
return 0 return 0
elif self.species == "wheat": elif self.species == "tomato":
return 0 return 0
elif self.species == "sorrel": elif self.species == "pepper":
if self.is_healthy == 0: if self.is_healthy == 0:
return 0 return 0
elif self.is_healthy == 1: elif self.is_healthy == 1:
@ -98,9 +103,9 @@ class Plant(pygame.sprite.Sprite):
return 0 return 0
elif self.species == "strawberry": elif self.species == "strawberry":
return 1 return 1
elif self.species == "sorrel": elif self.species == "pepper":
return 1 return 1
elif self.species == "wheat": elif self.species == "tomato":
return 1 return 1
elif self.is_healthy == 0: elif self.is_healthy == 0:
return 0 return 0
@ -146,6 +151,7 @@ class Plant(pygame.sprite.Sprite):
self.growth = 4 self.growth = 4
if self.growth < 0: if self.growth < 0:
self.growth = 0 self.growth = 0
self.update() self.update()
def tick(self): def tick(self):
@ -153,3 +159,6 @@ class Plant(pygame.sprite.Sprite):
if self.tickscount >= 25: if self.tickscount >= 25:
self.tickscount = 0 self.tickscount = 0
self.ticks = 1 self.ticks = 1
def remove(self):
self.field.planted = False

260
src/cases.py Normal file
View File

@ -0,0 +1,260 @@
class Case:
def __init__(self, values, attributes, Class):
self.values = values
self.attributes = attributes
self.Class = Class
attributes = ["field.hydration", "field.fertility", "species", "ticks", "is_healthy", "field.tractor_there"]
cases = [Case([4, 0, "potato", 0, 1, 0], attributes, 0),
Case([2, 0, "sorrel", 1, 1, 0], attributes, 1),
Case([4, 1, "wheat", 0, 0, 1], attributes, 0),
Case([1, 1, "potato", 1, 0, 1], attributes, 0),
Case([2, 1, "potato", 0, 0, 1], attributes, 0),
Case([2, 0, "potato", 0, 1, 0], attributes, 0),
Case([1, 1, "strawberry", 1, 0, 1], attributes, -1),
Case([1, 0, "wheat", 1, 1, 1], attributes, 0),
Case([2, 0, "wheat", 1, 0, 1], attributes, 0),
Case([1, 1, "strawberry", 0, 1, 1], attributes, 0),
Case([3, 1, "potato", 1, 1, 0], attributes, 1),
Case([2, 1, "strawberry", 1, 0, 0], attributes, 0),
Case([4, 0, "wheat", 1, 1, 1], attributes, 0),
Case([4, 1, "wheat", 1, 0, 1], attributes, 0),
Case([5, 1, "potato", 1, 1, 1], attributes, 0),
Case([4, 0, "strawberry", 1, 0, 0], attributes, 0),
Case([1, 1, "sorrel", 1, 0, 0], attributes, 0),
Case([0, 0, "sorrel", 0, 0, 1], attributes, 0),
Case([2, 0, "sorrel", 1, 1, 0], attributes, 1),
Case([0, 1, "sorrel", 0, 1, 1], attributes, 0),
Case([0, 1, "strawberry", 1, 1, 0], attributes, -1),
Case([2, 0, "sorrel", 0, 1, 1], attributes, 0),
Case([4, 0, "wheat", 1, 0, 1], attributes, 0),
Case([5, 0, "wheat", 0, 0, 0], attributes, 0),
Case([0, 0, "strawberry", 1, 0, 0], attributes, -1),
Case([4, 0, "sorrel", 1, 0, 0], attributes, 0),
Case([3, 0, "sorrel", 0, 0, 1], attributes, 0),
Case([3, 1, "potato", 1, 0, 1], attributes, 0),
Case([4, 1, "potato", 0, 0, 1], attributes, 0),
Case([1, 1, "wheat", 0, 1, 1], attributes, 0),
Case([3, 0, "wheat", 0, 1, 1], attributes, 0),
Case([2, 0, "wheat", 0, 0, 0], attributes, 0),
Case([5, 1, "potato", 1, 1, 1], attributes, 0),
Case([4, 1, "strawberry", 0, 0, 1], attributes, 0),
Case([1, 0, "potato", 1, 1, 0], attributes, 0),
Case([4, 1, "sorrel", 1, 1, 1], attributes, 0),
Case([0, 1, "sorrel", 0, 0, 0], attributes, 0),
Case([4, 0, "strawberry", 1, 0, 0], attributes, 0),
Case([4, 0, "sorrel", 1, 0, 0], attributes, 0),
Case([5, 0, "strawberry", 0, 1, 1], attributes, 0),
Case([3, 1, "wheat", 1, 1, 1], attributes, 0),
Case([3, 0, "strawberry", 0, 1, 1], attributes, 0),
Case([4, 0, "potato", 0, 0, 1], attributes, 0),
Case([5, 1, "wheat", 1, 1, 1], attributes, 0),
Case([3, 0, "strawberry", 0, 1, 1], attributes, 0),
Case([3, 0, "sorrel", 0, 0, 1], attributes, 0),
Case([0, 0, "potato", 1, 1, 1], attributes, -1),
Case([4, 0, "strawberry", 1, 1, 1], attributes, 0),
Case([2, 1, "strawberry", 1, 0, 1], attributes, 0),
Case([2, 1, "wheat", 0, 0, 1], attributes, 0),
Case([2, 1, "sorrel", 1, 1, 0], attributes, 1),
Case([1, 0, "potato", 1, 0, 1], attributes, 0),
Case([4, 0, "strawberry", 1, 0, 0], attributes, 0),
Case([4, 1, "potato", 1, 1, 1], attributes, 0),
Case([0, 0, "strawberry", 1, 0, 1], attributes, -1),
Case([0, 1, "wheat", 0, 0, 0], attributes, 0),
Case([1, 1, "wheat", 0, 1, 0], attributes, 0),
Case([0, 0, "sorrel", 1, 1, 0], attributes, -1),
Case([2, 0, "sorrel", 0, 0, 1], attributes, 0),
Case([5, 1, "wheat", 1, 1, 1], attributes, 0),
Case([2, 0, "strawberry", 0, 1, 0], attributes, 0),
Case([2, 1, "wheat", 0, 0, 1], attributes, 0),
Case([3, 0, "potato", 1, 0, 1], attributes, 0),
Case([5, 0, "wheat", 1, 1, 1], attributes, 0),
Case([0, 1, "strawberry", 1, 0, 0], attributes, -1),
Case([0, 0, "wheat", 0, 0, 0], attributes, 0),
Case([5, 0, "potato", 1, 1, 0], attributes, 1),
Case([2, 0, "strawberry", 0, 1, 1], attributes, 0),
Case([3, 1, "sorrel", 0, 1, 0], attributes, 0),
Case([2, 1, "potato", 1, 1, 1], attributes, 0),
Case([5, 0, "strawberry", 1, 1, 0], attributes, 1),
Case([5, 0, "wheat", 0, 0, 0], attributes, 0),
Case([5, 0, "wheat", 1, 1, 0], attributes, 1),
Case([2, 0, "potato", 1, 0, 0], attributes, 0),
Case([3, 1, "wheat", 0, 1, 0], attributes, 0),
Case([3, 0, "potato", 1, 1, 1], attributes, 0),
Case([0, 1, "sorrel", 1, 1, 1], attributes, -1),
Case([0, 0, "strawberry", 1, 1, 1], attributes, -1),
Case([2, 1, "strawberry", 0, 1, 0], attributes, 0),
Case([0, 1, "sorrel", 1, 1, 0], attributes, -1),
Case([3, 0, "wheat", 0, 1, 1], attributes, 0),
Case([4, 0, "strawberry", 1, 0, 1], attributes, 0),
Case([3, 1, "potato", 0, 0, 1], attributes, 0),
Case([1, 1, "sorrel", 0, 0, 0], attributes, 0),
Case([5, 1, "wheat", 1, 0, 1], attributes, 0),
Case([5, 0, "potato", 1, 0, 1], attributes, 0),
Case([3, 0, "potato", 0, 0, 1], attributes, 0),
Case([1, 0, "wheat", 0, 1, 0], attributes, 0),
Case([5, 0, "sorrel", 0, 1, 1], attributes, 0),
Case([4, 0, "potato", 0, 1, 0], attributes, 0),
Case([0, 0, "strawberry", 0, 0, 0], attributes, 0),
Case([5, 0, "sorrel", 1, 1, 0], attributes, 1),
Case([4, 1, "sorrel", 0, 0, 0], attributes, 0),
Case([1, 1, "strawberry", 1, 1, 0], attributes, -1),
Case([5, 0, "strawberry", 1, 0, 0], attributes, 0),
Case([5, 1, "wheat", 0, 0, 0], attributes, 0),
Case([0, 1, "sorrel", 1, 1, 0], attributes, -1),
Case([3, 1, "potato", 1, 0, 1], attributes, 0),
Case([1, 0, "sorrel", 0, 0, 0], attributes, 0),
Case([3, 0, "wheat", 0, 1, 1], attributes, 0),
Case([0, 0, "wheat", 0, 0, 1], attributes, 0),
Case([1, 0, "potato", 0, 0, 0], attributes, 0),
Case([1, 1, "sorrel", 0, 1, 0], attributes, 0),
Case([0, 1, "strawberry", 0, 1, 0], attributes, 0),
Case([5, 1, "potato", 1, 0, 1], attributes, 0),
Case([2, 0, "strawberry", 1, 1, 1], attributes, 0),
Case([4, 1, "wheat", 1, 0, 0], attributes, 0),
Case([0, 1, "sorrel", 1, 1, 0], attributes, -1),
Case([1, 0, "strawberry", 1, 1, 1], attributes, -1),
Case([4, 0, "wheat", 0, 0, 0], attributes, 0),
Case([4, 0, "strawberry", 0, 1, 0], attributes, 0),
Case([0, 0, "sorrel", 1, 0, 1], attributes, -1),
Case([1, 0, "strawberry", 0, 1, 0], attributes, 0),
Case([0, 0, "strawberry", 1, 1, 1], attributes, -1),
Case([2, 1, "potato", 0, 0, 0], attributes, 0),
Case([3, 1, "strawberry", 1, 1, 0], attributes, 1),
Case([1, 0, "sorrel", 1, 1, 1], attributes, 0),
Case([5, 1, "strawberry", 1, 1, 0], attributes, 1),
Case([2, 0, "wheat", 1, 1, 1], attributes, 0),
Case([5, 1, "strawberry", 0, 1, 0], attributes, 0),
Case([1, 1, "wheat", 0, 1, 1], attributes, 0),
Case([1, 1, "potato", 0, 1, 0], attributes, 0),
Case([4, 0, "potato", 1, 1, 0], attributes, 1),
Case([2, 1, "strawberry", 0, 0, 1], attributes, 0),
Case([0, 1, "potato", 0, 0, 1], attributes, 0),
Case([3, 0, "sorrel", 1, 0, 1], attributes, 0),
Case([4, 0, "wheat", 1, 0, 1], attributes, 0),
Case([5, 1, "sorrel", 1, 0, 0], attributes, 0),
Case([1, 0, "sorrel", 1, 0, 0], attributes, 0),
Case([5, 0, "sorrel", 1, 0, 1], attributes, 0),
Case([2, 1, "potato", 1, 0, 1], attributes, 0),
Case([1, 0, "potato", 1, 1, 1], attributes, 0),
Case([4, 0, "wheat", 1, 1, 0], attributes, 1),
Case([0, 0, "sorrel", 0, 1, 0], attributes, 0),
Case([2, 0, "wheat", 0, 0, 1], attributes, 0),
Case([0, 1, "potato", 1, 0, 1], attributes, -1),
Case([0, 1, "sorrel", 1, 0, 1], attributes, -1),
Case([1, 1, "potato", 0, 0, 1], attributes, 0),
Case([3, 0, "sorrel", 1, 1, 1], attributes, 0),
Case([0, 0, "potato", 1, 0, 1], attributes, -1),
Case([4, 0, "potato", 0, 0, 1], attributes, 0),
Case([0, 0, "strawberry", 1, 0, 0], attributes, -1),
Case([0, 0, "strawberry", 1, 1, 1], attributes, -1),
Case([5, 0, "potato", 0, 1, 1], attributes, 0),
Case([2, 0, "potato", 0, 0, 0], attributes, 0),
Case([0, 1, "potato", 1, 0, 0], attributes, -1),
Case([1, 0, "potato", 0, 0, 1], attributes, 0),
Case([4, 0, "sorrel", 1, 0, 1], attributes, 0),
Case([1, 0, "potato", 1, 0, 0], attributes, 0),
Case([5, 1, "potato", 1, 0, 1], attributes, 0),
Case([2, 1, "wheat", 1, 0, 0], attributes, 0),
Case([0, 1, "potato", 1, 1, 1], attributes, -1),
Case([5, 1, "strawberry", 0, 1, 0], attributes, 0),
Case([3, 0, "strawberry", 0, 1, 1], attributes, 0),
Case([3, 0, "strawberry", 1, 0, 0], attributes, 0),
Case([0, 1, "strawberry", 0, 0, 1], attributes, 0),
Case([0, 1, "wheat", 0, 0, 0], attributes, 0),
Case([0, 1, "strawberry", 1, 0, 0], attributes, -1),
Case([1, 0, "potato", 0, 0, 1], attributes, 0),
Case([1, 1, "wheat", 0, 0, 0], attributes, 0),
Case([0, 1, "strawberry", 1, 0, 1], attributes, -1),
Case([1, 1, "potato", 0, 0, 1], attributes, 0),
Case([0, 0, "wheat", 0, 0, 1], attributes, 0),
Case([4, 1, "sorrel", 1, 1, 1], attributes, 0),
Case([5, 1, "wheat", 0, 0, 1], attributes, 0),
Case([5, 1, "strawberry", 0, 0, 0], attributes, 0),
Case([4, 1, "wheat", 0, 0, 1], attributes, 0),
Case([1, 1, "sorrel", 0, 0, 0], attributes, 0),
Case([1, 1, "potato", 1, 1, 0], attributes, 0),
Case([0, 1, "sorrel", 1, 0, 0], attributes, -1),
Case([5, 0, "sorrel", 1, 0, 0], attributes, 0),
Case([0, 0, "strawberry", 1, 1, 1], attributes, -1),
Case([0, 0, "potato", 0, 0, 0], attributes, 0),
Case([0, 0, "strawberry", 1, 1, 1], attributes, -1),
Case([4, 0, "strawberry", 0, 0, 1], attributes, 0),
Case([2, 0, "potato", 1, 0, 0], attributes, 0),
Case([4, 0, "strawberry", 0, 0, 0], attributes, 0),
Case([0, 1, "strawberry", 0, 1, 1], attributes, 0),
Case([1, 1, "wheat", 1, 1, 0], attributes, 0),
Case([3, 0, "potato", 0, 1, 0], attributes, 0),
Case([1, 1, "wheat", 0, 1, 0], attributes, 0),
Case([1, 1, "sorrel", 0, 0, 1], attributes, 0),
Case([3, 1, "wheat", 0, 0, 0], attributes, 0),
Case([3, 1, "wheat", 0, 0, 0], attributes, 0),
Case([1, 0, "wheat", 0, 1, 1], attributes, 0),
Case([5, 1, "potato", 1, 0, 1], attributes, 0),
Case([5, 0, "wheat", 0, 0, 1], attributes, 0),
Case([2, 1, "sorrel", 0, 1, 0], attributes, 0),
Case([5, 0, "sorrel", 1, 0, 0], attributes, 0),
Case([1, 0, "potato", 1, 1, 1], attributes, 0),
Case([5, 1, "wheat", 1, 1, 0], attributes, 1),
Case([3, 1, "sorrel", 0, 0, 0], attributes, 0),
Case([5, 0, "wheat", 0, 0, 1], attributes, 0),
Case([2, 0, "strawberry", 0, 1, 1], attributes, 0),
Case([1, 0, "potato", 1, 1, 1], attributes, 0),
Case([0, 1, "sorrel", 0, 0, 0], attributes, 0),
Case([5, 1, "potato", 1, 1, 0], attributes, 1),
Case([2, 0, "wheat", 0, 1, 0], attributes, 0),
Case([3, 0, "strawberry", 0, 1, 1], attributes, 0),
Case([3, 0, "potato", 1, 1, 0], attributes, 0),
Case([1, 1, "potato", 0, 0, 0], attributes, 0),
Case([2, 1, "potato", 0, 1, 0], attributes, 0),
Case([2, 1, "wheat", 0, 1, 0], attributes, 0),
Case([2, 0, "sorrel", 1, 1, 0], attributes, 1),
Case([3, 0, "strawberry", 0, 1, 1], attributes, 0),
Case([2, 0, "wheat", 1, 1, 1], attributes, 0),
Case([5, 0, "wheat", 1, 1, 0], attributes, 1),
Case([1, 1, "sorrel", 0, 0, 0], attributes, 0),
Case([0, 1, "potato", 0, 0, 0], attributes, 0),
Case([5, 1, "strawberry", 1, 1, 1], attributes, 0),
Case([4, 1, "wheat", 1, 0, 0], attributes, 0),
Case([5, 1, "sorrel", 0, 0, 1], attributes, 0),
Case([1, 1, "wheat", 1, 0, 0], attributes, 0),
Case([5, 0, "strawberry", 1, 0, 1], attributes, 0),
Case([5, 0, "wheat", 1, 0, 1], attributes, 0),
Case([2, 0, "potato", 1, 0, 1], attributes, 0),
Case([3, 1, "wheat", 1, 0, 0], attributes, 0),
Case([0, 1, "strawberry", 1, 0, 0], attributes, -1),
Case([0, 1, "strawberry", 0, 1, 1], attributes, 0),
Case([3, 0, "wheat", 0, 1, 0], attributes, 0),
Case([4, 1, "potato", 1, 1, 1], attributes, 0),
Case([3, 0, "potato", 0, 0, 1], attributes, 0),
Case([2, 1, "strawberry", 1, 1, 0], attributes, 0),
Case([1, 1, "sorrel", 0, 0, 0], attributes, 0),
Case([4, 1, "wheat", 1, 0, 1], attributes, 0),
Case([2, 0, "potato", 0, 1, 0], attributes, 0),
Case([5, 0, "sorrel", 0, 1, 1], attributes, 0),
Case([0, 1, "wheat", 1, 1, 0], attributes, -1),
Case([5, 1, "wheat", 1, 0, 0], attributes, 0),
Case([2, 0, "potato", 0, 0, 0], attributes, 0),
Case([2, 0, "strawberry", 0, 1, 1], attributes, 0),
Case([4, 1, "potato", 0, 1, 1], attributes, 0),
Case([0, 1, "sorrel", 1, 1, 0], attributes, -1),
Case([1, 1, "strawberry", 1, 0, 1], attributes, -1),
Case([3, 0, "sorrel", 1, 1, 0], attributes, 1),
Case([5, 1, "wheat", 1, 0, 0], attributes, 0),
Case([4, 0, "sorrel", 1, 1, 0], attributes, 1),
Case([2, 1, "sorrel", 1, 0, 0], attributes, 0),
Case([0, 1, "wheat", 0, 1, 0], attributes, 0),
Case([5, 0, "potato", 1, 1, 0], attributes, 1),
Case([3, 1, "strawberry", 0, 1, 0], attributes, 0),
Case([5, 1, "strawberry", 0, 0, 0], attributes, 0),
Case([4, 1, "potato", 1, 1, 1], attributes, 0),
Case([5, 1, "potato", 1, 0, 1], attributes, 0),
Case([5, 1, "potato", 1, 1, 1], attributes, 0),
Case([0, 0, "sorrel", 0, 0, 0], attributes, 0),
Case([1, 1, "sorrel", 1, 1, 0], attributes, 1),
Case([0, 1, "potato", 0, 1, 0], attributes, 0),
Case([4, 1, "strawberry", 1, 1, 1], attributes, 0),
Case([0, 0, "wheat", 0, 1, 1], attributes, 0),
Case([3, 0, "wheat", 1, 1, 0], attributes, 1)]

View File

@ -6,6 +6,15 @@ BROWN1 = (160, 130, 70)
BROWN2 = (140, 110, 55) BROWN2 = (140, 110, 55)
BROWN3 = (110, 85, 40) BROWN3 = (110, 85, 40)
BROWN4 = (80, 60, 20) BROWN4 = (80, 60, 20)
BROWN5 = (80, 60, 20)
REDDISH0 = (230, 150, 90)
REDDISH1 = (210, 130, 70)
REDDISH2 = (190, 110, 55)
REDDISH3 = (160, 85, 40)
REDDISH4 = (130, 60, 20)
REDDISH5 = (130, 60, 20)
DBROWN = (65, 50, 20) DBROWN = (65, 50, 20)
LBROWN = (108, 97, 62) LBROWN = (108, 97, 62)
BLUE = (18, 93, 156) BLUE = (18, 93, 156)

View File

@ -2,12 +2,12 @@
GSIZE = 10 GSIZE = 10
# This sets the WIDTH and HEIGHT of each grid location # This sets the WIDTH and HEIGHT of each grid location
WIDTH = 35 WIDTH = 80
HEIGHT = 35 HEIGHT = 80
# This sets the margin between each cell # This sets the margin between each cell
MARGIN = 5 MARGIN = 5
# Window size # Window size
SCREEN_WIDTH = GSIZE * (WIDTH + MARGIN) + MARGIN SCREEN_WIDTH = GSIZE * (WIDTH + MARGIN) + MARGIN
SCREEN_HEIGHT = GSIZE * (HEIGHT + MARGIN) + MARGIN SCREEN_HEIGHT = GSIZE * (HEIGHT + MARGIN) + MARGIN + 100

28
src/mapschema.py Normal file
View File

@ -0,0 +1,28 @@
def createField():
field = [["soil", "soil", "soil", "soil", "soil", "soil", "rocks", "soil", "soil", "soil"],
["soil", "soil", "soil", "soil", "soil", "soil", "rocks", "soil", "soil", "soil"],
["soil", "soil", "soil", "soil", "soil", "road", "road", "road", "road", "road"],
["rocks", "rocks", "rocks", "rocks", "soil", "road", "soil", "soil", "rocks", "soil"],
["soil", "soil", "soil", "soil", "soil", "road", "rocks", "rocks", "soil", "soil"],
["soil", "soil", "soil", "pond", "rocks", "road", "rocks", "soil", "soil", "rocks"],
["rocks", "pond", "pond", "pond", "pond", "road", "rocks", "soil", "soil", "rocks"],
["road", "road", "road", "road", "road", "road", "rocks", "soil", "soil", "soil"],
["soil", "soil", "soil", "soil", "soil", "soil", "rocks", "soil", "rocks", "rocks"],
["soil", "soil", "soil", "soil", "soil", "rocks", "soil", "rocks", "rocks", "soil"]
]
return field
def createPlants():
field = [["wheat", "wheat", "wheat", "wheat", "wheat", "wheat", 0, "strawberry", "strawberry", "strawberry"],
["wheat", "wheat", "wheat", "wheat", "wheat", "wheat", 0, "strawberry", "strawberry", "strawberry"],
["wheat", "wheat", "wheat", "wheat", 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
["wheat", "wheat", "wheat", "wheat", 0, 0, 0, 0, 0, 0],
["wheat", "wheat", "wheat", 0, 0, 0, 0, "potato", "potato", 0],
[0, 0, 0, 0, 0, 0, 0, "potato", "potato", 0],
[0, 0, 0, 0, 0, 0, 0, "potato", "potato", "potato"],
["strawberry", "strawberry", "strawberry", "strawberry", "strawberry", 0, 0, "potato", 0, 0],
["strawberry", "strawberry", "strawberry", "strawberry", "strawberry", 0, 0, 0, 0, 0]
]
return field

View File

@ -1,4 +1,5 @@
import os import os
import pygame import pygame
# set up asset folders # set up asset folders

View File

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

Before

Width:  |  Height:  |  Size: 3.0 KiB

After

Width:  |  Height:  |  Size: 3.0 KiB

View File

Before

Width:  |  Height:  |  Size: 4.1 KiB

After

Width:  |  Height:  |  Size: 4.1 KiB

View File

Before

Width:  |  Height:  |  Size: 5.6 KiB

After

Width:  |  Height:  |  Size: 5.6 KiB

View File

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

Before

Width:  |  Height:  |  Size: 2.5 KiB

After

Width:  |  Height:  |  Size: 2.5 KiB

View File

Before

Width:  |  Height:  |  Size: 3.2 KiB

After

Width:  |  Height:  |  Size: 3.2 KiB

View File

Before

Width:  |  Height:  |  Size: 3.6 KiB

After

Width:  |  Height:  |  Size: 3.6 KiB

View File

Before

Width:  |  Height:  |  Size: 2.7 KiB

After

Width:  |  Height:  |  Size: 2.7 KiB

View File

Before

Width:  |  Height:  |  Size: 2.6 KiB

After

Width:  |  Height:  |  Size: 2.6 KiB

View File

Before

Width:  |  Height:  |  Size: 2.0 KiB

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

Before

Width:  |  Height:  |  Size: 2.0 KiB

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

View File

Before

Width:  |  Height:  |  Size: 2.9 KiB

After

Width:  |  Height:  |  Size: 2.9 KiB

View File

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

Before

Width:  |  Height:  |  Size: 5.9 KiB

After

Width:  |  Height:  |  Size: 5.9 KiB

View File

Before

Width:  |  Height:  |  Size: 4.7 KiB

After

Width:  |  Height:  |  Size: 4.7 KiB

View File

Before

Width:  |  Height:  |  Size: 4.7 KiB

After

Width:  |  Height:  |  Size: 4.7 KiB

View File

Before

Width:  |  Height:  |  Size: 5.2 KiB

After

Width:  |  Height:  |  Size: 5.2 KiB

View File

Before

Width:  |  Height:  |  Size: 5.2 KiB

After

Width:  |  Height:  |  Size: 5.2 KiB

View File

Before

Width:  |  Height:  |  Size: 5.2 KiB

After

Width:  |  Height:  |  Size: 5.2 KiB

View File

Before

Width:  |  Height:  |  Size: 5.2 KiB

After

Width:  |  Height:  |  Size: 5.2 KiB

View File

Before

Width:  |  Height:  |  Size: 5.0 KiB

After

Width:  |  Height:  |  Size: 5.0 KiB

View File

Before

Width:  |  Height:  |  Size: 5.3 KiB

After

Width:  |  Height:  |  Size: 5.3 KiB

View File

Before

Width:  |  Height:  |  Size: 5.2 KiB

After

Width:  |  Height:  |  Size: 5.2 KiB

View File

Before

Width:  |  Height:  |  Size: 5.1 KiB

After

Width:  |  Height:  |  Size: 5.1 KiB

View File

Before

Width:  |  Height:  |  Size: 5.1 KiB

After

Width:  |  Height:  |  Size: 5.1 KiB

View File

Before

Width:  |  Height:  |  Size: 5.0 KiB

After

Width:  |  Height:  |  Size: 5.0 KiB

View File

Before

Width:  |  Height:  |  Size: 4.9 KiB

After

Width:  |  Height:  |  Size: 4.9 KiB

View File

Before

Width:  |  Height:  |  Size: 5.0 KiB

After

Width:  |  Height:  |  Size: 5.0 KiB

View File

Before

Width:  |  Height:  |  Size: 4.3 KiB

After

Width:  |  Height:  |  Size: 4.3 KiB

View File

Before

Width:  |  Height:  |  Size: 4.3 KiB

After

Width:  |  Height:  |  Size: 4.3 KiB

View File

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

Before

Width:  |  Height:  |  Size: 4.7 KiB

After

Width:  |  Height:  |  Size: 4.7 KiB

View File

Before

Width:  |  Height:  |  Size: 4.3 KiB

After

Width:  |  Height:  |  Size: 4.3 KiB

View File

Before

Width:  |  Height:  |  Size: 4.6 KiB

After

Width:  |  Height:  |  Size: 4.6 KiB

View File

Before

Width:  |  Height:  |  Size: 4.4 KiB

After

Width:  |  Height:  |  Size: 4.4 KiB

View File

Before

Width:  |  Height:  |  Size: 4.5 KiB

After

Width:  |  Height:  |  Size: 4.5 KiB

View File

Before

Width:  |  Height:  |  Size: 4.4 KiB

After

Width:  |  Height:  |  Size: 4.4 KiB

View File

Before

Width:  |  Height:  |  Size: 4.4 KiB

After

Width:  |  Height:  |  Size: 4.4 KiB

View File

@ -1,20 +1,7 @@
import pygame from src.dimensions import *
from src.sprites import *
from plant import *
from pygame.locals import (
K_UP,
K_DOWN,
K_LEFT,
K_RIGHT,
K_ESCAPE,
K_SPACE,
K_c,
KEYDOWN,
QUIT
)
from dimensions import *
from colors import *
from sprites import *
class Tractor(pygame.sprite.Sprite): class Tractor(pygame.sprite.Sprite):
def __init__(self, field, position): def __init__(self, field, position):
@ -30,13 +17,13 @@ class Tractor(pygame.sprite.Sprite):
def move(self): def move(self):
self.field[self.position[0]][self.position[1]].tractor_there = False self.field[self.position[0]][self.position[1]].tractor_there = False
self.rect.move_ip(self.direction[0]*(WIDTH + MARGIN), self.direction[1]*(HEIGHT + MARGIN)) self.rect.move_ip(self.direction[0] * (WIDTH + MARGIN), self.direction[1] * (HEIGHT + MARGIN))
self.position[0] += self.direction[0] self.position[0] += self.direction[0]
self.position[1] += self.direction[1] self.position[1] += self.direction[1]
if self.position[0] >= GSIZE: if self.position[0] >= GSIZE:
self.position[0] = GSIZE-1 self.position[0] = GSIZE - 1
if self.position[1] >= GSIZE: if self.position[1] >= GSIZE:
self.position[1] = GSIZE-1 self.position[1] = GSIZE - 1
if self.position[0] < 0: if self.position[0] < 0:
self.position[0] = 0 self.position[0] = 0
if self.position[1] < 0: if self.position[1] < 0:
@ -44,16 +31,15 @@ class Tractor(pygame.sprite.Sprite):
if self.rect.top <= MARGIN: if self.rect.top <= MARGIN:
self.rect.top = MARGIN self.rect.top = MARGIN
if self.rect.bottom >= SCREEN_HEIGHT-MARGIN: if self.rect.bottom >= SCREEN_HEIGHT - MARGIN:
self.rect.bottom = SCREEN_HEIGHT-MARGIN self.rect.bottom = SCREEN_HEIGHT - MARGIN
if self.rect.left < MARGIN: if self.rect.left < MARGIN:
self.rect.left = MARGIN self.rect.left = MARGIN
if self.rect.right > SCREEN_WIDTH-MARGIN: if self.rect.right > SCREEN_WIDTH - MARGIN:
self.rect.right = SCREEN_WIDTH-MARGIN self.rect.right = SCREEN_WIDTH - MARGIN
self.field[self.position[0]][self.position[1]].tractor_there = True self.field[self.position[0]][self.position[1]].tractor_there = True
def rotate_right(self): def rotate_right(self):
if self.direction == [1, 0]: if self.direction == [1, 0]:
self.direction = [0, 1] self.direction = [0, 1]
@ -86,10 +72,13 @@ class Tractor(pygame.sprite.Sprite):
field[self.position[0]][self.position[1]].hydrate() field[self.position[0]][self.position[1]].hydrate()
def cut(self, field, pressed_keys): def cut(self, field, pressed_keys):
if pressed_keys[K_c]:
field[self.position[0]][self.position[1]].free() field[self.position[0]][self.position[1]].free()
def plant(self, field, plant, pressed_keys): def plant(self, plant_map, plants):
if field.planted == 0: print(plant_map[self.position[0]][self.position[1]])
field.planted = plant plant = Plant(self.field[self.position[0]][self.position[1]], plant_map[self.position[0]][self.position[1]])
plant.field = field plants.append(plant)
def fertilize(self, field, plants, type):
if plants[self.position[0]][self.position[1]].species == type:
field[self.position[0]][self.position[1]].fertility = 1