Esempio n. 1
0
 def next(self):
     if self.the_list[self.my_index] > self.the_list[self.my_index + 1]:
         self.the_list[self.my_index], self.the_list[self.my_index+1] = \
           self.the_list[self.my_index+1], self.the_list[self.my_index]
         if self.my_index > 0: activate(self.actors[self.my_index - 1])
         if self.my_index < self.N - 2:
             activate(self.actors[self.my_index + 1])
Esempio n. 2
0
def shortest_path(d):
    R = range(len(d))
    actors = [[[[] for k in R] for j in R] for i in R]
    for i in R:
        for j in R:
            for k in R:
                actors[i][j][k] = triangle_inequality(d, i, j, k)
                actors[i][j][k].actors = actors
                activate(actors[i][j][k])
    run()
Esempio n. 3
0
def sort_simple(the_list):
    initial_list = list(the_list)
    N = len(the_list)
    actors = [sort_pair(the_list, my_index) for my_index in range(N - 1)]
    for actor in actors:
        actor.actors = actors
        activate(actor)
    run()
    initial_list.sort()
    assert the_list == initial_list
Esempio n. 4
0
    def __init__(self, left_index, right_index, the_list):
        """
        Parameters
        ----------
        left_index: int
          The index into the_list where this sublist starts.
        right_index: int
          The index into the_list where this sublist ends.

        """
        self.left_index, self.right_index, self.the_list = left_index, right_index, the_list
        self.left_actor = None
        self.right_actor = None
        activate(self)
Esempio n. 5
0
 def next(self):
     self.l[self.i:self.j] = sorted(self.l[self.i:self.j])
     if (self.l[self.i - 1] > self.l[self.i]):
         # The left boundary is out of order. So order it and then
         # activate all agents registered with the left signal and oneself.
         self.l[self.i -
                1], self.l[self.i] = self.l[self.i], self.l[self.i - 1]
         self.signals[self.i].activate()
         activate(self)
     if (self.j < len(self.l)) and (self.l[self.j - 1] > self.l[self.j]):
         # The right boundary is out of order. So order it and then
         # activate all agents registered with the right signal and oneself.
         self.l[self.j -
                1], self.l[self.j] = self.l[self.j], self.l[self.j - 1]
         self.signals[self.j - 1].activate()
         activate(self)
Esempio n. 6
0
    def __init__(self, i, j, l, signals, name):
        """
        Parameters
        ----------
           i, j: int
              i, j are the left and right indexes of this sublist and l is the
              main list.
           signals: list of Shared
              signals[k] is a Shared variable for all k. An agent responsible for
              sublist [i:j] registers with the shared variable signals[i-1] and
              signals[j]. The neighboring agent to the left activates all agents
              registered with signals[i-1] when list[i] is changed. Similarly,
              the neighboring agent to the right activates all agents
              registered with signals[j] when list[j-1] is changed.

        """
        self.i, self.j, self.l = i, j, l
        self.signals = signals
        self.name = name
        signals[i - 1].register(self)
        signals[j].register(self)
        activate(self)
Esempio n. 7
0
 def next(self):
     self.the_list[self.left_index : self.right_index] = \
       sorted(self.the_list[self.left_index : self.right_index])
     # If the leftmost element is out of order then order it and activate
     # oneself and the left neighboring agent.
     if ((self.left_index > 1) and (self.the_list[self.left_index - 1] >
                                    self.the_list[self.left_index])):
         self.the_list[self.left_index-1], self.the_list[self.left_index] = \
           self.the_list[self.left_index], self.the_list[self.left_index-1]
         if self.left_actor: activate(self.left_actor)
         activate(self)
     # If the rightmost element is out of order then order it and activate
     # oneself and the right neighboring agent.
     if ((self.right_index < len(self.the_list))
             and (self.the_list[self.right_index - 1] >
                  self.the_list[self.right_index])):
         self.the_list[self.right_index-1], self.the_list[self.right_index] = \
           self.the_list[self.right_index], self.the_list[self.right_index-1]
         if self.right_actor: activate(self.right_actor)
         activate(self)
Esempio n. 8
0
 def next(self):
     if self.d[self.i][self.k] > self.d[self.i][self.j] + self.d[self.j][self.k]:
         self.d[self.i][self.k] = self.d[self.i][self.j] + self.d[self.j][self.k]
         for r in range(len(self.d)):
             activate(self.actors[self.i][self.k][r])
             activate(self.actors[r][self.i][self.k])