Usuń 'Kinga/Tractor.py'
This commit is contained in:
parent
a164cbd6fa
commit
9e0d58e17f
184
Kinga/Tractor.py
184
Kinga/Tractor.py
@ -1,184 +0,0 @@
|
||||
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
|
Loading…
Reference in New Issue
Block a user