2020-05-17 15:23:06 +02:00
|
|
|
from Mapa.unboxOnTheFloor import UnboxOnTheFloor
|
|
|
|
from genetyczny.Gene import Gene
|
|
|
|
from Mapa.box import Box
|
|
|
|
from AStar import AStar
|
|
|
|
import random
|
2020-05-18 12:53:51 +02:00
|
|
|
import numpy
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generateGeny(data):
|
|
|
|
zajeteRegaly = data.zajeteRegaly[:]
|
|
|
|
for r in zajeteRegaly:
|
|
|
|
g = Gene(r)
|
|
|
|
g.unbox1 = policzCost(data.astarMap,r,data.unbox[0])
|
|
|
|
if(len(data.unbox) > 1):
|
|
|
|
g.unbox2 = policzCost(data.astarMap,r,data.unbox[1])
|
|
|
|
data.geny.append(g)
|
2020-05-17 15:23:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
def znajdzUnbox(data,mapa):
|
|
|
|
unboxy = []
|
|
|
|
iterator = 0
|
|
|
|
ostatniWiersz = len(mapa) -1
|
|
|
|
for x in mapa[ostatniWiersz]:
|
|
|
|
if (isinstance(x, UnboxOnTheFloor)):
|
|
|
|
unboxy.append((ostatniWiersz, iterator))
|
|
|
|
iterator += 1
|
|
|
|
|
|
|
|
data.unbox = unboxy
|
2020-05-18 12:53:51 +02:00
|
|
|
|
2020-05-17 15:23:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
def policzCost(mapaBoxy, poczatek, koniec):
|
|
|
|
astar = AStar()
|
|
|
|
koszt = astar.search(poczatek, koniec, mapaBoxy, 1, 0)
|
|
|
|
return koszt
|
|
|
|
|
2020-05-18 12:53:51 +02:00
|
|
|
def genRandomChromosome(data):
|
|
|
|
chromosome = data.geny[:]
|
|
|
|
random.shuffle(chromosome)
|
|
|
|
krotkaKosztJakiUnbox = None
|
|
|
|
unboxLastGen = None
|
2020-05-17 15:23:06 +02:00
|
|
|
|
2020-05-18 12:53:51 +02:00
|
|
|
for gen in chromosome:
|
|
|
|
gen.ktoryUnbox = unboxLastGen
|
|
|
|
krotkaKosztJakiUnbox = wybierzUnbox(gen, data.jakLiczycKoszt)
|
|
|
|
unboxLastGen = krotkaKosztJakiUnbox[1]
|
2020-05-17 15:23:06 +02:00
|
|
|
return chromosome
|
|
|
|
|
2020-05-18 12:53:51 +02:00
|
|
|
def genRandomPopulation(data, ileWPopulacji):
|
2020-05-17 15:23:06 +02:00
|
|
|
populacja = []
|
|
|
|
for i in range(ileWPopulacji):
|
2020-05-18 12:53:51 +02:00
|
|
|
populacja.append(genRandomChromosome(data))
|
2020-05-17 15:23:06 +02:00
|
|
|
return populacja
|
|
|
|
|
2020-05-18 12:53:51 +02:00
|
|
|
|
|
|
|
def fitness(chromosome, data):
|
|
|
|
koszt = 0
|
|
|
|
unboxPoprzedniegoGenu = None
|
|
|
|
|
|
|
|
for item, gen in enumerate(chromosome):
|
|
|
|
if(item == 0):
|
|
|
|
koszt += policzCost(data.astarMap, data.kordyWozka, gen.kordy)
|
|
|
|
krotkaKosztJakiUnbox = wybierzUnbox(gen, data.jakLiczycKoszt)
|
|
|
|
koszt += krotkaKosztJakiUnbox[0]
|
|
|
|
unboxPoprzedniegoGenu = krotkaKosztJakiUnbox[1]
|
|
|
|
|
|
|
|
else:
|
|
|
|
if unboxPoprzedniegoGenu == 0:
|
|
|
|
koszt += gen.unbox1
|
|
|
|
elif unboxPoprzedniegoGenu == 1:
|
|
|
|
koszt += gen.unbox2
|
|
|
|
|
|
|
|
krotkaKosztJakiUnbox = wybierzUnbox(gen, data.jakLiczycKoszt)
|
|
|
|
koszt += krotkaKosztJakiUnbox[0]
|
|
|
|
unboxPoprzedniegoGenu = krotkaKosztJakiUnbox[1]
|
|
|
|
|
|
|
|
|
|
|
|
return koszt
|
|
|
|
|
|
|
|
def wybierzUnbox(gen, jakLiczycKoszt):
|
|
|
|
if(jakLiczycKoszt == 0):
|
|
|
|
x = random.choice([gen.unbox1, gen.unbox2])
|
|
|
|
if(x == gen.unbox1):
|
|
|
|
y = 0
|
|
|
|
else:
|
|
|
|
y= 1
|
|
|
|
return (x,y)
|
|
|
|
elif(jakLiczycKoszt == 1):
|
|
|
|
return (gen.unbox1,0)
|
|
|
|
elif(jakLiczycKoszt == 2):
|
|
|
|
return (gen.unbox2,1)
|
|
|
|
elif(jakLiczycKoszt == 3):
|
|
|
|
x = min(gen.unbox1,gen.unbox2)
|
|
|
|
if(x == gen.unbox1):
|
|
|
|
y = 0
|
|
|
|
else:
|
|
|
|
y = 1
|
|
|
|
return (x,y)
|
|
|
|
|
2020-05-17 15:23:06 +02:00
|
|
|
def randomBox(mapa, regals, ile):
|
|
|
|
|
|
|
|
regals = regals
|
|
|
|
mapa = mapa
|
|
|
|
tupleList = []
|
|
|
|
ileRegalow = len(regals)
|
|
|
|
iteration = 0
|
|
|
|
|
|
|
|
while iteration < ileRegalow and iteration < ile:
|
|
|
|
regal = random.choice(regals)
|
|
|
|
if regal in tupleList:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
tupleList.append(regal)
|
|
|
|
iteration+=1
|
|
|
|
|
|
|
|
for (i,j,x) in tupleList:
|
|
|
|
box = Box()
|
|
|
|
mapa[i][j].put(box)
|
|
|
|
"""
|
|
|
|
for t in tupleList:
|
|
|
|
listaRegalow.append((t[0],t[1]))
|
|
|
|
data.zajeteRegaly = listaRegalow
|
|
|
|
"""
|
|
|
|
return mapa
|
|
|
|
|
|
|
|
def znajdzBox(mapa, regals):
|
|
|
|
zajeteRegaly = []
|
|
|
|
|
|
|
|
for (x,y,z) in regals:
|
|
|
|
shelf = mapa[x][y]
|
|
|
|
tmp = shelf.occupied
|
|
|
|
if(tmp == True):
|
|
|
|
zajeteRegaly.append((x,y))
|
|
|
|
return zajeteRegaly
|
2020-05-18 12:53:51 +02:00
|
|
|
|
|
|
|
def dwieNajlepsze(populacja, data):
|
|
|
|
tmpPopulacja = populacja[:]
|
|
|
|
chromFitness = []
|
|
|
|
|
|
|
|
for chrom in populacja:
|
|
|
|
chromFitness.append(fitness(chrom,data))
|
|
|
|
|
|
|
|
bestValue = min(chromFitness)
|
|
|
|
bestChromIndex = chromFitness.index(bestValue)
|
|
|
|
pierwsza = tmpPopulacja[bestChromIndex]
|
|
|
|
if (data.best == None):
|
|
|
|
data.best = (pierwsza[:],bestValue)
|
|
|
|
elif(data.best[1] > bestValue):
|
|
|
|
data.best = (pierwsza[:],bestValue)
|
2020-05-18 18:55:18 +02:00
|
|
|
data.doWykresu.append(bestValue)
|
2020-05-18 12:53:51 +02:00
|
|
|
|
|
|
|
tmpPopulacja.pop(bestChromIndex)
|
|
|
|
chromFitness.pop(bestChromIndex)
|
|
|
|
|
|
|
|
bestValue = min(chromFitness)
|
|
|
|
bestChromIndex = chromFitness.index(bestValue)
|
|
|
|
druga = tmpPopulacja[bestChromIndex]
|
|
|
|
tmpPopulacja.pop(bestChromIndex)
|
|
|
|
chromFitness.pop(bestChromIndex)
|
|
|
|
|
|
|
|
|
|
|
|
return (pierwsza, druga)
|
|
|
|
|
|
|
|
def genPopulacje(pierwszy, drugi, ileWPopulacji, fragmentLiczba, wspMutacji):
|
|
|
|
ileWChrom = len(pierwszy)
|
|
|
|
fragment = round(fragmentLiczba*ileWChrom)
|
|
|
|
nowaPopulacja = []
|
|
|
|
|
|
|
|
for i in range(ileWPopulacji):
|
|
|
|
nowaPopulacja.append(crossover(pierwszy,drugi,fragment, wspMutacji))
|
|
|
|
|
|
|
|
return nowaPopulacja
|
|
|
|
|
|
|
|
def crossover(pierwszy, drugi, fragmentLiczba, wspMutacji):
|
|
|
|
ileWChrom = len(pierwszy)
|
|
|
|
tmp = random.randint(0, ileWChrom-fragmentLiczba)
|
|
|
|
kordyFragment = (tmp,tmp+fragmentLiczba)
|
|
|
|
nowyChrom = [None] * ileWChrom
|
|
|
|
iterator = kordyFragment[1]
|
|
|
|
pomIterator = kordyFragment[1]
|
|
|
|
nowyChrom[kordyFragment[0]:kordyFragment[1]] = pierwszy[kordyFragment[0]:kordyFragment[1]]
|
|
|
|
|
|
|
|
while(None in nowyChrom):
|
|
|
|
if(iterator > ileWChrom - 1):
|
|
|
|
iterator = 0
|
|
|
|
if(pomIterator > ileWChrom - 1):
|
|
|
|
pomIterator = 0
|
|
|
|
if(nowyChrom[iterator] == None and drugi[pomIterator] not in nowyChrom):
|
|
|
|
nowyChrom[iterator] = drugi[pomIterator]
|
|
|
|
iterator += 1
|
|
|
|
pomIterator += 1
|
|
|
|
elif(nowyChrom[iterator] == None and drugi[pomIterator] in nowyChrom):
|
|
|
|
pomIterator +=1
|
|
|
|
nowyChrom = mutate(wspMutacji, nowyChrom)
|
|
|
|
return nowyChrom
|
|
|
|
|
|
|
|
|
|
|
|
def updateMap(data, map, mapForAstar, regals):
|
|
|
|
data.mapa = map
|
|
|
|
|
|
|
|
znajdzUnbox(data, map)
|
|
|
|
data.zajeteRegaly = znajdzBox(map, regals)
|
|
|
|
data.astarMap = data.genMap(mapForAstar)
|
|
|
|
|
|
|
|
def mutate(wspMutacji, chrom):
|
|
|
|
ileWChrom = len(chrom)
|
|
|
|
ileZmian = round(ileWChrom * wspMutacji)
|
|
|
|
for i in range(ileZmian):
|
|
|
|
pom = None
|
|
|
|
pierw = random.randint(0,ileWChrom - 1)
|
|
|
|
drug = random.randint(0,ileWChrom - 1)
|
|
|
|
pom = chrom[pierw]
|
|
|
|
chrom[pierw] = chrom[drug]
|
|
|
|
chrom[drug] = pom
|
|
|
|
return chrom
|