forked from s444399/AI
87 lines
2.1 KiB
Python
87 lines
2.1 KiB
Python
|
from Mapa.unboxOnTheFloor import UnboxOnTheFloor
|
||
|
from genetyczny.Gene import Gene
|
||
|
from Mapa.box import Box
|
||
|
from AStar import AStar
|
||
|
import random
|
||
|
|
||
|
|
||
|
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
|
||
|
def wybierzUnbox(data):
|
||
|
return random.choice(data.unbox)
|
||
|
|
||
|
|
||
|
def policzCost(mapaBoxy, poczatek, koniec):
|
||
|
astar = AStar()
|
||
|
koszt = astar.search(poczatek, koniec, mapaBoxy, 1, 0)
|
||
|
return koszt
|
||
|
|
||
|
def generateChromosome(data, mapaBoxy, wheelxy):
|
||
|
chromosome = []
|
||
|
regaly = data.zajeteRegaly
|
||
|
random.shuffle(regaly)
|
||
|
|
||
|
|
||
|
for r in regaly:
|
||
|
gen0 = Gene(wheelxy,r,"poczatek-regal")
|
||
|
gen0.koszt = policzCost(mapaBoxy, wheelxy, r)
|
||
|
chromosome.append(gen0)
|
||
|
|
||
|
unbox = wybierzUnbox(data)
|
||
|
gen1 = Gene(r,unbox,"regal-unbox")
|
||
|
gen1.koszt = policzCost(mapaBoxy, r, unbox)
|
||
|
chromosome.append(gen1)
|
||
|
|
||
|
return chromosome
|
||
|
|
||
|
def generatePopulation(data, mapaBoxy, wheelxy, ileWPopulacji):
|
||
|
populacja = []
|
||
|
for i in range(ileWPopulacji):
|
||
|
populacja.append(generateChromosome(data, mapaBoxy, wheelxy))
|
||
|
|
||
|
return populacja
|
||
|
|
||
|
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
|