Esempio n. 1
0
 def simulate(self, limit, hz):
     # reset and restart
     time = 0
     self.evtQue = MinBinaryHeap(key=lambda x: x.time)
     self.evtQue.push(Event(time, None, None))
     map(lambda x: self.predict(x, time, limit), self.particles)
     # main loop
     while len(self.evtQue) > 0:
         # get the nearest event
         evt = self.evtQue.pop()
         if not evt.isValid():
             continue
         # update all particles
         for pa in self.particles:
             pa.move(evt.time - self.time)
         self.time = evt.time
         # handle collision and predict the future
         if evt.pa != None and evt.pb != None:
             evt.pa.bounceOff(evt.pb)
             self.predict(evt.pa, time, limit)
             self.predict(evt.pb, time, limit)
         elif evt.pa != None and evt.pb == None:
             evt.pa.bounceoffVerticalWall()
             self.predict(evt.pa, time, limit)
         elif evt.pa == None and evt.pb != None:
             evt.pb.bounceOffHorizontalWall()
             self.predict(evt.pb, time, limit)
         else:
             self.redraw(hz)
Esempio n. 2
0
 def main_2(grp, src):
     vtx = [0] * len(grp)
     dis = [None] * len(grp)
     hp = MinBinaryHeap(key=lambda x: x[1])
     dis[src] = 0
     vtx[src] = 1
     for i, w in grp[src]:
         dis[i] = w
         hp.push((i, dis[i]))
     while len(hp) > 0:
         i, w = hp.pop()
         if vtx[i] == 1 or dis[i] < w:
             continue
         vtx[i] = 1
         for j, v in grp[i]:
             if vtx[j] == 0 and (dis[j] == None or dis[j] > v):
                 dis[j] = v
                 hp.push((j, dis[j]))
     return sum(dis)
Esempio n. 3
0
 def f4(grp, src):
     vtx = [0] * len(grp)
     vtx[src] = 1
     dis = [None] * len(grp)
     dis[src] = 0
     hp = MinBinaryHeap(key=lambda x: x[1])
     for i, w in grp[src]:
         dis[i] = w
         hp.push((i, dis[i]))
     ret = []
     while len(hp) > 0:
         i, w = hp.pop()
         if vtx[i] == 1 or w > dis[i]:
             continue
         vtx[i] = 1
         for j, v in grp[i]:
             if vtx[j] == 0 and (dis[j] is None or dis[j] > dis[i] + v):
                 dis[j] = dis[i] + v
                 hp.push((j, dis[j]))
         ret.append(w)
     return sum(ret)
Esempio n. 4
0
 def main_Dijkstra_2(self, grp, src):
     # 1) initialize
     vtx = [0 if i != src else 1 for i in range(len(grp))]
     dis = [None if i != src else 0 for i in range(len(grp))]
     pre = [None] * len(grp)
     hp = MinBinaryHeap(key=lambda x: x[1])
     for i, w in grp[src]:
         dis[i] = w
         pre[i] = src
         hp.push((i, dis[i]))
     # 2) calculate by greedily selecting vertexes from heap
     while len(hp) > 0:
         i, w = hp.pop()
         assert (dis[i] is not None and w >= dis[i])
         if w > dis[i] or vtx[i] != 0:
             continue
         vtx[i] = 1
         for j, v in grp[i]:
             if vtx[j] == 0 and (dis[j] is None or dis[j] > dis[i] + v):
                 dis[j] = dis[i] + v
                 pre[j] = i
                 hp.push((j, dis[j]))
     # 3) build shortest-path tree
     return dis, pre