Prześlij pliki do 'Kinga'

This commit is contained in:
Kinga Jagodzińska 2020-05-13 09:28:52 +00:00
parent 7eb23ea677
commit f1da1798f9
3 changed files with 519 additions and 0 deletions

162
Kinga/Genetyczny.py Normal file
View File

@ -0,0 +1,162 @@
import random
import pygame
class Gen(object):
def __init__(self, game):
self.game = game
self.pokolenie = 0
self.length = 0
self.max_gen = 99
self.index_gen = []
self.empty()
def empty(self):
for x in range(25):
if self.game.fields[x][0] == "puste":
self.length = self.length + 1
self.index_gen.append(x)
def bbrrr(self):
return self.index_gen
# tworzenie dna
def dna_create(self):
self.dna = []
for x in range(self.max_gen):
temp = []
for y in range(self.length):
temp.append(random.choice(["żyto", "jęczmień", "owies", "marchew", "rzodkiew", "pietruszka"]))
self.dna.append(temp)
def algorytm(self):
l = []
first = False
self.dna_create()
while True:
if first == False:
for x in range(len(self.dna)):
l.append(self.l_score(self.dna[x]))
first = True
elif first == True:
for x in range(int(self.max_gen * 1 / 3)):
l.append(self.l_score(self.dna[x + int(self.max_gen * 2 / 3)]))
if max(l) >= self.length or self.pokolenie > 200:
break
# usuwamy najmniej odpowiadające nam dna
for x in range(int(self.max_gen * 1 / 3)):
del self.dna[l.index(min(l))]
l.remove(min(l))
for x in range(int(self.max_gen * 1 / 3)):
temp = []
for y in range(self.length):
if y % 2 == 0:
temp.append(self.dna[x][y])
if y % 2 == 1:
temp.append(self.dna[int(self.max_gen * 2 / 3) - 1 - x][y])
# mutacja
if random.randint(0, 100) <= 40:
temp[random.randint(0, self.length - 1)] = random.choice(["żyto", "jęczmień", "owies", "marchew", "rzodkiew", "pietruszka"])
temp[random.randint(0, self.length - 1)] = random.choice(["żyto", "jęczmień", "owies", "marchew", "rzodkiew", "pietruszka"])
temp[random.randint(0, self.length - 1)] = random.choice(["żyto", "jęczmień", "owies", "marchew", "rzodkiew", "pietruszka"])
self.dna.append(temp)
self.pokolenie = self.pokolenie + 1
print(self.dna[l.index(max(l))])
return self.dna[l.index(max(l))]
def l_score(self, tab):
suma1 = 0
for x in range(len(tab)):
suma = 1
for y in self.game.neighbours[self.index_gen[x]]:
if y in self.index_gen:
som = tab[self.index_gen.index(y)]
else:
som = self.game.fields[y][0]
if tab[x] == "żyto":
if som == "żyto":
suma = suma * 0.5
elif som == "jęczmień":
suma = suma * 1
elif som == "owies":
suma = suma * 0.7
elif som == "marchew":
suma = suma * 0
elif som == "rzodkiew":
suma = suma * 1
elif som == "pietruszka":
suma = suma * 0.6
elif tab[x] == "jęczmień":
if som == "żyto":
suma = suma * 1
elif som == "jęczmień":
suma = suma * 0.8
elif som == "owies":
suma = suma * 0.2
elif som == "marchew":
suma = suma * 1
elif som == "rzodkiew":
suma = suma * 0.2
elif som == "pietruszka":
suma = suma * 0.9
elif tab[x] == "owies":
if som == "żyto":
suma = suma * 0.7
elif som == "jęczmień":
suma = suma * 0.2
if som == "owies":
suma = suma * 1
if som == "marchew":
suma = suma * 0.3
if som == "rzodkiew":
suma = suma * 0
if som == "pietruszka":
suma = suma * 1
if tab[x] == "marchew":
if som == "żyto":
suma = suma * 0
if som == "jęczmień":
suma = suma * 1
if som == "owies":
suma = suma * 0.3
if som == "marchew":
suma = suma * 0.9
if som == "rzodkiew":
suma = suma * 1
if som == "pietruszka":
suma = suma * 0.8
if tab[x] == "rzodkiew":
if som == "żyto":
suma = suma * 1
if som == "jęczmień":
suma = suma * 0.2
if som == "owies":
suma = suma * 0
if som == "marchew":
suma = suma * 1
if som == "rzodkiew":
suma = suma * 0.9
if som == "pietruszka":
suma = suma * 0.7
if tab[x] == "pietruszka":
if som == "żyto":
suma = suma * 0.6
if som == "jęczmień":
suma = suma * 0.9
if som == "owies":
suma = suma * 1
if som == "marchew":
suma = suma * 0.8
if som == "rzodkiew":
suma = suma * 0.7
if som == "pietruszka":
suma = suma * 0.4
suma1 = suma1 + suma
return suma1

184
Kinga/Tractor.py Normal file
View File

@ -0,0 +1,184 @@
import pygame
from pygame.math import Vector2
import math as m
import sys
from itertools import permutations
from Genetyczny import Gen
class Tractor(object):
def __init__(self, game):
self.game = game
self.gen = Gen(game)
size = self.game.screen.get_size()
self.pos = Vector2(22, 22)
self.oy = True
self.oz = False
# A*
self.g_score = []
self.f_score = []
self.came_from = []
self.game.neighbours()
self.score()
self.best_path()
# ruszanie się
self.road = self.algo(0, 24)
# self.best_path()
def tick(self):
# input
# pressed = pygame.key.get_pressed()
# if pressed[pygame.K_d]:
# self.add_force(Vector2(self.speed,0))
# if pressed[pygame.K_s]:
# self.add_force(Vector2(0,self.speed))
# if pressed[pygame.K_a]:
# self.add_force(Vector2(-self.speed,0))
# if pressed[pygame.K_w]:
# self.add_force(Vector2(0,-self.speed))
if pygame.key.get_pressed()[pygame.K_SPACE]:
# print(self.t)
pole = self.pos.y // 144 * 5 + self.pos.x // 144
if len(self.road) == 0:
sys.exit(0)
if self.road[0] == pole + 1:
self.pos.x = self.pos.x + 144
elif self.road[0] == pole - 1:
self.pos.x = self.pos.x - 144
elif self.road[0] == pole + 5:
self.pos.y = self.pos.y + 144
elif self.road[0] == pole - 5:
self.pos.y = self.pos.y - 144
self.road.pop(0)
#
# if (self.pos.x >= 576) and (self.pos.y >= 576) and (self.oz == False):
# self.oz = True
# elif (self.pos.x < 576) and (self.oy == True) and (self.oz == False):
# self.pos.x = self.pos.x + 144
# elif (self.pos.x >= 576) and (self.oy == True) and (self.oz == False):
# self.pos.y = self.pos.y + 144
# self.oy = False
# elif (self.pos.x > 144) and (self.oy == False) and (self.oz == False):
# self.pos.x = self.pos.x - 144
# elif (self.pos.x <= 144) and (self.oy == False) and (self.oz == False):
# self.pos.y = self.pos.y + 144
# self.oy = True
# elif (self.pos.x <= 144) and (self.pos.y <= 144) and (self.oz == True):
# self.oz = False
# elif (self.pos.x < 576) and (self.oy == False) and (self.oz == True):
# self.pos.x = self.pos.x + 144
# elif (self.pos.x >= 576) and (self.oy == False) and (self.oz == True):
# self.pos.y = self.pos.y - 144
# self.oy = True
# elif (self.pos.x > 144) and (self.oy == True) and (self.oz == True):
# self.pos.x = self.pos.x - 144
# elif (self.pos.x <= 144) and (self.oy == True) and (self.oz == True):
# self.pos.y = self.pos.y - 144
# self.oy = False
def draw(self):
# drawing
rect = pygame.Rect(self.pos.x, self.pos.y, 100, 100)
pygame.draw.rect(self.game.screen, (255, 255, 0), rect)
# Tworzenie list dla g, h, f oraz came_from
def score(self):
for x in range(25):
self.g_score.append(0)
self.f_score.append(0)
self.came_from.append(0)
# Obliczanie h (założenie - odległość pomiędzy sąsiednimi polami wynosi 2, tak jak koszt wjazdu na puste pole)
# s to pole na którym jesteśmy
# f to pole końcowe
def h_score(self, s, f):
if f >= s:
a_h = (f - s) // 5
else:
a_h = (s - f) // 5
if f % 5 >= s % 5:
b_h = f % 5 - s % 5
else:
b_h = s % 5 - f % 5 + 1
return 2 * m.sqrt(a_h ** 2 + b_h ** 2)
# A*
def algo(self, start, koniec):
# definiowanie setów
closed_set = []
open_set = [start]
while open_set:
# Szukanie pola w open_set z najniższym f
temp1 = max(self.f_score) + 1
x = 0
for i in range(len(open_set)):
if self.f_score[open_set[i]] <= temp1:
x = open_set[i]
temp1 = self.f_score[open_set[i]]
if x == koniec:
closed_set.clear()
open_set.clear()
return self.reconstruct_path(self.came_from, koniec)
open_set.remove(x)
closed_set.append(x)
for y in self.game.neighbours[x]:
if y in closed_set:
continue
tentative_g_score = self.g_score[x] + self.game.fields[y][3]
if y not in open_set:
open_set.append(y)
tentative_is_better = True
elif tentative_g_score < self.g_score[y]:
tentative_is_better = True
if tentative_is_better == True:
self.came_from[y] = x
self.g_score[y] = tentative_g_score
self.f_score[y] = self.g_score[y] + self.h_score(y, koniec)
print("failure")
def reconstruct_path(self, came_from, current):
total_path = [current]
while came_from[current] != 0:
current = came_from[current]
total_path.insert(0, current)
return total_path
def best_path(self):
best = 999999
for x in permutations(self.gen.index_gen, self.gen.length):
print("index_gen:", self.gen.index_gen)
print("length:", self.gen.length)
# # punkt początkowy
start = 0
droga = []
tem = False
# dlug = 0
print("x:", x)
for y in x:
print("start:", start)
print("y:", y)
# print("x[y]:", x[y])
print(self.algo(start, y))
temp1 = self.algo(start, y)
droga.extend(temp1)
start = y
print(droga)
#
# # for z in droga:
# # dlug = dlug + self.f_score[z]
# # if dlug < best:
# best_route = droga
# # best = dlug
# return best_route

173
Kinga/ran.py Normal file
View File

@ -0,0 +1,173 @@
import pygame
import sys
import random
from Tractor import Tractor
from Genetyczny import Gen
# dodać growth rate
# Dodać co jeśli pusta
# Pamiętać o zmianie algorytmu po dodaniu growth rate
class Game(object):
def __init__(self):
# Config
self.max_tps = 2.0
self.res = (720, 720)
self.fields = []
self.randomize_field()
# Initialization
pygame.init()
self.screen = pygame.display.set_mode(self.res)
pygame.display.set_caption('Traktorek')
self.clock = pygame.time.Clock()
self.dt = 0.0
self.player = Tractor(self)
self.gen = Gen(self)
self.sadzenie = self.gen.algorytm()
while True:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
elif event.type == pygame.K_ESCAPE:
sys.exit(0)
# Ticking
self.dt += self.clock.tick() / 1000.0
while self.dt > 1 / self.max_tps:
self.tick()
self.dt -= 1 / self.max_tps
# Rendering
self.screen.fill((0, 0, 0))
self.draw()
pygame.display.flip()
def tick(self):
self.player.tick()
def draw(self):
self.screen.fill((0, 0, 0))
self.draw_field()
self.draw_net()
self.player.draw()
pygame.display.update()
def draw_net(self):
color = (255, 255, 255)
for i in range(1, 5):
krat = int(720 / 5 * i)
# linia pozioma
pygame.draw.line(self.screen, color, (0, krat), (720, krat), 1)
# linia pionowa
pygame.draw.line(self.screen, color, (krat, 0), (krat, 720), 1)
def randomize_field(self):
for x in range(25):
temp = []
# nasiona
temp.append(random.choice(["żyto", "jęczmień", "owies", "marchew", "rzodkiew", "pietruszka", "puste"]))
# gleba
temp.append(random.choice([True, False]))
# woda
temp.append(random.choice([True, False]))
# # growth rate
# temp.append(random)
# # cost
if temp[0] == "żyto":
temp.append(10)
elif temp[0] == "jęczmień":
temp.append(12)
elif temp[0] == "owies":
temp.append(8)
elif temp[0] == "marchew":
temp.append(14)
elif temp[0] == "rzodkiew":
temp.append(7)
elif temp[0] == "pietruszka":
temp.append(6)
elif temp[0] == "puste":
temp.append(2)
else:
temp.append(0)
self.fields.append(temp)
def draw_field(self):
for x in range(25):
self.screen.fill(self.color(x), (144 * (x % 5), 144 * (x // 5), 144 * (x % 5 + 1), 144 * (x // 5 + 1)))
def color(self, z):
if self.fields[z][0] == 'owies':
return (255, 200, 55)
# if self.fields[z][1] == True:
# if self.fields[z][2] == True:
# return (255, 200, 55)
# elif self.fields[z][2] == False:
# return (255, 200, 100)
# elif self.fields[z][1] == False:
# if self.fields[z][2] == True:
# return (255, 170, 55)
# elif self.fields[z][2] == False:
# return (255, 170, 100)
elif self.fields[z][0] == 'jęczmień':
return (170, 150, 40)
# if self.fields[z][1] == True:
# if self.fields[z][2] == True:
# return (170, 150, 40)
# elif self.fields[z][2] == False:
# return (170, 150, 85)
# elif self.fields[z][1] == False:
# if self.fields[z][2] == True:
# return (170, 120, 40)
# elif self.fields[z][2] == False:
# return (170, 120, 85)
elif self.fields[z][0] == 'żyto':
return (100, 215, 80)
# if self.fields[z][1] == True:
# if self.fields[z][2] == True:
# return (100, 215, 80)
# elif self.fields[z][2] == False:
# return (100, 215, 125)
# elif self.fields[z][1] == False:
# if self.fields[z][2] == True:
# return (100, 185, 80)
# elif self.fields[z][2] == False:
# return (100, 185, 125)
elif self.fields[z][0] == 'marchew':
return (224, 60, 14)
elif self.fields[z][0] == 'rzodkiew':
return (142, 24, 104)
elif self.fields[z][0] == 'pietruszka':
return (254, 247, 246)
else:
return (0,0,0)
def neighbours(self):
self.neighbours = list(range(25))
self.neighbours[0] = [1, 5]
self.neighbours[4] = [3, 9]
self.neighbours[20] = [15, 21]
self.neighbours[24] = [19, 23]
for x in range(1, 4):
self.neighbours[x] = [x - 1, x + 5, x + 1]
for x in range(5, 16, 5):
self.neighbours[x] = [x - 5, x + 1, x + 5]
for x in range(9, 20, 5):
self.neighbours[x] = [x - 5, x - 1, x + 5]
for x in range(21, 24):
self.neighbours[x] = [x - 1, x - 5, x + 1]
for x in [6, 7, 8, 11, 12, 13, 16, 17, 18]:
self.neighbours[x] = [x - 5, x - 1, x + 1, x + 5]
if __name__ == "__main__":
Game()