Compare commits
9 Commits
newClasses
...
master
Author | SHA1 | Date | |
---|---|---|---|
abee4a4a6c | |||
ffde8acc7b | |||
6ff8fdbe59 | |||
c6a088e6d7 | |||
7aebe22f82 | |||
acafb05ae0 | |||
72cc8dcac3 | |||
45aaf233f1 | |||
16b056047c |
@ -1,4 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
|
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (pythonProject)" project-jdk-type="Python SDK" />
|
||||||
</project>
|
</project>
|
@ -1,8 +1,7 @@
|
|||||||
{
|
{
|
||||||
"ExpandedNodes": [
|
"ExpandedNodes": [
|
||||||
"",
|
""
|
||||||
"\\decisionTree"
|
|
||||||
],
|
],
|
||||||
"SelectedNode": "\\decisionTree\\treemaker.py",
|
"SelectedNode": "\\C:\\Users\\zmysz\\Desktop\\nowy-inteligentny-traktor",
|
||||||
"PreviewInSolutionExplorer": false
|
"PreviewInSolutionExplorer": false
|
||||||
}
|
}
|
BIN
.vs/nowy-inteligentny-traktor/v17/.wsuo
Normal file
BIN
.vs/slnx.sqlite
82
NN/Generator.py
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
from PIL import Image
|
||||||
|
import random
|
||||||
|
|
||||||
|
plants = [[], [], []]
|
||||||
|
plants[0].append(Image.open("w1.png"))
|
||||||
|
plants[0].append(Image.open("w2.png"))
|
||||||
|
plants[0].append(Image.open("w3.png"))
|
||||||
|
plants[1].append(Image.open("c1.png"))
|
||||||
|
plants[1].append(Image.open("c2.png"))
|
||||||
|
plants[1].append(Image.open("c3.png"))
|
||||||
|
plants[2].append(Image.open("ca1.png"))
|
||||||
|
plants[2].append(Image.open("ca2.png"))
|
||||||
|
plants[2].append(Image.open("ca3.png"))
|
||||||
|
b = [Image.open("b1.png").convert('RGBA'), Image.open("b2.png").convert('RGBA'), Image.open("b3.png").convert('RGBA')]
|
||||||
|
|
||||||
|
|
||||||
|
def generate(water, fertilizer, plantf):
|
||||||
|
if water == 1:
|
||||||
|
new_im = Image.new('RGB', (100, 100),
|
||||||
|
(160 + random.randint(-10, 10), 80 + random.randint(-10, 10), 40 + random.randint(-10, 10)))
|
||||||
|
tmp = plants[plantf][random.randint(0, 2)].resize(
|
||||||
|
(25 + random.randint(-10, 25), 25 + random.randint(-10, 25))).rotate(random.randint(0, 359))
|
||||||
|
new_im.paste(tmp, (random.randint(0, 50), random.randint(0, 50)), tmp)
|
||||||
|
if fertilizer:
|
||||||
|
tmp = b[random.randint(0, 2)].resize(
|
||||||
|
(20 + random.randint(0, 25), 20 + random.randint(0, 25))).rotate(random.randint(0, 359))
|
||||||
|
new_im.paste(tmp, (random.randint(25, 75), random.randint(25, 75)), tmp)
|
||||||
|
else:
|
||||||
|
if fertilizer:
|
||||||
|
new_im = Image.new('RGB', (100, 100),
|
||||||
|
(
|
||||||
|
50 + random.randint(-10, 10), 25 + random.randint(-10, 10),
|
||||||
|
0 + random.randint(-10, 10)))
|
||||||
|
tmp = plants[plantf][random.randint(0, 2)].resize(
|
||||||
|
(25 + random.randint(-10, 25), 25 + random.randint(-10, 25))).rotate(random.randint(0, 359))
|
||||||
|
new_im.paste(tmp, (random.randint(0, 50), random.randint(0, 50)), tmp)
|
||||||
|
tmp = b[random.randint(0, 2)].resize(
|
||||||
|
(20 + random.randint(0, 25), 20 + random.randint(0, 25))).rotate(random.randint(0, 359))
|
||||||
|
new_im.paste(tmp, (random.randint(25, 75), random.randint(25, 75)), tmp)
|
||||||
|
|
||||||
|
else:
|
||||||
|
if random.randint(0, 1) == 1:
|
||||||
|
new_im = Image.new('RGB', (100, 100),
|
||||||
|
(50 + random.randint(-10, 10), 25 + random.randint(-10, 10),
|
||||||
|
0 + random.randint(-10, 10)))
|
||||||
|
else:
|
||||||
|
new_im = Image.new('RGB', (100, 100),
|
||||||
|
(160 + random.randint(-10, 10), 80 + random.randint(-10, 10),
|
||||||
|
40 + random.randint(-10, 10)))
|
||||||
|
if random.randint(0, 1) == 1: # big
|
||||||
|
tmp = plants[plantf][random.randint(0, 2)].resize(
|
||||||
|
(75 + random.randint(-10, 25), 75 + random.randint(-10, 25))).rotate(random.randint(0, 359))
|
||||||
|
new_im.paste(tmp, (random.randint(0, 15), random.randint(0, 15)), tmp)
|
||||||
|
else:
|
||||||
|
tmp = plants[plantf][random.randint(0, 2)].resize(
|
||||||
|
(random.randint(10, 80), random.randint(10, 80))).rotate(random.randint(0, 359))
|
||||||
|
datas = tmp.getdata()
|
||||||
|
|
||||||
|
new_image_data = []
|
||||||
|
for item in datas:
|
||||||
|
# change all white (also shades of whites) pixels to yellow
|
||||||
|
if item[0] in list(range(190, 256)):
|
||||||
|
new_image_data.append(
|
||||||
|
(random.randint(0, 10), 255 + random.randint(-150, 0), random.randint(0, 10)))
|
||||||
|
else:
|
||||||
|
new_image_data.append(item)
|
||||||
|
|
||||||
|
# update image data
|
||||||
|
tmp.putdata(new_image_data)
|
||||||
|
new_im.paste(tmp, (random.randint(0, 30), random.randint(0, 30)), tmp)
|
||||||
|
|
||||||
|
return new_im
|
||||||
|
|
||||||
|
|
||||||
|
for x in range(0, 1000):
|
||||||
|
generate(0, 0, random.randint(0, 2)).save('datasets/00/' + str(x) + '.png')
|
||||||
|
for x in range(0, 1000):
|
||||||
|
generate(1, 0, random.randint(0, 2)).save('datasets/10/' + str(x) + '.png')
|
||||||
|
for x in range(0, 1000):
|
||||||
|
generate(0, 1, random.randint(0, 2)).save('datasets/01/' + str(x) + '.png')
|
||||||
|
for x in range(0, 1000):
|
||||||
|
generate(1, 1, random.randint(0, 2)).save('datasets/11/' + str(x) + '.png')
|
BIN
NN/ca1.png
Normal file
After Width: | Height: | Size: 448 KiB |
BIN
NN/ca2.png
Normal file
After Width: | Height: | Size: 397 KiB |
BIN
NN/ca3.png
Normal file
After Width: | Height: | Size: 821 KiB |
48
NN/trainer.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
import pathlib
|
||||||
|
import random
|
||||||
|
|
||||||
|
import torch
|
||||||
|
from torch import nn
|
||||||
|
from torch.utils.data import DataLoader
|
||||||
|
from torchvision import datasets, transforms
|
||||||
|
from torchvision.transforms import Lambda
|
||||||
|
|
||||||
|
|
||||||
|
device = torch.device('cpu')
|
||||||
|
|
||||||
|
|
||||||
|
def train(model, dataset, n_iter=100, batch_size=2560000):
|
||||||
|
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
|
||||||
|
criterion = nn.NLLLoss()
|
||||||
|
dl = DataLoader(dataset, batch_size=batch_size)
|
||||||
|
model.train()
|
||||||
|
for epoch in range(n_iter):
|
||||||
|
for images, targets in dl:
|
||||||
|
optimizer.zero_grad()
|
||||||
|
out = model(images.to(device))
|
||||||
|
loss = criterion(out, targets.to(device))
|
||||||
|
loss.backward()
|
||||||
|
optimizer.step()
|
||||||
|
if epoch % 10 == 0:
|
||||||
|
print('epoch: %3d loss: %.4f' % (epoch, loss))
|
||||||
|
|
||||||
|
|
||||||
|
image_path_list = list(pathlib.Path('./').glob("*/*/*.png"))
|
||||||
|
|
||||||
|
random_image_path = random.choice(image_path_list)
|
||||||
|
data_transform = transforms.Compose([
|
||||||
|
transforms.Resize(size=(100, 100)),
|
||||||
|
transforms.RandomHorizontalFlip(p=0.5),
|
||||||
|
transforms.ToTensor(),
|
||||||
|
Lambda(lambda x: x.flatten())
|
||||||
|
])
|
||||||
|
|
||||||
|
train_data = datasets.ImageFolder(root="./datasets",
|
||||||
|
transform=data_transform,
|
||||||
|
target_transform=None)
|
||||||
|
|
||||||
|
model1 = nn.Sequential(nn.Linear(30000, 10000), nn.ReLU(), nn.Linear(10000, 10000), nn.ReLU(), nn.Linear(10000, 0000), nn.Linear(10000, 4), nn.LogSoftmax(dim=-1)).to(device)
|
||||||
|
model1.load_state_dict(torch.load("./trained"))
|
||||||
|
train(model1, train_data)
|
||||||
|
|
||||||
|
torch.save(model1.state_dict(), "./trained")
|
10
astar.py
@ -1,7 +1,6 @@
|
|||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
import cart
|
import cart
|
||||||
import copy
|
import copy
|
||||||
from classes import Field
|
|
||||||
|
|
||||||
|
|
||||||
class Istate:
|
class Istate:
|
||||||
@ -92,7 +91,7 @@ def fieldCost(T,node):
|
|||||||
def cost(T, node):
|
def cost(T, node):
|
||||||
cost = 0
|
cost = 0
|
||||||
|
|
||||||
while (node.get_parent() != None):
|
while node.get_parent() is not None:
|
||||||
cost = cost + fieldCost(T, node)
|
cost = cost + fieldCost(T, node)
|
||||||
node = node.get_parent()
|
node = node.get_parent()
|
||||||
|
|
||||||
@ -154,7 +153,7 @@ def heuristic(goaltest, node):
|
|||||||
|
|
||||||
def print_moves(elem):
|
def print_moves(elem):
|
||||||
moves_list = []
|
moves_list = []
|
||||||
while (elem.get_parent() != None):
|
while elem.get_parent() is not None:
|
||||||
moves_list.append(elem.get_action())
|
moves_list.append(elem.get_action())
|
||||||
elem = elem.get_parent()
|
elem = elem.get_parent()
|
||||||
moves_list.reverse()
|
moves_list.reverse()
|
||||||
@ -184,7 +183,6 @@ def succ(elem):
|
|||||||
temp_move_east = elem.get_x() + 1
|
temp_move_east = elem.get_x() + 1
|
||||||
temp_move_north = elem.get_y() + 1
|
temp_move_north = elem.get_y() + 1
|
||||||
|
|
||||||
|
|
||||||
if cart.Cart.is_move_allowed_succ(elem) == "x + 1":
|
if cart.Cart.is_move_allowed_succ(elem) == "x + 1":
|
||||||
actions_list.append(("move", (elem.get_direction(), temp_move_east, elem.get_y())))
|
actions_list.append(("move", (elem.get_direction(), temp_move_east, elem.get_y())))
|
||||||
elif cart.Cart.is_move_allowed_succ(elem) == "y + 1":
|
elif cart.Cart.is_move_allowed_succ(elem) == "y + 1":
|
||||||
@ -194,8 +192,4 @@ def succ(elem):
|
|||||||
elif cart.Cart.is_move_allowed_succ(elem) == "x - 1":
|
elif cart.Cart.is_move_allowed_succ(elem) == "x - 1":
|
||||||
actions_list.append(("move", (elem.get_direction(), temp_move_west, elem.get_y())))
|
actions_list.append(("move", (elem.get_direction(), temp_move_west, elem.get_y())))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return actions_list
|
return actions_list
|
14
bfs.py
@ -1,4 +1,3 @@
|
|||||||
import sys
|
|
||||||
import cart
|
import cart
|
||||||
import copy
|
import copy
|
||||||
|
|
||||||
@ -74,16 +73,12 @@ def goal_test(goaltest,elem):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
# def graphsearch(explored, fringe, goaltest, istate, succ): # przeszukiwanie grafu wszerz
|
def graphsearch(goaltest, istate): # przeszukiwanie grafu wszerz
|
||||||
def graphsearch(explored, fringe, goaltest, istate): # przeszukiwanie grafu wszerz
|
|
||||||
node = Node(None, istate.get_direction(), None, istate.get_x(), istate.get_y())
|
node = Node(None, istate.get_direction(), None, istate.get_x(), istate.get_y())
|
||||||
|
|
||||||
fringe = []
|
fringe = []
|
||||||
#elem = []
|
|
||||||
explored = []
|
explored = []
|
||||||
#action = []
|
|
||||||
fringe.append(node) # wierzchołki do odwiedzenia
|
fringe.append(node) # wierzchołki do odwiedzenia
|
||||||
# fringe = [node]
|
|
||||||
while True:
|
while True:
|
||||||
if not fringe:
|
if not fringe:
|
||||||
return False
|
return False
|
||||||
@ -109,7 +104,7 @@ def graphsearch(explored, fringe, goaltest, istate): # przeszukiwanie grafu wsz
|
|||||||
|
|
||||||
def print_moves(elem):
|
def print_moves(elem):
|
||||||
moves_list = []
|
moves_list = []
|
||||||
while (elem.get_parent() != None):
|
while elem.get_parent() is not None:
|
||||||
moves_list.append(elem.get_action())
|
moves_list.append(elem.get_action())
|
||||||
elem = elem.get_parent()
|
elem = elem.get_parent()
|
||||||
moves_list.reverse()
|
moves_list.reverse()
|
||||||
@ -139,7 +134,6 @@ def succ(elem):
|
|||||||
temp_move_east = elem.get_x() + 1
|
temp_move_east = elem.get_x() + 1
|
||||||
temp_move_north = elem.get_y() + 1
|
temp_move_north = elem.get_y() + 1
|
||||||
|
|
||||||
|
|
||||||
if cart.Cart.is_move_allowed_succ(elem) == "x + 1":
|
if cart.Cart.is_move_allowed_succ(elem) == "x + 1":
|
||||||
actions_list.append(("move", (elem.get_direction(), temp_move_east, elem.get_y())))
|
actions_list.append(("move", (elem.get_direction(), temp_move_east, elem.get_y())))
|
||||||
elif cart.Cart.is_move_allowed_succ(elem) == "y + 1":
|
elif cart.Cart.is_move_allowed_succ(elem) == "y + 1":
|
||||||
@ -149,8 +143,4 @@ def succ(elem):
|
|||||||
elif cart.Cart.is_move_allowed_succ(elem) == "x - 1":
|
elif cart.Cart.is_move_allowed_succ(elem) == "x - 1":
|
||||||
actions_list.append(("move", (elem.get_direction(), temp_move_west, elem.get_y())))
|
actions_list.append(("move", (elem.get_direction(), temp_move_west, elem.get_y())))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return actions_list
|
return actions_list
|
10
board.py
@ -1,6 +1,5 @@
|
|||||||
import pygame
|
import pygame
|
||||||
from screen import SCREEN
|
from screen import SCREEN
|
||||||
global BLACK
|
|
||||||
|
|
||||||
# global SCREEN
|
# global SCREEN
|
||||||
global BLACK
|
global BLACK
|
||||||
@ -8,9 +7,9 @@ global gridObjects
|
|||||||
global imgTree
|
global imgTree
|
||||||
global imgTree
|
global imgTree
|
||||||
imgTree = pygame.image.load('img/tree.png')
|
imgTree = pygame.image.load('img/tree.png')
|
||||||
|
|
||||||
gridObjects = {} # Store grid-box objects from Grid Class
|
gridObjects = {} # Store grid-box objects from Grid Class
|
||||||
|
|
||||||
|
|
||||||
class Grid(object):
|
class Grid(object):
|
||||||
# ta klasa rysuje kratę na ekranie
|
# ta klasa rysuje kratę na ekranie
|
||||||
def __init__(self, x, y, sx, sy):
|
def __init__(self, x, y, sx, sy):
|
||||||
@ -27,6 +26,7 @@ class Grid(object):
|
|||||||
BLACK = (0, 0, 0)
|
BLACK = (0, 0, 0)
|
||||||
pygame.draw.rect(SCREEN, BLACK, (self.x, self.y, self.sx, self.sy), self.width)
|
pygame.draw.rect(SCREEN, BLACK, (self.x, self.y, self.sx, self.sy), self.width)
|
||||||
|
|
||||||
|
|
||||||
class Box(object):
|
class Box(object):
|
||||||
# global SCREEN
|
# global SCREEN
|
||||||
|
|
||||||
@ -43,6 +43,7 @@ class Box(object):
|
|||||||
# global BLACK
|
# global BLACK
|
||||||
pygame.draw.rect(SCREEN, self.color, pygame.Rect(self.x, self.y, self.sx, self.sy))
|
pygame.draw.rect(SCREEN, self.color, pygame.Rect(self.x, self.y, self.sx, self.sy))
|
||||||
|
|
||||||
|
|
||||||
class Obstacle(object):
|
class Obstacle(object):
|
||||||
def __init__(self, mouseObj):
|
def __init__(self, mouseObj):
|
||||||
self.mseX = mouseObj[0]
|
self.mseX = mouseObj[0]
|
||||||
@ -54,8 +55,8 @@ class Obstacle(object):
|
|||||||
self.y = g.y
|
self.y = g.y
|
||||||
self.sx = g.sx
|
self.sx = g.sx
|
||||||
self.sy = g.sy
|
self.sy = g.sy
|
||||||
if self.mseX > self.x and self.mseX < self.x + self.sx:
|
if self.x < self.mseX < self.x + self.sx:
|
||||||
if self.mseY > self.y and self.mseY < self.y + self.sy:
|
if self.y < self.mseY < self.y + self.sy:
|
||||||
self.posX = self.x
|
self.posX = self.x
|
||||||
self.posY = self.y
|
self.posY = self.y
|
||||||
self.gridBox = grid
|
self.gridBox = grid
|
||||||
@ -66,6 +67,7 @@ class Obstacle(object):
|
|||||||
SCREEN.blit(imgTree, (self.posX, self.posY))
|
SCREEN.blit(imgTree, (self.posX, self.posY))
|
||||||
# pygame.display.update()
|
# pygame.display.update()
|
||||||
|
|
||||||
|
|
||||||
def getGridBoxes(grid_box):
|
def getGridBoxes(grid_box):
|
||||||
global gridObjects
|
global gridObjects
|
||||||
return gridObjects[grid_box]
|
return gridObjects[grid_box]
|
5
cart.py
@ -25,9 +25,7 @@ class Cart:
|
|||||||
def set_y(self, y):
|
def set_y(self, y):
|
||||||
self.y = y
|
self.y = y
|
||||||
|
|
||||||
|
def is_move_allowed(self, cart_rect): # sprawdza czy dany ruch, który chce wykonać wózek jest możliwy, zwraca prawdę lub fałsz
|
||||||
def is_move_allowed(self,
|
|
||||||
cart_rect): # sprawdza czy dany ruch, który chce wykonać wózek jest możliwy, zwraca prawdę lub fałsz
|
|
||||||
if self.direction == definitions.CART_DIRECTION_EAST and cart_rect.x + definitions.BLOCK_SIZE < definitions.WIDTH_MAP:
|
if self.direction == definitions.CART_DIRECTION_EAST and cart_rect.x + definitions.BLOCK_SIZE < definitions.WIDTH_MAP:
|
||||||
return True
|
return True
|
||||||
elif self.direction == definitions.CART_DIRECTION_SOUTH and cart_rect.y - definitions.BLOCK_SIZE > 0:
|
elif self.direction == definitions.CART_DIRECTION_SOUTH and cart_rect.y - definitions.BLOCK_SIZE > 0:
|
||||||
@ -74,4 +72,3 @@ class Cart:
|
|||||||
self.direction = 1
|
self.direction = 1
|
||||||
else:
|
else:
|
||||||
self.direction = self.direction + 1
|
self.direction = self.direction + 1
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
||||||
# Generowanie unikalnej losowej linii tekstu
|
# Generowanie unikalnej losowej linii tekstu
|
||||||
def generate_unique_line(existing_lines):
|
def generate_unique_line(existing_lines):
|
||||||
while True:
|
while True:
|
||||||
@ -9,6 +10,7 @@ def generate_unique_line(existing_lines):
|
|||||||
if line not in existing_lines:
|
if line not in existing_lines:
|
||||||
return line
|
return line
|
||||||
|
|
||||||
|
|
||||||
# Generowanie 200 unikalnych linii tekstu
|
# Generowanie 200 unikalnych linii tekstu
|
||||||
lines = []
|
lines = []
|
||||||
while len(lines) < 200:
|
while len(lines) < 200:
|
||||||
|
@ -53,7 +53,7 @@ with open("database.txt", 'r') as f:
|
|||||||
view.append(x)
|
view.append(x)
|
||||||
X1.append(test_list)
|
X1.append(test_list)
|
||||||
|
|
||||||
f = open("learning_set.txt", "w") # zapisuje atrybuty s?ownie
|
f = open("learning_set.txt", "w") # zapisuje atrybuty s這wnie
|
||||||
for i in view:
|
for i in view:
|
||||||
f.write(str(i)+"\n")
|
f.write(str(i)+"\n")
|
||||||
f.close()
|
f.close()
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
# definicje
|
|
||||||
import os
|
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
90
genetic_algorithm.py
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
import random
|
||||||
|
import math
|
||||||
|
|
||||||
|
|
||||||
|
def create_initial_population(population_size, new_list, player):
|
||||||
|
population = []
|
||||||
|
for _ in range(population_size):
|
||||||
|
chromosome = new_list.copy()
|
||||||
|
chromosome.remove((player.x+1, player.y+1))
|
||||||
|
random.shuffle(chromosome)
|
||||||
|
chromosome.insert(0, (player.x+1, player.y+1))
|
||||||
|
population.append(chromosome)
|
||||||
|
return population
|
||||||
|
|
||||||
|
|
||||||
|
def calculate_distance(node1, node2):
|
||||||
|
x1, y1 = node1
|
||||||
|
x2, y2 = node2
|
||||||
|
distance = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
|
||||||
|
return distance
|
||||||
|
|
||||||
|
|
||||||
|
def calculate_fitness(individual):
|
||||||
|
total_distance = 0
|
||||||
|
num_nodes = len(individual)
|
||||||
|
|
||||||
|
for i in range(num_nodes - 1):
|
||||||
|
node1 = individual[i]
|
||||||
|
node2 = individual[i + 1]
|
||||||
|
distance = calculate_distance(node1, node2)
|
||||||
|
|
||||||
|
total_distance += distance
|
||||||
|
|
||||||
|
if total_distance == 0:
|
||||||
|
fitness = float('inf')
|
||||||
|
return fitness
|
||||||
|
|
||||||
|
fitness = 1 / total_distance
|
||||||
|
return fitness
|
||||||
|
|
||||||
|
|
||||||
|
def crossover(parent1, parent2, player):
|
||||||
|
child = [(player.x+1, player.y+1)] + [None] * (len(parent1) - 1)
|
||||||
|
start_index = random.randint(1, len(parent1) - 1)
|
||||||
|
end_index = random.randint(start_index + 1, len(parent1))
|
||||||
|
child[start_index:end_index] = parent1[start_index:end_index]
|
||||||
|
remaining_nodes = [node for node in parent2 if node not in child]
|
||||||
|
child[1:start_index] = remaining_nodes[:start_index - 1]
|
||||||
|
child[end_index:] = remaining_nodes[start_index - 1:]
|
||||||
|
return child
|
||||||
|
|
||||||
|
|
||||||
|
def mutate(individual, mutation_rate):
|
||||||
|
for i in range(1, len(individual)):
|
||||||
|
if random.random() < mutation_rate:
|
||||||
|
j = random.randint(1, len(individual) - 1)
|
||||||
|
individual[i], individual[j] = individual[j], individual[i]
|
||||||
|
return individual
|
||||||
|
|
||||||
|
|
||||||
|
def genetic_algorithm(new_list, player):
|
||||||
|
max_generations = 200
|
||||||
|
population_size = 200
|
||||||
|
mutation_rate = 0.1
|
||||||
|
population = create_initial_population(population_size, new_list, player)
|
||||||
|
best_individual = None
|
||||||
|
best_fitness = float('-inf')
|
||||||
|
|
||||||
|
for generation in range(max_generations):
|
||||||
|
fitness_values = [calculate_fitness(individual) for individual in population]
|
||||||
|
population = [x for _, x in sorted(zip(fitness_values, population), reverse=True)]
|
||||||
|
fitness_values.sort(reverse=True)
|
||||||
|
best_individuals = population[:10]
|
||||||
|
new_population = best_individuals.copy()
|
||||||
|
|
||||||
|
while len(new_population) < population_size:
|
||||||
|
parent1, parent2 = random.choices(best_individuals, k=2)
|
||||||
|
child = crossover(parent1, parent2, player)
|
||||||
|
child = mutate(child, mutation_rate)
|
||||||
|
new_population.append(child)
|
||||||
|
|
||||||
|
for individual in best_individuals:
|
||||||
|
fitness = calculate_fitness(individual)
|
||||||
|
if fitness > best_fitness:
|
||||||
|
best_fitness = fitness
|
||||||
|
best_individual = individual
|
||||||
|
|
||||||
|
population = new_population[:population_size]
|
||||||
|
|
||||||
|
return best_individual
|
BIN
img/player.png
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 2.9 KiB |
BIN
img/tree.png
Before Width: | Height: | Size: 2.0 KiB After Width: | Height: | Size: 3.9 KiB |
2
main.py
@ -413,7 +413,7 @@ def eventHandler(kbdObj, mouseObj):
|
|||||||
|
|
||||||
# Wczytywanie modelu z pliku
|
# Wczytywanie modelu z pliku
|
||||||
labels = ['Rain', 'Planted', 'Temperature', 'Sun', 'Snow', 'Moisture', 'Rotten', 'Time']
|
labels = ['Rain', 'Planted', 'Temperature', 'Sun', 'Snow', 'Moisture', 'Rotten', 'Time']
|
||||||
loaded_model = joblib.load('decisionTree/decisionTreeFinal.sav')
|
loaded_model = joblib.load('decisionTree/decisionTree.sav')
|
||||||
sample = W[goalNode[0]-1][goalNode[1]-1]
|
sample = W[goalNode[0]-1][goalNode[1]-1]
|
||||||
|
|
||||||
# Klasyfikacja przy użyciu wczytanego modelu
|
# Klasyfikacja przy użyciu wczytanego modelu
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
import pygame
|
import pygame
|
||||||
SCREEN = pygame.display.set_mode([600,665])
|
SCREEN = pygame.display.set_mode([600, 690])
|
||||||
|
BIN
test/00/test.png
Normal file
After Width: | Height: | Size: 3.1 KiB |