Browse Source

It's not Perfect, but it's Fine

master
iamsosmart19 4 years ago
parent
commit
f580594b10
  1. 109
      main.py

109
main.py

@ -8,8 +8,8 @@ import time
import math import math
from queue import PriorityQueue as pq from queue import PriorityQueue as pq
DEBUG=True # DEBUG=True
# DEBUG=False DEBUG=False
class SquareType(Enum): class SquareType(Enum):
AISLE = 0 AISLE = 0
@ -25,6 +25,8 @@ entries=[(0,3)]
# entries=[(7,3),(7,43)] # entries=[(7,3),(7,43)]
# entries=[(7,3)] # entries=[(7,3)]
passengers = None
class GridSquare: class GridSquare:
#typ: SquareType, occupant: index #typ: SquareType, occupant: index
@ -42,6 +44,9 @@ def makeGrid():
class Passenger: class Passenger:
shuffled = 0 shuffled = 0
inter = None inter = None
interback = None
path = []
recalc = True
#dest and curr are both 2-tuples #dest and curr are both 2-tuples
def __init__(self, dest, curr): def __init__(self, dest, curr):
self.dest = dest self.dest = dest
@ -57,7 +62,9 @@ def mooreNeighbourhood(pos):
i[0] >= 0 and i[1] >= 0 and i[0] < height and i[1] < width] i[0] >= 0 and i[1] >= 0 and i[0] < height and i[1] < width]
def nextSquare(passenger, grid): def nextSquare(passenger, grid):
passenger.path=[]
dist=[[0 for x in range(0,width)] for y in range(0,height)] dist=[[0 for x in range(0,width)] for y in range(0,height)]
prev=[[None for x in range(0,width)] for y in range(0,height)]
dest = None dest = None
@ -75,15 +82,20 @@ def nextSquare(passenger, grid):
mn = mooreNeighbourhood(passenger.curr) mn = mooreNeighbourhood(passenger.curr)
for (i, man) in enumerate(mn): for (i, man) in enumerate(mn):
if (grid[passenger.curr[0]][passenger.curr[1]].typ != SquareType.SEAT or man[1] == passenger.curr[1]) and grid[man[0]][man[1]].typ != SquareType.WALL: if (grid[passenger.curr[0]][passenger.curr[1]].typ != SquareType.SEAT or man[1] == passenger.curr[1]) and grid[man[0]][man[1]].typ != SquareType.WALL and not (grid[man[0]][man[1]].typ == SquareType.AISLE and grid[man[0]][man[1]].occupant is not None):
q.put((1, man, i)) q.put((1, man, i))
dist[man[0]][man[1]] = 1 dist[man[0]][man[1]] = 1
prev[man[0]][man[1]] = (0, passenger.curr, 0)
while not q.empty(): while not q.empty():
u = q.get() u = q.get()
if u[1] == dest: if u[1] == dest:
return mn[u[2]] while prev[u[1][0]][u[1][1]] != None:
passenger.path.append(u[1])
u=prev[u[1][0]][u[1][1]]
return
for x in mooreNeighbourhood(u[1]): for x in mooreNeighbourhood(u[1]):
if not( grid[x[0]][x[1]].typ == SquareType.SEAT and u[1][1] != x[1]) and grid[x[0]][x[1]].typ != SquareType.WALL:
alt = 0 alt = 0
if grid[x[0]][x[1]].typ == SquareType.SEAT or grid[u[1][0]][u[1][1]].typ == SquareType.SEAT: if grid[x[0]][x[1]].typ == SquareType.SEAT or grid[u[1][0]][u[1][1]].typ == SquareType.SEAT:
alt = dist[u[1][0]][u[1][1]] + 5 alt = dist[u[1][0]][u[1][1]] + 5
@ -95,9 +107,10 @@ def nextSquare(passenger, grid):
# print("dist" + str(x) + ": " + str(dist[x[0]][x[1]])) # print("dist" + str(x) + ": " + str(dist[x[0]][x[1]]))
if alt < dist[x[0]][x[1]]: if alt < dist[x[0]][x[1]]:
dist[x[0]][x[1]] = alt dist[x[0]][x[1]] = alt
prev[x[0]][x[1]] = u
q.put((alt, x, u[2])) q.put((alt, x, u[2]))
return passenger.curr passenger.path = [passenger.curr]
def GenPassList(): def GenPassList():
@ -132,39 +145,41 @@ def GenPassList():
inc = (-1)**i * sign * math.floor((i+1)/2) inc = (-1)**i * sign * math.floor((i+1)/2)
if (inc + x.dest[0] > 0) and (inc + x.dest[0] < height) and (grid[x.dest[0]+inc][x.dest[1]].typ == SquareType.AISLE): if (inc + x.dest[0] > 0) and (inc + x.dest[0] < height) and (grid[x.dest[0]+inc][x.dest[1]].typ == SquareType.AISLE):
x.inter=(inc + x.dest[0], x.dest[1]) x.inter=(inc + x.dest[0], x.dest[1])
x.interback = x.inter
return border return border
#boarding order
passengers = GenPassList()
# rnd.seed(605)
# passengers.reverse()
rnd.shuffle(passengers)
toad = 0
btime = 3 #passengers board every btime ticks
def nunty(x): def nunty(x):
if x is None: return -1 if x is None: return -1
return x return x
def tick(t): btime = 3 #passengers board every btime ticks
def tick(t, toad):
done=True done=True
global toad
global btime global btime
# print grid # print grid
if DEBUG: if DEBUG:
print(chr(0x3000), end="")
print(chr(0x3000), end="")
for x in range(0, width): for x in range(0, width):
if x % 10 == 0: if x % 10 == 0:
print(chr(0xFF10 + int(x / 10)), end="") print(chr(0xFF10 + int(x / 10)), end="")
else: else:
print(chr(0x3000), end="") print(chr(0x3000), end="")
print() print()
print(chr(0x3000), end="")
print(chr(0x3000), end="")
for x in range(0, width): for x in range(0, width):
print(chr(0xFF10 + (x % 10)), end="") print(chr(0xFF10 + (x % 10)), end="")
print() print()
for row in grid: for (i, row) in enumerate(grid):
if i % 10 == 0:
print(chr(0xFF10 + int(i / 10)), end="")
else:
print(chr(0x3000), end="")
print(chr(0xFF10 + (i % 10)), end="")
for guy in row: for guy in row:
if guy.occupant is not None: if guy.occupant is not None:
print(chr(0x20000 + guy.occupant), end="") print(chr(0x20000 + guy.occupant), end="")
@ -189,15 +204,34 @@ def tick(t):
if man.inter is not None and man.curr == man.inter: if man.inter is not None and man.curr == man.inter:
man.shuffled = 3 man.shuffled = 3
man.inter = None man.inter = None
man.recalc = True
if man.curr == (-1, -1): if man.curr == (-1, -1):
continue continue
# if i == 36 and DEBUG: # if i == 36 and DEBUG:
# print(nextSquare(man, grid)) # print(nextSquare(man, grid))
if DEBUG:
print(str(man.curr) + ", " + str(man.inter) + ", " + str(man.dest))
done=False done=False
#im sexist #im sexist
nextS=nextSquare(man,grid) if man.recalc:
# if DEBUG:
# print("Recalculating: " + str(man.curr))
nextSquare(man, grid)
man.recalc = False
if len(man.path) == 0:
# if DEBUG:
# print("Recalculating: " + str(man.curr))
nextSquare(man, grid)
nextS = man.path.pop()
if manhattanDistance(nextS, man.curr) > 1:
# if DEBUG:
# print("Recalculating: " + str(man.curr))
nextSquare(man, grid)
nextS = man.path.pop()
if DEBUG:
print(str(man.curr) + ", " + str(man.interback) + ", " + str(man.dest) + ", " + str(nextS) + ", " + str(man.inter))
if passengers[i].shuffled == 0 and not ( grid[nextS[0]][nextS[1]].typ == SquareType.AISLE and grid[nextS[0]][nextS[1]].occupant is not None): if passengers[i].shuffled == 0 and not ( grid[nextS[0]][nextS[1]].typ == SquareType.AISLE and grid[nextS[0]][nextS[1]].occupant is not None):
cp = man.curr cp = man.curr
man.curr = nextS man.curr = nextS
@ -211,16 +245,39 @@ def tick(t):
if man.shuffled != 0: if man.shuffled != 0:
man.shuffled-=1 man.shuffled-=1
if DEBUG: if DEBUG:
time.sleep(0.05) # time.sleep(1.0)
# time.sleep(0.5)
# time.sleep(0.1)
# time.sleep(0.05)
# time.sleep(0.02)
# time.sleep(0.01)
# time.sleep(0.001)
os.system("clear") os.system("clear")
if done and toad >= len(passengers): if done and toad >= len(passengers):
print("Number of ticks: " + str(t)) print("Number of ticks: " + str(t))
exit() return -1
# exit()
return toad
def run(): def run():
toad = 0
t = 0 t = 0
while 1: while toad != -1:
tick(t) toad = tick(t,toad)
t += 1 t += 1
run()
def main():
global passengers
#boarding order
for seediter in range(0,200):
passengers = GenPassList()
print("Seed: " + str(seediter))
rnd.seed(seediter)
# passengers.reverse()
rnd.shuffle(passengers)
run()
if __name__ == "__main__":
main()

Loading…
Cancel
Save