143 lines
4.8 KiB
Python
143 lines
4.8 KiB
Python
from genetyczny.funkcje import *
|
|
from easygui import *
|
|
from genetyczny.Data import Data
|
|
import matplotlib.pyplot as plt
|
|
import numpy as np
|
|
import matplotlib.animation as animation
|
|
|
|
def start(data, wheel):
|
|
|
|
ileGeneracji = 200
|
|
ileWPopulacji = 16
|
|
fragment = 0.2
|
|
mutacja = 0.05
|
|
unbox = 3
|
|
|
|
|
|
data.kordyWozka = (wheel.ns, wheel.we)
|
|
data.jakLiczycKoszt = unbox
|
|
|
|
randomPopulation = genRandomPopulation(data, ileWPopulacji)
|
|
for i in range(ileGeneracji):
|
|
if i == 0:
|
|
best2 = dwieNajlepsze(randomPopulation, data)
|
|
else:
|
|
x = genPopulacje(best2[0], best2[1], ileWPopulacji, fragment, mutacja)
|
|
best2 = dwieNajlepsze(x, data)
|
|
data.histZmian.append(data.best[1])
|
|
break
|
|
#rysujWykres(data, ileGeneracji, 1000, 2000)
|
|
|
|
|
|
|
|
def ruszWozek(gen,data, moves, wheel):
|
|
regalKordy = gen.kordy
|
|
unbox = data.unbox[gen.ktoryUnbox]
|
|
star = AStar()
|
|
mapForAStar = data.astarMap[:]
|
|
mapForAStar[regalKordy[0]][regalKordy[1]] = 0
|
|
path = star.search([wheel.ns, wheel.we], regalKordy, mapForAStar, 1, 1)
|
|
cns = wheel.ns
|
|
cwe = wheel.we
|
|
value = path[cns][cwe]
|
|
while True:
|
|
if cns > 0 and path[cns - 1][cwe] == (value + 1):
|
|
cns = cns - 1
|
|
moves.append(1)
|
|
value = value + 1
|
|
continue
|
|
if cns < (len(mapForAStar) - 1) and path[cns + 1][cwe] == (value + 1):
|
|
cns = cns + 1
|
|
moves.append(2)
|
|
value = value + 1
|
|
continue
|
|
if cwe > 0 and path[cns][cwe - 1] == (value + 1):
|
|
cwe = cwe - 1
|
|
moves.append(3)
|
|
value = value + 1
|
|
continue
|
|
if cwe < (len(mapForAStar[0]) - 1) and path[cns][cwe + 1] == (value + 1):
|
|
cns = cns + 1
|
|
moves.append(4)
|
|
value = value + 1
|
|
continue
|
|
break
|
|
mapForAStar[regalKordy[0]][regalKordy[1]] = 1
|
|
# wyszukiwanie ścieżki z miejsca podjęcia paczki do regału
|
|
# zmienna path posiada macierz oraz kroki podjęte przez wózek
|
|
path = star.search([regalKordy[0], regalKordy[1]], unbox, mapForAStar, 1, 1)
|
|
#mapForAStar[where[0]][where[1]] = 1
|
|
value = path[cns][cwe]
|
|
while True:
|
|
if cns > 0 and path[cns - 1][cwe] == (value + 1):
|
|
cns = cns - 1
|
|
moves.append(1)
|
|
value = value + 1
|
|
continue
|
|
if cns < (len(mapForAStar) - 1) and path[cns + 1][cwe] == (value + 1):
|
|
cns = cns + 1
|
|
moves.append(2)
|
|
value = value + 1
|
|
continue
|
|
if cwe > 0 and path[cns][cwe - 1] == (value + 1):
|
|
cwe = cwe - 1
|
|
moves.append(3)
|
|
value = value + 1
|
|
continue
|
|
if cwe < (len(mapForAStar[0]) - 1) and path[cns][cwe + 1] == (value + 1):
|
|
cwe = cwe + 1
|
|
moves.append(4)
|
|
value = value + 1
|
|
continue
|
|
break
|
|
|
|
def rysujWykres(data, x, yStart, yEnd):
|
|
|
|
plt.axis([0, x,yStart, yEnd])
|
|
|
|
for i in range(0, len(data.doWykresu) - 1):
|
|
y = data.doWykresu[i]
|
|
x = i
|
|
plt.scatter(x, y)
|
|
plt.pause(0.0001)
|
|
|
|
plt.show()
|
|
|
|
|
|
def okno():
|
|
good = True
|
|
fieldValues = multenterbox("Wprowadź warunki początkowe", "Start algorytmu genetycznego", ["Ile chrom. w generacji", "Wielkosc dziedziczonego fragmentu (x>0 and x<1)", "Wartosc mutacji (x>0 and x<1)", "Unbox: (0or 1 or 2 or 2)", "Ile generacji"])
|
|
if(fieldValues[0].isnumeric() and (fieldValues[0]!="")):
|
|
good = False
|
|
elif(good==True):
|
|
msgbox("Wartość nie jest liczbą", "Błąd")
|
|
good = False
|
|
if(fieldValues[1].isnumeric() and (fieldValues[1]!="")):
|
|
if((int(fieldValues[1])<=0) and (good==True) and (int(fieldValues[1])>= 1)):
|
|
msgbox("Zla wartosc fragmentu")
|
|
good = False
|
|
elif (good == True):
|
|
msgbox("Wartość nie jest liczbą", "Błąd")
|
|
good = False
|
|
if(fieldValues[2].isnumeric() and (fieldValues[2]!="")):
|
|
if((int(fieldValues[1])<=0) and (good==True) and (int(fieldValues[1])>= 1)):
|
|
msgbox("Zla wartosc mutacji")
|
|
good = False
|
|
elif (good == True):
|
|
msgbox("Wartość nie jest liczbą", "Błąd")
|
|
good = False
|
|
if(fieldValues[3].isnumeric() and (fieldValues[3]!="")):
|
|
if(((int(fieldValues[3]) != 0) or (int(fieldValues[3]) != 1) or (int(fieldValues[3]) != 2) or (int(fieldValues[3]) != 3)) and (good==True)):
|
|
msgbox("Zla wartosc unboxa")
|
|
good = False
|
|
elif (good == True):
|
|
msgbox("Wartość nie jest liczbą", "Błąd")
|
|
good = False
|
|
if(fieldValues[4].isnumeric() and (fieldValues[4]!="")):
|
|
pass
|
|
else:
|
|
msgbox("Wartość nie jest liczbą", "Błąd")
|
|
good = False
|
|
if(good == True):
|
|
return [fieldValues[0], fieldValues[1], fieldValues[2],fieldValues[3]]
|